Source code

Revision control

Other Tools

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 {
19
namespace dom {
20
21
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(MessageChannel, mGlobal, mPort1, mPort2)
22
NS_IMPL_CYCLE_COLLECTING_ADDREF(MessageChannel)
23
NS_IMPL_CYCLE_COLLECTING_RELEASE(MessageChannel)
24
25
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MessageChannel)
26
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
27
NS_INTERFACE_MAP_ENTRY(nsISupports)
28
NS_INTERFACE_MAP_END
29
30
MessageChannel::MessageChannel(nsIGlobalObject* aGlobal) : mGlobal(aGlobal) {
31
MOZ_ASSERT(aGlobal);
32
}
33
34
MessageChannel::~MessageChannel() {}
35
36
JSObject* MessageChannel::WrapObject(JSContext* aCx,
37
JS::Handle<JSObject*> aGivenProto) {
38
return MessageChannel_Binding::Wrap(aCx, this, aGivenProto);
39
}
40
41
/* static */
42
already_AddRefed<MessageChannel> MessageChannel::Constructor(
43
const GlobalObject& aGlobal, ErrorResult& aRv) {
44
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
45
return Constructor(global, aRv);
46
}
47
48
/* static */
49
already_AddRefed<MessageChannel> MessageChannel::Constructor(
50
nsIGlobalObject* aGlobal, ErrorResult& aRv) {
51
MOZ_ASSERT(aGlobal);
52
53
nsID portUUID1;
54
aRv = nsContentUtils::GenerateUUIDInPlace(portUUID1);
55
if (aRv.Failed()) {
56
return nullptr;
57
}
58
59
nsID portUUID2;
60
aRv = nsContentUtils::GenerateUUIDInPlace(portUUID2);
61
if (aRv.Failed()) {
62
return nullptr;
63
}
64
65
RefPtr<MessageChannel> channel = new MessageChannel(aGlobal);
66
67
channel->mPort1 = MessagePort::Create(aGlobal, portUUID1, portUUID2, aRv);
68
if (NS_WARN_IF(aRv.Failed())) {
69
return nullptr;
70
}
71
72
channel->mPort2 = MessagePort::Create(aGlobal, portUUID2, portUUID1, aRv);
73
if (NS_WARN_IF(aRv.Failed())) {
74
return nullptr;
75
}
76
77
channel->mPort1->UnshippedEntangle(channel->mPort2);
78
channel->mPort2->UnshippedEntangle(channel->mPort1);
79
80
// MessagePorts should not work if created from a disconnected window.
81
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(aGlobal);
82
if (window && !window->GetDocShell()) {
83
// The 2 ports are entangled. We can close one of them to close the other
84
// too.
85
channel->mPort1->CloseForced();
86
}
87
88
return channel.forget();
89
}
90
91
} // namespace dom
92
} // namespace mozilla