Bug 790133 - Final version: Fire devicecreated event with a BluetoothDevice, r=qdot, sr=mrbkap

This commit is contained in:
Gina Yeh
2012-09-17 14:39:47 +08:00
parent 3af9b4e8c4
commit aa82a44aa4
8 changed files with 143 additions and 2 deletions

View File

@@ -947,6 +947,18 @@ EventFilter(DBusConnection* aConn, DBusMessage* aMsg, void* aData)
} else {
v = NS_ConvertUTF8toUTF16(str);
}
// Get device properties and then send to BluetoothAdapter
BluetoothService* bs = BluetoothService::Get();
if (!bs) {
NS_WARNING("BluetoothService not available!");
}
if (NS_FAILED(bs->GetDevicePropertiesInternal(v, signalPath))) {
NS_WARNING("get properties failed");
}
return DBUS_HANDLER_RESULT_HANDLED;
} else if (dbus_message_is_signal(aMsg, DBUS_ADAPTER_IFACE, "DeviceRemoved")) {
const char* str;
if (!dbus_message_get_args(aMsg, &err,
@@ -1264,6 +1276,66 @@ BluetoothDBusService::StartDiscoveryInternal(const nsAString& aAdapterPath,
return SendDiscoveryMessage(aAdapterPath, "StartDiscovery", aRunnable);
}
class BluetoothDevicePropertiesRunnable : public nsRunnable
{
public:
BluetoothDevicePropertiesRunnable(const nsAString& aDevicePath,
const nsAString& aSignalPath) :
mDevicePath(aDevicePath),
mSignalPath(aSignalPath)
{
MOZ_ASSERT(NS_IsMainThread());
}
~BluetoothDevicePropertiesRunnable()
{
}
NS_IMETHOD Run()
{
MOZ_ASSERT(!NS_IsMainThread());
DBusError err;
dbus_error_init(&err);
BluetoothValue v = InfallibleTArray<BluetoothNamedValue>();
nsString replyError;
DBusMessage* msg = dbus_func_args_timeout(gThreadConnection->GetConnection(),
1000,
&err,
NS_ConvertUTF16toUTF8(mDevicePath).get(),
DBUS_DEVICE_IFACE,
"GetProperties",
DBUS_TYPE_INVALID);
UnpackDevicePropertiesMessage(msg, &err, v, replyError);
if (!replyError.IsEmpty()) {
NS_WARNING("Failed to get device properties");
return NS_ERROR_FAILURE;
}
if (msg) {
dbus_message_unref(msg);
}
BluetoothSignal signal(NS_LITERAL_STRING("DeviceCreated"),
mSignalPath, v);
nsRefPtr<DistributeBluetoothSignalTask> t = new DistributeBluetoothSignalTask(signal);
if (NS_FAILED(NS_DispatchToMainThread(t))) {
NS_WARNING("Failed to dispatch to main thread!");
return NS_ERROR_FAILURE;
}
return NS_OK;
}
private:
nsString mDevicePath;
nsString mSignalPath;
};
class BluetoothPairedDevicePropertiesRunnable : public nsRunnable
{
public:
@@ -1364,6 +1436,26 @@ BluetoothDBusService::GetProperties(BluetoothObjectType aType,
return NS_OK;
}
nsresult
BluetoothDBusService::GetDevicePropertiesInternal(const nsAString& aDevicePath,
const nsAString& aSignalPath)
{
NS_ASSERTION(NS_IsMainThread(), "Must be called from main thread!");
if (!mConnection || !gThreadConnection) {
NS_ERROR("Bluetooth service not started yet!");
return NS_ERROR_FAILURE;
}
nsRefPtr<nsRunnable> func(new BluetoothDevicePropertiesRunnable(aDevicePath, aSignalPath));
if (NS_FAILED(mBluetoothCommandThread->Dispatch(func, NS_DISPATCH_NORMAL))) {
NS_WARNING("Cannot dispatch task!");
return NS_ERROR_FAILURE;
}
return NS_OK;
}
nsresult
BluetoothDBusService::GetPairedDevicePropertiesInternal(const nsTArray<nsString>& aDeviceAddresses,
BluetoothReplyRunnable* aRunnable)