From 45213344ef8a614d0034635080b60e068db8f871 Mon Sep 17 00:00:00 2001 From: Thomas Zimmermann Date: Thu, 10 Jan 2013 07:55:43 -0800 Subject: [PATCH] Bug 827888: Fix usage of sIsParing variable [r=echou,qdot] The variable sIsPairing signals to the Bluetooth pairing response notifier if pairing is still requested. Being a boolean value, the variable only supported one pairing request at a time. To support multiple parallel pairing requests, this patch converts it into a counter. The counter is atomic, because notifier calls run in separate threads. Additionally, we now set sIsPairing before starting the pairing request. Otherwise the notifier might run before we can increment the variable. --- dom/bluetooth/linux/BluetoothDBusService.cpp | 38 ++++++++++---------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/dom/bluetooth/linux/BluetoothDBusService.cpp b/dom/bluetooth/linux/BluetoothDBusService.cpp index 8134db4a97ef..bd4e5b73011d 100644 --- a/dom/bluetooth/linux/BluetoothDBusService.cpp +++ b/dom/bluetooth/linux/BluetoothDBusService.cpp @@ -29,6 +29,7 @@ #include #include +#include "pratom.h" #include "nsAutoPtr.h" #include "nsThreadUtils.h" #include "nsDebug.h" @@ -151,7 +152,7 @@ static const char* sBluetoothDBusSignals[] = static nsAutoPtr gThreadConnection; static nsDataHashtable sPairingReqTable; static nsDataHashtable sAuthorizeReqTable; -static bool sIsPairing = false; +static PRInt32 sIsPairing = 0; typedef void (*UnpackFunc)(DBusMessage*, DBusError*, BluetoothValue&, nsAString&); class RemoveDeviceTask : public nsRunnable { @@ -907,8 +908,7 @@ GetObjectPathCallback(DBusMessage* aMsg, void* aBluetoothReplyRunnable) if (sIsPairing) { RunDBusCallback(aMsg, aBluetoothReplyRunnable, UnpackObjectPathMessage); - - sIsPairing = false; + PR_AtomicDecrement(&sIsPairing); } } @@ -1655,7 +1655,7 @@ BluetoothDBusService::StopInternal() sAuthorizeReqTable.EnumerateRead(UnrefDBusMessages, nullptr); sAuthorizeReqTable.Clear(); - sIsPairing = false; + PR_AtomicSet(&sIsPairing, 0); StopDBus(); return NS_OK; @@ -2154,6 +2154,20 @@ BluetoothDBusService::CreatePairedDeviceInternal(const nsAString& aAdapterPath, nsCString tempDeviceAddress = NS_ConvertUTF16toUTF8(aDeviceAddress); const char *deviceAddress = tempDeviceAddress.get(); + /** + * FIXME: Bug 820274 + * + * If the user turns off Bluetooth in the middle of pairing process, the + * callback function GetObjectPathCallback (see the third argument of the + * function call above) may still be called while enabling next time by + * dbus daemon. To prevent this from happening, added a flag to distinguish + * if Bluetooth has been turned off. Nevertheless, we need a check if there + * is a better solution. + * + * Please see Bug 818696 for more information. + */ + PR_AtomicIncrement(&sIsPairing); + nsRefPtr runnable = aRunnable; // Then send CreatePairedDevice, it will register a temp device agent then // unregister it after pairing process is over @@ -2168,27 +2182,11 @@ BluetoothDBusService::CreatePairedDeviceInternal(const nsAString& aAdapterPath, DBUS_TYPE_OBJECT_PATH, &deviceAgentPath, DBUS_TYPE_STRING, &capabilities, DBUS_TYPE_INVALID); - if (!ret) { NS_WARNING("Could not start async function!"); return NS_ERROR_FAILURE; } - - /** - * FIXME: Bug 820274 - * - * If the user turns off Bluetooth in the middle of pairing process, the - * callback function GetObjectPathCallback (see the third argument of the - * function call above) may still be called while enabling next time by - * dbus daemon. To prevent this from happening, added a flag to distinguish - * if Bluetooth has been turned off. Nevertheless, we need a check if there - * is a better solution. - * - * Please see Bug 818696 for more information. - */ - sIsPairing = true; - runnable.forget(); return NS_OK; }