Bug 1026454 - Fire PropertyChanged when discovery state changes, r=echou
authorBen Tian <btian@mozilla.com>
Thu, 19 Jun 2014 15:47:03 +0800
changeset 189425 597a15e183a4f16b4f57b2ab04fe698b95491b68
parent 189424 9c12b64037ada69c17874516c00dbc3158092b98
child 189426 384f7bec8735a16cd5ff582164e82c5e79fdebc2
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersechou
bugs1026454
milestone33.0a1
Bug 1026454 - Fire PropertyChanged when discovery state changes, r=echou
dom/bluetooth2/BluetoothAdapter.cpp
dom/bluetooth2/BluetoothCommon.h
dom/bluetooth2/bluedroid/BluetoothServiceBluedroid.cpp
dom/bluetooth2/bluedroid/hfp/BluetoothHfpManager.cpp
--- a/dom/bluetooth2/BluetoothAdapter.cpp
+++ b/dom/bluetooth2/BluetoothAdapter.cpp
@@ -161,18 +161,17 @@ public:
 static int kCreatePairedDeviceTimeout = 50000; // unit: msec
 
 BluetoothAdapter::BluetoothAdapter(nsPIDOMWindow* aWindow,
                                    const BluetoothValue& aValue)
   : DOMEventTargetHelper(aWindow)
   , BluetoothPropertyContainer(BluetoothObjectType::TYPE_ADAPTER)
   , mJsUuids(nullptr)
   , mJsDeviceAddresses(nullptr)
-  // TODO: Change to Disabled after Bug 1006309 landed
-  , mState(BluetoothAdapterState::Enabled)
+  , mState(BluetoothAdapterState::Disabled)
   , mDiscoverable(false)
   , mDiscovering(false)
   , mPairable(false)
   , mPowered(false)
   , mIsRooted(false)
 {
   MOZ_ASSERT(aWindow);
   MOZ_ASSERT(IsDOMBinding());
--- a/dom/bluetooth2/BluetoothCommon.h
+++ b/dom/bluetooth2/BluetoothCommon.h
@@ -63,17 +63,18 @@ extern bool gBluetoothDebugFlag;
 #define BT_API2_LOGR(msg, ...)                                       \
   BT_LOGR("[WEBBT-API2] " msg, ##__VA_ARGS__)
 
 /**
  * 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))
+  array.AppendElement(BluetoothNamedValue(NS_LITERAL_STRING(name),   \
+                                          BluetoothValue(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]",                         \
--- a/dom/bluetooth2/bluedroid/BluetoothServiceBluedroid.cpp
+++ b/dom/bluetooth2/bluedroid/BluetoothServiceBluedroid.cpp
@@ -142,37 +142,38 @@ public:
 class CleanupTask MOZ_FINAL : public nsRunnable
 {
 public:
   NS_IMETHOD
   Run()
   {
     MOZ_ASSERT(NS_IsMainThread());
 
-    /*
-     * Cleanup static adapter properties and notify adapter to clean them
-     *
-     * TODO: clean up and notify Discovering also
-     */
+    // Return error if BluetoothService is unavailable
+    BluetoothService* bs = BluetoothService::Get();
+    NS_ENSURE_TRUE(bs, NS_ERROR_FAILURE);
+
+    // Cleanup static adapter properties and notify adapter.
     sAdapterBdAddress.Truncate();
     sAdapterBdName.Truncate();
-    sAdapterDiscoverable = false;
 
     InfallibleTArray<BluetoothNamedValue> props;
     BT_APPEND_NAMED_VALUE(props, "Name", sAdapterBdName);
     BT_APPEND_NAMED_VALUE(props, "Address", sAdapterBdAddress);
-    BT_APPEND_NAMED_VALUE(props, "Discoverable",
-                          BluetoothValue(sAdapterDiscoverable));
-    BluetoothValue value(props);
+    if (sAdapterDiscoverable) {
+      sAdapterDiscoverable = false;
+      BT_APPEND_NAMED_VALUE(props, "Discoverable", false);
+    }
+    if (sAdapterDiscovering) {
+      sAdapterDiscovering = false;
+      BT_APPEND_NAMED_VALUE(props, "Discovering", false);
+    }
+
     BluetoothSignal signal(NS_LITERAL_STRING("PropertyChanged"),
-                           NS_LITERAL_STRING(KEY_ADAPTER), value);
-
-    BluetoothService* bs = BluetoothService::Get();
-    NS_ENSURE_TRUE(bs, NS_ERROR_FAILURE);
-
+                           NS_LITERAL_STRING(KEY_ADAPTER), props);
     bs->DistributeSignal(signal);
 
     // Cleanup bluetooth interfaces after BT state becomes BT_STATE_OFF.
     BluetoothHfpManager::DeinitHfpInterface();
     BluetoothA2dpManager::DeinitA2dpInterface();
     sBtInterface->cleanup();
 
     return NS_OK;
@@ -403,17 +404,17 @@ AdapterPropertiesCallback(bt_status_t aS
         nsAutoString deviceBdAddress;
         BdAddressTypeToString(deviceBdAddressTypes + index, deviceBdAddress);
         sAdapterBondedAddressArray.AppendElement(deviceBdAddress);
       }
 
       propertyValue = sAdapterBondedAddressArray;
       BT_APPEND_NAMED_VALUE(props, "Devices", propertyValue);
     } else if (p.type == BT_PROPERTY_UUIDS) {
-      //FIXME: This will be implemented in the later patchset
+      // FIXME: Later patchset will implement this.
       continue;
     } else {
       BT_LOGD("Unhandled adapter property type: %d", p.type);
       continue;
     }
   }
 
   NS_ENSURE_TRUE_VOID(props.Length() > 0);
