Bug 1061126: Cleanup ANDROID_VERSION from BluetoothA2DPManager, r=shuang
authorThomas Zimmermann <tdz@users.sourceforge.net>
Tue, 02 Sep 2014 12:46:08 +0200
changeset 226222 92f2ed449adb243d20580e6d8105c3920e89023b
parent 226221 bae6b0b0a4b5207ef67ddf38de09c9bbcc9fe90a
child 226223 a00b602f4b95709cc3ea88cb557b4b29aa1d11cf
push id4187
push userbhearsum@mozilla.com
push dateFri, 28 Nov 2014 15:29:12 +0000
treeherdermozilla-beta@f23cc6a30c11 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersshuang
bugs1061126
milestone34.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 1061126: Cleanup ANDROID_VERSION from BluetoothA2DPManager, r=shuang All interfaces for Bluetooth profiles are now always available, so we don't have to check for specific versions of Bluedroid. Instead, we try to call the methods, and check their results.
dom/bluetooth/bluedroid/BluetoothA2dpManager.cpp
--- a/dom/bluetooth/bluedroid/BluetoothA2dpManager.cpp
+++ b/dom/bluetooth/bluedroid/BluetoothA2dpManager.cpp
@@ -26,19 +26,17 @@ USING_BLUETOOTH_NAMESPACE
 #define AVRC_ID_FAST_FOR 0x49
 #define AVRC_KEY_PRESS_STATE  1
 #define AVRC_KEY_RELEASE_STATE  0
 
 namespace {
   StaticRefPtr<BluetoothA2dpManager> sBluetoothA2dpManager;
   bool sInShutdown = false;
   static BluetoothA2dpInterface* sBtA2dpInterface;
-#if ANDROID_VERSION > 17
   static BluetoothAvrcpInterface* sBtAvrcpInterface;
-#endif
 } // anonymous namespace
 
 /*
  * This function maps attribute id and returns corresponding values
  */
 static void
 ConvertAttributeString(BluetoothAvrcpMediaAttribute aAttrId,
                        nsAString& aAttrStr)
@@ -117,44 +115,50 @@ AvStatusToSinkString(BluetoothA2dpConnec
       aString.AssignLiteral("disconnecting");
       break;
     default:
       BT_WARNING("Unknown sink state %d", static_cast<int>(aState));
       return;
   }
 }
 
-#if ANDROID_VERSION > 17
 class InitAvrcpResultHandler MOZ_FINAL : public BluetoothAvrcpResultHandler
 {
 public:
   InitAvrcpResultHandler(BluetoothProfileResultHandler* aRes)
   : mRes(aRes)
   { }
 
   void OnError(BluetoothStatus aStatus) MOZ_OVERRIDE
   {
     BT_WARNING("BluetoothAvrcpInterface::Init failed: %d",
                (int)aStatus);
     if (mRes) {
-      mRes->OnError(NS_ERROR_FAILURE);
+      if (aStatus == STATUS_UNSUPPORTED) {
+        /* Not all versions of Bluedroid support AVRCP. So if the
+         * initialization fails with STATUS_UNSUPPORTED, we still
+         * signal success.
+         */
+        mRes->Init();
+      } else {
+        mRes->OnError(NS_ERROR_FAILURE);
+      }
     }
   }
 
   void Init() MOZ_OVERRIDE
   {
     if (mRes) {
       mRes->Init();
     }
   }
 
 private:
   nsRefPtr<BluetoothProfileResultHandler> mRes;
 };
