Bug 1006309 - Patch 3/4: Query adapters list from bluetooth backend, r=echou

This commit is contained in:
Ben Tian 2014-05-29 17:16:20 +08:00
Родитель 21579063d9
Коммит 0402aa3fd0
14 изменённых файлов: 139 добавлений и 51 удалений

Просмотреть файл

@ -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<JS::Value> 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<BluetoothNamedValue>& 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<BluetoothManager> 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<BluetoothReplyRunnable> result = new GetAdaptersTask(this);
NS_ENSURE_SUCCESS_VOID(bs->GetAdaptersInternal(result));
}
BluetoothManager::~BluetoothManager()

Просмотреть файл

@ -34,6 +34,8 @@ BluetoothReplyRunnable::~BluetoothReplyRunnable()
nsresult
BluetoothReplyRunnable::FireReply(JS::Handle<JS::Value> aVal)
{
NS_ENSURE_TRUE(mDOMRequest, NS_OK);
nsCOMPtr<nsIDOMRequestService> rs =
do_GetService(DOMREQUEST_SERVICE_CONTRACTID);
NS_ENSURE_TRUE(rs, NS_ERROR_FAILURE);
@ -46,6 +48,8 @@ BluetoothReplyRunnable::FireReply(JS::Handle<JS::Value> aVal)
nsresult
BluetoothReplyRunnable::FireErrorString()
{
NS_ENSURE_TRUE(mDOMRequest, NS_OK);
nsCOMPtr<nsIDOMRequestService> 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;

Просмотреть файл

@ -48,6 +48,10 @@ private:
nsresult FireReply(JS::Handle<JS::Value> aVal);
nsresult FireErrorString();
/**
* mDOMRequest is nullptr for internal IPC that require no DOMRequest,
* e.g., GetAdaptersTask triggered by BluetoothManager
*/
nsCOMPtr<nsIDOMDOMRequest> mDOMRequest;
nsString mErrorString;
};

Просмотреть файл

@ -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());

Просмотреть файл

@ -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

Просмотреть файл

@ -808,30 +808,45 @@ BluetoothServiceBluedroid::StopInternal()
}
nsresult
BluetoothServiceBluedroid::GetDefaultAdapterPathInternal(
BluetoothServiceBluedroid::GetAdaptersInternal(
BluetoothReplyRunnable* aRunnable)
{
MOZ_ASSERT(NS_IsMainThread());
BluetoothValue v = InfallibleTArray<BluetoothNamedValue>();
/**
* Wrap BluetoothValue =
* BluetoothNamedValue[]
* |
* |__ BluetoothNamedValue =
* | {"Adapter", BluetoothValue = BluetoothNamedValue[]}
* |
* |__ BluetoothNamedValue =
* | {"Adapter", BluetoothValue = BluetoothNamedValue[]}
* ...
*/
BluetoothValue adaptersProperties = InfallibleTArray<BluetoothNamedValue>();
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<BluetoothNamedValue>();
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;
}

Просмотреть файл

@ -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<nsString>& aDeviceAddress,
BluetoothReplyRunnable* aRunnable);
virtual nsresult
GetPairedDevicePropertiesInternal(const nsTArray<nsString>& aDeviceAddress,
BluetoothReplyRunnable* aRunnable);
virtual nsresult StartDiscoveryInternal(BluetoothReplyRunnable* aRunnable);
virtual nsresult StopDiscoveryInternal(BluetoothReplyRunnable* aRunnable);

Просмотреть файл

@ -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);

Просмотреть файл

@ -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<nsString>& aDeviceAddresses,
BluetoothReplyRunnable* aRunnable) MOZ_OVERRIDE;
virtual nsresult
GetPairedDevicePropertiesInternal(const nsTArray<nsString>& aDeviceAddresses,
BluetoothReplyRunnable* aRunnable) MOZ_OVERRIDE;
virtual nsresult StartDiscoveryInternal(BluetoothReplyRunnable* aRunnable) MOZ_OVERRIDE;

Просмотреть файл

@ -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;

Просмотреть файл

@ -126,7 +126,7 @@ protected:
RequestComplete();
bool
DoRequest(const DefaultAdapterPathRequest& aRequest);
DoRequest(const GetAdaptersRequest& aRequest);
bool
DoRequest(const SetPropertyRequest& aRequest);

Просмотреть файл

@ -96,10 +96,10 @@ BluetoothServiceChildProcess::UnregisterBluetoothSignalHandler(
}
nsresult
BluetoothServiceChildProcess::GetDefaultAdapterPathInternal(
BluetoothServiceChildProcess::GetAdaptersInternal(
BluetoothReplyRunnable* aRunnable)
{
SendRequest(aRunnable, DefaultAdapterPathRequest());
SendRequest(aRunnable, GetAdaptersRequest());
return NS_OK;
}

Просмотреть файл

@ -44,7 +44,7 @@ public:
MOZ_OVERRIDE;
virtual nsresult
GetDefaultAdapterPathInternal(BluetoothReplyRunnable* aRunnable) MOZ_OVERRIDE;
GetAdaptersInternal(BluetoothReplyRunnable* aRunnable) MOZ_OVERRIDE;
virtual nsresult
GetPairedDevicePropertiesInternal(const nsTArray<nsString>& aDeviceAddresses,

Просмотреть файл

@ -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;