author Gijs Kruitbosch <>
Tue, 11 Jul 2017 16:33:10 +0100
changeset 418487 eadb66a18addce2862b1269fa10d288ee6ca4333
parent 387361 c3d718ec7f07dc353021a32f643064718263b8e3
child 429770 78cd991b074fff879264429397f6cb01d7ce8522
permissions -rw-r--r--
Bug 1354086 - fix fonts in overflow menu, combined buttons overflowing in customize mode, r=mikedeboer MozReview-Commit-ID: Ck2Za6FUbeo

/* -*- 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 */

#ifndef mozilla_dom_MessagePort_h
#define mozilla_dom_MessagePort_h

#include "mozilla/Attributes.h"
#include "mozilla/DOMEventTargetHelper.h"
#include "nsAutoPtr.h"
#include "nsIIPCBackgroundChildCreateCallback.h"
#include "nsTArray.h"

#ifdef XP_WIN
#undef PostMessage

class nsIGlobalObject;

namespace mozilla {
namespace dom {

class ClonedMessageData;
class MessagePortChild;
class MessagePortIdentifier;
class PostMessageRunnable;
class SharedMessagePortMessage;

namespace workers {
class WorkerHolder;
} // namespace workers

class MessagePort final : public DOMEventTargetHelper
                        , public nsIIPCBackgroundChildCreateCallback
                        , public nsIObserver
  friend class PostMessageRunnable;


  static already_AddRefed<MessagePort>
  Create(nsIGlobalObject* aGlobal, const nsID& aUUID,
         const nsID& aDestinationUUID, ErrorResult& aRv);

  static already_AddRefed<MessagePort>
  Create(nsIGlobalObject* aGlobal,
         const MessagePortIdentifier& aIdentifier,
         ErrorResult& aRv);

  // For IPC.
  static void
  ForceClose(const MessagePortIdentifier& aIdentifier);

  virtual JSObject*
  WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;

  PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage,
              const Sequence<JSObject*>& aTransferable,
              ErrorResult& aRv);

  void Start();

  void Close();

  EventHandlerNonNull* GetOnmessage();

  void SetOnmessage(EventHandlerNonNull* aCallback);

  // Non WebIDL methods

  void UnshippedEntangle(MessagePort* aEntangledPort);

  void CloneAndDisentangle(MessagePortIdentifier& aIdentifier);

  void CloseForced();

  // These methods are useful for MessagePortChild

  void Entangled(nsTArray<ClonedMessageData>& aMessages);
  void MessagesReceived(nsTArray<ClonedMessageData>& aMessages);
  void StopSendingDataConfirmed();
  void Closed();

  explicit MessagePort(nsIGlobalObject* aGlobal);

  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.

    // 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.

    // We are not fully entangled yet but are already disentangled.

    // We are not fully entangled yet but are already closed.

    // 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.

    // 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.

    // 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.

    // We are here if Close() has been called. We are disentangled but we can
    // still send pending messages.

  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();

  void CloseInternal(bool aSoftly);

  // 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();

  bool IsCertainlyAliveForCC() const override
    return mIsKeptAlive;

  nsAutoPtr<workers::WorkerHolder> mWorkerHolder;

  RefPtr<PostMessageRunnable> mPostMessageRunnable;

  RefPtr<MessagePortChild> mActor;

  RefPtr<MessagePort> mUnshippedEntangledPort;

  nsTArray<RefPtr<SharedMessagePortMessage>> mMessages;
  nsTArray<RefPtr<SharedMessagePortMessage>> mMessagesForTheOtherPort;

  nsAutoPtr<MessagePortIdentifier> mIdentifier;

  uint64_t mInnerID;

  State mState;

  bool mMessageQueueEnabled;

  bool mIsKeptAlive;

} // namespace dom
} // namespace mozilla

#endif // mozilla_dom_MessagePort_h