Bug 729173 - Part 4: Fill in the MobileConnection DOM implementation. r=mounir
authorPhilipp von Weitershausen <philipp@weitershausen.de>
Thu, 19 Apr 2012 18:33:25 -0300
changeset 95317 466592cf04888fd518815a7436111d2567cfdd33
parent 95316 6c0ce3fad8ae8e69fbd7a44533f8702ee268c933
child 95318 ee7f4d1d2934a7e041736240cbb89d70a2dbb166
push id886
push userlsblakk@mozilla.com
push dateMon, 04 Jun 2012 19:57:52 +0000
treeherdermozilla-beta@bbd8d5efd6d1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmounir
bugs729173
milestone14.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 729173 - Part 4: Fill in the MobileConnection DOM implementation. r=mounir
dom/network/interfaces/Makefile.in
dom/network/interfaces/nsIMobileConnectionProvider.idl
dom/network/src/MobileConnection.cpp
dom/network/src/MobileConnection.h
--- a/dom/network/interfaces/Makefile.in
+++ b/dom/network/interfaces/Makefile.in
@@ -44,11 +44,12 @@ include $(DEPTH)/config/autoconf.mk
 XPIDL_MODULE = dom_network
 
 include $(topsrcdir)/dom/dom-config.mk
 
 XPIDLSRCS = \
   nsIDOMNavigatorNetwork.idl \
   nsIDOMConnection.idl \
   nsIDOMMobileConnection.idl \
+  nsIMobileConnectionProvider.idl \
   $(NULL)
 
 include $(topsrcdir)/config/rules.mk
