Bug 895325 - [Bluetooth][Cleanup] Revise BluetoothOppManager constructor and BluetoothOppManager::Get(), r=echou
authorBen Tian <btian@mozilla.com>
Fri, 19 Jul 2013 10:27:07 +0800
changeset 151693 126c0732c9184a92c46b013dcd5739493220223a
parent 151692 a965d9f77a3d470db8f85c8d9bb3d400ac8e54be
child 151694 6c89c3bbfe3b4f9978c4558b4bd8b961fa30a62e
push id2859
push userakeybl@mozilla.com
push dateMon, 16 Sep 2013 19:14:59 +0000
treeherdermozilla-beta@87d3c51cd2bf [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersechou
bugs895325
milestone25.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 895325 - [Bluetooth][Cleanup] Revise BluetoothOppManager constructor and BluetoothOppManager::Get(), r=echou
dom/bluetooth/BluetoothA2dpManager.cpp
dom/bluetooth/BluetoothHfpManager.cpp
dom/bluetooth/BluetoothOppManager.cpp
--- a/dom/bluetooth/BluetoothA2dpManager.cpp
+++ b/dom/bluetooth/BluetoothA2dpManager.cpp
@@ -19,26 +19,26 @@
 #include "nsIAudioManager.h"
 #include "nsIObserverService.h"
 
 
 using namespace mozilla;
 USING_BLUETOOTH_NAMESPACE
 
 namespace {
-  StaticRefPtr<BluetoothA2dpManager> gBluetoothA2dpManager;
-  bool gInShutdown = false;
+  StaticRefPtr<BluetoothA2dpManager> sBluetoothA2dpManager;
+  bool sInShutdown = false;
 } // anonymous namespace
 
 NS_IMETHODIMP
 BluetoothA2dpManager::Observe(nsISupports* aSubject,
                               const char* aTopic,
                               const PRUnichar* aData)
 {
-  MOZ_ASSERT(gBluetoothA2dpManager);
+  MOZ_ASSERT(sBluetoothA2dpManager);
 
   if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
     HandleShutdown();
     return NS_OK;
   }
 
   MOZ_ASSERT(false, "BluetoothA2dpManager got unexpected topic!");
   return NS_ERROR_UNEXPECTED;
@@ -96,51 +96,51 @@ StatusStringToSinkState(const nsAString&
 }
 
 //static
 BluetoothA2dpManager*
 BluetoothA2dpManager::Get()
 {
   MOZ_ASSERT(NS_IsMainThread());
 
-  // If we already exist, exit early
-  if (gBluetoothA2dpManager) {
-    return gBluetoothA2dpManager;
+  // If sBluetoothA2dpManager already exists, exit early
+  if (sBluetoothA2dpManager) {
+    return sBluetoothA2dpManager;
   }
 
   // If we're in shutdown, don't create a new instance
-  if (gInShutdown) {
+  if (sInShutdown) {
     NS_WARNING("BluetoothA2dpManager can't be created during shutdown");
     return nullptr;
   }
 
-  // Create new instance, register, return
+  // Create a new instance, register, and return
   BluetoothA2dpManager* manager = new BluetoothA2dpManager();
   NS_ENSURE_TRUE(manager->Init(), nullptr);
 
-  gBluetoothA2dpManager = manager;
-  return gBluetoothA2dpManager;
+  sBluetoothA2dpManager = manager;
+  return sBluetoothA2dpManager;
 }
 
 void
 BluetoothA2dpManager::HandleShutdown()
 {
   MOZ_ASSERT(NS_IsMainThread());
-  gInShutdown = true;
+  sInShutdown = true;
   Disconnect();
-  gBluetoothA2dpManager = nullptr;
+  sBluetoothA2dpManager = nullptr;
 }
 
 bool
 BluetoothA2dpManager::Connect(const nsAString& aDeviceAddress)
 {
   MOZ_ASSERT(NS_IsMainThread());
   MOZ_ASSERT(!aDeviceAddress.IsEmpty());
 
-  if (gInShutdown) {
+  if (sInShutdown) {
     NS_WARNING("Connect called while in shutdown!");
     return false;
   }
 
   if (mConnected) {
     NS_WARNING("BluetoothA2dpManager is connected");
     return false;
   }
--- a/dom/bluetooth/BluetoothHfpManager.cpp
+++ b/dom/bluetooth/BluetoothHfpManager.cpp
@@ -58,18 +58,18 @@
 #define MOBILE_CONNECTION_VOICE_CHANGED_ID   "mobile-connection-voice-changed"
 #define AUDIO_VOLUME_BT_SCO_ID               "audio.volume.bt_sco"
 
 using namespace mozilla;
 using namespace mozilla::ipc;
 USING_BLUETOOTH_NAMESPACE
 
 namespace {
-  StaticRefPtr<BluetoothHfpManager> gBluetoothHfpManager;
-  bool gInShutdown = false;
+  StaticRefPtr<BluetoothHfpManager> sBluetoothHfpManager;
+  bool sInShutdown = false;
   static const char kHfpCrlf[] = "\xd\xa";
 
   // Sending ringtone related
   static bool sStopSendingRingFlag = true;
   static int sRingInterval = 3000; //unit: ms
 
   // Wait for 2 seconds for Dialer processing event 'BLDN'. '2' seconds is a
   // magic number. The mechanism should be revised once we can get call history.
@@ -227,21 +227,21 @@ BluetoothHfpManager::Notify(const hal::B
   }
 }
 
 class BluetoothHfpManager::RespondToBLDNTask : public Task
 {
 private:
   void Run() MOZ_OVERRIDE
   {
-    MOZ_ASSERT(gBluetoothHfpManager);
+    MOZ_ASSERT(sBluetoothHfpManager);
 
-    if (!gBluetoothHfpManager->mDialingRequestProcessed) {
-      gBluetoothHfpManager->mDialingRequestProcessed = true;
-      gBluetoothHfpManager->SendLine("ERROR");
+    if (!sBluetoothHfpManager->mDialingRequestProcessed) {
+      sBluetoothHfpManager->mDialingRequestProcessed = true;
+      sBluetoothHfpManager->SendLine("ERROR");
     }
   }
 };
 
 class BluetoothHfpManager::SendRingIndicatorTask : public Task
 {
 public:
   SendRingIndicatorTask(const nsAString& aNumber, int aType)
@@ -255,30 +255,30 @@ public:
   {
     MOZ_ASSERT(NS_IsMainThread());
 
     // Stop sending RING indicator
     if (sStopSendingRingFlag) {
       return;
     }
 
-    if (!gBluetoothHfpManager) {
+    if (!sBluetoothHfpManager) {
       NS_WARNING("BluetoothHfpManager no longer exists, cannot send ring!");
       return;
     }
 
     nsAutoCString ringMsg("RING");
-    gBluetoothHfpManager->SendLine(ringMsg.get());
+    sBluetoothHfpManager->SendLine(ringMsg.get());
 
     if (!mNumber.IsEmpty()) {
       nsAutoCString clipMsg("+CLIP: \"");
       clipMsg.Append(NS_ConvertUTF16toUTF8(mNumber).get());
       clipMsg.AppendLiteral("\",");
       clipMsg.AppendInt(mType);
-      gBluetoothHfpManager->SendLine(clipMsg.get());
+      sBluetoothHfpManager->SendLine(clipMsg.get());
     }
 
     MessageLoop::current()->
       PostDelayedTask(FROM_HERE,
                       new SendRingIndicatorTask(mNumber, mType),
                       sRingInterval);
   }
 
@@ -287,19 +287,19 @@ private:
   int mType;
 };
 
 class BluetoothHfpManager::CloseScoTask : public Task
 {
 private:
   void Run() MOZ_OVERRIDE
   {
-    MOZ_ASSERT(gBluetoothHfpManager);
+    MOZ_ASSERT(sBluetoothHfpManager);
 
-    gBluetoothHfpManager->DisconnectSco();
+    sBluetoothHfpManager->DisconnectSco();
   }
 };
 
 static bool
 IsValidDtmf(const char aChar) {
   // Valid DTMF: [*#0-9ABCD]
   if (aChar == '*' || aChar == '#') {
     return true;
@@ -429,33 +429,33 @@ BluetoothHfpManager::~BluetoothHfpManage
 }
 
 //static
 BluetoothHfpManager*
 BluetoothHfpManager::Get()
 {
   MOZ_ASSERT(NS_IsMainThread());
 
-  // If we already exist, exit early
-  if (gBluetoothHfpManager) {
-    return gBluetoothHfpManager;
+  // If sBluetoothHfpManager already exists, exit early
+  if (sBluetoothHfpManager) {
+    return sBluetoothHfpManager;
   }
 
   // If we're in shutdown, don't create a new instance
-  if (gInShutdown) {
+  if (sInShutdown) {
     NS_WARNING("BluetoothHfpManager can't be created during shutdown");
     return nullptr;
   }
 
-  // Create new instance, register, return
+  // Create a new instance, register, and return
   BluetoothHfpManager* manager = new BluetoothHfpManager();
   NS_ENSURE_TRUE(manager->Init(), nullptr);
 
-  gBluetoothHfpManager = manager;
-  return gBluetoothHfpManager;
+  sBluetoothHfpManager = manager;
+  return sBluetoothHfpManager;
 }
 
 void
 BluetoothHfpManager::NotifyStatusChanged(const nsAString& aType)
 {
   nsString type, name;
   BluetoothValue v;
   InfallibleTArray<BluetoothNamedValue> parameters;
@@ -645,20 +645,20 @@ BluetoothHfpManager::HandleIccInfoChange
   NS_ENSURE_TRUE_VOID(iccInfo);
   iccInfo->GetMsisdn(mMsisdn);
 }
 
 void
 BluetoothHfpManager::HandleShutdown()
 {
   MOZ_ASSERT(NS_IsMainThread());
-  gInShutdown = true;
+  sInShutdown = true;
   Disconnect();
   DisconnectSco();
-  gBluetoothHfpManager = nullptr;
+  sBluetoothHfpManager = nullptr;
 }
 
 // Virtual function of class SocketConsumer
 void
 BluetoothHfpManager::ReceiveSocketData(BluetoothSocket* aSocket,
                                        nsAutoPtr<UnixSocketRawData>& aMessage)
 {
   MOZ_ASSERT(NS_IsMainThread());
@@ -995,17 +995,17 @@ respond_with_ok:
 void
 BluetoothHfpManager::Connect(const nsAString& aDeviceAddress,
                              const bool aIsHandsfree,
                              BluetoothReplyRunnable* aRunnable)
 {
   MOZ_ASSERT(NS_IsMainThread());
 
   BluetoothService* bs = BluetoothService::Get();
-  if (!bs || gInShutdown) {
+  if (!bs || sInShutdown) {
     DispatchBluetoothReply(aRunnable, BluetoothValue(),
                            NS_LITERAL_STRING(ERR_NO_AVAILABLE_RESOURCE));
     return;
   }
 
   if (mSocket) {
     if (mDeviceAddress == aDeviceAddress) {
       DispatchBluetoothReply(aRunnable, BluetoothValue(),
@@ -1052,17 +1052,17 @@ BluetoothHfpManager::Connect(const nsASt
     new BluetoothSocket(this, BluetoothSocketType::RFCOMM, true, true);
 }
 
 bool
 BluetoothHfpManager::Listen()
 {
   MOZ_ASSERT(NS_IsMainThread());
 
-  if (gInShutdown) {
+  if (sInShutdown) {
     NS_WARNING("Listen called while in shutdown!");
     return false;
   }
 
   if (mSocket) {
     NS_WARNING("mSocket exists. Failed to listen.");
     return false;
   }
@@ -1674,17 +1674,17 @@ BluetoothHfpManager::GetAddress(nsAStrin
   return mSocket->GetAddress(aDeviceAddress);
 }
 
 bool
 BluetoothHfpManager::ConnectSco(BluetoothReplyRunnable* aRunnable)
 {
   MOZ_ASSERT(NS_IsMainThread());
 
-  if (gInShutdown) {
+  if (sInShutdown) {
     NS_WARNING("ConnecteSco called while in shutdown!");
     return false;
   }
 
   if (!IsConnected()) {
     NS_WARNING("BluetoothHfpManager is not connected");
     return false;
   }
@@ -1727,17 +1727,17 @@ BluetoothHfpManager::DisconnectSco()
   return true;
 }
 
 bool
 BluetoothHfpManager::ListenSco()
 {
   MOZ_ASSERT(NS_IsMainThread());
 
-  if (gInShutdown) {
+  if (sInShutdown) {
     NS_WARNING("ListenSco called while in shutdown!");
     return false;
   }
 
   if (mScoSocket->GetConnectionStatus() ==
       SocketConnectionStatus::SOCKET_LISTENING) {
     NS_WARNING("SCO socket has been already listening");
     return false;
--- a/dom/bluetooth/BluetoothOppManager.cpp
+++ b/dom/bluetooth/BluetoothOppManager.cpp
@@ -41,26 +41,26 @@ namespace {
 static const uint32_t kUpdateProgressBase = 50 * 1024;
 
 /*
  * The format of the header of an PUT request is
  * [opcode:1][packet length:2][headerId:1][header length:2]
  */
 static const uint32_t kPutRequestHeaderSize = 6;
 
-StaticRefPtr<BluetoothOppManager> sInstance;
+StaticRefPtr<BluetoothOppManager> sBluetoothOppManager;
 static bool sInShutdown = false;
 }
 
 NS_IMETHODIMP
 BluetoothOppManager::Observe(nsISupports* aSubject,
                              const char* aTopic,
                              const PRUnichar* aData)
 {
-  MOZ_ASSERT(sInstance);
+  MOZ_ASSERT(sBluetoothOppManager);
 
   if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
     HandleShutdown();
     return NS_OK;
   }
 
   MOZ_ASSERT(false, "BluetoothOppManager got unexpected topic!");
   return NS_ERROR_UNEXPECTED;
@@ -75,17 +75,17 @@ public:
   {
     MOZ_ASSERT(!NS_IsMainThread());
   }
 
   NS_IMETHOD Run()
   {
     MOZ_ASSERT(NS_IsMainThread());
 
-    sInstance->SendPutRequest(mStream, mSize);
+    sBluetoothOppManager->SendPutRequest(mStream, mSize);
 
     return NS_OK;
   }
 
 private:
   nsAutoArrayPtr<uint8_t> mStream;
   uint32_t mSize;
 };
@@ -112,17 +112,17 @@ public:
     nsresult rv = mInputStream->Read(buf, mAvailablePacketSize, &numRead);
     if (NS_FAILED(rv)) {
       // Needs error handling here
       NS_WARNING("Failed to read from input stream");
       return NS_ERROR_FAILURE;
     }
 
     if (numRead > 0) {
-      sInstance->CheckPutFinal(numRead);
+      sBluetoothOppManager->CheckPutFinal(numRead);
 
       nsRefPtr<SendSocketDataTask> task =
         new SendSocketDataTask((uint8_t*)buf.forget(), numRead);
       if (NS_FAILED(NS_DispatchToMainThread(task))) {
         NS_WARNING("Failed to dispatch to main thread!");
         return NS_ERROR_FAILURE;
       }
     }
@@ -173,17 +173,16 @@ BluetoothOppManager::BluetoothOppManager
                                            , mIsServer(true)
                                            , mWaitingForConfirmationFlag(false)
                                            , mFileLength(0)
                                            , mSentFileLength(0)
                                            , mWaitingToSendPutFinal(false)
                                            , mCurrentBlobIndex(-1)
 {
   mConnectedDeviceAddress.AssignLiteral(BLUETOOTH_ADDRESS_NONE);
-  Init();
 }
 
 BluetoothOppManager::~BluetoothOppManager()
 {
   nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
   NS_ENSURE_TRUE_VOID(obs);
   if (NS_FAILED(obs->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID))) {
     BT_WARNING("Failed to remove shutdown observer!");
@@ -206,22 +205,33 @@ BluetoothOppManager::Init()
 }
 
 //static
 BluetoothOppManager*
 BluetoothOppManager::Get()
 {
   MOZ_ASSERT(NS_IsMainThread());
 
-  if (!sInstance) {
-    sInstance = new BluetoothOppManager();
-    NS_ENSURE_TRUE(sInstance->Init(), nullptr);
+  // If sBluetoothOppManager already exists, exit early
+  if (sBluetoothOppManager) {
+    return sBluetoothOppManager;
   }
 
-  return sInstance;
+  // If we're in shutdown, don't create a new instance
+  if (sInShutdown) {
+    NS_WARNING("BluetoothOppManager can't be created during shutdown");
+    return nullptr;
+  }
+
+  // Create a new instance, register, and return
+  BluetoothOppManager *manager = new BluetoothOppManager();
+  NS_ENSURE_TRUE(manager->Init(), nullptr);
+
+  sBluetoothOppManager = manager;
+  return sBluetoothOppManager;
 }
 
 void
 BluetoothOppManager::Connect(const nsAString& aDeviceAddress,
                              BluetoothReplyRunnable* aRunnable)
 {
   MOZ_ASSERT(NS_IsMainThread());
 
@@ -282,17 +292,17 @@ BluetoothOppManager::Disconnect()
 }
 
 void
 BluetoothOppManager::HandleShutdown()
 {
   MOZ_ASSERT(NS_IsMainThread());
   sInShutdown = true;
   Disconnect();
-  sInstance = nullptr;
+  sBluetoothOppManager = nullptr;
 }
 
 bool
 BluetoothOppManager::Listen()
 {
   MOZ_ASSERT(NS_IsMainThread());
 
   if (mSocket) {
@@ -906,17 +916,17 @@ BluetoothOppManager::ClientDataHandler(U
     AfterOppConnected();
 
     // Keep remote information
     mRemoteObexVersion = aMessage->mData[3];
     mRemoteConnectionFlags = aMessage->mData[4];
     mRemoteMaxPacketLength =
       (((int)(aMessage->mData[5]) << 8) | aMessage->mData[6]);
 
-    sInstance->SendPutHeaderRequest(mFileName, mFileLength);
+    sBluetoothOppManager->SendPutHeaderRequest(mFileName, mFileLength);
   } else if (mLastCommand == ObexRequestCode::Put) {
     if (mWaitingToSendPutFinal) {
       SendPutFinalRequest();
       return;
     }
 
     if (mAbortFlag) {
       SendAbortRequest();