Bug 1133655 - Replace |DispatchBluetoothReply| with |DispatchReplySuccess| and |DispatchReplyError|, f=jocelyn, f=jaliu, r=shuang

This commit is contained in:
Ben Tian 2015-03-03 13:24:49 +08:00
Родитель a5e5616a71
Коммит 0dc9c10b21
8 изменённых файлов: 197 добавлений и 234 удалений

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

@ -115,8 +115,7 @@ BluetoothProfileController::AddProfileWithServiceClass(
profile = BluetoothHidManager::Get();
break;
default:
DispatchBluetoothReply(mRunnable, BluetoothValue(),
NS_LITERAL_STRING(ERR_UNKNOWN_PROFILE));
DispatchReplyError(mRunnable, NS_LITERAL_STRING(ERR_UNKNOWN_PROFILE));
mCallback();
return;
}
@ -129,8 +128,7 @@ BluetoothProfileController::AddProfile(BluetoothProfileManagerBase* aProfile,
bool aCheckConnected)
{
if (!aProfile) {
DispatchBluetoothReply(mRunnable, BluetoothValue(),
NS_LITERAL_STRING(ERR_NO_AVAILABLE_RESOURCE));
DispatchReplyError(mRunnable, NS_LITERAL_STRING(ERR_NO_AVAILABLE_RESOURCE));
mCallback();
return;
}
@ -255,13 +253,11 @@ BluetoothProfileController::EndSession()
// The action has completed, so the DOM request should be replied then invoke
// the callback.
if (mSuccess) {
DispatchBluetoothReply(mRunnable, BluetoothValue(true), EmptyString());
DispatchReplySuccess(mRunnable);
} else if (mConnect) {
DispatchBluetoothReply(mRunnable, BluetoothValue(true),
NS_LITERAL_STRING(ERR_CONNECTION_FAILED));
DispatchReplyError(mRunnable, NS_LITERAL_STRING(ERR_CONNECTION_FAILED));
} else {
DispatchBluetoothReply(mRunnable, BluetoothValue(true),
NS_LITERAL_STRING(ERR_DISCONNECTION_FAILED));
DispatchReplyError(mRunnable, NS_LITERAL_STRING(ERR_DISCONNECTION_FAILED));
}
mCallback();

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

@ -64,6 +64,9 @@ StringToUuid(const char* aString, BluetoothUuid& aUuid)
memcpy(&aUuid.mUuid[14], &uuid5, sizeof(uint16_t));
}
/**
* |SetJsObject| is an internal function used by |BroadcastSystemMessage| only
*/
bool
SetJsObject(JSContext* aContext,
const BluetoothValue& aValue,
@ -119,7 +122,7 @@ BroadcastSystemMessage(const nsAString& aType,
const BluetoothValue& aData)
{
mozilla::AutoSafeJSContext cx;
NS_ASSERTION(!::JS_IsExceptionPending(cx),
MOZ_ASSERT(!::JS_IsExceptionPending(cx),
"Shouldn't get here when an exception is pending!");
nsCOMPtr<nsISystemMessagesInternal> systemMessenger =
@ -160,7 +163,7 @@ BroadcastSystemMessage(const nsAString& aType,
const InfallibleTArray<BluetoothNamedValue>& aData)
{
mozilla::AutoSafeJSContext cx;
NS_ASSERTION(!::JS_IsExceptionPending(cx),
MOZ_ASSERT(!::JS_IsExceptionPending(cx),
"Shouldn't get here when an exception is pending!");
JS::Rooted<JSObject*> obj(cx, JS_NewPlainObject(cx));
@ -186,44 +189,50 @@ BroadcastSystemMessage(const nsAString& aType,
}
void
DispatchBluetoothReply(BluetoothReplyRunnable* aRunnable,
const BluetoothValue& aValue,
const nsAString& aErrorStr)
DispatchReplySuccess(BluetoothReplyRunnable* aRunnable)
{
// Reply will be deleted by the runnable after running on main thread
BluetoothReply* reply;
if (!aErrorStr.IsEmpty()) {
nsString err(aErrorStr);
reply = new BluetoothReply(BluetoothReplyError(STATUS_FAIL, err));
} else {
MOZ_ASSERT(aValue.type() != BluetoothValue::T__None);
reply = new BluetoothReply(BluetoothReplySuccess(aValue));
}
aRunnable->SetReply(reply);
if (NS_FAILED(NS_DispatchToMainThread(aRunnable))) {
BT_WARNING("Failed to dispatch to main thread!");
}
DispatchReplySuccess(aRunnable, BluetoothValue(true));
}
void
DispatchBluetoothReply(BluetoothReplyRunnable* aRunnable,
const BluetoothValue& aValue,
const enum BluetoothStatus aStatusCode)
DispatchReplySuccess(BluetoothReplyRunnable* aRunnable,
const BluetoothValue& aValue)
{
// Reply will be deleted by the runnable after running on main thread
BluetoothReply* reply;
if (aStatusCode != STATUS_SUCCESS) {
reply = new BluetoothReply(BluetoothReplyError(aStatusCode, EmptyString()));
} else {
MOZ_ASSERT(aValue.type() != BluetoothValue::T__None);
reply = new BluetoothReply(BluetoothReplySuccess(aValue));
}
MOZ_ASSERT(aRunnable);
MOZ_ASSERT(aValue.type() != BluetoothValue::T__None);
aRunnable->SetReply(reply);
if (NS_FAILED(NS_DispatchToMainThread(aRunnable))) {
BT_WARNING("Failed to dispatch to main thread!");
}
BluetoothReply* reply = new BluetoothReply(BluetoothReplySuccess(aValue));
aRunnable->SetReply(reply); // runnable will delete reply after Run()
NS_WARN_IF(NS_FAILED(NS_DispatchToMainThread(aRunnable)));
}
void
DispatchReplyError(BluetoothReplyRunnable* aRunnable,
const nsAString& aErrorStr)
{
MOZ_ASSERT(aRunnable);
MOZ_ASSERT(!aErrorStr.IsEmpty());
BluetoothReply* reply =
new BluetoothReply(BluetoothReplyError(STATUS_FAIL, nsString(aErrorStr)));
aRunnable->SetReply(reply); // runnable will delete reply after Run()
NS_WARN_IF(NS_FAILED(NS_DispatchToMainThread(aRunnable)));
}
void
DispatchReplyError(BluetoothReplyRunnable* aRunnable,
const enum BluetoothStatus aStatus)
{
MOZ_ASSERT(aRunnable);
MOZ_ASSERT(aStatus != STATUS_SUCCESS);
BluetoothReply* reply =
new BluetoothReply(BluetoothReplyError(aStatus, EmptyString()));
aRunnable->SetReply(reply); // runnable will delete reply after Run()
NS_WARN_IF(NS_FAILED(NS_DispatchToMainThread(aRunnable)));
}
void

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

@ -13,9 +13,18 @@
BEGIN_BLUETOOTH_NAMESPACE
class BluetoothNamedValue;
class BluetoothValue;
class BluetoothReplyRunnable;
class BluetoothValue;
//
// BluetoothUuid <-> uuid string conversion
//
/**
* Convert BluetoothUuid object to xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxx uuid string.
* This utility function is used by gecko internal only to convert BluetoothUuid
* created by bluetooth stack to uuid string representation.
*/
void
UuidToString(const BluetoothUuid& aUuid, nsAString& aString);
@ -27,10 +36,9 @@ UuidToString(const BluetoothUuid& aUuid, nsAString& aString);
void
StringToUuid(const char* aString, BluetoothUuid& aUuid);
bool
SetJsObject(JSContext* aContext,
const BluetoothValue& aValue,
JS::Handle<JSObject*> aObj);
//
// Broadcast system message
//
bool
BroadcastSystemMessage(const nsAString& aType,
@ -40,41 +48,59 @@ bool
BroadcastSystemMessage(const nsAString& aType,
const InfallibleTArray<BluetoothNamedValue>& aData);
//
// Dispatch bluetooth reply to main thread
//
/**
* Dispatch BluetoothReply to main thread. The reply contains an error string
* if the request fails.
* Dispatch successful bluetooth reply with NO value to reply request.
*
* @param aRunnable the runnable to reply bluetooth request.
*/
void
DispatchReplySuccess(BluetoothReplyRunnable* aRunnable);
/**
* Dispatch successful bluetooth reply with value to reply request.
*
* @param aRunnable the runnable to reply bluetooth request.
* @param aValue the BluetoothValue to reply successful request.
*/
void
DispatchReplySuccess(BluetoothReplyRunnable* aRunnable,
const BluetoothValue& aValue);
/**
* Dispatch failed bluetooth reply with error string.
*
* This function is for methods returning DOMRequest. If |aErrorStr| is not
* empty, the DOMRequest property 'error.name' would be updated to |aErrorStr|
* before callback function 'onerror' is fired.
*
* NOTE: For methods returning Promise, |aErrorStr| would be ignored and only
* STATUS_FAIL is returned in BluetoothReplyRunnable.
*
* @param aRunnable the runnable to reply bluetooth request.
* @param aValue the BluetoothValue used to reply successful request.
* @param aErrorStr the error string used to reply failed request.
* @param aErrorStr the error string to reply failed request.
*/
void
DispatchBluetoothReply(BluetoothReplyRunnable* aRunnable,
const BluetoothValue& aValue,
const nsAString& aErrorStr);
DispatchReplyError(BluetoothReplyRunnable* aRunnable,
const nsAString& aErrorStr);
/**
* Dispatch BluetoothReply to main thread. The reply contains an error string
* if the request fails.
* Dispatch failed bluetooth reply with error status.
*
* This function is for methods returning Promise. If |aStatusCode| is not
* STATUS_SUCCESS, the Promise would reject with an Exception object with
* nsError associated with |aStatusCode|. The name and messege of Exception
* (defined in dom/base/domerr.msg) are filled automatically during promise
* rejection.
* This function is for methods returning Promise. The Promise would reject
* with an Exception object that carries nsError associated with |aStatus|.
* The name and messege of Exception (defined in dom/base/domerr.msg) are
* filled automatically during promise rejection.
*
* @param aRunnable the runnable to reply bluetooth request.
* @param aValue the BluetoothValue to reply successful request.
* @param aStatusCode the error status to reply failed request.
* @param aRunnable the runnable to reply bluetooth request.
* @param aStatus the error status to reply failed request.
*/
void
DispatchBluetoothReply(BluetoothReplyRunnable* aRunnable,
const BluetoothValue& aValue,
const enum BluetoothStatus aStatusCode);
DispatchReplyError(BluetoothReplyRunnable* aRunnable,
const enum BluetoothStatus aStatus);
void
DispatchStatusChangedEvent(const nsAString& aType,

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

@ -20,9 +20,8 @@
#define ENSURE_GATT_CLIENT_INTF_IS_READY_VOID(runnable) \
do { \
if (!sBluetoothGattInterface) { \
NS_NAMED_LITERAL_STRING(errorStr, \
"BluetoothGattClientInterface is not ready"); \
DispatchBluetoothReply(runnable, BluetoothValue(), errorStr); \
DispatchReplyError(runnable, \
NS_LITERAL_STRING("BluetoothGattClientInterface is not ready")); \
return; \
} \
} while(0)
@ -270,10 +269,8 @@ public:
// Reject the connect request
if (mClient->mConnectRunnable) {
NS_NAMED_LITERAL_STRING(errorStr, "Register GATT client failed");
DispatchBluetoothReply(mClient->mConnectRunnable,
BluetoothValue(),
errorStr);
DispatchReplyError(mClient->mConnectRunnable,
NS_LITERAL_STRING("Register GATT client failed"));
mClient->mConnectRunnable = nullptr;
}
@ -308,9 +305,7 @@ public:
bs->DistributeSignal(signal);
// Resolve the unregister request
DispatchBluetoothReply(mClient->mUnregisterClientRunnable,
BluetoothValue(true),
EmptyString());
DispatchReplySuccess(mClient->mUnregisterClientRunnable);
mClient->mUnregisterClientRunnable = nullptr;
sClients->RemoveElement(mClient);
@ -323,10 +318,8 @@ public:
MOZ_ASSERT(mClient->mUnregisterClientRunnable);
// Reject the unregister request
NS_NAMED_LITERAL_STRING(errorStr, "Unregister GATT client failed");
DispatchBluetoothReply(mClient->mUnregisterClientRunnable,
BluetoothValue(),
errorStr);
DispatchReplyError(mClient->mUnregisterClientRunnable,
NS_LITERAL_STRING("Unregister GATT client failed"));
mClient->mUnregisterClientRunnable = nullptr;
}
@ -348,10 +341,8 @@ BluetoothGattManager::UnregisterClient(int aClientIf,
// Reject the unregister request if the client is not found
if (index == sClients->NoIndex) {
NS_NAMED_LITERAL_STRING(errorStr, "Unregister GATT client failed");
DispatchBluetoothReply(aRunnable,
BluetoothValue(),
errorStr);
DispatchReplyError(aRunnable,
NS_LITERAL_STRING("Unregister GATT client failed"));
return;
}
@ -390,10 +381,8 @@ public:
bs->DistributeSignal(signal);
// Reject the connect request
NS_NAMED_LITERAL_STRING(errorStr, "Connect failed");
DispatchBluetoothReply(mClient->mConnectRunnable,
BluetoothValue(),
errorStr);
DispatchReplyError(mClient->mConnectRunnable,
NS_LITERAL_STRING("Connect failed"));
mClient->mConnectRunnable = nullptr;
}
@ -462,10 +451,8 @@ public:
bs->DistributeSignal(signal);
// Reject the disconnect request
NS_NAMED_LITERAL_STRING(errorStr, "Disconnect failed");
DispatchBluetoothReply(mClient->mDisconnectRunnable,
BluetoothValue(),
errorStr);
DispatchReplyError(mClient->mDisconnectRunnable,
NS_LITERAL_STRING("Disconnect failed"));
mClient->mDisconnectRunnable = nullptr;
}
@ -487,8 +474,7 @@ BluetoothGattManager::Disconnect(const nsAString& aAppUuid,
// Reject the disconnect request if the client is not found
if (index == sClients->NoIndex) {
NS_NAMED_LITERAL_STRING(errorStr, "Disconnect failed");
DispatchBluetoothReply(aRunnable, BluetoothValue(), errorStr);
DispatchReplyError(aRunnable, NS_LITERAL_STRING("Disconnect failed"));
return;
}
@ -536,11 +522,9 @@ BluetoothGattManager::RegisterClientNotification(int aStatus,
// Reject the connect request
if (client->mConnectRunnable) {
NS_NAMED_LITERAL_STRING(errorStr,
"Connect failed due to registration failed");
DispatchBluetoothReply(client->mConnectRunnable,
BluetoothValue(),
errorStr);
DispatchReplyError(client->mConnectRunnable,
NS_LITERAL_STRING(
"Connect failed due to registration failed"));
client->mConnectRunnable = nullptr;
}
@ -600,10 +584,8 @@ BluetoothGattManager::ConnectNotification(int aConnId,
// Reject the connect request
if (client->mConnectRunnable) {
NS_NAMED_LITERAL_STRING(errorStr, "Connect failed");
DispatchBluetoothReply(client->mConnectRunnable,
BluetoothValue(),
errorStr);
DispatchReplyError(client->mConnectRunnable,
NS_LITERAL_STRING("Connect failed"));
client->mConnectRunnable = nullptr;
}
@ -621,9 +603,7 @@ BluetoothGattManager::ConnectNotification(int aConnId,
// Resolve the connect request
if (client->mConnectRunnable) {
DispatchBluetoothReply(client->mConnectRunnable,
BluetoothValue(true),
EmptyString());
DispatchReplySuccess(client->mConnectRunnable);
client->mConnectRunnable = nullptr;
}
}
@ -655,10 +635,8 @@ BluetoothGattManager::DisconnectNotification(int aConnId,
// Reject the disconnect request
if (client->mDisconnectRunnable) {
NS_NAMED_LITERAL_STRING(errorStr, "Disconnect failed");
DispatchBluetoothReply(client->mDisconnectRunnable,
BluetoothValue(),
errorStr);
DispatchReplyError(client->mDisconnectRunnable,
NS_LITERAL_STRING("Disconnect failed"));
client->mDisconnectRunnable = nullptr;
}
@ -676,9 +654,7 @@ BluetoothGattManager::DisconnectNotification(int aConnId,
// Resolve the disconnect request
if (client->mDisconnectRunnable) {
DispatchBluetoothReply(client->mDisconnectRunnable,
BluetoothValue(true),
EmptyString());
DispatchReplySuccess(client->mDisconnectRunnable);
client->mDisconnectRunnable = nullptr;
}
}

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

@ -33,15 +33,11 @@
#include "mozilla/StaticPtr.h"
#include "mozilla/unused.h"
#define ERR_SET_PROPERTY "SetPropertyError"
#define ERR_START_BLUETOOTH "StartBluetoothError"
#define ERR_STOP_BLUETOOTH "StopBluetoothError"
#define ENSURE_BLUETOOTH_IS_READY(runnable, result) \
do { \
if (!sBtInterface || !IsEnabled()) { \
NS_NAMED_LITERAL_STRING(errorStr, "Bluetooth is not ready"); \
DispatchBluetoothReply(runnable, BluetoothValue(), errorStr); \
DispatchReplyError(runnable, \
NS_LITERAL_STRING("Bluetooth is not ready")); \
return result; \
} \
} while(0)
@ -49,8 +45,8 @@
#define ENSURE_BLUETOOTH_IS_READY_VOID(runnable) \
do { \
if (!sBtInterface || !IsEnabled()) { \
NS_NAMED_LITERAL_STRING(errorStr, "Bluetooth is not ready"); \
DispatchBluetoothReply(runnable, BluetoothValue(), errorStr); \
DispatchReplyError(runnable, \
NS_LITERAL_STRING("Bluetooth is not ready")); \
return; \
} \
} while(0)
@ -58,8 +54,8 @@
#define ENSURE_GATT_MGR_IS_READY_VOID(gatt, runnable) \
do { \
if (!gatt) { \
NS_NAMED_LITERAL_STRING(replyError, "GattManager is not ready"); \
DispatchBluetoothReply(runnable, BluetoothValue(), replyError); \
DispatchReplyError(runnable, \
NS_LITERAL_STRING("GattManager is not ready")); \
return; \
} \
} while(0)
@ -270,36 +266,6 @@ BluetoothServiceBluedroid::StopGonkBluetooth()
return NS_OK;
}
void
BluetoothServiceBluedroid::ReplyStatusError(
BluetoothReplyRunnable* aBluetoothReplyRunnable,
BluetoothStatus aStatusCode, const nsAString& aCustomMsg)
{
MOZ_ASSERT(aBluetoothReplyRunnable, "Reply runnable is nullptr");
BT_LOGR("error code(%d)", aStatusCode);
nsAutoString replyError;
replyError.Assign(aCustomMsg);
if (aStatusCode == STATUS_BUSY) {
replyError.AppendLiteral(":BT_STATUS_BUSY");
} else if (aStatusCode == STATUS_NOT_READY) {
replyError.AppendLiteral(":BT_STATUS_NOT_READY");
} else if (aStatusCode == STATUS_DONE) {
replyError.AppendLiteral(":BT_STATUS_DONE");
} else if (aStatusCode == STATUS_AUTH_FAILURE) {
replyError.AppendLiteral(":BT_STATUS_AUTH_FAILURE");
} else if (aStatusCode == STATUS_RMT_DEV_DOWN) {
replyError.AppendLiteral(":BT_STATUS_RMT_DEV_DOWN");
} else if (aStatusCode == STATUS_FAIL) {
replyError.AppendLiteral(":BT_STATUS_FAIL");
}
DispatchBluetoothReply(aBluetoothReplyRunnable, BluetoothValue(true),
replyError);
}
/**
* Member functions
*/
@ -331,8 +297,7 @@ BluetoothServiceBluedroid::StartInternal(BluetoothReplyRunnable* aRunnable)
// Reject Promise
if(aRunnable) {
DispatchBluetoothReply(aRunnable, BluetoothValue(),
NS_LITERAL_STRING(ERR_START_BLUETOOTH));
DispatchReplyError(aRunnable, NS_LITERAL_STRING("StartBluetoothError"));
sChangeAdapterStateRunnableArray.RemoveElement(aRunnable);
}
@ -389,8 +354,7 @@ BluetoothServiceBluedroid::StopInternal(BluetoothReplyRunnable* aRunnable)
// Reject Promise
if(aRunnable) {
DispatchBluetoothReply(aRunnable, BluetoothValue(),
NS_LITERAL_STRING(ERR_STOP_BLUETOOTH));
DispatchReplyError(aRunnable, NS_LITERAL_STRING("StopBluetoothError"));
sChangeAdapterStateRunnableArray.RemoveElement(aRunnable);
}
@ -440,7 +404,7 @@ BluetoothServiceBluedroid::GetAdaptersInternal(
"Adapter", properties);
}
DispatchBluetoothReply(aRunnable, adaptersProperties, EmptyString());
DispatchReplySuccess(aRunnable, adaptersProperties);
return NS_OK;
}
@ -463,8 +427,7 @@ public:
/* dispatch result after final pending operation */
if (--sRequestedDeviceCountArray[0] == 0) {
if (!sGetDeviceRunnableArray.IsEmpty()) {
DispatchBluetoothReply(
sGetDeviceRunnableArray[0], sRemoteDevicesPack,
DispatchReplyError(sGetDeviceRunnableArray[0],
NS_LITERAL_STRING("GetRemoteDeviceProperties failed"));
sGetDeviceRunnableArray.RemoveElementAt(0);
}
@ -489,9 +452,7 @@ BluetoothServiceBluedroid::GetConnectedDevicePropertiesInternal(
BluetoothProfileManagerBase* profile =
BluetoothUuidHelper::GetBluetoothProfileManager(aServiceUuid);
if (!profile) {
InfallibleTArray<BluetoothNamedValue> emptyArr;
DispatchBluetoothReply(aRunnable, emptyArr,
NS_LITERAL_STRING(ERR_UNKNOWN_PROFILE));
DispatchReplyError(aRunnable, NS_LITERAL_STRING(ERR_UNKNOWN_PROFILE));
return NS_OK;
}
@ -505,7 +466,7 @@ BluetoothServiceBluedroid::GetConnectedDevicePropertiesInternal(
int requestedDeviceCount = deviceAddresses.Length();
if (requestedDeviceCount == 0) {
InfallibleTArray<BluetoothNamedValue> emptyArr;
DispatchBluetoothReply(aRunnable, emptyArr, EmptyString());
DispatchReplySuccess(aRunnable, emptyArr);
return NS_OK;
}
@ -531,7 +492,7 @@ BluetoothServiceBluedroid::GetPairedDevicePropertiesInternal(
int requestedDeviceCount = aDeviceAddress.Length();
if (requestedDeviceCount == 0) {
DispatchBluetoothReply(aRunnable, BluetoothValue(true), EmptyString());
DispatchReplySuccess(aRunnable);
return NS_OK;
}
@ -556,7 +517,7 @@ public:
{
MOZ_ASSERT(NS_IsMainThread());
sChangeDiscoveryRunnableArray.RemoveElement(mRunnable);
ReplyStatusError(mRunnable, aStatus, NS_LITERAL_STRING("StartDiscovery"));
DispatchReplyError(mRunnable, aStatus);
}
private:
@ -589,7 +550,7 @@ public:
{
MOZ_ASSERT(NS_IsMainThread());
sChangeDiscoveryRunnableArray.RemoveElement(mRunnable);
ReplyStatusError(mRunnable, aStatus, NS_LITERAL_STRING("StopDiscovery"));
DispatchReplyError(mRunnable, aStatus);
}
private:
@ -622,7 +583,7 @@ public:
{
MOZ_ASSERT(NS_IsMainThread());
sFetchUuidsRunnableArray.RemoveElement(mRunnable);
ReplyStatusError(mRunnable, aStatus, NS_LITERAL_STRING("FetchUuids"));
DispatchReplyError(mRunnable, aStatus);
}
private:
@ -665,7 +626,7 @@ public:
{
MOZ_ASSERT(NS_IsMainThread());
sSetPropertyRunnableArray.RemoveElement(mRunnable);
ReplyStatusError(mRunnable, aStatus, NS_LITERAL_STRING("SetProperty"));
DispatchReplyError(mRunnable, aStatus);
}
private:
BluetoothReplyRunnable* mRunnable;
@ -718,7 +679,7 @@ public:
void OnError(BluetoothStatus aStatus) MOZ_OVERRIDE
{
sBondingRunnableArray.RemoveElement(mRunnable);
ReplyStatusError(mRunnable, aStatus, NS_LITERAL_STRING("CreatedPairedDevice"));
DispatchReplyError(mRunnable, aStatus);
}
private:
@ -754,7 +715,7 @@ public:
void OnError(BluetoothStatus aStatus) MOZ_OVERRIDE
{
sUnbondingRunnableArray.RemoveElement(mRunnable);
ReplyStatusError(mRunnable, aStatus, NS_LITERAL_STRING("RemoveDevice"));
DispatchReplyError(mRunnable, aStatus);
}
private:
@ -787,12 +748,12 @@ public:
void PinReply() MOZ_OVERRIDE
{
DispatchBluetoothReply(mRunnable, BluetoothValue(true), EmptyString());
DispatchReplySuccess(mRunnable);
}
void OnError(BluetoothStatus aStatus) MOZ_OVERRIDE
{
ReplyStatusError(mRunnable, aStatus, NS_LITERAL_STRING("SetPinCode"));
DispatchReplyError(mRunnable, aStatus);
}
private:
@ -830,13 +791,12 @@ public:
void SspReply() MOZ_OVERRIDE
{
DispatchBluetoothReply(mRunnable, BluetoothValue(true), EmptyString());
DispatchReplySuccess(mRunnable);
}
void OnError(BluetoothStatus aStatus) MOZ_OVERRIDE
{
ReplyStatusError(mRunnable, aStatus,
NS_LITERAL_STRING("SetPairingConfirmation"));
DispatchReplyError(mRunnable, aStatus);
}
private:
@ -933,12 +893,12 @@ BluetoothServiceBluedroid::SendFile(const nsAString& aDeviceAddress,
// has been determined when calling 'Connect()'. Nevertheless, keep
// it for future use.
BluetoothOppManager* opp = BluetoothOppManager::Get();
nsAutoString errorStr;
if (!opp || !opp->SendFile(aDeviceAddress, aBlobParent)) {
errorStr.AssignLiteral("Calling SendFile() failed");
DispatchReplyError(aRunnable, NS_LITERAL_STRING("SendFile failed"));
return;
}
DispatchBluetoothReply(aRunnable, BluetoothValue(true), errorStr);
DispatchReplySuccess(aRunnable);
}
void
@ -953,12 +913,12 @@ BluetoothServiceBluedroid::SendFile(const nsAString& aDeviceAddress,
// has been determined when calling 'Connect()'. Nevertheless, keep
// it for future use.
BluetoothOppManager* opp = BluetoothOppManager::Get();
nsAutoString errorStr;
if (!opp || !opp->SendFile(aDeviceAddress, aBlob)) {
errorStr.AssignLiteral("Calling SendFile() failed");
DispatchReplyError(aRunnable, NS_LITERAL_STRING("SendFile failed"));
return;
}
DispatchBluetoothReply(aRunnable, BluetoothValue(true), errorStr);
DispatchReplySuccess(aRunnable);
}
void
@ -974,10 +934,11 @@ BluetoothServiceBluedroid::StopSendingFile(const nsAString& aDeviceAddress,
BluetoothOppManager* opp = BluetoothOppManager::Get();
nsAutoString errorStr;
if (!opp || !opp->StopSendingFile()) {
errorStr.AssignLiteral("Calling StopSendingFile() failed");
DispatchReplyError(aRunnable, NS_LITERAL_STRING("StopSendingFile failed"));
return;
}
DispatchBluetoothReply(aRunnable, BluetoothValue(true), errorStr);
DispatchReplySuccess(aRunnable);
}
void
@ -985,7 +946,7 @@ BluetoothServiceBluedroid::ConfirmReceivingFile(
const nsAString& aDeviceAddress, bool aConfirm,
BluetoothReplyRunnable* aRunnable)
{
MOZ_ASSERT(NS_IsMainThread(), "Must be called from main thread!");
MOZ_ASSERT(NS_IsMainThread());
// Currently we only support one device sending one file at a time,
// so we don't need aDeviceAddress here because the target device
@ -994,10 +955,12 @@ BluetoothServiceBluedroid::ConfirmReceivingFile(
BluetoothOppManager* opp = BluetoothOppManager::Get();
nsAutoString errorStr;
if (!opp || !opp->ConfirmReceivingFile(aConfirm)) {
errorStr.AssignLiteral("Calling ConfirmReceivingFile() failed");
DispatchReplyError(aRunnable,
NS_LITERAL_STRING("ConfirmReceivingFile failed"));
return;
}
DispatchBluetoothReply(aRunnable, BluetoothValue(true), errorStr);
DispatchReplySuccess(aRunnable);
}
void
@ -1007,12 +970,11 @@ BluetoothServiceBluedroid::ConnectSco(BluetoothReplyRunnable* aRunnable)
BluetoothHfpManager* hfp = BluetoothHfpManager::Get();
if (!hfp || !hfp->ConnectSco()) {
NS_NAMED_LITERAL_STRING(replyError, "Calling ConnectSco() failed");
DispatchBluetoothReply(aRunnable, BluetoothValue(), replyError);
DispatchReplyError(aRunnable, NS_LITERAL_STRING("ConnectSco failed"));
return;
}
DispatchBluetoothReply(aRunnable, BluetoothValue(true), EmptyString());
DispatchReplySuccess(aRunnable);
}
void
@ -1022,12 +984,11 @@ BluetoothServiceBluedroid::DisconnectSco(BluetoothReplyRunnable* aRunnable)
BluetoothHfpManager* hfp = BluetoothHfpManager::Get();
if (!hfp || !hfp->DisconnectSco()) {
NS_NAMED_LITERAL_STRING(replyError, "Calling DisconnectSco() failed");
DispatchBluetoothReply(aRunnable, BluetoothValue(), replyError);
DispatchReplyError(aRunnable, NS_LITERAL_STRING("DisconnectSco failed"));
return;
}
DispatchBluetoothReply(aRunnable, BluetoothValue(true), EmptyString());
DispatchReplySuccess(aRunnable);
}
void
@ -1037,12 +998,11 @@ BluetoothServiceBluedroid::IsScoConnected(BluetoothReplyRunnable* aRunnable)
BluetoothHfpManager* hfp = BluetoothHfpManager::Get();
if (!hfp) {
NS_NAMED_LITERAL_STRING(replyError, "Fail to get BluetoothHfpManager");
DispatchBluetoothReply(aRunnable, BluetoothValue(), replyError);
DispatchReplyError(aRunnable, NS_LITERAL_STRING("IsScoConnected failed"));
return;
}
DispatchBluetoothReply(aRunnable, hfp->IsScoConnected(), EmptyString());
DispatchReplySuccess(aRunnable, BluetoothValue(hfp->IsScoConnected()));
}
void
@ -1059,7 +1019,7 @@ BluetoothServiceBluedroid::SendMetaData(const nsAString& aTitle,
a2dp->UpdateMetaData(aTitle, aArtist, aAlbum, aMediaNumber,
aTotalMediaCount, aDuration);
}
DispatchBluetoothReply(aRunnable, BluetoothValue(true), EmptyString());
DispatchReplySuccess(aRunnable);
}
void
@ -1074,7 +1034,7 @@ BluetoothServiceBluedroid::SendPlayStatus(
PlayStatusStringToControlPlayStatus(aPlayStatus);
a2dp->UpdatePlayStatus(aDuration, aPosition, playStatus);
}
DispatchBluetoothReply(aRunnable, BluetoothValue(true), EmptyString());
DispatchReplySuccess(aRunnable);
}
void
@ -1281,7 +1241,7 @@ BluetoothServiceBluedroid::AdapterStateChangedNotification(bool aState)
sBondingRunnableArray.Clear();
sUnbondingRunnableArray.Clear();
// Bluetooth scan mode is SCAN_MODE_CONNECTABLE by default, i.e., It should
// Bluetooth scan mode is SCAN_MODE_CONNECTABLE by default, i.e., it should
// be connectable and non-discoverable.
NS_ENSURE_TRUE_VOID(sBtInterface);
sBtInterface->SetAdapterProperty(
@ -1297,9 +1257,7 @@ BluetoothServiceBluedroid::AdapterStateChangedNotification(bool aState)
// Resolve promise if existed
if (!sChangeAdapterStateRunnableArray.IsEmpty()) {
DispatchBluetoothReply(sChangeAdapterStateRunnableArray[0],
BluetoothValue(true), EmptyString());
DispatchReplySuccess(sChangeAdapterStateRunnableArray[0]);
sChangeAdapterStateRunnableArray.RemoveElementAt(0);
}
}
@ -1369,8 +1327,7 @@ BluetoothServiceBluedroid::AdapterPropertiesNotification(
// Send reply for SetProperty
if (!sSetPropertyRunnableArray.IsEmpty()) {
DispatchBluetoothReply(sSetPropertyRunnableArray[0],
BluetoothValue(true), EmptyString());
DispatchReplySuccess(sSetPropertyRunnableArray[0]);
sSetPropertyRunnableArray.RemoveElementAt(0);
}
}
@ -1435,9 +1392,9 @@ BluetoothServiceBluedroid::RemoteDevicePropertiesNotification(
// (2) distribute the signal, and finally
// (3) send any pending Bluetooth replies.
//
// |DispatchBluetoothReply| creates its own internal runnable, which is
// |DispatchReplySuccess| creates its own internal runnable, which is
// always run after we completed the current method. This means that we
// can exchange |DistributeBluetoothReply| with other operations without
// can exchange |DispatchReplySuccess| with other operations without
// changing the order of (1,2) and (3).
// Update to registered BluetoothDevice objects
@ -1447,9 +1404,8 @@ BluetoothServiceBluedroid::RemoteDevicePropertiesNotification(
// FetchUuids task
if (!sFetchUuidsRunnableArray.IsEmpty()) {
// propertiesArray contains Address and Uuids only
DispatchBluetoothReply(sFetchUuidsRunnableArray[0],
propertiesArray[1].value() /* Uuids */,
EmptyString());
DispatchReplySuccess(sFetchUuidsRunnableArray[0],
propertiesArray[1].value()); /* Uuids */
sFetchUuidsRunnableArray.RemoveElementAt(0);
DistributeSignal(signal);
return;
@ -1469,8 +1425,7 @@ BluetoothServiceBluedroid::RemoteDevicePropertiesNotification(
if (--sRequestedDeviceCountArray[0] == 0) {
if (!sGetDeviceRunnableArray.IsEmpty()) {
DispatchBluetoothReply(sGetDeviceRunnableArray[0],
sRemoteDevicesPack, EmptyString());
DispatchReplySuccess(sGetDeviceRunnableArray[0], sRemoteDevicesPack);
sGetDeviceRunnableArray.RemoveElementAt(0);
}
@ -1550,9 +1505,7 @@ BluetoothServiceBluedroid::DiscoveryStateChangedNotification(bool aState)
// Reply that Promise is resolved
if (!sChangeDiscoveryRunnableArray.IsEmpty()) {
DispatchBluetoothReply(sChangeDiscoveryRunnableArray[0],
BluetoothValue(true), EmptyString());
DispatchReplySuccess(sChangeDiscoveryRunnableArray[0]);
sChangeDiscoveryRunnableArray.RemoveElementAt(0);
}
}
@ -1670,25 +1623,21 @@ BluetoothServiceBluedroid::BondStateChangedNotification(
if (aStatus == STATUS_SUCCESS) {
// Resolve existing pair/unpair promise when pair/unpair succeeded
if (bonded && !sBondingRunnableArray.IsEmpty()) {
DispatchBluetoothReply(sBondingRunnableArray[0],
BluetoothValue(true), EmptyString());
DispatchReplySuccess(sBondingRunnableArray[0]);
sBondingRunnableArray.RemoveElementAt(0);
} else if (!bonded && !sUnbondingRunnableArray.IsEmpty()) {
DispatchBluetoothReply(sUnbondingRunnableArray[0],
BluetoothValue(true), EmptyString());
DispatchReplySuccess(sUnbondingRunnableArray[0]);
sUnbondingRunnableArray.RemoveElementAt(0);
}
} else {
// Reject existing pair/unpair promise when pair/unpair failed
if (!bonded && !sBondingRunnableArray.IsEmpty()) {
DispatchBluetoothReply(sBondingRunnableArray[0],
BluetoothValue(),
NS_LITERAL_STRING("Pair Error"));
DispatchReplyError(sBondingRunnableArray[0],
NS_LITERAL_STRING("Pair failed"));
sBondingRunnableArray.RemoveElementAt(0);
} else if (bonded && !sUnbondingRunnableArray.IsEmpty()) {
DispatchBluetoothReply(sUnbondingRunnableArray[0],
BluetoothValue(),
NS_LITERAL_STRING("Unpair Error"));
DispatchReplyError(sUnbondingRunnableArray[0],
NS_LITERAL_STRING("Unpair failed"));
sUnbondingRunnableArray.RemoveElementAt(0);
}
}

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

@ -240,9 +240,6 @@ protected:
static void NextBluetoothProfileController();
static ControlPlayStatus PlayStatusStringToControlPlayStatus(
const nsAString& aPlayStatus);
static void ReplyStatusError(BluetoothReplyRunnable* aReplyRunnable,
BluetoothStatus aStatusCode,
const nsAString& aCustomMsg);
};
END_BLUETOOTH_NAMESPACE

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

@ -408,6 +408,18 @@ DispatchToBtThread(nsIRunnable* aRunnable)
return sBluetoothThread->Dispatch(aRunnable, NS_DISPATCH_NORMAL);
}
static void
DispatchBluetoothReply(BluetoothReplyRunnable* aRunnable,
const BluetoothValue& aValue,
const nsAString& aErrorStr)
{
if (!aErrorStr.IsEmpty()) {
DispatchReplyError(aRunnable, aErrorStr);
} else {
DispatchReplySuccess(aRunnable, aValue);
}
}
BluetoothDBusService::BluetoothDBusService()
{
sGetPropertyMonitor = new Monitor("BluetoothService.sGetPropertyMonitor");

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

@ -1885,8 +1885,7 @@ BluetoothHfpManager::OnScoConnectSuccess()
{
// For active connection request, we need to reply the DOMRequest
if (mScoRunnable) {
DispatchBluetoothReply(mScoRunnable,
BluetoothValue(true), EmptyString());
DispatchReplySuccess(mScoRunnable);
mScoRunnable = nullptr;
}
@ -1900,9 +1899,8 @@ void
BluetoothHfpManager::OnScoConnectError()
{
if (mScoRunnable) {
NS_NAMED_LITERAL_STRING(replyError, "Failed to create SCO socket!");
DispatchBluetoothReply(mScoRunnable, BluetoothValue(), replyError);
DispatchReplyError(mScoRunnable,
NS_LITERAL_STRING("Failed to create SCO socket!"));
mScoRunnable = nullptr;
}