dom/mobileconnection/src/MobileConnectionArray.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/mobileconnection/src/MobileConnectionArray.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,112 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=8 sts=2 et sw=2 tw=80: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "MobileConnectionArray.h"
    1.11 +#include "mozilla/dom/MozMobileConnectionArrayBinding.h"
    1.12 +#include "mozilla/Preferences.h"
    1.13 +
    1.14 +using namespace mozilla::dom;
    1.15 +
    1.16 +NS_IMPL_CYCLE_COLLECTION_CLASS(MobileConnectionArray)
    1.17 +NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(MobileConnectionArray)
    1.18 +  NS_IMPL_CYCLE_COLLECTION_UNLINK(mWindow)
    1.19 +  // Notify our mobile connections that we're going away.
    1.20 +  tmp->DropConnections();
    1.21 +  NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
    1.22 +NS_IMPL_CYCLE_COLLECTION_UNLINK_END
    1.23 +NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(MobileConnectionArray)
    1.24 +  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWindow)
    1.25 +  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMobileConnections)
    1.26 +  NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
    1.27 +NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
    1.28 +NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(MobileConnectionArray)
    1.29 +
    1.30 +NS_IMPL_CYCLE_COLLECTING_ADDREF(MobileConnectionArray)
    1.31 +NS_IMPL_CYCLE_COLLECTING_RELEASE(MobileConnectionArray)
    1.32 +
    1.33 +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MobileConnectionArray)
    1.34 +  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
    1.35 +  NS_INTERFACE_MAP_ENTRY(nsISupports)
    1.36 +NS_INTERFACE_MAP_END
    1.37 +
    1.38 +MobileConnectionArray::MobileConnectionArray(nsPIDOMWindow* aWindow)
    1.39 +: mWindow(aWindow), mInitialized(false)
    1.40 +{
    1.41 +  uint32_t numRil = mozilla::Preferences::GetUint("ril.numRadioInterfaces", 1);
    1.42 +  MOZ_ASSERT(numRil > 0);
    1.43 +
    1.44 +  mMobileConnections.SetLength(numRil);
    1.45 +
    1.46 +  SetIsDOMBinding();
    1.47 +}
    1.48 +
    1.49 +MobileConnectionArray::~MobileConnectionArray()
    1.50 +{
    1.51 +  DropConnections();
    1.52 +}
    1.53 +
    1.54 +void
    1.55 +MobileConnectionArray::Init()
    1.56 +{
    1.57 +  mInitialized = true;
    1.58 +
    1.59 +  for (uint32_t id = 0; id < mMobileConnections.Length(); id++) {
    1.60 +    nsRefPtr<MobileConnection> mobileConnection = new MobileConnection(id);
    1.61 +    mobileConnection->Init(mWindow);
    1.62 +    mMobileConnections[id] = mobileConnection;
    1.63 +  }
    1.64 +}
    1.65 +
    1.66 +void
    1.67 +MobileConnectionArray::DropConnections()
    1.68 +{
    1.69 +  if (mInitialized) {
    1.70 +    for (uint32_t i = 0; i < mMobileConnections.Length(); i++) {
    1.71 +      mMobileConnections[i]->Shutdown();
    1.72 +    }
    1.73 +  }
    1.74 +
    1.75 +  mMobileConnections.Clear();
    1.76 +}
    1.77 +
    1.78 +nsPIDOMWindow*
    1.79 +MobileConnectionArray::GetParentObject() const
    1.80 +{
    1.81 +  MOZ_ASSERT(mWindow);
    1.82 +  return mWindow;
    1.83 +}
    1.84 +
    1.85 +JSObject*
    1.86 +MobileConnectionArray::WrapObject(JSContext* aCx)
    1.87 +{
    1.88 +  return MozMobileConnectionArrayBinding::Wrap(aCx, this);
    1.89 +}
    1.90 +
    1.91 +nsIDOMMozMobileConnection*
    1.92 +MobileConnectionArray::Item(uint32_t aIndex)
    1.93 +{
    1.94 +  bool unused;
    1.95 +  return IndexedGetter(aIndex, unused);
    1.96 +}
    1.97 +
    1.98 +uint32_t
    1.99 +MobileConnectionArray::Length() const
   1.100 +{
   1.101 +  return mMobileConnections.Length();
   1.102 +}
   1.103 +
   1.104 +nsIDOMMozMobileConnection*
   1.105 +MobileConnectionArray::IndexedGetter(uint32_t aIndex, bool& aFound)
   1.106 +{
   1.107 +  if (!mInitialized) {
   1.108 +    Init();
   1.109 +  }
   1.110 +
   1.111 +  aFound = false;
   1.112 +  aFound = aIndex < mMobileConnections.Length();
   1.113 +
   1.114 +  return aFound ? mMobileConnections[aIndex] : nullptr;
   1.115 +}

mercurial