|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #include "MobileConnectionArray.h" |
|
8 #include "mozilla/dom/MozMobileConnectionArrayBinding.h" |
|
9 #include "mozilla/Preferences.h" |
|
10 |
|
11 using namespace mozilla::dom; |
|
12 |
|
13 NS_IMPL_CYCLE_COLLECTION_CLASS(MobileConnectionArray) |
|
14 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(MobileConnectionArray) |
|
15 NS_IMPL_CYCLE_COLLECTION_UNLINK(mWindow) |
|
16 // Notify our mobile connections that we're going away. |
|
17 tmp->DropConnections(); |
|
18 NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER |
|
19 NS_IMPL_CYCLE_COLLECTION_UNLINK_END |
|
20 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(MobileConnectionArray) |
|
21 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWindow) |
|
22 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMobileConnections) |
|
23 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS |
|
24 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END |
|
25 NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(MobileConnectionArray) |
|
26 |
|
27 NS_IMPL_CYCLE_COLLECTING_ADDREF(MobileConnectionArray) |
|
28 NS_IMPL_CYCLE_COLLECTING_RELEASE(MobileConnectionArray) |
|
29 |
|
30 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MobileConnectionArray) |
|
31 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY |
|
32 NS_INTERFACE_MAP_ENTRY(nsISupports) |
|
33 NS_INTERFACE_MAP_END |
|
34 |
|
35 MobileConnectionArray::MobileConnectionArray(nsPIDOMWindow* aWindow) |
|
36 : mWindow(aWindow), mInitialized(false) |
|
37 { |
|
38 uint32_t numRil = mozilla::Preferences::GetUint("ril.numRadioInterfaces", 1); |
|
39 MOZ_ASSERT(numRil > 0); |
|
40 |
|
41 mMobileConnections.SetLength(numRil); |
|
42 |
|
43 SetIsDOMBinding(); |
|
44 } |
|
45 |
|
46 MobileConnectionArray::~MobileConnectionArray() |
|
47 { |
|
48 DropConnections(); |
|
49 } |
|
50 |
|
51 void |
|
52 MobileConnectionArray::Init() |
|
53 { |
|
54 mInitialized = true; |
|
55 |
|
56 for (uint32_t id = 0; id < mMobileConnections.Length(); id++) { |
|
57 nsRefPtr<MobileConnection> mobileConnection = new MobileConnection(id); |
|
58 mobileConnection->Init(mWindow); |
|
59 mMobileConnections[id] = mobileConnection; |
|
60 } |
|
61 } |
|
62 |
|
63 void |
|
64 MobileConnectionArray::DropConnections() |
|
65 { |
|
66 if (mInitialized) { |
|
67 for (uint32_t i = 0; i < mMobileConnections.Length(); i++) { |
|
68 mMobileConnections[i]->Shutdown(); |
|
69 } |
|
70 } |
|
71 |
|
72 mMobileConnections.Clear(); |
|
73 } |
|
74 |
|
75 nsPIDOMWindow* |
|
76 MobileConnectionArray::GetParentObject() const |
|
77 { |
|
78 MOZ_ASSERT(mWindow); |
|
79 return mWindow; |
|
80 } |
|
81 |
|
82 JSObject* |
|
83 MobileConnectionArray::WrapObject(JSContext* aCx) |
|
84 { |
|
85 return MozMobileConnectionArrayBinding::Wrap(aCx, this); |
|
86 } |
|
87 |
|
88 nsIDOMMozMobileConnection* |
|
89 MobileConnectionArray::Item(uint32_t aIndex) |
|
90 { |
|
91 bool unused; |
|
92 return IndexedGetter(aIndex, unused); |
|
93 } |
|
94 |
|
95 uint32_t |
|
96 MobileConnectionArray::Length() const |
|
97 { |
|
98 return mMobileConnections.Length(); |
|
99 } |
|
100 |
|
101 nsIDOMMozMobileConnection* |
|
102 MobileConnectionArray::IndexedGetter(uint32_t aIndex, bool& aFound) |
|
103 { |
|
104 if (!mInitialized) { |
|
105 Init(); |
|
106 } |
|
107 |
|
108 aFound = false; |
|
109 aFound = aIndex < mMobileConnections.Length(); |
|
110 |
|
111 return aFound ? mMobileConnections[aIndex] : nullptr; |
|
112 } |