Merge inbound to m-c on a CLOSED TREE.
[gecko.git] / dom / base / MessageChannel.cpp
bloba61d2a7e7d142266b8fdf50139849354415df23b
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/. */
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"
13 namespace mozilla {
14 namespace dom {
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)
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
25 namespace {
26 bool gPrefInitialized = false;
27 bool gPrefEnabled = false;
32 /* static */ bool
33 MessageChannel::PrefEnabled()
35 if (!gPrefInitialized) {
36 Preferences::AddBoolVarCache(&gPrefEnabled, "dom.messageChannel.enabled");
37 gPrefInitialized = true;
40 return gPrefEnabled;
43 MessageChannel::MessageChannel(nsPIDOMWindow* aWindow)
44 : mWindow(aWindow)
46 MOZ_COUNT_CTOR(MessageChannel);
47 SetIsDOMBinding();
49 mPort1 = new MessagePort(mWindow);
50 mPort2 = new MessagePort(mWindow);
52 mPort1->Entangle(mPort2);
53 mPort2->Entangle(mPort1);
56 MessageChannel::~MessageChannel()
58 MOZ_COUNT_DTOR(MessageChannel);
61 JSObject*
62 MessageChannel::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
64 return MessageChannelBinding::Wrap(aCx, aScope, this);
67 /* static */ already_AddRefed<MessageChannel>
68 MessageChannel::Constructor(const GlobalObject& aGlobal, ErrorResult& aRv)
70 nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.GetAsSupports());
71 if (!window) {
72 aRv.Throw(NS_ERROR_UNEXPECTED);
73 return nullptr;
76 nsRefPtr<MessageChannel> channel = new MessageChannel(window);
77 return channel.forget();
80 } // namespace dom
81 } // namespace mozilla