michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 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 "MobileConnectionArray.h" michael@0: #include "mozilla/dom/MozMobileConnectionArrayBinding.h" michael@0: #include "mozilla/Preferences.h" michael@0: michael@0: using namespace mozilla::dom; michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_CLASS(MobileConnectionArray) michael@0: NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(MobileConnectionArray) michael@0: NS_IMPL_CYCLE_COLLECTION_UNLINK(mWindow) michael@0: // Notify our mobile connections that we're going away. michael@0: tmp->DropConnections(); michael@0: NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER michael@0: NS_IMPL_CYCLE_COLLECTION_UNLINK_END michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(MobileConnectionArray) michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWindow) michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMobileConnections) michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END michael@0: NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(MobileConnectionArray) michael@0: michael@0: NS_IMPL_CYCLE_COLLECTING_ADDREF(MobileConnectionArray) michael@0: NS_IMPL_CYCLE_COLLECTING_RELEASE(MobileConnectionArray) michael@0: michael@0: NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MobileConnectionArray) michael@0: NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY michael@0: NS_INTERFACE_MAP_ENTRY(nsISupports) michael@0: NS_INTERFACE_MAP_END michael@0: michael@0: MobileConnectionArray::MobileConnectionArray(nsPIDOMWindow* aWindow) michael@0: : mWindow(aWindow), mInitialized(false) michael@0: { michael@0: uint32_t numRil = mozilla::Preferences::GetUint("ril.numRadioInterfaces", 1); michael@0: MOZ_ASSERT(numRil > 0); michael@0: michael@0: mMobileConnections.SetLength(numRil); michael@0: michael@0: SetIsDOMBinding(); michael@0: } michael@0: michael@0: MobileConnectionArray::~MobileConnectionArray() michael@0: { michael@0: DropConnections(); michael@0: } michael@0: michael@0: void michael@0: MobileConnectionArray::Init() michael@0: { michael@0: mInitialized = true; michael@0: michael@0: for (uint32_t id = 0; id < mMobileConnections.Length(); id++) { michael@0: nsRefPtr mobileConnection = new MobileConnection(id); michael@0: mobileConnection->Init(mWindow); michael@0: mMobileConnections[id] = mobileConnection; michael@0: } michael@0: } michael@0: michael@0: void michael@0: MobileConnectionArray::DropConnections() michael@0: { michael@0: if (mInitialized) { michael@0: for (uint32_t i = 0; i < mMobileConnections.Length(); i++) { michael@0: mMobileConnections[i]->Shutdown(); michael@0: } michael@0: } michael@0: michael@0: mMobileConnections.Clear(); michael@0: } michael@0: michael@0: nsPIDOMWindow* michael@0: MobileConnectionArray::GetParentObject() const michael@0: { michael@0: MOZ_ASSERT(mWindow); michael@0: return mWindow; michael@0: } michael@0: michael@0: JSObject* michael@0: MobileConnectionArray::WrapObject(JSContext* aCx) michael@0: { michael@0: return MozMobileConnectionArrayBinding::Wrap(aCx, this); michael@0: } michael@0: michael@0: nsIDOMMozMobileConnection* michael@0: MobileConnectionArray::Item(uint32_t aIndex) michael@0: { michael@0: bool unused; michael@0: return IndexedGetter(aIndex, unused); michael@0: } michael@0: michael@0: uint32_t michael@0: MobileConnectionArray::Length() const michael@0: { michael@0: return mMobileConnections.Length(); michael@0: } michael@0: michael@0: nsIDOMMozMobileConnection* michael@0: MobileConnectionArray::IndexedGetter(uint32_t aIndex, bool& aFound) michael@0: { michael@0: if (!mInitialized) { michael@0: Init(); michael@0: } michael@0: michael@0: aFound = false; michael@0: aFound = aIndex < mMobileConnections.Length(); michael@0: michael@0: return aFound ? mMobileConnections[aIndex] : nullptr; michael@0: }