Bug 1006314 - Patch 2/3: Implement BluetoothDiscoveryHandle, r=bz, r=echou
authorBen Tian <btian@mozilla.com>
Wed, 02 Jul 2014 11:16:37 +0800
changeset 191897 c1811ea9b47444b84b62ce8044e89792bfde5a0a
parent 191896 45dd35d71e50e948cf9f869828fee5aecaa6f4a0
child 191898 5aca12a38f2cb3fe72bf142dbbd0d839b587de71
push id45685
push usercbook@mozilla.com
push dateWed, 02 Jul 2014 13:09:48 +0000
treeherdermozilla-inbound@60133a85f8ae [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbz, echou
bugs1006314
milestone33.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 1006314 - Patch 2/3: Implement BluetoothDiscoveryHandle, r=bz, r=echou
dom/bluetooth2/BluetoothCommon.h
dom/bluetooth2/BluetoothDiscoveryHandle.cpp
dom/bluetooth2/BluetoothDiscoveryHandle.h
dom/bluetooth2/bluedroid/BluetoothServiceBluedroid.cpp
dom/bluetooth2/moz.build
--- a/dom/bluetooth2/BluetoothCommon.h
+++ b/dom/bluetooth2/BluetoothCommon.h
@@ -97,20 +97,21 @@ extern bool gBluetoothDebugFlag;
 
 #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"
-#define KEY_REMOTE_AGENT "/B2G/bluetooth/remote_device_agent"
-#define KEY_MANAGER      "/B2G/bluetooth/manager"
-#define KEY_ADAPTER      "/B2G/bluetooth/adapter"
+#define KEY_LOCAL_AGENT       "/B2G/bluetooth/agent"
+#define KEY_REMOTE_AGENT      "/B2G/bluetooth/remote_device_agent"
+#define KEY_MANAGER           "/B2G/bluetooth/manager"
+#define KEY_ADAPTER           "/B2G/bluetooth/adapter"
+#define KEY_DISCOVERY_HANDLE  "/B2G/bluetooth/discovery_handle"
 
 /**
  * When the connection status of a Bluetooth profile is changed, we'll notify
  * observers which register the following topics.
  */
 #define BLUETOOTH_A2DP_STATUS_CHANGED_ID "bluetooth-a2dp-status-changed"
 #define BLUETOOTH_HFP_STATUS_CHANGED_ID  "bluetooth-hfp-status-changed"
 #define BLUETOOTH_HID_STATUS_CHANGED_ID  "bluetooth-hid-status-changed"
new file mode 100644
--- /dev/null
+++ b/dom/bluetooth2/BluetoothDiscoveryHandle.cpp
@@ -0,0 +1,98 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim:set ts=2 sw=2 sts=2 et cindent: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "BluetoothDiscoveryHandle.h"
+#include "BluetoothService.h"
+
+#include "mozilla/dom/bluetooth/BluetoothTypes.h"
+#include "mozilla/dom/BluetoothDeviceEvent.h"
+#include "mozilla/dom/BluetoothDiscoveryHandleBinding.h"
+#include "nsThreadUtils.h"
+
+USING_BLUETOOTH_NAMESPACE
+
+NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(BluetoothDiscoveryHandle)
+NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
+
+NS_IMPL_ADDREF_INHERITED(BluetoothDiscoveryHandle, DOMEventTargetHelper)
+NS_IMPL_RELEASE_INHERITED(BluetoothDiscoveryHandle, DOMEventTargetHelper)
+
+BluetoothDiscoveryHandle::BluetoothDiscoveryHandle(nsPIDOMWindow* aWindow)
+  : DOMEventTargetHelper(aWindow)
+{
+  MOZ_ASSERT(aWindow);
+  MOZ_ASSERT(IsDOMBinding());
+
+  ListenToBluetoothSignal(true);
+}
+
+BluetoothDiscoveryHandle::~BluetoothDiscoveryHandle()
+{
+  ListenToBluetoothSignal(false);
+}
+
+void
+BluetoothDiscoveryHandle::ListenToBluetoothSignal(bool aStart)
+{
+  BluetoothService* bs = BluetoothService::Get();
+  NS_ENSURE_TRUE_VOID(bs);
+
+  if (aStart) {
+    bs->RegisterBluetoothSignalHandler(
+      NS_LITERAL_STRING(KEY_DISCOVERY_HANDLE), this);
+  } else {
+    bs->UnregisterBluetoothSignalHandler(
+      NS_LITERAL_STRING(KEY_DISCOVERY_HANDLE), this);
+  }
+}
+
+// static
+already_AddRefed<BluetoothDiscoveryHandle>
+BluetoothDiscoveryHandle::Create(nsPIDOMWindow* aWindow)
+{
+  MOZ_ASSERT(NS_IsMainThread());
+  MOZ_ASSERT(aWindow);
+
+  nsRefPtr<BluetoothDiscoveryHandle> handle =
+    new BluetoothDiscoveryHandle(aWindow);
+  return handle.forget();
+}
+
+void
+BluetoothDiscoveryHandle::DispatchDeviceEvent(const BluetoothValue& aValue)
+{
+  // Create a new BluetoothDevice
+  nsRefPtr<BluetoothDevice> device =
+    BluetoothDevice::Create(GetOwner(), aValue);
+
+  // Notify application of discovered device
+  BluetoothDeviceEventInit init;
+  init.mDevice = device;
+  nsRefPtr<BluetoothDeviceEvent> event =
+    BluetoothDeviceEvent::Constructor(this,
+                                      NS_LITERAL_STRING("devicefound"),
+                                      init);
+  DispatchTrustedEvent(event);
+}
+
+void
+BluetoothDiscoveryHandle::Notify(const BluetoothSignal& aData)
+{
+  BT_LOGD("[DH] %s", NS_ConvertUTF16toUTF8(aData.name()).get());
+
+  if (aData.name().EqualsLiteral("DeviceFound")) {
+    DispatchDeviceEvent(aData.value());
+  } else {
+    BT_WARNING("Not handling discovery handle signal: %s",
+               NS_ConvertUTF16toUTF8(aData.name()).get());
+  }
+}
+
+JSObject*
+BluetoothDiscoveryHandle::WrapObject(JSContext* aCx)
+{
+  return BluetoothDiscoveryHandleBinding::Wrap(aCx, this);
+}
new file mode 100644
--- /dev/null
+++ b/dom/bluetooth2/BluetoothDiscoveryHandle.h
@@ -0,0 +1,56 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim:set ts=2 sw=2 sts=2 et cindent: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_dom_bluetooth_bluetoothdiscoveryhandle_h
+#define mozilla_dom_bluetooth_bluetoothdiscoveryhandle_h
+
+#include "BluetoothCommon.h"
+#include "mozilla/Attributes.h"
+#include "mozilla/dom/bluetooth/BluetoothTypes.h"
+#include "mozilla/DOMEventTargetHelper.h"
+#include "mozilla/Observer.h"
+#include "nsISupportsImpl.h"
+
+BEGIN_BLUETOOTH_NAMESPACE
+
+class BluetoothValue;
+
+class BluetoothDiscoveryHandle MOZ_FINAL : public DOMEventTargetHelper
+                                         , public BluetoothSignalObserver
+{
+public:
+  NS_DECL_ISUPPORTS_INHERITED
+
+  static already_AddRefed<BluetoothDiscoveryHandle>
+    Create(nsPIDOMWindow* aWindow);
+
+  IMPL_EVENT_HANDLER(devicefound);
+
+  void Notify(const BluetoothSignal& aData); // BluetoothSignalObserver
+  virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
+
+private:
+  BluetoothDiscoveryHandle(nsPIDOMWindow* aWindow);
+  ~BluetoothDiscoveryHandle();
+
+  /**
+   * Start/Stop listening to bluetooth signal.
+   *
+   * @param aStart [in] Whether to start or stop listening to bluetooth signal
+   */
+  void ListenToBluetoothSignal(bool aStart);
+
+  /**
+   * Fire BluetoothDeviceEvent to trigger ondevicefound event handler.
+   *
+   * @param aValue [in] Properties array of the found device
+   */
+  void DispatchDeviceEvent(const BluetoothValue& aValue);
+};
+
+END_BLUETOOTH_NAMESPACE
+
+#endif // mozilla_dom_bluetooth_bluetoothdiscoveryhandle_h
--- a/dom/bluetooth2/bluedroid/BluetoothServiceBluedroid.cpp
+++ b/dom/bluetooth2/bluedroid/BluetoothServiceBluedroid.cpp
@@ -576,17 +576,17 @@ DeviceFoundCallback(int aNumProperties, 
       BT_APPEND_NAMED_VALUE(propertiesArray, "Icon", propertyValue);
     } else {
       BT_LOGD("Not handled remote device property: %d", p.type);
     }
   }
 
   BluetoothValue value = propertiesArray;
   BluetoothSignal signal(NS_LITERAL_STRING("DeviceFound"),
-                         NS_LITERAL_STRING(KEY_ADAPTER), value);
+                         NS_LITERAL_STRING(KEY_DISCOVERY_HANDLE), value);
   nsRefPtr<DistributeBluetoothSignalTask>
     t = new DistributeBluetoothSignalTask(signal);
   if (NS_FAILED(NS_DispatchToMainThread(t))) {
     BT_WARNING("Failed to dispatch to main thread!");
   }
 }
 
 class DiscoveryStateChangedCallbackTask MOZ_FINAL : public nsRunnable
