Bug 1137103 - Simplify Bluetooth signal distribution function, r=shuang

This commit is contained in:
Ben Tian 2015-03-04 09:28:32 +08:00
parent 68319db207
commit 34a718604e
8 changed files with 79 additions and 66 deletions

View File

@ -324,6 +324,20 @@ BluetoothService::UnregisterAllSignalHandlers(BluetoothSignalObserver* aHandler)
mBluetoothSignalObserverTable.Enumerate(RemoveAllSignalHandlers, aHandler);
}
void
BluetoothService::DistributeSignal(const nsAString& aName, const nsAString& aPath)
{
DistributeSignal(aName, aPath, BluetoothValue(true));
}
void
BluetoothService::DistributeSignal(const nsAString& aName, const nsAString& aPath,
const BluetoothValue& aValue)
{
BluetoothSignal signal(nsString(aName), nsString(aPath), aValue);
DistributeSignal(signal);
}
void
BluetoothService::DistributeSignal(const BluetoothSignal& aSignal)
{
@ -653,11 +667,10 @@ BluetoothService::FireAdapterStateChanged(bool aEnable)
InfallibleTArray<BluetoothNamedValue> props;
BT_APPEND_NAMED_VALUE(props, "State", aEnable);
BluetoothValue value(props);
BluetoothSignal signal(NS_LITERAL_STRING("PropertyChanged"),
NS_LITERAL_STRING(KEY_ADAPTER), value);
DistributeSignal(signal);
DistributeSignal(NS_LITERAL_STRING("PropertyChanged"),
NS_LITERAL_STRING(KEY_ADAPTER),
BluetoothValue(props));
}
void

View File

@ -92,22 +92,40 @@ public:
void
UnregisterAllSignalHandlers(BluetoothSignalObserver* aMsgHandler);
/**
* Create a signal without value and distribute it to the observer list
*
* @param aName Name of the signal
* @param aPath Path of the signal to distribute to
*/
void
DistributeSignal(const nsAString& aName, const nsAString& aPath);
/**
* Create a signal and distribute it to the observer list
*
* @param aName Name of the signal
* @param aPath Path of the signal to distribute to
* @param aValue Value of the signal to carry
*/
void
DistributeSignal(const nsAString& aName, const nsAString& aPath,
const BluetoothValue& aValue);
/**
* Distribute a signal to the observer list
*
* @param aSignal Signal object to distribute
*
* @return NS_OK if signal distributed, NS_ERROR_FAILURE on error
*/
void
DistributeSignal(const BluetoothSignal& aEvent);
DistributeSignal(const BluetoothSignal& aSignal);
/**
* Returns the BluetoothService singleton. Only to be called from main thread.
*
* @param aService Pointer to return singleton into.
*
* @return NS_OK on proper assignment, NS_ERROR_FAILURE otherwise (if service
* @return non-nullptr on proper assignment, nullptr otherwise (if service
* has not yet been started, for instance)
*/
static BluetoothService*

View File

@ -246,11 +246,9 @@ DispatchStatusChangedEvent(const nsAString& aType,
BT_APPEND_NAMED_VALUE(data, "address", nsString(aAddress));
BT_APPEND_NAMED_VALUE(data, "status", aStatus);
BluetoothSignal signal(nsString(aType), NS_LITERAL_STRING(KEY_ADAPTER), data);
BluetoothService* bs = BluetoothService::Get();
NS_ENSURE_TRUE_VOID(bs);
bs->DistributeSignal(signal);
bs->DistributeSignal(aType, NS_LITERAL_STRING(KEY_ADAPTER), data);
}
bool

View File

