Bug 729991: fix followup problems of bug 713116, r=ben

This commit is contained in:
Eric Chou 2012-03-04 19:54:01 -08:00
parent 18a9d1b7ae
commit 6a15ecefcb
3 changed files with 55 additions and 44 deletions

View File

@ -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,

View File

@ -16,47 +16,56 @@
#include <bluedroid/bluetooth.h>
#endif
#define POWERED_EVENT_NAME NS_LITERAL_STRING("powered")
BEGIN_BLUETOOTH_NAMESPACE
USING_BLUETOOTH_NAMESPACE
class ToggleBtResultTask : public nsRunnable
{
public:
ToggleBtResultTask(bool result, nsRefPtr<BluetoothAdapter>& adapterPtr)
ToggleBtResultTask(nsRefPtr<BluetoothAdapter>& 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<BluetoothAdapter> 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<nsIRunnable> resultRunnable = new ToggleBtResultTask(result, mAdapterPtr);
NS_DispatchToMainThread(resultRunnable);
nsCOMPtr<nsIRunnable> 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<nsIRunnable> r = new ToggleBtTask(mPower, this);
mToggleBtThread->Dispatch(r, 0);
return mToggleBtThread->Dispatch(r, NS_DISPATCH_NORMAL);
}
nsresult
BluetoothAdapter::FirePowered()
{
nsRefPtr<nsDOMEvent> 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;

View File

@ -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<nsIEventTarget> mToggleBtThread;
void ToggleBluetoothAsync();
nsresult ToggleBluetoothAsync();
};
END_BLUETOOTH_NAMESPACE