diff --git a/dom/base/nsDOMClassInfo.cpp b/dom/base/nsDOMClassInfo.cpp index ecbde6f25f58..ab005304e85f 100644 --- a/dom/base/nsDOMClassInfo.cpp +++ b/dom/base/nsDOMClassInfo.cpp @@ -1617,8 +1617,8 @@ static nsDOMClassInfoData sClassInfoData[] = { #endif #ifdef MOZ_B2G_BT - NS_DEFINE_CLASSINFO_DATA(BluetoothAdapter, nsDOMGenericSH, - DOM_DEFAULT_SCRIPTABLE_FLAGS) + NS_DEFINE_CLASSINFO_DATA(BluetoothAdapter, nsEventTargetSH, + EVENTTARGET_SCRIPTABLE_FLAGS) #endif NS_DEFINE_CLASSINFO_DATA(DOMError, nsDOMGenericSH, diff --git a/dom/bluetooth/BluetoothAdapter.cpp b/dom/bluetooth/BluetoothAdapter.cpp index 446d9c4332ed..0b974922044d 100644 --- a/dom/bluetooth/BluetoothAdapter.cpp +++ b/dom/bluetooth/BluetoothAdapter.cpp @@ -16,47 +16,56 @@ #include #endif -#define POWERED_EVENT_NAME NS_LITERAL_STRING("powered") - -BEGIN_BLUETOOTH_NAMESPACE +USING_BLUETOOTH_NAMESPACE class ToggleBtResultTask : public nsRunnable { public: - ToggleBtResultTask(bool result, nsRefPtr& adapterPtr) + ToggleBtResultTask(nsRefPtr& adapterPtr, bool result) : mResult(result) { - MOZ_ASSERT(!NS_IsMainThread()); // This should be running on the worker thread + MOZ_ASSERT(!NS_IsMainThread()); mAdapterPtr.swap(adapterPtr); } - NS_IMETHOD Run() { - MOZ_ASSERT(NS_IsMainThread()); // This method is supposed to run on the main thread! + NS_IMETHOD Run() + { + MOZ_ASSERT(NS_IsMainThread()); + + if (!mResult) { + //TODO:Bug-731361 + NS_WARNING("BT firmware loading fails.\n"); + } + + //mAdapterPtr must be null before returning to prevent the background + //thread from racing to release it during the destruction of this runnable. mAdapterPtr->FirePowered(); + mAdapterPtr = nsnull; return NS_OK; } private: - bool mResult; nsRefPtr mAdapterPtr; + bool mResult; }; class ToggleBtTask : public nsRunnable { public: - ToggleBtTask(bool onOff, BluetoothAdapter* adapterPtr) + ToggleBtTask(bool onOff, BluetoothAdapter* adapterPtr) : mOnOff(onOff), - mAdapterPtr(adapterPtr) + mAdapterPtr(adapterPtr) { - MOZ_ASSERT(NS_IsMainThread()); // The constructor should be running on the main thread. + MOZ_ASSERT(NS_IsMainThread()); } - NS_IMETHOD Run() { - bool result; + NS_IMETHOD Run() + { + MOZ_ASSERT(!NS_IsMainThread()); - MOZ_ASSERT(!NS_IsMainThread()); // This should be running on the worker thread. + bool result; //Toggle BT here #if defined(MOZ_WIDGET_GONK) @@ -65,13 +74,16 @@ class ToggleBtTask : public nsRunnable } else { result = bt_disable(); } -#else +#else result = true; #endif // Create a result thread and pass it to Main Thread, - nsCOMPtr resultRunnable = new ToggleBtResultTask(result, mAdapterPtr); - NS_DispatchToMainThread(resultRunnable); + nsCOMPtr resultRunnable = new ToggleBtResultTask(mAdapterPtr, result); + + if (NS_FAILED(NS_DispatchToMainThread(resultRunnable))) { + NS_WARNING("Failed to dispatch to main thread!"); + } return NS_OK; } @@ -81,25 +93,21 @@ class ToggleBtTask : public nsRunnable bool mOnOff; }; -END_BLUETOOTH_NAMESPACE - -DOMCI_DATA(BluetoothAdapter, mozilla::dom::bluetooth::BluetoothAdapter) - -USING_BLUETOOTH_NAMESPACE +DOMCI_DATA(BluetoothAdapter, BluetoothAdapter) NS_IMPL_CYCLE_COLLECTION_CLASS(BluetoothAdapter) -NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(BluetoothAdapter, - nsDOMEventTargetHelper) -NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(powered) - NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END +NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(BluetoothAdapter, + nsDOMEventTargetHelper) + NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(powered) +NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END -NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(BluetoothAdapter, - nsDOMEventTargetHelper) -NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(powered) - NS_IMPL_CYCLE_COLLECTION_UNLINK_END +NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(BluetoothAdapter, + nsDOMEventTargetHelper) + NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(powered) +NS_IMPL_CYCLE_COLLECTION_UNLINK_END - NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(BluetoothAdapter) +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(BluetoothAdapter) NS_INTERFACE_MAP_ENTRY(nsIDOMBluetoothAdapter) NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(BluetoothAdapter) NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper) @@ -107,18 +115,16 @@ NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper) NS_IMPL_ADDREF_INHERITED(BluetoothAdapter, nsDOMEventTargetHelper) NS_IMPL_RELEASE_INHERITED(BluetoothAdapter, nsDOMEventTargetHelper) -BluetoothAdapter::BluetoothAdapter() : mPower(false) +BluetoothAdapter::BluetoothAdapter() + : mPower(false) { } NS_IMETHODIMP BluetoothAdapter::GetPower(bool* aPower) { -#if defined(MOZ_WIDGET_GONK) - *aPower = bt_is_enabled(); -#else *aPower = mPower; -#endif + return NS_OK; } @@ -128,13 +134,13 @@ BluetoothAdapter::SetPower(bool aPower) if (mPower != aPower) { mPower = aPower; - ToggleBluetoothAsync(); + return ToggleBluetoothAsync(); } return NS_OK; } -void +nsresult BluetoothAdapter::ToggleBluetoothAsync() { if (!mToggleBtThread) { @@ -143,14 +149,17 @@ BluetoothAdapter::ToggleBluetoothAsync() nsCOMPtr r = new ToggleBtTask(mPower, this); - mToggleBtThread->Dispatch(r, 0); + return mToggleBtThread->Dispatch(r, NS_DISPATCH_NORMAL); } nsresult BluetoothAdapter::FirePowered() { nsRefPtr event = new nsDOMEvent(nsnull, nsnull); - nsresult rv = event->InitEvent(POWERED_EVENT_NAME, false, false); + nsresult rv = event->InitEvent(NS_LITERAL_STRING("powered"), false, false); + NS_ENSURE_SUCCESS(rv, rv); + + rv = event->SetTrusted(true); NS_ENSURE_SUCCESS(rv, rv); bool dummy; diff --git a/dom/bluetooth/BluetoothAdapter.h b/dom/bluetooth/BluetoothAdapter.h index fa395a800d2a..c0559824672f 100644 --- a/dom/bluetooth/BluetoothAdapter.h +++ b/dom/bluetooth/BluetoothAdapter.h @@ -11,10 +11,12 @@ #include "nsDOMEventTargetHelper.h" #include "nsIDOMBluetoothAdapter.h" +class nsIEventTarget; + BEGIN_BLUETOOTH_NAMESPACE class BluetoothAdapter : public nsIDOMBluetoothAdapter - ,public nsDOMEventTargetHelper + , public nsDOMEventTargetHelper { public: NS_DECL_ISUPPORTS @@ -36,7 +38,7 @@ protected: private: nsCOMPtr mToggleBtThread; - void ToggleBluetoothAsync(); + nsresult ToggleBluetoothAsync(); }; END_BLUETOOTH_NAMESPACE