@@ -572,16 +573,29 @@ DeviceFoundCallback(int aNumProperties, 
 class DiscoveryStateChangedCallbackTask MOZ_FINAL : public nsRunnable
 {
 public:
   NS_IMETHOD
   Run()
   {
     MOZ_ASSERT(NS_IsMainThread());
 
+    // Return error if BluetoothService is unavailable
+    BluetoothService* bs = BluetoothService::Get();
+    NS_ENSURE_TRUE(bs, NS_ERROR_FAILURE);
+
+    // Fire PropertyChanged of Discovering
+    InfallibleTArray<BluetoothNamedValue> props;
+    BT_APPEND_NAMED_VALUE(props, "Discovering", sAdapterDiscovering);
+
+    BluetoothSignal signal(NS_LITERAL_STRING("PropertyChanged"),
+                           NS_LITERAL_STRING(KEY_ADAPTER), props);
+    bs->DistributeSignal(signal);
+
+    // Reply that Promise is resolved
     if (!sChangeDiscoveryRunnableArray.IsEmpty()) {
       BluetoothValue values(true);
       DispatchBluetoothReply(sChangeDiscoveryRunnableArray[0],
                              values, EmptyString());
 
       sChangeDiscoveryRunnableArray.RemoveElementAt(0);
     }
 
@@ -944,27 +958,25 @@ BluetoothServiceBluedroid::GetAdaptersIn
    */
   BluetoothValue adaptersProperties = InfallibleTArray<BluetoothNamedValue>();
   uint32_t numAdapters = 1; // Bluedroid supports single adapter only
 
   for (uint32_t i = 0; i < numAdapters; i++) {
     BluetoothValue properties = InfallibleTArray<BluetoothNamedValue>();
 
     BT_APPEND_NAMED_VALUE(properties.get_ArrayOfBluetoothNamedValue(),
-                          "State", BluetoothValue(sAdapterEnabled));
+                          "State", sAdapterEnabled);
     BT_APPEND_NAMED_VALUE(properties.get_ArrayOfBluetoothNamedValue(),
                           "Address", sAdapterBdAddress);
     BT_APPEND_NAMED_VALUE(properties.get_ArrayOfBluetoothNamedValue(),
                           "Name", sAdapterBdName);
     BT_APPEND_NAMED_VALUE(properties.get_ArrayOfBluetoothNamedValue(),
-                          "Discoverable",
-                          BluetoothValue(sAdapterDiscoverable));
+                          "Discoverable", sAdapterDiscoverable);
     BT_APPEND_NAMED_VALUE(properties.get_ArrayOfBluetoothNamedValue(),
-                          "Discovering",
-                          BluetoothValue(sAdapterDiscovering));
+                          "Discovering", sAdapterDiscovering);
 
     BT_APPEND_NAMED_VALUE(adaptersProperties.get_ArrayOfBluetoothNamedValue(),
                           "Adapter", properties);
   }
 
   DispatchBluetoothReply(aRunnable, adaptersProperties, EmptyString());
   return NS_OK;
 }
--- a/dom/bluetooth2/bluedroid/hfp/BluetoothHfpManager.cpp
+++ b/dom/bluetooth2/bluedroid/hfp/BluetoothHfpManager.cpp
@@ -195,17 +195,17 @@ static bthf_callbacks_t sBluetoothHfpCal
 static bool
 IsValidDtmf(const char aChar) {
   // Valid DTMF: [*#0-9ABCD]
   return (aChar == '*' || aChar == '#') ||
          (aChar >= '0' && aChar <= '9') ||
          (aChar >= 'A' && aChar <= 'D');
 }
 
-class BluetoothHfpManager::GetVolumeTask : public nsISettingsServiceCallback
+class BluetoothHfpManager::GetVolumeTask MOZ_FINAL : public nsISettingsServiceCallback
 {
 public:
   NS_DECL_ISUPPORTS
 
   NS_IMETHOD
   Handle(const nsAString& aName, JS::Handle<JS::Value> aResult)
   {
     MOZ_ASSERT(NS_IsMainThread());