2014-05-21 10:11:31 +04:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
/* 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 "BluetoothManager.h"
|
|
|
|
#include "BluetoothCommon.h"
|
|
|
|
#include "BluetoothAdapter.h"
|
|
|
|
#include "BluetoothService.h"
|
2014-05-29 13:16:20 +04:00
|
|
|
#include "BluetoothReplyRunnable.h"
|
2014-05-21 10:11:31 +04:00
|
|
|
|
2014-05-29 13:14:45 +04:00
|
|
|
#include "mozilla/dom/bluetooth/BluetoothTypes.h"
|
|
|
|
#include "mozilla/dom/BluetoothManager2Binding.h"
|
|
|
|
#include "mozilla/Services.h"
|
2014-05-21 10:11:31 +04:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsDOMClassInfo.h"
|
|
|
|
#include "nsIPermissionManager.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
|
|
|
USING_BLUETOOTH_NAMESPACE
|
|
|
|
|
|
|
|
// QueryInterface implementation for BluetoothManager
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(BluetoothManager)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(BluetoothManager, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(BluetoothManager, DOMEventTargetHelper)
|
|
|
|
|
2014-05-29 13:16:20 +04:00
|
|
|
class GetAdaptersTask : public BluetoothReplyRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
GetAdaptersTask(BluetoothManager* aManager)
|
|
|
|
: BluetoothReplyRunnable(nullptr)
|
|
|
|
, mManager(aManager)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
bool
|
|
|
|
ParseSuccessfulReply(JS::MutableHandle<JS::Value> aValue)
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Unwrap BluetoothReply.BluetoothReplySuccess.BluetoothValue =
|
|
|
|
* BluetoothNamedValue[]
|
|
|
|
* |
|
|
|
|
* |__ BluetoothNamedValue =
|
|
|
|
* | {"Adapter", BluetoothValue = BluetoothNamedValue[]}
|
|
|
|
* |
|
|
|
|
* |__ BluetoothNamedValue =
|
|
|
|
* | {"Adapter", BluetoothValue = BluetoothNamedValue[]}
|
|
|
|
* ...
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Extract the array of all adapters' properties
|
|
|
|
const BluetoothValue& adaptersProperties =
|
|
|
|
mReply->get_BluetoothReplySuccess().value();
|
|
|
|
NS_ENSURE_TRUE(adaptersProperties.type() ==
|
|
|
|
BluetoothValue::TArrayOfBluetoothNamedValue, false);
|
|
|
|
|
|
|
|
const InfallibleTArray<BluetoothNamedValue>& adaptersPropertiesArray =
|
|
|
|
adaptersProperties.get_ArrayOfBluetoothNamedValue();
|
2014-05-29 13:20:19 +04:00
|
|
|
BT_API2_LOGR("GetAdaptersTask: len[%d]", adaptersPropertiesArray.Length());
|
2014-05-29 13:16:20 +04:00
|
|
|
|
|
|
|
// Append a BluetoothAdapter into adapters array for each properties array
|
|
|
|
uint32_t numAdapters = adaptersPropertiesArray.Length();
|
|
|
|
for (uint32_t i = 0; i < numAdapters; i++) {
|
|
|
|
MOZ_ASSERT(adaptersPropertiesArray[i].name().EqualsLiteral("Adapter"));
|
|
|
|
|
|
|
|
const BluetoothValue& properties = adaptersPropertiesArray[i].value();
|
|
|
|
mManager->AppendAdapter(properties);
|
|
|
|
}
|
|
|
|
|
|
|
|
aValue.setUndefined();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ReleaseMembers()
|
|
|
|
{
|
|
|
|
BluetoothReplyRunnable::ReleaseMembers();
|
|
|
|
mManager = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<BluetoothManager> mManager;
|
|
|
|
};
|
|
|
|
|
2014-05-21 10:11:31 +04:00
|
|
|
BluetoothManager::BluetoothManager(nsPIDOMWindow *aWindow)
|
|
|
|
: DOMEventTargetHelper(aWindow)
|
2014-05-29 13:14:45 +04:00
|
|
|
, mDefaultAdapterIndex(-1)
|
2014-05-21 10:11:31 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWindow);
|
|
|
|
MOZ_ASSERT(IsDOMBinding());
|
|
|
|
|
2014-05-29 13:14:45 +04:00
|
|
|
ListenToBluetoothSignal(true);
|
2014-05-29 13:20:19 +04:00
|
|
|
BT_API2_LOGR("aWindow %p", aWindow);
|
2014-05-29 13:16:20 +04:00
|
|
|
|
|
|
|
// Query adapters list from bluetooth backend
|
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
|
|
|
NS_ENSURE_TRUE_VOID(bs);
|
|
|
|
|
|
|
|
nsRefPtr<BluetoothReplyRunnable> result = new GetAdaptersTask(this);
|
|
|
|
NS_ENSURE_SUCCESS_VOID(bs->GetAdaptersInternal(result));
|
2014-05-21 10:11:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
BluetoothManager::~BluetoothManager()
|
|
|
|
{
|
2014-05-29 13:14:45 +04:00
|
|
|
ListenToBluetoothSignal(false);
|
2014-05-21 10:11:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothManager::DisconnectFromOwner()
|
|
|
|
{
|
|
|
|
DOMEventTargetHelper::DisconnectFromOwner();
|
2014-05-29 13:14:45 +04:00
|
|
|
ListenToBluetoothSignal(false);
|
2014-05-21 10:11:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-05-29 13:14:45 +04:00
|
|
|
BluetoothManager::ListenToBluetoothSignal(bool aStart)
|
2014-05-21 10:11:31 +04:00
|
|
|
{
|
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
2014-05-29 13:14:45 +04:00
|
|
|
NS_ENSURE_TRUE_VOID(bs);
|
|
|
|
|
|
|
|
if (aStart) {
|
|
|
|
bs->RegisterBluetoothSignalHandler(NS_LITERAL_STRING(KEY_MANAGER), this);
|
|
|
|
} else {
|
|
|
|
bs->UnregisterBluetoothSignalHandler(NS_LITERAL_STRING(KEY_MANAGER), this);
|
2014-05-21 10:11:31 +04:00
|
|
|
}
|
2014-05-29 13:14:45 +04:00
|
|
|
}
|
2014-05-21 10:11:31 +04:00
|
|
|
|
2014-05-29 13:14:45 +04:00
|
|
|
BluetoothAdapter*
|
|
|
|
BluetoothManager::GetDefaultAdapter()
|
|
|
|
{
|
2014-05-29 13:20:19 +04:00
|
|
|
BT_API2_LOGR("mDefaultAdapterIndex: %d", mDefaultAdapterIndex);
|
|
|
|
|
|
|
|
return DefaultAdapterExists() ? mAdapters[mDefaultAdapterIndex] : nullptr;
|
2014-05-21 10:11:31 +04:00
|
|
|
}
|
|
|
|
|
2014-05-29 13:14:45 +04:00
|
|
|
void
|
|
|
|
BluetoothManager::AppendAdapter(const BluetoothValue& aValue)
|
2014-05-21 10:11:31 +04:00
|
|
|
{
|
2014-05-29 13:14:45 +04:00
|
|
|
MOZ_ASSERT(aValue.type() == BluetoothValue::TArrayOfBluetoothNamedValue);
|
2014-05-21 10:11:31 +04:00
|
|
|
|
2014-05-29 13:14:45 +04:00
|
|
|
// Create a new BluetoothAdapter and append it to adapters array
|
|
|
|
const InfallibleTArray<BluetoothNamedValue>& values =
|
|
|
|
aValue.get_ArrayOfBluetoothNamedValue();
|
|
|
|
nsRefPtr<BluetoothAdapter> adapter =
|
|
|
|
BluetoothAdapter::Create(GetOwner(), values);
|
2014-05-21 10:11:31 +04:00
|
|
|
|
2014-05-29 13:14:45 +04:00
|
|
|
mAdapters.AppendElement(adapter);
|
2014-05-21 10:11:31 +04:00
|
|
|
|
2014-05-29 13:14:45 +04:00
|
|
|
// Set this adapter as default adapter if no adapter exists
|
|
|
|
if (!DefaultAdapterExists()) {
|
|
|
|
MOZ_ASSERT(mAdapters.Length() == 1);
|
|
|
|
ReselectDefaultAdapter();
|
2014-05-21 10:11:31 +04:00
|
|
|
}
|
2014-05-29 13:14:45 +04:00
|
|
|
}
|
2014-05-21 10:11:31 +04:00
|
|
|
|
2014-05-29 13:14:45 +04:00
|
|
|
void
|
|
|
|
BluetoothManager::GetAdapters(nsTArray<nsRefPtr<BluetoothAdapter> >& aAdapters)
|
|
|
|
{
|
|
|
|
aAdapters = mAdapters;
|
2014-05-21 10:11:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<BluetoothManager>
|
|
|
|
BluetoothManager::Create(nsPIDOMWindow* aWindow)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aWindow);
|
|
|
|
|
|
|
|
nsRefPtr<BluetoothManager> manager = new BluetoothManager(aWindow);
|
|
|
|
return manager.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-05-29 13:14:45 +04:00
|
|
|
BluetoothManager::HandleAdapterAdded(const BluetoothValue& aValue)
|
2014-05-21 10:11:31 +04:00
|
|
|
{
|
2014-05-29 13:14:45 +04:00
|
|
|
MOZ_ASSERT(aValue.type() == BluetoothValue::TArrayOfBluetoothNamedValue);
|
2014-05-29 13:20:19 +04:00
|
|
|
BT_API2_LOGR();
|
2014-05-21 10:11:31 +04:00
|
|
|
|
2014-05-29 13:14:45 +04:00
|
|
|
AppendAdapter(aValue);
|
|
|
|
|
|
|
|
// Notify application of added adapter
|
|
|
|
BluetoothAdapterEventInit init;
|
|
|
|
init.mAdapter = mAdapters.LastElement();
|
|
|
|
DispatchAdapterEvent(NS_LITERAL_STRING("adapteradded"), init);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothManager::HandleAdapterRemoved(const BluetoothValue& aValue)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aValue.type() == BluetoothValue::TnsString);
|
|
|
|
MOZ_ASSERT(DefaultAdapterExists());
|
|
|
|
|
|
|
|
// Remove the adapter of given address from adapters array
|
|
|
|
nsString addressToRemove = aValue.get_nsString();
|
|
|
|
|
|
|
|
uint32_t numAdapters = mAdapters.Length();
|
|
|
|
for (uint32_t i = 0; i < numAdapters; i++) {
|
|
|
|
nsString address;
|
|
|
|
mAdapters[i]->GetAddress(address);
|
|
|
|
if (address.Equals(addressToRemove)) {
|
|
|
|
mAdapters.RemoveElementAt(i);
|
|
|
|
|
|
|
|
if (mDefaultAdapterIndex == (int)i) {
|
|
|
|
ReselectDefaultAdapter();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-05-21 10:11:31 +04:00
|
|
|
}
|
2014-05-29 13:14:45 +04:00
|
|
|
|
|
|
|
// Notify application of removed adapter
|
|
|
|
BluetoothAdapterEventInit init;
|
|
|
|
init.mAddress = addressToRemove;
|
|
|
|
DispatchAdapterEvent(NS_LITERAL_STRING("adapterremoved"), init);
|
2014-05-21 10:11:31 +04:00
|
|
|
}
|
|
|
|
|
2014-05-29 13:14:45 +04:00
|
|
|
void
|
|
|
|
BluetoothManager::ReselectDefaultAdapter()
|
2014-05-21 10:11:31 +04:00
|
|
|
{
|
2014-05-29 13:14:45 +04:00
|
|
|
// Select the first of existing/remaining adapters as default adapter
|
|
|
|
mDefaultAdapterIndex = mAdapters.IsEmpty() ? -1 : 0;
|
2014-05-29 13:20:19 +04:00
|
|
|
BT_API2_LOGR("mAdapters length: %d => NEW mDefaultAdapterIndex: %d",
|
|
|
|
mAdapters.Length(), mDefaultAdapterIndex);
|
2014-05-29 13:14:45 +04:00
|
|
|
|
|
|
|
// Notify application of default adapter change
|
|
|
|
DispatchAttributeEvent();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothManager::DispatchAdapterEvent(const nsAString& aType,
|
|
|
|
const BluetoothAdapterEventInit& aInit)
|
|
|
|
{
|
2014-05-29 13:20:19 +04:00
|
|
|
BT_API2_LOGR("aType (%s)", NS_ConvertUTF16toUTF8(aType).get());
|
|
|
|
|
2014-05-29 13:14:45 +04:00
|
|
|
nsRefPtr<BluetoothAdapterEvent> event =
|
|
|
|
BluetoothAdapterEvent::Constructor(this, aType, aInit);
|
|
|
|
DispatchTrustedEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothManager::DispatchAttributeEvent()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-05-29 13:20:19 +04:00
|
|
|
BT_API2_LOGR();
|
2014-05-29 13:14:45 +04:00
|
|
|
|
|
|
|
AutoJSContext cx;
|
|
|
|
JS::Rooted<JS::Value> value(cx, JS::NullValue());
|
2014-05-29 13:20:19 +04:00
|
|
|
|
2014-06-19 08:03:09 +04:00
|
|
|
nsCOMPtr<nsIGlobalObject> global =
|
|
|
|
do_QueryInterface(GetOwner());
|
|
|
|
NS_ENSURE_TRUE_VOID(global);
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> scope(cx, global->GetGlobalJSObject());
|
|
|
|
NS_ENSURE_TRUE_VOID(scope);
|
|
|
|
|
|
|
|
JSAutoCompartment ac(cx, scope);
|
|
|
|
|
|
|
|
nsTArray<nsString> types;
|
|
|
|
types.AppendElement(NS_LITERAL_STRING("DefaultAdapter"));
|
|
|
|
|
|
|
|
if (!ToJSValue(cx, types, &value)) {
|
|
|
|
JS_ClearPendingException(cx);
|
|
|
|
return;
|
2014-05-21 10:11:31 +04:00
|
|
|
}
|
|
|
|
|
2014-05-29 13:14:45 +04:00
|
|
|
// Notify application of default adapter change
|
|
|
|
RootedDictionary<BluetoothAttributeEventInit> init(cx);
|
2014-06-19 08:03:09 +04:00
|
|
|
init.mAttrs = value;
|
2014-05-29 13:14:45 +04:00
|
|
|
nsRefPtr<BluetoothAttributeEvent> event =
|
|
|
|
BluetoothAttributeEvent::Constructor(this,
|
|
|
|
NS_LITERAL_STRING("attributechanged"),
|
|
|
|
init);
|
|
|
|
DispatchTrustedEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothManager::Notify(const BluetoothSignal& aData)
|
|
|
|
{
|
|
|
|
BT_LOGD("[M] %s", NS_ConvertUTF16toUTF8(aData.name()).get());
|
|
|
|
|
|
|
|
if (aData.name().EqualsLiteral("AdapterAdded")) {
|
|
|
|
HandleAdapterAdded(aData.value());
|
|
|
|
} else if (aData.name().EqualsLiteral("AdapterRemoved")) {
|
|
|
|
HandleAdapterRemoved(aData.value());
|
|
|
|
} else {
|
|
|
|
BT_WARNING("Not handling manager signal: %s",
|
|
|
|
NS_ConvertUTF16toUTF8(aData.name()).get());
|
|
|
|
}
|
2014-05-21 10:11:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
BluetoothManager::WrapObject(JSContext* aCx)
|
|
|
|
{
|
|
|
|
return BluetoothManagerBinding::Wrap(aCx, this);
|
|
|
|
}
|