|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include "MessageChannel.h" |
|
7 #include "mozilla/Preferences.h" |
|
8 #include "mozilla/dom/MessageChannelBinding.h" |
|
9 #include "mozilla/dom/MessagePort.h" |
|
10 #include "nsContentUtils.h" |
|
11 #include "nsPIDOMWindow.h" |
|
12 |
|
13 namespace mozilla { |
|
14 namespace dom { |
|
15 |
|
16 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_3(MessageChannel, mWindow, mPort1, mPort2) |
|
17 NS_IMPL_CYCLE_COLLECTING_ADDREF(MessageChannel) |
|
18 NS_IMPL_CYCLE_COLLECTING_RELEASE(MessageChannel) |
|
19 |
|
20 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MessageChannel) |
|
21 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY |
|
22 NS_INTERFACE_MAP_ENTRY(nsISupports) |
|
23 NS_INTERFACE_MAP_END |
|
24 |
|
25 namespace { |
|
26 bool gPrefInitialized = false; |
|
27 bool gPrefEnabled = false; |
|
28 |
|
29 } |
|
30 |
|
31 |
|
32 /* static */ bool |
|
33 MessageChannel::PrefEnabled() |
|
34 { |
|
35 if (!gPrefInitialized) { |
|
36 Preferences::AddBoolVarCache(&gPrefEnabled, "dom.messageChannel.enabled"); |
|
37 gPrefInitialized = true; |
|
38 } |
|
39 |
|
40 return gPrefEnabled; |
|
41 } |
|
42 |
|
43 MessageChannel::MessageChannel(nsPIDOMWindow* aWindow) |
|
44 : mWindow(aWindow) |
|
45 { |
|
46 MOZ_COUNT_CTOR(MessageChannel); |
|
47 SetIsDOMBinding(); |
|
48 |
|
49 mPort1 = new MessagePort(mWindow); |
|
50 mPort2 = new MessagePort(mWindow); |
|
51 |
|
52 mPort1->Entangle(mPort2); |
|
53 mPort2->Entangle(mPort1); |
|
54 } |
|
55 |
|
56 MessageChannel::~MessageChannel() |
|
57 { |
|
58 MOZ_COUNT_DTOR(MessageChannel); |
|
59 } |
|
60 |
|
61 JSObject* |
|
62 MessageChannel::WrapObject(JSContext* aCx) |
|
63 { |
|
64 return MessageChannelBinding::Wrap(aCx, this); |
|
65 } |
|
66 |
|
67 /* static */ already_AddRefed<MessageChannel> |
|
68 MessageChannel::Constructor(const GlobalObject& aGlobal, ErrorResult& aRv) |
|
69 { |
|
70 nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.GetAsSupports()); |
|
71 if (!window) { |
|
72 aRv.Throw(NS_ERROR_UNEXPECTED); |
|
73 return nullptr; |
|
74 } |
|
75 |
|
76 nsRefPtr<MessageChannel> channel = new MessageChannel(window); |
|
77 return channel.forget(); |
|
78 } |
|
79 |
|
80 } // namespace dom |
|
81 } // namespace mozilla |