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"
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
)
27 bool gPrefInitialized
= false;
28 bool gPrefEnabled
= false;
33 MessageChannel::Enabled(JSContext
* aCx
, JSObject
* aObj
)
35 if (!gPrefInitialized
) {
36 Preferences::AddBoolVarCache(&gPrefEnabled
, "dom.messageChannel.enabled");
37 gPrefInitialized
= true;
45 // Chrome callers are allowed.
46 if (nsContentUtils::ThreadsafeIsCallerChrome()) {
50 nsCOMPtr
<nsIPrincipal
> principal
= nsContentUtils::SubjectPrincipal();
51 MOZ_ASSERT(principal
);
54 if (NS_FAILED(principal
->GetURI(getter_AddRefs(uri
))) || !uri
) {
58 bool isResource
= false;
59 if (NS_FAILED(uri
->SchemeIs("resource", &isResource
))) {
66 MessageChannel::MessageChannel(nsPIDOMWindow
* aWindow
)
69 MOZ_COUNT_CTOR(MessageChannel
);
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
);
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());
95 aRv
.Throw(NS_ERROR_UNEXPECTED
);
99 nsRefPtr
<MessageChannel
> channel
= new MessageChannel(window
);
100 return channel
.forget();
104 } // namespace mozilla