Bug 799243 - Patch 1: Fix Bluetooth*Manager observer pointer types. r=echou, a=blocking-basecamp
authorKyle Machulis <kyle@nonpolynomial.com>
Mon, 08 Oct 2012 21:59:33 -0700
changeset 113344 ca6e58244d5ba8d1abcacd3785022e5a10b93a17
parent 113343 43dd673647ae47e1dfe9f2bb0be6f7bbcc000345
child 113345 0136c82b58cd7a6da2207ba5bb75273e422ffb85
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)
reviewersechou, blocking-basecamp
bugs799243
milestone18.0a2
Bug 799243 - Patch 1: Fix Bluetooth*Manager observer pointer types. r=echou, a=blocking-basecamp
dom/bluetooth/BluetoothHfpManager.cpp
dom/bluetooth/BluetoothScoManager.cpp
--- a/dom/bluetooth/BluetoothHfpManager.cpp
+++ b/dom/bluetooth/BluetoothHfpManager.cpp
@@ -36,41 +36,56 @@ USING_BLUETOOTH_NAMESPACE
 class mozilla::dom::bluetooth::BluetoothHfpManagerObserver : public nsIObserver
 {
 public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSIOBSERVER
 
   BluetoothHfpManagerObserver()
   {
+  }
+
+  bool Init()
+  {
     nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
     MOZ_ASSERT(obs);
     if (NS_FAILED(obs->AddObserver(this, MOZSETTINGS_CHANGED_ID, false))) {
       NS_WARNING("Failed to add settings change observer!");
+      return false;
     }
 
     if (NS_FAILED(obs->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false))) {
       NS_WARNING("Failed to add shutdown observer!");
+      return false;
     }
+
+    return true;
+  }
+
+  bool Shutdown()
+  {
+    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, or already unregistered!");
+      return false;
+    }
+    return true;
   }
 
   ~BluetoothHfpManagerObserver()
   {
-    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!");
-    }
+    Shutdown();
   }
 };
 
 namespace {
   StaticRefPtr<BluetoothHfpManager> gBluetoothHfpManager;
-  StaticAutoPtr<BluetoothHfpManagerObserver> sHfpObserver;
+  StaticRefPtr<BluetoothHfpManagerObserver> sHfpObserver;
   bool gInShutdown = false;
   static nsCOMPtr<nsIThread> sHfpCommandThread;
   static bool sStopSendingRingFlag = true;
 
   static int kRingInterval = 3000000;  //unit: us
 } // anonymous namespace
 
 NS_IMPL_ISUPPORTS1(BluetoothHfpManagerObserver, nsIObserver)
@@ -165,16 +180,19 @@ BluetoothHfpManager::BluetoothHfpManager
   , mCurrentCallState(nsIRadioInterfaceLayer::CALL_STATE_DISCONNECTED)
 {
 }
 
 bool
 BluetoothHfpManager::Init()
 {
   sHfpObserver = new BluetoothHfpManagerObserver();
+  if (!sHfpObserver->Init()) {
+    NS_WARNING("Cannot set up Hfp Observers!");
+  }
 
   mListener = new BluetoothRilListener();
   if (!mListener->StartListening()) {
     NS_WARNING("Failed to start listening RIL");
     return false;
   }
 
   if (!sHfpCommandThread) {
@@ -204,16 +222,17 @@ BluetoothHfpManager::Cleanup()
   if (sHfpCommandThread) {
     nsCOMPtr<nsIThread> thread;
     sHfpCommandThread.swap(thread);
     if (NS_FAILED(thread->Shutdown())) {
       NS_WARNING("Failed to shut down the bluetooth hfpmanager command thread!");
     }
   }
 
+  sHfpObserver->Shutdown();
   sHfpObserver = nullptr;
 }
 
 //static
 BluetoothHfpManager*
 BluetoothHfpManager::Get()
 {
   MOZ_ASSERT(NS_IsMainThread());
--- a/dom/bluetooth/BluetoothScoManager.cpp
+++ b/dom/bluetooth/BluetoothScoManager.cpp
@@ -28,39 +28,52 @@ USING_BLUETOOTH_NAMESPACE
 class mozilla::dom::bluetooth::BluetoothScoManagerObserver : public nsIObserver
 {
 public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSIOBSERVER
 
   BluetoothScoManagerObserver()
   {
+  }
+
+  bool Init()
+  {
     nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
     MOZ_ASSERT(obs);
 
     if (NS_FAILED(obs->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false))) {
       NS_WARNING("Failed to add shutdown observer!");
+      return false;
     }
+    return true;
+  }
+
+  bool Shutdown()
+  {
+    nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
+    if (!obs ||
+        (NS_FAILED(obs->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID)))) {
+      NS_WARNING("Can't unregister observers!");
+      return false;
+    }
+    return true;
   }
 
   ~BluetoothScoManagerObserver()
   {
-    nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
-    if (obs &&
-        (NS_FAILED(obs->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID)))) {
-      NS_WARNING("Can't unregister observers!");
-    }
+    Shutdown();
   }
 };
 
 NS_IMPL_ISUPPORTS1(BluetoothScoManagerObserver, nsIObserver)
 
 namespace {
 StaticRefPtr<BluetoothScoManager> gBluetoothScoManager;
-StaticAutoPtr<BluetoothScoManagerObserver> sScoObserver;
+StaticRefPtr<BluetoothScoManagerObserver> sScoObserver;
 bool gInShutdown = false;
 } // anonymous namespace
 
 NS_IMETHODIMP
 BluetoothScoManagerObserver::Observe(nsISupports* aSubject,
                                      const char* aTopic,
                                      const PRUnichar* aData)
 {
@@ -77,27 +90,31 @@ BluetoothScoManager::BluetoothScoManager
   : mConnected(false)
 {
 }
 
 bool
 BluetoothScoManager::Init()
 {
   sScoObserver = new BluetoothScoManagerObserver();
+  if (sScoObserver->Init()) {
+    NS_WARNING("Cannot set up SCO observers!");
+  }
   return true;
 }
 
 BluetoothScoManager::~BluetoothScoManager()
 {
   Cleanup();
 }
 
 void
 BluetoothScoManager::Cleanup()
 {
+  sScoObserver->Shutdown();
   sScoObserver = nullptr;
 }
 
 //static
 BluetoothScoManager*
 BluetoothScoManager::Get()
 {
   MOZ_ASSERT(NS_IsMainThread());