Bug 782586 - Firing onadapteradded event when bluetooth is ready to get default adapter. r=qdot, a=blocking-basecamp
authorGina Yeh <gyeh@mozilla.com>
Tue, 09 Oct 2012 11:29:37 +0800
changeset 113343 43dd673647ae47e1dfe9f2bb0be6f7bbcc000345
parent 113342 34a039c14957d4a2a0f87e355df7c754b0c06709
child 113344 ca6e58244d5ba8d1abcacd3785022e5a10b93a17
push id2293
push userryanvm@gmail.com
push dateSat, 13 Oct 2012 19:46:06 +0000
treeherdermozilla-aurora@a24b525c7f93 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersqdot, blocking-basecamp
bugs782586
milestone18.0a2
Bug 782586 - Firing onadapteradded event when bluetooth is ready to get default adapter. r=qdot, a=blocking-basecamp
dom/bluetooth/BluetoothService.cpp
dom/bluetooth/linux/BluetoothDBusService.cpp
--- a/dom/bluetooth/BluetoothService.cpp
+++ b/dom/bluetooth/BluetoothService.cpp
@@ -276,16 +276,17 @@ BluetoothService::Init()
 
 void
 BluetoothService::Cleanup()
 {
   MOZ_ASSERT(NS_IsMainThread());
 
   if (mRegisteredForLocalAgent) {
     UnregisterBluetoothSignalHandler(NS_LITERAL_STRING(LOCAL_AGENT_PATH), this);
+    UnregisterBluetoothSignalHandler(NS_LITERAL_STRING(REMOTE_AGENT_PATH), this);
     mRegisteredForLocalAgent = false;
   }
 
   nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
   if (obs &&
       (NS_FAILED(obs->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) ||
        NS_FAILED(obs->RemoveObserver(this, MOZSETTINGS_CHANGED_ID)))) {
     NS_WARNING("Can't unregister observers!");
@@ -380,21 +381,16 @@ BluetoothService::StartStopBluetooth(boo
     rv = NS_NewNamedThread("BluetoothCmd",
                            getter_AddRefs(mBluetoothCommandThread));
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
   if (aStart) {
     RegisterBluetoothSignalHandler(NS_LITERAL_STRING(LOCAL_AGENT_PATH), this);
     RegisterBluetoothSignalHandler(NS_LITERAL_STRING(REMOTE_AGENT_PATH), this);
-
-    BluetoothManagerList::ForwardIterator iter(mLiveManagers);
-    while (iter.HasMore()) {
-      RegisterBluetoothSignalHandler(NS_LITERAL_STRING("/"), (BluetoothSignalObserver*)iter.GetNext());
-    }
   }
 
   nsCOMPtr<nsIRunnable> runnable = new ToggleBtTask(aStart);
   rv = mBluetoothCommandThread->Dispatch(runnable, NS_DISPATCH_NORMAL);
   NS_ENSURE_SUCCESS(rv, rv);
 
   return NS_OK;
 }
@@ -413,16 +409,32 @@ BluetoothService::SetEnabled(bool aEnabl
 
   AutoInfallibleTArray<BluetoothParent*, 10> childActors;
   GetAllBluetoothActors(childActors);
 
   for (uint32_t index = 0; index < childActors.Length(); index++) {
     unused << childActors[index]->SendEnabled(aEnabled);
   }
 
+  if (aEnabled) {
+    BluetoothManagerList::ForwardIterator iter(mLiveManagers);
+    BluetoothSignalObserverList* ol;
+    nsString managerPath = NS_LITERAL_STRING("/");
+
+    // Skip when BluetoothManager has been registered in constructor
+    // Re-register here after toggling due to table mBluetoothSignalObserverTable was cleared
+    if (!mBluetoothSignalObserverTable.Get(managerPath, &ol)) {
+      while (iter.HasMore()) {
+        RegisterBluetoothSignalHandler(managerPath, (BluetoothSignalObserver*)iter.GetNext());
+      }
+    }
+  } else {
+    mBluetoothSignalObserverTable.Clear();
+  }
+
   BluetoothManagerList::ForwardIterator iter(mLiveManagers);
   while (iter.HasMore()) {
     if (NS_FAILED(iter.GetNext()->FireEnabledDisabledEvent(aEnabled))) {
       NS_WARNING("FireEnabledDisabledEvent failed!");
     }
   }
 }
 
--- a/dom/bluetooth/linux/BluetoothDBusService.cpp
+++ b/dom/bluetooth/linux/BluetoothDBusService.cpp
@@ -1446,17 +1446,16 @@ BluetoothDBusService::StopInternal()
   if (!dbus_connection_unregister_object_path(gThreadConnection->GetConnection(),
                                               REMOTE_AGENT_PATH)) {
     LOG("%s: Can't unregister object path %s for agent!",
         __FUNCTION__, LOCAL_AGENT_PATH);
   }
 
   mConnection = nullptr;
   gThreadConnection = nullptr;
-  mBluetoothSignalObserverTable.Clear();
 
   // unref stored DBusMessages before clear the hashtable
   sPairingReqTable.EnumerateRead(UnrefDBusMessages, nullptr);
   sPairingReqTable.Clear();
 
   sAuthorizeReqTable.EnumerateRead(UnrefDBusMessages, nullptr);
   sAuthorizeReqTable.Clear();