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"
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
)
26 bool gPrefInitialized
= false;
27 bool gPrefEnabled
= false;
33 MessageChannel::PrefEnabled()
35 if (!gPrefInitialized
) {
36 Preferences::AddBoolVarCache(&gPrefEnabled
, "dom.messageChannel.enabled");
37 gPrefInitialized
= true;
43 MessageChannel::MessageChannel(nsPIDOMWindow
* aWindow
)
46 MOZ_COUNT_CTOR(MessageChannel
);
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
);
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());
72 aRv
.Throw(NS_ERROR_UNEXPECTED
);
76 nsRefPtr
<MessageChannel
> channel
= new MessageChannel(window
);
77 return channel
.forget();
81 } // namespace mozilla