-#endif
 
 class InitA2dpResultHandler MOZ_FINAL : public BluetoothA2dpResultHandler
 {
 public:
   InitA2dpResultHandler(BluetoothProfileResultHandler* aRes)
   : mRes(aRes)
   { }
 
@@ -164,32 +168,30 @@ public:
                (int)aStatus);
     if (mRes) {
       mRes->OnError(NS_ERROR_FAILURE);
     }
   }
 
   void Init() MOZ_OVERRIDE
   {
-#if ANDROID_VERSION > 17
-    /* Also init AVRCP if it's available, ... */
     BluetoothInterface* btInf = BluetoothInterface::GetInstance();
-    NS_ENSURE_TRUE_VOID(btInf);
+    if (NS_WARN_IF(!btInf)) {
+      mRes->OnError(NS_ERROR_FAILURE);
+      return;
+    }
 
     sBtAvrcpInterface = btInf->GetBluetoothAvrcpInterface();
-    NS_ENSURE_TRUE_VOID(sBtAvrcpInterface);
+    if (NS_WARN_IF(!sBtAvrcpInterface)) {
+      mRes->OnError(NS_ERROR_FAILURE);
+      return;
+    }
 
     BluetoothA2dpManager* a2dpManager = BluetoothA2dpManager::Get();
     sBtAvrcpInterface->Init(a2dpManager, new InitAvrcpResultHandler(mRes));
-#else
-    /* ...or signal success otherwise. */
-    if (mRes) {
-      mRes->Init();
-    }
-#endif
   }
 
 private:
   nsRefPtr<BluetoothProfileResultHandler> mRes;
 };
 
 /*
  * This function will be only called when Bluetooth is turning on.
@@ -277,45 +279,51 @@ BluetoothA2dpManager::Get()
   NS_ENSURE_FALSE(sInShutdown, nullptr);
 
   // Create a new instance, register, and return
   BluetoothA2dpManager* manager = new BluetoothA2dpManager();
   sBluetoothA2dpManager = manager;
   return sBluetoothA2dpManager;
 }
 
-#if ANDROID_VERSION > 17
 class CleanupAvrcpResultHandler MOZ_FINAL : public BluetoothAvrcpResultHandler
 {
 public:
   CleanupAvrcpResultHandler(BluetoothProfileResultHandler* aRes)
   : mRes(aRes)
   { }
 
   void OnError(BluetoothStatus aStatus) MOZ_OVERRIDE
   {
     BT_WARNING("BluetoothAvrcpInterface::Cleanup failed: %d",
                (int)aStatus);
     if (mRes) {
-      mRes->OnError(NS_ERROR_FAILURE);
+      if (aStatus == STATUS_UNSUPPORTED) {
+        /* Not all versions of Bluedroid support AVRCP. So if the
+         * cleanup fails with STATUS_UNSUPPORTED, we still signal
+         * success.
+         */
+        mRes->Deinit();
+      } else {
+        mRes->OnError(NS_ERROR_FAILURE);
+      }
     }
   }
 
   void Cleanup() MOZ_OVERRIDE
   {
     sBtAvrcpInterface = nullptr;
     if (mRes) {
       mRes->Deinit();
     }
   }
 
 private:
   nsRefPtr<BluetoothProfileResultHandler> mRes;
 };
-#endif
 
 class CleanupA2dpResultHandler MOZ_FINAL : public BluetoothA2dpResultHandler
 {
 public:
   CleanupA2dpResultHandler(BluetoothProfileResultHandler* aRes)
   : mRes(aRes)
   { }
 
@@ -326,25 +334,18 @@ public:
     if (mRes) {
       mRes->OnError(NS_ERROR_FAILURE);
     }
   }
 
   void Cleanup() MOZ_OVERRIDE
   {
     sBtA2dpInterface = nullptr;
-#if ANDROID_VERSION > 17
-    /* Cleanup AVRCP if it's available and initialized, ...*/
     if (sBtAvrcpInterface) {
       sBtAvrcpInterface->Cleanup(new CleanupAvrcpResultHandler(mRes));
-    } else
-#endif
-    if (mRes) {
-      /* ...or simply signal success from here. */
-      mRes->Deinit();
     }
   }
 
 private:
   nsRefPtr<BluetoothProfileResultHandler> mRes;
 };
 
 class CleanupA2dpResultHandlerRunnable MOZ_FINAL : public nsRunnable
@@ -352,25 +353,18 @@ class CleanupA2dpResultHandlerRunnable M
 public:
   CleanupA2dpResultHandlerRunnable(BluetoothProfileResultHandler* aRes)
   : mRes(aRes)
   { }
 
   NS_IMETHOD Run() MOZ_OVERRIDE
   {
     sBtA2dpInterface = nullptr;
-#if ANDROID_VERSION > 17
-    /* Cleanup AVRCP if it's available and initialized, ...*/
     if (sBtAvrcpInterface) {
       sBtAvrcpInterface->Cleanup(new CleanupAvrcpResultHandler(mRes));
-    } else
-#endif
-    if (mRes) {
-      /* ...or simply signal success from here. */
-      mRes->Deinit();
     }
 
     return NS_OK;
   }
 
 private:
   nsRefPtr<BluetoothProfileResultHandler> mRes;
 };