new file mode 100644
--- /dev/null
+++ b/dom/network/interfaces/nsIMobileConnectionProvider.idl
@@ -0,0 +1,23 @@
+/* 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 "nsISupports.idl"
+
+interface nsIDOMMozMobileConnectionInfo;
+interface nsIDOMDOMRequest;
+interface nsIDOMWindow;
+
+/**
+ * XPCOM component (in the content process) that provides the mobile
+ * network information.
+ */
+[scriptable, uuid(1ecd19eb-15d4-47c0-a2cf-80cfa3b94eeb)]
+interface nsIMobileConnectionProvider : nsISupports
+{
+  readonly attribute DOMString cardState;
+  readonly attribute nsIDOMMozMobileConnectionInfo voiceConnectionInfo;
+  readonly attribute nsIDOMMozMobileConnectionInfo dataConnectionInfo;
+
+  nsIDOMDOMRequest getNetworks(in nsIDOMWindow window);
+};
--- a/dom/network/src/MobileConnection.cpp
+++ b/dom/network/src/MobileConnection.cpp
@@ -1,96 +1,187 @@
 /* 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 "MobileConnection.h"
 #include "nsIDOMDOMRequest.h"
 #include "nsIDOMClassInfo.h"
+#include "nsDOMEvent.h"
+#include "nsIObserverService.h"
+#include "mozilla/Services.h"
+
+#define NS_RILCONTENTHELPER_CONTRACTID "@mozilla.org/ril/content-helper;1"
+
+#define VOICECHANGE_EVENTNAME      NS_LITERAL_STRING("voicechange")
+#define DATACHANGE_EVENTNAME       NS_LITERAL_STRING("datachange")
+#define CARDSTATECHANGE_EVENTNAME  NS_LITERAL_STRING("cardstatechange")
 
 DOMCI_DATA(MozMobileConnection, mozilla::dom::network::MobileConnection)
 
 namespace mozilla {
 namespace dom {
 namespace network {
 
+const char* kVoiceChangedTopic     = "mobile-connection-voice-changed";
+const char* kDataChangedTopic      = "mobile-connection-data-changed";
+const char* kCardStateChangedTopic = "mobile-connection-cardstate-changed";
+
 NS_IMPL_CYCLE_COLLECTION_CLASS(MobileConnection)
 
 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(MobileConnection,
                                                   nsDOMEventTargetHelper)
   NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(cardstatechange)
   NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(voicechange)
   NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(datachange)
 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
 
 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(MobileConnection,
                                                 nsDOMEventTargetHelper)
   NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(cardstatechange)
   NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(voicechange)
   NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(datachange)
+  tmp->mProvider = nsnull;
 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
 
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(MobileConnection)
   NS_INTERFACE_MAP_ENTRY(nsIDOMMozMobileConnection)
   NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMMozMobileConnection)
   NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(MozMobileConnection)
 NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
 
 NS_IMPL_ADDREF_INHERITED(MobileConnection, nsDOMEventTargetHelper)
 NS_IMPL_RELEASE_INHERITED(MobileConnection, nsDOMEventTargetHelper)
 
 MobileConnection::MobileConnection()
 {
+  mProvider = do_GetService(NS_RILCONTENTHELPER_CONTRACTID);
+
+  // Not being able to acquire the provider isn't fatal since we check
+  // for it explicitly below.
+  if (!mProvider) {
+    NS_WARNING("Could not acquire nsIMobileConnectionProvider!");
+  }
 }
 
 void
 MobileConnection::Init(nsPIDOMWindow* aWindow)
 {
   BindToOwner(aWindow);
+
+  nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
+  if (!obs) {
+    NS_WARNING("Could not acquire nsIObserverService!");
+    return;
+  }
+
+  obs->AddObserver(this, kVoiceChangedTopic, false);
+  obs->AddObserver(this, kDataChangedTopic, false);
+  obs->AddObserver(this, kCardStateChangedTopic, false);
 }
 
 void
 MobileConnection::Shutdown()
 {
+  nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
+  if (!obs) {
+    NS_WARNING("Could not acquire nsIObserverService!");
+    return;
+  }
+
+  obs->RemoveObserver(this, kVoiceChangedTopic);
+  obs->RemoveObserver(this, kDataChangedTopic);
+  obs->RemoveObserver(this, kCardStateChangedTopic);
 }
 
 // nsIObserver
 
 NS_IMETHODIMP
 MobileConnection::Observe(nsISupports* aSubject,
                           const char* aTopic,
                           const PRUnichar* aData)
 {
+  if (!strcmp(aTopic, kVoiceChangedTopic)) {
+    InternalDispatchEvent(VOICECHANGE_EVENTNAME);
+    return NS_OK;
+  }
+
+  if (!strcmp(aTopic, kDataChangedTopic)) {
+    InternalDispatchEvent(DATACHANGE_EVENTNAME);
+    return NS_OK;
+  }
+
+  if (!strcmp(aTopic, kCardStateChangedTopic)) {
+    InternalDispatchEvent(CARDSTATECHANGE_EVENTNAME);
+    return NS_OK;
+  }
+
+  MOZ_NOT_REACHED("Unknown observer topic!");
   return NS_OK;
 }
 
 // nsIDOMMozMobileConnection
 
 NS_IMETHODIMP
 MobileConnection::GetCardState(nsAString& cardState)
 {
-  return NS_ERROR_NOT_IMPLEMENTED;
+  if (!mProvider) {
+    cardState.SetIsVoid(true);
+    return NS_OK;
+  }
+  return mProvider->GetCardState(cardState);
 }
 
 NS_IMETHODIMP
 MobileConnection::GetVoice(nsIDOMMozMobileConnectionInfo** voice)
 {
-  return NS_ERROR_NOT_IMPLEMENTED;
+  if (!mProvider) {
+    *voice = nsnull;
+    return NS_OK;
+  }
+  return mProvider->GetVoiceConnectionInfo(voice);
 }
 
 NS_IMETHODIMP
 MobileConnection::GetData(nsIDOMMozMobileConnectionInfo** data)
 {
-  return NS_ERROR_NOT_IMPLEMENTED;
+  if (!mProvider) {
+    *data = nsnull;
+    return NS_OK;
+  }
+  return mProvider->GetDataConnectionInfo(data);
 }
 
 NS_IMETHODIMP
 MobileConnection::GetNetworks(nsIDOMDOMRequest** request)
 {
-  return NS_ERROR_NOT_IMPLEMENTED;
+  *request = nsnull;
+
+  if (!mProvider) {
+    return NS_ERROR_FAILURE;
+  }
+
+  return mProvider->GetNetworks(GetOwner(), request);
+}
+
+nsresult
+MobileConnection::InternalDispatchEvent(const nsAString& aType)
+{
+  nsRefPtr<nsDOMEvent> event = new nsDOMEvent(nsnull, nsnull);
+  nsresult rv = event->InitEvent(aType, false, false);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  rv = event->SetTrusted(true);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  bool dummy;
+  rv = DispatchEvent(event, &dummy);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  return NS_OK;
 }
 
 NS_IMPL_EVENT_HANDLER(MobileConnection, cardstatechange)
 NS_IMPL_EVENT_HANDLER(MobileConnection, voicechange)
 NS_IMPL_EVENT_HANDLER(MobileConnection, datachange)
 
 } // namespace network
 } // namespace dom
--- a/dom/network/src/MobileConnection.h
+++ b/dom/network/src/MobileConnection.h
@@ -2,16 +2,17 @@
  * 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_MobileConnection_h
 #define mozilla_dom_network_MobileConnection_h
 
 #include "nsIObserver.h"
 #include "nsIDOMMobileConnection.h"
+#include "nsIMobileConnectionProvider.h"
 #include "nsDOMEventTargetHelper.h"
 #include "nsCycleCollectionParticipant.h"
 
 namespace mozilla {
 namespace dom {
 namespace network {
 
 class MobileConnection : public nsDOMEventTargetHelper
@@ -29,16 +30,20 @@ public:
 
   void Init(nsPIDOMWindow *aWindow);
   void Shutdown();
 
   NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(MobileConnection,
                                            nsDOMEventTargetHelper)
 
 private:
+  nsCOMPtr<nsIMobileConnectionProvider> mProvider;
+
+  nsresult InternalDispatchEvent(const nsAString& aType);
+
   NS_DECL_EVENT_HANDLER(cardstatechange)
   NS_DECL_EVENT_HANDLER(voicechange)
   NS_DECL_EVENT_HANDLER(datachange)
 };
 
 } // namespace network
 } // namespace dom
 } // namespace mozilla