2012-07-18 03:41:54 +00:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
2012-08-21 03:21:24 +00:00
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
2012-07-18 03:41:54 +00: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 "BluetoothDevice.h"
|
2012-08-01 04:53:04 +00:00
|
|
|
#include "BluetoothReplyRunnable.h"
|
|
|
|
#include "BluetoothService.h"
|
|
|
|
#include "BluetoothUtils.h"
|
2012-07-18 03:41:54 +00:00
|
|
|
|
|
|
|
#include "nsDOMClassInfo.h"
|
2012-08-01 04:53:04 +00:00
|
|
|
#include "nsContentUtils.h"
|
2013-02-06 12:07:27 +00:00
|
|
|
#include "nsTArrayHelpers.h"
|
|
|
|
|
2012-09-13 16:37:14 +00:00
|
|
|
#include "mozilla/dom/bluetooth/BluetoothTypes.h"
|
2013-08-12 08:34:28 +00:00
|
|
|
#include "mozilla/dom/BluetoothDeviceBinding.h"
|
2012-07-18 03:41:54 +00:00
|
|
|
|
|
|
|
USING_BLUETOOTH_NAMESPACE
|
|
|
|
|
|
|
|
DOMCI_DATA(BluetoothDevice, BluetoothDevice)
|
|
|
|
|
2013-08-02 01:29:05 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(BluetoothDevice)
|
|
|
|
|
2012-08-01 04:53:04 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(BluetoothDevice,
|
|
|
|
nsDOMEventTargetHelper)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mJsUuids)
|
2012-09-06 03:06:20 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mJsServices)
|
2012-08-01 04:53:04 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2013-01-29 06:52:30 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(BluetoothDevice,
|
2012-07-18 03:41:54 +00:00
|
|
|
nsDOMEventTargetHelper)
|
2012-08-01 04:53:04 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
2012-07-18 03:41:54 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2013-01-29 06:52:30 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(BluetoothDevice,
|
2012-07-18 03:41:54 +00:00
|
|
|
nsDOMEventTargetHelper)
|
2012-08-01 04:53:04 +00:00
|
|
|
tmp->Unroot();
|
2012-07-18 03:41:54 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(BluetoothDevice)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMBluetoothDevice)
|
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(BluetoothDevice)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(BluetoothDevice, nsDOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(BluetoothDevice, nsDOMEventTargetHelper)
|
|
|
|
|
2013-01-29 06:52:39 +00:00
|
|
|
BluetoothDevice::BluetoothDevice(nsPIDOMWindow* aWindow,
|
2012-08-01 04:53:04 +00:00
|
|
|
const nsAString& aAdapterPath,
|
2013-08-12 08:34:28 +00:00
|
|
|
const BluetoothValue& aValue)
|
|
|
|
: nsDOMEventTargetHelper(aWindow)
|
|
|
|
, BluetoothPropertyContainer(BluetoothObjectType::TYPE_DEVICE)
|
|
|
|
, mJsUuids(nullptr)
|
|
|
|
, mJsServices(nullptr)
|
|
|
|
, mAdapterPath(aAdapterPath)
|
|
|
|
, mIsRooted(false)
|
2012-07-18 03:41:54 +00:00
|
|
|
{
|
2013-01-29 06:52:39 +00:00
|
|
|
MOZ_ASSERT(aWindow);
|
2013-08-12 08:34:28 +00:00
|
|
|
MOZ_ASSERT(IsDOMBinding());
|
2013-01-29 06:52:39 +00:00
|
|
|
|
|
|
|
BindToOwner(aWindow);
|
2012-07-18 03:41:54 +00:00
|
|
|
const InfallibleTArray<BluetoothNamedValue>& values =
|
2012-08-01 04:53:04 +00:00
|
|
|
aValue.get_ArrayOfBluetoothNamedValue();
|
2012-07-18 03:41:54 +00:00
|
|
|
for (uint32_t i = 0; i < values.Length(); ++i) {
|
|
|
|
SetPropertyByValue(values[i]);
|
|
|
|
}
|
2013-02-08 10:03:09 +00:00
|
|
|
|
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
|
|
|
NS_ENSURE_TRUE_VOID(bs);
|
2013-03-15 07:15:47 +00:00
|
|
|
bs->RegisterBluetoothSignalHandler(mAddress, this);
|
2012-07-18 03:41:54 +00:00
|
|
|
}
|
|
|
|
|
2012-08-01 04:53:04 +00:00
|
|
|
BluetoothDevice::~BluetoothDevice()
|
|
|
|
{
|
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
|
|
|
// bs can be null on shutdown, where destruction might happen.
|
2013-02-08 10:03:09 +00:00
|
|
|
NS_ENSURE_TRUE_VOID(bs);
|
2013-03-15 07:15:47 +00:00
|
|
|
bs->UnregisterBluetoothSignalHandler(mAddress, this);
|
2012-08-01 04:53:04 +00:00
|
|
|
Unroot();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothDevice::Root()
|
|
|
|
{
|
|
|
|
if (!mIsRooted) {
|
|
|
|
NS_HOLD_JS_OBJECTS(this, BluetoothDevice);
|
|
|
|
mIsRooted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothDevice::Unroot()
|
|
|
|
{
|
|
|
|
if (mIsRooted) {
|
2012-11-28 01:37:57 +00:00
|
|
|
mJsUuids = nullptr;
|
|
|
|
mJsServices = nullptr;
|
2012-08-01 04:53:04 +00:00
|
|
|
NS_DROP_JS_OBJECTS(this, BluetoothDevice);
|
|
|
|
mIsRooted = false;
|
|
|
|
}
|
|
|
|
}
|
2012-09-06 03:06:20 +00:00
|
|
|
|
2012-07-18 03:41:54 +00:00
|
|
|
void
|
|
|
|
BluetoothDevice::SetPropertyByValue(const BluetoothNamedValue& aValue)
|
|
|
|
{
|
|
|
|
const nsString& name = aValue.name();
|
|
|
|
const BluetoothValue& value = aValue.value();
|
|
|
|
if (name.EqualsLiteral("Name")) {
|
|
|
|
mName = value.get_nsString();
|
2012-09-06 03:06:20 +00:00
|
|
|
} else if (name.EqualsLiteral("Path")) {
|
2012-09-25 18:48:51 +00:00
|
|
|
MOZ_ASSERT(value.get_nsString().Length() > 0);
|
2012-09-06 03:06:20 +00:00
|
|
|
mPath = value.get_nsString();
|
|
|
|
} else if (name.EqualsLiteral("Address")) {
|
|
|
|
mAddress = value.get_nsString();
|
2012-07-18 03:41:54 +00:00
|
|
|
} else if (name.EqualsLiteral("Class")) {
|
|
|
|
mClass = value.get_uint32_t();
|
2012-08-30 03:15:35 +00:00
|
|
|
} else if (name.EqualsLiteral("Icon")) {
|
|
|
|
mIcon = value.get_nsString();
|
2012-07-18 03:41:54 +00:00
|
|
|
} else if (name.EqualsLiteral("Connected")) {
|
|
|
|
mConnected = value.get_bool();
|
|
|
|
} else if (name.EqualsLiteral("Paired")) {
|
|
|
|
mPaired = value.get_bool();
|
|
|
|
} else if (name.EqualsLiteral("UUIDs")) {
|
|
|
|
mUuids = value.get_ArrayOfnsString();
|
2012-08-01 04:53:04 +00:00
|
|
|
nsresult rv;
|
|
|
|
nsIScriptContext* sc = GetContextForEventHandlers(&rv);
|
2013-01-29 06:52:39 +00:00
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
|
|
|
|
2013-05-24 08:50:00 +00:00
|
|
|
AutoPushJSContext cx(sc->GetNativeContext());
|
|
|
|
|
2013-06-18 10:00:38 +00:00
|
|
|
JS::Rooted<JSObject*> uuids(cx);
|
|
|
|
if (NS_FAILED(nsTArrayToJSArray(cx, mUuids, uuids.address()))) {
|
2013-01-29 06:52:39 +00:00
|
|
|
NS_WARNING("Cannot set JS UUIDs object!");
|
|
|
|
return;
|
2012-08-01 04:53:04 +00:00
|
|
|
}
|
2013-06-18 10:00:38 +00:00
|
|
|
mJsUuids = uuids;
|
2013-01-29 06:52:39 +00:00
|
|
|
Root();
|
2012-09-06 03:06:20 +00:00
|
|
|
} else if (name.EqualsLiteral("Services")) {
|
|
|
|
mServices = value.get_ArrayOfnsString();
|
|
|
|
nsresult rv;
|
|
|
|
nsIScriptContext* sc = GetContextForEventHandlers(&rv);
|
2013-01-29 06:52:39 +00:00
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
|
|
|
|
2013-05-24 08:50:00 +00:00
|
|
|
AutoPushJSContext cx(sc->GetNativeContext());
|
|
|
|
|
2013-06-18 10:00:38 +00:00
|
|
|
JS::Rooted<JSObject*> services(cx);
|
|
|
|
if (NS_FAILED(nsTArrayToJSArray(cx, mServices, services.address()))) {
|
2013-02-06 12:07:27 +00:00
|
|
|
NS_WARNING("Cannot set JS Services object!");
|
2013-01-29 06:52:39 +00:00
|
|
|
return;
|
2012-09-06 03:06:20 +00:00
|
|
|
}
|
2013-06-18 10:00:38 +00:00
|
|
|
mJsServices = services;
|
2013-01-29 06:52:39 +00:00
|
|
|
Root();
|
2012-08-01 04:53:04 +00:00
|
|
|
} else {
|
2012-07-18 03:41:54 +00:00
|
|
|
nsCString warningMsg;
|
|
|
|
warningMsg.AssignLiteral("Not handling device property: ");
|
|
|
|
warningMsg.Append(NS_ConvertUTF16toUTF8(name));
|
|
|
|
NS_WARNING(warningMsg.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<BluetoothDevice>
|
2013-01-29 06:52:39 +00:00
|
|
|
BluetoothDevice::Create(nsPIDOMWindow* aWindow,
|
2012-08-01 04:53:04 +00:00
|
|
|
const nsAString& aAdapterPath,
|
|
|
|
const BluetoothValue& aValue)
|
2012-07-18 03:41:54 +00:00
|
|
|
{
|
2013-01-29 06:52:39 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aWindow);
|
2012-08-01 04:53:04 +00:00
|
|
|
|
2013-01-29 06:52:39 +00:00
|
|
|
nsRefPtr<BluetoothDevice> device =
|
|
|
|
new BluetoothDevice(aWindow, aAdapterPath, aValue);
|
2012-07-18 03:41:54 +00:00
|
|
|
return device.forget();
|
|
|
|
}
|
|
|
|
|
2012-08-01 04:53:04 +00:00
|
|
|
void
|
|
|
|
BluetoothDevice::Notify(const BluetoothSignal& aData)
|
|
|
|
{
|
2013-01-29 10:37:51 +00:00
|
|
|
BT_LOG("[D] %s: %s", __FUNCTION__, NS_ConvertUTF16toUTF8(aData.name()).get());
|
|
|
|
|
2013-01-29 06:52:30 +00:00
|
|
|
BluetoothValue v = aData.value();
|
2012-08-01 04:53:04 +00:00
|
|
|
if (aData.name().EqualsLiteral("PropertyChanged")) {
|
2013-01-29 06:52:30 +00:00
|
|
|
NS_ASSERTION(v.type() == BluetoothValue::TArrayOfBluetoothNamedValue,
|
2012-09-19 03:19:45 +00:00
|
|
|
"PropertyChanged: Invalid value type");
|
2013-02-06 12:07:27 +00:00
|
|
|
const InfallibleTArray<BluetoothNamedValue>& arr =
|
|
|
|
v.get_ArrayOfBluetoothNamedValue();
|
2012-09-19 03:19:45 +00:00
|
|
|
|
2013-01-29 06:52:30 +00:00
|
|
|
NS_ASSERTION(arr.Length() == 1,
|
|
|
|
"Got more than one property in a change message!");
|
|
|
|
SetPropertyByValue(arr[0]);
|
2012-08-01 04:53:04 +00:00
|
|
|
} else {
|
|
|
|
#ifdef DEBUG
|
|
|
|
nsCString warningMsg;
|
|
|
|
warningMsg.AssignLiteral("Not handling device signal: ");
|
|
|
|
warningMsg.Append(NS_ConvertUTF16toUTF8(aData.name()));
|
|
|
|
NS_WARNING(warningMsg.get());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-12 08:34:28 +00:00
|
|
|
JS::Value
|
|
|
|
BluetoothDevice::GetUuids(JSContext* aCx, ErrorResult& aRv)
|
2012-07-18 03:41:54 +00:00
|
|
|
{
|
2013-08-12 08:34:28 +00:00
|
|
|
if (!mJsUuids) {
|
2012-08-01 04:53:04 +00:00
|
|
|
NS_WARNING("UUIDs not yet set!\n");
|
2013-08-12 08:34:28 +00:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return JS::NullValue();
|
2012-09-06 03:06:20 +00:00
|
|
|
}
|
2013-08-12 08:34:28 +00:00
|
|
|
|
|
|
|
return JS::ObjectValue(*xpc_UnmarkGrayObject(mJsUuids));
|
2012-09-06 03:06:20 +00:00
|
|
|
}
|
|
|
|
|
2013-08-12 08:34:28 +00:00
|
|
|
JS::Value
|
|
|
|
BluetoothDevice::GetServices(JSContext* aCx, ErrorResult& aRv)
|
2012-09-06 03:06:20 +00:00
|
|
|
{
|
2013-08-12 08:34:28 +00:00
|
|
|
if (!mJsServices) {
|
2012-09-06 03:06:20 +00:00
|
|
|
NS_WARNING("Services not yet set!\n");
|
2013-08-12 08:34:28 +00:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return JS::Value(JSVAL_NULL);
|
2012-09-06 03:06:20 +00:00
|
|
|
}
|
2013-08-12 08:34:28 +00:00
|
|
|
|
|
|
|
return JS::ObjectValue(*xpc_UnmarkGrayObject(mJsServices));
|
2012-08-01 04:53:04 +00:00
|
|
|
}
|
|
|
|
|
2013-08-12 08:34:28 +00:00
|
|
|
JSObject*
|
|
|
|
BluetoothDevice::WrapObject(JSContext* aContext, JS::Handle<JSObject*> aScope)
|
|
|
|
{
|
|
|
|
return BluetoothDeviceBinding::Wrap(aContext, aScope, this);
|
|
|
|
}
|