From 0402aa3fd081c1621b501be247ca1685a4fdb440 Mon Sep 17 00:00:00 2001 From: Ben Tian Date: Thu, 29 May 2014 17:16:20 +0800 Subject: [PATCH] Bug 1006309 - Patch 3/4: Query adapters list from bluetooth backend, r=echou --- dom/bluetooth2/BluetoothManager.cpp | 64 +++++++++++++++++++ dom/bluetooth2/BluetoothReplyRunnable.cpp | 5 +- dom/bluetooth2/BluetoothReplyRunnable.h | 4 ++ dom/bluetooth2/BluetoothService.cpp | 7 +- dom/bluetooth2/BluetoothService.h | 4 +- .../bluedroid/BluetoothServiceBluedroid.cpp | 47 +++++++++----- .../bluedroid/BluetoothServiceBluedroid.h | 15 +++-- dom/bluetooth2/bluez/BluetoothDBusService.cpp | 7 +- dom/bluetooth2/bluez/BluetoothDBusService.h | 15 +++-- dom/bluetooth2/ipc/BluetoothParent.cpp | 10 +-- dom/bluetooth2/ipc/BluetoothParent.h | 2 +- .../ipc/BluetoothServiceChildProcess.cpp | 4 +- .../ipc/BluetoothServiceChildProcess.h | 2 +- dom/bluetooth2/ipc/PBluetooth.ipdl | 4 +- 14 files changed, 139 insertions(+), 51 deletions(-) diff --git a/dom/bluetooth2/BluetoothManager.cpp b/dom/bluetooth2/BluetoothManager.cpp index 36ce4cfd659b..f8735f010847 100644 --- a/dom/bluetooth2/BluetoothManager.cpp +++ b/dom/bluetooth2/BluetoothManager.cpp @@ -9,6 +9,7 @@ #include "BluetoothCommon.h" #include "BluetoothAdapter.h" #include "BluetoothService.h" +#include "BluetoothReplyRunnable.h" #include "mozilla/dom/bluetooth/BluetoothTypes.h" #include "mozilla/dom/BluetoothManager2Binding.h" @@ -29,6 +30,62 @@ NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper) NS_IMPL_ADDREF_INHERITED(BluetoothManager, DOMEventTargetHelper) NS_IMPL_RELEASE_INHERITED(BluetoothManager, DOMEventTargetHelper) +class GetAdaptersTask : public BluetoothReplyRunnable +{ + public: + GetAdaptersTask(BluetoothManager* aManager) + : BluetoothReplyRunnable(nullptr) + , mManager(aManager) + { } + + bool + ParseSuccessfulReply(JS::MutableHandle aValue) + { + /** + * Unwrap BluetoothReply.BluetoothReplySuccess.BluetoothValue = + * BluetoothNamedValue[] + * | + * |__ BluetoothNamedValue = + * | {"Adapter", BluetoothValue = BluetoothNamedValue[]} + * | + * |__ BluetoothNamedValue = + * | {"Adapter", BluetoothValue = BluetoothNamedValue[]} + * ... + */ + + // Extract the array of all adapters' properties + const BluetoothValue& adaptersProperties = + mReply->get_BluetoothReplySuccess().value(); + NS_ENSURE_TRUE(adaptersProperties.type() == + BluetoothValue::TArrayOfBluetoothNamedValue, false); + + const InfallibleTArray& adaptersPropertiesArray = + adaptersProperties.get_ArrayOfBluetoothNamedValue(); + + // Append a BluetoothAdapter into adapters array for each properties array + uint32_t numAdapters = adaptersPropertiesArray.Length(); + for (uint32_t i = 0; i < numAdapters; i++) { + MOZ_ASSERT(adaptersPropertiesArray[i].name().EqualsLiteral("Adapter")); + + const BluetoothValue& properties = adaptersPropertiesArray[i].value(); + mManager->AppendAdapter(properties); + } + + aValue.setUndefined(); + return true; + } + + void + ReleaseMembers() + { + BluetoothReplyRunnable::ReleaseMembers(); + mManager = nullptr; + } + +private: + nsRefPtr mManager; +}; + BluetoothManager::BluetoothManager(nsPIDOMWindow *aWindow) : DOMEventTargetHelper(aWindow) , mDefaultAdapterIndex(-1) @@ -37,6 +94,13 @@ BluetoothManager::BluetoothManager(nsPIDOMWindow *aWindow) MOZ_ASSERT(IsDOMBinding()); ListenToBluetoothSignal(true); + + // Query adapters list from bluetooth backend + BluetoothService* bs = BluetoothService::Get(); + NS_ENSURE_TRUE_VOID(bs); + + nsRefPtr result = new GetAdaptersTask(this); + NS_ENSURE_SUCCESS_VOID(bs->GetAdaptersInternal(result)); } BluetoothManager::~BluetoothManager() diff --git a/dom/bluetooth2/BluetoothReplyRunnable.cpp b/dom/bluetooth2/BluetoothReplyRunnable.cpp index a8fe77632a98..772cb338c195 100644 --- a/dom/bluetooth2/BluetoothReplyRunnable.cpp +++ b/dom/bluetooth2/BluetoothReplyRunnable.cpp @@ -34,6 +34,8 @@ BluetoothReplyRunnable::~BluetoothReplyRunnable() nsresult BluetoothReplyRunnable::FireReply(JS::Handle aVal) { + NS_ENSURE_TRUE(mDOMRequest, NS_OK); + nsCOMPtr rs = do_GetService(DOMREQUEST_SERVICE_CONTRACTID); NS_ENSURE_TRUE(rs, NS_ERROR_FAILURE); @@ -46,6 +48,8 @@ BluetoothReplyRunnable::FireReply(JS::Handle aVal) nsresult BluetoothReplyRunnable::FireErrorString() { + NS_ENSURE_TRUE(mDOMRequest, NS_OK); + nsCOMPtr rs = do_GetService("@mozilla.org/dom/dom-request-service;1"); NS_ENSURE_TRUE(rs, NS_ERROR_FAILURE); @@ -57,7 +61,6 @@ NS_IMETHODIMP BluetoothReplyRunnable::Run() { MOZ_ASSERT(NS_IsMainThread()); - MOZ_ASSERT(mDOMRequest); MOZ_ASSERT(mReply); nsresult rv; diff --git a/dom/bluetooth2/BluetoothReplyRunnable.h b/dom/bluetooth2/BluetoothReplyRunnable.h index 244b9bb5da3b..6f47d5c54df3 100644 --- a/dom/bluetooth2/BluetoothReplyRunnable.h +++ b/dom/bluetooth2/BluetoothReplyRunnable.h @@ -48,6 +48,10 @@ private: nsresult FireReply(JS::Handle aVal); nsresult FireErrorString(); + /** + * mDOMRequest is nullptr for internal IPC that require no DOMRequest, + * e.g., GetAdaptersTask triggered by BluetoothManager + */ nsCOMPtr mDOMRequest; nsString mErrorString; }; diff --git a/dom/bluetooth2/BluetoothService.cpp b/dom/bluetooth2/BluetoothService.cpp index 6155de6d7f99..35cc8f2dc79d 100644 --- a/dom/bluetooth2/BluetoothService.cpp +++ b/dom/bluetooth2/BluetoothService.cpp @@ -375,11 +375,8 @@ BluetoothService::DistributeSignal(const BluetoothSignal& aSignal) BluetoothSignalObserverList* ol; if (!mBluetoothSignalObserverTable.Get(aSignal.path(), &ol)) { -#if DEBUG - nsAutoCString msg("No observer registered for path "); - msg.Append(NS_ConvertUTF16toUTF8(aSignal.path())); - BT_WARNING(msg.get()); -#endif + BT_WARNING("No observer registered for path %s", + NS_ConvertUTF16toUTF8(aSignal.path()).get()); return; } MOZ_ASSERT(ol->Length()); diff --git a/dom/bluetooth2/BluetoothService.h b/dom/bluetooth2/BluetoothService.h index 4800bf904c83..04fc0c98ca91 100644 --- a/dom/bluetooth2/BluetoothService.h +++ b/dom/bluetooth2/BluetoothService.h @@ -123,13 +123,13 @@ public: } /** - * Returns the path of the default adapter, implemented via a platform + * Returns an array of each adapter's properties, implemented via a platform * specific method. * * @return NS_OK on success, NS_ERROR_FAILURE otherwise */ virtual nsresult - GetDefaultAdapterPathInternal(BluetoothReplyRunnable* aRunnable) = 0; + GetAdaptersInternal(BluetoothReplyRunnable* aRunnable) = 0; /** * Returns the properties of paired devices, implemented via a platform diff --git a/dom/bluetooth2/bluedroid/BluetoothServiceBluedroid.cpp b/dom/bluetooth2/bluedroid/BluetoothServiceBluedroid.cpp index 6b09ba57ad89..4d6b8b26d93b 100644 --- a/dom/bluetooth2/bluedroid/BluetoothServiceBluedroid.cpp +++ b/dom/bluetooth2/bluedroid/BluetoothServiceBluedroid.cpp @@ -808,30 +808,45 @@ BluetoothServiceBluedroid::StopInternal() } nsresult -BluetoothServiceBluedroid::GetDefaultAdapterPathInternal( +BluetoothServiceBluedroid::GetAdaptersInternal( BluetoothReplyRunnable* aRunnable) { MOZ_ASSERT(NS_IsMainThread()); - BluetoothValue v = InfallibleTArray(); + /** + * Wrap BluetoothValue = + * BluetoothNamedValue[] + * | + * |__ BluetoothNamedValue = + * | {"Adapter", BluetoothValue = BluetoothNamedValue[]} + * | + * |__ BluetoothNamedValue = + * | {"Adapter", BluetoothValue = BluetoothNamedValue[]} + * ... + */ + BluetoothValue adaptersProperties = InfallibleTArray(); + uint32_t numAdapters = 1; // Bluedroid supports single adapter only - BT_APPEND_NAMED_VALUE(v.get_ArrayOfBluetoothNamedValue(), - "Address", sAdapterBdAddress); + for (uint32_t i = 0; i < numAdapters; i++) { + BluetoothValue properties = InfallibleTArray(); - BT_APPEND_NAMED_VALUE(v.get_ArrayOfBluetoothNamedValue(), - "Name", sAdapterBdName); + // TODO: Revise here based on new BluetoothAdapter interface + 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", sAdapterDiscoverable); + BT_APPEND_NAMED_VALUE(properties.get_ArrayOfBluetoothNamedValue(), + "DiscoverableTimeout", sAdapterDiscoverableTimeout); + BT_APPEND_NAMED_VALUE(properties.get_ArrayOfBluetoothNamedValue(), + "Devices", sAdapterBondedAddressArray); - BT_APPEND_NAMED_VALUE(v.get_ArrayOfBluetoothNamedValue(), - "Discoverable", sAdapterDiscoverable); - - BT_APPEND_NAMED_VALUE(v.get_ArrayOfBluetoothNamedValue(), - "DiscoverableTimeout", sAdapterDiscoverableTimeout); - - BT_APPEND_NAMED_VALUE(v.get_ArrayOfBluetoothNamedValue(), - "Devices", sAdapterBondedAddressArray); - - DispatchBluetoothReply(aRunnable, v, EmptyString()); + BT_APPEND_NAMED_VALUE(adaptersProperties.get_ArrayOfBluetoothNamedValue(), + "Adapter", properties); + } + DispatchBluetoothReply(aRunnable, adaptersProperties, EmptyString()); return NS_OK; } diff --git a/dom/bluetooth2/bluedroid/BluetoothServiceBluedroid.h b/dom/bluetooth2/bluedroid/BluetoothServiceBluedroid.h index c00c3199152d..ea66b3f76e74 100644 --- a/dom/bluetooth2/bluedroid/BluetoothServiceBluedroid.h +++ b/dom/bluetooth2/bluedroid/BluetoothServiceBluedroid.h @@ -25,15 +25,16 @@ public: virtual nsresult StartInternal(); virtual nsresult StopInternal(); - virtual nsresult GetDefaultAdapterPathInternal( - BluetoothReplyRunnable* aRunnable); + virtual nsresult + GetAdaptersInternal(BluetoothReplyRunnable* aRunnable); - virtual nsresult GetConnectedDevicePropertiesInternal(uint16_t aProfileId, - BluetoothReplyRunnable* aRunnable); + virtual nsresult + GetConnectedDevicePropertiesInternal(uint16_t aProfileId, + BluetoothReplyRunnable* aRunnable); - virtual nsresult GetPairedDevicePropertiesInternal( - const nsTArray& aDeviceAddress, - BluetoothReplyRunnable* aRunnable); + virtual nsresult + GetPairedDevicePropertiesInternal(const nsTArray& aDeviceAddress, + BluetoothReplyRunnable* aRunnable); virtual nsresult StartDiscoveryInternal(BluetoothReplyRunnable* aRunnable); virtual nsresult StopDiscoveryInternal(BluetoothReplyRunnable* aRunnable); diff --git a/dom/bluetooth2/bluez/BluetoothDBusService.cpp b/dom/bluetooth2/bluez/BluetoothDBusService.cpp index 44680cba3926..0de8fa2e63d8 100644 --- a/dom/bluetooth2/bluez/BluetoothDBusService.cpp +++ b/dom/bluetooth2/bluez/BluetoothDBusService.cpp @@ -2376,11 +2376,14 @@ private: }; nsresult -BluetoothDBusService::GetDefaultAdapterPathInternal( - BluetoothReplyRunnable* aRunnable) +BluetoothDBusService::GetAdaptersInternal(BluetoothReplyRunnable* aRunnable) { MOZ_ASSERT(NS_IsMainThread()); + /** + * TODO: implement method GetAdaptersInternal for bluez + */ + if (!IsReady()) { NS_NAMED_LITERAL_STRING(errorStr, "Bluetooth service is not ready yet!"); DispatchBluetoothReply(aRunnable, BluetoothValue(), errorStr); diff --git a/dom/bluetooth2/bluez/BluetoothDBusService.h b/dom/bluetooth2/bluez/BluetoothDBusService.h index cfb90ceea9db..6acbebf7e5f5 100644 --- a/dom/bluetooth2/bluez/BluetoothDBusService.h +++ b/dom/bluetooth2/bluez/BluetoothDBusService.h @@ -51,15 +51,16 @@ public: virtual nsresult StopInternal() MOZ_OVERRIDE; - virtual nsresult GetDefaultAdapterPathInternal( - BluetoothReplyRunnable* aRunnable) MOZ_OVERRIDE; + virtual nsresult + GetAdaptersInternal(BluetoothReplyRunnable* aRunnable) MOZ_OVERRIDE; - virtual nsresult GetConnectedDevicePropertiesInternal(uint16_t aServiceUuid, - BluetoothReplyRunnable* aRunnable) MOZ_OVERRIDE; + virtual nsresult + GetConnectedDevicePropertiesInternal(uint16_t aServiceUuid, + BluetoothReplyRunnable* aRunnable) MOZ_OVERRIDE; - virtual nsresult GetPairedDevicePropertiesInternal( - const nsTArray& aDeviceAddresses, - BluetoothReplyRunnable* aRunnable) MOZ_OVERRIDE; + virtual nsresult + GetPairedDevicePropertiesInternal(const nsTArray& aDeviceAddresses, + BluetoothReplyRunnable* aRunnable) MOZ_OVERRIDE; virtual nsresult StartDiscoveryInternal(BluetoothReplyRunnable* aRunnable) MOZ_OVERRIDE; diff --git a/dom/bluetooth2/ipc/BluetoothParent.cpp b/dom/bluetooth2/ipc/BluetoothParent.cpp index 19caf20084fe..b91e43f4d910 100644 --- a/dom/bluetooth2/ipc/BluetoothParent.cpp +++ b/dom/bluetooth2/ipc/BluetoothParent.cpp @@ -183,8 +183,8 @@ BluetoothParent::RecvPBluetoothRequestConstructor( #endif switch (aRequest.type()) { - case Request::TDefaultAdapterPathRequest: - return actor->DoRequest(aRequest.get_DefaultAdapterPathRequest()); + case Request::TGetAdaptersRequest: + return actor->DoRequest(aRequest.get_GetAdaptersRequest()); case Request::TSetPropertyRequest: return actor->DoRequest(aRequest.get_SetPropertyRequest()); case Request::TStartDiscoveryRequest: @@ -303,12 +303,12 @@ BluetoothRequestParent::RequestComplete() } bool -BluetoothRequestParent::DoRequest(const DefaultAdapterPathRequest& aRequest) +BluetoothRequestParent::DoRequest(const GetAdaptersRequest& aRequest) { MOZ_ASSERT(mService); - MOZ_ASSERT(mRequestType == Request::TDefaultAdapterPathRequest); + MOZ_ASSERT(mRequestType == Request::TGetAdaptersRequest); - nsresult rv = mService->GetDefaultAdapterPathInternal(mReplyRunnable.get()); + nsresult rv = mService->GetAdaptersInternal(mReplyRunnable.get()); NS_ENSURE_SUCCESS(rv, false); return true; diff --git a/dom/bluetooth2/ipc/BluetoothParent.h b/dom/bluetooth2/ipc/BluetoothParent.h index d5ee4a391782..fcf022ce57b4 100644 --- a/dom/bluetooth2/ipc/BluetoothParent.h +++ b/dom/bluetooth2/ipc/BluetoothParent.h @@ -126,7 +126,7 @@ protected: RequestComplete(); bool - DoRequest(const DefaultAdapterPathRequest& aRequest); + DoRequest(const GetAdaptersRequest& aRequest); bool DoRequest(const SetPropertyRequest& aRequest); diff --git a/dom/bluetooth2/ipc/BluetoothServiceChildProcess.cpp b/dom/bluetooth2/ipc/BluetoothServiceChildProcess.cpp index e2ea06eb7777..3f5a33260a23 100644 --- a/dom/bluetooth2/ipc/BluetoothServiceChildProcess.cpp +++ b/dom/bluetooth2/ipc/BluetoothServiceChildProcess.cpp @@ -96,10 +96,10 @@ BluetoothServiceChildProcess::UnregisterBluetoothSignalHandler( } nsresult -BluetoothServiceChildProcess::GetDefaultAdapterPathInternal( +BluetoothServiceChildProcess::GetAdaptersInternal( BluetoothReplyRunnable* aRunnable) { - SendRequest(aRunnable, DefaultAdapterPathRequest()); + SendRequest(aRunnable, GetAdaptersRequest()); return NS_OK; } diff --git a/dom/bluetooth2/ipc/BluetoothServiceChildProcess.h b/dom/bluetooth2/ipc/BluetoothServiceChildProcess.h index 13c872214ace..24e4279f2cba 100644 --- a/dom/bluetooth2/ipc/BluetoothServiceChildProcess.h +++ b/dom/bluetooth2/ipc/BluetoothServiceChildProcess.h @@ -44,7 +44,7 @@ public: MOZ_OVERRIDE; virtual nsresult - GetDefaultAdapterPathInternal(BluetoothReplyRunnable* aRunnable) MOZ_OVERRIDE; + GetAdaptersInternal(BluetoothReplyRunnable* aRunnable) MOZ_OVERRIDE; virtual nsresult GetPairedDevicePropertiesInternal(const nsTArray& aDeviceAddresses, diff --git a/dom/bluetooth2/ipc/PBluetooth.ipdl b/dom/bluetooth2/ipc/PBluetooth.ipdl index e6ae3f72d803..1d2b0cdeca37 100644 --- a/dom/bluetooth2/ipc/PBluetooth.ipdl +++ b/dom/bluetooth2/ipc/PBluetooth.ipdl @@ -22,7 +22,7 @@ namespace bluetooth { * Bluetooth request types. */ -struct DefaultAdapterPathRequest +struct GetAdaptersRequest { }; struct SetPropertyRequest @@ -165,7 +165,7 @@ struct SendPlayStatusRequest union Request { - DefaultAdapterPathRequest; + GetAdaptersRequest; SetPropertyRequest; GetPropertyRequest; StartDiscoveryRequest;