2012-07-17 20:41:54 -07:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
2012-08-20 20:21:24 -07:00
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
2012-07-17 20:41:54 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* 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 "base/basictypes.h"
|
|
|
|
|
|
|
|
#include "BluetoothService.h"
|
2012-09-13 09:37:14 -07:00
|
|
|
|
2013-03-15 00:15:47 -07:00
|
|
|
#include "BluetoothCommon.h"
|
2013-09-06 04:20:27 -07:00
|
|
|
#include "BluetoothA2dpManager.h"
|
2013-08-08 02:51:37 -07:00
|
|
|
#include "BluetoothHfpManager.h"
|
2013-09-06 04:20:27 -07:00
|
|
|
#include "BluetoothHidManager.h"
|
2012-09-06 07:15:36 -07:00
|
|
|
#include "BluetoothManager.h"
|
2013-08-08 02:51:37 -07:00
|
|
|
#include "BluetoothOppManager.h"
|
2012-09-13 09:37:14 -07:00
|
|
|
#include "BluetoothParent.h"
|
2012-07-17 20:41:54 -07:00
|
|
|
#include "BluetoothReplyRunnable.h"
|
2012-09-13 09:37:14 -07:00
|
|
|
#include "BluetoothServiceChildProcess.h"
|
2012-11-20 23:33:07 -08:00
|
|
|
#include "BluetoothUtils.h"
|
2012-07-17 20:41:54 -07:00
|
|
|
|
2012-09-06 07:15:36 -07:00
|
|
|
#include "jsapi.h"
|
|
|
|
#include "mozilla/Services.h"
|
2012-09-13 09:37:14 -07:00
|
|
|
#include "mozilla/StaticPtr.h"
|
|
|
|
#include "mozilla/unused.h"
|
|
|
|
#include "mozilla/dom/ContentParent.h"
|
|
|
|
#include "mozilla/dom/bluetooth/BluetoothTypes.h"
|
2012-09-25 13:13:15 -07:00
|
|
|
#include "mozilla/ipc/UnixSocket.h"
|
2013-08-30 11:18:43 -07:00
|
|
|
#include "mozilla/LazyIdleThread.h"
|
2012-09-06 07:15:36 -07:00
|
|
|
#include "nsContentUtils.h"
|
2013-05-22 09:05:27 -07:00
|
|
|
#include "nsCxPusher.h"
|
2012-09-06 07:15:36 -07:00
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsISettingsService.h"
|
2012-09-13 09:37:14 -07:00
|
|
|
#include "nsISystemMessagesInternal.h"
|
|
|
|
#include "nsITimer.h"
|
2013-09-10 13:56:05 -07:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2012-09-06 07:15:36 -07:00
|
|
|
#include "nsXPCOM.h"
|
2012-09-13 09:37:14 -07:00
|
|
|
|
2013-02-05 23:28:04 -08:00
|
|
|
#if defined(MOZ_WIDGET_GONK)
|
|
|
|
#include "cutils/properties.h"
|
|
|
|
#endif
|
|
|
|
|
2012-09-13 09:37:14 -07:00
|
|
|
#if defined(MOZ_B2G_BT)
|
2013-11-17 22:29:45 -08:00
|
|
|
#if defined(MOZ_B2G_BT_BLUEZ)
|
|
|
|
/**
|
|
|
|
* B2G blueZ:
|
|
|
|
* MOZ_B2G_BT and MOZ_B2G_BT_BLUEZ are both defined.
|
|
|
|
*/
|
2014-02-13 07:28:07 -08:00
|
|
|
#include "BluetoothDBusService.h"
|
2013-11-17 22:29:45 -08:00
|
|
|
#elif defined(MOZ_B2G_BT_BLUEDROID)
|
|
|
|
/**
|
|
|
|
* B2G bluedroid:
|
|
|
|
* MOZ_B2G_BT and MOZ_B2G_BT_BLUEDROID are both defined;
|
|
|
|
* MOZ_B2G_BLUEZ is not defined.
|
|
|
|
*/
|
2013-10-22 23:12:24 -07:00
|
|
|
#include "BluetoothServiceBluedroid.h"
|
2013-10-22 23:12:24 -07:00
|
|
|
#endif
|
2013-11-17 22:29:45 -08:00
|
|
|
#elif defined(MOZ_BLUETOOTH_DBUS)
|
|
|
|
/**
|
|
|
|
* Desktop bluetooth:
|
|
|
|
* MOZ_B2G_BT is not defined; MOZ_BLUETOOTH_DBUS is defined.
|
|
|
|
*/
|
|
|
|
#include "BluetoothDBusService.h"
|
|
|
|
#else
|
|
|
|
#error No backend
|
2012-09-13 09:37:14 -07:00
|
|
|
#endif
|
2012-09-06 07:15:36 -07:00
|
|
|
|
2013-01-29 02:37:51 -08:00
|
|
|
#define MOZSETTINGS_CHANGED_ID "mozsettings-changed"
|
|
|
|
#define BLUETOOTH_ENABLED_SETTING "bluetooth.enabled"
|
|
|
|
#define BLUETOOTH_DEBUGGING_SETTING "bluetooth.debugging.enabled"
|
2012-07-17 20:41:54 -07:00
|
|
|
|
2013-02-05 23:28:04 -08:00
|
|
|
#define PROP_BLUETOOTH_ENABLED "bluetooth.isEnabled"
|
|
|
|
|
2013-08-30 11:18:43 -07:00
|
|
|
#define DEFAULT_THREAD_TIMEOUT_MS 3000
|
2012-09-13 09:37:14 -07:00
|
|
|
#define DEFAULT_SHUTDOWN_TIMER_MS 5000
|
2012-07-17 20:41:54 -07:00
|
|
|
|
2013-01-29 02:37:51 -08:00
|
|
|
bool gBluetoothDebugFlag = false;
|
|
|
|
|
2012-09-13 09:37:14 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
2012-07-17 20:41:54 -07:00
|
|
|
USING_BLUETOOTH_NAMESPACE
|
|
|
|
|
2012-09-13 09:37:14 -07:00
|
|
|
namespace {
|
2012-07-17 20:41:54 -07:00
|
|
|
|
2014-01-09 17:34:43 -08:00
|
|
|
StaticRefPtr<BluetoothService> sBluetoothService;
|
2012-09-13 09:37:14 -07:00
|
|
|
|
2014-01-09 17:34:43 -08:00
|
|
|
bool sInShutdown = false;
|
|
|
|
bool sToggleInProgress = false;
|
2012-09-13 09:37:14 -07:00
|
|
|
|
|
|
|
bool
|
|
|
|
IsMainProcess()
|
|
|
|
{
|
|
|
|
return XRE_GetProcessType() == GeckoProcessType_Default;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ShutdownTimeExceeded(nsITimer* aTimer, void* aClosure)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
*static_cast<bool*>(aClosure) = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GetAllBluetoothActors(InfallibleTArray<BluetoothParent*>& aActors)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aActors.IsEmpty());
|
|
|
|
|
|
|
|
nsAutoTArray<ContentParent*, 20> contentActors;
|
|
|
|
ContentParent::GetAll(contentActors);
|
|
|
|
|
|
|
|
for (uint32_t contentIndex = 0;
|
|
|
|
contentIndex < contentActors.Length();
|
|
|
|
contentIndex++) {
|
|
|
|
MOZ_ASSERT(contentActors[contentIndex]);
|
|
|
|
|
|
|
|
AutoInfallibleTArray<PBluetoothParent*, 5> bluetoothActors;
|
|
|
|
contentActors[contentIndex]->ManagedPBluetoothParent(bluetoothActors);
|
|
|
|
|
|
|
|
for (uint32_t bluetoothIndex = 0;
|
|
|
|
bluetoothIndex < bluetoothActors.Length();
|
|
|
|
bluetoothIndex++) {
|
|
|
|
MOZ_ASSERT(bluetoothActors[bluetoothIndex]);
|
|
|
|
|
|
|
|
BluetoothParent* actor =
|
|
|
|
static_cast<BluetoothParent*>(bluetoothActors[bluetoothIndex]);
|
|
|
|
aActors.AppendElement(actor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
2012-07-17 20:41:54 -07:00
|
|
|
|
2014-03-03 04:07:17 -08:00
|
|
|
BluetoothService::ToggleBtAck::ToggleBtAck(bool aEnabled)
|
|
|
|
: mEnabled(aEnabled)
|
2012-07-17 20:41:54 -07:00
|
|
|
{
|
2014-03-03 04:07:17 -08:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
}
|
2012-09-06 07:15:36 -07:00
|
|
|
|
2014-03-03 04:07:17 -08:00
|
|
|
NS_METHOD
|
|
|
|
BluetoothService::ToggleBtAck::Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-08-08 10:46:39 -07:00
|
|
|
|
2014-03-03 04:07:17 -08:00
|
|
|
// This is requested in Bug 836516. With settings this property, WLAN
|
|
|
|
// firmware could be aware of Bluetooth has been turned on/off, so that the
|
|
|
|
// mecahnism of handling coexistence of WIFI and Bluetooth could be started.
|
|
|
|
//
|
|
|
|
// In the future, we may have our own way instead of setting a system
|
|
|
|
// property to let firmware developers be able to sense that Bluetooth has
|
|
|
|
// been toggled.
|
2014-03-03 04:07:16 -08:00
|
|
|
#if defined(MOZ_WIDGET_GONK)
|
2014-03-03 04:07:17 -08:00
|
|
|
if (property_set(PROP_BLUETOOTH_ENABLED, mEnabled ? "true" : "false") != 0) {
|
|
|
|
BT_WARNING("Failed to set bluetooth enabled property");
|
|
|
|
}
|
2014-03-03 04:07:16 -08:00
|
|
|
#endif
|
|
|
|
|
2014-03-03 04:07:17 -08:00
|
|
|
NS_ENSURE_TRUE(sBluetoothService, NS_OK);
|
2012-07-17 20:41:54 -07:00
|
|
|
|
2014-03-03 04:07:17 -08:00
|
|
|
if (sInShutdown) {
|
|
|
|
sBluetoothService = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-11-20 20:58:04 -08:00
|
|
|
|
2014-03-03 04:07:17 -08:00
|
|
|
// Update mEnabled of BluetoothService object since
|
|
|
|
// StartInternal/StopInternal have been already done.
|
|
|
|
sBluetoothService->SetEnabled(mEnabled);
|
|
|
|
sToggleInProgress = false;
|
2013-11-19 23:01:54 -08:00
|
|
|
|
2014-03-03 04:07:17 -08:00
|
|
|
nsAutoString signalName;
|
|
|
|
signalName = mEnabled ? NS_LITERAL_STRING("Enabled")
|
|
|
|
: NS_LITERAL_STRING("Disabled");
|
|
|
|
BluetoothSignal signal(signalName, NS_LITERAL_STRING(KEY_MANAGER), true);
|
|
|
|
sBluetoothService->DistributeSignal(signal);
|
2013-11-27 00:24:51 -08:00
|
|
|
|
2014-03-03 04:07:17 -08:00
|
|
|
// Event 'AdapterAdded' has to be fired after firing 'Enabled'
|
|
|
|
sBluetoothService->TryFiringAdapterAdded();
|
2012-08-08 10:46:39 -07:00
|
|
|
|
2014-03-03 04:07:17 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-07-17 20:41:54 -07:00
|
|
|
|
2012-09-06 07:15:36 -07:00
|
|
|
class BluetoothService::ToggleBtTask : public nsRunnable
|
2012-07-17 20:41:54 -07:00
|
|
|
{
|
|
|
|
public:
|
2013-08-01 20:58:01 -07:00
|
|
|
ToggleBtTask(bool aEnabled, bool aIsStartup)
|
2012-09-06 07:15:36 -07:00
|
|
|
: mEnabled(aEnabled)
|
2013-08-01 20:58:01 -07:00
|
|
|
, mIsStartup(aIsStartup)
|
2012-07-17 20:41:54 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
}
|
|
|
|
|
2012-09-20 06:22:02 -07:00
|
|
|
NS_IMETHOD Run()
|
2012-07-17 20:41:54 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
|
2013-08-01 20:58:01 -07:00
|
|
|
/**
|
2012-11-30 01:46:55 -08:00
|
|
|
* mEnabled: expected status of bluetooth
|
2014-01-09 17:34:43 -08:00
|
|
|
* sBluetoothService->IsEnabled(): real status of bluetooth
|
2012-11-30 01:46:55 -08:00
|
|
|
*
|
2013-08-01 20:58:01 -07:00
|
|
|
* When two values are the same, we don't switch on/off bluetooth
|
|
|
|
* but we still do ToggleBtAck task. One special case happens at startup
|
|
|
|
* stage. At startup, the initialization of BluetoothService still has to
|
|
|
|
* be done even if mEnabled is equal to the status of Bluetooth firmware.
|
|
|
|
*
|
|
|
|
* Please see bug 892392 for more information.
|
2012-11-30 01:46:55 -08:00
|
|
|
*/
|
2014-01-09 17:34:43 -08:00
|
|
|
if (!mIsStartup && mEnabled == sBluetoothService->IsEnabledInternal()) {
|
2013-09-13 15:51:00 -07:00
|
|
|
BT_WARNING("Bluetooth has already been enabled/disabled before.");
|
2014-03-03 04:07:17 -08:00
|
|
|
nsCOMPtr<nsIRunnable> ackTask = new BluetoothService::ToggleBtAck(mEnabled);
|
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(ackTask))) {
|
|
|
|
BT_WARNING("Failed to dispatch to main thread!");
|
|
|
|
}
|
2012-11-30 01:46:55 -08:00
|
|
|
} else {
|
|
|
|
// Switch on/off bluetooth
|
|
|
|
if (mEnabled) {
|
2014-01-09 17:34:43 -08:00
|
|
|
if (NS_FAILED(sBluetoothService->StartInternal())) {
|
2013-09-13 15:51:00 -07:00
|
|
|
BT_WARNING("Bluetooth service failed to start!");
|
2012-11-30 01:46:55 -08:00
|
|
|
mEnabled = !mEnabled;
|
|
|
|
}
|
|
|
|
} else {
|
2014-01-09 17:34:43 -08:00
|
|
|
if (NS_FAILED(sBluetoothService->StopInternal())) {
|
2013-09-13 15:51:00 -07:00
|
|
|
BT_WARNING("Bluetooth service failed to stop!");
|
2012-11-30 01:46:55 -08:00
|
|
|
mEnabled = !mEnabled;
|
|
|
|
}
|
2012-07-17 20:41:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool mEnabled;
|
2013-08-01 20:58:01 -07:00
|
|
|
bool mIsStartup;
|
2012-07-17 20:41:54 -07:00
|
|
|
};
|
|
|
|
|
2012-09-06 07:15:36 -07:00
|
|
|
class BluetoothService::StartupTask : public nsISettingsServiceCallback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2014-01-09 09:39:36 -08:00
|
|
|
NS_IMETHOD Handle(const nsAString& aName, JS::Handle<JS::Value> aResult)
|
2012-09-06 07:15:36 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (!aResult.isBoolean()) {
|
2013-09-13 15:51:00 -07:00
|
|
|
BT_WARNING("Setting for '" BLUETOOTH_ENABLED_SETTING "' is not a boolean!");
|
2012-09-06 07:15:36 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-13 09:37:14 -07:00
|
|
|
// It is theoretically possible to shut down before the first settings check
|
|
|
|
// has completed (though extremely unlikely).
|
2014-01-09 17:34:43 -08:00
|
|
|
if (sBluetoothService) {
|
|
|
|
return sBluetoothService->HandleStartupSettingsCheck(aResult.toBoolean());
|
2012-09-13 09:37:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2012-09-06 07:15:36 -07:00
|
|
|
}
|
|
|
|
|
2012-11-14 13:46:38 -08:00
|
|
|
NS_IMETHOD HandleError(const nsAString& aName)
|
2012-09-06 07:15:36 -07:00
|
|
|
{
|
2013-09-13 15:51:00 -07:00
|
|
|
BT_WARNING("Unable to get value for '" BLUETOOTH_ENABLED_SETTING "'");
|
2012-09-06 07:15:36 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(BluetoothService::StartupTask, nsISettingsServiceCallback);
|
|
|
|
|
2012-09-13 09:37:14 -07:00
|
|
|
NS_IMPL_ISUPPORTS1(BluetoothService, nsIObserver)
|
|
|
|
|
2012-11-30 01:46:55 -08:00
|
|
|
bool
|
|
|
|
BluetoothService::IsToggling() const
|
|
|
|
{
|
2014-01-09 17:34:43 -08:00
|
|
|
return sToggleInProgress;
|
2012-11-30 01:46:55 -08:00
|
|
|
}
|
|
|
|
|
2012-09-12 07:03:42 -07:00
|
|
|
BluetoothService::~BluetoothService()
|
|
|
|
{
|
2012-09-13 09:37:14 -07:00
|
|
|
Cleanup();
|
|
|
|
}
|
|
|
|
|
2013-01-09 07:51:53 -08:00
|
|
|
PLDHashOperator
|
|
|
|
RemoveObserversExceptBluetoothManager
|
|
|
|
(const nsAString& key,
|
|
|
|
nsAutoPtr<BluetoothSignalObserverList>& value,
|
|
|
|
void* arg)
|
|
|
|
{
|
2013-03-15 00:15:47 -07:00
|
|
|
if (!key.EqualsLiteral(KEY_MANAGER)) {
|
2013-02-14 16:45:13 -08:00
|
|
|
return PL_DHASH_REMOVE;
|
2013-01-09 07:51:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothService::RemoveObserverFromTable(const nsAString& key)
|
|
|
|
{
|
|
|
|
mBluetoothSignalObserverTable.Remove(key);
|
|
|
|
}
|
|
|
|
|
2012-09-13 09:37:14 -07:00
|
|
|
// static
|
|
|
|
BluetoothService*
|
|
|
|
BluetoothService::Create()
|
|
|
|
{
|
|
|
|
#if defined(MOZ_B2G_BT)
|
|
|
|
if (!IsMainProcess()) {
|
|
|
|
return BluetoothServiceChildProcess::Create();
|
2012-09-12 07:03:42 -07:00
|
|
|
}
|
2012-09-13 09:37:14 -07:00
|
|
|
|
2013-11-17 22:29:45 -08:00
|
|
|
#if defined(MOZ_B2G_BT_BLUEZ)
|
2014-02-13 07:28:07 -08:00
|
|
|
return new BluetoothDBusService();
|
2013-11-17 22:29:45 -08:00
|
|
|
#elif defined(MOZ_B2G_BT_BLUEDROID)
|
|
|
|
return new BluetoothServiceBluedroid();
|
2013-10-22 23:12:24 -07:00
|
|
|
#endif
|
2012-09-13 09:37:14 -07:00
|
|
|
#elif defined(MOZ_BLUETOOTH_DBUS)
|
|
|
|
return new BluetoothDBusService();
|
2013-10-22 23:12:24 -07:00
|
|
|
#endif
|
2013-11-17 22:29:45 -08:00
|
|
|
|
2013-09-13 15:51:00 -07:00
|
|
|
BT_WARNING("No platform support for bluetooth!");
|
2012-09-13 09:37:14 -07:00
|
|
|
return nullptr;
|
|
|
|
}
|
2012-09-12 07:03:42 -07:00
|
|
|
|
2012-09-13 09:37:14 -07:00
|
|
|
bool
|
|
|
|
BluetoothService::Init()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
|
|
|
NS_ENSURE_TRUE(obs, false);
|
|
|
|
|
|
|
|
if (NS_FAILED(obs->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID,
|
|
|
|
false))) {
|
2013-09-13 15:51:00 -07:00
|
|
|
BT_WARNING("Failed to add shutdown observer!");
|
2012-09-13 09:37:14 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only the main process should observe bluetooth settings changes.
|
|
|
|
if (IsMainProcess() &&
|
|
|
|
NS_FAILED(obs->AddObserver(this, MOZSETTINGS_CHANGED_ID, false))) {
|
2013-09-13 15:51:00 -07:00
|
|
|
BT_WARNING("Failed to add settings change observer!");
|
2012-09-13 09:37:14 -07:00
|
|
|
return false;
|
2012-09-12 07:03:42 -07:00
|
|
|
}
|
2012-09-13 09:37:14 -07:00
|
|
|
|
|
|
|
return true;
|
2012-09-12 07:03:42 -07:00
|
|
|
}
|
|
|
|
|
2012-09-13 09:37:14 -07:00
|
|
|
void
|
|
|
|
BluetoothService::Cleanup()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
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)))) {
|
2013-09-13 15:51:00 -07:00
|
|
|
BT_WARNING("Can't unregister observers!");
|
2012-09-13 09:37:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-28 22:52:58 -08:00
|
|
|
BluetoothService::RegisterBluetoothSignalHandler(
|
|
|
|
const nsAString& aNodeName,
|
|
|
|
BluetoothSignalObserver* aHandler)
|
2012-07-17 20:41:54 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-09-13 09:37:14 -07:00
|
|
|
MOZ_ASSERT(aHandler);
|
|
|
|
|
2013-09-13 15:51:00 -07:00
|
|
|
BT_LOGD("[S] %s: %s", __FUNCTION__, NS_ConvertUTF16toUTF8(aNodeName).get());
|
2013-01-29 02:37:51 -08:00
|
|
|
|
2012-07-17 20:41:54 -07:00
|
|
|
BluetoothSignalObserverList* ol;
|
|
|
|
if (!mBluetoothSignalObserverTable.Get(aNodeName, &ol)) {
|
|
|
|
ol = new BluetoothSignalObserverList();
|
|
|
|
mBluetoothSignalObserverTable.Put(aNodeName, ol);
|
|
|
|
}
|
2013-01-09 07:51:53 -08:00
|
|
|
|
2012-07-17 20:41:54 -07:00
|
|
|
ol->AddObserver(aHandler);
|
|
|
|
}
|
|
|
|
|
2012-09-13 09:37:14 -07:00
|
|
|
void
|
2013-01-28 22:52:58 -08:00
|
|
|
BluetoothService::UnregisterBluetoothSignalHandler(
|
|
|
|
const nsAString& aNodeName,
|
|
|
|
BluetoothSignalObserver* aHandler)
|
2012-07-17 20:41:54 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-09-13 09:37:14 -07:00
|
|
|
MOZ_ASSERT(aHandler);
|
|
|
|
|
2013-09-13 15:51:00 -07:00
|
|
|
BT_LOGD("[S] %s: %s", __FUNCTION__, NS_ConvertUTF16toUTF8(aNodeName).get());
|
2013-01-29 02:37:51 -08:00
|
|
|
|
2012-07-17 20:41:54 -07:00
|
|
|
BluetoothSignalObserverList* ol;
|
2012-09-13 09:37:14 -07:00
|
|
|
if (mBluetoothSignalObserverTable.Get(aNodeName, &ol)) {
|
|
|
|
ol->RemoveObserver(aHandler);
|
2013-04-19 01:40:56 -07:00
|
|
|
// We shouldn't have duplicate instances in the ObserverList, but there's
|
|
|
|
// no appropriate way to do duplication check while registering, so
|
|
|
|
// assertions are added here.
|
|
|
|
MOZ_ASSERT(!ol->RemoveObserver(aHandler));
|
2012-09-13 09:37:14 -07:00
|
|
|
if (ol->Length() == 0) {
|
|
|
|
mBluetoothSignalObserverTable.Remove(aNodeName);
|
|
|
|
}
|
2012-07-17 20:41:54 -07:00
|
|
|
}
|
2012-09-13 09:37:14 -07:00
|
|
|
else {
|
2013-09-13 15:51:00 -07:00
|
|
|
BT_WARNING("Node was never registered!");
|
2012-07-17 20:41:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-05 17:35:29 -08:00
|
|
|
PLDHashOperator
|
|
|
|
RemoveAllSignalHandlers(const nsAString& aKey,
|
|
|
|
nsAutoPtr<BluetoothSignalObserverList>& aData,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
2013-04-19 01:40:56 -07:00
|
|
|
BluetoothSignalObserver* handler = static_cast<BluetoothSignalObserver*>(aUserArg);
|
|
|
|
aData->RemoveObserver(handler);
|
|
|
|
// We shouldn't have duplicate instances in the ObserverList, but there's
|
|
|
|
// no appropriate way to do duplication check while registering, so
|
|
|
|
// assertions are added here.
|
|
|
|
MOZ_ASSERT(!aData->RemoveObserver(handler));
|
2013-02-05 17:35:29 -08:00
|
|
|
return aData->Length() ? PL_DHASH_NEXT : PL_DHASH_REMOVE;
|
|
|
|
}
|
|
|
|
|
2012-09-13 09:37:14 -07:00
|
|
|
void
|
|
|
|
BluetoothService::UnregisterAllSignalHandlers(BluetoothSignalObserver* aHandler)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aHandler);
|
|
|
|
|
2013-02-05 17:35:29 -08:00
|
|
|
mBluetoothSignalObserverTable.Enumerate(RemoveAllSignalHandlers, aHandler);
|
2012-09-13 09:37:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothService::DistributeSignal(const BluetoothSignal& aSignal)
|
2012-07-17 20:41:54 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-11-20 23:33:07 -08:00
|
|
|
|
2013-03-15 00:15:47 -07:00
|
|
|
if (aSignal.path().EqualsLiteral(KEY_LOCAL_AGENT)) {
|
2012-11-20 23:33:07 -08:00
|
|
|
Notify(aSignal);
|
|
|
|
return;
|
2013-03-15 00:15:47 -07:00
|
|
|
} else if (aSignal.path().EqualsLiteral(KEY_REMOTE_AGENT)) {
|
2012-11-20 23:33:07 -08:00
|
|
|
Notify(aSignal);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-17 20:41:54 -07:00
|
|
|
BluetoothSignalObserverList* ol;
|
2012-09-13 09:37:14 -07:00
|
|
|
if (!mBluetoothSignalObserverTable.Get(aSignal.path(), &ol)) {
|
2012-09-05 20:06:20 -07:00
|
|
|
#if DEBUG
|
2012-09-13 09:37:14 -07:00
|
|
|
nsAutoCString msg("No observer registered for path ");
|
|
|
|
msg.Append(NS_ConvertUTF16toUTF8(aSignal.path()));
|
2013-09-13 15:51:00 -07:00
|
|
|
BT_WARNING(msg.get());
|
2012-09-05 20:06:20 -07:00
|
|
|
#endif
|
2012-09-13 09:37:14 -07:00
|
|
|
return;
|
2012-09-05 20:06:20 -07:00
|
|
|
}
|
2012-09-13 09:37:14 -07:00
|
|
|
MOZ_ASSERT(ol->Length());
|
|
|
|
ol->Broadcast(aSignal);
|
2012-07-17 20:41:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-08-01 20:58:01 -07:00
|
|
|
BluetoothService::StartStopBluetooth(bool aStart, bool aIsStartup)
|
2012-07-17 20:41:54 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2014-01-09 17:34:43 -08:00
|
|
|
if (sInShutdown) {
|
2012-09-06 07:15:36 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-08-30 11:18:43 -07:00
|
|
|
if (!mBluetoothThread) {
|
2012-09-06 07:15:36 -07:00
|
|
|
// Don't create a new thread after we've begun shutdown since bluetooth
|
|
|
|
// can't be running.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-08 02:51:37 -07:00
|
|
|
if (!aStart) {
|
2013-09-06 04:20:27 -07:00
|
|
|
BluetoothProfileManagerBase* profile;
|
|
|
|
profile = BluetoothHfpManager::Get();
|
|
|
|
NS_ENSURE_TRUE(profile, NS_ERROR_FAILURE);
|
|
|
|
if (profile->IsConnected()) {
|
|
|
|
profile->Disconnect(nullptr);
|
2014-01-29 03:13:10 -08:00
|
|
|
} else {
|
|
|
|
profile->Reset();
|
2013-09-06 04:20:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
profile = BluetoothOppManager::Get();
|
|
|
|
NS_ENSURE_TRUE(profile, NS_ERROR_FAILURE);
|
|
|
|
if (profile->IsConnected()) {
|
|
|
|
profile->Disconnect(nullptr);
|
|
|
|
}
|
2013-08-08 02:51:37 -07:00
|
|
|
|
2013-09-06 04:20:27 -07:00
|
|
|
profile = BluetoothA2dpManager::Get();
|
|
|
|
NS_ENSURE_TRUE(profile, NS_ERROR_FAILURE);
|
|
|
|
if (profile->IsConnected()) {
|
|
|
|
profile->Disconnect(nullptr);
|
2014-01-29 03:13:10 -08:00
|
|
|
} else {
|
|
|
|
profile->Reset();
|
2013-09-06 04:20:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
profile = BluetoothHidManager::Get();
|
|
|
|
NS_ENSURE_TRUE(profile, NS_ERROR_FAILURE);
|
|
|
|
if (profile->IsConnected()) {
|
|
|
|
profile->Disconnect(nullptr);
|
2014-01-29 03:13:10 -08:00
|
|
|
} else {
|
|
|
|
profile->Reset();
|
2013-09-06 04:20:27 -07:00
|
|
|
}
|
2014-01-29 03:13:10 -08:00
|
|
|
|
2013-08-08 02:51:37 -07:00
|
|
|
}
|
|
|
|
|
2013-08-30 11:18:43 -07:00
|
|
|
if (!mBluetoothThread) {
|
|
|
|
mBluetoothThread = new LazyIdleThread(DEFAULT_THREAD_TIMEOUT_MS,
|
|
|
|
NS_LITERAL_CSTRING("Bluetooth"),
|
|
|
|
LazyIdleThread::ManualShutdown);
|
|
|
|
}
|
|
|
|
|
2013-11-27 00:24:51 -08:00
|
|
|
mAdapterAddedReceived = false;
|
|
|
|
|
2013-08-01 20:58:01 -07:00
|
|
|
nsCOMPtr<nsIRunnable> runnable = new ToggleBtTask(aStart, aIsStartup);
|
2013-08-30 11:18:43 -07:00
|
|
|
nsresult rv = mBluetoothThread->Dispatch(runnable, NS_DISPATCH_NORMAL);
|
2012-09-06 07:15:36 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothService::SetEnabled(bool aEnabled)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2012-09-13 09:37:14 -07:00
|
|
|
AutoInfallibleTArray<BluetoothParent*, 10> childActors;
|
|
|
|
GetAllBluetoothActors(childActors);
|
|
|
|
|
|
|
|
for (uint32_t index = 0; index < childActors.Length(); index++) {
|
|
|
|
unused << childActors[index]->SendEnabled(aEnabled);
|
|
|
|
}
|
|
|
|
|
2013-02-08 02:03:09 -08:00
|
|
|
if (!aEnabled) {
|
2013-01-09 07:51:53 -08:00
|
|
|
/**
|
|
|
|
* Remove all handlers except BluetoothManager when turning off bluetooth
|
|
|
|
* since it is possible that the event 'onAdapterAdded' would be fired after
|
|
|
|
* BluetoothManagers of child process are registered. Please see Bug 827759
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
mBluetoothSignalObserverTable.Enumerate(
|
2013-02-14 16:45:13 -08:00
|
|
|
RemoveObserversExceptBluetoothManager, nullptr);
|
2012-10-08 20:29:37 -07:00
|
|
|
}
|
|
|
|
|
2012-11-30 01:46:55 -08:00
|
|
|
/**
|
|
|
|
* mEnabled: real status of bluetooth
|
|
|
|
* aEnabled: expected status of bluetooth
|
|
|
|
*/
|
|
|
|
if (mEnabled == aEnabled) {
|
2014-02-20 02:15:47 -08:00
|
|
|
BT_WARNING("Bluetooth has already been enabled/disabled before "
|
2014-01-09 17:34:43 -08:00
|
|
|
"or the toggling is failed.");
|
2012-11-30 01:46:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
mEnabled = aEnabled;
|
2012-09-06 07:15:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
BluetoothService::HandleStartup()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-01-09 17:34:43 -08:00
|
|
|
MOZ_ASSERT(!sToggleInProgress);
|
2012-09-06 07:15:36 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsISettingsService> settings =
|
|
|
|
do_GetService("@mozilla.org/settingsService;1");
|
|
|
|
NS_ENSURE_TRUE(settings, NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
nsCOMPtr<nsISettingsServiceLock> settingsLock;
|
2014-02-07 03:19:58 -08:00
|
|
|
nsresult rv = settings->CreateLock(nullptr, getter_AddRefs(settingsLock));
|
2012-09-06 07:15:36 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsRefPtr<StartupTask> callback = new StartupTask();
|
|
|
|
rv = settingsLock->Get(BLUETOOTH_ENABLED_SETTING, callback);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2014-01-09 17:34:43 -08:00
|
|
|
sToggleInProgress = true;
|
2012-09-13 09:37:14 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
BluetoothService::HandleStartupSettingsCheck(bool aEnable)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-08-01 20:58:01 -07:00
|
|
|
return StartStopBluetooth(aEnable, true);
|
2012-07-17 20:41:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-09-06 07:15:36 -07:00
|
|
|
BluetoothService::HandleSettingsChanged(const nsAString& aData)
|
2012-07-17 20:41:54 -07:00
|
|
|
{
|
2012-09-06 07:15:36 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
// The string that we're interested in will be a JSON string that looks like:
|
|
|
|
// {"key":"bluetooth.enabled","value":true}
|
|
|
|
|
2013-05-30 07:54:46 -07:00
|
|
|
AutoSafeJSContext cx;
|
2012-09-06 07:15:36 -07:00
|
|
|
if (!cx) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-05-29 01:29:39 -07:00
|
|
|
JS::Rooted<JS::Value> val(cx);
|
2012-09-06 07:15:36 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-01-31 01:55:20 -08:00
|
|
|
JS::Rooted<JSObject*> obj(cx, &val.toObject());
|
2012-09-06 07:15:36 -07:00
|
|
|
|
2013-07-26 02:00:38 -07:00
|
|
|
JS::Rooted<JS::Value> key(cx);
|
2014-01-31 01:55:20 -08:00
|
|
|
if (!JS_GetProperty(cx, obj, "key", &key)) {
|
2012-09-06 07:15:36 -07:00
|
|
|
MOZ_ASSERT(!JS_IsExceptionPending(cx));
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!key.isString()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-01-29 02:37:51 -08:00
|
|
|
// First, check if the string equals to BLUETOOTH_DEBUGGING_SETTING
|
2013-08-08 15:53:04 -07:00
|
|
|
bool match;
|
2013-01-29 02:37:51 -08:00
|
|
|
if (!JS_StringEqualsAscii(cx, key.toString(), BLUETOOTH_DEBUGGING_SETTING, &match)) {
|
2012-09-06 07:15:36 -07:00
|
|
|
MOZ_ASSERT(!JS_IsExceptionPending(cx));
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2013-01-29 02:37:51 -08:00
|
|
|
if (match) {
|
2013-07-26 02:00:38 -07:00
|
|
|
JS::Rooted<JS::Value> value(cx);
|
2014-01-31 01:55:20 -08:00
|
|
|
if (!JS_GetProperty(cx, obj, "value", &value)) {
|
2013-01-29 02:37:51 -08:00
|
|
|
MOZ_ASSERT(!JS_IsExceptionPending(cx));
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!value.isBoolean()) {
|
|
|
|
MOZ_ASSERT(false, "Expecting a boolean for 'bluetooth.debugging.enabled'!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_BT_DEBUG(value.toBoolean());
|
|
|
|
|
2012-09-06 07:15:36 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-01-29 02:37:51 -08:00
|
|
|
// Second, check if the string is BLUETOOTH_ENABLED_SETTING
|
|
|
|
if (!JS_StringEqualsAscii(cx, key.toString(), BLUETOOTH_ENABLED_SETTING, &match)) {
|
2012-09-06 07:15:36 -07:00
|
|
|
MOZ_ASSERT(!JS_IsExceptionPending(cx));
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2013-01-29 02:37:51 -08:00
|
|
|
if (match) {
|
2013-07-26 02:00:38 -07:00
|
|
|
JS::Rooted<JS::Value> value(cx);
|
2014-01-31 01:55:20 -08:00
|
|
|
if (!JS_GetProperty(cx, obj, "value", &value)) {
|
2013-01-29 02:37:51 -08:00
|
|
|
MOZ_ASSERT(!JS_IsExceptionPending(cx));
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2012-09-06 07:15:36 -07:00
|
|
|
|
2013-01-29 02:37:51 -08:00
|
|
|
if (!value.isBoolean()) {
|
|
|
|
MOZ_ASSERT(false, "Expecting a boolean for 'bluetooth.enabled'!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2012-09-06 07:15:36 -07:00
|
|
|
|
2014-01-09 17:34:43 -08:00
|
|
|
if (sToggleInProgress || value.toBoolean() == IsEnabled()) {
|
2013-01-29 02:37:51 -08:00
|
|
|
// Nothing to do here.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-01-28 03:23:02 -08:00
|
|
|
|
2014-01-09 17:34:43 -08:00
|
|
|
sToggleInProgress = true;
|
2013-01-29 02:37:51 -08:00
|
|
|
|
2013-08-01 20:58:01 -07:00
|
|
|
nsresult rv = StartStopBluetooth(value.toBoolean(), false);
|
2013-01-29 02:37:51 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2012-09-06 07:15:36 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
2012-07-17 20:41:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-09-06 07:15:36 -07:00
|
|
|
BluetoothService::HandleShutdown()
|
2012-07-17 20:41:54 -07:00
|
|
|
{
|
2012-09-06 07:15:36 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2012-09-13 09:37:14 -07:00
|
|
|
// This is a two phase shutdown. First we notify all child processes that
|
|
|
|
// bluetooth is going away, and then we wait for them to acknowledge. Then we
|
|
|
|
// close down all the bluetooth machinery.
|
|
|
|
|
2014-01-09 17:34:43 -08:00
|
|
|
sInShutdown = true;
|
2012-09-06 07:15:36 -07:00
|
|
|
|
2012-09-13 09:37:14 -07:00
|
|
|
Cleanup();
|
|
|
|
|
|
|
|
AutoInfallibleTArray<BluetoothParent*, 10> childActors;
|
|
|
|
GetAllBluetoothActors(childActors);
|
|
|
|
|
|
|
|
if (!childActors.IsEmpty()) {
|
|
|
|
// Notify child processes that they should stop using bluetooth now.
|
|
|
|
for (uint32_t index = 0; index < childActors.Length(); index++) {
|
|
|
|
childActors[index]->BeginShutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a timer to ensure that we don't wait forever for a child process
|
|
|
|
// or the bluetooth threads to finish. If we don't get a timer or can't use
|
|
|
|
// it for some reason then we skip all the waiting entirely since we really
|
|
|
|
// can't afford to hang on shutdown.
|
|
|
|
nsCOMPtr<nsITimer> timer = do_CreateInstance(NS_TIMER_CONTRACTID);
|
|
|
|
MOZ_ASSERT(timer);
|
|
|
|
|
|
|
|
if (timer) {
|
|
|
|
bool timeExceeded = false;
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(timer->InitWithFuncCallback(ShutdownTimeExceeded,
|
|
|
|
&timeExceeded,
|
|
|
|
DEFAULT_SHUTDOWN_TIMER_MS,
|
|
|
|
nsITimer::TYPE_ONE_SHOT))) {
|
|
|
|
nsIThread* currentThread = NS_GetCurrentThread();
|
|
|
|
MOZ_ASSERT(currentThread);
|
|
|
|
|
|
|
|
// Wait for those child processes to acknowledge.
|
|
|
|
while (!timeExceeded && !childActors.IsEmpty()) {
|
|
|
|
if (!NS_ProcessNextEvent(currentThread)) {
|
|
|
|
MOZ_ASSERT(false, "Something horribly wrong here!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
GetAllBluetoothActors(childActors);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(timer->Cancel())) {
|
2013-06-28 18:38:30 -07:00
|
|
|
MOZ_CRASH("Failed to cancel shutdown timer, this will crash!");
|
2012-09-13 09:37:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
MOZ_ASSERT(false, "Failed to initialize shutdown timer!");
|
|
|
|
}
|
|
|
|
}
|
2012-09-06 07:15:36 -07:00
|
|
|
}
|
|
|
|
|
2013-08-01 20:58:01 -07:00
|
|
|
if (IsEnabled() && NS_FAILED(StartStopBluetooth(false, false))) {
|
2012-09-13 09:37:14 -07:00
|
|
|
MOZ_ASSERT(false, "Failed to deliver stop message!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2012-09-06 07:15:36 -07:00
|
|
|
}
|
|
|
|
|
2012-07-17 20:41:54 -07:00
|
|
|
// static
|
|
|
|
BluetoothService*
|
|
|
|
BluetoothService::Get()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
// If we already exist, exit early
|
2014-01-09 17:34:43 -08:00
|
|
|
if (sBluetoothService) {
|
|
|
|
return sBluetoothService;
|
2012-07-17 20:41:54 -07:00
|
|
|
}
|
2012-09-06 07:15:36 -07:00
|
|
|
|
2012-07-17 20:41:54 -07:00
|
|
|
// If we're in shutdown, don't create a new instance
|
2014-01-09 17:34:43 -08:00
|
|
|
if (sInShutdown) {
|
2013-09-13 15:51:00 -07:00
|
|
|
BT_WARNING("BluetoothService can't be created during shutdown");
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-07-17 20:41:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create new instance, register, return
|
2012-09-06 07:15:36 -07:00
|
|
|
nsRefPtr<BluetoothService> service = BluetoothService::Create();
|
|
|
|
NS_ENSURE_TRUE(service, nullptr);
|
|
|
|
|
2012-09-13 09:37:14 -07:00
|
|
|
if (!service->Init()) {
|
|
|
|
service->Cleanup();
|
2012-09-12 07:03:42 -07:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-01-09 17:34:43 -08:00
|
|
|
sBluetoothService = service;
|
|
|
|
return sBluetoothService;
|
2012-07-17 20:41:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
BluetoothService::Observe(nsISupports* aSubject, const char* aTopic,
|
2014-01-04 07:02:17 -08:00
|
|
|
const char16_t* aData)
|
2012-07-17 20:41:54 -07:00
|
|
|
{
|
2012-09-06 07:15:36 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (!strcmp(aTopic, "profile-after-change")) {
|
|
|
|
return HandleStartup();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(aTopic, MOZSETTINGS_CHANGED_ID)) {
|
|
|
|
return HandleSettingsChanged(nsDependentString(aData));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
|
|
|
|
return HandleShutdown();
|
2012-07-17 20:41:54 -07:00
|
|
|
}
|
|
|
|
|
2012-09-06 07:15:36 -07:00
|
|
|
MOZ_ASSERT(false, "BluetoothService got unexpected topic!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2012-07-17 20:41:54 -07:00
|
|
|
}
|
2012-09-12 07:03:42 -07:00
|
|
|
|
2013-11-27 00:24:51 -08:00
|
|
|
void
|
|
|
|
BluetoothService::TryFiringAdapterAdded()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (IsToggling() || !mAdapterAddedReceived) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
BluetoothSignal signal(NS_LITERAL_STRING("AdapterAdded"),
|
|
|
|
NS_LITERAL_STRING(KEY_MANAGER), true);
|
|
|
|
DistributeSignal(signal);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothService::AdapterAddedReceived()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
mAdapterAddedReceived = true;
|
|
|
|
}
|
|
|
|
|
2012-09-12 07:03:42 -07:00
|
|
|
void
|
|
|
|
BluetoothService::Notify(const BluetoothSignal& aData)
|
|
|
|
{
|
2013-05-20 00:34:45 -07:00
|
|
|
nsString type = NS_LITERAL_STRING("bluetooth-pairing-request");
|
2012-09-12 07:03:42 -07:00
|
|
|
|
2013-05-22 09:05:27 -07:00
|
|
|
AutoSafeJSContext cx;
|
2014-01-16 09:48:58 -08:00
|
|
|
JS::Rooted<JSObject*> obj(cx, JS_NewObject(cx, nullptr, JS::NullPtr(),
|
|
|
|
JS::NullPtr()));
|
2013-06-19 21:14:09 -07:00
|
|
|
NS_ENSURE_TRUE_VOID(obj);
|
2012-09-12 07:03:42 -07:00
|
|
|
|
2013-01-28 22:52:39 -08:00
|
|
|
if (!SetJsObject(cx, aData.value(), obj)) {
|
2013-09-13 15:51:00 -07:00
|
|
|
BT_WARNING("Failed to set properties of system message!");
|
2012-09-12 07:03:42 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-13 15:51:00 -07:00
|
|
|
BT_LOGD("[S] %s: %s", __FUNCTION__, NS_ConvertUTF16toUTF8(aData.name()).get());
|
2013-01-29 02:37:51 -08:00
|
|
|
|
2012-09-12 07:03:42 -07:00
|
|
|
if (aData.name().EqualsLiteral("RequestConfirmation")) {
|
2013-05-26 18:45:06 -07:00
|
|
|
MOZ_ASSERT(aData.value().get_ArrayOfBluetoothNamedValue().Length() == 4,
|
2013-01-28 22:52:39 -08:00
|
|
|
"RequestConfirmation: Wrong length of parameters");
|
2012-09-12 07:03:42 -07:00
|
|
|
} else if (aData.name().EqualsLiteral("RequestPinCode")) {
|
2013-05-26 18:45:06 -07:00
|
|
|
MOZ_ASSERT(aData.value().get_ArrayOfBluetoothNamedValue().Length() == 3,
|
2013-01-28 22:52:39 -08:00
|
|
|
"RequestPinCode: Wrong length of parameters");
|
2012-09-12 07:03:42 -07:00
|
|
|
} else if (aData.name().EqualsLiteral("RequestPasskey")) {
|
2013-05-26 18:45:06 -07:00
|
|
|
MOZ_ASSERT(aData.value().get_ArrayOfBluetoothNamedValue().Length() == 3,
|
2013-01-28 22:52:39 -08:00
|
|
|
"RequestPinCode: Wrong length of parameters");
|
2012-09-12 07:03:42 -07:00
|
|
|
} else if (aData.name().EqualsLiteral("Cancel")) {
|
2013-05-26 18:45:06 -07:00
|
|
|
MOZ_ASSERT(aData.value().get_ArrayOfBluetoothNamedValue().Length() == 0,
|
2013-01-28 22:52:39 -08:00
|
|
|
"Cancel: Wrong length of parameters");
|
2012-09-12 07:03:42 -07:00
|
|
|
type.AssignLiteral("bluetooth-cancel");
|
2013-08-02 03:32:57 -07:00
|
|
|
} else if (aData.name().EqualsLiteral(PAIRED_STATUS_CHANGED_ID)) {
|
2013-05-26 18:45:06 -07:00
|
|
|
MOZ_ASSERT(aData.value().get_ArrayOfBluetoothNamedValue().Length() == 1,
|
2013-08-02 03:32:57 -07:00
|
|
|
"pairedstatuschanged: Wrong length of parameters");
|
2012-09-29 19:45:21 -07:00
|
|
|
type.AssignLiteral("bluetooth-pairedstatuschanged");
|
2012-09-12 07:03:42 -07:00
|
|
|
} else {
|
|
|
|
nsCString warningMsg;
|
|
|
|
warningMsg.AssignLiteral("Not handling service signal: ");
|
|
|
|
warningMsg.Append(NS_ConvertUTF16toUTF8(aData.name()));
|
2013-09-13 15:51:00 -07:00
|
|
|
BT_WARNING(warningMsg.get());
|
2013-05-20 00:34:45 -07:00
|
|
|
return;
|
2012-09-12 07:03:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISystemMessagesInternal> systemMessenger =
|
|
|
|
do_GetService("@mozilla.org/system-message-internal;1");
|
2012-11-20 23:33:07 -08:00
|
|
|
NS_ENSURE_TRUE_VOID(systemMessenger);
|
2012-09-12 07:03:42 -07:00
|
|
|
|
2014-01-09 09:39:36 -08:00
|
|
|
JS::Rooted<JS::Value> value(cx, JS::ObjectValue(*obj));
|
|
|
|
systemMessenger->BroadcastMessage(type, value,
|
|
|
|
JS::UndefinedHandleValue);
|
2012-09-12 07:03:42 -07:00
|
|
|
}
|