michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "MessageChannel.h" michael@0: #include "mozilla/Preferences.h" michael@0: #include "mozilla/dom/MessageChannelBinding.h" michael@0: #include "mozilla/dom/MessagePort.h" michael@0: #include "nsContentUtils.h" michael@0: #include "nsPIDOMWindow.h" michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_3(MessageChannel, mWindow, mPort1, mPort2) michael@0: NS_IMPL_CYCLE_COLLECTING_ADDREF(MessageChannel) michael@0: NS_IMPL_CYCLE_COLLECTING_RELEASE(MessageChannel) michael@0: michael@0: NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MessageChannel) michael@0: NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY michael@0: NS_INTERFACE_MAP_ENTRY(nsISupports) michael@0: NS_INTERFACE_MAP_END michael@0: michael@0: namespace { michael@0: bool gPrefInitialized = false; michael@0: bool gPrefEnabled = false; michael@0: michael@0: } michael@0: michael@0: michael@0: /* static */ bool michael@0: MessageChannel::PrefEnabled() michael@0: { michael@0: if (!gPrefInitialized) { michael@0: Preferences::AddBoolVarCache(&gPrefEnabled, "dom.messageChannel.enabled"); michael@0: gPrefInitialized = true; michael@0: } michael@0: michael@0: return gPrefEnabled; michael@0: } michael@0: michael@0: MessageChannel::MessageChannel(nsPIDOMWindow* aWindow) michael@0: : mWindow(aWindow) michael@0: { michael@0: MOZ_COUNT_CTOR(MessageChannel); michael@0: SetIsDOMBinding(); michael@0: michael@0: mPort1 = new MessagePort(mWindow); michael@0: mPort2 = new MessagePort(mWindow); michael@0: michael@0: mPort1->Entangle(mPort2); michael@0: mPort2->Entangle(mPort1); michael@0: } michael@0: michael@0: MessageChannel::~MessageChannel() michael@0: { michael@0: MOZ_COUNT_DTOR(MessageChannel); michael@0: } michael@0: michael@0: JSObject* michael@0: MessageChannel::WrapObject(JSContext* aCx) michael@0: { michael@0: return MessageChannelBinding::Wrap(aCx, this); michael@0: } michael@0: michael@0: /* static */ already_AddRefed michael@0: MessageChannel::Constructor(const GlobalObject& aGlobal, ErrorResult& aRv) michael@0: { michael@0: nsCOMPtr window = do_QueryInterface(aGlobal.GetAsSupports()); michael@0: if (!window) { michael@0: aRv.Throw(NS_ERROR_UNEXPECTED); michael@0: return nullptr; michael@0: } michael@0: michael@0: nsRefPtr channel = new MessageChannel(window); michael@0: return channel.forget(); michael@0: } michael@0: michael@0: } // namespace dom michael@0: } // namespace mozilla