2015-06-17 10:44:27 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_MessagePort_h
|
|
|
|
#define mozilla_dom_MessagePort_h
|
|
|
|
|
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "mozilla/DOMEventTargetHelper.h"
|
|
|
|
#include "nsIIPCBackgroundChildCreateCallback.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
#undef PostMessage
|
|
|
|
#endif
|
|
|
|
|
|
|
|
class nsPIDOMWindow;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
class DispatchEventRunnable;
|
|
|
|
class MessagePortChild;
|
|
|
|
class MessagePortIdentifier;
|
|
|
|
class MessagePortMessage;
|
2015-11-17 23:38:01 +00:00
|
|
|
class PostMessageRunnable;
|
2015-06-17 10:44:27 +00:00
|
|
|
class SharedMessagePortMessage;
|
|
|
|
|
|
|
|
namespace workers {
|
|
|
|
class WorkerFeature;
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace workers
|
2015-06-17 10:44:27 +00:00
|
|
|
|
2015-09-16 03:27:56 +00:00
|
|
|
class MessagePort final : public DOMEventTargetHelper
|
2015-06-17 10:44:27 +00:00
|
|
|
, public nsIIPCBackgroundChildCreateCallback
|
|
|
|
, public nsIObserver
|
|
|
|
{
|
|
|
|
friend class DispatchEventRunnable;
|
2015-11-17 23:38:01 +00:00
|
|
|
friend class PostMessageRunnable;
|
2015-06-17 10:44:27 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
NS_DECL_NSIIPCBACKGROUNDCHILDCREATECALLBACK
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2015-06-29 16:42:41 +00:00
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(MessagePort,
|
2015-09-16 03:27:56 +00:00
|
|
|
DOMEventTargetHelper)
|
2015-06-17 10:44:27 +00:00
|
|
|
|
|
|
|
static already_AddRefed<MessagePort>
|
|
|
|
Create(nsPIDOMWindow* aWindow, const nsID& aUUID,
|
|
|
|
const nsID& aDestinationUUID, ErrorResult& aRv);
|
|
|
|
|
|
|
|
static already_AddRefed<MessagePort>
|
|
|
|
Create(nsPIDOMWindow* aWindow, const MessagePortIdentifier& aIdentifier,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2015-11-17 23:38:01 +00:00
|
|
|
// For IPC.
|
2015-06-23 22:50:00 +00:00
|
|
|
static void
|
|
|
|
ForceClose(const MessagePortIdentifier& aIdentifier);
|
|
|
|
|
2015-06-17 10:44:27 +00:00
|
|
|
virtual JSObject*
|
|
|
|
WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
|
|
|
|
|
2015-09-16 03:27:56 +00:00
|
|
|
void
|
2015-06-17 10:44:27 +00:00
|
|
|
PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage,
|
|
|
|
const Optional<Sequence<JS::Value>>& aTransferable,
|
2015-09-16 03:27:56 +00:00
|
|
|
ErrorResult& aRv);
|
2015-06-17 10:44:27 +00:00
|
|
|
|
2015-09-16 03:27:56 +00:00
|
|
|
void Start();
|
2015-06-17 10:44:27 +00:00
|
|
|
|
2015-09-16 03:27:56 +00:00
|
|
|
void Close();
|
2015-06-17 10:44:27 +00:00
|
|
|
|
2015-09-16 03:27:56 +00:00
|
|
|
EventHandlerNonNull* GetOnmessage();
|
2015-06-17 10:44:27 +00:00
|
|
|
|
2015-09-16 03:27:56 +00:00
|
|
|
void SetOnmessage(EventHandlerNonNull* aCallback);
|
2015-06-17 10:44:27 +00:00
|
|
|
|
|
|
|
// Non WebIDL methods
|
|
|
|
|
|
|
|
void UnshippedEntangle(MessagePort* aEntangledPort);
|
|
|
|
|
2015-09-16 03:27:56 +00:00
|
|
|
void CloneAndDisentangle(MessagePortIdentifier& aIdentifier);
|
2015-06-17 10:44:27 +00:00
|
|
|
|
2015-11-17 23:38:01 +00:00
|
|
|
void CloseForced();
|
|
|
|
|
2015-06-17 10:44:27 +00:00
|
|
|
// These methods are useful for MessagePortChild
|
|
|
|
|
|
|
|
void Entangled(nsTArray<MessagePortMessage>& aMessages);
|
|
|
|
void MessagesReceived(nsTArray<MessagePortMessage>& aMessages);
|
|
|
|
void StopSendingDataConfirmed();
|
|
|
|
void Closed();
|
|
|
|
|
|
|
|
private:
|
|
|
|
explicit MessagePort(nsPIDOMWindow* aWindow);
|
|
|
|
~MessagePort();
|
|
|
|
|
|
|
|
enum State {
|
|
|
|
// When a port is created by a MessageChannel it is entangled with the
|
|
|
|
// other. They both run on the same thread, same event loop and the
|
|
|
|
// messages are added to the queues without using PBackground actors.
|
|
|
|
// When one of the port is shipped, the state is changed to
|
|
|
|
// StateEntangling.
|
|
|
|
eStateUnshippedEntangled,
|
|
|
|
|
2015-11-17 23:38:01 +00:00
|
|
|
// If the port is closed or cloned when we are in this state, we go in one
|
|
|
|
// of the following 2 steps. EntanglingForClose or ForDisentangle.
|
2015-06-17 10:44:27 +00:00
|
|
|
eStateEntangling,
|
|
|
|
|
2015-11-17 23:38:01 +00:00
|
|
|
// We are not fully entangled yet but are already disentangled.
|
|
|
|
eStateEntanglingForDisentangle,
|
|
|
|
|
|
|
|
// We are not fully entangled yet but are already closed.
|
|
|
|
eStateEntanglingForClose,
|
|
|
|
|
2015-06-17 10:44:27 +00:00
|
|
|
// When entangled() is received we send all the messages in the
|
|
|
|
// mMessagesForTheOtherPort to the actor and we change the state to
|
|
|
|
// StateEntangled. At this point the port is entangled with the other. We
|
|
|
|
// send and receive messages.
|
|
|
|
// If the port queue is not enabled, the received messages are stored in
|
|
|
|
// the mMessages.
|
|
|
|
eStateEntangled,
|
|
|
|
|
|
|
|
// When the port is cloned or disentangled we want to stop receiving
|
|
|
|
// messages. We call 'SendStopSendingData' to the actor and we wait for an
|
|
|
|
// answer. All the messages received between now and the
|
|
|
|
// 'StopSendingDataComfirmed are queued in the mMessages but not
|
|
|
|
// dispatched.
|
|
|
|
eStateDisentangling,
|
|
|
|
|
|
|
|
// When 'StopSendingDataConfirmed' is received, we can disentangle the port
|
|
|
|
// calling SendDisentangle in the actor because we are 100% sure that we
|
|
|
|
// don't receive any other message, so nothing will be lost.
|
|
|
|
// Disentangling the port we send all the messages from the mMessages
|
|
|
|
// though the actor.
|
2015-11-17 23:38:01 +00:00
|
|
|
eStateDisentangled,
|
|
|
|
|
|
|
|
// We are here if Close() has been called. We are disentangled but we can
|
|
|
|
// still send pending messages.
|
|
|
|
eStateDisentangledForClose
|
2015-06-17 10:44:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void Initialize(const nsID& aUUID, const nsID& aDestinationUUID,
|
|
|
|
uint32_t aSequenceID, bool mNeutered, State aState,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
|
|
|
void ConnectToPBackground();
|
|
|
|
|
|
|
|
// Dispatch events from the Message Queue using a nsRunnable.
|
|
|
|
void Dispatch();
|
|
|
|
|
|
|
|
void StartDisentangling();
|
|
|
|
void Disentangle();
|
|
|
|
|
|
|
|
void RemoveDocFromBFCache();
|
|
|
|
|
2015-11-17 23:38:01 +00:00
|
|
|
void CloseInternal(bool aSoftly);
|
|
|
|
|
2015-06-17 10:44:27 +00:00
|
|
|
// This method is meant to keep alive the MessagePort when this object is
|
|
|
|
// creating the actor and until the actor is entangled.
|
|
|
|
// We release the object when the port is closed or disentangled.
|
|
|
|
void UpdateMustKeepAlive();
|
|
|
|
|
2015-06-29 16:42:41 +00:00
|
|
|
bool IsCertainlyAliveForCC() const override
|
|
|
|
{
|
|
|
|
return mIsKeptAlive;
|
|
|
|
}
|
|
|
|
|
2015-06-17 10:44:27 +00:00
|
|
|
nsAutoPtr<workers::WorkerFeature> mWorkerFeature;
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<DispatchEventRunnable> mDispatchRunnable;
|
2015-06-17 10:44:27 +00:00
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<MessagePortChild> mActor;
|
2015-06-17 10:44:27 +00:00
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<MessagePort> mUnshippedEntangledPort;
|
2015-06-17 10:44:27 +00:00
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
nsTArray<RefPtr<SharedMessagePortMessage>> mMessages;
|
|
|
|
nsTArray<RefPtr<SharedMessagePortMessage>> mMessagesForTheOtherPort;
|
2015-06-17 10:44:27 +00:00
|
|
|
|
|
|
|
nsAutoPtr<MessagePortIdentifier> mIdentifier;
|
|
|
|
|
|
|
|
uint64_t mInnerID;
|
|
|
|
|
|
|
|
State mState;
|
|
|
|
|
|
|
|
bool mMessageQueueEnabled;
|
|
|
|
|
|
|
|
bool mIsKeptAlive;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_MessagePort_h
|