Bug 1153159 - Pass |promise| as a parameter in |BT_ENSURE_TRUE_RESOLVE| and |BT_ENSURE_TRUE_REJECT| macros. r=btian

This commit is contained in:
Bruce Sun 2015-04-23 11:07:59 +08:00
Родитель 7895e576a0
Коммит bd4023d862
7 изменённых файлов: 66 добавлений и 38 удалений

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

@ -124,26 +124,26 @@ extern bool gBluetoothDebugFlag;
} while(0) \
/**
* Resolve promise with |ret| if |x| is false.
* Resolve |promise| with |ret| if |x| is false.
*/
#define BT_ENSURE_TRUE_RESOLVE(x, ret) \
#define BT_ENSURE_TRUE_RESOLVE(x, promise, ret) \
do { \
if (MOZ_UNLIKELY(!(x))) { \
BT_API2_LOGR("BT_ENSURE_TRUE_RESOLVE(" #x ") failed"); \
promise->MaybeResolve(ret); \
return promise.forget(); \
(promise)->MaybeResolve(ret); \
return (promise).forget(); \
} \
} while(0)
/**
* Reject promise with |ret| if |x| is false.
* Reject |promise| with |ret| if |x| is false.
*/
#define BT_ENSURE_TRUE_REJECT(x, ret) \
#define BT_ENSURE_TRUE_REJECT(x, promise, ret) \
do { \
if (MOZ_UNLIKELY(!(x))) { \
BT_API2_LOGR("BT_ENSURE_TRUE_REJECT(" #x ") failed"); \
promise->MaybeReject(ret); \
return promise.forget(); \
(promise)->MaybeReject(ret); \
return (promise).forget(); \
} \
} while(0)

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

@ -404,11 +404,12 @@ BluetoothAdapter::StartDiscovery(ErrorResult& aRv)
* - adapter is already enabled, and
* - BluetoothService is available
*/
BT_ENSURE_TRUE_REJECT(!mDiscovering, NS_ERROR_DOM_INVALID_STATE_ERR);
BT_ENSURE_TRUE_REJECT(!mDiscovering, promise, NS_ERROR_DOM_INVALID_STATE_ERR);
BT_ENSURE_TRUE_REJECT(mState == BluetoothAdapterState::Enabled,
promise,
NS_ERROR_DOM_INVALID_STATE_ERR);
BluetoothService* bs = BluetoothService::Get();
BT_ENSURE_TRUE_REJECT(bs, NS_ERROR_NOT_AVAILABLE);
BT_ENSURE_TRUE_REJECT(bs, promise, NS_ERROR_NOT_AVAILABLE);
BT_API2_LOGR();
@ -423,6 +424,7 @@ BluetoothAdapter::StartDiscovery(ErrorResult& aRv)
nsRefPtr<BluetoothReplyRunnable> result =
new StartDiscoveryTask(this, promise);
BT_ENSURE_TRUE_REJECT(NS_SUCCEEDED(bs->StartDiscoveryInternal(result)),
promise,
NS_ERROR_DOM_OPERATION_ERR);
return promise.forget();
@ -446,11 +448,12 @@ BluetoothAdapter::StopDiscovery(ErrorResult& aRv)
* - adapter is already enabled, and
* - BluetoothService is available
*/
BT_ENSURE_TRUE_RESOLVE(mDiscovering, JS::UndefinedHandleValue);
BT_ENSURE_TRUE_RESOLVE(mDiscovering, promise, JS::UndefinedHandleValue);
BT_ENSURE_TRUE_REJECT(mState == BluetoothAdapterState::Enabled,
promise,
NS_ERROR_DOM_INVALID_STATE_ERR);
BluetoothService* bs = BluetoothService::Get();
BT_ENSURE_TRUE_REJECT(bs, NS_ERROR_NOT_AVAILABLE);
BT_ENSURE_TRUE_REJECT(bs, promise, NS_ERROR_NOT_AVAILABLE);
BT_API2_LOGR();
@ -459,6 +462,7 @@ BluetoothAdapter::StopDiscovery(ErrorResult& aRv)
promise,
NS_LITERAL_STRING("StopDiscovery"));
BT_ENSURE_TRUE_REJECT(NS_SUCCEEDED(bs->StopDiscoveryInternal(result)),
promise,
NS_ERROR_DOM_OPERATION_ERR);
return promise.forget();
@ -482,11 +486,14 @@ BluetoothAdapter::SetName(const nsAString& aName, ErrorResult& aRv)
* - adapter is already enabled, and
* - BluetoothService is available
*/
BT_ENSURE_TRUE_RESOLVE(!mName.Equals(aName), JS::UndefinedHandleValue);
BT_ENSURE_TRUE_RESOLVE(!mName.Equals(aName),
promise,
JS::UndefinedHandleValue);
BT_ENSURE_TRUE_REJECT(mState == BluetoothAdapterState::Enabled,
promise,
NS_ERROR_DOM_INVALID_STATE_ERR);
BluetoothService* bs = BluetoothService::Get();
BT_ENSURE_TRUE_REJECT(bs, NS_ERROR_NOT_AVAILABLE);
BT_ENSURE_TRUE_REJECT(bs, promise, NS_ERROR_NOT_AVAILABLE);
// Wrap property to set and runnable to handle result
nsString name(aName);
@ -499,6 +506,7 @@ BluetoothAdapter::SetName(const nsAString& aName, ErrorResult& aRv)
BT_ENSURE_TRUE_REJECT(
NS_SUCCEEDED(bs->SetProperty(BluetoothObjectType::TYPE_ADAPTER,
property, result)),
promise,
NS_ERROR_DOM_OPERATION_ERR);
return promise.forget();
@ -523,11 +531,13 @@ BluetoothAdapter::SetDiscoverable(bool aDiscoverable, ErrorResult& aRv)
* - BluetoothService is available
*/
BT_ENSURE_TRUE_RESOLVE(mDiscoverable != aDiscoverable,
promise,
JS::UndefinedHandleValue);
BT_ENSURE_TRUE_REJECT(mState == BluetoothAdapterState::Enabled,
promise,
NS_ERROR_DOM_INVALID_STATE_ERR);
BluetoothService* bs = BluetoothService::Get();
BT_ENSURE_TRUE_REJECT(bs, NS_ERROR_NOT_AVAILABLE);
BT_ENSURE_TRUE_REJECT(bs, promise, NS_ERROR_NOT_AVAILABLE);
// Wrap property to set and runnable to handle result
BluetoothNamedValue property(NS_LITERAL_STRING("Discoverable"),
@ -539,6 +549,7 @@ BluetoothAdapter::SetDiscoverable(bool aDiscoverable, ErrorResult& aRv)
BT_ENSURE_TRUE_REJECT(
NS_SUCCEEDED(bs->SetProperty(BluetoothObjectType::TYPE_ADAPTER,
property, result)),
promise,
NS_ERROR_DOM_OPERATION_ERR);
return promise.forget();
@ -603,11 +614,13 @@ BluetoothAdapter::PairUnpair(bool aPair, const nsAString& aDeviceAddress,
* - BluetoothService is available.
*/
BT_ENSURE_TRUE_REJECT(!aDeviceAddress.IsEmpty(),
promise,
NS_ERROR_DOM_INVALID_STATE_ERR);
BT_ENSURE_TRUE_REJECT(mState == BluetoothAdapterState::Enabled,
promise,
NS_ERROR_DOM_INVALID_STATE_ERR);
BluetoothService* bs = BluetoothService::Get();
BT_ENSURE_TRUE_REJECT(bs, NS_ERROR_NOT_AVAILABLE);
BT_ENSURE_TRUE_REJECT(bs, promise, NS_ERROR_NOT_AVAILABLE);
nsresult rv;
if (aPair) {
@ -625,7 +638,7 @@ BluetoothAdapter::PairUnpair(bool aPair, const nsAString& aDeviceAddress,
NS_LITERAL_STRING("Unpair"));
rv = bs->RemoveDeviceInternal(aDeviceAddress, result);
}
BT_ENSURE_TRUE_REJECT(NS_SUCCEEDED(rv), NS_ERROR_DOM_OPERATION_ERR);
BT_ENSURE_TRUE_REJECT(NS_SUCCEEDED(rv), promise, NS_ERROR_DOM_OPERATION_ERR);
return promise.forget();
}
@ -660,9 +673,10 @@ BluetoothAdapter::Enable(ErrorResult& aRv)
* - BluetoothService is available.
*/
BT_ENSURE_TRUE_REJECT(mState == BluetoothAdapterState::Disabled,
promise,
NS_ERROR_DOM_INVALID_STATE_ERR);
BluetoothService* bs = BluetoothService::Get();
BT_ENSURE_TRUE_REJECT(bs, NS_ERROR_NOT_AVAILABLE);
BT_ENSURE_TRUE_REJECT(bs, promise, NS_ERROR_NOT_AVAILABLE);
// Set adapter state "Enabling"
SetAdapterState(BluetoothAdapterState::Enabling);
@ -700,9 +714,10 @@ BluetoothAdapter::Disable(ErrorResult& aRv)
* - BluetoothService is available.
*/
BT_ENSURE_TRUE_REJECT(mState == BluetoothAdapterState::Enabled,
promise,
NS_ERROR_DOM_INVALID_STATE_ERR);
BluetoothService* bs = BluetoothService::Get();
BT_ENSURE_TRUE_REJECT(bs, NS_ERROR_NOT_AVAILABLE);
BT_ENSURE_TRUE_REJECT(bs, promise, NS_ERROR_NOT_AVAILABLE);
// Set adapter state "Disabling"
SetAdapterState(BluetoothAdapterState::Disabling);

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

@ -170,7 +170,7 @@ BluetoothDevice::FetchUuids(ErrorResult& aRv)
NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
BluetoothService* bs = BluetoothService::Get();
BT_ENSURE_TRUE_REJECT(bs, NS_ERROR_NOT_AVAILABLE);
BT_ENSURE_TRUE_REJECT(bs, promise, NS_ERROR_NOT_AVAILABLE);
nsRefPtr<BluetoothReplyRunnable> result =
new FetchUuidsTask(promise,
@ -178,7 +178,7 @@ BluetoothDevice::FetchUuids(ErrorResult& aRv)
this);
nsresult rv = bs->FetchUuidsInternal(mAddress, result);
BT_ENSURE_TRUE_REJECT(NS_SUCCEEDED(rv), NS_ERROR_DOM_OPERATION_ERR);
BT_ENSURE_TRUE_REJECT(NS_SUCCEEDED(rv), promise, NS_ERROR_DOM_OPERATION_ERR);
return promise.forget();
}

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

@ -111,14 +111,16 @@ BluetoothGatt::Connect(ErrorResult& aRv)
BT_ENSURE_TRUE_REJECT(
mConnectionState == BluetoothConnectionState::Disconnected,
promise,
NS_ERROR_DOM_INVALID_STATE_ERR);
BluetoothService* bs = BluetoothService::Get();
BT_ENSURE_TRUE_REJECT(bs, NS_ERROR_NOT_AVAILABLE);
BT_ENSURE_TRUE_REJECT(bs, promise, NS_ERROR_NOT_AVAILABLE);
if (mAppUuid.IsEmpty()) {
GenerateUuid(mAppUuid);
BT_ENSURE_TRUE_REJECT(!mAppUuid.IsEmpty(),
promise,
NS_ERROR_DOM_OPERATION_ERR);
bs->RegisterBluetoothSignalHandler(mAppUuid, this);
}
@ -149,10 +151,11 @@ BluetoothGatt::Disconnect(ErrorResult& aRv)
BT_ENSURE_TRUE_REJECT(
mConnectionState == BluetoothConnectionState::Connected,
promise,
NS_ERROR_DOM_INVALID_STATE_ERR);
BluetoothService* bs = BluetoothService::Get();
BT_ENSURE_TRUE_REJECT(bs, NS_ERROR_NOT_AVAILABLE);
BT_ENSURE_TRUE_REJECT(bs, promise, NS_ERROR_NOT_AVAILABLE);
UpdateConnectionState(BluetoothConnectionState::Disconnecting);
nsRefPtr<BluetoothReplyRunnable> result =
@ -201,10 +204,11 @@ BluetoothGatt::ReadRemoteRssi(ErrorResult& aRv)
BT_ENSURE_TRUE_REJECT(
mConnectionState == BluetoothConnectionState::Connected,
promise,
NS_ERROR_DOM_INVALID_STATE_ERR);
BluetoothService* bs = BluetoothService::Get();
BT_ENSURE_TRUE_REJECT(bs, NS_ERROR_NOT_AVAILABLE);
BT_ENSURE_TRUE_REJECT(bs, promise, NS_ERROR_NOT_AVAILABLE);
nsRefPtr<BluetoothReplyRunnable> result =
new ReadRemoteRssiTask(promise);
@ -228,10 +232,11 @@ BluetoothGatt::DiscoverServices(ErrorResult& aRv)
BT_ENSURE_TRUE_REJECT(
mConnectionState == BluetoothConnectionState::Connected &&
!mDiscoveringServices,
promise,
NS_ERROR_DOM_INVALID_STATE_ERR);
BluetoothService* bs = BluetoothService::Get();
BT_ENSURE_TRUE_REJECT(bs, NS_ERROR_NOT_AVAILABLE);
BT_ENSURE_TRUE_REJECT(bs, promise, NS_ERROR_NOT_AVAILABLE);
mDiscoveringServices = true;
nsRefPtr<BluetoothReplyRunnable> result =

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

@ -75,8 +75,8 @@ BluetoothGattCharacteristic::StartNotifications(ErrorResult& aRv)
NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
BluetoothService* bs = BluetoothService::Get();
BT_ENSURE_TRUE_REJECT(bs, NS_ERROR_NOT_AVAILABLE);
BT_ENSURE_TRUE_REJECT(mService, NS_ERROR_NOT_AVAILABLE);
BT_ENSURE_TRUE_REJECT(bs, promise, NS_ERROR_NOT_AVAILABLE);
BT_ENSURE_TRUE_REJECT(mService, promise, NS_ERROR_NOT_AVAILABLE);
nsRefPtr<BluetoothReplyRunnable> result =
new BluetoothVoidReplyRunnable(
@ -104,8 +104,8 @@ BluetoothGattCharacteristic::StopNotifications(ErrorResult& aRv)
NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
BluetoothService* bs = BluetoothService::Get();
BT_ENSURE_TRUE_REJECT(bs, NS_ERROR_NOT_AVAILABLE);
BT_ENSURE_TRUE_REJECT(mService, NS_ERROR_NOT_AVAILABLE);
BT_ENSURE_TRUE_REJECT(bs, promise, NS_ERROR_NOT_AVAILABLE);
BT_ENSURE_TRUE_REJECT(mService, promise, NS_ERROR_NOT_AVAILABLE);
nsRefPtr<BluetoothReplyRunnable> result =
new BluetoothVoidReplyRunnable(
@ -251,10 +251,11 @@ BluetoothGattCharacteristic::ReadValue(ErrorResult& aRv)
NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
BT_ENSURE_TRUE_REJECT(mProperties & GATT_CHAR_PROP_BIT_READ,
promise,
NS_ERROR_NOT_AVAILABLE);
BluetoothService* bs = BluetoothService::Get();
BT_ENSURE_TRUE_REJECT(bs, NS_ERROR_NOT_AVAILABLE);
BT_ENSURE_TRUE_REJECT(bs, promise, NS_ERROR_NOT_AVAILABLE);
nsRefPtr<BluetoothReplyRunnable> result = new ReadValueTask(this, promise);
bs->GattClientReadCharacteristicValueInternal(mService->GetAppUuid(),
@ -282,6 +283,7 @@ BluetoothGattCharacteristic::WriteValue(const ArrayBuffer& aValue,
(GATT_CHAR_PROP_BIT_WRITE_NO_RESPONSE ||
GATT_CHAR_PROP_BIT_WRITE ||
GATT_CHAR_PROP_BIT_SIGNED_WRITE),
promise,
NS_ERROR_NOT_AVAILABLE);
aValue.ComputeLengthAndData();
@ -290,7 +292,7 @@ BluetoothGattCharacteristic::WriteValue(const ArrayBuffer& aValue,
value.AppendElements(aValue.Data(), aValue.Length());
BluetoothService* bs = BluetoothService::Get();
BT_ENSURE_TRUE_REJECT(bs, NS_ERROR_NOT_AVAILABLE);
BT_ENSURE_TRUE_REJECT(bs, promise, NS_ERROR_NOT_AVAILABLE);
nsRefPtr<BluetoothReplyRunnable> result = new BluetoothVoidReplyRunnable(
nullptr, promise, NS_LITERAL_STRING("GattClientWriteCharacteristicValue"));

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

@ -157,7 +157,7 @@ BluetoothGattDescriptor::ReadValue(ErrorResult& aRv)
NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
BluetoothService* bs = BluetoothService::Get();
BT_ENSURE_TRUE_REJECT(bs, NS_ERROR_NOT_AVAILABLE);
BT_ENSURE_TRUE_REJECT(bs, promise, NS_ERROR_NOT_AVAILABLE);
nsRefPtr<BluetoothReplyRunnable> result = new ReadValueTask(this, promise);
bs->GattClientReadDescriptorValueInternal(
@ -189,7 +189,7 @@ BluetoothGattDescriptor::WriteValue(
value.AppendElements(aValue.Data(), aValue.Length());
BluetoothService* bs = BluetoothService::Get();
BT_ENSURE_TRUE_REJECT(bs, NS_ERROR_NOT_AVAILABLE);
BT_ENSURE_TRUE_REJECT(bs, promise, NS_ERROR_NOT_AVAILABLE);
nsRefPtr<BluetoothReplyRunnable> result = new BluetoothVoidReplyRunnable(
nullptr, promise, NS_LITERAL_STRING("GattClientWriteDescriptorValue"));

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

@ -76,10 +76,11 @@ BluetoothPairingHandle::SetPinCode(const nsAString& aPinCode, ErrorResult& aRv)
NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
BT_ENSURE_TRUE_REJECT(mType.EqualsLiteral(PAIRING_REQ_TYPE_ENTERPINCODE),
promise,
NS_ERROR_DOM_INVALID_STATE_ERR);
BluetoothService* bs = BluetoothService::Get();
BT_ENSURE_TRUE_REJECT(bs, NS_ERROR_NOT_AVAILABLE);
BT_ENSURE_TRUE_REJECT(bs, promise, NS_ERROR_NOT_AVAILABLE);
nsRefPtr<BluetoothReplyRunnable> result =
new BluetoothVoidReplyRunnable(nullptr /* DOMRequest */,
@ -104,13 +105,16 @@ BluetoothPairingHandle::Accept(ErrorResult& aRv)
BT_ENSURE_TRUE_REJECT(mType.EqualsLiteral(PAIRING_REQ_TYPE_CONFIRMATION) ||
mType.EqualsLiteral(PAIRING_REQ_TYPE_CONSENT),
promise,
NS_ERROR_DOM_INVALID_STATE_ERR);
BluetoothService* bs = BluetoothService::Get();
BT_ENSURE_TRUE_REJECT(bs, NS_ERROR_NOT_AVAILABLE);
BT_ENSURE_TRUE_REJECT(bs, promise, NS_ERROR_NOT_AVAILABLE);
BluetoothSspVariant variant;
BT_ENSURE_TRUE_REJECT(GetSspVariant(variant), NS_ERROR_DOM_OPERATION_ERR);
BT_ENSURE_TRUE_REJECT(GetSspVariant(variant),
promise,
NS_ERROR_DOM_OPERATION_ERR);
nsRefPtr<BluetoothReplyRunnable> result =
new BluetoothVoidReplyRunnable(nullptr /* DOMRequest */,
@ -135,7 +139,7 @@ BluetoothPairingHandle::Reject(ErrorResult& aRv)
NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
BluetoothService* bs = BluetoothService::Get();
BT_ENSURE_TRUE_REJECT(bs, NS_ERROR_NOT_AVAILABLE);
BT_ENSURE_TRUE_REJECT(bs, promise, NS_ERROR_NOT_AVAILABLE);
nsRefPtr<BluetoothReplyRunnable> result =
new BluetoothVoidReplyRunnable(nullptr /* DOMRequest */,
@ -147,7 +151,9 @@ BluetoothPairingHandle::Reject(ErrorResult& aRv)
mDeviceAddress, false /* aAccept */, EmptyString(), result);
} else { // Ssp request
BluetoothSspVariant variant;
BT_ENSURE_TRUE_REJECT(GetSspVariant(variant), NS_ERROR_DOM_OPERATION_ERR);
BT_ENSURE_TRUE_REJECT(GetSspVariant(variant),
promise,
NS_ERROR_DOM_OPERATION_ERR);
bs->SspReplyInternal(
mDeviceAddress, variant, false /* aAccept */, result);