2012-07-18 07:41:54 +04:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
2012-08-21 07:21:24 +04:00
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
2012-07-18 07:41:54 +04: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 20:37:14 +04:00
|
|
|
|
2013-03-15 11:15:47 +04:00
|
|
|
#include "BluetoothCommon.h"
|
2013-09-06 15:20:27 +04:00
|
|
|
#include "BluetoothA2dpManager.h"
|
2013-08-08 13:51:37 +04:00
|
|
|
#include "BluetoothHfpManager.h"
|
2013-09-06 15:20:27 +04:00
|
|
|
#include "BluetoothHidManager.h"
|
2012-09-06 18:15:36 +04:00
|
|
|
#include "BluetoothManager.h"
|
2013-08-08 13:51:37 +04:00
|
|
|
#include "BluetoothOppManager.h"
|
2012-09-13 20:37:14 +04:00
|
|
|
#include "BluetoothParent.h"
|
2012-07-18 07:41:54 +04:00
|
|
|
#include "BluetoothReplyRunnable.h"
|
2012-09-13 20:37:14 +04:00
|
|
|
#include "BluetoothServiceChildProcess.h"
|
2012-11-21 11:33:07 +04:00
|
|
|
#include "BluetoothUtils.h"
|
2012-07-18 07:41:54 +04:00
|
|
|
|
2012-09-06 18:15:36 +04:00
|
|
|
#include "jsapi.h"
|
2014-08-07 10:01:24 +04:00
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2012-09-06 18:15:36 +04:00
|
|
|
#include "mozilla/Services.h"
|
2012-09-13 20:37:14 +04:00
|
|
|
#include "mozilla/StaticPtr.h"
|
|
|
|
#include "mozilla/unused.h"
|
|
|
|
#include "mozilla/dom/ContentParent.h"
|
|
|
|
#include "mozilla/dom/bluetooth/BluetoothTypes.h"
|
2014-09-27 03:21:57 +04:00
|
|
|
#include "mozilla/dom/ipc/BlobChild.h"
|
|
|
|
#include "mozilla/dom/ipc/BlobParent.h"
|
2012-09-26 00:13:15 +04:00
|
|
|
#include "mozilla/ipc/UnixSocket.h"
|
2012-09-06 18:15:36 +04:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsISettingsService.h"
|
2012-09-13 20:37:14 +04:00
|
|
|
#include "nsISystemMessagesInternal.h"
|
|
|
|
#include "nsITimer.h"
|
2013-09-11 00:56:05 +04:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2012-09-06 18:15:36 +04:00
|
|
|
#include "nsXPCOM.h"
|
2014-09-16 21:15:16 +04:00
|
|
|
#include "mozilla/dom/SettingChangeNotificationBinding.h"
|
2012-09-13 20:37:14 +04:00
|
|
|
|
2013-02-06 11:28:04 +04:00
|
|
|
#if defined(MOZ_WIDGET_GONK)
|
|
|
|
#include "cutils/properties.h"
|
|
|
|
#endif
|
|
|
|
|
2012-09-13 20:37:14 +04:00
|
|
|
#if defined(MOZ_B2G_BT)
|
2013-11-18 10:29:45 +04:00
|
|
|
#if defined(MOZ_B2G_BT_BLUEZ)
|
|
|
|
/**
|
|
|
|
* B2G blueZ:
|
|
|
|
* MOZ_B2G_BT and MOZ_B2G_BT_BLUEZ are both defined.
|
|
|
|
*/
|
2014-02-13 19:28:07 +04:00
|
|
|
#include "BluetoothDBusService.h"
|
2013-11-18 10:29:45 +04:00
|
|
|
#elif defined(MOZ_B2G_BT_BLUEDROID)
|
|
|
|
/**
|
|
|
|
* B2G bluedroid:
|
|
|
|
* MOZ_B2G_BT and MOZ_B2G_BT_BLUEDROID are both defined;
|
2014-11-03 15:03:49 +03:00
|
|
|
* MOZ_B2G_BLUEZ or MOZ_B2G_DAEMON are not defined.
|
|
|
|
*/
|
|
|
|
#include "BluetoothServiceBluedroid.h"
|
|
|
|
#elif defined(MOZ_B2G_BT_DAEMON)
|
|
|
|
/**
|
|
|
|
* B2G Bluetooth daemon:
|
|
|
|
* MOZ_B2G_BT, MOZ_B2G_BLUEDROID and MOZ_B2G_BT_DAEMON are defined;
|
2013-11-18 10:29:45 +04:00
|
|
|
* MOZ_B2G_BLUEZ is not defined.
|
|
|
|
*/
|
2013-10-23 10:12:24 +04:00
|
|
|
#include "BluetoothServiceBluedroid.h"
|
2013-10-23 10:12:24 +04:00
|
|
|
#endif
|
2013-11-18 10:29:45 +04: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 20:37:14 +04:00
|
|
|
#endif
|
2012-09-06 18:15:36 +04:00
|
|
|
|
2013-01-29 14:37:51 +04:00
|
|
|
#define MOZSETTINGS_CHANGED_ID "mozsettings-changed"
|
|
|
|
#define BLUETOOTH_ENABLED_SETTING "bluetooth.enabled"
|
|
|
|
#define BLUETOOTH_DEBUGGING_SETTING "bluetooth.debugging.enabled"
|
2012-07-18 07:41:54 +04:00
|
|
|
|
2013-02-06 11:28:04 +04:00
|
|
|
#define PROP_BLUETOOTH_ENABLED "bluetooth.isEnabled"
|
|
|
|
|
2012-09-13 20:37:14 +04:00
|
|
|
#define DEFAULT_SHUTDOWN_TIMER_MS 5000
|
2012-07-18 07:41:54 +04:00
|
|
|
|
2013-01-29 14:37:51 +04:00
|
|
|
bool gBluetoothDebugFlag = false;
|
|
|
|
|
2012-09-13 20:37:14 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
2012-07-18 07:41:54 +04:00
|
|
|
USING_BLUETOOTH_NAMESPACE
|
|
|
|
|
2012-09-13 20:37:14 +04:00
|
|
|
namespace {
|
2012-07-18 07:41:54 +04:00
|
|
|
|
2014-01-10 05:34:43 +04:00
|
|
|
StaticRefPtr<BluetoothService> sBluetoothService;
|
2012-09-13 20:37:14 +04:00
|
|
|
|
2014-01-10 05:34:43 +04:00
|
|
|
bool sInShutdown = false;
|
|
|
|
bool sToggleInProgress = false;
|
2012-09-13 20:37:14 +04: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-18 07:41:54 +04:00
|
|
|
|
2014-03-03 16:07:17 +04:00
|
|
|
BluetoothService::ToggleBtAck::ToggleBtAck(bool aEnabled)
|
|
|
|
: mEnabled(aEnabled)
|
2014-03-14 13:49:33 +04:00
|
|
|
{ }
|
2012-09-06 18:15:36 +04:00
|
|
|
|
2014-03-03 16:07:17 +04:00
|
|
|
NS_METHOD
|
|
|
|
BluetoothService::ToggleBtAck::Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-08-08 21:46:39 +04:00
|
|
|
|
2014-03-03 16:07:17 +04: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 16:07:16 +04:00
|
|
|
#if defined(MOZ_WIDGET_GONK)
|
2014-03-03 16:07:17 +04:00
|
|
|
if (property_set(PROP_BLUETOOTH_ENABLED, mEnabled ? "true" : "false") != 0) {
|
|
|
|
BT_WARNING("Failed to set bluetooth enabled property");
|
|
|
|
}
|
2014-03-03 16:07:16 +04:00
|
|
|
#endif
|
|
|
|
|
2014-03-03 16:07:17 +04:00
|
|
|
NS_ENSURE_TRUE(sBluetoothService, NS_OK);
|
2012-07-18 07:41:54 +04:00
|
|
|
|
2014-03-03 16:07:17 +04:00
|
|
|
if (sInShutdown) {
|
|
|
|
sBluetoothService = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-11-21 08:58:04 +04:00
|
|
|
|
2014-03-03 16:07:17 +04:00
|
|
|
// Update mEnabled of BluetoothService object since
|
|
|
|
// StartInternal/StopInternal have been already done.
|
|
|
|
sBluetoothService->SetEnabled(mEnabled);
|
|
|
|
sToggleInProgress = false;
|
2013-11-20 11:01:54 +04:00
|
|
|
|
2014-03-03 16:07:17 +04: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 12:24:51 +04:00
|
|
|
|
2014-03-03 16:07:17 +04:00
|
|
|
// Event 'AdapterAdded' has to be fired after firing 'Enabled'
|
|
|
|
sBluetoothService->TryFiringAdapterAdded();
|
2012-08-08 21:46:39 +04:00
|
|
|
|
2014-03-03 16:07:17 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-07-18 07:41:54 +04:00
|
|
|
|
2012-09-06 18:15:36 +04:00
|
|
|
class BluetoothService::StartupTask : public nsISettingsServiceCallback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2014-01-09 21:39:36 +04:00
|
|
|
NS_IMETHOD Handle(const nsAString& aName, JS::Handle<JS::Value> aResult)
|
2012-09-06 18:15:36 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (!aResult.isBoolean()) {
|
2013-09-14 02:51:00 +04:00
|
|
|
BT_WARNING("Setting for '" BLUETOOTH_ENABLED_SETTING "' is not a boolean!");
|
2012-09-06 18:15:36 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-13 20:37:14 +04:00
|
|
|
// It is theoretically possible to shut down before the first settings check
|
|
|
|
// has completed (though extremely unlikely).
|
2014-01-10 05:34:43 +04:00
|
|
|
if (sBluetoothService) {
|
|
|
|
return sBluetoothService->HandleStartupSettingsCheck(aResult.toBoolean());
|
2012-09-13 20:37:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2012-09-06 18:15:36 +04:00
|
|
|
}
|
|
|
|
|
2012-11-15 01:46:38 +04:00
|
|
|
NS_IMETHOD HandleError(const nsAString& aName)
|
2012-09-06 18:15:36 +04:00
|
|
|
{
|
2013-09-14 02:51:00 +04:00
|
|
|
BT_WARNING("Unable to get value for '" BLUETOOTH_ENABLED_SETTING "'");
|
2012-09-06 18:15:36 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(BluetoothService::StartupTask, nsISettingsServiceCallback);
|
2012-09-06 18:15:36 +04:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(BluetoothService, nsIObserver)
|
2012-09-13 20:37:14 +04:00
|
|
|
|
2012-11-30 13:46:55 +04:00
|
|
|
bool
|
|
|
|
BluetoothService::IsToggling() const
|
|
|
|
{
|
2014-01-10 05:34:43 +04:00
|
|
|
return sToggleInProgress;
|
2012-11-30 13:46:55 +04:00
|
|
|
}
|
|
|
|
|
2012-09-12 18:03:42 +04:00
|
|
|
BluetoothService::~BluetoothService()
|
|
|
|
{
|
2012-09-13 20:37:14 +04:00
|
|
|
Cleanup();
|
|
|
|
}
|
|
|
|
|
2013-01-09 19:51:53 +04:00
|
|
|
PLDHashOperator
|
|
|
|
RemoveObserversExceptBluetoothManager
|
|
|
|
(const nsAString& key,
|
|
|
|
nsAutoPtr<BluetoothSignalObserverList>& value,
|
|
|
|
void* arg)
|
|
|
|
{
|
2013-03-15 11:15:47 +04:00
|
|
|
if (!key.EqualsLiteral(KEY_MANAGER)) {
|
2013-02-15 04:45:13 +04:00
|
|
|
return PL_DHASH_REMOVE;
|
2013-01-09 19:51:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2012-09-13 20:37:14 +04:00
|
|
|
// static
|
|
|
|
BluetoothService*
|
|
|
|
BluetoothService::Create()
|
|
|
|
{
|
|
|
|
#if defined(MOZ_B2G_BT)
|
|
|
|
if (!IsMainProcess()) {
|
|
|
|
return BluetoothServiceChildProcess::Create();
|
2012-09-12 18:03:42 +04:00
|
|
|
}
|
2012-09-13 20:37:14 +04:00
|
|
|
|
2013-11-18 10:29:45 +04:00
|
|
|
#if defined(MOZ_B2G_BT_BLUEZ)
|
2014-02-13 19:28:07 +04:00
|
|
|
return new BluetoothDBusService();
|
2013-11-18 10:29:45 +04:00
|
|
|
#elif defined(MOZ_B2G_BT_BLUEDROID)
|
|
|
|
return new BluetoothServiceBluedroid();
|
2014-11-03 15:03:49 +03:00
|
|
|
#elif defined(MOZ_B2G_BT_DAEMON)
|
|
|
|
return new BluetoothServiceBluedroid();
|
2013-10-23 10:12:24 +04:00
|
|
|
#endif
|
2012-09-13 20:37:14 +04:00
|
|
|
#elif defined(MOZ_BLUETOOTH_DBUS)
|
|
|
|
return new BluetoothDBusService();
|
2013-10-23 10:12:24 +04:00
|
|
|
#endif
|
2013-11-18 10:29:45 +04:00
|
|
|
|
2013-09-14 02:51:00 +04:00
|
|
|
BT_WARNING("No platform support for bluetooth!");
|
2012-09-13 20:37:14 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
2012-09-12 18:03:42 +04:00
|
|
|
|
2012-09-13 20:37:14 +04: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-14 02:51:00 +04:00
|
|
|
BT_WARNING("Failed to add shutdown observer!");
|
2012-09-13 20:37:14 +04: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-14 02:51:00 +04:00
|
|
|
BT_WARNING("Failed to add settings change observer!");
|
2012-09-13 20:37:14 +04:00
|
|
|
return false;
|
2012-09-12 18:03:42 +04:00
|
|
|
}
|
2012-09-13 20:37:14 +04:00
|
|
|
|
|
|
|
return true;
|
2012-09-12 18:03:42 +04:00
|
|
|
}
|
|
|
|
|
2012-09-13 20:37:14 +04: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-14 02:51:00 +04:00
|
|
|
BT_WARNING("Can't unregister observers!");
|
2012-09-13 20:37:14 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-29 10:52:58 +04:00
|
|
|
BluetoothService::RegisterBluetoothSignalHandler(
|
|
|
|
const nsAString& aNodeName,
|
|
|
|
BluetoothSignalObserver* aHandler)
|
2012-07-18 07:41:54 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-09-13 20:37:14 +04:00
|
|
|
MOZ_ASSERT(aHandler);
|
|
|
|
|
2013-09-14 02:51:00 +04:00
|
|
|
BT_LOGD("[S] %s: %s", __FUNCTION__, NS_ConvertUTF16toUTF8(aNodeName).get());
|
2013-01-29 14:37:51 +04:00
|
|
|
|
2012-07-18 07:41:54 +04:00
|
|
|
BluetoothSignalObserverList* ol;
|
|
|
|
if (!mBluetoothSignalObserverTable.Get(aNodeName, &ol)) {
|
|
|
|
ol = new BluetoothSignalObserverList();
|
|
|
|
mBluetoothSignalObserverTable.Put(aNodeName, ol);
|
|
|
|
}
|
2013-01-09 19:51:53 +04:00
|
|
|
|
2012-07-18 07:41:54 +04:00
|
|
|
ol->AddObserver(aHandler);
|
|
|
|
}
|
|
|
|
|
2012-09-13 20:37:14 +04:00
|
|
|
void
|
2013-01-29 10:52:58 +04:00
|
|
|
BluetoothService::UnregisterBluetoothSignalHandler(
|
|
|
|
const nsAString& aNodeName,
|
|
|
|
BluetoothSignalObserver* aHandler)
|
2012-07-18 07:41:54 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-09-13 20:37:14 +04:00
|
|
|
MOZ_ASSERT(aHandler);
|
|
|
|
|
2013-09-14 02:51:00 +04:00
|
|
|
BT_LOGD("[S] %s: %s", __FUNCTION__, NS_ConvertUTF16toUTF8(aNodeName).get());
|
2013-01-29 14:37:51 +04:00
|
|
|
|
2012-07-18 07:41:54 +04:00
|
|
|
BluetoothSignalObserverList* ol;
|
2012-09-13 20:37:14 +04:00
|
|
|
if (mBluetoothSignalObserverTable.Get(aNodeName, &ol)) {
|
|
|
|
ol->RemoveObserver(aHandler);
|
2013-04-19 12:40:56 +04: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 20:37:14 +04:00
|
|
|
if (ol->Length() == 0) {
|
|
|
|
mBluetoothSignalObserverTable.Remove(aNodeName);
|
|
|
|
}
|
2012-07-18 07:41:54 +04:00
|
|
|
}
|
2012-09-13 20:37:14 +04:00
|
|
|
else {
|
2013-09-14 02:51:00 +04:00
|
|
|
BT_WARNING("Node was never registered!");
|
2012-07-18 07:41:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-06 05:35:29 +04:00
|
|
|
PLDHashOperator
|
|
|
|
RemoveAllSignalHandlers(const nsAString& aKey,
|
|
|
|
nsAutoPtr<BluetoothSignalObserverList>& aData,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
2013-04-19 12:40:56 +04: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-06 05:35:29 +04:00
|
|
|
return aData->Length() ? PL_DHASH_NEXT : PL_DHASH_REMOVE;
|
|
|
|
}
|
|
|
|
|
2012-09-13 20:37:14 +04:00
|
|
|
void
|
|
|
|
BluetoothService::UnregisterAllSignalHandlers(BluetoothSignalObserver* aHandler)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aHandler);
|
|
|
|
|
2013-02-06 05:35:29 +04:00
|
|
|
mBluetoothSignalObserverTable.Enumerate(RemoveAllSignalHandlers, aHandler);
|
2012-09-13 20:37:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothService::DistributeSignal(const BluetoothSignal& aSignal)
|
2012-07-18 07:41:54 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-11-21 11:33:07 +04:00
|
|
|
|
2013-03-15 11:15:47 +04:00
|
|
|
if (aSignal.path().EqualsLiteral(KEY_LOCAL_AGENT)) {
|
2012-11-21 11:33:07 +04:00
|
|
|
Notify(aSignal);
|
|
|
|
return;
|
2013-03-15 11:15:47 +04:00
|
|
|
} else if (aSignal.path().EqualsLiteral(KEY_REMOTE_AGENT)) {
|
2012-11-21 11:33:07 +04:00
|
|
|
Notify(aSignal);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-18 07:41:54 +04:00
|
|
|
BluetoothSignalObserverList* ol;
|
2012-09-13 20:37:14 +04:00
|
|
|
if (!mBluetoothSignalObserverTable.Get(aSignal.path(), &ol)) {
|
2012-09-06 07:06:20 +04:00
|
|
|
#if DEBUG
|
2012-09-13 20:37:14 +04:00
|
|
|
nsAutoCString msg("No observer registered for path ");
|
|
|
|
msg.Append(NS_ConvertUTF16toUTF8(aSignal.path()));
|
2013-09-14 02:51:00 +04:00
|
|
|
BT_WARNING(msg.get());
|
2012-09-06 07:06:20 +04:00
|
|
|
#endif
|
2012-09-13 20:37:14 +04:00
|
|
|
return;
|
2012-09-06 07:06:20 +04:00
|
|
|
}
|
2012-09-13 20:37:14 +04:00
|
|
|
MOZ_ASSERT(ol->Length());
|
|
|
|
ol->Broadcast(aSignal);
|
2012-07-18 07:41:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-03-14 13:50:02 +04:00
|
|
|
BluetoothService::StartBluetooth(bool aIsStartup)
|
2012-07-18 07:41:54 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2014-01-10 05:34:43 +04:00
|
|
|
if (sInShutdown) {
|
2014-03-14 13:50:02 +04:00
|
|
|
// Don't try to start if we're already shutting down.
|
|
|
|
MOZ_ASSERT(false, "Start called while in shutdown!");
|
|
|
|
return NS_ERROR_FAILURE;
|
2012-09-06 18:15:36 +04:00
|
|
|
}
|
|
|
|
|
2014-03-14 13:50:02 +04:00
|
|
|
mAdapterAddedReceived = false;
|
2013-09-06 15:20:27 +04:00
|
|
|
|
2014-03-14 13:50:18 +04:00
|
|
|
/* When IsEnabled() is true, we don't switch on Bluetooth but we still
|
|
|
|
* send ToggleBtAck task. One special case happens at startup stage. At
|
|
|
|
* startup, the initialization of BluetoothService still has to be done
|
|
|
|
* even if Bluetooth is already enabled.
|
|
|
|
*
|
|
|
|
* Please see bug 892392 for more information.
|
|
|
|
*/
|
|
|
|
if (aIsStartup || !sBluetoothService->IsEnabled()) {
|
|
|
|
// Switch Bluetooth on
|
|
|
|
if (NS_FAILED(sBluetoothService->StartInternal())) {
|
|
|
|
BT_WARNING("Bluetooth service failed to start!");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
BT_WARNING("Bluetooth has already been enabled before.");
|
|
|
|
nsRefPtr<nsRunnable> runnable = new BluetoothService::ToggleBtAck(true);
|
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(runnable))) {
|
|
|
|
BT_WARNING("Failed to dispatch to main thread!");
|
|
|
|
}
|
|
|
|
}
|
2013-08-08 13:51:37 +04:00
|
|
|
|
2014-03-14 13:50:02 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-09-06 15:20:27 +04:00
|
|
|
|
2014-03-14 13:50:02 +04:00
|
|
|
nsresult
|
|
|
|
BluetoothService::StopBluetooth(bool aIsStartup)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
BluetoothProfileManagerBase* profile;
|
|
|
|
profile = BluetoothHfpManager::Get();
|
|
|
|
NS_ENSURE_TRUE(profile, NS_ERROR_FAILURE);
|
|
|
|
if (profile->IsConnected()) {
|
|
|
|
profile->Disconnect(nullptr);
|
|
|
|
} else {
|
|
|
|
profile->Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
profile = BluetoothOppManager::Get();
|
|
|
|
NS_ENSURE_TRUE(profile, NS_ERROR_FAILURE);
|
|
|
|
if (profile->IsConnected()) {
|
|
|
|
profile->Disconnect(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
profile = BluetoothA2dpManager::Get();
|
|
|
|
NS_ENSURE_TRUE(profile, NS_ERROR_FAILURE);
|
|
|
|
if (profile->IsConnected()) {
|
|
|
|
profile->Disconnect(nullptr);
|
|
|
|
} else {
|
|
|
|
profile->Reset();
|
|
|
|
}
|
2014-01-29 15:13:10 +04:00
|
|
|
|
2014-03-14 13:50:02 +04:00
|
|
|
profile = BluetoothHidManager::Get();
|
|
|
|
NS_ENSURE_TRUE(profile, NS_ERROR_FAILURE);
|
|
|
|
if (profile->IsConnected()) {
|
|
|
|
profile->Disconnect(nullptr);
|
|
|
|
} else {
|
|
|
|
profile->Reset();
|
2013-08-08 13:51:37 +04:00
|
|
|
}
|
|
|
|
|
2013-11-27 12:24:51 +04:00
|
|
|
mAdapterAddedReceived = false;
|
|
|
|
|
2014-03-14 13:50:18 +04:00
|
|
|
/* When IsEnabled() is false, we don't switch off Bluetooth but we still
|
|
|
|
* send ToggleBtAck task. One special case happens at startup stage. At
|
|
|
|
* startup, the initialization of BluetoothService still has to be done
|
|
|
|
* even if Bluetooth is disabled.
|
|
|
|
*
|
|
|
|
* Please see bug 892392 for more information.
|
|
|
|
*/
|
|
|
|
if (aIsStartup || sBluetoothService->IsEnabled()) {
|
|
|
|
// Switch Bluetooth off
|
|
|
|
if (NS_FAILED(sBluetoothService->StopInternal())) {
|
|
|
|
BT_WARNING("Bluetooth service failed to stop!");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
BT_WARNING("Bluetooth has already been enabled/disabled before.");
|
|
|
|
nsRefPtr<nsRunnable> runnable = new BluetoothService::ToggleBtAck(false);
|
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(runnable))) {
|
|
|
|
BT_WARNING("Failed to dispatch to main thread!");
|
|
|
|
}
|
|
|
|
}
|
2012-09-06 18:15:36 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-03-14 13:50:02 +04:00
|
|
|
nsresult
|
|
|
|
BluetoothService::StartStopBluetooth(bool aStart, bool aIsStartup)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
if (aStart) {
|
|
|
|
rv = StartBluetooth(aIsStartup);
|
|
|
|
} else {
|
|
|
|
rv = StopBluetooth(aIsStartup);
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-09-06 18:15:36 +04:00
|
|
|
void
|
|
|
|
BluetoothService::SetEnabled(bool aEnabled)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2012-09-13 20:37:14 +04:00
|
|
|
AutoInfallibleTArray<BluetoothParent*, 10> childActors;
|
|
|
|
GetAllBluetoothActors(childActors);
|
|
|
|
|
|
|
|
for (uint32_t index = 0; index < childActors.Length(); index++) {
|
|
|
|
unused << childActors[index]->SendEnabled(aEnabled);
|
|
|
|
}
|
|
|
|
|
2013-02-08 14:03:09 +04:00
|
|
|
if (!aEnabled) {
|
2013-01-09 19:51:53 +04: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-15 04:45:13 +04:00
|
|
|
RemoveObserversExceptBluetoothManager, nullptr);
|
2012-10-09 07:29:37 +04:00
|
|
|
}
|
|
|
|
|
2012-11-30 13:46:55 +04:00
|
|
|
/**
|
|
|
|
* mEnabled: real status of bluetooth
|
|
|
|
* aEnabled: expected status of bluetooth
|
|
|
|
*/
|
|
|
|
if (mEnabled == aEnabled) {
|
2014-02-20 14:15:47 +04:00
|
|
|
BT_WARNING("Bluetooth has already been enabled/disabled before "
|
2014-01-10 05:34:43 +04:00
|
|
|
"or the toggling is failed.");
|
2012-11-30 13:46:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mEnabled = aEnabled;
|
2012-09-06 18:15:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
BluetoothService::HandleStartup()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-01-10 05:34:43 +04:00
|
|
|
MOZ_ASSERT(!sToggleInProgress);
|
2012-09-06 18:15:36 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsISettingsService> settings =
|
|
|
|
do_GetService("@mozilla.org/settingsService;1");
|
|
|
|
NS_ENSURE_TRUE(settings, NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
nsCOMPtr<nsISettingsServiceLock> settingsLock;
|
2014-02-07 15:19:58 +04:00
|
|
|
nsresult rv = settings->CreateLock(nullptr, getter_AddRefs(settingsLock));
|
2012-09-06 18:15:36 +04: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-10 05:34:43 +04:00
|
|
|
sToggleInProgress = true;
|
2012-09-13 20:37:14 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
BluetoothService::HandleStartupSettingsCheck(bool aEnable)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-08-02 07:58:01 +04:00
|
|
|
return StartStopBluetooth(aEnable, true);
|
2012-07-18 07:41:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-09-16 21:15:16 +04:00
|
|
|
BluetoothService::HandleSettingsChanged(nsISupports* aSubject)
|
2012-07-18 07:41:54 +04:00
|
|
|
{
|
2012-09-06 18:15:36 +04: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}
|
|
|
|
|
2014-09-16 21:15:16 +04:00
|
|
|
AutoJSAPI jsapi;
|
|
|
|
jsapi.Init();
|
|
|
|
JSContext* cx = jsapi.cx();
|
|
|
|
RootedDictionary<SettingChangeNotification> setting(cx);
|
|
|
|
if (!WrappedJSToDictionary(cx, aSubject, setting)) {
|
2012-09-06 18:15:36 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-09-16 21:15:16 +04:00
|
|
|
if (setting.mKey.EqualsASCII(BLUETOOTH_DEBUGGING_SETTING)) {
|
|
|
|
if (!setting.mValue.isBoolean()) {
|
2013-01-29 14:37:51 +04:00
|
|
|
MOZ_ASSERT(false, "Expecting a boolean for 'bluetooth.debugging.enabled'!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2014-11-03 15:03:49 +03:00
|
|
|
|
2014-09-16 21:15:16 +04:00
|
|
|
SWITCH_BT_DEBUG(setting.mValue.toBoolean());
|
2013-01-29 14:37:51 +04:00
|
|
|
|
2012-09-06 18:15:36 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-01-29 14:37:51 +04:00
|
|
|
// Second, check if the string is BLUETOOTH_ENABLED_SETTING
|
2014-09-16 21:15:16 +04:00
|
|
|
if (!setting.mKey.EqualsASCII(BLUETOOTH_ENABLED_SETTING)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (!setting.mValue.isBoolean()) {
|
|
|
|
MOZ_ASSERT(false, "Expecting a boolean for 'bluetooth.enabled'!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2012-09-06 18:15:36 +04:00
|
|
|
}
|
|
|
|
|
2014-09-16 21:15:16 +04:00
|
|
|
sToggleInProgress = true;
|
2013-01-29 14:37:51 +04:00
|
|
|
|
2014-09-16 21:15:16 +04:00
|
|
|
nsresult rv = StartStopBluetooth(setting.mValue.toBoolean(), false);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-09-06 18:15:36 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
2012-07-18 07:41:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-09-06 18:15:36 +04:00
|
|
|
BluetoothService::HandleShutdown()
|
2012-07-18 07:41:54 +04:00
|
|
|
{
|
2012-09-06 18:15:36 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2012-09-13 20:37:14 +04: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-10 05:34:43 +04:00
|
|
|
sInShutdown = true;
|
2012-09-06 18:15:36 +04:00
|
|
|
|
2012-09-13 20:37:14 +04: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-29 05:38:30 +04:00
|
|
|
MOZ_CRASH("Failed to cancel shutdown timer, this will crash!");
|
2012-09-13 20:37:14 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
MOZ_ASSERT(false, "Failed to initialize shutdown timer!");
|
|
|
|
}
|
|
|
|
}
|
2012-09-06 18:15:36 +04:00
|
|
|
}
|
|
|
|
|
2014-03-14 13:50:02 +04:00
|
|
|
if (IsEnabled() && NS_FAILED(StopBluetooth(false))) {
|
2012-09-13 20:37:14 +04:00
|
|
|
MOZ_ASSERT(false, "Failed to deliver stop message!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2012-09-06 18:15:36 +04:00
|
|
|
}
|
|
|
|
|
2012-07-18 07:41:54 +04:00
|
|
|
// static
|
|
|
|
BluetoothService*
|
|
|
|
BluetoothService::Get()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
// If we already exist, exit early
|
2014-01-10 05:34:43 +04:00
|
|
|
if (sBluetoothService) {
|
|
|
|
return sBluetoothService;
|
2012-07-18 07:41:54 +04:00
|
|
|
}
|
2012-09-06 18:15:36 +04:00
|
|
|
|
2012-07-18 07:41:54 +04:00
|
|
|
// If we're in shutdown, don't create a new instance
|
2014-01-10 05:34:43 +04:00
|
|
|
if (sInShutdown) {
|
2013-09-14 02:51:00 +04:00
|
|
|
BT_WARNING("BluetoothService can't be created during shutdown");
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-07-18 07:41:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create new instance, register, return
|
2014-08-07 10:01:24 +04:00
|
|
|
sBluetoothService = BluetoothService::Create();
|
|
|
|
NS_ENSURE_TRUE(sBluetoothService, nullptr);
|
2012-09-06 18:15:36 +04:00
|
|
|
|
2014-08-07 10:01:24 +04:00
|
|
|
if (!sBluetoothService->Init()) {
|
|
|
|
sBluetoothService->Cleanup();
|
2012-09-12 18:03:42 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-08-07 10:01:24 +04:00
|
|
|
ClearOnShutdown(&sBluetoothService);
|
2014-01-10 05:34:43 +04:00
|
|
|
return sBluetoothService;
|
2012-07-18 07:41:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
BluetoothService::Observe(nsISupports* aSubject, const char* aTopic,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* aData)
|
2012-07-18 07:41:54 +04:00
|
|
|
{
|
2012-09-06 18:15:36 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (!strcmp(aTopic, "profile-after-change")) {
|
|
|
|
return HandleStartup();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(aTopic, MOZSETTINGS_CHANGED_ID)) {
|
2014-09-16 21:15:16 +04:00
|
|
|
return HandleSettingsChanged(aSubject);
|
2012-09-06 18:15:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
|
|
|
|
return HandleShutdown();
|
2012-07-18 07:41:54 +04:00
|
|
|
}
|
|
|
|
|
2012-09-06 18:15:36 +04:00
|
|
|
MOZ_ASSERT(false, "BluetoothService got unexpected topic!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2012-07-18 07:41:54 +04:00
|
|
|
}
|
2012-09-12 18:03:42 +04:00
|
|
|
|
2013-11-27 12:24:51 +04: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 18:03:42 +04:00
|
|
|
void
|
|
|
|
BluetoothService::Notify(const BluetoothSignal& aData)
|
|
|
|
{
|
2013-05-20 11:34:45 +04:00
|
|
|
nsString type = NS_LITERAL_STRING("bluetooth-pairing-request");
|
2012-09-12 18:03:42 +04:00
|
|
|
|
2013-05-22 20:05:27 +04:00
|
|
|
AutoSafeJSContext cx;
|
2014-01-16 21:48:58 +04:00
|
|
|
JS::Rooted<JSObject*> obj(cx, JS_NewObject(cx, nullptr, JS::NullPtr(),
|
|
|
|
JS::NullPtr()));
|
2013-06-20 08:14:09 +04:00
|
|
|
NS_ENSURE_TRUE_VOID(obj);
|
2012-09-12 18:03:42 +04:00
|
|
|
|
2013-01-29 10:52:39 +04:00
|
|
|
if (!SetJsObject(cx, aData.value(), obj)) {
|
2013-09-14 02:51:00 +04:00
|
|
|
BT_WARNING("Failed to set properties of system message!");
|
2012-09-12 18:03:42 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-14 02:51:00 +04:00
|
|
|
BT_LOGD("[S] %s: %s", __FUNCTION__, NS_ConvertUTF16toUTF8(aData.name()).get());
|
2013-01-29 14:37:51 +04:00
|
|
|
|
2012-09-12 18:03:42 +04:00
|
|
|
if (aData.name().EqualsLiteral("RequestConfirmation")) {
|
2013-05-27 05:45:06 +04:00
|
|
|
MOZ_ASSERT(aData.value().get_ArrayOfBluetoothNamedValue().Length() == 4,
|
2013-01-29 10:52:39 +04:00
|
|
|
"RequestConfirmation: Wrong length of parameters");
|
2012-09-12 18:03:42 +04:00
|
|
|
} else if (aData.name().EqualsLiteral("RequestPinCode")) {
|
2013-05-27 05:45:06 +04:00
|
|
|
MOZ_ASSERT(aData.value().get_ArrayOfBluetoothNamedValue().Length() == 3,
|
2013-01-29 10:52:39 +04:00
|
|
|
"RequestPinCode: Wrong length of parameters");
|
2012-09-12 18:03:42 +04:00
|
|
|
} else if (aData.name().EqualsLiteral("RequestPasskey")) {
|
2013-05-27 05:45:06 +04:00
|
|
|
MOZ_ASSERT(aData.value().get_ArrayOfBluetoothNamedValue().Length() == 3,
|
2013-01-29 10:52:39 +04:00
|
|
|
"RequestPinCode: Wrong length of parameters");
|
2012-09-12 18:03:42 +04:00
|
|
|
} else if (aData.name().EqualsLiteral("Cancel")) {
|
2013-05-27 05:45:06 +04:00
|
|
|
MOZ_ASSERT(aData.value().get_ArrayOfBluetoothNamedValue().Length() == 0,
|
2013-01-29 10:52:39 +04:00
|
|
|
"Cancel: Wrong length of parameters");
|
2012-09-12 18:03:42 +04:00
|
|
|
type.AssignLiteral("bluetooth-cancel");
|
2013-08-02 14:32:57 +04:00
|
|
|
} else if (aData.name().EqualsLiteral(PAIRED_STATUS_CHANGED_ID)) {
|
2013-05-27 05:45:06 +04:00
|
|
|
MOZ_ASSERT(aData.value().get_ArrayOfBluetoothNamedValue().Length() == 1,
|
2013-08-02 14:32:57 +04:00
|
|
|
"pairedstatuschanged: Wrong length of parameters");
|
2012-09-30 06:45:21 +04:00
|
|
|
type.AssignLiteral("bluetooth-pairedstatuschanged");
|
2012-09-12 18:03:42 +04:00
|
|
|
} else {
|
|
|
|
nsCString warningMsg;
|
|
|
|
warningMsg.AssignLiteral("Not handling service signal: ");
|
|
|
|
warningMsg.Append(NS_ConvertUTF16toUTF8(aData.name()));
|
2013-09-14 02:51:00 +04:00
|
|
|
BT_WARNING(warningMsg.get());
|
2013-05-20 11:34:45 +04:00
|
|
|
return;
|
2012-09-12 18:03:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISystemMessagesInternal> systemMessenger =
|
|
|
|
do_GetService("@mozilla.org/system-message-internal;1");
|
2012-11-21 11:33:07 +04:00
|
|
|
NS_ENSURE_TRUE_VOID(systemMessenger);
|
2012-09-12 18:03:42 +04:00
|
|
|
|
2014-01-09 21:39:36 +04:00
|
|
|
JS::Rooted<JS::Value> value(cx, JS::ObjectValue(*obj));
|
|
|
|
systemMessenger->BroadcastMessage(type, value,
|
|
|
|
JS::UndefinedHandleValue);
|
2012-09-12 18:03:42 +04:00
|
|
|
}
|