dom/base/MessageChannel.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial