Backed out 8 changesets (bug 1873776) for causing vendor failures. CLOSED TREE
[gecko.git] / dom / messagechannel / MessageChannel.cpp
blobbf9561308232784e5353337d63d4d9ffd30cf7d1
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/. */
7 #include "MessageChannel.h"
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 "mozilla/dom/Document.h"
14 #include "nsIGlobalObject.h"
15 #include "nsServiceManagerUtils.h"
17 namespace mozilla::dom {
19 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(MessageChannel, mGlobal, mPort1, mPort2)
20 NS_IMPL_CYCLE_COLLECTING_ADDREF(MessageChannel)
21 NS_IMPL_CYCLE_COLLECTING_RELEASE(MessageChannel)
23 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MessageChannel)
24 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
25 NS_INTERFACE_MAP_ENTRY(nsISupports)
26 NS_INTERFACE_MAP_END
28 MessageChannel::MessageChannel(nsIGlobalObject* aGlobal) : mGlobal(aGlobal) {
29 MOZ_ASSERT(aGlobal);
32 MessageChannel::~MessageChannel() = default;
34 JSObject* MessageChannel::WrapObject(JSContext* aCx,
35 JS::Handle<JSObject*> aGivenProto) {
36 return MessageChannel_Binding::Wrap(aCx, this, aGivenProto);
39 /* static */
40 already_AddRefed<MessageChannel> MessageChannel::Constructor(
41 const GlobalObject& aGlobal, ErrorResult& aRv) {
42 nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
43 return Constructor(global, aRv);
46 /* static */
47 already_AddRefed<MessageChannel> MessageChannel::Constructor(
48 nsIGlobalObject* aGlobal, ErrorResult& aRv) {
49 MOZ_ASSERT(aGlobal);
51 nsID portUUID1;
52 aRv = nsID::GenerateUUIDInPlace(portUUID1);
53 if (aRv.Failed()) {
54 return nullptr;
57 nsID portUUID2;
58 aRv = nsID::GenerateUUIDInPlace(portUUID2);
59 if (aRv.Failed()) {
60 return nullptr;
63 RefPtr<MessageChannel> channel = new MessageChannel(aGlobal);
65 channel->mPort1 = MessagePort::Create(aGlobal, portUUID1, portUUID2, aRv);
66 if (NS_WARN_IF(aRv.Failed())) {
67 return nullptr;
70 channel->mPort2 = MessagePort::Create(aGlobal, portUUID2, portUUID1, aRv);
71 if (NS_WARN_IF(aRv.Failed())) {
72 return nullptr;
75 channel->mPort1->UnshippedEntangle(channel->mPort2);
76 channel->mPort2->UnshippedEntangle(channel->mPort1);
78 // MessagePorts should not work if created from a disconnected window.
79 nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(aGlobal);
80 if (window && !window->GetDocShell()) {
81 // The 2 ports are entangled. We can close one of them to close the other
82 // too.
83 channel->mPort1->CloseForced();
86 return channel.forget();
89 } // namespace mozilla::dom