1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "MessageChannel.h"
8 
9 #include "mozilla/dom/MessageChannelBinding.h"
10 #include "mozilla/dom/MessagePort.h"
11 #include "mozilla/dom/Navigator.h"
12 #include "mozilla/dom/WorkerRunnable.h"
13 #include "nsContentUtils.h"
14 #include "mozilla/dom/Document.h"
15 #include "nsIGlobalObject.h"
16 #include "nsServiceManagerUtils.h"
17 
18 namespace mozilla::dom {
19 
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(MessageChannel,mGlobal,mPort1,mPort2)20 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(MessageChannel, mGlobal, mPort1, mPort2)
21 NS_IMPL_CYCLE_COLLECTING_ADDREF(MessageChannel)
22 NS_IMPL_CYCLE_COLLECTING_RELEASE(MessageChannel)
23 
24 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MessageChannel)
25   NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
26   NS_INTERFACE_MAP_ENTRY(nsISupports)
27 NS_INTERFACE_MAP_END
28 
29 MessageChannel::MessageChannel(nsIGlobalObject* aGlobal) : mGlobal(aGlobal) {
30   MOZ_ASSERT(aGlobal);
31 }
32 
33 MessageChannel::~MessageChannel() = default;
34 
WrapObject(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)35 JSObject* MessageChannel::WrapObject(JSContext* aCx,
36                                      JS::Handle<JSObject*> aGivenProto) {
37   return MessageChannel_Binding::Wrap(aCx, this, aGivenProto);
38 }
39 
40 /* static */
Constructor(const GlobalObject & aGlobal,ErrorResult & aRv)41 already_AddRefed<MessageChannel> MessageChannel::Constructor(
42     const GlobalObject& aGlobal, ErrorResult& aRv) {
43   nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
44   return Constructor(global, aRv);
45 }
46 
47 /* static */
Constructor(nsIGlobalObject * aGlobal,ErrorResult & aRv)48 already_AddRefed<MessageChannel> MessageChannel::Constructor(
49     nsIGlobalObject* aGlobal, ErrorResult& aRv) {
50   MOZ_ASSERT(aGlobal);
51 
52   nsID portUUID1;
53   aRv = nsContentUtils::GenerateUUIDInPlace(portUUID1);
54   if (aRv.Failed()) {
55     return nullptr;
56   }
57 
58   nsID portUUID2;
59   aRv = nsContentUtils::GenerateUUIDInPlace(portUUID2);
60   if (aRv.Failed()) {
61     return nullptr;
62   }
63 
64   RefPtr<MessageChannel> channel = new MessageChannel(aGlobal);
65 
66   channel->mPort1 = MessagePort::Create(aGlobal, portUUID1, portUUID2, aRv);
67   if (NS_WARN_IF(aRv.Failed())) {
68     return nullptr;
69   }
70 
71   channel->mPort2 = MessagePort::Create(aGlobal, portUUID2, portUUID1, aRv);
72   if (NS_WARN_IF(aRv.Failed())) {
73     return nullptr;
74   }
75 
76   channel->mPort1->UnshippedEntangle(channel->mPort2);
77   channel->mPort2->UnshippedEntangle(channel->mPort1);
78 
79   // MessagePorts should not work if created from a disconnected window.
80   nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(aGlobal);
81   if (window && !window->GetDocShell()) {
82     // The 2 ports are entangled. We can close one of them to close the other
83     // too.
84     channel->mPort1->CloseForced();
85   }
86 
87   return channel.forget();
88 }
89 
90 }  // namespace mozilla::dom
91