Bug 731361 - Change bluetooth firmware loading to use a DOMRequest based API. r=bent

This commit is contained in:
Eric Chou 2012-03-27 19:17:35 -04:00
parent f955145443
commit 2769592846
4 changed files with 97 additions and 84 deletions

View File

@ -1153,7 +1153,7 @@ Navigator::GetMozBluetooth(nsIDOMBluetoothAdapter** aBluetooth)
nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mWindow);
NS_ENSURE_TRUE(window, NS_ERROR_FAILURE);
mBluetooth = new bluetooth::BluetoothAdapter();
mBluetooth = new bluetooth::BluetoothAdapter(window);
bluetooth = mBluetooth;
}

View File

@ -4,13 +4,33 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "BluetoothAdapter.h"
#include "nsDOMClassInfo.h"
#include "nsDOMEvent.h"
#include "nsThreadUtils.h"
#include "nsXPCOMCIDInternal.h"
#include "mozilla/LazyIdleThread.h"
#include "mozilla/Util.h"
#include <dlfcn.h>
#include "BluetoothAdapter.h"
static void
FireEnabled(bool aResult, nsIDOMDOMRequest* aDomRequest)
{
nsCOMPtr<nsIDOMRequestService> rs = do_GetService("@mozilla.org/dom/dom-request-service;1");
if (!rs) {
NS_WARNING("No DOMRequest Service!");
return;
}
mozilla::DebugOnly<nsresult> rv = aResult ?
rs->FireSuccess(aDomRequest, JSVAL_VOID) :
rs->FireError(aDomRequest,
NS_LITERAL_STRING("Bluetooth firmware loading failed"));
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "Bluetooth firmware loading failed");
}
USING_BLUETOOTH_NAMESPACE
@ -67,11 +87,16 @@ static bool EnsureBluetoothInit() {
class ToggleBtResultTask : public nsRunnable
{
public:
ToggleBtResultTask(nsRefPtr<BluetoothAdapter>& adapterPtr, bool result)
: mResult(result)
ToggleBtResultTask(nsRefPtr<BluetoothAdapter>& adapterPtr,
nsCOMPtr<nsIDOMDOMRequest>& req,
bool enabled,
bool result)
: mResult(result),
mEnabled(enabled)
{
MOZ_ASSERT(!NS_IsMainThread());
mDOMRequest.swap(req);
mAdapterPtr.swap(adapterPtr);
}
@ -79,29 +104,35 @@ class ToggleBtResultTask : public nsRunnable
{
MOZ_ASSERT(NS_IsMainThread());
if (!mResult) {
//TODO:Bug-731361
NS_WARNING("BT firmware loading fails.\n");
// Update bt power status to BluetoothAdapter only if loading bluetooth
// firmware succeeds.
if (mResult) {
mAdapterPtr->SetEnabledInternal(mEnabled);
}
FireEnabled(mResult, mDOMRequest);
//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;
mDOMRequest = nsnull;
return NS_OK;
}
private:
nsRefPtr<BluetoothAdapter> mAdapterPtr;
nsCOMPtr<nsIDOMDOMRequest> mDOMRequest;
bool mEnabled;
bool mResult;
};
class ToggleBtTask : public nsRunnable
{
public:
ToggleBtTask(bool onOff, BluetoothAdapter* adapterPtr)
: mOnOff(onOff),
ToggleBtTask(bool enabled, nsIDOMDOMRequest* req, BluetoothAdapter* adapterPtr)
: mEnabled(enabled),
mDOMRequest(req),
mAdapterPtr(adapterPtr)
{
MOZ_ASSERT(NS_IsMainThread());
@ -111,23 +142,36 @@ class ToggleBtTask : public nsRunnable
{
MOZ_ASSERT(!NS_IsMainThread());
bool result;
#ifdef MOZ_WIDGET_GONK
// Platform specific check for gonk until object is divided in
// different implementations per platform. Linux doesn't require
// bluetooth firmware loading, but code should work otherwise.
if(!EnsureBluetoothInit()) {
NS_ERROR("Failed to load bluedroid library.\n");
return NS_ERROR_FAILURE;
}
bool result;
// return 1 if it's enabled, 0 if it's disabled, and -1 on error
int isEnabled = sBluedroidFunctions.bt_is_enabled();
//Toggle BT here
if (mOnOff) {
result = sBluedroidFunctions.bt_enable();
if ((isEnabled == 1 && mEnabled) || (isEnabled == 0 && !mEnabled)) {
result = true;
} else if (isEnabled < 0) {
result = false;
} else if (mEnabled) {
result = (sBluedroidFunctions.bt_enable() == 0) ? true : false;
} else {
result = sBluedroidFunctions.bt_disable();
result = (sBluedroidFunctions.bt_disable() == 0) ? true : false;
}
#else
result = true;
NS_WARNING("No bluetooth support in this build configuration, faking a success event instead");
#endif
// Create a result thread and pass it to Main Thread,
nsCOMPtr<nsIRunnable> resultRunnable = new ToggleBtResultTask(mAdapterPtr, result);
nsCOMPtr<nsIRunnable> resultRunnable = new ToggleBtResultTask(mAdapterPtr, mDOMRequest, mEnabled, result);
if (NS_FAILED(NS_DispatchToMainThread(resultRunnable))) {
NS_WARNING("Failed to dispatch to main thread!");
@ -137,8 +181,9 @@ class ToggleBtTask : public nsRunnable
}
private:
bool mOnOff;
bool mEnabled;
nsRefPtr<BluetoothAdapter> mAdapterPtr;
nsCOMPtr<nsIDOMDOMRequest> mDOMRequest;
};
DOMCI_DATA(BluetoothAdapter, BluetoothAdapter)
@ -147,12 +192,12 @@ NS_IMPL_CYCLE_COLLECTION_CLASS(BluetoothAdapter)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(BluetoothAdapter,
nsDOMEventTargetHelper)
NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(powered)
NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(enabled)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(BluetoothAdapter,
nsDOMEventTargetHelper)
NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(powered)
NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(enabled)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(BluetoothAdapter)
@ -163,77 +208,43 @@ NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
NS_IMPL_ADDREF_INHERITED(BluetoothAdapter, nsDOMEventTargetHelper)
NS_IMPL_RELEASE_INHERITED(BluetoothAdapter, nsDOMEventTargetHelper)
BluetoothAdapter::BluetoothAdapter()
: mPower(false)
BluetoothAdapter::BluetoothAdapter(nsPIDOMWindow *aWindow)
{
BindToOwner(aWindow);
}
NS_IMETHODIMP
BluetoothAdapter::GetPower(bool* aPower)
BluetoothAdapter::SetEnabled(bool aEnabled, nsIDOMDOMRequest** aDomRequest)
{
#ifdef MOZ_WIDGET_GONK
if(!EnsureBluetoothInit()) {
NS_ERROR("Failed to load bluedroid library.\n");
nsCOMPtr<nsIDOMRequestService> rs = do_GetService("@mozilla.org/dom/dom-request-service;1");
if (!rs) {
NS_ERROR("No DOMRequest Service!");
return NS_ERROR_FAILURE;
}
*aPower = sBluedroidFunctions.bt_is_enabled();
#else
*aPower = mPower;
#endif
return NS_OK;
}
NS_IMETHODIMP
BluetoothAdapter::SetPower(bool aPower)
{
#ifdef MOZ_WIDGET_GONK
// Platform specific check for gonk until object is divided in
// different implementations per platform. Linux doesn't require
// bluetooth firmware loading, but code should work otherwise.
if(!EnsureBluetoothInit()) {
NS_ERROR("Failed to load bluedroid library.\n");
return NS_ERROR_FAILURE;
}
#endif
if (mPower != aPower) {
mPower = aPower;
#ifdef MOZ_WIDGET_GONK
return ToggleBluetoothAsync();
#endif
}
return NS_OK;
}
nsresult
BluetoothAdapter::ToggleBluetoothAsync()
{
nsCOMPtr<nsIDOMDOMRequest> request;
nsresult rv = rs->CreateRequest(GetOwner(), getter_AddRefs(request));
NS_ENSURE_SUCCESS(rv, rv);
if (!mToggleBtThread) {
mToggleBtThread = new LazyIdleThread(15000);
}
nsCOMPtr<nsIRunnable> r = new ToggleBtTask(mPower, this);
nsCOMPtr<nsIRunnable> r = new ToggleBtTask(aEnabled, request, this);
return mToggleBtThread->Dispatch(r, NS_DISPATCH_NORMAL);
}
nsresult
BluetoothAdapter::FirePowered()
{
nsRefPtr<nsDOMEvent> event = new nsDOMEvent(nsnull, nsnull);
nsresult rv = event->InitEvent(NS_LITERAL_STRING("powered"), false, false);
rv = mToggleBtThread->Dispatch(r, NS_DISPATCH_NORMAL);
NS_ENSURE_SUCCESS(rv, rv);
rv = event->SetTrusted(true);
NS_ENSURE_SUCCESS(rv, rv);
bool dummy;
rv = DispatchEvent(event, &dummy);
NS_ENSURE_SUCCESS(rv, rv);
request.forget(aDomRequest);
return NS_OK;
}
NS_IMPL_EVENT_HANDLER(BluetoothAdapter, powered)
NS_IMETHODIMP
BluetoothAdapter::GetEnabled(bool* aEnabled)
{
*aEnabled = mEnabled;
return NS_OK;
}

View File

@ -10,6 +10,7 @@
#include "BluetoothCommon.h"
#include "nsDOMEventTargetHelper.h"
#include "nsIDOMBluetoothAdapter.h"
#include "nsIDOMDOMRequest.h"
class nsIEventTarget;
@ -27,19 +28,19 @@ public:
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(BluetoothAdapter,
nsDOMEventTargetHelper)
BluetoothAdapter();
BluetoothAdapter(nsPIDOMWindow*);
nsresult FirePowered();
inline void SetEnabledInternal(bool aEnabled) {mEnabled = aEnabled;}
protected:
bool mPower;
bool mEnabled;
NS_DECL_EVENT_HANDLER(powered)
NS_DECL_EVENT_HANDLER(enabled)
private:
nsCOMPtr<nsIEventTarget> mToggleBtThread;
nsresult ToggleBluetoothAsync();
};
END_BLUETOOTH_NAMESPACE
#endif

View File

@ -6,12 +6,13 @@
#include "nsIDOMEventTarget.idl"
interface nsIDOMDOMRequest;
interface nsIDOMEventListener;
[scriptable, builtinclass, uuid(3dbaa9f4-5c93-11e1-8592-ff9bfcc3ab4b)]
[scriptable, builtinclass, uuid(ac288eab-dcdb-4f6a-b94d-6c0e286d6a73)]
interface nsIDOMBluetoothAdapter : nsIDOMEventTarget
{
attribute boolean power;
readonly attribute bool enabled;
attribute nsIDOMEventListener onpowered;
nsIDOMDOMRequest setEnabled(in boolean enabled);
};