Bug 814629 - Part 4: MobileConnectionArray implementation. f=hsinyi r=khuey
authorJessica Jong <jjong@mozilla.com>
Wed, 13 Nov 2013 16:51:23 +0800
changeset 154683 5b9e451f9d2c89fb2a60f52f8a610c4033855825
parent 154682 3bca3d180cff417a45eea5fe69e593b0ae48dfdb
child 154684 5b2f8a5fcd7f7715dff838ffb8d1a5cf3bf815d3
push id25653
push userryanvm@gmail.com
push dateFri, 15 Nov 2013 01:57:38 +0000
treeherdermozilla-central@319c3d822ae4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskhuey
bugs814629
milestone28.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 814629 - Part 4: MobileConnectionArray implementation. f=hsinyi r=khuey
dom/base/Navigator.cpp
dom/base/Navigator.h
dom/network/src/MobileConnectionArray.cpp
dom/network/src/MobileConnectionArray.h
dom/network/src/moz.build
--- a/dom/base/Navigator.cpp
+++ b/dom/base/Navigator.cpp
@@ -35,17 +35,17 @@
 #include "mozilla/ClearOnShutdown.h"
 #include "mozilla/StaticPtr.h"
 #include "Connection.h"
 #include "nsDOMEvent.h"
 #include "nsGlobalWindow.h"
 #ifdef MOZ_B2G_RIL
 #include "mozilla/dom/IccManager.h"
 #include "mozilla/dom/CellBroadcast.h"
-#include "mozilla/dom/network/MobileConnection.h"
+#include "mozilla/dom/network/MobileConnectionArray.h"
 #include "mozilla/dom/Voicemail.h"
 #endif
 #include "nsIIdleObserver.h"
 #include "nsIPermissionManager.h"
 #include "nsNetUtil.h"
 #include "nsIHttpChannel.h"
 #include "TimeManager.h"
 #include "DeviceStorage.h"
@@ -136,17 +136,17 @@ NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(
   NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mGeolocation)
   NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mNotification)
   NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mBatteryManager)
   NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPowerManager)
   NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMobileMessageManager)
   NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mTelephony)
   NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mConnection)
 #ifdef MOZ_B2G_RIL
-  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMobileConnection)
+  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMobileConnections)
   NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCellBroadcast)
   NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mIccManager)
   NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mVoicemail)
 #endif
 #ifdef MOZ_B2G_BT
   NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mBluetooth)
 #endif
 #ifdef MOZ_AUDIO_CHANNEL_MANAGER
@@ -214,19 +214,18 @@ Navigator::Invalidate()
   }
 
   if (mConnection) {
     mConnection->Shutdown();
     mConnection = nullptr;
   }
 
 #ifdef MOZ_B2G_RIL
