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 "BluetoothPropertyEvent.h"
|
|
|
|
#include "BluetoothReplyRunnable.h"
|
|
|
|
#include "BluetoothService.h"
|
|
|
|
#include "BluetoothUtils.h"
|
2012-07-17 20:41:54 -07:00
|
|
|
|
2012-07-31 21:53:04 -07:00
|
|
|
#include "nsIDOMDOMRequest.h"
|
2012-07-17 20:41:54 -07:00
|
|
|
#include "nsDOMClassInfo.h"
|
2012-07-31 21:53:04 -07:00
|
|
|
#include "nsContentUtils.h"
|
2012-10-18 06:20:54 -07:00
|
|
|
#include "nsTArrayHelpers.h"
|
2012-09-13 09:37:14 -07:00
|
|
|
#include "mozilla/dom/bluetooth/BluetoothTypes.h"
|
2012-07-17 20:41:54 -07:00
|
|
|
|
|
|
|
USING_BLUETOOTH_NAMESPACE
|
|
|
|
|
|
|
|
DOMCI_DATA(BluetoothDevice, BluetoothDevice)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(BluetoothDevice)
|
|
|
|
|
2012-07-31 21:53:04 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(BluetoothDevice,
|
|
|
|
nsDOMEventTargetHelper)
|
|
|
|
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
|
|
|
|
|
2012-07-17 20:41:54 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(BluetoothDevice,
|
|
|
|
nsDOMEventTargetHelper)
|
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
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(BluetoothDevice,
|
|
|
|
nsDOMEventTargetHelper)
|
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)
|
|
|
|
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)
|
|
|
|
|
2012-07-31 21:53:04 -07:00
|
|
|
BluetoothDevice::BluetoothDevice(nsPIDOMWindow* aOwner,
|
|
|
|
const nsAString& aAdapterPath,
|
|
|
|
const BluetoothValue& aValue) :
|
|
|
|
BluetoothPropertyContainer(BluetoothObjectType::TYPE_DEVICE),
|
|
|
|
mJsUuids(nullptr),
|
2012-09-05 20:06:20 -07:00
|
|
|
mJsServices(nullptr),
|
2012-07-31 21:53:04 -07:00
|
|
|
mAdapterPath(aAdapterPath),
|
|
|
|
mIsRooted(false)
|
2012-07-17 20:41:54 -07:00
|
|
|
{
|
2012-07-31 21:53:04 -07:00
|
|
|
BindToOwner(aOwner);
|
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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-31 21:53:04 -07:00
|
|
|
BluetoothDevice::~BluetoothDevice()
|
|
|
|
{
|
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
|
|
|
// bs can be null on shutdown, where destruction might happen.
|
|
|
|
if (bs) {
|
2012-09-13 09:37:14 -07:00
|
|
|
bs->UnregisterBluetoothSignalHandler(mPath, this);
|
2012-07-31 21:53:04 -07:00
|
|
|
}
|
|
|
|
Unroot();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothDevice::Root()
|
|
|
|
{
|
|
|
|
if (!mIsRooted) {
|
|
|
|
NS_HOLD_JS_OBJECTS(this, BluetoothDevice);
|
|
|
|
mIsRooted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothDevice::Unroot()
|
|
|
|
{
|
|
|
|
if (mIsRooted) {
|
2012-11-27 17:37:57 -08:00
|
|
|
mJsUuids = nullptr;
|
|
|
|
mJsServices = nullptr;
|
2012-07-31 21:53:04 -07:00
|
|
|
NS_DROP_JS_OBJECTS(this, BluetoothDevice);
|
|
|
|
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);
|
|
|
|
if (sc) {
|
|
|
|
rv =
|
2012-10-18 06:20:54 -07:00
|
|
|
nsTArrayToJSArray(sc->GetNativeContext(), mUuids, &mJsUuids);
|
2012-07-31 21:53:04 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Cannot set JS UUIDs object!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Root();
|
|
|
|
} else {
|
|
|
|
NS_WARNING("Could not get context!");
|
|
|
|
}
|
2012-09-05 20:06:20 -07:00
|
|
|
} else if (name.EqualsLiteral("Services")) {
|
|
|
|
mServices = value.get_ArrayOfnsString();
|
|
|
|
nsresult rv;
|
|
|
|
nsIScriptContext* sc = GetContextForEventHandlers(&rv);
|
|
|
|
if (sc) {
|
|
|
|
rv =
|
2012-10-18 06:20:54 -07:00
|
|
|
nsTArrayToJSArray(sc->GetNativeContext(), mServices, &mJsServices);
|
2012-09-05 20:06:20 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Cannot set JS Services object!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Root();
|
|
|
|
} else {
|
|
|
|
NS_WARNING("Could not get context!");
|
|
|
|
}
|
2012-07-17 20:41:54 -07:00
|
|
|
#ifdef DEBUG
|
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));
|
|
|
|
NS_WARNING(warningMsg.get());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<BluetoothDevice>
|
2012-07-31 21:53:04 -07:00
|
|
|
BluetoothDevice::Create(nsPIDOMWindow* aOwner,
|
|
|
|
const nsAString& aAdapterPath,
|
|
|
|
const BluetoothValue& aValue)
|
2012-07-17 20:41:54 -07:00
|
|
|
{
|
2012-07-31 21:53:04 -07:00
|
|
|
// Make sure we at least have a service
|
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
|
|
|
if (!bs) {
|
|
|
|
NS_WARNING("BluetoothService not available!");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<BluetoothDevice> device = new BluetoothDevice(aOwner, aAdapterPath,
|
|
|
|
aValue);
|
2012-09-13 09:37:14 -07:00
|
|
|
|
|
|
|
bs->RegisterBluetoothSignalHandler(device->mPath, device);
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (aData.name().EqualsLiteral("PropertyChanged")) {
|
2012-09-18 20:19:45 -07:00
|
|
|
NS_ASSERTION(aData.value().type() == BluetoothValue::TArrayOfBluetoothNamedValue,
|
|
|
|
"PropertyChanged: Invalid value type");
|
2012-12-18 17:16:06 -08:00
|
|
|
InfallibleTArray<BluetoothNamedValue> arr(aData.value().get_ArrayOfBluetoothNamedValue());
|
2012-09-18 20:19:45 -07:00
|
|
|
|
|
|
|
NS_ASSERTION(arr.Length() == 1, "Got more than one property in a change message!");
|
|
|
|
BluetoothNamedValue v = arr[0];
|
2012-07-31 21:53:04 -07:00
|
|
|
nsString name = v.name();
|
2012-08-22 23:14:20 -07:00
|
|
|
|
2012-09-18 20:22:38 -07:00
|
|
|
SetPropertyByValue(v);
|
2012-08-22 23:14:20 -07:00
|
|
|
if (name.EqualsLiteral("Connected")) {
|
2012-09-27 13:11:31 -07:00
|
|
|
DispatchTrustedEvent(mConnected ? NS_LITERAL_STRING("connected")
|
|
|
|
: NS_LITERAL_STRING("disconnected"));
|
2012-08-22 23:14:20 -07:00
|
|
|
} else {
|
|
|
|
nsRefPtr<BluetoothPropertyEvent> e = BluetoothPropertyEvent::Create(name);
|
|
|
|
e->Dispatch(ToIDOMEventTarget(), NS_LITERAL_STRING("propertychanged"));
|
|
|
|
}
|
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()));
|
|
|
|
NS_WARNING(warningMsg.get());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-17 20:41:54 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BluetoothDevice::GetAddress(nsAString& aAddress)
|
|
|
|
{
|
|
|
|
aAddress = mAddress;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BluetoothDevice::GetName(nsAString& aName)
|
|
|
|
{
|
|
|
|
aName = mName;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-29 20:15:35 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BluetoothDevice::GetIcon(nsAString& aIcon)
|
|
|
|
{
|
|
|
|
aIcon = mIcon;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-07-17 20:41:54 -07:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
BluetoothDevice::GetDeviceClass(uint32_t* aClass)
|
2012-07-17 20:41:54 -07:00
|
|
|
{
|
|
|
|
*aClass = mClass;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BluetoothDevice::GetPaired(bool* aPaired)
|
|
|
|
{
|
|
|
|
*aPaired = mPaired;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BluetoothDevice::GetConnected(bool* aConnected)
|
|
|
|
{
|
|
|
|
*aConnected = mConnected;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-07-31 21:53:04 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BluetoothDevice::GetUuids(JSContext* aCx, jsval* aUuids)
|
|
|
|
{
|
|
|
|
if (mJsUuids) {
|
|
|
|
aUuids->setObject(*mJsUuids);
|
|
|
|
} else {
|
|
|
|
NS_WARNING("UUIDs not yet set!\n");
|
|
|
|
return NS_ERROR_FAILURE;
|
2012-09-05 20:06:20 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BluetoothDevice::GetServices(JSContext* aCx, jsval* aServices)
|
|
|
|
{
|
|
|
|
if (mJsServices) {
|
|
|
|
aServices->setObject(*mJsServices);
|
|
|
|
} else {
|
|
|
|
NS_WARNING("Services not yet set!\n");
|
|
|
|
}
|
2012-07-31 21:53:04 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-07-17 20:41:54 -07:00
|
|
|
NS_IMPL_EVENT_HANDLER(BluetoothDevice, propertychanged)
|
2012-08-22 23:14:20 -07:00
|
|
|
NS_IMPL_EVENT_HANDLER(BluetoothDevice, connected)
|
|
|
|
NS_IMPL_EVENT_HANDLER(BluetoothDevice, disconnected)
|