Bug 804687 - Remove sHfpCommandThread from BluetoothHfpManager. r=echou, a=blocking-basecamp
authorKyle Machulis <kyle@nonpolynomial.com>
Thu, 25 Oct 2012 18:25:15 -0700
changeset 116438 b73c1929073fbf10884bc968c672b8ecfa0142a2
parent 116437 b1f9bffd2e4f1c4d3af28ab2b54112b1fd8ffe38
child 116439 084f77c4b23c3674df3e2e22498e22baec8487e5
push id1708
push userakeybl@mozilla.com
push dateMon, 19 Nov 2012 21:10:21 +0000
treeherdermozilla-beta@27b14fe50103 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersechou, blocking-basecamp
bugs804687
milestone18.0a2
Bug 804687 - Remove sHfpCommandThread from BluetoothHfpManager. r=echou, a=blocking-basecamp
dom/bluetooth/BluetoothHfpManager.cpp
--- a/dom/bluetooth/BluetoothHfpManager.cpp
+++ b/dom/bluetooth/BluetoothHfpManager.cpp
@@ -137,20 +137,19 @@ public:
     Shutdown();
   }
 };
 
 namespace {
   StaticRefPtr<BluetoothHfpManager> gBluetoothHfpManager;
   StaticRefPtr<BluetoothHfpManagerObserver> sHfpObserver;
   bool gInShutdown = false;
-  static nsCOMPtr<nsIThread> sHfpCommandThread;
   static bool sStopSendingRingFlag = true;
 
-  static int kRingInterval = 3000000;  //unit: us
+  static int sRingInterval = 3000; //unit: ms
 } // anonymous namespace
 
 NS_IMPL_ISUPPORTS1(BluetoothHfpManagerObserver, nsIObserver)
 
 NS_IMETHODIMP
 BluetoothHfpManagerObserver::Observe(nsISupports* aSubject,
                                      const char* aTopic,
                                      const PRUnichar* aData)
@@ -162,35 +161,45 @@ BluetoothHfpManagerObserver::Observe(nsI
   } else if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
     return gBluetoothHfpManager->HandleShutdown();
   }
 
   MOZ_ASSERT(false, "BluetoothHfpManager got unexpected topic!");
   return NS_ERROR_UNEXPECTED;
 }
 
-class SendRingIndicatorTask : public nsRunnable
+class SendRingIndicatorTask : public Task
 {
 public:
   SendRingIndicatorTask()
   {
     MOZ_ASSERT(NS_IsMainThread());
   }
 
-  NS_IMETHOD Run()
+  void Run() MOZ_OVERRIDE
   {
-    MOZ_ASSERT(!NS_IsMainThread());
+    MOZ_ASSERT(NS_IsMainThread());
 
-    while (!sStopSendingRingFlag) {
-      gBluetoothHfpManager->SendLine("RING");
-
-      usleep(kRingInterval);
+    if (sStopSendingRingFlag) {
+      return;
     }
 
-    return NS_OK;
+    if (!gBluetoothHfpManager) {
+      NS_WARNING("BluetoothHfpManager no longer exists, cannot send ring!");
+      return;
+    }
+
+    gBluetoothHfpManager->SendLine("RING");
+
+    MessageLoop::current()->
+      PostDelayedTask(FROM_HERE,
+                      new SendRingIndicatorTask(),
+                      sRingInterval);
+
+    return;
   }
 };
 
 void
 OpenScoSocket(const nsAString& aDeviceAddress)
 {
   MOZ_ASSERT(NS_IsMainThread());
 
@@ -237,23 +246,16 @@ BluetoothHfpManager::Init()
   }
 
   mListener = new BluetoothRilListener();
   if (!mListener->StartListening()) {
     NS_WARNING("Failed to start listening RIL");
     return false;
   }
 
-  if (!sHfpCommandThread) {
-    if (NS_FAILED(NS_NewThread(getter_AddRefs(sHfpCommandThread)))) {
-      NS_ERROR("Failed to new thread for sHfpCommandThread");
-      return false;
-    }
-  }
-
   float volume;
   nsCOMPtr<nsIAudioManager> am = do_GetService("@mozilla.org/telephony/audiomanager;1");
   if (!am) {
     NS_WARNING("Failed to get AudioManager Service!");
     return false;
   }
   am->GetMasterVolume(&volume);
 
@@ -272,25 +274,16 @@ BluetoothHfpManager::~BluetoothHfpManage
 void
 BluetoothHfpManager::Cleanup()
 {
   if (!mListener->StopListening()) {
     NS_WARNING("Failed to stop listening RIL");
   }
   mListener = nullptr;
 
-  // Shut down the command thread if it still exists.
-  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()
 {
@@ -716,22 +709,18 @@ BluetoothHfpManager::SetupCIND(int aCall
     case nsIRadioInterfaceLayer::CALL_STATE_INCOMING:
       sCINDItems[CINDType::CALLSETUP].value = CallSetupState::INCOMING;
       if (!aInitial) {
         SendCommand("+CIEV: ", CINDType::CALLSETUP);
       }
 
       // Start sending RING indicator to HF
       sStopSendingRingFlag = false;
-      sendRingTask = new SendRingIndicatorTask();
-
-      if (NS_FAILED(sHfpCommandThread->Dispatch(sendRingTask, NS_DISPATCH_NORMAL))) {
-        NS_WARNING("Cannot dispatch ring task!");
-        return;
-      };
+      MessageLoop::current()->PostTask(FROM_HERE,
+                                       new SendRingIndicatorTask());
       break;
     case nsIRadioInterfaceLayer::CALL_STATE_DIALING:
       sCINDItems[CINDType::CALLSETUP].value = CallSetupState::OUTGOING;
       if (!aInitial) {
         SendCommand("+CIEV: ", CINDType::CALLSETUP);
 
         GetSocketAddr(address);
         OpenScoSocket(address);