Skip to content

Commit 9e960ae

Browse files
committed
Bug 1250572 - Force a parent object in MessagePort/Channel and in StructuredCloneHolder, r=smaug
1 parent 8b94dc4 commit 9e960ae

File tree

8 files changed

+81
-51
lines changed

8 files changed

+81
-51
lines changed

dom/base/StructuredCloneHolder.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ StructuredCloneHolder::Read(nsISupports* aParent,
295295
{
296296
MOZ_ASSERT_IF(mSupportedContext == SameProcessSameThread,
297297
mCreationThread == NS_GetCurrentThread());
298+
MOZ_ASSERT(aParent);
298299

299300
mozilla::AutoRestore<nsISupports*> guard(mParent);
300301
mParent = aParent;
@@ -1044,9 +1045,11 @@ StructuredCloneHolder::CustomReadTransferHandler(JSContext* aCx,
10441045
MOZ_ASSERT(aExtraData < mPortIdentifiers.Length());
10451046
const MessagePortIdentifier& portIdentifier = mPortIdentifiers[aExtraData];
10461047

1048+
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(mParent);
1049+
10471050
ErrorResult rv;
10481051
RefPtr<MessagePort> port =
1049-
MessagePort::Create(mParent, portIdentifier, rv);
1052+
MessagePort::Create(global, portIdentifier, rv);
10501053
if (NS_WARN_IF(rv.Failed())) {
10511054
return false;
10521055
}

dom/messagechannel/MessageChannel.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
#include "mozilla/dom/WorkerRunnable.h"
1414
#include "nsContentUtils.h"
1515
#include "nsIDocument.h"
16+
#include "nsIGlobalObject.h"
1617
#include "nsIPrincipal.h"
17-
#include "nsPIDOMWindow.h"
1818
#include "nsServiceManagerUtils.h"
1919

2020
namespace mozilla {
2121
namespace dom {
2222

23-
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(MessageChannel, mWindow, mPort1, mPort2)
23+
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(MessageChannel, mGlobal, mPort1, mPort2)
2424
NS_IMPL_CYCLE_COLLECTING_ADDREF(MessageChannel)
2525
NS_IMPL_CYCLE_COLLECTING_RELEASE(MessageChannel)
2626

@@ -29,9 +29,10 @@ NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MessageChannel)
2929
NS_INTERFACE_MAP_ENTRY(nsISupports)
3030
NS_INTERFACE_MAP_END
3131

32-
MessageChannel::MessageChannel(nsPIDOMWindowInner* aWindow)
33-
: mWindow(aWindow)
32+
MessageChannel::MessageChannel(nsIGlobalObject* aGlobal)
33+
: mGlobal(aGlobal)
3434
{
35+
MOZ_ASSERT(aGlobal);
3536
}
3637

3738
MessageChannel::~MessageChannel()
@@ -47,14 +48,15 @@ MessageChannel::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
4748
/* static */ already_AddRefed<MessageChannel>
4849
MessageChannel::Constructor(const GlobalObject& aGlobal, ErrorResult& aRv)
4950
{
50-
// window can be null in workers.
51-
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(aGlobal.GetAsSupports());
52-
return Constructor(window, aRv);
51+
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
52+
return Constructor(global, aRv);
5353
}
5454

5555
/* static */ already_AddRefed<MessageChannel>
56-
MessageChannel::Constructor(nsPIDOMWindowInner* aWindow, ErrorResult& aRv)
56+
MessageChannel::Constructor(nsIGlobalObject* aGlobal, ErrorResult& aRv)
5757
{
58+
MOZ_ASSERT(aGlobal);
59+
5860
nsID portUUID1;
5961
aRv = nsContentUtils::GenerateUUIDInPlace(portUUID1);
6062
if (aRv.Failed()) {
@@ -67,14 +69,14 @@ MessageChannel::Constructor(nsPIDOMWindowInner* aWindow, ErrorResult& aRv)
6769
return nullptr;
6870
}
6971

70-
RefPtr<MessageChannel> channel = new MessageChannel(aWindow);
72+
RefPtr<MessageChannel> channel = new MessageChannel(aGlobal);
7173

72-
channel->mPort1 = MessagePort::Create(aWindow, portUUID1, portUUID2, aRv);
74+
channel->mPort1 = MessagePort::Create(aGlobal, portUUID1, portUUID2, aRv);
7375
if (NS_WARN_IF(aRv.Failed())) {
7476
return nullptr;
7577
}
7678

77-
channel->mPort2 = MessagePort::Create(aWindow, portUUID2, portUUID1, aRv);
79+
channel->mPort2 = MessagePort::Create(aGlobal, portUUID2, portUUID1, aRv);
7880
if (NS_WARN_IF(aRv.Failed())) {
7981
return nullptr;
8082
}

dom/messagechannel/MessageChannel.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "nsWrapperCache.h"
1515
#include "nsCOMPtr.h"
1616

17-
class nsPIDOMWindowInner;
17+
class nsIGlobalObject;
1818

1919
namespace mozilla {
2020
namespace dom {
@@ -28,10 +28,10 @@ class MessageChannel final : public nsISupports
2828
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
2929
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(MessageChannel)
3030

31-
nsPIDOMWindowInner*
31+
nsIGlobalObject*
3232
GetParentObject() const
3333
{
34-
return mWindow;
34+
return mGlobal;
3535
}
3636

3737
virtual JSObject*
@@ -41,7 +41,7 @@ class MessageChannel final : public nsISupports
4141
Constructor(const GlobalObject& aGlobal, ErrorResult& aRv);
4242

4343
static already_AddRefed<MessageChannel>
44-
Constructor(nsPIDOMWindowInner* aWindow, ErrorResult& aRv);
44+
Constructor(nsIGlobalObject* aGlobal, ErrorResult& aRv);
4545

4646
MessagePort*
4747
Port1() const
@@ -56,10 +56,10 @@ class MessageChannel final : public nsISupports
5656
}
5757

5858
private:
59-
explicit MessageChannel(nsPIDOMWindowInner* aWindow);
59+
explicit MessageChannel(nsIGlobalObject* aGlobal);
6060
~MessageChannel();
6161

62-
nsCOMPtr<nsPIDOMWindowInner> mWindow;
62+
nsCOMPtr<nsIGlobalObject> mGlobal;
6363

6464
RefPtr<MessagePort> mPort1;
6565
RefPtr<MessagePort> mPort2;

dom/messagechannel/MessagePort.cpp

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,8 @@ class PostMessageRunnable final : public nsICancelableRunnable
8989
nsresult
9090
DispatchMessage() const
9191
{
92-
nsCOMPtr<nsIGlobalObject> globalObject;
93-
94-
if (NS_IsMainThread()) {
95-
globalObject = do_QueryInterface(mPort->GetParentObject());
96-
} else {
97-
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
98-
MOZ_ASSERT(workerPrivate);
99-
globalObject = workerPrivate->GlobalScope();
100-
}
92+
nsCOMPtr<nsIGlobalObject> globalObject = mPort->GetParentObject();
93+
MOZ_ASSERT(globalObject);
10194

10295
AutoJSAPI jsapi;
10396
if (!globalObject || !jsapi.Init(globalObject)) {
@@ -282,20 +275,17 @@ NS_IMPL_ISUPPORTS(ForceCloseHelper, nsIIPCBackgroundChildCreateCallback)
282275

283276
} // namespace
284277

285-
MessagePort::MessagePort(nsISupports* aSupports)
286-
: mInnerID(0)
278+
MessagePort::MessagePort(nsIGlobalObject* aGlobal)
279+
: DOMEventTargetHelper(aGlobal)
280+
, mInnerID(0)
287281
, mMessageQueueEnabled(false)
288282
, mIsKeptAlive(false)
289283
{
284+
MOZ_ASSERT(aGlobal);
285+
290286
mIdentifier = new MessagePortIdentifier();
291287
mIdentifier->neutered() = true;
292288
mIdentifier->sequenceId() = 0;
293-
294-
nsCOMPtr<nsIGlobalObject> globalObject = do_QueryInterface(aSupports);
295-
if (NS_WARN_IF(!globalObject)) {
296-
return;
297-
}
298-
BindToOwner(globalObject);
299289
}
300290

301291
MessagePort::~MessagePort()
@@ -305,21 +295,25 @@ MessagePort::~MessagePort()
305295
}
306296

307297
/* static */ already_AddRefed<MessagePort>
308-
MessagePort::Create(nsISupports* aSupport, const nsID& aUUID,
298+
MessagePort::Create(nsIGlobalObject* aGlobal, const nsID& aUUID,
309299
const nsID& aDestinationUUID, ErrorResult& aRv)
310300
{
311-
RefPtr<MessagePort> mp = new MessagePort(aSupport);
301+
MOZ_ASSERT(aGlobal);
302+
303+
RefPtr<MessagePort> mp = new MessagePort(aGlobal);
312304
mp->Initialize(aUUID, aDestinationUUID, 1 /* 0 is an invalid sequence ID */,
313305
false /* Neutered */, eStateUnshippedEntangled, aRv);
314306
return mp.forget();
315307
}
316308

317309
/* static */ already_AddRefed<MessagePort>
318-
MessagePort::Create(nsISupports* aSupport,
310+
MessagePort::Create(nsIGlobalObject* aGlobal,
319311
const MessagePortIdentifier& aIdentifier,
320312
ErrorResult& aRv)
321313
{
322-
RefPtr<MessagePort> mp = new MessagePort(aSupport);
314+
MOZ_ASSERT(aGlobal);
315+
316+
RefPtr<MessagePort> mp = new MessagePort(aGlobal);
323317
mp->Initialize(aIdentifier.uuid(), aIdentifier.destinationUuid(),
324318
aIdentifier.sequenceId(), aIdentifier.neutered(),
325319
eStateEntangling, aRv);

dom/messagechannel/MessagePort.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#undef PostMessage
1717
#endif
1818

19-
class nsPIDOMWindowInner;
19+
class nsIGlobalObject;
2020

2121
namespace mozilla {
2222
namespace dom {
@@ -45,11 +45,12 @@ class MessagePort final : public DOMEventTargetHelper
4545
DOMEventTargetHelper)
4646

4747
static already_AddRefed<MessagePort>
48-
Create(nsISupports* aSupport, const nsID& aUUID,
48+
Create(nsIGlobalObject* aGlobal, const nsID& aUUID,
4949
const nsID& aDestinationUUID, ErrorResult& aRv);
5050

5151
static already_AddRefed<MessagePort>
52-
Create(nsISupports* aSupport, const MessagePortIdentifier& aIdentifier,
52+
Create(nsIGlobalObject* aGlobal,
53+
const MessagePortIdentifier& aIdentifier,
5354
ErrorResult& aRv);
5455

5556
// For IPC.
@@ -88,7 +89,7 @@ class MessagePort final : public DOMEventTargetHelper
8889
void Closed();
8990

9091
private:
91-
explicit MessagePort(nsISupports* nsISupports);
92+
explicit MessagePort(nsIGlobalObject* aGlobal);
9293
~MessagePort();
9394

9495
enum State {

dom/workers/RuntimeService.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2288,13 +2288,14 @@ RuntimeService::CreateSharedWorkerFromLoadInfo(JSContext* aCx,
22882288

22892289
// We don't actually care about this MessageChannel, but we use it to 'steal'
22902290
// its 2 connected ports.
2291-
RefPtr<MessageChannel> channel = MessageChannel::Constructor(window, rv);
2291+
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(window);
2292+
RefPtr<MessageChannel> channel = MessageChannel::Constructor(global, rv);
22922293
if (NS_WARN_IF(rv.Failed())) {
22932294
return rv.StealNSResult();
22942295
}
22952296

22962297
RefPtr<SharedWorker> sharedWorker = new SharedWorker(window, workerPrivate,
2297-
channel->Port1());
2298+
channel->Port1());
22982299

22992300
if (!workerPrivate->RegisterSharedWorker(sharedWorker, channel->Port2())) {
23002301
NS_WARNING("Worker is unreachable, this shouldn't happen!");

dom/workers/WorkerPrivate.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -668,11 +668,24 @@ class MessageEventRunnable final : public WorkerRunnable
668668
DispatchDOMEvent(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
669669
DOMEventTargetHelper* aTarget, bool aIsMainThread)
670670
{
671-
nsCOMPtr<nsPIDOMWindowInner> parent;
672-
if (aIsMainThread) {
673-
parent = do_QueryInterface(aTarget->GetParentObject());
671+
nsCOMPtr<nsIGlobalObject> parent = do_QueryInterface(aTarget->GetParentObject());
672+
673+
// For some workers without window, parent is null and we try to find it
674+
// from the JS Context.
675+
if (!parent) {
676+
JS::Rooted<JSObject*> globalObject(aCx, JS::CurrentGlobalOrNull(aCx));
677+
if (NS_WARN_IF(!globalObject)) {
678+
return false;
679+
}
680+
681+
parent = xpc::NativeGlobal(globalObject);
682+
if (NS_WARN_IF(!parent)) {
683+
return false;
684+
}
674685
}
675686

687+
MOZ_ASSERT(parent);
688+
676689
JS::Rooted<JS::Value> messageData(aCx);
677690
ErrorResult rv;
678691

@@ -6376,7 +6389,7 @@ WorkerPrivate::ConnectMessagePort(JSContext* aCx,
63766389
// This MessagePortIdentifier is used to create a new port, still connected
63776390
// with the other one, but in the worker thread.
63786391
ErrorResult rv;
6379-
RefPtr<MessagePort> port = MessagePort::Create(nullptr, aIdentifier, rv);
6392+
RefPtr<MessagePort> port = MessagePort::Create(globalScope, aIdentifier, rv);
63806393
if (NS_WARN_IF(rv.Failed())) {
63816394
return false;
63826395
}

dom/workers/XMLHttpRequest.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "nsVariant.h"
2929

3030
#include "RuntimeService.h"
31+
#include "WorkerScope.h"
3132
#include "WorkerPrivate.h"
3233
#include "WorkerRunnable.h"
3334
#include "XMLHttpRequestUpload.h"
@@ -1355,7 +1356,12 @@ EventRunnable::WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
13551356

13561357
ErrorResult rv;
13571358
JS::Rooted<JS::Value> response(aCx);
1358-
Read(nullptr, aCx, &response, rv);
1359+
1360+
GlobalObject globalObj(aCx, aWorkerPrivate->GlobalScope()->GetWrapper());
1361+
nsCOMPtr<nsIGlobalObject> global =
1362+
do_QueryInterface(globalObj.GetAsSupports());
1363+
1364+
Read(global, aCx, &response, rv);
13591365
if (NS_WARN_IF(rv.Failed())) {
13601366
rv.SuppressException();
13611367
return false;
@@ -1532,8 +1538,18 @@ SendRunnable::MainThreadRun()
15321538

15331539
ErrorResult rv;
15341540

1541+
JS::Rooted<JSObject*> globalObject(cx, JS::CurrentGlobalOrNull(cx));
1542+
if (NS_WARN_IF(!globalObject)) {
1543+
return NS_ERROR_FAILURE;
1544+
}
1545+
1546+
nsCOMPtr<nsIGlobalObject> parent = xpc::NativeGlobal(globalObject);
1547+
if (NS_WARN_IF(!parent)) {
1548+
return NS_ERROR_FAILURE;
1549+
}
1550+
15351551
JS::Rooted<JS::Value> body(cx);
1536-
Read(nullptr, cx, &body, rv);
1552+
Read(parent, cx, &body, rv);
15371553
if (NS_WARN_IF(rv.Failed())) {
15381554
return rv.StealNSResult();
15391555
}

0 commit comments

Comments
 (0)