2012-02-20 07:15:19 -08:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
2012-08-16 19:53:45 -07:00
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
2012-02-20 07:15:19 -08: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/. */
|
|
|
|
|
2012-07-17 20:41:54 -07:00
|
|
|
#include "base/basictypes.h"
|
2012-08-19 21:23:32 -07:00
|
|
|
#include "GeneratedEvents.h"
|
2012-08-22 03:37:08 -07:00
|
|
|
#include "nsContentUtils.h"
|
2013-05-23 07:10:00 -07:00
|
|
|
#include "nsCxPusher.h"
|
2012-02-20 07:15:19 -08:00
|
|
|
#include "nsDOMClassInfo.h"
|
2012-09-29 03:26:46 -07:00
|
|
|
#include "nsIDOMBluetoothDeviceEvent.h"
|
2013-08-02 03:33:15 -07:00
|
|
|
#include "nsIDOMBluetoothStatusChangedEvent.h"
|
2012-10-18 06:20:54 -07:00
|
|
|
#include "nsTArrayHelpers.h"
|
2013-01-28 22:52:39 -08:00
|
|
|
#include "DOMRequest.h"
|
2012-08-22 03:37:08 -07:00
|
|
|
#include "nsThreadUtils.h"
|
2012-07-17 20:41:54 -07:00
|
|
|
|
2012-09-29 03:26:46 -07:00
|
|
|
#include "mozilla/dom/bluetooth/BluetoothTypes.h"
|
2013-08-12 01:32:53 -07:00
|
|
|
#include "mozilla/dom/BluetoothAdapterBinding.h"
|
2012-09-28 03:59:12 -07:00
|
|
|
#include "mozilla/dom/ContentChild.h"
|
2012-02-23 07:55:48 -08:00
|
|
|
#include "mozilla/LazyIdleThread.h"
|
2012-03-27 16:17:35 -07:00
|
|
|
#include "mozilla/Util.h"
|
|
|
|
|
2013-07-29 02:32:35 -07:00
|
|
|
#include "BluetoothAdapter.h"
|
|
|
|
#include "BluetoothDevice.h"
|
|
|
|
#include "BluetoothReplyRunnable.h"
|
|
|
|
#include "BluetoothService.h"
|
|
|
|
#include "BluetoothUtils.h"
|
|
|
|
|
2012-07-17 20:41:54 -07:00
|
|
|
using namespace mozilla;
|
2013-08-12 01:32:53 -07:00
|
|
|
using namespace mozilla::dom;
|
2012-07-17 20:41:54 -07:00
|
|
|
|
2012-03-04 19:54:01 -08:00
|
|
|
USING_BLUETOOTH_NAMESPACE
|
2012-02-23 07:55:48 -08:00
|
|
|
|
2013-08-01 18:29:05 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(BluetoothAdapter)
|
|
|
|
|
2012-07-31 21:53:04 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(BluetoothAdapter,
|
|
|
|
nsDOMEventTargetHelper)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mJsUuids)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mJsDeviceAddresses)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(BluetoothAdapter,
|
2012-03-04 19:54:01 -08:00
|
|
|
nsDOMEventTargetHelper)
|
2012-07-31 21:53:04 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
2012-03-04 19:54:01 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
2012-02-20 07:15:19 -08:00
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(BluetoothAdapter,
|
2012-03-04 19:54:01 -08:00
|
|
|
nsDOMEventTargetHelper)
|
2012-07-31 21:53:04 -07:00
|
|
|
tmp->Unroot();
|
2012-03-04 19:54:01 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
2012-02-23 07:55:48 -08:00
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
// QueryInterface implementation for BluetoothAdapter
|
2012-03-04 19:54:01 -08:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(BluetoothAdapter)
|
2012-02-23 07:55:48 -08:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(BluetoothAdapter, nsDOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(BluetoothAdapter, nsDOMEventTargetHelper)
|
|
|
|
|
2013-04-26 03:48:21 -07:00
|
|
|
class GetDevicesTask : public BluetoothReplyRunnable
|
2012-08-16 19:53:45 -07:00
|
|
|
{
|
|
|
|
public:
|
2013-04-26 03:48:21 -07:00
|
|
|
GetDevicesTask(BluetoothAdapter* aAdapterPtr,
|
2012-08-16 19:53:45 -07:00
|
|
|
nsIDOMDOMRequest* aReq) :
|
2012-09-19 16:37:42 -07:00
|
|
|
BluetoothReplyRunnable(aReq),
|
|
|
|
mAdapterPtr(aAdapterPtr)
|
2012-08-16 19:53:45 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aReq && aAdapterPtr);
|
|
|
|
}
|
|
|
|
|
2013-04-02 16:06:14 -07:00
|
|
|
virtual bool ParseSuccessfulReply(JS::Value* aValue)
|
2012-08-16 19:53:45 -07:00
|
|
|
{
|
|
|
|
*aValue = JSVAL_VOID;
|
2013-01-28 22:52:48 -08:00
|
|
|
|
|
|
|
const BluetoothValue& v = mReply->get_BluetoothReplySuccess().value();
|
2012-08-16 19:53:45 -07:00
|
|
|
if (v.type() != BluetoothValue::TArrayOfBluetoothNamedValue) {
|
|
|
|
NS_WARNING("Not a BluetoothNamedValue array!");
|
2013-01-28 22:52:48 -08:00
|
|
|
SetError(NS_LITERAL_STRING("BluetoothReplyTypeError"));
|
2012-08-16 19:53:45 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-28 22:52:48 -08:00
|
|
|
const InfallibleTArray<BluetoothNamedValue>& values =
|
|
|
|
v.get_ArrayOfBluetoothNamedValue();
|
|
|
|
|
2012-08-16 19:53:45 -07:00
|
|
|
nsTArray<nsRefPtr<BluetoothDevice> > devices;
|
|
|
|
JSObject* JsDevices;
|
2013-01-28 22:52:48 -08:00
|
|
|
for (uint32_t i = 0; i < values.Length(); i++) {
|
|
|
|
const BluetoothValue properties = values[i].value();
|
|
|
|
if (properties.type() != BluetoothValue::TArrayOfBluetoothNamedValue) {
|
2012-08-16 19:53:45 -07:00
|
|
|
NS_WARNING("Not a BluetoothNamedValue array!");
|
2013-01-28 22:52:48 -08:00
|
|
|
SetError(NS_LITERAL_STRING("BluetoothReplyTypeError"));
|
2012-08-16 19:53:45 -07:00
|
|
|
return false;
|
|
|
|
}
|
2013-01-28 22:52:48 -08:00
|
|
|
nsRefPtr<BluetoothDevice> d =
|
|
|
|
BluetoothDevice::Create(mAdapterPtr->GetOwner(),
|
|
|
|
mAdapterPtr->GetPath(),
|
|
|
|
properties);
|
2012-08-16 19:53:45 -07:00
|
|
|
devices.AppendElement(d);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsIScriptContext* sc = mAdapterPtr->GetContextForEventHandlers(&rv);
|
|
|
|
if (!sc) {
|
|
|
|
NS_WARNING("Cannot create script context!");
|
2013-01-28 22:52:48 -08:00
|
|
|
SetError(NS_LITERAL_STRING("BluetoothScriptContextError"));
|
2012-08-16 19:53:45 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-23 07:10:00 -07:00
|
|
|
AutoPushJSContext cx(sc->GetNativeContext());
|
|
|
|
rv = nsTArrayToJSArray(cx, devices, &JsDevices);
|
2013-01-28 22:52:48 -08:00
|
|
|
if (!JsDevices) {
|
|
|
|
NS_WARNING("Cannot create JS array!");
|
|
|
|
SetError(NS_LITERAL_STRING("BluetoothError"));
|
2012-08-16 19:53:45 -07:00
|
|
|
return false;
|
|
|
|
}
|
2013-01-28 22:52:48 -08:00
|
|
|
|
|
|
|
aValue->setObject(*JsDevices);
|
2012-08-16 19:53:45 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ReleaseMembers()
|
|
|
|
{
|
|
|
|
BluetoothReplyRunnable::ReleaseMembers();
|
|
|
|
mAdapterPtr = nullptr;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
nsRefPtr<BluetoothAdapter> mAdapterPtr;
|
|
|
|
};
|
|
|
|
|
2013-05-10 03:58:59 -07:00
|
|
|
class GetScoConnectionStatusTask : public BluetoothReplyRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
GetScoConnectionStatusTask(nsIDOMDOMRequest* aReq) :
|
|
|
|
BluetoothReplyRunnable(aReq)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aReq);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool ParseSuccessfulReply(JS::Value* aValue)
|
|
|
|
{
|
|
|
|
*aValue = JSVAL_VOID;
|
|
|
|
|
|
|
|
const BluetoothValue& v = mReply->get_BluetoothReplySuccess().value();
|
|
|
|
if (v.type() != BluetoothValue::Tbool) {
|
|
|
|
NS_WARNING("Not a boolean!");
|
|
|
|
SetError(NS_LITERAL_STRING("BluetoothReplyTypeError"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
aValue->setBoolean(v.get_bool());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ReleaseMembers()
|
|
|
|
{
|
|
|
|
BluetoothReplyRunnable::ReleaseMembers();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-08-17 03:35:59 -07:00
|
|
|
static int kCreatePairedDeviceTimeout = 50000; // unit: msec
|
|
|
|
|
2013-01-28 22:52:39 -08:00
|
|
|
BluetoothAdapter::BluetoothAdapter(nsPIDOMWindow* aWindow,
|
|
|
|
const BluetoothValue& aValue)
|
2013-08-12 01:32:53 -07:00
|
|
|
: nsDOMEventTargetHelper(aWindow)
|
|
|
|
, BluetoothPropertyContainer(BluetoothObjectType::TYPE_ADAPTER)
|
|
|
|
, mJsUuids(nullptr)
|
|
|
|
, mJsDeviceAddresses(nullptr)
|
2013-01-28 22:52:39 -08:00
|
|
|
, mDiscoverable(false)
|
|
|
|
, mDiscovering(false)
|
|
|
|
, mPairable(false)
|
|
|
|
, mPowered(false)
|
|
|
|
, mIsRooted(false)
|
2012-06-02 11:23:16 -07:00
|
|
|
{
|
2013-01-28 22:52:39 -08:00
|
|
|
MOZ_ASSERT(aWindow);
|
2013-08-12 01:32:53 -07:00
|
|
|
MOZ_ASSERT(IsDOMBinding());
|
2013-01-28 22:52:39 -08:00
|
|
|
|
|
|
|
BindToOwner(aWindow);
|
2012-08-08 10:47:09 -07:00
|
|
|
const InfallibleTArray<BluetoothNamedValue>& values =
|
|
|
|
aValue.get_ArrayOfBluetoothNamedValue();
|
|
|
|
for (uint32_t i = 0; i < values.Length(); ++i) {
|
|
|
|
SetPropertyByValue(values[i]);
|
|
|
|
}
|
2013-02-08 02:03:09 -08:00
|
|
|
|
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
|
|
|
NS_ENSURE_TRUE_VOID(bs);
|
2013-03-15 00:15:47 -07:00
|
|
|
bs->RegisterBluetoothSignalHandler(NS_LITERAL_STRING(KEY_ADAPTER), this);
|
2012-07-31 21:53:04 -07:00
|
|
|
}
|
2012-06-02 11:23:16 -07:00
|
|
|
|
|
|
|
BluetoothAdapter::~BluetoothAdapter()
|
|
|
|
{
|
2013-04-16 14:54:00 -07:00
|
|
|
Unroot();
|
2012-07-17 20:41:54 -07:00
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
|
|
|
// We can be null on shutdown, where this might happen
|
2013-02-08 02:03:09 -08:00
|
|
|
NS_ENSURE_TRUE_VOID(bs);
|
2013-03-15 00:15:47 -07:00
|
|
|
bs->UnregisterBluetoothSignalHandler(NS_LITERAL_STRING(KEY_ADAPTER), this);
|
2012-07-31 21:53:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothAdapter::Unroot()
|
|
|
|
{
|
|
|
|
if (!mIsRooted) {
|
|
|
|
return;
|
|
|
|
}
|
2012-11-27 17:37:57 -08:00
|
|
|
mJsUuids = nullptr;
|
|
|
|
mJsDeviceAddresses = nullptr;
|
2012-07-31 21:53:04 -07:00
|
|
|
NS_DROP_JS_OBJECTS(this, BluetoothAdapter);
|
|
|
|
mIsRooted = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothAdapter::Root()
|
|
|
|
{
|
|
|
|
if (mIsRooted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NS_HOLD_JS_OBJECTS(this, BluetoothAdapter);
|
|
|
|
mIsRooted = true;
|
2012-07-17 20:41:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothAdapter::SetPropertyByValue(const BluetoothNamedValue& aValue)
|
|
|
|
{
|
|
|
|
const nsString& name = aValue.name();
|
|
|
|
const BluetoothValue& value = aValue.value();
|
|
|
|
if (name.EqualsLiteral("Name")) {
|
|
|
|
mName = value.get_nsString();
|
|
|
|
} else if (name.EqualsLiteral("Address")) {
|
|
|
|
mAddress = value.get_nsString();
|
2012-07-31 21:53:04 -07:00
|
|
|
} else if (name.EqualsLiteral("Path")) {
|
|
|
|
mPath = value.get_nsString();
|
2012-07-17 20:41:54 -07:00
|
|
|
} else if (name.EqualsLiteral("Discoverable")) {
|
|
|
|
mDiscoverable = value.get_bool();
|
2012-07-31 21:53:04 -07:00
|
|
|
} else if (name.EqualsLiteral("Discovering")) {
|
|
|
|
mDiscovering = value.get_bool();
|
2012-07-17 20:41:54 -07:00
|
|
|
} else if (name.EqualsLiteral("Pairable")) {
|
|
|
|
mPairable = value.get_bool();
|
|
|
|
} else if (name.EqualsLiteral("Powered")) {
|
|
|
|
mPowered = value.get_bool();
|
|
|
|
} else if (name.EqualsLiteral("PairableTimeout")) {
|
|
|
|
mPairableTimeout = value.get_uint32_t();
|
|
|
|
} else if (name.EqualsLiteral("DiscoverableTimeout")) {
|
|
|
|
mDiscoverableTimeout = value.get_uint32_t();
|
|
|
|
} else if (name.EqualsLiteral("Class")) {
|
|
|
|
mClass = value.get_uint32_t();
|
|
|
|
} else if (name.EqualsLiteral("UUIDs")) {
|
|
|
|
mUuids = value.get_ArrayOfnsString();
|
2012-07-31 21:53:04 -07:00
|
|
|
nsresult rv;
|
|
|
|
nsIScriptContext* sc = GetContextForEventHandlers(&rv);
|
2013-01-28 22:52:39 -08:00
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
|
|
|
|
2013-05-23 07:10:00 -07:00
|
|
|
AutoPushJSContext cx(sc->GetNativeContext());
|
2013-06-18 03:00:38 -07:00
|
|
|
JS::Rooted<JSObject*> uuids(cx);
|
|
|
|
if (NS_FAILED(nsTArrayToJSArray(cx, mUuids, uuids.address()))) {
|
2013-01-28 22:52:39 -08:00
|
|
|
NS_WARNING("Cannot set JS UUIDs object!");
|
|
|
|
return;
|
2012-07-31 21:53:04 -07:00
|
|
|
}
|
2013-06-18 03:00:38 -07:00
|
|
|
mJsUuids = uuids;
|
2013-01-28 22:52:39 -08:00
|
|
|
Root();
|
2012-07-31 21:53:04 -07:00
|
|
|
} else if (name.EqualsLiteral("Devices")) {
|
|
|
|
mDeviceAddresses = value.get_ArrayOfnsString();
|
2013-04-26 03:48:21 -07:00
|
|
|
|
|
|
|
uint32_t length = mDeviceAddresses.Length();
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
mDeviceAddresses[i] = GetAddressFromObjectPath(mDeviceAddresses[i]);
|
|
|
|
}
|
|
|
|
|
2012-07-31 21:53:04 -07:00
|
|
|
nsresult rv;
|
|
|
|
nsIScriptContext* sc = GetContextForEventHandlers(&rv);
|
2013-01-28 22:52:39 -08:00
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
|
|
|
|
2013-05-23 07:10:00 -07:00
|
|
|
AutoPushJSContext cx(sc->GetNativeContext());
|
2013-06-18 03:00:38 -07:00
|
|
|
JS::Rooted<JSObject*> deviceAddresses(cx);
|
2013-05-23 07:10:00 -07:00
|
|
|
if (NS_FAILED(nsTArrayToJSArray(cx, mDeviceAddresses,
|
2013-06-18 03:00:38 -07:00
|
|
|
deviceAddresses.address()))) {
|
2013-01-28 22:52:39 -08:00
|
|
|
NS_WARNING("Cannot set JS Devices object!");
|
|
|
|
return;
|
2012-07-31 21:53:04 -07:00
|
|
|
}
|
2013-06-18 03:00:38 -07:00
|
|
|
mJsDeviceAddresses = deviceAddresses;
|
2013-01-28 22:52:39 -08:00
|
|
|
Root();
|
2012-07-17 20:41:54 -07:00
|
|
|
} else {
|
|
|
|
#ifdef DEBUG
|
|
|
|
nsCString warningMsg;
|
|
|
|
warningMsg.AssignLiteral("Not handling adapter property: ");
|
|
|
|
warningMsg.Append(NS_ConvertUTF16toUTF8(name));
|
|
|
|
NS_WARNING(warningMsg.get());
|
|
|
|
#endif
|
2012-06-02 11:23:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<BluetoothAdapter>
|
2013-01-28 22:52:39 -08:00
|
|
|
BluetoothAdapter::Create(nsPIDOMWindow* aWindow, const BluetoothValue& aValue)
|
2012-07-17 20:41:54 -07:00
|
|
|
{
|
2013-01-28 22:52:39 -08:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aWindow);
|
2012-07-17 20:41:54 -07:00
|
|
|
|
2013-01-28 22:52:39 -08:00
|
|
|
nsRefPtr<BluetoothAdapter> adapter = new BluetoothAdapter(aWindow, aValue);
|
2012-06-02 11:23:16 -07:00
|
|
|
return adapter.forget();
|
|
|
|
}
|
|
|
|
|
2012-07-17 20:41:54 -07:00
|
|
|
void
|
|
|
|
BluetoothAdapter::Notify(const BluetoothSignal& aData)
|
|
|
|
{
|
2012-08-17 03:35:59 -07:00
|
|
|
InfallibleTArray<BluetoothNamedValue> arr;
|
|
|
|
|
2013-01-29 02:37:51 -08:00
|
|
|
BT_LOG("[A] %s: %s", __FUNCTION__, NS_ConvertUTF16toUTF8(aData.name()).get());
|
|
|
|
|
2013-01-28 22:52:30 -08:00
|
|
|
BluetoothValue v = aData.value();
|
2012-07-17 20:41:54 -07:00
|
|
|
if (aData.name().EqualsLiteral("DeviceFound")) {
|
2012-08-20 19:54:28 -07:00
|
|
|
nsRefPtr<BluetoothDevice> device = BluetoothDevice::Create(GetOwner(), mPath, aData.value());
|
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
2013-03-09 03:34:29 -08:00
|
|
|
NS_NewDOMBluetoothDeviceEvent(getter_AddRefs(event), this, nullptr, nullptr);
|
2012-08-20 19:54:28 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMBluetoothDeviceEvent> e = do_QueryInterface(event);
|
|
|
|
e->InitBluetoothDeviceEvent(NS_LITERAL_STRING("devicefound"),
|
|
|
|
false, false, device);
|
2012-09-27 13:11:31 -07:00
|
|
|
DispatchTrustedEvent(event);
|
2012-07-31 21:53:04 -07:00
|
|
|
} else if (aData.name().EqualsLiteral("PropertyChanged")) {
|
2013-08-02 03:33:15 -07:00
|
|
|
MOZ_ASSERT(v.type() == BluetoothValue::TArrayOfBluetoothNamedValue);
|
2013-01-28 22:52:30 -08:00
|
|
|
const InfallibleTArray<BluetoothNamedValue>& arr =
|
|
|
|
v.get_ArrayOfBluetoothNamedValue();
|
2012-09-18 20:19:45 -07:00
|
|
|
|
2013-08-02 03:33:15 -07:00
|
|
|
MOZ_ASSERT(arr.Length() == 1);
|
2013-01-28 22:52:30 -08:00
|
|
|
SetPropertyByValue(arr[0]);
|
2013-08-02 03:33:15 -07:00
|
|
|
} else if (aData.name().EqualsLiteral(PAIRED_STATUS_CHANGED_ID) ||
|
|
|
|
aData.name().EqualsLiteral(HFP_STATUS_CHANGED_ID) ||
|
|
|
|
aData.name().EqualsLiteral(SCO_STATUS_CHANGED_ID) ||
|
|
|
|
aData.name().EqualsLiteral(A2DP_STATUS_CHANGED_ID)) {
|
|
|
|
MOZ_ASSERT(v.type() == BluetoothValue::TArrayOfBluetoothNamedValue);
|
|
|
|
const InfallibleTArray<BluetoothNamedValue>& arr =
|
|
|
|
v.get_ArrayOfBluetoothNamedValue();
|
|
|
|
|
|
|
|
MOZ_ASSERT(arr.Length() == 2 &&
|
|
|
|
arr[0].value().type() == BluetoothValue::TnsString &&
|
|
|
|
arr[1].value().type() == BluetoothValue::Tbool);
|
|
|
|
nsString address = arr[0].value().get_nsString();
|
|
|
|
bool status = arr[1].value().get_bool();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
|
|
|
NS_NewDOMBluetoothStatusChangedEvent(
|
|
|
|
getter_AddRefs(event), this, nullptr, nullptr);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMBluetoothStatusChangedEvent> e = do_QueryInterface(event);
|
|
|
|
e->InitBluetoothStatusChangedEvent(aData.name(), false, false,
|
|
|
|
address, status);
|
|
|
|
DispatchTrustedEvent(event);
|
2012-07-17 20:41:54 -07:00
|
|
|
} else {
|
|
|
|
#ifdef DEBUG
|
|
|
|
nsCString warningMsg;
|
2012-09-05 20:06:20 -07:00
|
|
|
warningMsg.AssignLiteral("Not handling adapter signal: ");
|
2012-07-17 20:41:54 -07:00
|
|
|
warningMsg.Append(NS_ConvertUTF16toUTF8(aData.name()));
|
|
|
|
NS_WARNING(warningMsg.get());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
already_AddRefed<DOMRequest>
|
|
|
|
BluetoothAdapter::StartStopDiscovery(bool aStart, ErrorResult& aRv)
|
2012-07-17 20:41:54 -07:00
|
|
|
{
|
2013-08-12 01:32:53 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win = GetOwner();
|
|
|
|
if (!win) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-07-17 20:41:54 -07:00
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
nsRefPtr<DOMRequest> request = new DOMRequest(win);
|
2013-01-28 22:52:48 -08:00
|
|
|
nsRefPtr<BluetoothVoidReplyRunnable> results =
|
2013-08-12 01:32:53 -07:00
|
|
|
new BluetoothVoidReplyRunnable(request);
|
2012-07-17 20:41:54 -07:00
|
|
|
|
2013-01-28 22:52:48 -08:00
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
2013-08-12 01:32:53 -07:00
|
|
|
if (!bs) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
nsresult rv;
|
2012-07-17 20:41:54 -07:00
|
|
|
if (aStart) {
|
2013-03-15 00:15:47 -07:00
|
|
|
rv = bs->StartDiscoveryInternal(results);
|
2012-07-17 20:41:54 -07:00
|
|
|
} else {
|
2013-03-15 00:15:47 -07:00
|
|
|
rv = bs->StopDiscoveryInternal(results);
|
2012-07-17 20:41:54 -07:00
|
|
|
}
|
2013-08-12 01:32:53 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2013-01-28 22:52:48 -08:00
|
|
|
NS_WARNING("Start/Stop Discovery failed!");
|
2013-08-12 01:32:53 -07:00
|
|
|
aRv.Throw(rv);
|
|
|
|
return nullptr;
|
2012-07-17 20:41:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// mDiscovering is not set here, we'll get a Property update from our external
|
|
|
|
// protocol to tell us that it's been set.
|
2013-01-28 22:52:48 -08:00
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
return request.forget();
|
2013-08-12 09:08:03 -07:00
|
|
|
}
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
already_AddRefed<DOMRequest>
|
|
|
|
BluetoothAdapter::StartDiscovery(ErrorResult& aRv)
|
2013-08-12 09:08:03 -07:00
|
|
|
{
|
2013-08-12 01:32:53 -07:00
|
|
|
return StartStopDiscovery(true, aRv);
|
2013-08-12 09:08:03 -07:00
|
|
|
}
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
already_AddRefed<DOMRequest>
|
|
|
|
BluetoothAdapter::StopDiscovery(ErrorResult& aRv)
|
2013-08-12 09:08:03 -07:00
|
|
|
{
|
2013-08-12 01:32:53 -07:00
|
|
|
return StartStopDiscovery(false, aRv);
|
2013-08-12 09:08:03 -07:00
|
|
|
}
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
JS::Value
|
|
|
|
BluetoothAdapter::GetDevices(JSContext* aContext, ErrorResult& aRv)
|
2013-08-12 09:08:03 -07:00
|
|
|
{
|
2013-08-12 01:32:53 -07:00
|
|
|
if (!mJsDeviceAddresses) {
|
2013-08-12 09:08:03 -07:00
|
|
|
NS_WARNING("Devices not yet set!\n");
|
2013-08-12 01:32:53 -07:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return JS::NullValue();
|
2013-08-12 01:32:53 -07:00
|
|
|
}
|
2013-08-12 01:32:53 -07:00
|
|
|
|
|
|
|
return JS::ObjectValue(*xpc_UnmarkGrayObject(mJsDeviceAddresses));
|
2013-08-12 09:08:03 -07:00
|
|
|
}
|
2013-08-12 01:32:53 -07:00
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
JS::Value
|
|
|
|
BluetoothAdapter::GetUuids(JSContext* aContext, ErrorResult& aRv)
|
2013-08-12 09:08:03 -07:00
|
|
|
{
|
2013-08-12 01:32:53 -07:00
|
|
|
if (!mJsUuids) {
|
2013-08-12 09:08:03 -07:00
|
|
|
NS_WARNING("UUIDs not yet set!\n");
|
2013-08-12 01:32:53 -07:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return JS::NullValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
return JS::ObjectValue(*xpc_UnmarkGrayObject(mJsUuids));
|
2012-07-17 20:41:54 -07:00
|
|
|
}
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
already_AddRefed<DOMRequest>
|
|
|
|
BluetoothAdapter::SetName(const nsAString& aName, ErrorResult& aRv)
|
2012-07-31 21:53:04 -07:00
|
|
|
{
|
|
|
|
if (mName.Equals(aName)) {
|
2013-08-12 01:32:53 -07:00
|
|
|
return FirePropertyAlreadySet(GetOwner(), aRv);
|
2012-07-31 21:53:04 -07:00
|
|
|
}
|
|
|
|
nsString name(aName);
|
|
|
|
BluetoothValue value(name);
|
|
|
|
BluetoothNamedValue property(NS_LITERAL_STRING("Name"), value);
|
2013-08-12 01:32:53 -07:00
|
|
|
return SetProperty(GetOwner(), property, aRv);
|
2012-07-31 21:53:04 -07:00
|
|
|
}
|
2013-03-15 00:15:47 -07:00
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
already_AddRefed<DOMRequest>
|
|
|
|
BluetoothAdapter::SetDiscoverable(bool aDiscoverable, ErrorResult& aRv)
|
2012-07-31 21:53:04 -07:00
|
|
|
{
|
|
|
|
if (aDiscoverable == mDiscoverable) {
|
2013-08-12 01:32:53 -07:00
|
|
|
return FirePropertyAlreadySet(GetOwner(), aRv);
|
2012-07-31 21:53:04 -07:00
|
|
|
}
|
|
|
|
BluetoothValue value(aDiscoverable);
|
|
|
|
BluetoothNamedValue property(NS_LITERAL_STRING("Discoverable"), value);
|
2013-08-12 01:32:53 -07:00
|
|
|
return SetProperty(GetOwner(), property, aRv);
|
2012-07-31 21:53:04 -07:00
|
|
|
}
|
2013-08-12 01:32:53 -07:00
|
|
|
|
|
|
|
already_AddRefed<DOMRequest>
|
|
|
|
BluetoothAdapter::SetDiscoverableTimeout(uint32_t aDiscoverableTimeout, ErrorResult& aRv)
|
2012-07-31 21:53:04 -07:00
|
|
|
{
|
|
|
|
if (aDiscoverableTimeout == mDiscoverableTimeout) {
|
2013-08-12 01:32:53 -07:00
|
|
|
return FirePropertyAlreadySet(GetOwner(), aRv);
|
2012-07-31 21:53:04 -07:00
|
|
|
}
|
|
|
|
BluetoothValue value(aDiscoverableTimeout);
|
|
|
|
BluetoothNamedValue property(NS_LITERAL_STRING("DiscoverableTimeout"), value);
|
2013-08-12 01:32:53 -07:00
|
|
|
return SetProperty(GetOwner(), property, aRv);
|
2012-07-31 21:53:04 -07:00
|
|
|
}
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
already_AddRefed<DOMRequest>
|
|
|
|
BluetoothAdapter::GetConnectedDevices(uint16_t aProfileId, ErrorResult& aRv)
|
2013-04-26 03:48:21 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win = GetOwner();
|
|
|
|
if (!win) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<DOMRequest> request = new DOMRequest(win);
|
2013-04-26 03:48:21 -07:00
|
|
|
nsRefPtr<BluetoothReplyRunnable> results =
|
2013-08-12 01:32:53 -07:00
|
|
|
new GetDevicesTask(this, request);
|
2013-04-26 03:48:21 -07:00
|
|
|
|
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
2013-08-12 01:32:53 -07:00
|
|
|
if (!bs) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
nsresult rv = bs->GetConnectedDevicePropertiesInternal(aProfileId, results);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-04-26 03:48:21 -07:00
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
return request.forget();
|
2013-04-26 03:48:21 -07:00
|
|
|
}
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
already_AddRefed<DOMRequest>
|
|
|
|
BluetoothAdapter::GetPairedDevices(ErrorResult& aRv)
|
2012-08-16 19:53:45 -07:00
|
|
|
{
|
2013-08-12 01:32:53 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win = GetOwner();
|
|
|
|
if (!win) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-08-16 19:53:45 -07:00
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
nsRefPtr<DOMRequest> request = new DOMRequest(win);
|
2013-01-28 22:52:48 -08:00
|
|
|
nsRefPtr<BluetoothReplyRunnable> results =
|
2013-08-12 01:32:53 -07:00
|
|
|
new GetDevicesTask(this, request);
|
2012-08-16 19:53:45 -07:00
|
|
|
|
2013-01-28 22:52:48 -08:00
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
2013-08-12 01:32:53 -07:00
|
|
|
if (!bs) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
nsresult rv = bs->GetPairedDevicePropertiesInternal(mDeviceAddresses, results);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-08-16 19:53:45 -07:00
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
return request.forget();
|
2012-08-16 19:53:45 -07:00
|
|
|
}
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
already_AddRefed<DOMRequest>
|
2013-08-12 01:34:28 -07:00
|
|
|
BluetoothAdapter::PairUnpair(bool aPair, BluetoothDevice& aDevice,
|
2013-08-12 01:32:53 -07:00
|
|
|
ErrorResult& aRv)
|
2012-08-17 03:35:59 -07:00
|
|
|
{
|
2013-08-12 01:32:53 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win = GetOwner();
|
|
|
|
if (!win) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-08-17 03:35:59 -07:00
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
nsRefPtr<DOMRequest> request = new DOMRequest(win);
|
2013-01-28 22:52:48 -08:00
|
|
|
nsRefPtr<BluetoothVoidReplyRunnable> results =
|
2013-08-12 01:32:53 -07:00
|
|
|
new BluetoothVoidReplyRunnable(request);
|
2012-08-17 03:35:59 -07:00
|
|
|
|
2013-03-15 00:15:47 -07:00
|
|
|
nsAutoString addr;
|
2013-08-12 01:34:28 -07:00
|
|
|
aDevice.GetAddress(addr);
|
2012-08-17 03:35:59 -07:00
|
|
|
|
2013-01-28 22:52:48 -08:00
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
2013-08-12 01:32:53 -07:00
|
|
|
if (!bs) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
nsresult rv;
|
2012-08-17 03:35:59 -07:00
|
|
|
if (aPair) {
|
2013-03-15 00:15:47 -07:00
|
|
|
rv = bs->CreatePairedDeviceInternal(addr,
|
2012-08-17 03:35:59 -07:00
|
|
|
kCreatePairedDeviceTimeout,
|
|
|
|
results);
|
|
|
|
} else {
|
2013-03-15 00:15:47 -07:00
|
|
|
rv = bs->RemoveDeviceInternal(addr, results);
|
2012-08-17 03:35:59 -07:00
|
|
|
}
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Pair/Unpair failed!");
|
2013-08-12 01:32:53 -07:00
|
|
|
aRv.Throw(rv);
|
|
|
|
return nullptr;
|
2012-08-17 03:35:59 -07:00
|
|
|
}
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
return request.forget();
|
2012-08-17 03:35:59 -07:00
|
|
|
}
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
already_AddRefed<DOMRequest>
|
2013-08-12 01:34:28 -07:00
|
|
|
BluetoothAdapter::Pair(BluetoothDevice& aDevice, ErrorResult& aRv)
|
2012-08-17 03:35:59 -07:00
|
|
|
{
|
2013-08-12 01:32:53 -07:00
|
|
|
return PairUnpair(true, aDevice, aRv);
|
2012-08-17 03:35:59 -07:00
|
|
|
}
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
already_AddRefed<DOMRequest>
|
2013-08-12 01:34:28 -07:00
|
|
|
BluetoothAdapter::Unpair(BluetoothDevice& aDevice, ErrorResult& aRv)
|
2012-08-17 03:35:59 -07:00
|
|
|
{
|
2013-08-12 01:32:53 -07:00
|
|
|
return PairUnpair(false, aDevice, aRv);
|
2012-08-17 03:35:59 -07:00
|
|
|
}
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
already_AddRefed<DOMRequest>
|
2012-09-19 16:37:42 -07:00
|
|
|
BluetoothAdapter::SetPinCode(const nsAString& aDeviceAddress,
|
2013-08-12 01:32:53 -07:00
|
|
|
const nsAString& aPinCode, ErrorResult& aRv)
|
2012-08-17 03:35:59 -07:00
|
|
|
{
|
2013-08-12 01:32:53 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win = GetOwner();
|
|
|
|
if (!win) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-09-19 16:37:42 -07:00
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
nsRefPtr<DOMRequest> request = new DOMRequest(win);
|
2013-01-28 22:52:48 -08:00
|
|
|
nsRefPtr<BluetoothVoidReplyRunnable> results =
|
2013-08-12 01:32:53 -07:00
|
|
|
new BluetoothVoidReplyRunnable(request);
|
2012-09-19 16:37:42 -07:00
|
|
|
|
2013-01-28 22:52:48 -08:00
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
2013-08-12 01:32:53 -07:00
|
|
|
if (!bs) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (!bs->SetPinCodeInternal(aDeviceAddress, aPinCode, results)) {
|
2012-09-19 16:37:42 -07:00
|
|
|
NS_WARNING("SetPinCode failed!");
|
2013-08-12 01:32:53 -07:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
2012-09-19 16:37:42 -07:00
|
|
|
}
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
return request.forget();
|
2012-08-17 03:35:59 -07:00
|
|
|
}
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
already_AddRefed<DOMRequest>
|
2012-09-19 16:37:42 -07:00
|
|
|
BluetoothAdapter::SetPasskey(const nsAString& aDeviceAddress, uint32_t aPasskey,
|
2013-08-12 01:32:53 -07:00
|
|
|
ErrorResult& aRv)
|
2012-08-17 03:35:59 -07:00
|
|
|
{
|
2013-08-12 01:32:53 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win = GetOwner();
|
|
|
|
if (!win) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-09-19 16:37:42 -07:00
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
nsRefPtr<DOMRequest> request = new DOMRequest(win);
|
2013-01-28 22:52:48 -08:00
|
|
|
nsRefPtr<BluetoothVoidReplyRunnable> results =
|
2013-08-12 01:32:53 -07:00
|
|
|
new BluetoothVoidReplyRunnable(request);
|
2012-09-19 16:37:42 -07:00
|
|
|
|
2013-01-28 22:52:48 -08:00
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
2013-08-12 01:32:53 -07:00
|
|
|
if (!bs) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (bs->SetPasskeyInternal(aDeviceAddress, aPasskey, results)) {
|
2012-09-19 16:37:42 -07:00
|
|
|
NS_WARNING("SetPasskeyInternal failed!");
|
2013-08-12 01:32:53 -07:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
2012-09-19 16:37:42 -07:00
|
|
|
}
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
return request.forget();
|
2012-08-17 03:35:59 -07:00
|
|
|
}
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
already_AddRefed<DOMRequest>
|
2012-09-19 16:37:42 -07:00
|
|
|
BluetoothAdapter::SetPairingConfirmation(const nsAString& aDeviceAddress,
|
2013-08-12 01:32:53 -07:00
|
|
|
bool aConfirmation, ErrorResult& aRv)
|
2012-08-17 03:35:59 -07:00
|
|
|
{
|
2013-08-12 01:32:53 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win = GetOwner();
|
|
|
|
if (!win) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-09-19 16:37:42 -07:00
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
nsRefPtr<DOMRequest> request = new DOMRequest(win);
|
2013-01-28 22:52:48 -08:00
|
|
|
nsRefPtr<BluetoothVoidReplyRunnable> results =
|
2013-08-12 01:32:53 -07:00
|
|
|
new BluetoothVoidReplyRunnable(request);
|
2012-09-19 16:37:42 -07:00
|
|
|
|
2013-01-28 22:52:48 -08:00
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
2013-08-12 01:32:53 -07:00
|
|
|
if (!bs) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (!bs->SetPairingConfirmationInternal(aDeviceAddress,
|
|
|
|
aConfirmation,
|
|
|
|
results)) {
|
2012-09-19 16:37:42 -07:00
|
|
|
NS_WARNING("SetPairingConfirmation failed!");
|
2013-08-12 01:32:53 -07:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
2012-09-19 16:37:42 -07:00
|
|
|
}
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
return request.forget();
|
2012-08-17 03:35:59 -07:00
|
|
|
}
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
already_AddRefed<DOMRequest>
|
2012-09-19 16:37:42 -07:00
|
|
|
BluetoothAdapter::SetAuthorization(const nsAString& aDeviceAddress, bool aAllow,
|
2013-08-12 01:32:53 -07:00
|
|
|
ErrorResult& aRv)
|
2012-08-17 03:35:59 -07:00
|
|
|
{
|
2013-08-12 01:32:53 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win = GetOwner();
|
|
|
|
if (!win) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-09-19 16:37:42 -07:00
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
nsRefPtr<DOMRequest> request = new DOMRequest(win);
|
2013-01-28 22:52:48 -08:00
|
|
|
nsRefPtr<BluetoothVoidReplyRunnable> results =
|
2013-08-12 01:32:53 -07:00
|
|
|
new BluetoothVoidReplyRunnable(request);
|
2012-08-17 03:35:59 -07:00
|
|
|
|
2013-01-28 22:52:48 -08:00
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
2013-08-12 01:32:53 -07:00
|
|
|
if (!bs) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (!bs->SetAuthorizationInternal(aDeviceAddress, aAllow, results)) {
|
2012-09-19 16:37:42 -07:00
|
|
|
NS_WARNING("SetAuthorization failed!");
|
2013-08-12 01:32:53 -07:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
2012-09-19 16:37:42 -07:00
|
|
|
}
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
return request.forget();
|
2012-08-17 03:35:59 -07:00
|
|
|
}
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
already_AddRefed<DOMRequest>
|
2012-09-29 03:26:46 -07:00
|
|
|
BluetoothAdapter::Connect(const nsAString& aDeviceAddress,
|
2013-08-12 01:32:53 -07:00
|
|
|
uint16_t aProfileId, ErrorResult& aRv)
|
2012-09-29 03:26:46 -07:00
|
|
|
{
|
2013-08-12 01:32:53 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win = GetOwner();
|
|
|
|
if (!win) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-08-12 09:08:03 -07:00
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
nsRefPtr<DOMRequest> request = new DOMRequest(win);
|
2013-08-12 09:08:03 -07:00
|
|
|
nsRefPtr<BluetoothVoidReplyRunnable> results =
|
2013-08-12 01:32:53 -07:00
|
|
|
new BluetoothVoidReplyRunnable(request);
|
|
|
|
|
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
|
|
|
if (!bs) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-03-15 00:15:47 -07:00
|
|
|
bs->Connect(aDeviceAddress, aProfileId, results);
|
2012-09-29 03:26:46 -07:00
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
return request.forget();
|
2012-09-29 03:26:46 -07:00
|
|
|
}
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
already_AddRefed<DOMRequest>
|
|
|
|
BluetoothAdapter::Disconnect(uint16_t aProfileId, ErrorResult& aRv)
|
2012-09-29 03:26:46 -07:00
|
|
|
{
|
2013-08-12 01:32:53 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win = GetOwner();
|
|
|
|
if (!win) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-09-29 03:26:46 -07:00
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
nsRefPtr<DOMRequest> request = new DOMRequest(win);
|
2013-01-28 22:52:48 -08:00
|
|
|
nsRefPtr<BluetoothVoidReplyRunnable> results =
|
2013-08-12 01:32:53 -07:00
|
|
|
new BluetoothVoidReplyRunnable(request);
|
2013-01-28 22:52:48 -08:00
|
|
|
|
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
2013-08-12 01:32:53 -07:00
|
|
|
if (!bs) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-01-28 22:52:48 -08:00
|
|
|
bs->Disconnect(aProfileId, results);
|
2012-09-29 03:26:46 -07:00
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
return request.forget();
|
2012-09-29 03:26:46 -07:00
|
|
|
}
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
already_AddRefed<DOMRequest>
|
2012-09-29 03:26:46 -07:00
|
|
|
BluetoothAdapter::SendFile(const nsAString& aDeviceAddress,
|
2013-08-12 01:32:53 -07:00
|
|
|
nsIDOMBlob* aBlob, ErrorResult& aRv)
|
2012-09-29 03:26:46 -07:00
|
|
|
{
|
2013-08-12 01:32:53 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win = GetOwner();
|
|
|
|
if (!win) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-09-28 03:59:12 -07:00
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
nsRefPtr<DOMRequest> request = new DOMRequest(win);
|
2013-01-28 22:52:48 -08:00
|
|
|
nsRefPtr<BluetoothVoidReplyRunnable> results =
|
2013-08-12 01:32:53 -07:00
|
|
|
new BluetoothVoidReplyRunnable(request);
|
2012-09-28 03:59:12 -07:00
|
|
|
|
2013-01-28 22:52:48 -08:00
|
|
|
BlobChild* actor =
|
2013-08-12 01:32:53 -07:00
|
|
|
ContentChild::GetSingleton()->GetOrCreateActorForBlob(aBlob);
|
2012-09-28 03:59:12 -07:00
|
|
|
if (!actor) {
|
|
|
|
NS_WARNING("Can't create actor");
|
2013-08-12 01:32:53 -07:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
2012-09-28 03:59:12 -07:00
|
|
|
}
|
|
|
|
|
2013-01-28 22:52:48 -08:00
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
2013-08-12 01:32:53 -07:00
|
|
|
if (!bs) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-01-28 22:52:48 -08:00
|
|
|
bs->SendFile(aDeviceAddress, nullptr, actor, results);
|
2012-09-28 03:59:12 -07:00
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
return request.forget();
|
2012-09-29 03:26:46 -07:00
|
|
|
}
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
already_AddRefed<DOMRequest>
|
|
|
|
BluetoothAdapter::StopSendingFile(const nsAString& aDeviceAddress, ErrorResult& aRv)
|
2012-09-29 03:26:46 -07:00
|
|
|
{
|
2013-08-12 01:32:53 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win = GetOwner();
|
|
|
|
if (!win) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-10-03 00:09:27 -07:00
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
nsRefPtr<DOMRequest> request = new DOMRequest(win);
|
2013-01-28 22:52:48 -08:00
|
|
|
nsRefPtr<BluetoothVoidReplyRunnable> results =
|
2013-08-12 01:32:53 -07:00
|
|
|
new BluetoothVoidReplyRunnable(request);
|
2012-10-03 00:09:27 -07:00
|
|
|
|
2013-01-28 22:52:48 -08:00
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
2013-08-12 01:32:53 -07:00
|
|
|
if (!bs) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-01-28 22:52:48 -08:00
|
|
|
bs->StopSendingFile(aDeviceAddress, results);
|
2012-10-03 00:09:27 -07:00
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
return request.forget();
|
2012-09-29 03:26:46 -07:00
|
|
|
}
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
already_AddRefed<DOMRequest>
|
2012-10-18 23:16:13 -07:00
|
|
|
BluetoothAdapter::ConfirmReceivingFile(const nsAString& aDeviceAddress,
|
2013-08-12 01:32:53 -07:00
|
|
|
bool aConfirmation, ErrorResult& aRv)
|
2012-10-18 23:16:13 -07:00
|
|
|
{
|
2013-08-12 01:32:53 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win = GetOwner();
|
|
|
|
if (!win) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-10-18 23:16:13 -07:00
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
nsRefPtr<DOMRequest> request = new DOMRequest(win);
|
2013-01-28 22:52:48 -08:00
|
|
|
nsRefPtr<BluetoothVoidReplyRunnable> results =
|
2013-08-12 01:32:53 -07:00
|
|
|
new BluetoothVoidReplyRunnable(request);
|
2012-10-18 23:16:13 -07:00
|
|
|
|
2013-01-28 22:52:48 -08:00
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
2013-08-12 01:32:53 -07:00
|
|
|
if (!bs) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-01-28 22:52:48 -08:00
|
|
|
bs->ConfirmReceivingFile(aDeviceAddress, aConfirmation, results);
|
2012-10-18 23:16:13 -07:00
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
return request.forget();
|
2012-10-18 23:16:13 -07:00
|
|
|
}
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
already_AddRefed<DOMRequest>
|
|
|
|
BluetoothAdapter::ConnectSco(ErrorResult& aRv)
|
2013-05-10 03:58:59 -07:00
|
|
|
{
|
2013-08-12 01:32:53 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win = GetOwner();
|
|
|
|
if (!win) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-05-10 03:58:59 -07:00
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
nsRefPtr<DOMRequest> request = new DOMRequest(win);
|
2013-05-10 03:58:59 -07:00
|
|
|
nsRefPtr<BluetoothVoidReplyRunnable> results =
|
2013-08-12 01:32:53 -07:00
|
|
|
new BluetoothVoidReplyRunnable(request);
|
2013-05-10 03:58:59 -07:00
|
|
|
|
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
2013-08-12 01:32:53 -07:00
|
|
|
if (!bs) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-05-10 03:58:59 -07:00
|
|
|
bs->ConnectSco(results);
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
return request.forget();
|
2013-05-10 03:58:59 -07:00
|
|
|
}
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
already_AddRefed<DOMRequest>
|
|
|
|
BluetoothAdapter::DisconnectSco(ErrorResult& aRv)
|
2013-05-10 03:58:59 -07:00
|
|
|
{
|
2013-08-12 01:32:53 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win = GetOwner();
|
|
|
|
if (!win) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-05-10 03:58:59 -07:00
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
nsRefPtr<DOMRequest> request = new DOMRequest(win);
|
2013-05-10 03:58:59 -07:00
|
|
|
nsRefPtr<BluetoothVoidReplyRunnable> results =
|
2013-08-12 01:32:53 -07:00
|
|
|
new BluetoothVoidReplyRunnable(request);
|
2013-05-10 03:58:59 -07:00
|
|
|
|
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
2013-08-12 01:32:53 -07:00
|
|
|
if (!bs) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-05-10 03:58:59 -07:00
|
|
|
bs->DisconnectSco(results);
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
return request.forget();
|
2013-05-10 03:58:59 -07:00
|
|
|
}
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
already_AddRefed<DOMRequest>
|
|
|
|
BluetoothAdapter::IsScoConnected(ErrorResult& aRv)
|
2013-05-10 03:58:59 -07:00
|
|
|
{
|
2013-08-12 01:32:53 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win = GetOwner();
|
|
|
|
if (!win) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-05-10 03:58:59 -07:00
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
nsRefPtr<DOMRequest> request = new DOMRequest(win);
|
2013-05-10 03:58:59 -07:00
|
|
|
nsRefPtr<BluetoothReplyRunnable> results =
|
2013-08-12 01:32:53 -07:00
|
|
|
new GetScoConnectionStatusTask(request);
|
2013-05-10 03:58:59 -07:00
|
|
|
|
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
2013-08-12 01:32:53 -07:00
|
|
|
if (!bs) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-05-10 03:58:59 -07:00
|
|
|
bs->IsScoConnected(results);
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
return request.forget();
|
2013-05-10 03:58:59 -07:00
|
|
|
}
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
already_AddRefed<DOMRequest>
|
|
|
|
BluetoothAdapter::SendMediaMetaData(const MediaMetaData& aMediaMetaData, ErrorResult& aRv)
|
2013-07-29 02:32:34 -07:00
|
|
|
{
|
2013-08-12 01:32:53 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win = GetOwner();
|
|
|
|
if (!win) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-07-29 02:32:34 -07:00
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
nsRefPtr<DOMRequest> request = new DOMRequest(win);
|
2013-07-29 02:32:34 -07:00
|
|
|
nsRefPtr<BluetoothReplyRunnable> results =
|
2013-08-12 01:32:53 -07:00
|
|
|
new BluetoothVoidReplyRunnable(request);
|
2013-07-29 02:32:34 -07:00
|
|
|
|
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
2013-08-12 01:32:53 -07:00
|
|
|
if (!bs) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
bs->SendMetaData(aMediaMetaData.mTitle,
|
|
|
|
aMediaMetaData.mArtist,
|
|
|
|
aMediaMetaData.mAlbum,
|
|
|
|
aMediaMetaData.mMediaNumber,
|
|
|
|
aMediaMetaData.mTotalMediaCount,
|
|
|
|
aMediaMetaData.mDuration,
|
2013-07-29 02:32:34 -07:00
|
|
|
results);
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
return request.forget();
|
2013-07-29 02:32:34 -07:00
|
|
|
}
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
already_AddRefed<DOMRequest>
|
|
|
|
BluetoothAdapter::SendMediaPlayStatus(const MediaPlayStatus& aMediaPlayStatus, ErrorResult& aRv)
|
2013-07-29 02:32:34 -07:00
|
|
|
{
|
2013-08-12 01:32:53 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win = GetOwner();
|
|
|
|
if (!win) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-07-29 02:32:34 -07:00
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
nsRefPtr<DOMRequest> request = new DOMRequest(win);
|
2013-07-29 02:32:34 -07:00
|
|
|
nsRefPtr<BluetoothReplyRunnable> results =
|
2013-08-12 01:32:53 -07:00
|
|
|
new BluetoothVoidReplyRunnable(request);
|
2013-07-29 02:32:34 -07:00
|
|
|
|
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
2013-08-12 01:32:53 -07:00
|
|
|
if (!bs) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
bs->SendPlayStatus(aMediaPlayStatus.mDuration,
|
|
|
|
aMediaPlayStatus.mPosition,
|
|
|
|
aMediaPlayStatus.mPlayStatus,
|
2013-07-29 02:32:34 -07:00
|
|
|
results);
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
return request.forget();
|
2013-07-29 02:32:34 -07:00
|
|
|
}
|
|
|
|
|
2013-08-12 01:32:53 -07:00
|
|
|
JSObject*
|
|
|
|
BluetoothAdapter::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
|
|
|
|
{
|
|
|
|
return BluetoothAdapterBinding::Wrap(aCx, aScope, this);
|
|
|
|
}
|