Bumping manifests a=b2g-bump
[gecko.git] / dom / base / MessageChannel.cpp
blob4c92c25ad8b24e595a05bd68c5525e27fd9b6ed0
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"
8 #include "mozilla/Preferences.h"
9 #include "mozilla/dom/MessageChannelBinding.h"
10 #include "mozilla/dom/MessagePort.h"
11 #include "nsContentUtils.h"
12 #include "nsPIDOMWindow.h"
14 namespace mozilla {
15 namespace dom {
17 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(MessageChannel, mWindow, mPort1, mPort2)
18 NS_IMPL_CYCLE_COLLECTING_ADDREF(MessageChannel)
19 NS_IMPL_CYCLE_COLLECTING_RELEASE(MessageChannel)
21 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MessageChannel)
22 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
23 NS_INTERFACE_MAP_ENTRY(nsISupports)
24 NS_INTERFACE_MAP_END
26 namespace {
27 bool gPrefInitialized = false;
28 bool gPrefEnabled = false;
32 /* static */ bool
33 MessageChannel::Enabled(JSContext* aCx, JSObject* aObj)
35 if (!gPrefInitialized) {
36 Preferences::AddBoolVarCache(&gPrefEnabled, "dom.messageChannel.enabled");
37 gPrefInitialized = true;
40 // Enabled by pref
41 if (gPrefEnabled) {
42 return true;
45 // Chrome callers are allowed.
46 if (nsContentUtils::ThreadsafeIsCallerChrome()) {
47 return true;
50 nsCOMPtr<nsIPrincipal> principal = nsContentUtils::SubjectPrincipal();
51 MOZ_ASSERT(principal);
53 nsCOMPtr<nsIURI> uri;
54 if (NS_FAILED(principal->GetURI(getter_AddRefs(uri))) || !uri) {
55 return false;
58 bool isResource = false;
59 if (NS_FAILED(uri->SchemeIs("resource", &isResource))) {
60 return false;
63 return isResource;
66 MessageChannel::MessageChannel(nsPIDOMWindow* aWindow)
67 : mWindow(aWindow)
69 MOZ_COUNT_CTOR(MessageChannel);
70 SetIsDOMBinding();
72 mPort1 = new MessagePort(mWindow);
73 mPort2 = new MessagePort(mWindow);
75 mPort1->Entangle(mPort2);
76 mPort2->Entangle(mPort1);
79 MessageChannel::~MessageChannel()
81 MOZ_COUNT_DTOR(MessageChannel);
84 JSObject*
85 MessageChannel::WrapObject(JSContext* aCx)
87 return MessageChannelBinding::Wrap(aCx, this);
90 /* static */ already_AddRefed<MessageChannel>
91 MessageChannel::Constructor(const GlobalObject& aGlobal, ErrorResult& aRv)
93 nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.GetAsSupports());
94 if (!window) {
95 aRv.Throw(NS_ERROR_UNEXPECTED);
96 return nullptr;
99 nsRefPtr<MessageChannel> channel = new MessageChannel(window);
100 return channel.forget();
103 } // namespace dom
104 } // namespace mozilla