From eb8a5e3410efc70c26f74e74ffece2797c4f5c13 Mon Sep 17 00:00:00 2001 From: Jamin Liu Date: Fri, 20 Jun 2014 12:07:23 +0800 Subject: [PATCH] Bug 1020300 - Dispatch part of AdapterStateChangedCallback to main thread. r=btian --- .../bluedroid/BluetoothServiceBluedroid.cpp | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/dom/bluetooth2/bluedroid/BluetoothServiceBluedroid.cpp b/dom/bluetooth2/bluedroid/BluetoothServiceBluedroid.cpp index 545ff954c544..06b555a438a4 100644 --- a/dom/bluetooth2/bluedroid/BluetoothServiceBluedroid.cpp +++ b/dom/bluetooth2/bluedroid/BluetoothServiceBluedroid.cpp @@ -50,12 +50,12 @@ USING_BLUETOOTH_NAMESPACE static nsString sAdapterBdAddress; static nsString sAdapterBdName; static InfallibleTArray sAdapterBondedAddressArray; -static nsTArray > sChangeAdapterStateRunnableArray; // Static variables below should only be used on *main thread* static const bt_interface_t* sBtInterface; static nsTArray > sControllerArray; static nsTArray sRequestedDeviceCountArray; +static nsTArray > sChangeAdapterStateRunnableArray; static nsTArray > sChangeDiscoveryRunnableArray; static nsTArray > sSetPropertyRunnableArray; static nsTArray > sGetDeviceRunnableArray; @@ -295,6 +295,27 @@ PlayStatusStringToControlPlayStatus(const nsAString& aPlayStatus) return playStatus; } +class AdapterStateChangedCallbackTask MOZ_FINAL : public nsRunnable +{ +public: + NS_IMETHOD + Run() + { + MOZ_ASSERT(NS_IsMainThread()); + + // Resolve promise if existed + if (!sChangeAdapterStateRunnableArray.IsEmpty()) { + BluetoothValue values(true); + DispatchBluetoothReply(sChangeAdapterStateRunnableArray[0], + values, EmptyString()); + + sChangeAdapterStateRunnableArray.RemoveElementAt(0); + } + + return NS_OK; + } +}; + /** * Bluedroid HAL callback functions * @@ -327,13 +348,8 @@ AdapterStateChangeCallback(bt_state_t aStatus) return; } - // Resolve promise if existed - if(!sChangeAdapterStateRunnableArray.IsEmpty()) { - DispatchBluetoothReply(sChangeAdapterStateRunnableArray[0], - BluetoothValue(true), - EmptyString()); - sChangeAdapterStateRunnableArray.RemoveElementAt(0); - } + // Redirect to main thread to avoid racing problem + NS_DispatchToMainThread(new AdapterStateChangedCallbackTask()); } class AdapterPropertiesCallbackTask MOZ_FINAL : public nsRunnable