Bug 995061 - Patch 1/2: [bluedroid] define and apply macro to append named value in profile managers, r=echou
authorBen Tian <btian@mozilla.com>
Fri, 11 Apr 2014 11:52:44 +0800
changeset 178919 c43864d0f1852ce8b2224f6e586afbebafdc49f3
parent 178918 fd5fd1effa2ab0527bff5dce6b63f22a8eff5857
child 178920 143a22dd2399e78908f6e444363ae66c769c56ac
push id272
push userpvanderbeken@mozilla.com
push dateMon, 05 May 2014 16:31:18 +0000
reviewersechou
bugs995061
milestone31.0a1
Bug 995061 - Patch 1/2: [bluedroid] define and apply macro to append named value in profile managers, r=echou
dom/bluetooth/BluetoothCommon.h
dom/bluetooth/BluetoothHidManager.cpp
dom/bluetooth/bluedroid/BluetoothA2dpManager.cpp
dom/bluetooth/bluedroid/BluetoothOppManager.cpp
dom/bluetooth/bluedroid/BluetoothUtils.cpp
dom/bluetooth/bluedroid/hfp/BluetoothHfpManager.cpp
--- a/dom/bluetooth/BluetoothCommon.h
+++ b/dom/bluetooth/BluetoothCommon.h
@@ -52,16 +52,35 @@ extern bool gBluetoothDebugFlag;
       printf("%s: " msg, __FUNCTION__, ##__VA_ARGS__);               \
     }                                                                \
   } while(0)
 
 #define BT_LOGR(msg, ...) printf("%s: " msg, __FUNCTION__, ##__VA_ARGS__))
 #define BT_WARNING(msg, ...) printf("%s: " msg, __FUNCTION__, ##__VA_ARGS__))
 #endif
 
+/**
+ * Wrap literal name and value into a BluetoothNamedValue
+ * and append it to the array.
+ */
+#define BT_APPEND_NAMED_VALUE(array, name, value)                    \
+  array.AppendElement(BluetoothNamedValue(NS_LITERAL_STRING(name), value))
+
+/**
+ * Ensure success of system message broadcast with void return.
+ */
+#define BT_ENSURE_TRUE_VOID_BROADCAST_SYSMSG(type, parameters)       \
+  do {                                                               \
+    if (!BroadcastSystemMessage(type, parameters)) {                 \
+      BT_WARNING("Failed to broadcast [%s]",                         \
+                 NS_ConvertUTF16toUTF8(type).get());                 \
+      return;                                                        \
+    }                                                                \
+  } while(0)
+
 #define BEGIN_BLUETOOTH_NAMESPACE \
   namespace mozilla { namespace dom { namespace bluetooth {
 #define END_BLUETOOTH_NAMESPACE \
   } /* namespace bluetooth */ } /* namespace dom */ } /* namespace mozilla */
 #define USING_BLUETOOTH_NAMESPACE \
   using namespace mozilla::dom::bluetooth;
 
 #define KEY_LOCAL_AGENT  "/B2G/bluetooth/agent"