@ -1022,10 +1022,8 @@ BluetoothA2dpManager::GetPlayStatusNotification()
return;
}
bs->DistributeSignal(
BluetoothSignal(NS_LITERAL_STRING(REQUEST_MEDIA_PLAYSTATUS_ID),
NS_LITERAL_STRING(KEY_ADAPTER),
InfallibleTArray<BluetoothNamedValue>()));
bs->DistributeSignal(NS_LITERAL_STRING(REQUEST_MEDIA_PLAYSTATUS_ID),
NS_LITERAL_STRING(KEY_ADAPTER));
}
/* Player application settings is optional for AVRCP 1.3. B2G

View File

@ -261,11 +261,10 @@ public:
NS_ENSURE_TRUE_VOID(bs);
// Notify BluetoothGatt for client disconnected
BluetoothSignal signal(
bs->DistributeSignal(
NS_LITERAL_STRING(GATT_CONNECTION_STATE_CHANGED_ID),
mClient->mAppUuid,
BluetoothValue(false)); // Disconnected
bs->DistributeSignal(signal);
// Reject the connect request
if (mClient->mConnectRunnable) {
@ -298,11 +297,10 @@ public:
NS_ENSURE_TRUE_VOID(bs);
// Notify BluetoothGatt to clear the clientIf
BluetoothSignal signal(
bs->DistributeSignal(
NS_LITERAL_STRING("ClientUnregistered"),
mClient->mAppUuid,
BluetoothValue(true));
bs->DistributeSignal(signal);
// Resolve the unregister request
DispatchReplySuccess(mClient->mUnregisterClientRunnable);
@ -374,11 +372,10 @@ public:
NS_ENSURE_TRUE_VOID(bs);
// Notify BluetoothGatt for client disconnected
BluetoothSignal signal(
bs->DistributeSignal(
NS_LITERAL_STRING(GATT_CONNECTION_STATE_CHANGED_ID),
mClient->mAppUuid,
BluetoothValue(false)); // Disconnected
bs->DistributeSignal(signal);
// Reject the connect request
DispatchReplyError(mClient->mConnectRunnable,
@ -444,11 +441,10 @@ public:
NS_ENSURE_TRUE_VOID(bs);
// Notify BluetoothGatt that the client remains connected
BluetoothSignal signal(
bs->DistributeSignal(
NS_LITERAL_STRING(GATT_CONNECTION_STATE_CHANGED_ID),
mClient->mAppUuid,
BluetoothValue(true)); // Connected
bs->DistributeSignal(signal);
// Reject the disconnect request
DispatchReplyError(mClient->mDisconnectRunnable,
@ -515,10 +511,9 @@ BluetoothGattManager::RegisterClientNotification(int aStatus,
aClientIf, aStatus, NS_ConvertUTF16toUTF8(uuid).get());
// Notify BluetoothGatt for client disconnected
BluetoothSignal signal(
bs->DistributeSignal(
NS_LITERAL_STRING(GATT_CONNECTION_STATE_CHANGED_ID),
uuid, BluetoothValue(false)); // Disconnected
bs->DistributeSignal(signal);
// Reject the connect request
if (client->mConnectRunnable) {
@ -535,10 +530,9 @@ BluetoothGattManager::RegisterClientNotification(int aStatus,
client->mClientIf = aClientIf;
// Notify BluetoothGatt to update the clientIf
BluetoothSignal signal(
bs->DistributeSignal(
NS_LITERAL_STRING("ClientRegistered"),
uuid, BluetoothValue(uint32_t(aClientIf)));
bs->DistributeSignal(signal);
// Client just registered, proceed remaining connect request.
if (client->mConnectRunnable) {
@ -576,11 +570,10 @@ BluetoothGattManager::ConnectNotification(int aConnId,
aClientIf, aConnId, aStatus);
// Notify BluetoothGatt that the client remains disconnected
BluetoothSignal signal(
bs->DistributeSignal(
NS_LITERAL_STRING(GATT_CONNECTION_STATE_CHANGED_ID),
client->mAppUuid,
BluetoothValue(false)); // Disconnected
bs->DistributeSignal(signal);
// Reject the connect request
if (client->mConnectRunnable) {
@ -595,11 +588,10 @@ BluetoothGattManager::ConnectNotification(int aConnId,
client->mConnId = aConnId;
// Notify BluetoothGatt for client connected
BluetoothSignal signal(
bs->DistributeSignal(
NS_LITERAL_STRING(GATT_CONNECTION_STATE_CHANGED_ID),
client->mAppUuid,
BluetoothValue(true)); // Connected
bs->DistributeSignal(signal);
// Resolve the connect request
if (client->mConnectRunnable) {
@ -627,11 +619,10 @@ BluetoothGattManager::DisconnectNotification(int aConnId,
if (aStatus) { // operation failed
// Notify BluetoothGatt that the client remains connected
BluetoothSignal signal(
bs->DistributeSignal(
NS_LITERAL_STRING(GATT_CONNECTION_STATE_CHANGED_ID),
client->mAppUuid,
BluetoothValue(true)); // Connected
bs->DistributeSignal(signal);
// Reject the disconnect request
if (client->mDisconnectRunnable) {
@ -646,11 +637,10 @@ BluetoothGattManager::DisconnectNotification(int aConnId,
client->mConnId = 0;
// Notify BluetoothGatt for client disconnected
BluetoothSignal signal(
bs->DistributeSignal(
NS_LITERAL_STRING(GATT_CONNECTION_STATE_CHANGED_ID),
client->mAppUuid,
BluetoothValue(false)); // Disconnected
bs->DistributeSignal(signal);
// Resolve the disconnect request
if (client->mDisconnectRunnable) {

View File

@ -1219,9 +1219,9 @@ BluetoothServiceBluedroid::AdapterStateChangedNotification(bool aState)
BT_APPEND_NAMED_VALUE(props, "Discovering", false);
}
BluetoothSignal signal(NS_LITERAL_STRING("PropertyChanged"),
NS_LITERAL_STRING(KEY_ADAPTER), props);
bs->DistributeSignal(signal);
bs->DistributeSignal(NS_LITERAL_STRING("PropertyChanged"),
NS_LITERAL_STRING(KEY_ADAPTER),
BluetoothValue(props));
// Cleanup bluetooth interfaces after BT state becomes BT_STATE_OFF.
nsRefPtr<ProfileDeinitResultHandler> res =
@ -1322,9 +1322,9 @@ BluetoothServiceBluedroid::AdapterPropertiesNotification(
NS_ENSURE_TRUE_VOID(propertiesArray.Length() > 0);
DistributeSignal(BluetoothSignal(NS_LITERAL_STRING("PropertyChanged"),
NS_LITERAL_STRING(KEY_ADAPTER),
BluetoothValue(propertiesArray)));
DistributeSignal(NS_LITERAL_STRING("PropertyChanged"),
NS_LITERAL_STRING(KEY_ADAPTER),
BluetoothValue(propertiesArray));
// Send reply for SetProperty
if (!sSetPropertyRunnableArray.IsEmpty()) {
@ -1484,9 +1484,9 @@ BluetoothServiceBluedroid::DeviceFoundNotification(
}
}
DistributeSignal(BluetoothSignal(NS_LITERAL_STRING("DeviceFound"),
NS_LITERAL_STRING(KEY_ADAPTER),
BluetoothValue(propertiesArray)));
DistributeSignal(NS_LITERAL_STRING("DeviceFound"),
NS_LITERAL_STRING(KEY_ADAPTER),
BluetoothValue(propertiesArray));
}
void
@ -1500,9 +1500,9 @@ BluetoothServiceBluedroid::DiscoveryStateChangedNotification(bool aState)
InfallibleTArray<BluetoothNamedValue> propertiesArray;
BT_APPEND_NAMED_VALUE(propertiesArray, "Discovering", sAdapterDiscovering);
DistributeSignal(BluetoothSignal(NS_LITERAL_STRING("PropertyChanged"),
NS_LITERAL_STRING(KEY_ADAPTER),
BluetoothValue(propertiesArray)));
DistributeSignal(NS_LITERAL_STRING("PropertyChanged"),
NS_LITERAL_STRING(KEY_ADAPTER),
BluetoothValue(propertiesArray));
// Reply that Promise is resolved
if (!sChangeDiscoveryRunnableArray.IsEmpty()) {
@ -1528,9 +1528,9 @@ BluetoothServiceBluedroid::PinRequestNotification(const nsAString& aRemoteBdAddr
sPairingNameTable.Put(nsString(aRemoteBdAddr), nsString(aBdName));
DistributeSignal(BluetoothSignal(NS_LITERAL_STRING("PairingRequest"),
NS_LITERAL_STRING(KEY_PAIRING_LISTENER),
BluetoothValue(propertiesArray)));
DistributeSignal(NS_LITERAL_STRING("PairingRequest"),
NS_LITERAL_STRING(KEY_PAIRING_LISTENER),
BluetoothValue(propertiesArray));
}
void
@ -1576,9 +1576,9 @@ BluetoothServiceBluedroid::SspRequestNotification(
sPairingNameTable.Put(nsString(aRemoteBdAddr), nsString(aBdName));
DistributeSignal(BluetoothSignal(NS_LITERAL_STRING("PairingRequest"),
NS_LITERAL_STRING(KEY_PAIRING_LISTENER),
BluetoothValue(propertiesArray)));
DistributeSignal(NS_LITERAL_STRING("PairingRequest"),
NS_LITERAL_STRING(KEY_PAIRING_LISTENER),
BluetoothValue(propertiesArray));
}
void
@ -1622,9 +1622,9 @@ BluetoothServiceBluedroid::BondStateChangedNotification(
BT_APPEND_NAMED_VALUE(propertiesArray, "Name", deviceName);
}
DistributeSignal(BluetoothSignal(NS_LITERAL_STRING("PropertyChanged"),
nsString(aRemoteBdAddr),
BluetoothValue(propertiesArray)));
DistributeSignal(NS_LITERAL_STRING("PropertyChanged"),
aRemoteBdAddr,
BluetoothValue(propertiesArray));
// Insert address to signal properties and notify adapter.
BT_INSERT_NAMED_VALUE(propertiesArray, 0, "Address", nsString(aRemoteBdAddr));
@ -1632,9 +1632,9 @@ BluetoothServiceBluedroid::BondStateChangedNotification(
nsString signalName = bonded ? NS_LITERAL_STRING(DEVICE_PAIRED_ID)
: NS_LITERAL_STRING(DEVICE_UNPAIRED_ID);
DistributeSignal(BluetoothSignal(signalName,
NS_LITERAL_STRING(KEY_ADAPTER),
BluetoothValue(propertiesArray)));
DistributeSignal(signalName,
NS_LITERAL_STRING(KEY_ADAPTER),
BluetoothValue(propertiesArray));
if (aStatus == STATUS_SUCCESS) {
// Resolve existing pair/unpair promise when pair/unpair succeeded

View File

@ -1734,13 +1734,11 @@ public:
{
MOZ_ASSERT(NS_IsMainThread());
BluetoothSignal signal(NS_LITERAL_STRING(REQUEST_MEDIA_PLAYSTATUS_ID),
NS_LITERAL_STRING(KEY_ADAPTER),
InfallibleTArray<BluetoothNamedValue>());
BluetoothService* bs = BluetoothService::Get();
NS_ENSURE_TRUE(bs, NS_ERROR_FAILURE);
bs->DistributeSignal(signal);
bs->DistributeSignal(NS_LITERAL_STRING(REQUEST_MEDIA_PLAYSTATUS_ID),
NS_LITERAL_STRING(KEY_ADAPTER));
return NS_OK;
}

View File

@ -14,7 +14,6 @@ namespace bluetooth {
* Value structure for returns from bluetooth. Currently modeled after dbus
* returns, which can be a 32-bit int, an UTF16 string, a bool, or an array of
* UTF16 strings. Can also hold key-value pairs for dictionary-ish access.
*
*/
union BluetoothValue
{
@ -29,7 +28,6 @@ union BluetoothValue
/**
* Key-value pair for dicts returned by the bluetooth backend. Used for things
* like property updates, where the property will have a name and a type.
*
*/
struct BluetoothNamedValue
{