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/. */
|
|
|
|
|
|
|
|
#include "MessagePort.h"
|
|
|
|
|
|
|
|
#include "MessageEvent.h"
|
|
|
|
#include "MessagePortChild.h"
|
|
|
|
#include "mozilla/dom/BlobBinding.h"
|
|
|
|
#include "mozilla/dom/Event.h"
|
|
|
|
#include "mozilla/dom/File.h"
|
|
|
|
#include "mozilla/dom/MessageChannel.h"
|
2016-01-30 20:05:36 +03:00
|
|
|
#include "mozilla/dom/MessageEventBinding.h"
|
2015-06-17 13:44:27 +03:00
|
|
|
#include "mozilla/dom/MessagePortBinding.h"
|
|
|
|
#include "mozilla/dom/MessagePortChild.h"
|
|
|
|
#include "mozilla/dom/MessagePortList.h"
|
|
|
|
#include "mozilla/dom/PMessagePort.h"
|
|
|
|
#include "mozilla/dom/StructuredCloneTags.h"
|
|
|
|
#include "mozilla/dom/WorkerPrivate.h"
|
|
|
|
#include "mozilla/dom/WorkerScope.h"
|
|
|
|
#include "mozilla/ipc/BackgroundChild.h"
|
|
|
|
#include "mozilla/ipc/PBackgroundChild.h"
|
2015-06-24 01:50:00 +03:00
|
|
|
#include "mozilla/unused.h"
|
2015-06-17 13:44:27 +03:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsGlobalWindow.h"
|
|
|
|
#include "nsPresContext.h"
|
|
|
|
#include "ScriptSettings.h"
|
|
|
|
#include "SharedMessagePortMessage.h"
|
|
|
|
|
|
|
|
#include "nsIBFCacheEntry.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIDOMFileList.h"
|
|
|
|
#include "nsIPresShell.h"
|
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
#undef PostMessage
|
|
|
|
#endif
|
|
|
|
|
|
|
|
using namespace mozilla::dom::workers;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2016-02-11 22:42:13 +03:00
|
|
|
class PostMessageRunnable final : public nsICancelableRunnable
|
2015-06-17 13:44:27 +03:00
|
|
|
{
|
|
|
|
friend class MessagePort;
|
|
|
|
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2016-02-11 22:42:13 +03:00
|
|
|
PostMessageRunnable(MessagePort* aPort, SharedMessagePortMessage* aData)
|
2015-06-17 13:44:27 +03:00
|
|
|
: mPort(aPort)
|
2016-02-11 22:42:13 +03:00
|
|
|
, mData(aData)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aPort);
|
|
|
|
MOZ_ASSERT(aData);
|
|
|
|
}
|
2015-06-17 13:44:27 +03:00
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() override
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mPort);
|
2016-02-11 22:42:13 +03:00
|
|
|
MOZ_ASSERT(mPort->mPostMessageRunnable == this);
|
|
|
|
|
|
|
|
nsresult rv = DispatchMessage();
|
|
|
|
|
|
|
|
mPort->mPostMessageRunnable = nullptr;
|
2015-06-17 13:44:27 +03:00
|
|
|
mPort->Dispatch();
|
|
|
|
|
2016-02-11 22:42:13 +03:00
|
|
|
return rv;
|
2015-06-17 13:44:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Cancel() override
|
|
|
|
{
|
|
|
|
mPort = nullptr;
|
2016-02-11 22:42:13 +03:00
|
|
|
mData = nullptr;
|
2015-06-17 13:44:27 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-02-11 22:42:13 +03:00
|
|
|
nsresult
|
|
|
|
DispatchMessage() const
|
2015-06-17 13:44:27 +03:00
|
|
|
{
|
2016-02-24 22:54:35 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> globalObject;
|
|
|
|
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
globalObject = do_QueryInterface(mPort->GetParentObject());
|
|
|
|
} else {
|
|
|
|
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(workerPrivate);
|
|
|
|
globalObject = workerPrivate->GlobalScope();
|
|
|
|
}
|
2015-06-17 13:44:27 +03:00
|
|
|
|
|
|
|
AutoJSAPI jsapi;
|
|
|
|
if (!globalObject || !jsapi.Init(globalObject)) {
|
|
|
|
NS_WARNING("Failed to initialize AutoJSAPI object.");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSContext* cx = jsapi.cx();
|
|
|
|
|
2015-07-28 13:47:36 +03:00
|
|
|
ErrorResult rv;
|
2015-06-17 13:44:27 +03:00
|
|
|
JS::Rooted<JS::Value> value(cx);
|
|
|
|
|
2016-02-01 00:48:00 +03:00
|
|
|
mData->Read(mPort->GetParentObject(), cx, &value, rv);
|
2015-07-28 13:47:36 +03:00
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
return rv.StealNSResult();
|
|
|
|
}
|
2015-06-17 13:44:27 +03:00
|
|
|
|
|
|
|
// Create the event
|
|
|
|
nsCOMPtr<mozilla::dom::EventTarget> eventTarget =
|
|
|
|
do_QueryInterface(mPort->GetOwner());
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MessageEvent> event =
|
2015-06-17 13:44:27 +03:00
|
|
|
new MessageEvent(eventTarget, nullptr, nullptr);
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
event->InitMessageEvent(nullptr, NS_LITERAL_STRING("message"),
|
2015-06-17 13:44:27 +03:00
|
|
|
false /* non-bubbling */,
|
|
|
|
false /* cancelable */, value, EmptyString(),
|
2016-01-30 20:05:36 +03:00
|
|
|
EmptyString(), nullptr, nullptr);
|
2015-06-17 13:44:27 +03:00
|
|
|
event->SetTrusted(true);
|
|
|
|
event->SetSource(mPort);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray<RefPtr<MessagePort>> ports = mData->TakeTransferredPorts();
|
2015-08-27 19:19:13 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MessagePortList> portList =
|
2015-07-28 13:47:36 +03:00
|
|
|
new MessagePortList(static_cast<dom::Event*>(event.get()),
|
2015-08-27 19:19:13 +03:00
|
|
|
ports);
|
2015-06-17 13:44:27 +03:00
|
|
|
event->SetPorts(portList);
|
|
|
|
|
|
|
|
bool dummy;
|
|
|
|
mPort->DispatchEvent(static_cast<dom::Event*>(event.get()), &dummy);
|
2015-11-18 02:38:01 +03:00
|
|
|
|
|
|
|
// We must check if we were waiting for this message in order to shutdown
|
|
|
|
// the port.
|
|
|
|
mPort->UpdateMustKeepAlive();
|
2015-06-17 13:44:27 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-11-15 20:56:06 +03:00
|
|
|
private:
|
2015-06-17 13:44:27 +03:00
|
|
|
~PostMessageRunnable()
|
|
|
|
{}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MessagePort> mPort;
|
|
|
|
RefPtr<SharedMessagePortMessage> mData;
|
2015-06-17 13:44:27 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(PostMessageRunnable, nsICancelableRunnable, nsIRunnable)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(MessagePort)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(MessagePort,
|
2015-09-16 06:27:56 +03:00
|
|
|
DOMEventTargetHelper)
|
2016-02-11 22:42:13 +03:00
|
|
|
if (tmp->mPostMessageRunnable) {
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mPostMessageRunnable->mPort);
|
2015-06-17 13:44:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mMessages);
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mMessagesForTheOtherPort);
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mUnshippedEntangledPort);
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(MessagePort,
|
2015-09-16 06:27:56 +03:00
|
|
|
DOMEventTargetHelper)
|
2016-02-11 22:42:13 +03:00
|
|
|
if (tmp->mPostMessageRunnable) {
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPostMessageRunnable->mPort);
|
2015-06-17 13:44:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mUnshippedEntangledPort);
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(MessagePort)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIIPCBackgroundChildCreateCallback)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIObserver)
|
2015-09-16 06:27:56 +03:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
2015-06-17 13:44:27 +03:00
|
|
|
|
2015-09-16 06:27:56 +03:00
|
|
|
NS_IMPL_ADDREF_INHERITED(MessagePort, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(MessagePort, DOMEventTargetHelper)
|
2015-06-17 13:44:27 +03:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class MessagePortFeature final : public workers::WorkerFeature
|
|
|
|
{
|
|
|
|
MessagePort* mPort;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit MessagePortFeature(MessagePort* aPort)
|
|
|
|
: mPort(aPort)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aPort);
|
|
|
|
MOZ_COUNT_CTOR(MessagePortFeature);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool Notify(JSContext* aCx, workers::Status aStatus) override
|
|
|
|
{
|
2015-11-18 02:38:01 +03:00
|
|
|
if (aStatus > Running) {
|
|
|
|
// We cannot process messages anymore because we cannot dispatch new
|
|
|
|
// runnables. Let's force a Close().
|
|
|
|
mPort->CloseForced();
|
2015-06-17 13:44:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~MessagePortFeature()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(MessagePortFeature);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-06-24 01:50:00 +03:00
|
|
|
class ForceCloseHelper final : public nsIIPCBackgroundChildCreateCallback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
static void ForceClose(const MessagePortIdentifier& aIdentifier)
|
|
|
|
{
|
|
|
|
PBackgroundChild* actor =
|
|
|
|
mozilla::ipc::BackgroundChild::GetForCurrentThread();
|
|
|
|
if (actor) {
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << actor->SendMessagePortForceClose(aIdentifier.uuid(),
|
2015-06-24 01:50:00 +03:00
|
|
|
aIdentifier.destinationUuid(),
|
|
|
|
aIdentifier.sequenceId());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ForceCloseHelper> helper = new ForceCloseHelper(aIdentifier);
|
2015-06-24 01:50:00 +03:00
|
|
|
if (NS_WARN_IF(!mozilla::ipc::BackgroundChild::GetOrCreateForCurrentThread(helper))) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
explicit ForceCloseHelper(const MessagePortIdentifier& aIdentifier)
|
|
|
|
: mIdentifier(aIdentifier)
|
|
|
|
{}
|
|
|
|
|
|
|
|
~ForceCloseHelper() {}
|
|
|
|
|
|
|
|
void ActorFailed() override
|
|
|
|
{
|
|
|
|
MOZ_CRASH("Failed to create a PBackgroundChild actor!");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ActorCreated(mozilla::ipc::PBackgroundChild* aActor) override
|
|
|
|
{
|
|
|
|
ForceClose(mIdentifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
const MessagePortIdentifier mIdentifier;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(ForceCloseHelper, nsIIPCBackgroundChildCreateCallback)
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2015-06-17 13:44:27 +03:00
|
|
|
|
2016-02-24 22:54:35 +03:00
|
|
|
MessagePort::MessagePort(nsISupports* aSupports)
|
|
|
|
: mInnerID(0)
|
2015-06-17 13:44:27 +03:00
|
|
|
, mMessageQueueEnabled(false)
|
|
|
|
, mIsKeptAlive(false)
|
|
|
|
{
|
|
|
|
mIdentifier = new MessagePortIdentifier();
|
|
|
|
mIdentifier->neutered() = true;
|
|
|
|
mIdentifier->sequenceId() = 0;
|
2016-02-24 22:54:35 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIGlobalObject> globalObject = do_QueryInterface(aSupports);
|
|
|
|
if (NS_WARN_IF(!globalObject)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
BindToOwner(globalObject);
|
2015-06-17 13:44:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
MessagePort::~MessagePort()
|
|
|
|
{
|
2015-11-18 02:38:13 +03:00
|
|
|
CloseForced();
|
2015-06-17 13:44:27 +03:00
|
|
|
MOZ_ASSERT(!mWorkerFeature);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<MessagePort>
|
2016-02-24 22:54:35 +03:00
|
|
|
MessagePort::Create(nsISupports* aSupport, const nsID& aUUID,
|
2015-06-17 13:44:27 +03:00
|
|
|
const nsID& aDestinationUUID, ErrorResult& aRv)
|
|
|
|
{
|
2016-02-24 22:54:35 +03:00
|
|
|
RefPtr<MessagePort> mp = new MessagePort(aSupport);
|
2015-06-17 13:44:27 +03:00
|
|
|
mp->Initialize(aUUID, aDestinationUUID, 1 /* 0 is an invalid sequence ID */,
|
|
|
|
false /* Neutered */, eStateUnshippedEntangled, aRv);
|
|
|
|
return mp.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<MessagePort>
|
2016-02-24 22:54:35 +03:00
|
|
|
MessagePort::Create(nsISupports* aSupport,
|
2015-06-17 13:44:27 +03:00
|
|
|
const MessagePortIdentifier& aIdentifier,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2016-02-24 22:54:35 +03:00
|
|
|
RefPtr<MessagePort> mp = new MessagePort(aSupport);
|
2015-06-17 13:44:27 +03:00
|
|
|
mp->Initialize(aIdentifier.uuid(), aIdentifier.destinationUuid(),
|
|
|
|
aIdentifier.sequenceId(), aIdentifier.neutered(),
|
|
|
|
eStateEntangling, aRv);
|
|
|
|
return mp.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePort::UnshippedEntangle(MessagePort* aEntangledPort)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aEntangledPort);
|
|
|
|
MOZ_ASSERT(!mUnshippedEntangledPort);
|
|
|
|
|
|
|
|
mUnshippedEntangledPort = aEntangledPort;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePort::Initialize(const nsID& aUUID,
|
|
|
|
const nsID& aDestinationUUID,
|
|
|
|
uint32_t aSequenceID, bool mNeutered,
|
|
|
|
State aState, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mIdentifier);
|
|
|
|
mIdentifier->uuid() = aUUID;
|
|
|
|
mIdentifier->destinationUuid() = aDestinationUUID;
|
|
|
|
mIdentifier->sequenceId() = aSequenceID;
|
|
|
|
|
|
|
|
mState = aState;
|
|
|
|
|
|
|
|
if (mNeutered) {
|
2015-06-29 16:52:49 +03:00
|
|
|
// If this port is neutered we don't want to keep it alive artificially nor
|
|
|
|
// we want to add listeners or workerFeatures.
|
2015-06-17 13:44:27 +03:00
|
|
|
mState = eStateDisentangled;
|
2015-06-29 16:52:49 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mState == eStateEntangling) {
|
2015-06-17 13:44:27 +03:00
|
|
|
ConnectToPBackground();
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(mState == eStateUnshippedEntangled);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The port has to keep itself alive until it's entangled.
|
|
|
|
UpdateMustKeepAlive();
|
|
|
|
|
2015-09-16 06:27:56 +03:00
|
|
|
if (!NS_IsMainThread()) {
|
2015-06-17 13:44:27 +03:00
|
|
|
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(workerPrivate);
|
|
|
|
MOZ_ASSERT(!mWorkerFeature);
|
|
|
|
|
|
|
|
nsAutoPtr<WorkerFeature> feature(new MessagePortFeature(this));
|
2016-02-29 22:52:42 +03:00
|
|
|
if (NS_WARN_IF(!workerPrivate->AddFeature(feature))) {
|
2015-06-17 13:44:27 +03:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mWorkerFeature = Move(feature);
|
2015-09-16 06:27:56 +03:00
|
|
|
} else if (GetOwner()) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(GetOwner()->IsInnerWindow());
|
|
|
|
mInnerID = GetOwner()->WindowID();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
if (obs) {
|
|
|
|
obs->AddObserver(this, "inner-window-destroyed", false);
|
|
|
|
}
|
2015-06-17 13:44:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
MessagePort::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
|
|
|
{
|
|
|
|
return MessagePortBinding::Wrap(aCx, this, aGivenProto);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePort::PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage,
|
|
|
|
const Optional<Sequence<JS::Value>>& aTransferable,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
// We *must* clone the data here, or the JS::Value could be modified
|
|
|
|
// by script
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> transferable(aCx, JS::UndefinedValue());
|
|
|
|
if (aTransferable.WasPassed()) {
|
|
|
|
const Sequence<JS::Value>& realTransferable = aTransferable.Value();
|
|
|
|
|
|
|
|
// Here we want to check if the transerable object list contains
|
|
|
|
// this port. No other checks are done.
|
|
|
|
for (const JS::Value& value : realTransferable) {
|
|
|
|
if (!value.isObject()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-09-16 06:27:56 +03:00
|
|
|
MessagePort* port = nullptr;
|
2015-06-17 13:44:27 +03:00
|
|
|
nsresult rv = UNWRAP_OBJECT(MessagePort, &value.toObject(), port);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (port == this) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_DATA_CLONE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The input sequence only comes from the generated bindings code, which
|
|
|
|
// ensures it is rooted.
|
|
|
|
JS::HandleValueArray elements =
|
|
|
|
JS::HandleValueArray::fromMarkedLocation(realTransferable.Length(),
|
|
|
|
realTransferable.Elements());
|
|
|
|
|
|
|
|
JSObject* array =
|
|
|
|
JS_NewArrayObject(aCx, elements);
|
|
|
|
if (!array) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
transferable.setObject(*array);
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SharedMessagePortMessage> data = new SharedMessagePortMessage();
|
2015-06-17 13:44:27 +03:00
|
|
|
|
2015-07-28 13:47:36 +03:00
|
|
|
data->Write(aCx, aMessage, transferable, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2015-06-17 13:44:27 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This message has to be ignored.
|
|
|
|
if (mState > eStateEntangled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we are unshipped we are connected to the other port on the same thread.
|
|
|
|
if (mState == eStateUnshippedEntangled) {
|
|
|
|
MOZ_ASSERT(mUnshippedEntangledPort);
|
|
|
|
mUnshippedEntangledPort->mMessages.AppendElement(data);
|
|
|
|
mUnshippedEntangledPort->Dispatch();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-18 02:38:01 +03:00
|
|
|
// Not entangled yet, but already closed/disentangled.
|
|
|
|
if (mState == eStateEntanglingForDisentangle ||
|
|
|
|
mState == eStateEntanglingForClose) {
|
2015-06-17 13:44:27 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RemoveDocFromBFCache();
|
|
|
|
|
|
|
|
// Not entangled yet.
|
|
|
|
if (mState == eStateEntangling) {
|
|
|
|
mMessagesForTheOtherPort.AppendElement(data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mActor);
|
|
|
|
MOZ_ASSERT(mMessagesForTheOtherPort.IsEmpty());
|
|
|
|
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<RefPtr<SharedMessagePortMessage>, 1> array;
|
2015-06-17 13:44:27 +03:00
|
|
|
array.AppendElement(data);
|
|
|
|
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<MessagePortMessage, 1> messages;
|
2015-06-17 13:44:27 +03:00
|
|
|
SharedMessagePortMessage::FromSharedToMessagesChild(mActor, array, messages);
|
|
|
|
mActor->SendPostMessages(messages);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePort::Start()
|
|
|
|
{
|
|
|
|
if (mMessageQueueEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mMessageQueueEnabled = true;
|
|
|
|
Dispatch();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePort::Dispatch()
|
|
|
|
{
|
2016-02-11 22:42:13 +03:00
|
|
|
if (!mMessageQueueEnabled || mMessages.IsEmpty() || mPostMessageRunnable) {
|
2015-06-17 13:44:27 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-18 02:38:01 +03:00
|
|
|
switch (mState) {
|
|
|
|
case eStateUnshippedEntangled:
|
|
|
|
// Everything is fine here. We have messages because the other
|
|
|
|
// port populates our queue directly.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case eStateEntangling:
|
|
|
|
// Everything is fine here as well. We have messages because the other
|
|
|
|
// port populated our queue directly when we were in the
|
|
|
|
// eStateUnshippedEntangled state.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case eStateEntanglingForDisentangle:
|
|
|
|
// Here we don't want to ship messages because these messages must be
|
|
|
|
// delivered by the cloned version of this one. They will be sent in the
|
|
|
|
// SendDisentangle().
|
|
|
|
return;
|
|
|
|
|
|
|
|
case eStateEntanglingForClose:
|
|
|
|
// We still want to deliver messages if we are closing. These messages
|
|
|
|
// are here from the previous eStateUnshippedEntangled state.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case eStateEntangled:
|
|
|
|
// This port is up and running.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case eStateDisentangling:
|
|
|
|
// If we are in the process to disentangle the port, we cannot dispatch
|
|
|
|
// messages. They will be sent to the cloned version of this port via
|
|
|
|
// SendDisentangle();
|
|
|
|
return;
|
|
|
|
|
|
|
|
case eStateDisentangled:
|
|
|
|
MOZ_CRASH("This cannot happen.");
|
|
|
|
// It cannot happen because Disentangle should take off all the pending
|
|
|
|
// messages.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case eStateDisentangledForClose:
|
|
|
|
// If we are here is because the port has been closed. We can still
|
|
|
|
// process the pending messages.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SharedMessagePortMessage> data = mMessages.ElementAt(0);
|
2015-06-17 13:44:27 +03:00
|
|
|
mMessages.RemoveElementAt(0);
|
|
|
|
|
2016-02-11 22:42:13 +03:00
|
|
|
mPostMessageRunnable = new PostMessageRunnable(this, data);
|
2015-06-17 13:44:27 +03:00
|
|
|
|
2016-02-11 22:42:13 +03:00
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(NS_DispatchToCurrentThread(mPostMessageRunnable)));
|
2015-06-17 13:44:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePort::Close()
|
|
|
|
{
|
2015-11-18 02:38:01 +03:00
|
|
|
CloseInternal(true /* aSoftly */);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePort::CloseForced()
|
|
|
|
{
|
|
|
|
CloseInternal(false /* aSoftly */);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePort::CloseInternal(bool aSoftly)
|
|
|
|
{
|
|
|
|
// If we have some messages to send but we don't want a 'soft' close, we have
|
|
|
|
// to flush them now.
|
|
|
|
if (!aSoftly) {
|
|
|
|
mMessages.Clear();
|
|
|
|
}
|
|
|
|
|
2015-06-17 13:44:27 +03:00
|
|
|
if (mState == eStateUnshippedEntangled) {
|
|
|
|
MOZ_ASSERT(mUnshippedEntangledPort);
|
|
|
|
|
|
|
|
// This avoids loops.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MessagePort> port = Move(mUnshippedEntangledPort);
|
2015-06-17 13:44:27 +03:00
|
|
|
MOZ_ASSERT(mUnshippedEntangledPort == nullptr);
|
|
|
|
|
2015-11-18 02:38:01 +03:00
|
|
|
mState = eStateDisentangledForClose;
|
|
|
|
port->CloseInternal(aSoftly);
|
2015-06-17 13:44:27 +03:00
|
|
|
|
|
|
|
UpdateMustKeepAlive();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not entangled yet, we have to wait.
|
2015-09-16 06:27:56 +03:00
|
|
|
if (mState == eStateEntangling) {
|
2015-11-18 02:38:01 +03:00
|
|
|
mState = eStateEntanglingForClose;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not entangled but already cloned or closed
|
|
|
|
if (mState == eStateEntanglingForDisentangle ||
|
|
|
|
mState == eStateEntanglingForClose) {
|
2015-06-17 13:44:27 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-18 02:38:13 +03:00
|
|
|
// Maybe we were already closing the port but softly. In this case we call
|
|
|
|
// UpdateMustKeepAlive() to consider the empty pending message queue.
|
|
|
|
if (mState == eStateDisentangledForClose && !aSoftly) {
|
|
|
|
UpdateMustKeepAlive();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-17 13:44:27 +03:00
|
|
|
if (mState > eStateEntangled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't care about stopping the sending of messages because from now all
|
|
|
|
// the incoming messages will be ignored.
|
2015-11-18 02:38:01 +03:00
|
|
|
mState = eStateDisentangledForClose;
|
2015-06-17 13:44:27 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(mActor);
|
|
|
|
|
|
|
|
mActor->SendClose();
|
|
|
|
mActor->SetPort(nullptr);
|
|
|
|
mActor = nullptr;
|
|
|
|
|
|
|
|
UpdateMustKeepAlive();
|
|
|
|
}
|
|
|
|
|
|
|
|
EventHandlerNonNull*
|
|
|
|
MessagePort::GetOnmessage()
|
|
|
|
{
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
return GetEventHandler(nsGkAtoms::onmessage, EmptyString());
|
|
|
|
}
|
|
|
|
return GetEventHandler(nullptr, NS_LITERAL_STRING("message"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePort::SetOnmessage(EventHandlerNonNull* aCallback)
|
|
|
|
{
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
SetEventHandler(nsGkAtoms::onmessage, EmptyString(), aCallback);
|
|
|
|
} else {
|
|
|
|
SetEventHandler(nullptr, NS_LITERAL_STRING("message"), aCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
// When using onmessage, the call to start() is implied.
|
|
|
|
Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
// This method is called when the PMessagePortChild actor is entangled to
|
|
|
|
// another actor. It receives a list of messages to be dispatch. It can be that
|
|
|
|
// we were waiting for this entangling step in order to disentangle the port or
|
|
|
|
// to close it.
|
|
|
|
void
|
|
|
|
MessagePort::Entangled(nsTArray<MessagePortMessage>& aMessages)
|
|
|
|
{
|
2015-11-18 02:38:01 +03:00
|
|
|
MOZ_ASSERT(mState == eStateEntangling ||
|
|
|
|
mState == eStateEntanglingForDisentangle ||
|
|
|
|
mState == eStateEntanglingForClose);
|
2015-06-17 13:44:27 +03:00
|
|
|
|
2015-11-18 02:38:01 +03:00
|
|
|
State oldState = mState;
|
2015-06-17 13:44:27 +03:00
|
|
|
mState = eStateEntangled;
|
|
|
|
|
|
|
|
// If we have pending messages, these have to be sent.
|
|
|
|
if (!mMessagesForTheOtherPort.IsEmpty()) {
|
|
|
|
nsTArray<MessagePortMessage> messages;
|
|
|
|
SharedMessagePortMessage::FromSharedToMessagesChild(mActor,
|
|
|
|
mMessagesForTheOtherPort,
|
|
|
|
messages);
|
|
|
|
mMessagesForTheOtherPort.Clear();
|
|
|
|
mActor->SendPostMessages(messages);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We must convert the messages into SharedMessagePortMessages to avoid leaks.
|
2015-10-18 08:24:48 +03:00
|
|
|
FallibleTArray<RefPtr<SharedMessagePortMessage>> data;
|
2015-06-17 13:44:27 +03:00
|
|
|
if (NS_WARN_IF(!SharedMessagePortMessage::FromMessagesToSharedChild(aMessages,
|
|
|
|
data))) {
|
|
|
|
// OOM, we cannot continue.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-18 02:38:01 +03:00
|
|
|
// If the next step is to close the port, we do it ignoring the received
|
|
|
|
// messages.
|
|
|
|
if (oldState == eStateEntanglingForClose) {
|
|
|
|
CloseForced();
|
2015-06-17 13:44:27 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mMessages.AppendElements(data);
|
|
|
|
|
|
|
|
// We were waiting for the entangling callback in order to disentangle this
|
|
|
|
// port immediately after.
|
2015-11-18 02:38:01 +03:00
|
|
|
if (oldState == eStateEntanglingForDisentangle) {
|
2015-06-17 13:44:27 +03:00
|
|
|
StartDisentangling();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Dispatch();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePort::StartDisentangling()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mActor);
|
|
|
|
MOZ_ASSERT(mState == eStateEntangled);
|
|
|
|
|
|
|
|
mState = eStateDisentangling;
|
|
|
|
|
|
|
|
// Sending this message we communicate to the parent actor that we don't want
|
|
|
|
// to receive any new messages. It is possible that a message has been
|
|
|
|
// already sent but not received yet. So we have to collect all of them and
|
|
|
|
// we send them in the SendDispatch() request.
|
|
|
|
mActor->SendStopSendingData();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePort::MessagesReceived(nsTArray<MessagePortMessage>& aMessages)
|
|
|
|
{
|
2015-11-18 02:38:01 +03:00
|
|
|
MOZ_ASSERT(mState == eStateEntangled ||
|
|
|
|
mState == eStateDisentangling ||
|
|
|
|
// This last step can happen only if Close() has been called
|
|
|
|
// manually. At this point SendClose() is sent but we can still
|
|
|
|
// receive something until the Closing request is processed.
|
|
|
|
mState == eStateDisentangledForClose);
|
2015-06-17 13:44:27 +03:00
|
|
|
MOZ_ASSERT(mMessagesForTheOtherPort.IsEmpty());
|
|
|
|
|
|
|
|
RemoveDocFromBFCache();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
FallibleTArray<RefPtr<SharedMessagePortMessage>> data;
|
2015-07-21 10:18:25 +03:00
|
|
|
if (NS_WARN_IF(!SharedMessagePortMessage::FromMessagesToSharedChild(aMessages,
|
2015-06-17 13:44:27 +03:00
|
|
|
data))) {
|
|
|
|
// OOM, We cannot continue.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mMessages.AppendElements(data);
|
|
|
|
|
|
|
|
if (mState == eStateEntangled) {
|
|
|
|
Dispatch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePort::StopSendingDataConfirmed()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mState == eStateDisentangling);
|
|
|
|
MOZ_ASSERT(mActor);
|
|
|
|
|
|
|
|
Disentangle();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePort::Disentangle()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mState == eStateDisentangling);
|
|
|
|
MOZ_ASSERT(mActor);
|
|
|
|
|
|
|
|
mState = eStateDisentangled;
|
|
|
|
|
|
|
|
nsTArray<MessagePortMessage> messages;
|
|
|
|
SharedMessagePortMessage::FromSharedToMessagesChild(mActor, mMessages,
|
|
|
|
messages);
|
|
|
|
mMessages.Clear();
|
|
|
|
mActor->SendDisentangle(messages);
|
|
|
|
|
|
|
|
mActor->SetPort(nullptr);
|
|
|
|
mActor = nullptr;
|
|
|
|
|
|
|
|
UpdateMustKeepAlive();
|
|
|
|
}
|
|
|
|
|
2015-09-16 06:27:56 +03:00
|
|
|
void
|
2015-06-17 13:44:27 +03:00
|
|
|
MessagePort::CloneAndDisentangle(MessagePortIdentifier& aIdentifier)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mIdentifier);
|
|
|
|
|
|
|
|
// We can clone a port that has already been transfered. In this case, on the
|
|
|
|
// otherside will have a neutered port. Here we set neutered to true so that
|
|
|
|
// we are safe in case a early return.
|
|
|
|
aIdentifier.neutered() = true;
|
|
|
|
|
|
|
|
if (mState > eStateEntangled) {
|
2015-09-16 06:27:56 +03:00
|
|
|
return;
|
2015-06-17 13:44:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// We already have a 'next step'. We have to consider this port as already
|
|
|
|
// cloned/closed/disentangled.
|
2015-11-18 02:38:01 +03:00
|
|
|
if (mState == eStateEntanglingForDisentangle ||
|
|
|
|
mState == eStateEntanglingForClose) {
|
2015-09-16 06:27:56 +03:00
|
|
|
return;
|
2015-06-17 13:44:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
aIdentifier.uuid() = mIdentifier->uuid();
|
|
|
|
aIdentifier.destinationUuid() = mIdentifier->destinationUuid();
|
|
|
|
aIdentifier.sequenceId() = mIdentifier->sequenceId() + 1;
|
|
|
|
aIdentifier.neutered() = false;
|
|
|
|
|
|
|
|
// We have to entangle first.
|
|
|
|
if (mState == eStateUnshippedEntangled) {
|
|
|
|
MOZ_ASSERT(mUnshippedEntangledPort);
|
|
|
|
MOZ_ASSERT(mMessagesForTheOtherPort.IsEmpty());
|
|
|
|
|
|
|
|
// Disconnect the entangled port and connect it to PBackground.
|
|
|
|
mUnshippedEntangledPort->ConnectToPBackground();
|
|
|
|
mUnshippedEntangledPort = nullptr;
|
|
|
|
|
|
|
|
// In this case, we don't need to be connected to the PBackground service.
|
|
|
|
if (mMessages.IsEmpty()) {
|
|
|
|
aIdentifier.sequenceId() = mIdentifier->sequenceId();
|
|
|
|
|
|
|
|
mState = eStateDisentangled;
|
|
|
|
UpdateMustKeepAlive();
|
2015-09-16 06:27:56 +03:00
|
|
|
return;
|
2015-06-17 13:44:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Register this component to PBackground.
|
|
|
|
ConnectToPBackground();
|
|
|
|
|
2015-11-18 02:38:01 +03:00
|
|
|
mState = eStateEntanglingForDisentangle;
|
2015-09-16 06:27:56 +03:00
|
|
|
return;
|
2015-06-17 13:44:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Not entangled yet, we have to wait.
|
2015-11-18 02:38:01 +03:00
|
|
|
if (mState == eStateEntangling) {
|
|
|
|
mState = eStateEntanglingForDisentangle;
|
2015-09-16 06:27:56 +03:00
|
|
|
return;
|
2015-06-17 13:44:27 +03:00
|
|
|
}
|
|
|
|
|
2015-11-18 02:38:01 +03:00
|
|
|
MOZ_ASSERT(mState == eStateEntangled);
|
2015-06-17 13:44:27 +03:00
|
|
|
StartDisentangling();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePort::Closed()
|
|
|
|
{
|
2015-11-18 02:38:01 +03:00
|
|
|
if (mState >= eStateDisentangled) {
|
2015-06-17 13:44:27 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-18 02:38:01 +03:00
|
|
|
mState = eStateDisentangledForClose;
|
2015-06-17 13:44:27 +03:00
|
|
|
|
|
|
|
if (mActor) {
|
|
|
|
mActor->SetPort(nullptr);
|
|
|
|
mActor = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateMustKeepAlive();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePort::ConnectToPBackground()
|
|
|
|
{
|
|
|
|
mState = eStateEntangling;
|
|
|
|
|
|
|
|
PBackgroundChild* actor =
|
|
|
|
mozilla::ipc::BackgroundChild::GetForCurrentThread();
|
|
|
|
if (actor) {
|
|
|
|
ActorCreated(actor);
|
|
|
|
} else {
|
|
|
|
if (NS_WARN_IF(
|
|
|
|
!mozilla::ipc::BackgroundChild::GetOrCreateForCurrentThread(this))) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePort::ActorFailed()
|
|
|
|
{
|
|
|
|
MOZ_CRASH("Failed to create a PBackgroundChild actor!");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePort::ActorCreated(mozilla::ipc::PBackgroundChild* aActor)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aActor);
|
|
|
|
MOZ_ASSERT(!mActor);
|
|
|
|
MOZ_ASSERT(mIdentifier);
|
2015-11-18 02:38:01 +03:00
|
|
|
MOZ_ASSERT(mState == eStateEntangling ||
|
|
|
|
mState == eStateEntanglingForDisentangle ||
|
|
|
|
mState == eStateEntanglingForClose);
|
2015-06-17 13:44:27 +03:00
|
|
|
|
|
|
|
PMessagePortChild* actor =
|
|
|
|
aActor->SendPMessagePortConstructor(mIdentifier->uuid(),
|
|
|
|
mIdentifier->destinationUuid(),
|
|
|
|
mIdentifier->sequenceId());
|
|
|
|
|
|
|
|
mActor = static_cast<MessagePortChild*>(actor);
|
|
|
|
MOZ_ASSERT(mActor);
|
|
|
|
|
|
|
|
mActor->SetPort(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePort::UpdateMustKeepAlive()
|
|
|
|
{
|
2015-11-18 02:38:01 +03:00
|
|
|
if (mState >= eStateDisentangled &&
|
|
|
|
mMessages.IsEmpty() &&
|
|
|
|
mIsKeptAlive) {
|
2015-06-17 13:44:27 +03:00
|
|
|
mIsKeptAlive = false;
|
|
|
|
|
|
|
|
if (mWorkerFeature) {
|
|
|
|
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(workerPrivate);
|
|
|
|
|
2016-02-29 22:52:42 +03:00
|
|
|
workerPrivate->RemoveFeature(mWorkerFeature);
|
2015-06-17 13:44:27 +03:00
|
|
|
mWorkerFeature = nullptr;
|
|
|
|
}
|
|
|
|
|
2015-06-28 02:21:24 +03:00
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
nsCOMPtr<nsIObserverService> obs =
|
|
|
|
do_GetService("@mozilla.org/observer-service;1");
|
|
|
|
if (obs) {
|
|
|
|
obs->RemoveObserver(this, "inner-window-destroyed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-17 13:44:27 +03:00
|
|
|
Release();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mState < eStateDisentangled && !mIsKeptAlive) {
|
|
|
|
mIsKeptAlive = true;
|
|
|
|
AddRef();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MessagePort::Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const char16_t* aData)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (strcmp(aTopic, "inner-window-destroyed")) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the window id destroyed we have to release the reference that we are
|
|
|
|
// keeping.
|
|
|
|
if (!mIsKeptAlive) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupportsPRUint64> wrapper = do_QueryInterface(aSubject);
|
|
|
|
NS_ENSURE_TRUE(wrapper, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
uint64_t innerID;
|
|
|
|
nsresult rv = wrapper->GetData(&innerID);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (innerID == mInnerID) {
|
2015-11-18 02:38:01 +03:00
|
|
|
CloseForced();
|
2015-06-17 13:44:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePort::RemoveDocFromBFCache()
|
|
|
|
{
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPIDOMWindowInner* window = GetOwner();
|
2015-07-09 03:34:37 +03:00
|
|
|
if (!window) {
|
|
|
|
return;
|
|
|
|
}
|
2015-06-17 13:44:27 +03:00
|
|
|
|
|
|
|
nsIDocument* doc = window->GetExtantDoc();
|
|
|
|
if (!doc) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIBFCacheEntry> bfCacheEntry = doc->GetBFCacheEntry();
|
|
|
|
if (!bfCacheEntry) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bfCacheEntry->RemoveFromBFCacheSync();
|
|
|
|
}
|
|
|
|
|
2015-06-24 01:50:00 +03:00
|
|
|
/* static */ void
|
|
|
|
MessagePort::ForceClose(const MessagePortIdentifier& aIdentifier)
|
|
|
|
{
|
|
|
|
ForceCloseHelper::ForceClose(aIdentifier);
|
|
|
|
}
|
|
|
|
|
2015-06-17 13:44:27 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|