--- a/dom/bluetooth/BluetoothHidManager.cpp
+++ b/dom/bluetooth/BluetoothHidManager.cpp
@@ -236,28 +236,20 @@ BluetoothHidManager::HandleInputProperty
 void
 BluetoothHidManager::NotifyStatusChanged()
 {
   MOZ_ASSERT(NS_IsMainThread());
 
   NS_NAMED_LITERAL_STRING(type, BLUETOOTH_HID_STATUS_CHANGED_ID);
   InfallibleTArray<BluetoothNamedValue> parameters;
 
-  BluetoothValue v = mConnected;
-  parameters.AppendElement(
-    BluetoothNamedValue(NS_LITERAL_STRING("connected"), v));
+  BT_APPEND_NAMED_VALUE(parameters, "connected", mConnected);
+  BT_APPEND_NAMED_VALUE(parameters, "address", mDeviceAddress);
 
-  v = mDeviceAddress;
-  parameters.AppendElement(
-    BluetoothNamedValue(NS_LITERAL_STRING("address"), v));
-
-  if (!BroadcastSystemMessage(type, parameters)) {
-    BT_WARNING("Failed to broadcast system message to settings");
-    return;
-  }
+  BT_ENSURE_TRUE_VOID_BROADCAST_SYSMSG(type, parameters);
 }
 
 void
 BluetoothHidManager::OnGetServiceChannel(const nsAString& aDeviceAddress,
                                          const nsAString& aServiceUuid,
                                          int aChannel)
 {
   // Do nothing here as bluez acquires service channel and connects for us
--- a/dom/bluetooth/bluedroid/BluetoothA2dpManager.cpp
+++ b/dom/bluetooth/bluedroid/BluetoothA2dpManager.cpp
@@ -261,18 +261,17 @@ A2dpConnectionStateCallback(btav_connect
 
   nsString remoteDeviceBdAddress;
   BdAddressTypeToString(aBdAddress, remoteDeviceBdAddress);
 
   nsString a2dpState;
   AvStatusToSinkString(aState, a2dpState);
 
   InfallibleTArray<BluetoothNamedValue> props;
-  props.AppendElement(
-    BluetoothNamedValue(NS_LITERAL_STRING("State"), a2dpState));
+  BT_APPEND_NAMED_VALUE(props, "State", a2dpState);
 
   BluetoothSignal signal(NS_LITERAL_STRING("AudioSink"),
                          remoteDeviceBdAddress, props);
   NS_DispatchToMainThread(new SinkPropertyChangedHandler(signal));
 }
 
 static void
 A2dpAudioStateCallback(btav_audio_state_t aState,
@@ -291,18 +290,17 @@ A2dpAudioStateCallback(btav_audio_state_
     // for avdtp state stop stream
     a2dpState = NS_LITERAL_STRING("connected");
   } else if (aState == BTAV_AUDIO_STATE_REMOTE_SUSPEND) {
     // for avdtp state suspend stream from remote side
     a2dpState = NS_LITERAL_STRING("connected");
   }
 
   InfallibleTArray<BluetoothNamedValue> props;
-  props.AppendElement(
-    BluetoothNamedValue(NS_LITERAL_STRING("State"), a2dpState));
+  BT_APPEND_NAMED_VALUE(props, "State", a2dpState);
 
   BluetoothSignal signal(NS_LITERAL_STRING("AudioSink"),
                          remoteDeviceBdAddress, props);
   NS_DispatchToMainThread(new SinkPropertyChangedHandler(signal));
 }
 
 #if ANDROID_VERSION > 17
 /*
--- a/dom/bluetooth/bluedroid/BluetoothOppManager.cpp
+++ b/dom/bluetooth/bluedroid/BluetoothOppManager.cpp
@@ -1260,147 +1260,74 @@ BluetoothOppManager::SendObexData(uint8_
 
 void
 BluetoothOppManager::FileTransferComplete()
 {
   if (mSendTransferCompleteFlag) {
     return;
   }
 
-  nsString type, name;
-  BluetoothValue v;
+  NS_NAMED_LITERAL_STRING(type, "bluetooth-opp-transfer-complete");
   InfallibleTArray<BluetoothNamedValue> parameters;
-  type.AssignLiteral("bluetooth-opp-transfer-complete");
-
-  name.AssignLiteral("address");
-  v = mDeviceAddress;
-  parameters.AppendElement(BluetoothNamedValue(name, v));
-
-  name.AssignLiteral("success");
-  v = mSuccessFlag;
-  parameters.AppendElement(BluetoothNamedValue(name, v));
-
-  name.AssignLiteral("received");
-  v = mIsServer;
-  parameters.AppendElement(BluetoothNamedValue(name, v));
 
-  name.AssignLiteral("fileName");
-  v = mFileName;
-  parameters.AppendElement(BluetoothNamedValue(name, v));
-
-  name.AssignLiteral("fileLength");
-  v = mSentFileLength;
-  parameters.AppendElement(BluetoothNamedValue(name, v));
+  BT_APPEND_NAMED_VALUE(parameters, "address", mDeviceAddress);
+  BT_APPEND_NAMED_VALUE(parameters, "success", mSuccessFlag);
+  BT_APPEND_NAMED_VALUE(parameters, "received", mIsServer);
+  BT_APPEND_NAMED_VALUE(parameters, "fileName", mFileName);
+  BT_APPEND_NAMED_VALUE(parameters, "fileLength", mSentFileLength);
+  BT_APPEND_NAMED_VALUE(parameters, "contentType", mContentType);
 
-  name.AssignLiteral("contentType");
-  v = mContentType;
-  parameters.AppendElement(BluetoothNamedValue(name, v));
-
-  if (!BroadcastSystemMessage(type, parameters)) {
-    BT_WARNING("Failed to broadcast [bluetooth-opp-transfer-complete]");
-    return;
-  }
+  BT_ENSURE_TRUE_VOID_BROADCAST_SYSMSG(type, parameters);
 
   mSendTransferCompleteFlag = true;
 }
 
 void
 BluetoothOppManager::StartFileTransfer()
 {
-  nsString type, name;
-  BluetoothValue v;
+  NS_NAMED_LITERAL_STRING(type, "bluetooth-opp-transfer-start");
   InfallibleTArray<BluetoothNamedValue> parameters;
-  type.AssignLiteral("bluetooth-opp-transfer-start");
-
-  name.AssignLiteral("address");
-  v = mDeviceAddress;
-  parameters.AppendElement(BluetoothNamedValue(name, v));
-
-  name.AssignLiteral("received");
-  v = mIsServer;
-  parameters.AppendElement(BluetoothNamedValue(name, v));
 
-  name.AssignLiteral("fileName");
-  v = mFileName;
-  parameters.AppendElement(BluetoothNamedValue(name, v));
-
-  name.AssignLiteral("fileLength");
-  v = mFileLength;
-  parameters.AppendElement(BluetoothNamedValue(name, v));
+  BT_APPEND_NAMED_VALUE(parameters, "address", mDeviceAddress);
+  BT_APPEND_NAMED_VALUE(parameters, "received", mIsServer);
+  BT_APPEND_NAMED_VALUE(parameters, "fileName", mFileName);
+  BT_APPEND_NAMED_VALUE(parameters, "fileLength", mFileLength);
+  BT_APPEND_NAMED_VALUE(parameters, "contentType", mContentType);
 
-  name.AssignLiteral("contentType");
-  v = mContentType;
-  parameters.AppendElement(BluetoothNamedValue(name, v));
-
-  if (!BroadcastSystemMessage(type, parameters)) {
-    BT_WARNING("Failed to broadcast [bluetooth-opp-transfer-start]");
-    return;
-  }
+  BT_ENSURE_TRUE_VOID_BROADCAST_SYSMSG(type, parameters);
 
   mSendTransferCompleteFlag = false;
 }
 
 void
 BluetoothOppManager::UpdateProgress()
 {
-  nsString type, name;
-  BluetoothValue v;
+  NS_NAMED_LITERAL_STRING(type, "bluetooth-opp-update-progress");
   InfallibleTArray<BluetoothNamedValue> parameters;
-  type.AssignLiteral("bluetooth-opp-update-progress");
-
-  name.AssignLiteral("address");
-  v = mDeviceAddress;
-  parameters.AppendElement(BluetoothNamedValue(name, v));
-
-  name.AssignLiteral("received");
-  v = mIsServer;
-  parameters.AppendElement(BluetoothNamedValue(name, v));
 
-  name.AssignLiteral("processedLength");
-  v = mSentFileLength;
-  parameters.AppendElement(BluetoothNamedValue(name, v));
+  BT_APPEND_NAMED_VALUE(parameters, "address", mDeviceAddress);
+  BT_APPEND_NAMED_VALUE(parameters, "received", mIsServer);
+  BT_APPEND_NAMED_VALUE(parameters, "processedLength", mSentFileLength);
+  BT_APPEND_NAMED_VALUE(parameters, "fileLength", mFileLength);
 
-  name.AssignLiteral("fileLength");
-  v = mFileLength;
-  parameters.AppendElement(BluetoothNamedValue(name, v));
-
-  if (!BroadcastSystemMessage(type, parameters)) {
-    BT_WARNING("Failed to broadcast [bluetooth-opp-update-progress]");
-    return;
-  }
+  BT_ENSURE_TRUE_VOID_BROADCAST_SYSMSG(type, parameters);
 }
 
 void
 BluetoothOppManager::ReceivingFileConfirmation()
 {
-  nsString type, name;
-  BluetoothValue v;
+  NS_NAMED_LITERAL_STRING(type, "bluetooth-opp-receiving-file-confirmation");
   InfallibleTArray<BluetoothNamedValue> parameters;
-  type.AssignLiteral("bluetooth-opp-receiving-file-confirmation");
-
-  name.AssignLiteral("address");
-  v = mDeviceAddress;
-  parameters.AppendElement(BluetoothNamedValue(name, v));
-
-  name.AssignLiteral("fileName");
-  v = mFileName;
-  parameters.AppendElement(BluetoothNamedValue(name, v));
 
-  name.AssignLiteral("fileLength");
-  v = mFileLength;
-  parameters.AppendElement(BluetoothNamedValue(name, v));
+  BT_APPEND_NAMED_VALUE(parameters, "address", mDeviceAddress);
+  BT_APPEND_NAMED_VALUE(parameters, "fileName", mFileName);
+  BT_APPEND_NAMED_VALUE(parameters, "fileLength", mFileLength);
+  BT_APPEND_NAMED_VALUE(parameters, "contentType", mContentType);
 
-  name.AssignLiteral("contentType");
-  v = mContentType;
-  parameters.AppendElement(BluetoothNamedValue(name, v));
-
-  if (!BroadcastSystemMessage(type, parameters)) {
-    BT_WARNING("Failed to send [bluetooth-opp-receiving-file-confirmation]");
-    return;
-  }
+  BT_ENSURE_TRUE_VOID_BROADCAST_SYSMSG(type, parameters);
 }
 
 void
 BluetoothOppManager::NotifyAboutFileChange()
 {
   NS_NAMED_LITERAL_STRING(data, "modified");
 
   nsCOMPtr<nsIObserverService> obs =
--- a/dom/bluetooth/bluedroid/BluetoothUtils.cpp
+++ b/dom/bluetooth/bluedroid/BluetoothUtils.cpp
@@ -171,20 +171,18 @@ DispatchBluetoothReply(BluetoothReplyRun
 void
 DispatchStatusChangedEvent(const nsAString& aType,
                            const nsAString& aAddress,
                            bool aStatus)
 {
   MOZ_ASSERT(NS_IsMainThread());
 
   InfallibleTArray<BluetoothNamedValue> data;
-  data.AppendElement(
-    BluetoothNamedValue(NS_LITERAL_STRING("address"), nsString(aAddress)));
-  data.AppendElement(
-    BluetoothNamedValue(NS_LITERAL_STRING("status"), aStatus));
+  BT_APPEND_NAMED_VALUE(data, "address", nsString(aAddress));
+  BT_APPEND_NAMED_VALUE(data, "status", aStatus);
 
   BluetoothSignal signal(nsString(aType), NS_LITERAL_STRING(KEY_ADAPTER), data);
 
   BluetoothService* bs = BluetoothService::Get();
   NS_ENSURE_TRUE_VOID(bs);
   bs->DistributeSignal(signal);
 }
 
--- a/dom/bluetooth/bluedroid/hfp/BluetoothHfpManager.cpp
+++ b/dom/bluetooth/bluedroid/hfp/BluetoothHfpManager.cpp
@@ -806,28 +806,22 @@ BluetoothHfpManager::NotifyConnectionSta
       Reset();
     }
   }
 }
 
 void
 BluetoothHfpManager::NotifyDialer(const nsAString& aCommand)
 {
-  BluetoothValue v;
+  NS_NAMED_LITERAL_STRING(type, "bluetooth-dialer-command");
   InfallibleTArray<BluetoothNamedValue> parameters;
 
-  NS_NAMED_LITERAL_STRING(type, "bluetooth-dialer-command");
-  NS_NAMED_LITERAL_STRING(name, "command");
+  BT_APPEND_NAMED_VALUE(parameters, "command", nsString(aCommand));
 
-  v = nsString(aCommand);
-  parameters.AppendElement(BluetoothNamedValue(name, v));
-
-  if (!BroadcastSystemMessage(type, parameters)) {
-    BT_WARNING("Failed to broadcast system message to dialer");
-  }
+  BT_ENSURE_TRUE_VOID_BROADCAST_SYSMSG(type, parameters);
 }
 
 void
 BluetoothHfpManager::HandleVolumeChanged(const nsAString& aData)
 {
   MOZ_ASSERT(NS_IsMainThread());
 
   // The string that we're interested in will be a JSON string that looks like: