зеркало из https://github.com/mozilla/gecko-dev.git
Bug 782588 - 'mozBluetooth is not a singleton'. r=qDot.
This commit is contained in:
Родитель
4a8bc9fd25
Коммит
cbc670ae41
|
@ -15,17 +15,11 @@
|
||||||
#include "nsContentUtils.h"
|
#include "nsContentUtils.h"
|
||||||
#include "nsDOMClassInfo.h"
|
#include "nsDOMClassInfo.h"
|
||||||
#include "nsDOMEvent.h"
|
#include "nsDOMEvent.h"
|
||||||
#include "nsDOMEventTargetHelper.h"
|
|
||||||
#include "nsIDOMDOMRequest.h"
|
#include "nsIDOMDOMRequest.h"
|
||||||
#include "nsIJSContextStack.h"
|
|
||||||
#include "nsIObserverService.h"
|
|
||||||
#include "nsIPermissionManager.h"
|
#include "nsIPermissionManager.h"
|
||||||
#include "nsThreadUtils.h"
|
#include "nsThreadUtils.h"
|
||||||
#include "nsXPCOMCIDInternal.h"
|
#include "nsXPCOMCIDInternal.h"
|
||||||
#include "mozilla/LazyIdleThread.h"
|
|
||||||
#include "mozilla/Services.h"
|
|
||||||
#include "mozilla/Util.h"
|
#include "mozilla/Util.h"
|
||||||
#include "nsIDOMDOMRequest.h"
|
|
||||||
|
|
||||||
using namespace mozilla;
|
using namespace mozilla;
|
||||||
|
|
||||||
|
@ -116,8 +110,7 @@ public:
|
||||||
{
|
{
|
||||||
MOZ_ASSERT(NS_IsMainThread());
|
MOZ_ASSERT(NS_IsMainThread());
|
||||||
|
|
||||||
mManagerPtr->SetEnabledInternal(mEnabled);
|
mManagerPtr->FireEnabledDisabledEvent(mEnabled);
|
||||||
mManagerPtr->FireEnabledDisabledEvent();
|
|
||||||
|
|
||||||
// mManagerPtr must be null before returning to prevent the background
|
// mManagerPtr must be null before returning to prevent the background
|
||||||
// thread from racing to release it during the destruction of this runnable.
|
// thread from racing to release it during the destruction of this runnable.
|
||||||
|
@ -132,11 +125,11 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
nsresult
|
nsresult
|
||||||
BluetoothManager::FireEnabledDisabledEvent()
|
BluetoothManager::FireEnabledDisabledEvent(bool aEnabled)
|
||||||
{
|
{
|
||||||
nsString eventName;
|
nsString eventName;
|
||||||
|
|
||||||
if (mEnabled) {
|
if (aEnabled) {
|
||||||
eventName.AssignLiteral("enabled");
|
eventName.AssignLiteral("enabled");
|
||||||
} else {
|
} else {
|
||||||
eventName.AssignLiteral("disabled");
|
eventName.AssignLiteral("disabled");
|
||||||
|
@ -156,142 +149,21 @@ BluetoothManager::FireEnabledDisabledEvent()
|
||||||
return NS_OK;
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
BluetoothManager::BluetoothManager(nsPIDOMWindow *aWindow) :
|
BluetoothManager::BluetoothManager(nsPIDOMWindow *aWindow)
|
||||||
BluetoothPropertyContainer(BluetoothObjectType::TYPE_MANAGER),
|
: BluetoothPropertyContainer(BluetoothObjectType::TYPE_MANAGER)
|
||||||
mEnabled(false)
|
|
||||||
{
|
{
|
||||||
|
MOZ_ASSERT(aWindow);
|
||||||
|
|
||||||
BindToOwner(aWindow);
|
BindToOwner(aWindow);
|
||||||
mPath.AssignLiteral("/");
|
mPath.AssignLiteral("/");
|
||||||
|
|
||||||
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
||||||
if (obs) {
|
|
||||||
obs->AddObserver(this, "mozsettings-changed", false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BluetoothManager::~BluetoothManager()
|
BluetoothManager::~BluetoothManager()
|
||||||
{
|
{
|
||||||
BluetoothService* bs = BluetoothService::Get();
|
BluetoothService* bs = BluetoothService::Get();
|
||||||
// We can be null on shutdown, where this might happen
|
|
||||||
if (bs) {
|
if (bs) {
|
||||||
if (NS_FAILED(bs->UnregisterBluetoothSignalHandler(mPath, this))) {
|
bs->UnregisterManager(this);
|
||||||
NS_WARNING("Failed to unregister object with observer!");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
||||||
if (obs) {
|
|
||||||
obs->RemoveObserver(this, "mozsettings-changed");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
nsresult
|
|
||||||
BluetoothManager::HandleMozsettingChanged(const PRUnichar* aData)
|
|
||||||
{
|
|
||||||
// The string that we're interested in will be a JSON string that looks like:
|
|
||||||
// {"key":"bluetooth.enabled","value":true}
|
|
||||||
nsresult rv;
|
|
||||||
|
|
||||||
nsIScriptContext* sc = GetContextForEventHandlers(&rv);
|
|
||||||
if (NS_FAILED(rv)) {
|
|
||||||
return NS_ERROR_UNEXPECTED;
|
|
||||||
}
|
|
||||||
|
|
||||||
JSContext *cx = sc->GetNativeContext();
|
|
||||||
if (!cx) {
|
|
||||||
return NS_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
// In the following [if] blocks, NS_OK will be returned even if JS_* functions
|
|
||||||
// return false. That's because this function gets called whenever mozSettings
|
|
||||||
// changes, so that we'll receive signals we're not interested in and it would
|
|
||||||
// be one of the reasons for making JS_* functions return false.
|
|
||||||
nsDependentString dataStr(aData);
|
|
||||||
JS::Value val;
|
|
||||||
if (!JS_ParseJSON(cx, dataStr.get(), dataStr.Length(), &val)) {
|
|
||||||
return NS_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!val.isObject()) {
|
|
||||||
return NS_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
JSObject &obj(val.toObject());
|
|
||||||
JS::Value key;
|
|
||||||
if (!JS_GetProperty(cx, &obj, "key", &key)) {
|
|
||||||
return NS_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!key.isString()) {
|
|
||||||
return NS_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
JSBool match;
|
|
||||||
if (!JS_StringEqualsAscii(cx, key.toString(), "bluetooth.enabled", &match)) {
|
|
||||||
return NS_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!match) {
|
|
||||||
return NS_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
JS::Value value;
|
|
||||||
if (!JS_GetProperty(cx, &obj, "value", &value)) {
|
|
||||||
return NS_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!value.isBoolean()) {
|
|
||||||
return NS_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
BluetoothService* bs = BluetoothService::Get();
|
|
||||||
if (!bs) {
|
|
||||||
NS_WARNING("BluetoothService not available!");
|
|
||||||
return NS_ERROR_FAILURE;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool enabled = value.toBoolean();
|
|
||||||
bool isEnabled = (bs->IsEnabledInternal() > 0);
|
|
||||||
if (!isEnabled && enabled) {
|
|
||||||
if (NS_FAILED(bs->RegisterBluetoothSignalHandler(NS_LITERAL_STRING("/"), this))) {
|
|
||||||
NS_ERROR("Failed to register object with observer!");
|
|
||||||
return NS_ERROR_FAILURE;
|
|
||||||
}
|
|
||||||
} else if (isEnabled && !enabled){
|
|
||||||
if (NS_FAILED(bs->UnregisterBluetoothSignalHandler(NS_LITERAL_STRING("/"), this))) {
|
|
||||||
NS_WARNING("Failed to unregister object with observer!");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return NS_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
nsCOMPtr<nsIRunnable> resultTask = new ToggleBtResultTask(this, enabled);
|
|
||||||
|
|
||||||
if (enabled) {
|
|
||||||
if (NS_FAILED(bs->Start(resultTask))) {
|
|
||||||
return NS_ERROR_FAILURE;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (NS_FAILED(bs->Stop(resultTask))) {
|
|
||||||
return NS_ERROR_FAILURE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return NS_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
NS_IMETHODIMP
|
|
||||||
BluetoothManager::Observe(nsISupports* aSubject,
|
|
||||||
const char* aTopic,
|
|
||||||
const PRUnichar* aData)
|
|
||||||
{
|
|
||||||
nsresult rv = NS_OK;
|
|
||||||
|
|
||||||
if (!strcmp("mozsettings-changed", aTopic)) {
|
|
||||||
rv = HandleMozsettingChanged(aData);
|
|
||||||
}
|
|
||||||
|
|
||||||
return rv;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -309,7 +181,13 @@ BluetoothManager::SetPropertyByValue(const BluetoothNamedValue& aValue)
|
||||||
NS_IMETHODIMP
|
NS_IMETHODIMP
|
||||||
BluetoothManager::GetEnabled(bool* aEnabled)
|
BluetoothManager::GetEnabled(bool* aEnabled)
|
||||||
{
|
{
|
||||||
*aEnabled = mEnabled;
|
BluetoothService* bs = BluetoothService::Get();
|
||||||
|
if (!bs) {
|
||||||
|
NS_WARNING("BluetoothService not available!");
|
||||||
|
return NS_ERROR_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
*aEnabled = bs->IsEnabled();
|
||||||
return NS_OK;
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -321,7 +199,7 @@ BluetoothManager::GetDefaultAdapter(nsIDOMDOMRequest** aAdapter)
|
||||||
NS_WARNING("BluetoothService not available!");
|
NS_WARNING("BluetoothService not available!");
|
||||||
return NS_ERROR_FAILURE;
|
return NS_ERROR_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
nsCOMPtr<nsIDOMRequestService> rs = do_GetService("@mozilla.org/dom/dom-request-service;1");
|
nsCOMPtr<nsIDOMRequestService> rs = do_GetService("@mozilla.org/dom/dom-request-service;1");
|
||||||
|
|
||||||
if (!rs) {
|
if (!rs) {
|
||||||
|
@ -347,22 +225,21 @@ BluetoothManager::GetDefaultAdapter(nsIDOMDOMRequest** aAdapter)
|
||||||
|
|
||||||
// static
|
// static
|
||||||
already_AddRefed<BluetoothManager>
|
already_AddRefed<BluetoothManager>
|
||||||
BluetoothManager::Create(nsPIDOMWindow* aWindow) {
|
BluetoothManager::Create(nsPIDOMWindow* aWindow)
|
||||||
nsRefPtr<BluetoothManager> manager = new BluetoothManager(aWindow);
|
{
|
||||||
|
MOZ_ASSERT(NS_IsMainThread());
|
||||||
|
MOZ_ASSERT(aWindow);
|
||||||
|
|
||||||
BluetoothService* bs = BluetoothService::Get();
|
BluetoothService* bs = BluetoothService::Get();
|
||||||
if (!bs) {
|
if (!bs) {
|
||||||
NS_WARNING("BluetoothService not available!");
|
NS_WARNING("BluetoothService not available!");
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isEnabled = (bs->IsEnabledInternal() > 0);
|
nsRefPtr<BluetoothManager> manager = new BluetoothManager(aWindow);
|
||||||
if (isEnabled) {
|
|
||||||
if (NS_FAILED(bs->RegisterBluetoothSignalHandler(NS_LITERAL_STRING("/"), manager))) {
|
bs->RegisterManager(manager);
|
||||||
NS_ERROR("Failed to register object with observer!");
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return manager.forget();
|
return manager.forget();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -389,18 +266,14 @@ NS_NewBluetoothManager(nsPIDOMWindow* aWindow,
|
||||||
|
|
||||||
uint32_t permission;
|
uint32_t permission;
|
||||||
nsresult rv =
|
nsresult rv =
|
||||||
permMgr->TestPermissionFromPrincipal(principal, "mozBluetooth", &permission);
|
permMgr->TestPermissionFromPrincipal(principal, "mozBluetooth",
|
||||||
|
&permission);
|
||||||
NS_ENSURE_SUCCESS(rv, rv);
|
NS_ENSURE_SUCCESS(rv, rv);
|
||||||
|
|
||||||
if (permission != nsIPermissionManager::ALLOW_ACTION) {
|
nsRefPtr<BluetoothManager> bluetoothManager;
|
||||||
*aBluetoothManager = nullptr;
|
|
||||||
return NS_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
nsRefPtr<BluetoothManager> bluetoothManager = BluetoothManager::Create(aWindow);
|
if (permission == nsIPermissionManager::ALLOW_ACTION) {
|
||||||
if (!bluetoothManager) {
|
bluetoothManager = BluetoothManager::Create(aWindow);
|
||||||
NS_ERROR("Cannot create bluetooth manager!");
|
|
||||||
return NS_ERROR_FAILURE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bluetoothManager.forget(aBluetoothManager);
|
bluetoothManager.forget(aBluetoothManager);
|
||||||
|
|
|
@ -11,8 +11,6 @@
|
||||||
#include "BluetoothPropertyContainer.h"
|
#include "BluetoothPropertyContainer.h"
|
||||||
#include "nsDOMEventTargetHelper.h"
|
#include "nsDOMEventTargetHelper.h"
|
||||||
#include "nsIDOMBluetoothManager.h"
|
#include "nsIDOMBluetoothManager.h"
|
||||||
#include "nsIEventTarget.h"
|
|
||||||
#include "nsIObserver.h"
|
|
||||||
#include "mozilla/Observer.h"
|
#include "mozilla/Observer.h"
|
||||||
|
|
||||||
BEGIN_BLUETOOTH_NAMESPACE
|
BEGIN_BLUETOOTH_NAMESPACE
|
||||||
|
@ -23,33 +21,24 @@ class BluetoothManager : public nsDOMEventTargetHelper
|
||||||
, public nsIDOMBluetoothManager
|
, public nsIDOMBluetoothManager
|
||||||
, public BluetoothSignalObserver
|
, public BluetoothSignalObserver
|
||||||
, public BluetoothPropertyContainer
|
, public BluetoothPropertyContainer
|
||||||
, public nsIObserver
|
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
NS_DECL_ISUPPORTS_INHERITED
|
NS_DECL_ISUPPORTS_INHERITED
|
||||||
NS_DECL_NSIDOMBLUETOOTHMANAGER
|
NS_DECL_NSIDOMBLUETOOTHMANAGER
|
||||||
NS_DECL_NSIOBSERVER
|
|
||||||
|
|
||||||
NS_FORWARD_NSIDOMEVENTTARGET(nsDOMEventTargetHelper::)
|
NS_FORWARD_NSIDOMEVENTTARGET(nsDOMEventTargetHelper::)
|
||||||
|
|
||||||
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(BluetoothManager,
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(BluetoothManager,
|
||||||
nsDOMEventTargetHelper)
|
nsDOMEventTargetHelper)
|
||||||
|
|
||||||
|
|
||||||
inline void SetEnabledInternal(bool aEnabled) {mEnabled = aEnabled;}
|
|
||||||
|
|
||||||
static already_AddRefed<BluetoothManager>
|
static already_AddRefed<BluetoothManager>
|
||||||
Create(nsPIDOMWindow* aWindow);
|
Create(nsPIDOMWindow* aWindow);
|
||||||
void Notify(const BluetoothSignal& aData);
|
void Notify(const BluetoothSignal& aData);
|
||||||
virtual void SetPropertyByValue(const BluetoothNamedValue& aValue);
|
virtual void SetPropertyByValue(const BluetoothNamedValue& aValue);
|
||||||
nsresult FireEnabledDisabledEvent();
|
nsresult FireEnabledDisabledEvent(bool aEnabled);
|
||||||
private:
|
private:
|
||||||
BluetoothManager(nsPIDOMWindow* aWindow);
|
BluetoothManager(nsPIDOMWindow* aWindow);
|
||||||
~BluetoothManager();
|
~BluetoothManager();
|
||||||
|
|
||||||
nsresult HandleMozsettingChanged(const PRUnichar* aData);
|
|
||||||
|
|
||||||
bool mEnabled;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
END_BLUETOOTH_NAMESPACE
|
END_BLUETOOTH_NAMESPACE
|
||||||
|
|
|
@ -7,16 +7,23 @@
|
||||||
#include "base/basictypes.h"
|
#include "base/basictypes.h"
|
||||||
|
|
||||||
#include "BluetoothService.h"
|
#include "BluetoothService.h"
|
||||||
|
#include "BluetoothManager.h"
|
||||||
#include "BluetoothTypes.h"
|
#include "BluetoothTypes.h"
|
||||||
#include "BluetoothReplyRunnable.h"
|
#include "BluetoothReplyRunnable.h"
|
||||||
|
|
||||||
#include "nsIDOMDOMRequest.h"
|
#include "jsapi.h"
|
||||||
#include "nsThreadUtils.h"
|
|
||||||
#include "nsXPCOMCIDInternal.h"
|
|
||||||
#include "nsObserverService.h"
|
|
||||||
#include "mozilla/Services.h"
|
#include "mozilla/Services.h"
|
||||||
#include "mozilla/LazyIdleThread.h"
|
|
||||||
#include "mozilla/Util.h"
|
#include "mozilla/Util.h"
|
||||||
|
#include "nsContentUtils.h"
|
||||||
|
#include "nsIDOMDOMRequest.h"
|
||||||
|
#include "nsIObserverService.h"
|
||||||
|
#include "nsISettingsService.h"
|
||||||
|
#include "nsThreadUtils.h"
|
||||||
|
#include "nsXPCOM.h"
|
||||||
|
#include "nsXPCOMCIDInternal.h"
|
||||||
|
|
||||||
|
#define MOZSETTINGS_CHANGED_ID "mozsettings-changed"
|
||||||
|
#define BLUETOOTH_ENABLED_SETTING "bluetooth.enabled"
|
||||||
|
|
||||||
using namespace mozilla;
|
using namespace mozilla;
|
||||||
|
|
||||||
|
@ -27,42 +34,55 @@ static bool gInShutdown = false;
|
||||||
|
|
||||||
NS_IMPL_ISUPPORTS1(BluetoothService, nsIObserver)
|
NS_IMPL_ISUPPORTS1(BluetoothService, nsIObserver)
|
||||||
|
|
||||||
class ToggleBtAck : public nsRunnable
|
class BluetoothService::ToggleBtAck : public nsRunnable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ToggleBtAck(bool aEnabled) : mEnabled(aEnabled)
|
ToggleBtAck(bool aEnabled)
|
||||||
|
: mEnabled(aEnabled)
|
||||||
{
|
{
|
||||||
|
MOZ_ASSERT(!NS_IsMainThread());
|
||||||
}
|
}
|
||||||
|
|
||||||
NS_IMETHOD Run()
|
NS_IMETHOD Run()
|
||||||
{
|
{
|
||||||
MOZ_ASSERT(NS_IsMainThread());
|
MOZ_ASSERT(NS_IsMainThread());
|
||||||
|
|
||||||
if (!mEnabled || gInShutdown) {
|
if (!gBluetoothService) {
|
||||||
if (gBluetoothService->mBluetoothCommandThread) {
|
return NS_OK;
|
||||||
nsCOMPtr<nsIThread> t;
|
|
||||||
gBluetoothService->mBluetoothCommandThread.swap(t);
|
|
||||||
t->Shutdown();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (gInShutdown) {
|
if (!gInShutdown) {
|
||||||
gBluetoothService = nullptr;
|
// Notify all the managers about the state change.
|
||||||
|
gBluetoothService->SetEnabled(mEnabled);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!mEnabled || gInShutdown) {
|
||||||
|
// Shut down the command thread if it still exists.
|
||||||
|
if (gBluetoothService->mBluetoothCommandThread) {
|
||||||
|
nsCOMPtr<nsIThread> thread;
|
||||||
|
gBluetoothService->mBluetoothCommandThread.swap(thread);
|
||||||
|
if (NS_FAILED(thread->Shutdown())) {
|
||||||
|
NS_WARNING("Failed to shut down the bluetooth command thread!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (gInShutdown) {
|
||||||
|
gBluetoothService = nullptr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return NS_OK;
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
bool mEnabled;
|
bool mEnabled;
|
||||||
};
|
};
|
||||||
|
|
||||||
class ToggleBtTask : public nsRunnable
|
class BluetoothService::ToggleBtTask : public nsRunnable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ToggleBtTask(bool aEnabled,
|
ToggleBtTask(bool aEnabled)
|
||||||
nsIRunnable* aRunnable)
|
: mEnabled(aEnabled)
|
||||||
: mEnabled(aEnabled),
|
|
||||||
mRunnable(aRunnable)
|
|
||||||
{
|
{
|
||||||
MOZ_ASSERT(NS_IsMainThread());
|
MOZ_ASSERT(NS_IsMainThread());
|
||||||
}
|
}
|
||||||
|
@ -71,42 +91,56 @@ public:
|
||||||
{
|
{
|
||||||
MOZ_ASSERT(!NS_IsMainThread());
|
MOZ_ASSERT(!NS_IsMainThread());
|
||||||
|
|
||||||
nsString replyError;
|
|
||||||
if (mEnabled) {
|
if (mEnabled) {
|
||||||
if (NS_FAILED(gBluetoothService->StartInternal())) {
|
if (NS_FAILED(gBluetoothService->StartInternal())) {
|
||||||
replyError.AssignLiteral("Bluetooth service not available - We should never reach this point!");
|
NS_WARNING("Bluetooth service failed to start!");
|
||||||
|
mEnabled = !mEnabled;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else if (NS_FAILED(gBluetoothService->StopInternal())) {
|
||||||
if (NS_FAILED(gBluetoothService->StopInternal())) {
|
NS_WARNING("Bluetooth service failed to stop!");
|
||||||
replyError.AssignLiteral("Bluetooth service not available - We should never reach this point!");
|
mEnabled = !mEnabled;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Always has to be called since this is where we take care of our reference
|
nsCOMPtr<nsIRunnable> ackTask = new BluetoothService::ToggleBtAck(mEnabled);
|
||||||
// count for runnables. If there's an error, replyError won't be empty, so
|
|
||||||
// consider our status flipped.
|
|
||||||
nsCOMPtr<nsIRunnable> ackTask = new ToggleBtAck(mEnabled);
|
|
||||||
if (NS_FAILED(NS_DispatchToMainThread(ackTask))) {
|
if (NS_FAILED(NS_DispatchToMainThread(ackTask))) {
|
||||||
NS_WARNING("Failed to dispatch to main thread!");
|
NS_WARNING("Failed to dispatch to main thread!");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!mRunnable) {
|
|
||||||
return NS_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (NS_FAILED(NS_DispatchToMainThread(mRunnable))) {
|
|
||||||
NS_WARNING("Failed to dispatch to main thread!");
|
|
||||||
}
|
|
||||||
|
|
||||||
return NS_OK;
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool mEnabled;
|
bool mEnabled;
|
||||||
nsCOMPtr<nsIRunnable> mRunnable;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class BluetoothService::StartupTask : public nsISettingsServiceCallback
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
NS_DECL_ISUPPORTS
|
||||||
|
|
||||||
|
NS_IMETHOD Handle(const nsAString& aName, const jsval& aResult, JSContext* aCx)
|
||||||
|
{
|
||||||
|
MOZ_ASSERT(NS_IsMainThread());
|
||||||
|
MOZ_ASSERT(gBluetoothService);
|
||||||
|
|
||||||
|
if (!aResult.isBoolean()) {
|
||||||
|
NS_WARNING("Setting for '" BLUETOOTH_ENABLED_SETTING "' is not a boolean!");
|
||||||
|
return NS_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
return aResult.toBoolean() ? gBluetoothService->Start() : NS_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
NS_IMETHOD HandleError(const nsAString& aName, JSContext* aCx)
|
||||||
|
{
|
||||||
|
NS_WARNING("Unable to get value for '" BLUETOOTH_ENABLED_SETTING "'");
|
||||||
|
return NS_OK;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
NS_IMPL_ISUPPORTS1(BluetoothService::StartupTask, nsISettingsServiceCallback);
|
||||||
|
|
||||||
nsresult
|
nsresult
|
||||||
BluetoothService::RegisterBluetoothSignalHandler(const nsAString& aNodeName,
|
BluetoothService::RegisterBluetoothSignalHandler(const nsAString& aNodeName,
|
||||||
BluetoothSignalObserver* aHandler)
|
BluetoothSignalObserver* aHandler)
|
||||||
|
@ -163,38 +197,218 @@ BluetoothService::DistributeSignal(const BluetoothSignal& signal)
|
||||||
}
|
}
|
||||||
|
|
||||||
nsresult
|
nsresult
|
||||||
BluetoothService::StartStopBluetooth(nsIRunnable* aResultRunnable, bool aStart)
|
BluetoothService::StartStopBluetooth(bool aStart)
|
||||||
{
|
{
|
||||||
MOZ_ASSERT(NS_IsMainThread());
|
MOZ_ASSERT(NS_IsMainThread());
|
||||||
|
|
||||||
// If we're shutting down, bail early.
|
#ifdef DEBUG
|
||||||
if (gInShutdown && aStart) {
|
if (aStart && mLastRequestedEnable) {
|
||||||
NS_ERROR("Start called while in shutdown!");
|
MOZ_ASSERT(false, "Calling Start twice in a row!");
|
||||||
return NS_ERROR_FAILURE;
|
|
||||||
}
|
}
|
||||||
|
else if (!aStart && !mLastRequestedEnable) {
|
||||||
|
MOZ_ASSERT(false, "Calling Stop twice in a row!");
|
||||||
|
}
|
||||||
|
mLastRequestedEnable = aStart;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (gInShutdown) {
|
||||||
|
if (aStart) {
|
||||||
|
// Don't try to start if we're already shutting down.
|
||||||
|
MOZ_ASSERT(false, "Start called while in shutdown!");
|
||||||
|
return NS_ERROR_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!mBluetoothCommandThread) {
|
||||||
|
// Don't create a new thread after we've begun shutdown since bluetooth
|
||||||
|
// can't be running.
|
||||||
|
return NS_OK;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
nsresult rv;
|
||||||
|
|
||||||
if (!mBluetoothCommandThread) {
|
if (!mBluetoothCommandThread) {
|
||||||
nsresult rv = NS_NewNamedThread("BluetoothCmd",
|
MOZ_ASSERT(!gInShutdown);
|
||||||
getter_AddRefs(mBluetoothCommandThread));
|
|
||||||
|
rv = NS_NewNamedThread("BluetoothCmd",
|
||||||
|
getter_AddRefs(mBluetoothCommandThread));
|
||||||
NS_ENSURE_SUCCESS(rv, rv);
|
NS_ENSURE_SUCCESS(rv, rv);
|
||||||
}
|
}
|
||||||
nsCOMPtr<nsIRunnable> r = new ToggleBtTask(aStart, aResultRunnable);
|
|
||||||
if (NS_FAILED(mBluetoothCommandThread->Dispatch(r, NS_DISPATCH_NORMAL))) {
|
nsCOMPtr<nsIRunnable> runnable = new ToggleBtTask(aStart);
|
||||||
NS_WARNING("Cannot dispatch firmware loading task!");
|
rv = mBluetoothCommandThread->Dispatch(runnable, NS_DISPATCH_NORMAL);
|
||||||
return NS_ERROR_FAILURE;
|
NS_ENSURE_SUCCESS(rv, rv);
|
||||||
|
|
||||||
|
return NS_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
BluetoothService::SetEnabled(bool aEnabled)
|
||||||
|
{
|
||||||
|
MOZ_ASSERT(NS_IsMainThread());
|
||||||
|
|
||||||
|
if (aEnabled == mEnabled) {
|
||||||
|
// Nothing to do, maybe something failed.
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mEnabled = aEnabled;
|
||||||
|
|
||||||
|
BluetoothManagerList::ForwardIterator iter(mLiveManagers);
|
||||||
|
while (iter.HasMore()) {
|
||||||
|
if (NS_FAILED(iter.GetNext()->FireEnabledDisabledEvent(mEnabled))) {
|
||||||
|
NS_WARNING("FireEnabledDisabledEvent failed!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
nsresult
|
||||||
|
BluetoothService::Start()
|
||||||
|
{
|
||||||
|
MOZ_ASSERT(NS_IsMainThread());
|
||||||
|
return StartStopBluetooth(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
nsresult
|
||||||
|
BluetoothService::Stop()
|
||||||
|
{
|
||||||
|
MOZ_ASSERT(NS_IsMainThread());
|
||||||
|
return StartStopBluetooth(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
nsresult
|
||||||
|
BluetoothService::HandleStartup()
|
||||||
|
{
|
||||||
|
MOZ_ASSERT(NS_IsMainThread());
|
||||||
|
|
||||||
|
nsCOMPtr<nsISettingsService> settings =
|
||||||
|
do_GetService("@mozilla.org/settingsService;1");
|
||||||
|
NS_ENSURE_TRUE(settings, NS_ERROR_UNEXPECTED);
|
||||||
|
|
||||||
|
nsCOMPtr<nsISettingsServiceLock> settingsLock;
|
||||||
|
nsresult rv = settings->CreateLock(getter_AddRefs(settingsLock));
|
||||||
|
NS_ENSURE_SUCCESS(rv, rv);
|
||||||
|
|
||||||
|
nsRefPtr<StartupTask> callback = new StartupTask();
|
||||||
|
rv = settingsLock->Get(BLUETOOTH_ENABLED_SETTING, callback);
|
||||||
|
NS_ENSURE_SUCCESS(rv, rv);
|
||||||
|
|
||||||
return NS_OK;
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
nsresult
|
nsresult
|
||||||
BluetoothService::Start(nsIRunnable* aResultRunnable)
|
BluetoothService::HandleSettingsChanged(const nsAString& aData)
|
||||||
{
|
{
|
||||||
return StartStopBluetooth(aResultRunnable, true);
|
MOZ_ASSERT(NS_IsMainThread());
|
||||||
|
|
||||||
|
// The string that we're interested in will be a JSON string that looks like:
|
||||||
|
// {"key":"bluetooth.enabled","value":true}
|
||||||
|
|
||||||
|
JSContext* cx = nsContentUtils::GetSafeJSContext();
|
||||||
|
if (!cx) {
|
||||||
|
return NS_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
JS::Value val;
|
||||||
|
if (!JS_ParseJSON(cx, aData.BeginReading(), aData.Length(), &val)) {
|
||||||
|
return JS_ReportPendingException(cx) ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!val.isObject()) {
|
||||||
|
return NS_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
JSObject& obj(val.toObject());
|
||||||
|
|
||||||
|
JS::Value key;
|
||||||
|
if (!JS_GetProperty(cx, &obj, "key", &key)) {
|
||||||
|
MOZ_ASSERT(!JS_IsExceptionPending(cx));
|
||||||
|
return NS_ERROR_OUT_OF_MEMORY;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!key.isString()) {
|
||||||
|
return NS_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
JSBool match;
|
||||||
|
if (!JS_StringEqualsAscii(cx, key.toString(), BLUETOOTH_ENABLED_SETTING,
|
||||||
|
&match)) {
|
||||||
|
MOZ_ASSERT(!JS_IsExceptionPending(cx));
|
||||||
|
return NS_ERROR_OUT_OF_MEMORY;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!match) {
|
||||||
|
return NS_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
JS::Value value;
|
||||||
|
if (!JS_GetProperty(cx, &obj, "value", &value)) {
|
||||||
|
MOZ_ASSERT(!JS_IsExceptionPending(cx));
|
||||||
|
return NS_ERROR_OUT_OF_MEMORY;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!value.isBoolean()) {
|
||||||
|
MOZ_ASSERT(false, "Expecting a boolean for 'bluetooth.enabled'!");
|
||||||
|
return NS_ERROR_UNEXPECTED;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value.toBoolean() == IsEnabled()) {
|
||||||
|
// Nothing to do here.
|
||||||
|
return NS_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
nsresult rv;
|
||||||
|
|
||||||
|
if (IsEnabled()) {
|
||||||
|
rv = Stop();
|
||||||
|
NS_ENSURE_SUCCESS(rv, rv);
|
||||||
|
|
||||||
|
return NS_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
rv = Start();
|
||||||
|
NS_ENSURE_SUCCESS(rv, rv);
|
||||||
|
|
||||||
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
nsresult
|
nsresult
|
||||||
BluetoothService::Stop(nsIRunnable* aResultRunnable)
|
BluetoothService::HandleShutdown()
|
||||||
{
|
{
|
||||||
return StartStopBluetooth(aResultRunnable, false);
|
MOZ_ASSERT(NS_IsMainThread());
|
||||||
|
|
||||||
|
gInShutdown = true;
|
||||||
|
|
||||||
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
||||||
|
if (obs &&
|
||||||
|
(NS_FAILED(obs->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) ||
|
||||||
|
NS_FAILED(obs->RemoveObserver(this, MOZSETTINGS_CHANGED_ID)))) {
|
||||||
|
NS_WARNING("Can't unregister observers!");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
BluetoothService::RegisterManager(BluetoothManager* aManager)
|
||||||
|
{
|
||||||
|
MOZ_ASSERT(NS_IsMainThread());
|
||||||
|
MOZ_ASSERT(aManager);
|
||||||
|
MOZ_ASSERT(!mLiveManagers.Contains(aManager));
|
||||||
|
|
||||||
|
mLiveManagers.AppendElement(aManager);
|
||||||
|
RegisterBluetoothSignalHandler(aManager->GetPath(), aManager);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
BluetoothService::UnregisterManager(BluetoothManager* aManager)
|
||||||
|
{
|
||||||
|
MOZ_ASSERT(NS_IsMainThread());
|
||||||
|
MOZ_ASSERT(aManager);
|
||||||
|
MOZ_ASSERT(mLiveManagers.Contains(aManager));
|
||||||
|
|
||||||
|
UnregisterBluetoothSignalHandler(aManager->GetPath(), aManager);
|
||||||
|
mLiveManagers.RemoveElement(aManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
// static
|
// static
|
||||||
|
@ -207,25 +421,28 @@ BluetoothService::Get()
|
||||||
if (gBluetoothService) {
|
if (gBluetoothService) {
|
||||||
return gBluetoothService;
|
return gBluetoothService;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we're in shutdown, don't create a new instance
|
// If we're in shutdown, don't create a new instance
|
||||||
if (gInShutdown) {
|
if (gInShutdown) {
|
||||||
NS_WARNING("BluetoothService returns null during shutdown");
|
NS_WARNING("BluetoothService can't be created during shutdown");
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create new instance, register, return
|
// Create new instance, register, return
|
||||||
gBluetoothService = BluetoothService::Create();
|
nsRefPtr<BluetoothService> service = BluetoothService::Create();
|
||||||
if (!gBluetoothService) {
|
NS_ENSURE_TRUE(service, nullptr);
|
||||||
NS_WARNING("Cannot create bluetooth service!");
|
|
||||||
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
||||||
|
NS_ENSURE_TRUE(obs, nullptr);
|
||||||
|
|
||||||
|
if (NS_FAILED(obs->AddObserver(service, NS_XPCOM_SHUTDOWN_OBSERVER_ID,
|
||||||
|
false)) ||
|
||||||
|
NS_FAILED(obs->AddObserver(service, MOZSETTINGS_CHANGED_ID, false))) {
|
||||||
|
NS_WARNING("AddObserver failed!");
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
|
||||||
MOZ_ASSERT(obs);
|
|
||||||
|
|
||||||
if (NS_FAILED(obs->AddObserver(gBluetoothService, "xpcom-shutdown", false))) {
|
gBluetoothService.swap(service);
|
||||||
NS_ERROR("AddObserver failed!");
|
|
||||||
}
|
|
||||||
return gBluetoothService;
|
return gBluetoothService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -233,13 +450,20 @@ nsresult
|
||||||
BluetoothService::Observe(nsISupports* aSubject, const char* aTopic,
|
BluetoothService::Observe(nsISupports* aSubject, const char* aTopic,
|
||||||
const PRUnichar* aData)
|
const PRUnichar* aData)
|
||||||
{
|
{
|
||||||
NS_ASSERTION(!strcmp(aTopic, "xpcom-shutdown"),
|
MOZ_ASSERT(NS_IsMainThread());
|
||||||
"BluetoothService got unexpected topic!");
|
|
||||||
gInShutdown = true;
|
if (!strcmp(aTopic, "profile-after-change")) {
|
||||||
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
return HandleStartup();
|
||||||
if (obs && NS_FAILED(obs->RemoveObserver(this, "xpcom-shutdown"))) {
|
|
||||||
NS_WARNING("Can't unregister bluetooth service with xpcom shutdown!");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Stop(nullptr);
|
if (!strcmp(aTopic, MOZSETTINGS_CHANGED_ID)) {
|
||||||
|
return HandleSettingsChanged(nsDependentString(aData));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
|
||||||
|
return HandleShutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
MOZ_ASSERT(false, "BluetoothService got unexpected topic!");
|
||||||
|
return NS_ERROR_UNEXPECTED;
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,20 +7,31 @@
|
||||||
#ifndef mozilla_dom_bluetooth_bluetootheventservice_h__
|
#ifndef mozilla_dom_bluetooth_bluetootheventservice_h__
|
||||||
#define mozilla_dom_bluetooth_bluetootheventservice_h__
|
#define mozilla_dom_bluetooth_bluetootheventservice_h__
|
||||||
|
|
||||||
#include "nsThreadUtils.h"
|
#include "BluetoothCommon.h"
|
||||||
|
#include "nsAutoPtr.h"
|
||||||
#include "nsClassHashtable.h"
|
#include "nsClassHashtable.h"
|
||||||
#include "nsIObserver.h"
|
#include "nsIObserver.h"
|
||||||
#include "nsIRunnable.h"
|
#include "nsIThread.h"
|
||||||
#include "BluetoothCommon.h"
|
#include "nsTObserverArray.h"
|
||||||
|
|
||||||
BEGIN_BLUETOOTH_NAMESPACE
|
BEGIN_BLUETOOTH_NAMESPACE
|
||||||
|
|
||||||
class BluetoothSignal;
|
class BluetoothManager;
|
||||||
class BluetoothReplyRunnable;
|
|
||||||
class BluetoothNamedValue;
|
class BluetoothNamedValue;
|
||||||
|
class BluetoothReplyRunnable;
|
||||||
|
class BluetoothSignal;
|
||||||
|
|
||||||
class BluetoothService : public nsIObserver
|
class BluetoothService : public nsIObserver
|
||||||
{
|
{
|
||||||
|
class ToggleBtAck;
|
||||||
|
friend class ToggleBtAck;
|
||||||
|
|
||||||
|
class ToggleBtTask;
|
||||||
|
friend class ToggleBtTask;
|
||||||
|
|
||||||
|
class StartupTask;
|
||||||
|
friend class StartupTask;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
NS_DECL_ISUPPORTS
|
NS_DECL_ISUPPORTS
|
||||||
NS_DECL_NSIOBSERVER
|
NS_DECL_NSIOBSERVER
|
||||||
|
@ -65,15 +76,10 @@ public:
|
||||||
* bluetooth needs to operate on the current platform. Assumed to be run on
|
* bluetooth needs to operate on the current platform. Assumed to be run on
|
||||||
* the main thread with delayed return for blocking startup functions via
|
* the main thread with delayed return for blocking startup functions via
|
||||||
* runnable.
|
* runnable.
|
||||||
*
|
|
||||||
* @param aResultRunnable Runnable object to execute after bluetooth has
|
|
||||||
* either come up or failed. Runnable should check existence of
|
|
||||||
* BluetoothService via Get() function to see if service started correctly.
|
|
||||||
*
|
|
||||||
* @return NS_OK on initialization starting correctly, NS_ERROR_FAILURE
|
* @return NS_OK on initialization starting correctly, NS_ERROR_FAILURE
|
||||||
* otherwise
|
* otherwise
|
||||||
*/
|
*/
|
||||||
nsresult Start(nsIRunnable* aResultRunnable);
|
nsresult Start();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stop bluetooth services. Starts up any threads and connections that
|
* Stop bluetooth services. Starts up any threads and connections that
|
||||||
|
@ -81,14 +87,35 @@ public:
|
||||||
* the main thread with delayed return for blocking startup functions via
|
* the main thread with delayed return for blocking startup functions via
|
||||||
* runnable.
|
* runnable.
|
||||||
*
|
*
|
||||||
* @param aResultRunnable Runnable object to execute after bluetooth has
|
|
||||||
* either shut down or failed. Runnable should check lack of existence of
|
|
||||||
* BluetoothService via Get() function to see if service stopped correctly.
|
|
||||||
*
|
|
||||||
* @return NS_OK on initialization starting correctly, NS_ERROR_FAILURE
|
* @return NS_OK on initialization starting correctly, NS_ERROR_FAILURE
|
||||||
* otherwise
|
* otherwise
|
||||||
*/
|
*/
|
||||||
nsresult Stop(nsIRunnable* aResultRunnable);
|
nsresult Stop();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when XPCOM first creates this service.
|
||||||
|
*/
|
||||||
|
nsresult HandleStartup();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when "mozsettings-changed" observer topic fires.
|
||||||
|
*/
|
||||||
|
nsresult HandleSettingsChanged(const nsAString& aData);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when XPCOM is shutting down.
|
||||||
|
*/
|
||||||
|
nsresult HandleShutdown();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when a BluetoothManager is created.
|
||||||
|
*/
|
||||||
|
void RegisterManager(BluetoothManager* aManager);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when a BluetoothManager is destroyed.
|
||||||
|
*/
|
||||||
|
void UnregisterManager(BluetoothManager* aManager);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the BluetoothService singleton. Only to be called from main thread.
|
* Returns the BluetoothService singleton. Only to be called from main thread.
|
||||||
|
@ -99,7 +126,13 @@ public:
|
||||||
* has not yet been started, for instance)
|
* has not yet been started, for instance)
|
||||||
*/
|
*/
|
||||||
static BluetoothService* Get();
|
static BluetoothService* Get();
|
||||||
|
|
||||||
|
static already_AddRefed<BluetoothService> FactoryCreate()
|
||||||
|
{
|
||||||
|
nsRefPtr<BluetoothService> service = Get();
|
||||||
|
return service.forget();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the path of the default adapter, implemented via a platform
|
* Returns the path of the default adapter, implemented via a platform
|
||||||
* specific method.
|
* specific method.
|
||||||
|
@ -231,7 +264,33 @@ public:
|
||||||
virtual bool SetPasskeyInternal(const nsAString& aDeviceAddress, uint32_t aPasskey) = 0;
|
virtual bool SetPasskeyInternal(const nsAString& aDeviceAddress, uint32_t aPasskey) = 0;
|
||||||
virtual bool SetPairingConfirmationInternal(const nsAString& aDeviceAddress, bool aConfirm) = 0;
|
virtual bool SetPairingConfirmationInternal(const nsAString& aDeviceAddress, bool aConfirm) = 0;
|
||||||
virtual bool SetAuthorizationInternal(const nsAString& aDeviceAddress, bool aAllow) = 0;
|
virtual bool SetAuthorizationInternal(const nsAString& aDeviceAddress, bool aAllow) = 0;
|
||||||
virtual int IsEnabledInternal() = 0;
|
|
||||||
|
virtual bool IsEnabled()
|
||||||
|
{
|
||||||
|
return mEnabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
BluetoothService()
|
||||||
|
: mEnabled(false)
|
||||||
|
#ifdef DEBUG
|
||||||
|
, mLastRequestedEnable(false)
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
mBluetoothSignalObserverTable.Init();
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual ~BluetoothService()
|
||||||
|
{ }
|
||||||
|
|
||||||
|
nsresult StartStopBluetooth(bool aStart);
|
||||||
|
|
||||||
|
// Called by ToggleBtAck.
|
||||||
|
void SetEnabled(bool aEnabled);
|
||||||
|
|
||||||
|
// This function is implemented in platform-specific BluetoothServiceFactory
|
||||||
|
// files
|
||||||
|
static BluetoothService* Create();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Due to the fact that some operations require multiple calls, a
|
* Due to the fact that some operations require multiple calls, a
|
||||||
|
@ -247,26 +306,19 @@ public:
|
||||||
*/
|
*/
|
||||||
nsCOMPtr<nsIThread> mBluetoothCommandThread;
|
nsCOMPtr<nsIThread> mBluetoothCommandThread;
|
||||||
|
|
||||||
protected:
|
|
||||||
BluetoothService()
|
|
||||||
{
|
|
||||||
mBluetoothSignalObserverTable.Init();
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual ~BluetoothService()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
nsresult StartStopBluetooth(nsIRunnable* aResultRunnable, bool aStart);
|
|
||||||
// This function is implemented in platform-specific BluetoothServiceFactory
|
|
||||||
// files
|
|
||||||
static BluetoothService* Create();
|
|
||||||
|
|
||||||
typedef mozilla::ObserverList<BluetoothSignal> BluetoothSignalObserverList;
|
typedef mozilla::ObserverList<BluetoothSignal> BluetoothSignalObserverList;
|
||||||
typedef nsClassHashtable<nsStringHashKey, BluetoothSignalObserverList >
|
typedef nsClassHashtable<nsStringHashKey, BluetoothSignalObserverList >
|
||||||
BluetoothSignalObserverTable;
|
BluetoothSignalObserverTable;
|
||||||
|
|
||||||
BluetoothSignalObserverTable mBluetoothSignalObserverTable;
|
BluetoothSignalObserverTable mBluetoothSignalObserverTable;
|
||||||
|
|
||||||
|
typedef nsTObserverArray<BluetoothManager*> BluetoothManagerList;
|
||||||
|
BluetoothManagerList mLiveManagers;
|
||||||
|
|
||||||
|
bool mEnabled;
|
||||||
|
#ifdef DEBUG
|
||||||
|
bool mLastRequestedEnable;
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
END_BLUETOOTH_NAMESPACE
|
END_BLUETOOTH_NAMESPACE
|
||||||
|
|
|
@ -110,6 +110,15 @@ using mozilla::dom::gonk::SystemWorkerManager;
|
||||||
"@mozilla.org/telephony/system-worker-manager;1"
|
"@mozilla.org/telephony/system-worker-manager;1"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef MOZ_B2G_BT
|
||||||
|
#include "BluetoothService.h"
|
||||||
|
using mozilla::dom::bluetooth::BluetoothService;
|
||||||
|
#define BLUETOOTHSERVICE_CID \
|
||||||
|
{0xa753b487, 0x3344, 0x4de4, {0xad, 0x5f, 0x06, 0x36, 0x76, 0xa7, 0xc1, 0x04}}
|
||||||
|
#define BLUETOOTHSERVICE_CONTRACTID \
|
||||||
|
"@mozilla.org/bluetooth/service;1"
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef MOZ_WIDGET_GONK
|
#ifdef MOZ_WIDGET_GONK
|
||||||
#include "AudioManager.h"
|
#include "AudioManager.h"
|
||||||
using mozilla::dom::gonk::AudioManager;
|
using mozilla::dom::gonk::AudioManager;
|
||||||
|
@ -273,6 +282,10 @@ NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(DOMRequestService,
|
||||||
NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(SystemWorkerManager,
|
NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(SystemWorkerManager,
|
||||||
SystemWorkerManager::FactoryCreate)
|
SystemWorkerManager::FactoryCreate)
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef MOZ_B2G_BT
|
||||||
|
NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(BluetoothService,
|
||||||
|
BluetoothService::FactoryCreate)
|
||||||
|
#endif
|
||||||
NS_GENERIC_FACTORY_CONSTRUCTOR(nsDOMMutationObserver)
|
NS_GENERIC_FACTORY_CONSTRUCTOR(nsDOMMutationObserver)
|
||||||
|
|
||||||
#ifdef MOZ_WIDGET_GONK
|
#ifdef MOZ_WIDGET_GONK
|
||||||
|
@ -752,6 +765,9 @@ NS_DEFINE_NAMED_CID(DOMREQUEST_SERVICE_CID);
|
||||||
#ifdef MOZ_B2G_RIL
|
#ifdef MOZ_B2G_RIL
|
||||||
NS_DEFINE_NAMED_CID(SYSTEMWORKERMANAGER_CID);
|
NS_DEFINE_NAMED_CID(SYSTEMWORKERMANAGER_CID);
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef MOZ_B2G_BT
|
||||||
|
NS_DEFINE_NAMED_CID(BLUETOOTHSERVICE_CID);
|
||||||
|
#endif
|
||||||
#ifdef MOZ_WIDGET_GONK
|
#ifdef MOZ_WIDGET_GONK
|
||||||
NS_DEFINE_NAMED_CID(NS_AUDIOMANAGER_CID);
|
NS_DEFINE_NAMED_CID(NS_AUDIOMANAGER_CID);
|
||||||
NS_DEFINE_NAMED_CID(NS_VOLUMESERVICE_CID);
|
NS_DEFINE_NAMED_CID(NS_VOLUMESERVICE_CID);
|
||||||
|
@ -1025,6 +1041,9 @@ static const mozilla::Module::CIDEntry kLayoutCIDs[] = {
|
||||||
#ifdef MOZ_B2G_RIL
|
#ifdef MOZ_B2G_RIL
|
||||||
{ &kSYSTEMWORKERMANAGER_CID, true, NULL, SystemWorkerManagerConstructor },
|
{ &kSYSTEMWORKERMANAGER_CID, true, NULL, SystemWorkerManagerConstructor },
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef MOZ_B2G_BT
|
||||||
|
{ &kBLUETOOTHSERVICE_CID, true, NULL, BluetoothServiceConstructor },
|
||||||
|
#endif
|
||||||
#ifdef MOZ_WIDGET_GONK
|
#ifdef MOZ_WIDGET_GONK
|
||||||
{ &kNS_AUDIOMANAGER_CID, true, NULL, AudioManagerConstructor },
|
{ &kNS_AUDIOMANAGER_CID, true, NULL, AudioManagerConstructor },
|
||||||
{ &kNS_VOLUMESERVICE_CID, true, NULL, nsVolumeServiceConstructor },
|
{ &kNS_VOLUMESERVICE_CID, true, NULL, nsVolumeServiceConstructor },
|
||||||
|
@ -1160,9 +1179,12 @@ static const mozilla::Module::ContractIDEntry kLayoutContracts[] = {
|
||||||
{ "@mozilla.org/editor/texteditor;1", &kNS_TEXTEDITOR_CID },
|
{ "@mozilla.org/editor/texteditor;1", &kNS_TEXTEDITOR_CID },
|
||||||
{ INDEXEDDB_MANAGER_CONTRACTID, &kINDEXEDDB_MANAGER_CID },
|
{ INDEXEDDB_MANAGER_CONTRACTID, &kINDEXEDDB_MANAGER_CID },
|
||||||
{ DOMREQUEST_SERVICE_CONTRACTID, &kDOMREQUEST_SERVICE_CID },
|
{ DOMREQUEST_SERVICE_CONTRACTID, &kDOMREQUEST_SERVICE_CID },
|
||||||
#ifdef MOZ_B2G_RIL
|
#ifdef MOZ_B2G_RIL
|
||||||
{ SYSTEMWORKERMANAGER_CONTRACTID, &kSYSTEMWORKERMANAGER_CID },
|
{ SYSTEMWORKERMANAGER_CONTRACTID, &kSYSTEMWORKERMANAGER_CID },
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef MOZ_B2G_BT
|
||||||
|
{ BLUETOOTHSERVICE_CONTRACTID, &kBLUETOOTHSERVICE_CID },
|
||||||
|
#endif
|
||||||
#ifdef MOZ_WIDGET_GONK
|
#ifdef MOZ_WIDGET_GONK
|
||||||
{ NS_AUDIOMANAGER_CONTRACTID, &kNS_AUDIOMANAGER_CID },
|
{ NS_AUDIOMANAGER_CONTRACTID, &kNS_AUDIOMANAGER_CID },
|
||||||
{ NS_VOLUMESERVICE_CONTRACTID, &kNS_VOLUMESERVICE_CID },
|
{ NS_VOLUMESERVICE_CONTRACTID, &kNS_VOLUMESERVICE_CID },
|
||||||
|
@ -1230,6 +1252,9 @@ static const mozilla::Module::CategoryEntry kLayoutCategories[] = {
|
||||||
CONTENTDLF_CATEGORIES
|
CONTENTDLF_CATEGORIES
|
||||||
#ifdef MOZ_B2G_RIL
|
#ifdef MOZ_B2G_RIL
|
||||||
{ "profile-after-change", "Telephony System Worker Manager", SYSTEMWORKERMANAGER_CONTRACTID },
|
{ "profile-after-change", "Telephony System Worker Manager", SYSTEMWORKERMANAGER_CONTRACTID },
|
||||||
|
#endif
|
||||||
|
#ifdef MOZ_B2G_BT
|
||||||
|
{ "profile-after-change", "Bluetooth Service", BLUETOOTHSERVICE_CONTRACTID },
|
||||||
#endif
|
#endif
|
||||||
{ NULL }
|
{ NULL }
|
||||||
};
|
};
|
||||||
|
|
Загрузка…
Ссылка в новой задаче