@@ -717,17 +711,16 @@ BluetoothA2dpManager::UpdateMetaData(con
                                      const nsAString& aArtist,
                                      const nsAString& aAlbum,
                                      uint64_t aMediaNumber,
                                      uint64_t aTotalMediaCount,
                                      uint32_t aDuration)
 {
   MOZ_ASSERT(NS_IsMainThread());
 
-#if ANDROID_VERSION > 17
   NS_ENSURE_TRUE_VOID(sBtAvrcpInterface);
 
   // Send track changed and position changed if track num is not the same.
   // See also AVRCP 1.3 Spec 5.4.2
   if (mMediaNumber != aMediaNumber &&
       mTrackChangedNotifyType == AVRCP_NTF_INTERIM) {
     BluetoothAvrcpNotificationParam param;
     // convert to network big endian format
@@ -751,31 +744,29 @@ BluetoothA2dpManager::UpdateMetaData(con
   }
 
   mTitle.Assign(aTitle);
   mArtist.Assign(aArtist);
   mAlbum.Assign(aAlbum);
   mMediaNumber = aMediaNumber;
   mTotalMediaCount = aTotalMediaCount;
   mDuration = aDuration;
-#endif
 }
 
 /*
  * This function is to reply AvrcpGetPlayStatusCallback (play-status-request)
  * from media player application (Gaia side)
  */
 void
 BluetoothA2dpManager::UpdatePlayStatus(uint32_t aDuration,
                                        uint32_t aPosition,
                                        ControlPlayStatus aPlayStatus)
 {
   MOZ_ASSERT(NS_IsMainThread());
 
-#if ANDROID_VERSION > 17
   NS_ENSURE_TRUE_VOID(sBtAvrcpInterface);
   // always update playstatus first
   sBtAvrcpInterface->GetPlayStatusRsp(aPlayStatus, aDuration,
                                       aPosition, nullptr);
   // when play status changed, send both play status and position
   if (mPlayStatus != aPlayStatus &&
       mPlayStatusChangedNotifyType == AVRCP_NTF_INTERIM) {
     BluetoothAvrcpNotificationParam param;
@@ -794,33 +785,31 @@ BluetoothA2dpManager::UpdatePlayStatus(u
     sBtAvrcpInterface->RegisterNotificationRsp(AVRCP_EVENT_PLAY_POS_CHANGED,
                                                AVRCP_NTF_CHANGED,
                                                param, nullptr);
   }
 
   mDuration = aDuration;
   mPosition = aPosition;
   mPlayStatus = aPlayStatus;
-#endif
 }
 
 /*
  * This function handles RegisterNotification request from
  * AvrcpRegisterNotificationCallback, which updates current
  * track/status/position status in the INTERRIM response.
  *
  * aParam is only valid when position changed
  */
 void
 BluetoothA2dpManager::UpdateRegisterNotification(BluetoothAvrcpEvent aEvent,
                                                  uint32_t aParam)
 {
   MOZ_ASSERT(NS_IsMainThread());
 
-#if ANDROID_VERSION > 17
   NS_ENSURE_TRUE_VOID(sBtAvrcpInterface);
 
   BluetoothAvrcpNotificationParam param;
 
   switch (aEvent) {
     case AVRCP_EVENT_PLAY_STATUS_CHANGED:
       mPlayStatusChangedNotifyType = AVRCP_NTF_INTERIM;
       param.mPlayStatus = mPlayStatus;
@@ -857,17 +846,16 @@ BluetoothA2dpManager::UpdateRegisterNoti
       mPlaybackInterval = aParam;
       break;
     default:
       break;
   }
 
   sBtAvrcpInterface->RegisterNotificationRsp(aEvent, AVRCP_NTF_INTERIM,
                                              param, nullptr);
-#endif
 }
 
 void
 BluetoothA2dpManager::GetAlbum(nsAString& aAlbum)
 {
   aAlbum.Assign(mAlbum);
 }
 
@@ -1051,36 +1039,32 @@ BluetoothA2dpManager::GetElementAttrNoti
 
   for (uint8_t i = 0; i < aNumAttrs; ++i) {
     attrs[i].mId = aAttrs[i];
     ConvertAttributeString(
       static_cast<BluetoothAvrcpMediaAttribute>(attrs[i].mId),
       attrs[i].mValue);
   }
 
-#if ANDROID_VERSION >= 18
   MOZ_ASSERT(sBtAvrcpInterface);
   sBtAvrcpInterface->GetElementAttrRsp(aNumAttrs, attrs, nullptr);
-#endif // ANDROID_VERSION >= 18
 }
 
 void
 BluetoothA2dpManager::RegisterNotificationNotification(
   BluetoothAvrcpEvent aEvent, uint32_t aParam)
 {
   MOZ_ASSERT(NS_IsMainThread());
 
   BluetoothA2dpManager* a2dp = BluetoothA2dpManager::Get();
   if (!a2dp) {
     return;
   }
 
-#if ANDROID_VERSION >= 18
   a2dp->UpdateRegisterNotification(aEvent, aParam);
-#endif // ANDROID_VERSION >= 18
 }
 
 /* This method is used to get CT features from the Feature Bit Mask. If
  * Advanced Control Player bit is set, the CT supports volume sync (absolute
  * volume feature). If Browsing bit is set, AVRCP 1.4 Browse feature will be
  * supported.
  */
 void