Skip to content

Commit

Permalink
Bug 1204775 - SharedWorker.port should be a 'real' MessagePort, r=khuey
Browse files Browse the repository at this point in the history
  • Loading branch information
bakulf committed Sep 16, 2015
1 parent 1745e82 commit f0aa7a8
Show file tree
Hide file tree
Showing 26 changed files with 255 additions and 966 deletions.
3 changes: 1 addition & 2 deletions dom/base/PostMessageEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ PostMessageEvent::Run()
false /*cancelable */, messageData, mCallerOrigin,
EmptyString(), mSource);

nsTArray<nsRefPtr<MessagePortBase>> ports;
TakeTransferredPorts(ports);
nsTArray<nsRefPtr<MessagePort>> ports = TakeTransferredPorts();

event->SetPorts(new MessagePortList(static_cast<dom::Event*>(event.get()),
ports));
Expand Down
3 changes: 0 additions & 3 deletions dom/base/PostMessageEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ class nsPIDOMWindow;
namespace mozilla {
namespace dom {

class MessagePortBase;
class MessagePortIdentifier;

/**
* Class used to represent events generated by calls to Window.postMessage,
* which asynchronously creates and dispatches events.
Expand Down
6 changes: 2 additions & 4 deletions dom/base/StructuredCloneHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1072,16 +1072,14 @@ StructuredCloneHelper::WriteTransferCallback(JSContext* aCx,
}

{
MessagePortBase* port = nullptr;
MessagePort* port = nullptr;
nsresult rv = UNWRAP_OBJECT(MessagePort, aObj, port);
if (NS_SUCCEEDED(rv)) {
// We use aExtraData to store the index of this new port identifier.
*aExtraData = mPortIdentifiers.Length();
MessagePortIdentifier* identifier = mPortIdentifiers.AppendElement();

if (!port->CloneAndDisentangle(*identifier)) {
return false;
}
port->CloneAndDisentangle(*identifier);

*aTag = SCTAG_DOM_MAP_MESSAGEPORT;
*aOwnership = JS::SCTAG_TMO_CUSTOM;
Expand Down
10 changes: 5 additions & 5 deletions dom/base/StructuredCloneHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define mozilla_dom_StructuredCloneHelper_h

#include "js/StructuredClone.h"
#include "mozilla/Move.h"
#include "nsAutoPtr.h"
#include "nsISupports.h"
#include "nsTArray.h"
Expand Down Expand Up @@ -111,7 +112,7 @@ class StructuredCloneHelperInternal
};

class BlobImpl;
class MessagePortBase;
class MessagePort;
class MessagePortIdentifier;

class StructuredCloneHelper : public StructuredCloneHelperInternal
Expand Down Expand Up @@ -191,11 +192,10 @@ class StructuredCloneHelper : public StructuredCloneHelperInternal
// This must be called if the transferring has ports generated by Read().
// MessagePorts are not thread-safe and they must be retrieved in the thread
// where they are created.
void TakeTransferredPorts(nsTArray<nsRefPtr<MessagePortBase>>& aPorts)
nsTArray<nsRefPtr<MessagePort>>&& TakeTransferredPorts()
{
MOZ_ASSERT(mSupportsTransferring);
MOZ_ASSERT(aPorts.IsEmpty());
aPorts.SwapElements(mTransferredPorts);
return Move(mTransferredPorts);
}

nsTArray<MessagePortIdentifier>& PortIdentifiers()
Expand Down Expand Up @@ -291,7 +291,7 @@ class StructuredCloneHelper : public StructuredCloneHelperInternal

// This array contains the ports once we've finished the reading. It's
// generated from the mPortIdentifiers array.
nsTArray<nsRefPtr<MessagePortBase>> mTransferredPorts;
nsTArray<nsRefPtr<MessagePort>> mTransferredPorts;

// This array contains the identifiers of the MessagePorts. Based on these we
// are able to reconnect the new transferred ports with the other
Expand Down
5 changes: 0 additions & 5 deletions dom/bindings/Bindings.conf
Original file line number Diff line number Diff line change
Expand Up @@ -739,11 +739,6 @@ DOMInterfaces = {
'headerFile': 'MediaRecorder.h',
},

'MessagePort': {
'nativeType': 'mozilla::dom::MessagePortBase',
'headerFile': 'mozilla/dom/MessagePort.h',
},

'MimeType': {
'headerFile' : 'nsMimeTypeArray.h',
'nativeType': 'nsMimeType',
Expand Down
2 changes: 1 addition & 1 deletion dom/events/MessageEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ MessageEvent::Constructor(EventTarget* aEventTarget,
}

if (aParam.mPorts.WasPassed() && !aParam.mPorts.Value().IsNull()) {
nsTArray<nsRefPtr<MessagePortBase>> ports;
nsTArray<nsRefPtr<MessagePort>> ports;
for (uint32_t i = 0, len = aParam.mPorts.Value().Value().Length(); i < len; ++i) {
ports.AppendElement(aParam.mPorts.Value().Value()[i].get());
}
Expand Down
3 changes: 1 addition & 2 deletions dom/events/MessageEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ namespace dom {

struct MessageEventInit;
class MessagePort;
class MessagePortBase;
class MessagePortList;
class OwningWindowProxyOrMessagePortOrClient;

Expand Down Expand Up @@ -94,7 +93,7 @@ class MessageEvent final : public Event,
nsString mOrigin;
nsString mLastEventId;
nsCOMPtr<nsIDOMWindow> mWindowSource;
nsRefPtr<MessagePortBase> mPortSource;
nsRefPtr<MessagePort> mPortSource;
nsRefPtr<workers::ServiceWorkerClient> mClientSource;
nsRefPtr<MessagePortList> mPorts;
};
Expand Down
11 changes: 8 additions & 3 deletions dom/messagechannel/MessageChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ MessageChannel::Constructor(const GlobalObject& aGlobal, ErrorResult& aRv)
{
// window can be null in workers.
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.GetAsSupports());
return Constructor(window, aRv);
}

/* static */ already_AddRefed<MessageChannel>
MessageChannel::Constructor(nsPIDOMWindow* aWindow, ErrorResult& aRv)
{
nsID portUUID1;
aRv = nsContentUtils::GenerateUUIDInPlace(portUUID1);
if (aRv.Failed()) {
Expand All @@ -62,14 +67,14 @@ MessageChannel::Constructor(const GlobalObject& aGlobal, ErrorResult& aRv)
return nullptr;
}

nsRefPtr<MessageChannel> channel = new MessageChannel(window);
nsRefPtr<MessageChannel> channel = new MessageChannel(aWindow);

channel->mPort1 = MessagePort::Create(window, portUUID1, portUUID2, aRv);
channel->mPort1 = MessagePort::Create(aWindow, portUUID1, portUUID2, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}

channel->mPort2 = MessagePort::Create(window, portUUID2, portUUID1, aRv);
channel->mPort2 = MessagePort::Create(aWindow, portUUID2, portUUID1, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
Expand Down
3 changes: 3 additions & 0 deletions dom/messagechannel/MessageChannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class MessageChannel final : public nsISupports
static already_AddRefed<MessageChannel>
Constructor(const GlobalObject& aGlobal, ErrorResult& aRv);

static already_AddRefed<MessageChannel>
Constructor(nsPIDOMWindow* aWindow, ErrorResult& aRv);

MessagePort*
Port1() const
{
Expand Down
66 changes: 25 additions & 41 deletions dom/messagechannel/MessagePort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ class PostMessageRunnable final : public nsICancelableRunnable
event->SetTrusted(true);
event->SetSource(mPort);

nsTArray<nsRefPtr<MessagePortBase>> ports;
mData->TakeTransferredPorts(ports);
nsTArray<nsRefPtr<MessagePort>> ports = mData->TakeTransferredPorts();

nsRefPtr<MessagePortList> portList =
new MessagePortList(static_cast<dom::Event*>(event.get()),
Expand Down Expand Up @@ -171,19 +170,10 @@ class PostMessageRunnable final : public nsICancelableRunnable

NS_IMPL_ISUPPORTS(PostMessageRunnable, nsICancelableRunnable, nsIRunnable)

MessagePortBase::MessagePortBase(nsPIDOMWindow* aWindow)
: DOMEventTargetHelper(aWindow)
{
}

MessagePortBase::MessagePortBase()
{
}

NS_IMPL_CYCLE_COLLECTION_CLASS(MessagePort)

NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(MessagePort,
MessagePortBase)
DOMEventTargetHelper)
if (tmp->mDispatchRunnable) {
NS_IMPL_CYCLE_COLLECTION_UNLINK(mDispatchRunnable->mPort);
}
Expand All @@ -194,7 +184,7 @@ NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(MessagePort,
NS_IMPL_CYCLE_COLLECTION_UNLINK_END

NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(MessagePort,
MessagePortBase)
DOMEventTargetHelper)
if (tmp->mDispatchRunnable) {
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDispatchRunnable->mPort);
}
Expand All @@ -205,10 +195,10 @@ NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(MessagePort)
NS_INTERFACE_MAP_ENTRY(nsIIPCBackgroundChildCreateCallback)
NS_INTERFACE_MAP_ENTRY(nsIObserver)
NS_INTERFACE_MAP_END_INHERITING(MessagePortBase)
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)

NS_IMPL_ADDREF_INHERITED(MessagePort, MessagePortBase)
NS_IMPL_RELEASE_INHERITED(MessagePort, MessagePortBase)
NS_IMPL_ADDREF_INHERITED(MessagePort, DOMEventTargetHelper)
NS_IMPL_RELEASE_INHERITED(MessagePort, DOMEventTargetHelper)

namespace {

Expand Down Expand Up @@ -287,7 +277,7 @@ NS_IMPL_ISUPPORTS(ForceCloseHelper, nsIIPCBackgroundChildCreateCallback)
} // namespace

MessagePort::MessagePort(nsPIDOMWindow* aWindow)
: MessagePortBase(aWindow)
: DOMEventTargetHelper(aWindow)
, mInnerID(0)
, mMessageQueueEnabled(false)
, mIsKeptAlive(false)
Expand Down Expand Up @@ -364,16 +354,7 @@ MessagePort::Initialize(const nsID& aUUID,
// The port has to keep itself alive until it's entangled.
UpdateMustKeepAlive();

if (NS_IsMainThread()) {
MOZ_ASSERT(GetOwner());
MOZ_ASSERT(GetOwner()->IsInnerWindow());
mInnerID = GetOwner()->WindowID();

nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
if (obs) {
obs->AddObserver(this, "inner-window-destroyed", false);
}
} else {
if (!NS_IsMainThread()) {
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
MOZ_ASSERT(workerPrivate);
MOZ_ASSERT(!mWorkerFeature);
Expand All @@ -386,6 +367,15 @@ MessagePort::Initialize(const nsID& aUUID,
}

mWorkerFeature = Move(feature);
} 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);
}
}
}

Expand Down Expand Up @@ -414,7 +404,7 @@ MessagePort::PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage,
continue;
}

