2012-07-17 20:41:54 -07:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
2012-08-20 20:21:24 -07:00
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
2012-07-17 20:41:54 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "base/basictypes.h"
|
|
|
|
#include "BluetoothDevice.h"
|
2012-07-31 21:53:04 -07:00
|
|
|
#include "BluetoothReplyRunnable.h"
|
|
|
|
#include "BluetoothService.h"
|
|
|
|
#include "BluetoothUtils.h"
|
2012-07-17 20:41:54 -07:00
|
|
|
|
|
|
|
#include "nsDOMClassInfo.h"
|
2013-02-06 04:07:27 -08:00
|
|
|
#include "nsTArrayHelpers.h"
|
|
|
|
|
2012-09-13 09:37:14 -07:00
|
|
|
#include "mozilla/dom/bluetooth/BluetoothTypes.h"
|
2013-08-12 01:34:28 -07:00
|
|
|
#include "mozilla/dom/BluetoothDeviceBinding.h"
|
2012-07-17 20:41:54 -07:00
|
|
|
|
|
|
|
USING_BLUETOOTH_NAMESPACE
|
|
|
|
|
|
|
|
DOMCI_DATA(BluetoothDevice, BluetoothDevice)
|
|
|
|
|
2013-08-01 18:29:05 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(BluetoothDevice)
|
|
|
|
|
2012-07-31 21:53:04 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(BluetoothDevice,
|
2014-03-31 23:13:50 -07:00
|
|
|
DOMEventTargetHelper)
|
2012-07-31 21:53:04 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mJsUuids)
|
2012-09-05 20:06:20 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mJsServices)
|
2012-07-31 21:53:04 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2013-01-28 22:52:30 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(BluetoothDevice,
|
2014-03-31 23:13:50 -07:00
|
|
|
DOMEventTargetHelper)
|
2012-07-31 21:53:04 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
2012-07-17 20:41:54 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2013-01-28 22:52:30 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(BluetoothDevice,
|
2014-03-31 23:13:50 -07:00
|
|
|
DOMEventTargetHelper)
|
2012-07-31 21:53:04 -07:00
|
|
|
tmp->Unroot();
|
2012-07-17 20:41:54 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(BluetoothDevice)
|
2014-03-31 23:13:50 -07:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
2012-07-17 20:41:54 -07:00
|
|
|
|
2014-03-31 23:13:50 -07:00
|
|
|
NS_IMPL_ADDREF_INHERITED(BluetoothDevice, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(BluetoothDevice, DOMEventTargetHelper)
|
2012-07-17 20:41:54 -07:00
|
|
|
|
2013-01-28 22:52:39 -08:00
|
|
|
BluetoothDevice::BluetoothDevice(nsPIDOMWindow* aWindow,
|
2012-07-31 21:53:04 -07:00
|
|
|
const nsAString& aAdapterPath,
|
2013-08-12 01:34:28 -07:00
|
|
|
const BluetoothValue& aValue)
|
2014-03-31 23:13:50 -07:00
|
|
|
: DOMEventTargetHelper(aWindow)
|
2013-08-12 01:34:28 -07:00
|
|
|
, BluetoothPropertyContainer(BluetoothObjectType::TYPE_DEVICE)
|
|
|
|
, mJsUuids(nullptr)
|
|
|
|
, mJsServices(nullptr)
|
|
|
|
, mAdapterPath(aAdapterPath)
|
|
|
|
, mIsRooted(false)
|
2012-07-17 20:41:54 -07:00
|
|
|
{
|
2013-01-28 22:52:39 -08:00
|
|
|
MOZ_ASSERT(aWindow);
|
2013-08-12 01:34:28 -07:00
|
|
|
MOZ_ASSERT(IsDOMBinding());
|
2013-01-28 22:52:39 -08:00
|
|
|
|
2012-07-17 20:41:54 -07:00
|
|
|
const InfallibleTArray<BluetoothNamedValue>& values =
|
2012-07-31 21:53:04 -07:00
|
|
|
aValue.get_ArrayOfBluetoothNamedValue();
|
2012-07-17 20:41:54 -07:00
|
|
|
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(mAddress, this);
|
2012-07-17 20:41:54 -07:00
|
|
|
}
|
|
|
|
|
2012-07-31 21:53:04 -07:00
|
|
|
BluetoothDevice::~BluetoothDevice()
|
|
|
|
{
|
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
|
|
|
// bs can be null on shutdown, where destruction 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(mAddress, this);
|
2012-07-31 21:53:04 -07:00
|
|
|
Unroot();
|
|
|
|
}
|
|
|
|
|
2014-04-01 05:58:56 -07:00
|
|
|
void
|
|
|
|
BluetoothDevice::DisconnectFromOwner()
|
|
|
|
{
|
2014-04-01 15:10:06 -07:00
|
|
|
DOMEventTargetHelper::DisconnectFromOwner();
|
2014-04-01 05:58:56 -07:00
|
|
|
|
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
|
|
|
NS_ENSURE_TRUE_VOID(bs);
|
|
|
|
bs->UnregisterBluetoothSignalHandler(mAddress, this);
|
|
|
|
}
|
|
|
|
|
2012-07-31 21:53:04 -07:00
|
|
|
void
|
|
|
|
BluetoothDevice::Root()
|
|
|
|
{
|
|
|
|
if (!mIsRooted) {
|
2013-08-16 13:10:17 -07:00
|
|
|
mozilla::HoldJSObjects(this);
|
2012-07-31 21:53:04 -07:00
|
|
|
mIsRooted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothDevice::Unroot()
|
|
|
|
{
|
|
|
|
if (mIsRooted) {
|
2012-11-27 17:37:57 -08:00
|
|
|
mJsUuids = nullptr;
|
|
|
|
mJsServices = nullptr;
|
2013-08-16 13:10:17 -07:00
|
|
|
mozilla::DropJSObjects(this);
|
2012-07-31 21:53:04 -07:00
|
|
|
mIsRooted = false;
|
|
|
|
}
|
|
|
|
}
|
2012-09-05 20:06:20 -07:00
|
|
|
|
2012-07-17 20:41:54 -07: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-05 20:06:20 -07:00
|
|
|
} else if (name.EqualsLiteral("Path")) {
|
2012-09-25 11:48:51 -07:00
|
|
|
MOZ_ASSERT(value.get_nsString().Length() > 0);
|
2012-09-05 20:06:20 -07:00
|
|
|
mPath = value.get_nsString();
|
|
|
|
} else if (name.EqualsLiteral("Address")) {
|
|
|
|
mAddress = value.get_nsString();
|
2012-07-17 20:41:54 -07:00
|
|
|
} else if (name.EqualsLiteral("Class")) {
|
|
|
|
mClass = value.get_uint32_t();
|
2012-08-29 20:15:35 -07:00
|
|
|
} else if (name.EqualsLiteral("Icon")) {
|
|
|
|
mIcon = value.get_nsString();
|
2012-07-17 20:41:54 -07: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-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-11-05 22:26:46 -08:00
|
|
|
NS_ENSURE_TRUE_VOID(sc);
|
2013-01-28 22:52:39 -08:00
|
|
|
|
2013-05-24 01:50:00 -07:00
|
|
|
AutoPushJSContext cx(sc->GetNativeContext());
|
|
|
|
|
2013-06-18 03:00:38 -07:00
|
|
|
JS::Rooted<JSObject*> uuids(cx);
|
2014-06-02 09:40:13 -07:00
|
|
|
if (NS_FAILED(nsTArrayToJSArray(cx, mUuids, &uuids))) {
|
2013-09-13 15:51:00 -07:00
|
|
|
BT_WARNING("Cannot set JS UUIDs object!");
|
2013-01-28 22:52:39 -08:00
|
|
|
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-09-05 20:06:20 -07:00
|
|
|
} else if (name.EqualsLiteral("Services")) {
|
|
|
|
mServices = value.get_ArrayOfnsString();
|
|
|
|
nsresult rv;
|
|
|
|
nsIScriptContext* sc = GetContextForEventHandlers(&rv);
|
2013-01-28 22:52:39 -08:00
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
2013-11-05 22:26:46 -08:00
|
|
|
NS_ENSURE_TRUE_VOID(sc);
|
2013-01-28 22:52:39 -08:00
|
|
|
|
2013-05-24 01:50:00 -07:00
|
|
|
AutoPushJSContext cx(sc->GetNativeContext());
|
|
|
|
|
2013-06-18 03:00:38 -07:00
|
|
|
JS::Rooted<JSObject*> services(cx);
|
2014-06-02 09:40:13 -07:00
|
|
|
if (NS_FAILED(nsTArrayToJSArray(cx, mServices, &services))) {
|
2013-09-13 15:51:00 -07:00
|
|
|
BT_WARNING("Cannot set JS Services object!");
|
2013-01-28 22:52:39 -08:00
|
|
|
return;
|
2012-09-05 20:06:20 -07:00
|
|
|
}
|
2013-06-18 03:00:38 -07:00
|
|
|
mJsServices = services;
|
2013-01-28 22:52:39 -08:00
|
|
|
Root();
|
2012-07-31 21:53:04 -07:00
|
|
|
} else {
|
2012-07-17 20:41:54 -07:00
|
|
|
nsCString warningMsg;
|
|
|
|
warningMsg.AssignLiteral("Not handling device property: ");
|
|
|
|
warningMsg.Append(NS_ConvertUTF16toUTF8(name));
|
2013-09-13 15:51:00 -07:00
|
|
|
BT_WARNING(warningMsg.get());
|
2012-07-17 20:41:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<BluetoothDevice>
|
2013-01-28 22:52:39 -08:00
|
|
|
BluetoothDevice::Create(nsPIDOMWindow* aWindow,
|
2012-07-31 21:53:04 -07:00
|
|
|
const nsAString& aAdapterPath,
|
|
|
|
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-31 21:53:04 -07:00
|
|
|
|
2013-01-28 22:52:39 -08:00
|
|
|
nsRefPtr<BluetoothDevice> device =
|
|
|
|
new BluetoothDevice(aWindow, aAdapterPath, aValue);
|
2012-07-17 20:41:54 -07:00
|
|
|
return device.forget();
|
|
|
|
}
|
|
|
|
|
2012-07-31 21:53:04 -07:00
|
|
|
void
|
|
|
|
BluetoothDevice::Notify(const BluetoothSignal& aData)
|
|
|
|
{
|
2013-09-13 15:51:00 -07:00
|
|
|
BT_LOGD("[D] %s: %s", __FUNCTION__, NS_ConvertUTF16toUTF8(aData.name()).get());
|
2013-01-29 02:37:51 -08:00
|
|
|
|
2013-01-28 22:52:30 -08:00
|
|
|
BluetoothValue v = aData.value();
|
2012-07-31 21:53:04 -07:00
|
|
|
if (aData.name().EqualsLiteral("PropertyChanged")) {
|
2013-12-18 02:21:37 -08:00
|
|
|
MOZ_ASSERT(v.type() == BluetoothValue::TArrayOfBluetoothNamedValue);
|
|
|
|
|
2013-02-06 04:07:27 -08:00
|
|
|
const InfallibleTArray<BluetoothNamedValue>& arr =
|
|
|
|
v.get_ArrayOfBluetoothNamedValue();
|
2012-09-18 20:19:45 -07:00
|
|
|
|
2013-12-18 02:21:37 -08:00
|
|
|
for (uint32_t i = 0, propCount = arr.Length(); i < propCount; ++i) {
|
|
|
|
SetPropertyByValue(arr[i]);
|
|
|
|
}
|
2012-07-31 21:53:04 -07:00
|
|
|
} else {
|
|
|
|
#ifdef DEBUG
|
|
|
|
nsCString warningMsg;
|
|
|
|
warningMsg.AssignLiteral("Not handling device signal: ");
|
|
|
|
warningMsg.Append(NS_ConvertUTF16toUTF8(aData.name()));
|
2013-09-13 15:51:00 -07:00
|
|
|
BT_WARNING(warningMsg.get());
|
2012-07-31 21:53:04 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-11 13:26:52 -07:00
|
|
|
void
|
|
|
|
BluetoothDevice::GetUuids(JSContext* aContext,
|
|
|
|
JS::MutableHandle<JS::Value> aUuids,
|
|
|
|
ErrorResult& aRv)
|
2012-07-17 20:41:54 -07:00
|
|
|
{
|
2013-08-12 01:34:28 -07:00
|
|
|
if (!mJsUuids) {
|
2013-09-13 15:51:00 -07:00
|
|
|
BT_WARNING("UUIDs not yet set!");
|
2013-08-12 01:34:28 -07:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2014-06-11 13:26:52 -07:00
|
|
|
return;
|
2013-08-12 09:07:32 -07:00
|
|
|
}
|
2013-08-12 01:34:28 -07:00
|
|
|
|
2013-09-08 20:28:48 -07:00
|
|
|
JS::ExposeObjectToActiveJS(mJsUuids);
|
2014-06-11 13:26:52 -07:00
|
|
|
aUuids.setObject(*mJsUuids);
|
2012-07-31 21:53:04 -07:00
|
|
|
}
|
|
|
|
|
2014-06-11 13:26:52 -07:00
|
|
|
void
|
|
|
|
BluetoothDevice::GetServices(JSContext* aCx,
|
|
|
|
JS::MutableHandle<JS::Value> aServices,
|
|
|
|
ErrorResult& aRv)
|
2013-08-12 01:34:28 -07:00
|
|
|
{
|
2013-08-12 01:34:28 -07:00
|
|
|
if (!mJsServices) {
|
2013-09-13 15:51:00 -07:00
|
|
|
BT_WARNING("Services not yet set!");
|
2013-08-12 01:34:28 -07:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2014-06-11 13:26:52 -07:00
|
|
|
return;
|
2013-08-12 09:07:32 -07:00
|
|
|
}
|
2013-08-12 01:34:28 -07:00
|
|
|
|
2013-09-08 20:28:48 -07:00
|
|
|
JS::ExposeObjectToActiveJS(mJsServices);
|
2014-06-11 13:26:52 -07:00
|
|
|
aServices.setObject(*mJsServices);
|
2013-08-12 01:34:28 -07:00
|
|
|
}
|
2013-08-12 09:07:32 -07:00
|
|
|
|
2013-08-12 01:34:28 -07:00
|
|
|
JSObject*
|
2014-04-08 15:27:18 -07:00
|
|
|
BluetoothDevice::WrapObject(JSContext* aContext)
|
2013-08-12 01:34:28 -07:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-08 15:27:17 -07:00
|
|
|
return BluetoothDeviceBinding::Wrap(aContext, this);
|
2013-08-12 01:34:28 -07:00
|
|
|
}
|