--- a/dom/bluetooth2/moz.build
+++ b/dom/bluetooth2/moz.build
@@ -3,16 +3,17 @@
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 if CONFIG['MOZ_B2G_BT']:
     SOURCES += [
         'BluetoothAdapter.cpp',
         'BluetoothDevice.cpp',
+        'BluetoothDiscoveryHandle.cpp',
         'BluetoothHidManager.cpp',
         'BluetoothManager.cpp',
         'BluetoothProfileController.cpp',
         'BluetoothReplyRunnable.cpp',
         'BluetoothService.cpp',
         'BluetoothUuid.cpp',
         'ipc/BluetoothChild.cpp',
         'ipc/BluetoothParent.cpp',
@@ -89,16 +90,17 @@ if CONFIG['MOZ_B2G_BT']:
 EXPORTS.mozilla.dom.bluetooth.ipc += [
     'ipc/BluetoothMessageUtils.h',
 ]
 
 EXPORTS.mozilla.dom.bluetooth += [
     'BluetoothAdapter.h',
     'BluetoothCommon.h',
     'BluetoothDevice.h',
+    'BluetoothDiscoveryHandle.h',
     'BluetoothManager.h',
 ]
 
 IPDL_SOURCES += [
     'ipc/BluetoothTypes.ipdlh',
     'ipc/PBluetooth.ipdl',
     'ipc/PBluetoothRequest.ipdl',
 ]