Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
* License, v. 2.0. If a copy of the MPL was not distributed with this
5
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef mozilla_dom_MessagePort_h
8
#define mozilla_dom_MessagePort_h
9
10
#include "mozilla/Attributes.h"
11
#include "mozilla/DOMEventTargetHelper.h"
12
#include "mozilla/dom/DOMTypes.h"
13
#include "nsAutoPtr.h"
14
#include "nsTArray.h"
15
16
#ifdef XP_WIN
17
# undef PostMessage
18
#endif
19
20
class nsIGlobalObject;
21
22
namespace mozilla {
23
namespace dom {
24
25
class MessagePortChild;
26
struct PostMessageOptions;
27
class PostMessageRunnable;
28
class SharedMessagePortMessage;
29
class StrongWorkerRef;
30
31
// A class to hold a MessagePortIdentifier from
32
// MessagePort::CloneAndDistentangle() and close if neither passed to
33
// MessagePort::Create() nor release()ed to send via IPC.
34
// When the `neutered` field of the MessagePortIdentifier is false, a close is
35
// required.
36
// This does not derive from MessagePortIdentifier because
37
// MessagePortIdentifier is final and because use of UniqueMessagePortId as a
38
// MessagePortIdentifier is intentionally prevented without release of
39
// ownership.
40
class UniqueMessagePortId final {
41
public:
42
UniqueMessagePortId() { mIdentifier.neutered() = true; }
43
explicit UniqueMessagePortId(const MessagePortIdentifier& aIdentifier)
44
: mIdentifier(aIdentifier) {}
45
UniqueMessagePortId(UniqueMessagePortId&& aOther) noexcept
46
: mIdentifier(aOther.mIdentifier) {
47
aOther.mIdentifier.neutered() = true;
48
}
49
~UniqueMessagePortId() { ForceClose(); };
50
void ForceClose();
51
52
MOZ_MUST_USE MessagePortIdentifier release() {
53
MessagePortIdentifier id = mIdentifier;
54
mIdentifier.neutered() = true;
55
return id;
56
}
57
// const member accessors are not required because a const
58
// UniqueMessagePortId is not useful.
59
nsID& uuid() { return mIdentifier.uuid(); }
60
nsID& destinationUuid() { return mIdentifier.destinationUuid(); }
61
uint32_t& sequenceId() { return mIdentifier.sequenceId(); }
62
bool& neutered() { return mIdentifier.neutered(); }
63
64
UniqueMessagePortId(const UniqueMessagePortId& aOther) = delete;
65
void operator=(const UniqueMessagePortId& aOther) = delete;
66
67
private:
68
MessagePortIdentifier mIdentifier;
69
};
70
71
class MessagePort final : public DOMEventTargetHelper {
72
friend class PostMessageRunnable;
73
74
public:
75
NS_DECL_ISUPPORTS_INHERITED
76
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(MessagePort, DOMEventTargetHelper)
77
78
static already_AddRefed<MessagePort> Create(nsIGlobalObject* aGlobal,
79
const nsID& aUUID,
80
const nsID& aDestinationUUID,
81
ErrorResult& aRv);
82
83
static already_AddRefed<MessagePort> Create(nsIGlobalObject* aGlobal,
84
UniqueMessagePortId& aIdentifier,
85
ErrorResult& aRv);
86
87
// For IPC.
88
static void ForceClose(const MessagePortIdentifier& aIdentifier);
89
90
virtual JSObject* WrapObject(JSContext* aCx,
91
JS::Handle<JSObject*> aGivenProto) override;
92
93
void PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage,
94
const Sequence<JSObject*>& aTransferable, ErrorResult& aRv);
95
96
void PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage,
97
const PostMessageOptions& aOptions, ErrorResult& aRv);
98
99
void Start();
100
101
void Close();
102
103
EventHandlerNonNull* GetOnmessage();
104
105
void SetOnmessage(EventHandlerNonNull* aCallback);
106
107
IMPL_EVENT_HANDLER(messageerror)
108
109
// Non WebIDL methods
110
111
void UnshippedEntangle(MessagePort* aEntangledPort);
112
113
bool CanBeCloned() const { return !mHasBeenTransferredOrClosed; }
114
115
void CloneAndDisentangle(UniqueMessagePortId& aIdentifier);
116
117
void CloseForced();
118
119
// These methods are useful for MessagePortChild
120
121
void Entangled(nsTArray<ClonedMessageData>& aMessages);
122
void MessagesReceived(nsTArray<ClonedMessageData>& aMessages);
123
void StopSendingDataConfirmed();
124
void Closed();
125
126
private:
127
enum State {
128
// When a port is created by a MessageChannel it is entangled with the
129
// other. They both run on the same thread, same event loop and the
130
// messages are added to the queues without using PBackground actors.
131
// When one of the port is shipped, the state is changed to
132
// StateEntangling.
133
eStateUnshippedEntangled,
134
135
// If the port is closed or cloned when we are in this state, we go in one
136
// of the following 2 steps. EntanglingForClose or ForDisentangle.
137
eStateEntangling,
138
139
// We are not fully entangled yet but are already disentangled.
140
eStateEntanglingForDisentangle,
141
142
// We are not fully entangled yet but are already closed.
143
eStateEntanglingForClose,
144
145
// When entangled() is received we send all the messages in the
146
// mMessagesForTheOtherPort to the actor and we change the state to
147
// StateEntangled. At this point the port is entangled with the other. We
148
// send and receive messages.
149
// If the port queue is not enabled, the received messages are stored in
150
// the mMessages.
151
eStateEntangled,
152
153
// When the port is cloned or disentangled we want to stop receiving
154
// messages. We call 'SendStopSendingData' to the actor and we wait for an
155
// answer. All the messages received between now and the
156
// 'StopSendingDataComfirmed are queued in the mMessages but not
157
// dispatched.
158
eStateDisentangling,
159
160
// When 'StopSendingDataConfirmed' is received, we can disentangle the port
161
// calling SendDisentangle in the actor because we are 100% sure that we
162
// don't receive any other message, so nothing will be lost.
163
// Disentangling the port we send all the messages from the mMessages
164
// though the actor.
165
eStateDisentangled,
166
167
// We are here if Close() has been called. We are disentangled but we can
168
// still send pending messages.
169
eStateDisentangledForClose
170
};
171
172
explicit MessagePort(nsIGlobalObject* aGlobal, State aState);
173
~MessagePort();
174
175
void DisconnectFromOwner() override;
176
177
void Initialize(const nsID& aUUID, const nsID& aDestinationUUID,
178
uint32_t aSequenceID, bool aNeutered, ErrorResult& aRv);
179
180
bool ConnectToPBackground();
181
182
// Dispatch events from the Message Queue using a nsRunnable.
183
void Dispatch();
184
185
void DispatchError();
186
187
void StartDisentangling();
188
void Disentangle();
189
190
void RemoveDocFromBFCache();
191
192
void CloseInternal(bool aSoftly);
193
194
// This method is meant to keep alive the MessagePort when this object is
195
// creating the actor and until the actor is entangled.
196
// We release the object when the port is closed or disentangled.
197
void UpdateMustKeepAlive();
198
199
bool IsCertainlyAliveForCC() const override { return mIsKeptAlive; }
200
201
RefPtr<StrongWorkerRef> mWorkerRef;
202
203
RefPtr<PostMessageRunnable> mPostMessageRunnable;
204
205
RefPtr<MessagePortChild> mActor;
206
207
RefPtr<MessagePort> mUnshippedEntangledPort;
208
209
nsTArray<RefPtr<SharedMessagePortMessage>> mMessages;
210
nsTArray<RefPtr<SharedMessagePortMessage>> mMessagesForTheOtherPort;
211
212
nsAutoPtr<MessagePortIdentifier> mIdentifier;
213
214
State mState;
215
216
bool mMessageQueueEnabled;
217
218
bool mIsKeptAlive;
219
220
// mHasBeenTransferredOrClosed is used to know if this port has been manually
221
// closed or transferred via postMessage. Note that if the entangled port is
222
// closed, this port is closed as well (see mState) but, just because close()
223
// has not been called directly, by spec, this port can still be transferred.
224
bool mHasBeenTransferredOrClosed;
225
};
226
227
} // namespace dom
228
} // namespace mozilla
229
230
#endif // mozilla_dom_MessagePort_h