2015-06-17 13:44:27 +03: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"
|
2019-12-03 03:37:23 +03:00
|
|
|
#include "mozilla/dom/DOMTypes.h"
|
2020-04-04 00:05:17 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2015-06-17 13:44:27 +03:00
|
|
|
#include "nsTArray.h"
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
# undef PostMessage
|
|
|
|
#endif
|
|
|
|
|
2016-03-01 17:21:11 +03:00
|
|
|
class nsIGlobalObject;
|
2015-06-17 13:44:27 +03:00
|
|
|
|
2022-05-09 23:41:11 +03:00
|
|
|
namespace mozilla::dom {
|
2015-06-17 13:44:27 +03:00
|
|
|
|
2020-01-24 10:58:21 +03:00
|
|
|
class MessageData;
|
2015-06-17 13:44:27 +03:00
|
|
|
class MessagePortChild;
|
2015-11-18 02:38:01 +03:00
|
|
|
class PostMessageRunnable;
|
2021-09-28 22:59:51 +03:00
|
|
|
class RefMessageBodyService;
|
2021-09-29 15:48:17 +03:00
|
|
|
class SharedMessageBody;
|
2018-03-13 23:16:54 +03:00
|
|
|
class StrongWorkerRef;
|
2021-09-29 15:48:17 +03:00
|
|
|
struct StructuredSerializeOptions;
|
2015-06-17 13:44:27 +03:00
|
|
|
|
2019-12-03 03:37:23 +03:00
|
|
|
// A class to hold a MessagePortIdentifier from
|
|
|
|
// MessagePort::CloneAndDistentangle() and close if neither passed to
|
|
|
|
// MessagePort::Create() nor release()ed to send via IPC.
|
|
|
|
// When the `neutered` field of the MessagePortIdentifier is false, a close is
|
|
|
|
// required.
|
|
|
|
// This does not derive from MessagePortIdentifier because
|
|
|
|
// MessagePortIdentifier is final and because use of UniqueMessagePortId as a
|
|
|
|
// MessagePortIdentifier is intentionally prevented without release of
|
|
|
|
// ownership.
|
|
|
|
class UniqueMessagePortId final {
|
|
|
|
public:
|
|
|
|
UniqueMessagePortId() { mIdentifier.neutered() = true; }
|
|
|
|
explicit UniqueMessagePortId(const MessagePortIdentifier& aIdentifier)
|
|
|
|
: mIdentifier(aIdentifier) {}
|
|
|
|
UniqueMessagePortId(UniqueMessagePortId&& aOther) noexcept
|
|
|
|
: mIdentifier(aOther.mIdentifier) {
|
|
|
|
aOther.mIdentifier.neutered() = true;
|
|
|
|
}
|
|
|
|
~UniqueMessagePortId() { ForceClose(); };
|
|
|
|
void ForceClose();
|
|
|
|
|
2021-03-10 11:19:25 +03:00
|
|
|
[[nodiscard]] MessagePortIdentifier release() {
|
2019-12-03 03:37:23 +03:00
|
|
|
MessagePortIdentifier id = mIdentifier;
|
|
|
|
mIdentifier.neutered() = true;
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
// const member accessors are not required because a const
|
|
|
|
// UniqueMessagePortId is not useful.
|
|
|
|
nsID& uuid() { return mIdentifier.uuid(); }
|
|
|
|
nsID& destinationUuid() { return mIdentifier.destinationUuid(); }
|
|
|
|
uint32_t& sequenceId() { return mIdentifier.sequenceId(); }
|
|
|
|
bool& neutered() { return mIdentifier.neutered(); }
|
|
|
|
|
|
|
|
UniqueMessagePortId(const UniqueMessagePortId& aOther) = delete;
|
|
|
|
void operator=(const UniqueMessagePortId& aOther) = delete;
|
|
|
|
|
|
|
|
private:
|
|
|
|
MessagePortIdentifier mIdentifier;
|
|
|
|
};
|
|
|
|
|
2015-09-16 06:27:56 +03:00
|
|
|
class MessagePort final : public DOMEventTargetHelper {
|
2015-11-18 02:38:01 +03:00
|
|
|
friend class PostMessageRunnable;
|
2015-06-17 13:44:27 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2015-06-29 19:42:41 +03:00
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(MessagePort, DOMEventTargetHelper)
|
2015-06-17 13:44:27 +03:00
|
|
|
|
|
|
|
static already_AddRefed<MessagePort> Create(nsIGlobalObject* aGlobal,
|
2016-03-01 17:21:11 +03:00
|
|
|
const nsID& aUUID,
|
2015-06-17 13:44:27 +03:00
|
|
|
const nsID& aDestinationUUID,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2019-12-03 03:37:23 +03:00
|
|
|
static already_AddRefed<MessagePort> Create(nsIGlobalObject* aGlobal,
|
|
|
|
UniqueMessagePortId& aIdentifier,
|
|
|
|
ErrorResult& aRv);
|
2015-06-17 13:44:27 +03:00
|
|
|
|
2015-11-18 02:38:01 +03:00
|
|
|
// For IPC.
|
2015-06-24 01:50:00 +03:00
|
|
|
static void ForceClose(const MessagePortIdentifier& aIdentifier);
|
|
|
|
|
2015-06-17 13:44:27 +03:00
|
|
|
virtual JSObject* WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) override;
|
|
|
|
|
|
|
|
void PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage,
|
2017-02-03 13:00:38 +03:00
|
|
|
const Sequence<JSObject*>& aTransferable, ErrorResult& aRv);
|
2015-06-17 13:44:27 +03:00
|
|
|
|
2018-10-29 18:26:30 +03:00
|
|
|
void PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage,
|
2021-09-29 15:48:17 +03:00
|
|
|
const StructuredSerializeOptions& aOptions,
|
|
|
|
ErrorResult& aRv);
|
2018-10-29 18:26:30 +03:00
|
|
|
|
2015-09-16 06:27:56 +03:00
|
|
|
void Start();
|
2015-06-17 13:44:27 +03:00
|
|
|
|
2015-09-16 06:27:56 +03:00
|
|
|
void Close();
|
2015-06-17 13:44:27 +03:00
|
|
|
|
2015-09-16 06:27:56 +03:00
|
|
|
EventHandlerNonNull* GetOnmessage();
|
2015-06-17 13:44:27 +03:00
|
|
|
|
2015-09-16 06:27:56 +03:00
|
|
|
void SetOnmessage(EventHandlerNonNull* aCallback);
|
2015-06-17 13:44:27 +03:00
|
|
|
|
2017-09-13 06:59:05 +03:00
|
|
|
IMPL_EVENT_HANDLER(messageerror)
|
|
|
|
|
2015-06-17 13:44:27 +03:00
|
|
|
// Non WebIDL methods
|
|
|
|
|
2024-03-05 03:12:39 +03:00
|
|
|
void UnshippedEntangle(RefPtr<MessagePort>& aEntangledPort);
|
2015-06-17 13:44:27 +03:00
|
|
|
|
2018-03-28 10:23:17 +03:00
|
|
|
bool CanBeCloned() const { return !mHasBeenTransferredOrClosed; }
|
|
|
|
|
2019-12-03 03:37:23 +03:00
|
|
|
void CloneAndDisentangle(UniqueMessagePortId& aIdentifier);
|
2015-06-17 13:44:27 +03:00
|
|
|
|
2015-11-18 02:38:01 +03:00
|
|
|
void CloseForced();
|
|
|
|
|
2015-06-17 13:44:27 +03:00
|
|
|
// These methods are useful for MessagePortChild
|
|
|
|
|
2020-01-24 10:58:21 +03:00
|
|
|
void Entangled(nsTArray<MessageData>& aMessages);
|
|
|
|
void MessagesReceived(nsTArray<MessageData>& aMessages);
|
2015-06-17 13:44:27 +03:00
|
|
|
void StopSendingDataConfirmed();
|
|
|
|
void Closed();
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum State {
|
2024-03-05 03:12:39 +03:00
|
|
|
// The plan is to be eStateUnshippedEntangled once we are told about our
|
|
|
|
// unshipped entangled counterpart.
|
|
|
|
eStateInitializingUnshippedEntangled,
|
|
|
|
|
2015-06-17 13:44:27 +03:00
|
|
|
// 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-18 02:38:01 +03: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 13:44:27 +03:00
|
|
|
eStateEntangling,
|
|
|
|
|
2015-11-18 02:38:01 +03: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 13:44:27 +03: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-18 02:38:01 +03:00
|
|
|
eStateDisentangled,
|
|
|
|
|
|
|
|
// We are here if Close() has been called. We are disentangled but we can
|
|
|
|
// still send pending messages.
|
|
|
|
eStateDisentangledForClose
|
2015-06-17 13:44:27 +03:00
|
|
|
};
|
|
|
|
|
2018-06-16 17:21:46 +03:00
|
|
|
explicit MessagePort(nsIGlobalObject* aGlobal, State aState);
|
|
|
|
~MessagePort();
|
|
|
|
|
|
|
|
void DisconnectFromOwner() override;
|
|
|
|
|
2015-06-17 13:44:27 +03:00
|
|
|
void Initialize(const nsID& aUUID, const nsID& aDestinationUUID,
|
2018-07-13 18:41:34 +03:00
|
|
|
uint32_t aSequenceID, bool aNeutered, ErrorResult& aRv);
|
2015-06-17 13:44:27 +03:00
|
|
|
|
2017-10-25 09:45:52 +03:00
|
|
|
bool ConnectToPBackground();
|
2015-06-17 13:44:27 +03:00
|
|
|
|
|
|
|
// Dispatch events from the Message Queue using a nsRunnable.
|
|
|
|
void Dispatch();
|
|
|
|
|
2017-09-13 06:59:05 +03:00
|
|
|
void DispatchError();
|
|
|
|
|
2015-06-17 13:44:27 +03:00
|
|
|
void StartDisentangling();
|
|
|
|
void Disentangle();
|
|
|
|
|
|
|
|
void RemoveDocFromBFCache();
|
|
|
|
|
2015-11-18 02:38:01 +03:00
|
|
|
void CloseInternal(bool aSoftly);
|
|
|
|
|
2015-06-17 13:44:27 +03: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 19:42:41 +03:00
|
|
|
bool IsCertainlyAliveForCC() const override { return mIsKeptAlive; }
|
|
|
|
|
2018-03-13 23:16:54 +03:00
|
|
|
RefPtr<StrongWorkerRef> mWorkerRef;
|
2015-06-17 13:44:27 +03:00
|
|
|
|
2016-02-11 22:42:13 +03:00
|
|
|
RefPtr<PostMessageRunnable> mPostMessageRunnable;
|
2015-06-17 13:44:27 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MessagePortChild> mActor;
|
2015-06-17 13:44:27 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MessagePort> mUnshippedEntangledPort;
|
2015-06-17 13:44:27 +03:00
|
|
|
|
2020-01-24 10:58:34 +03:00
|
|
|
RefPtr<RefMessageBodyService> mRefMessageBodyService;
|
|
|
|
|
2020-01-24 10:59:36 +03:00
|
|
|
nsTArray<RefPtr<SharedMessageBody>> mMessages;
|
|
|
|
nsTArray<RefPtr<SharedMessageBody>> mMessagesForTheOtherPort;
|
2015-06-17 13:44:27 +03:00
|
|
|
|
2020-04-04 00:05:17 +03:00
|
|
|
UniquePtr<MessagePortIdentifier> mIdentifier;
|
2015-06-17 13:44:27 +03:00
|
|
|
|
|
|
|
State mState;
|
|
|
|
|
|
|
|
bool mMessageQueueEnabled;
|
|
|
|
|
|
|
|
bool mIsKeptAlive;
|
2018-03-28 10:23:17 +03:00
|
|
|
|
|
|
|
// mHasBeenTransferredOrClosed is used to know if this port has been manually
|
|
|
|
// closed or transferred via postMessage. Note that if the entangled port is
|
|
|
|
// closed, this port is closed as well (see mState) but, just because close()
|
|
|
|
// has not been called directly, by spec, this port can still be transferred.
|
|
|
|
bool mHasBeenTransferredOrClosed;
|
2015-06-17 13:44:27 +03:00
|
|
|
};
|
|
|
|
|
2022-05-09 23:41:11 +03:00
|
|
|
} // namespace mozilla::dom
|
2015-06-17 13:44:27 +03:00
|
|
|
|
|
|
|
#endif // mozilla_dom_MessagePort_h
|