MessagePortBase* port = nullptr;
MessagePort* port = nullptr;
nsresult rv = UNWRAP_OBJECT(MessagePort, &value.toObject(), port);
if (NS_FAILED(rv)) {
continue;
Expand Down Expand Up @@ -520,11 +510,6 @@ MessagePort::Dispatch()
void
MessagePort::Close()
{
// Not entangled yet, but already closed.
if (mNextStep != eNextStepNone) {
return;
}

if (mState == eStateUnshippedEntangled) {
MOZ_ASSERT(mUnshippedEntangledPort);

Expand All @@ -540,7 +525,7 @@ MessagePort::Close()
}

// Not entangled yet, we have to wait.
if (mState < eStateEntangling) {
if (mState == eStateEntangling) {
mNextStep = eNextStepClose;
return;
}
Expand Down Expand Up @@ -699,7 +684,7 @@ MessagePort::Disentangle()
UpdateMustKeepAlive();
}

bool
void
MessagePort::CloneAndDisentangle(MessagePortIdentifier& aIdentifier)
{
MOZ_ASSERT(mIdentifier);
Expand All @@ -710,13 +695,13 @@ MessagePort::CloneAndDisentangle(MessagePortIdentifier& aIdentifier)
aIdentifier.neutered() = true;

if (mState > eStateEntangled) {
return true;
return;
}

// We already have a 'next step'. We have to consider this port as already
// cloned/closed/disentangled.
if (mNextStep != eNextStepNone) {
return true;
return;
}

aIdentifier.uuid() = mIdentifier->uuid();
Expand All @@ -739,24 +724,23 @@ MessagePort::CloneAndDisentangle(MessagePortIdentifier& aIdentifier)

mState = eStateDisentangled;
UpdateMustKeepAlive();
return true;
return;
}

// Register this component to PBackground.
ConnectToPBackground();

mNextStep = eNextStepDisentangle;
return true;
return;
}

// Not entangled yet, we have to wait.
if (mState < eStateEntangled) {
mNextStep = eNextStepDisentangle;
return true;
return;
}

StartDisentangling();
return true;
}

void
Expand Down
Loading

0 comments on commit f0aa7a8

Please sign in to comment.