dom/base/MessageChannel.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/base/MessageChannel.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,81 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "MessageChannel.h"
    1.10 +#include "mozilla/Preferences.h"
    1.11 +#include "mozilla/dom/MessageChannelBinding.h"
    1.12 +#include "mozilla/dom/MessagePort.h"
    1.13 +#include "nsContentUtils.h"
    1.14 +#include "nsPIDOMWindow.h"
    1.15 +
    1.16 +namespace mozilla {
    1.17 +namespace dom {
    1.18 +
    1.19 +NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_3(MessageChannel, mWindow, mPort1, mPort2)
    1.20 +NS_IMPL_CYCLE_COLLECTING_ADDREF(MessageChannel)
    1.21 +NS_IMPL_CYCLE_COLLECTING_RELEASE(MessageChannel)
    1.22 +
    1.23 +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MessageChannel)
    1.24 +  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
    1.25 +  NS_INTERFACE_MAP_ENTRY(nsISupports)
    1.26 +NS_INTERFACE_MAP_END
    1.27 +
    1.28 +namespace {
    1.29 +  bool gPrefInitialized = false;
    1.30 +  bool gPrefEnabled = false;
    1.31 +
    1.32 +}
    1.33 +
    1.34 +
    1.35 +/* static */ bool
    1.36 +MessageChannel::PrefEnabled()
    1.37 +{
    1.38 +  if (!gPrefInitialized) {
    1.39 +    Preferences::AddBoolVarCache(&gPrefEnabled, "dom.messageChannel.enabled");
    1.40 +    gPrefInitialized = true;
    1.41 +  }
    1.42 +
    1.43 +  return gPrefEnabled;
    1.44 +}
    1.45 +
    1.46 +MessageChannel::MessageChannel(nsPIDOMWindow* aWindow)
    1.47 +  : mWindow(aWindow)
    1.48 +{
    1.49 +  MOZ_COUNT_CTOR(MessageChannel);
    1.50 +  SetIsDOMBinding();
    1.51 +
    1.52 +  mPort1 = new MessagePort(mWindow);
    1.53 +  mPort2 = new MessagePort(mWindow);
    1.54 +
    1.55 +  mPort1->Entangle(mPort2);
    1.56 +  mPort2->Entangle(mPort1);
    1.57 +}
    1.58 +
    1.59 +MessageChannel::~MessageChannel()
    1.60 +{
    1.61 +  MOZ_COUNT_DTOR(MessageChannel);
    1.62 +}
    1.63 +
    1.64 +JSObject*
    1.65 +MessageChannel::WrapObject(JSContext* aCx)
    1.66 +{
    1.67 +  return MessageChannelBinding::Wrap(aCx, this);
    1.68 +}
    1.69 +
    1.70 +/* static */ already_AddRefed<MessageChannel>
    1.71 +MessageChannel::Constructor(const GlobalObject& aGlobal, ErrorResult& aRv)
    1.72 +{
    1.73 +  nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.GetAsSupports());
    1.74 +  if (!window) {
    1.75 +    aRv.Throw(NS_ERROR_UNEXPECTED);
    1.76 +    return nullptr;
    1.77 +  }
    1.78 +
    1.79 +  nsRefPtr<MessageChannel> channel = new MessageChannel(window);
    1.80 +  return channel.forget();
    1.81 +}
    1.82 +
    1.83 +} // namespace dom
    1.84 +} // namespace mozilla

mercurial