-  if (mMobileConnection) {
-    mMobileConnection->Shutdown();
-    mMobileConnection = nullptr;
+  if (mMobileConnections) {
+    mMobileConnections = nullptr;
   }
 
   if (mCellBroadcast) {
     mCellBroadcast = nullptr;
   }
 
   if (mIccManager) {
     mIccManager->Shutdown();
@@ -1176,16 +1175,30 @@ Navigator::GetMozTelephony(ErrorResult& 
     mTelephony = Telephony::Create(mWindow, aRv);
   }
 
   return mTelephony;
 }
 
 #ifdef MOZ_B2G_RIL
 
+network::MobileConnectionArray*
+Navigator::GetMozMobileConnections(ErrorResult& aRv)
+{
+  if (!mMobileConnections) {
+    if (!mWindow) {
+      aRv.Throw(NS_ERROR_UNEXPECTED);
+      return nullptr;
+    }
+    mMobileConnections = new network::MobileConnectionArray(mWindow);
+  }
+
+  return mMobileConnections;
+}
+
 CellBroadcast*
 Navigator::GetMozCellBroadcast(ErrorResult& aRv)
 {
   if (!mCellBroadcast) {
     if (!mWindow) {
       aRv.Throw(NS_ERROR_UNEXPECTED);
       return nullptr;
     }
@@ -1267,33 +1280,16 @@ Navigator::GetMozConnection()
 
     mConnection = new network::Connection();
     mConnection->Init(mWindow);
   }
 
   return mConnection;
 }
 
-#ifdef MOZ_B2G_RIL
-nsIDOMMozMobileConnection*
-Navigator::GetMozMobileConnection(ErrorResult& aRv)
-{
-  if (!mMobileConnection) {
-    if (!mWindow) {
-      aRv.Throw(NS_ERROR_UNEXPECTED);
-      return nullptr;
-    }
-    mMobileConnection = new network::MobileConnection();
-    mMobileConnection->Init(mWindow);
-  }
-
-  return mMobileConnection;
-}
-#endif // MOZ_B2G_RIL
-
 #ifdef MOZ_B2G_BT
 bluetooth::BluetoothManager*
 Navigator::GetMozBluetooth(ErrorResult& aRv)
 {
   if (!mBluetooth) {
     if (!mWindow) {
       aRv.Throw(NS_ERROR_UNEXPECTED);
       return nullptr;
--- a/dom/base/Navigator.h
+++ b/dom/base/Navigator.h
@@ -30,17 +30,16 @@ namespace dom {
 class Geolocation;
 class systemMessageCallback;
 class MediaStreamConstraints;
 class MediaStreamConstraintsInternal;
 }
 }
 
 #ifdef MOZ_B2G_RIL
-class nsIDOMMozMobileConnection;
 class nsIDOMMozIccManager;
 #endif // MOZ_B2G_RIL
 
 //*****************************************************************************
 // Navigator: Script "navigator" object
 //*****************************************************************************
 
 void NS_GetNavigatorAppName(nsAString& aAppName);
@@ -68,17 +67,17 @@ class Gamepad;
 class NavigatorUserMediaSuccessCallback;
 class NavigatorUserMediaErrorCallback;
 class MozGetUserMediaDevicesSuccessCallback;
 #endif // MOZ_MEDIA_NAVIGATOR
 
 namespace network {
 class Connection;
 #ifdef MOZ_B2G_RIL
-class MobileConnection;
+class MobileConnectionArray;
 #endif
 } // namespace Connection;
 
 #ifdef MOZ_B2G_BT
 namespace bluetooth {
 class BluetoothManager;
 } // namespace bluetooth
 #endif // MOZ_B2G_BT
@@ -214,17 +213,17 @@ public:
   Telephony* GetMozTelephony(ErrorResult& aRv);
   nsIDOMMozConnection* GetMozConnection();
   nsDOMCameraManager* GetMozCameras(ErrorResult& aRv);
   void MozSetMessageHandler(const nsAString& aType,
                             systemMessageCallback* aCallback,
                             ErrorResult& aRv);
   bool MozHasPendingMessage(const nsAString& aType, ErrorResult& aRv);
 #ifdef MOZ_B2G_RIL
-  nsIDOMMozMobileConnection* GetMozMobileConnection(ErrorResult& aRv);
+  network::MobileConnectionArray* GetMozMobileConnections(ErrorResult& aRv);
   CellBroadcast* GetMozCellBroadcast(ErrorResult& aRv);
   Voicemail* GetMozVoicemail(ErrorResult& aRv);
   nsIDOMMozIccManager* GetMozIccManager(ErrorResult& aRv);
 #endif // MOZ_B2G_RIL
 #ifdef MOZ_GAMEPAD
   void GetGamepads(nsTArray<nsRefPtr<Gamepad> >& aGamepads, ErrorResult& aRv);
 #endif // MOZ_GAMEPAD
 #ifdef MOZ_B2G_FM
@@ -324,17 +323,17 @@ private:
 #ifdef MOZ_B2G_FM
   nsRefPtr<FMRadio> mFMRadio;
 #endif
   nsRefPtr<PowerManager> mPowerManager;
   nsRefPtr<MobileMessageManager> mMobileMessageManager;
   nsRefPtr<Telephony> mTelephony;
   nsRefPtr<network::Connection> mConnection;
 #ifdef MOZ_B2G_RIL
-  nsRefPtr<network::MobileConnection> mMobileConnection;
+  nsRefPtr<network::MobileConnectionArray> mMobileConnections;
   nsRefPtr<CellBroadcast> mCellBroadcast;
   nsRefPtr<IccManager> mIccManager;
   nsRefPtr<Voicemail> mVoicemail;
 #endif
 #ifdef MOZ_B2G_BT
   nsCOMPtr<bluetooth::BluetoothManager> mBluetooth;
 #endif
 #ifdef MOZ_AUDIO_CHANNEL_MANAGER
new file mode 100644
--- /dev/null
+++ b/dom/network/src/MobileConnectionArray.cpp
@@ -0,0 +1,81 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "MobileConnectionArray.h"
+#include "mozilla/dom/MozMobileConnectionArrayBinding.h"
+#include "mozilla/Preferences.h"
+
+using namespace mozilla::dom::network;
+
+NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_2(MobileConnectionArray,
+                                        mWindow,
+                                        mMobileConnections)
+
+NS_IMPL_CYCLE_COLLECTING_ADDREF(MobileConnectionArray)
+NS_IMPL_CYCLE_COLLECTING_RELEASE(MobileConnectionArray)
+
+NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MobileConnectionArray)
+  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
+  NS_INTERFACE_MAP_ENTRY(nsISupports)
+NS_INTERFACE_MAP_END
+
+MobileConnectionArray::MobileConnectionArray(nsPIDOMWindow* aWindow)
+: mWindow(aWindow)
+{
+  int32_t numRil = mozilla::Preferences::GetInt("ril.numRadioInterfaces", 1);
+  MOZ_ASSERT(numRil > 0);
+
+  for (int32_t id = 0; id < numRil; id++) {
+    nsRefPtr<MobileConnection> mobileConnection = new MobileConnection(id);
+    mobileConnection->Init(aWindow);
+    mMobileConnections.AppendElement(mobileConnection);
+  }
+
+  SetIsDOMBinding();
+}
+
+MobileConnectionArray::~MobileConnectionArray()
+{
+  for (uint32_t i = 0; i < mMobileConnections.Length(); i++) {
+    mMobileConnections[i]->Shutdown();
+  }
+  mMobileConnections.Clear();
+}
+
+nsPIDOMWindow*
+MobileConnectionArray::GetParentObject() const
+{
+  MOZ_ASSERT(mWindow);
+  return mWindow;
+}
+
+JSObject*
+MobileConnectionArray::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
+{
+  return MozMobileConnectionArrayBinding::Wrap(aCx, aScope, this);
+}
+
+nsIDOMMozMobileConnection*
+MobileConnectionArray::Item(uint32_t aIndex) const
+{
+  bool unused;
+  return IndexedGetter(aIndex, unused);
+}
+
+uint32_t
+MobileConnectionArray::Length() const
+{
+  return mMobileConnections.Length();
+}
+
+nsIDOMMozMobileConnection*
+MobileConnectionArray::IndexedGetter(uint32_t aIndex, bool& aFound) const
+{
+  aFound = false;
+  aFound = aIndex < mMobileConnections.Length();
+
+  return aFound ? mMobileConnections[aIndex] : nullptr;
+}
\ No newline at end of file
new file mode 100644
--- /dev/null
+++ b/dom/network/src/MobileConnectionArray.h
@@ -0,0 +1,56 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_dom_network_MobileConnectionArray_h__
+#define mozilla_dom_network_MobileConnectionArray_h__
+
+#include "nsWrapperCache.h"
+#include "mozilla/dom/network/MobileConnection.h"
+
+class nsIDOMMozMobileConnection;
+
+namespace mozilla {
+namespace dom {
+namespace network {
+
+class MobileConnectionArray MOZ_FINAL : public nsISupports,
+                                        public nsWrapperCache
+{
+public:
+  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
+  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(MobileConnectionArray)
+
+  MobileConnectionArray(nsPIDOMWindow* aWindow);
+
+  nsPIDOMWindow*
+  GetParentObject() const;
+
+  // WrapperCache
+  virtual JSObject*
+  WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope) MOZ_OVERRIDE;
+
+  //  WebIDL
+  nsIDOMMozMobileConnection*
+  Item(uint32_t aIndex) const;
+
+  uint32_t
+  Length() const;
+
+  nsIDOMMozMobileConnection*
+  IndexedGetter(uint32_t aIndex, bool& aFound) const;
+
+private:
+  ~MobileConnectionArray();
+
+  nsCOMPtr<nsPIDOMWindow> mWindow;
+  nsTArray<nsRefPtr<MobileConnection>> mMobileConnections;
+};
+
+} // namespace network
+} // namespace dom
+} // namespace mozilla
+
+#endif // mozilla_dom_network_MobileConnectionArray_h__
\ No newline at end of file
--- a/dom/network/src/moz.build
+++ b/dom/network/src/moz.build
@@ -23,19 +23,21 @@ SOURCES += [
     'TCPSocketParent.cpp',
     'UDPSocketChild.cpp',
     'UDPSocketParent.cpp',
 ]
 
 if CONFIG['MOZ_B2G_RIL']:
     EXPORTS.mozilla.dom.network += [
         'MobileConnection.h',
+        'MobileConnectionArray.h',
     ]
     SOURCES += [
         'MobileConnection.cpp',
+        'MobileConnectionArray.cpp',
     ]
 
 if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'gonk':
     EXTRA_JS_MODULES = [
         'NetworkStatsDB.jsm',
         'NetworkStatsService.jsm',
     ]