Bug 799243 - Patch 1: Fix Bluetooth*Manager observer pointer types; r=echou
authorKyle Machulis <kyle@nonpolynomial.com>
Mon, 08 Oct 2012 21:59:33 -0700
changeset 109722 d01545bde6644b46c9bb69c94daece593c840e2d
parent 109721 2c831793f1a656ddb14bd4e9d13426508398735c
child 109723 66f9632e35390f9037ebdba9e6c386ad1ca468c9
push id23648
push useremorley@mozilla.com
push dateTue, 09 Oct 2012 14:23:49 +0000
treeherdermozilla-central@dd61540f237c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersechou
bugs799243
milestone19.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 799243 - Patch 1: Fix Bluetooth*Manager observer pointer types; r=echou
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());