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-03-27 16:17:35 -07:00
|
|
|
#include "BluetoothAdapter.h"
|
2012-07-17 20:41:54 -07:00
|
|
|
#include "BluetoothDevice.h"
|
|
|
|
#include "BluetoothDeviceEvent.h"
|
2012-07-31 21:53:04 -07:00
|
|
|
#include "BluetoothPropertyEvent.h"
|
2012-07-17 20:41:54 -07:00
|
|
|
#include "BluetoothService.h"
|
|
|
|
#include "BluetoothTypes.h"
|
|
|
|
#include "BluetoothReplyRunnable.h"
|
2012-07-31 21:53:04 -07:00
|
|
|
#include "BluetoothUtils.h"
|
2012-03-27 16:17:35 -07:00
|
|
|
|
2012-02-20 07:15:19 -08:00
|
|
|
#include "nsDOMClassInfo.h"
|
2012-02-23 07:55:48 -08:00
|
|
|
#include "nsDOMEvent.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsXPCOMCIDInternal.h"
|
2012-07-17 20:41:54 -07:00
|
|
|
#include "nsIDOMDOMRequest.h"
|
2012-07-31 21:53:04 -07:00
|
|
|
#include "nsContentUtils.h"
|
2012-07-17 20:41:54 -07:00
|
|
|
|
2012-02-23 07:55:48 -08:00
|
|
|
#include "mozilla/LazyIdleThread.h"
|
2012-03-27 16:17:35 -07:00
|
|
|
#include "mozilla/Util.h"
|
|
|
|
|
2012-07-17 20:41:54 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2012-03-04 19:54:01 -08:00
|
|
|
USING_BLUETOOTH_NAMESPACE
|
2012-02-23 07:55:48 -08:00
|
|
|
|
2012-03-04 19:54:01 -08:00
|
|
|
DOMCI_DATA(BluetoothAdapter, BluetoothAdapter)
|
2012-02-23 07:55:48 -08: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
|
|
|
|
|
2012-03-04 19:54:01 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(BluetoothAdapter,
|
|
|
|
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_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(devicefound)
|
|
|
|
NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(devicedisappeared)
|
|
|
|
NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(propertychanged)
|
2012-03-04 19:54:01 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
2012-02-20 07:15:19 -08:00
|
|
|
|
2012-03-04 19:54:01 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(BluetoothAdapter,
|
|
|
|
nsDOMEventTargetHelper)
|
2012-07-31 21:53:04 -07:00
|
|
|
tmp->Unroot();
|
2012-07-17 20:41:54 -07:00
|
|
|
NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(devicefound)
|
|
|
|
NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(devicedisappeared)
|
|
|
|
NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(propertychanged)
|
2012-03-04 19:54:01 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
2012-02-23 07:55:48 -08:00
|
|
|
|
2012-03-04 19:54:01 -08:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(BluetoothAdapter)
|
2012-02-20 07:15:19 -08:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMBluetoothAdapter)
|
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(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)
|
|
|
|
|
2012-08-16 19:53:45 -07:00
|
|
|
class GetPairedDevicesTask : public BluetoothReplyRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
GetPairedDevicesTask(BluetoothAdapter* aAdapterPtr,
|
|
|
|
nsIDOMDOMRequest* aReq) :
|
|
|
|
mAdapterPtr(aAdapterPtr),
|
|
|
|
BluetoothReplyRunnable(aReq)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aReq && aAdapterPtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool ParseSuccessfulReply(jsval* aValue)
|
|
|
|
{
|
|
|
|
*aValue = JSVAL_VOID;
|
|
|
|
BluetoothValue& v = mReply->get_BluetoothReplySuccess().value();
|
|
|
|
if (v.type() != BluetoothValue::TArrayOfBluetoothNamedValue) {
|
|
|
|
NS_WARNING("Not a BluetoothNamedValue array!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const InfallibleTArray<BluetoothNamedValue>& reply =
|
|
|
|
mReply->get_BluetoothReplySuccess().value().get_ArrayOfBluetoothNamedValue();
|
|
|
|
nsTArray<nsRefPtr<BluetoothDevice> > devices;
|
|
|
|
JSObject* JsDevices;
|
|
|
|
for (uint32_t i = 0; i < reply.Length(); i++) {
|
|
|
|
if (reply[i].value().type() != BluetoothValue::TArrayOfBluetoothNamedValue) {
|
|
|
|
NS_WARNING("Not a BluetoothNamedValue array!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
nsRefPtr<BluetoothDevice> d = BluetoothDevice::Create(mAdapterPtr->GetOwner(),
|
|
|
|
mAdapterPtr->GetPath(),
|
|
|
|
reply[i].value());
|
|
|
|
devices.AppendElement(d);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsIScriptContext* sc = mAdapterPtr->GetContextForEventHandlers(&rv);
|
|
|
|
if (!sc) {
|
|
|
|
NS_WARNING("Cannot create script context!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = BluetoothDeviceArrayToJSArray(sc->GetNativeContext(),
|
|
|
|
sc->GetNativeGlobal(), devices, &JsDevices);
|
|
|
|
|
|
|
|
if (JsDevices) {
|
|
|
|
aValue->setObject(*JsDevices);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_WARNING("Paird not yet set!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ReleaseMembers()
|
|
|
|
{
|
|
|
|
BluetoothReplyRunnable::ReleaseMembers();
|
|
|
|
mAdapterPtr = nullptr;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
nsRefPtr<BluetoothAdapter> mAdapterPtr;
|
|
|
|
};
|
|
|
|
|
2012-08-08 10:47:09 -07:00
|
|
|
BluetoothAdapter::BluetoothAdapter(nsPIDOMWindow* aOwner, const BluetoothValue& aValue)
|
2012-07-31 21:53:04 -07:00
|
|
|
: BluetoothPropertyContainer(BluetoothObjectType::TYPE_ADAPTER)
|
2012-08-13 17:57:54 -07:00
|
|
|
, mEnabled(false)
|
|
|
|
, mDiscoverable(false)
|
|
|
|
, mDiscovering(false)
|
|
|
|
, mPairable(false)
|
|
|
|
, mPowered(false)
|
2012-07-31 21:53:04 -07:00
|
|
|
, mJsUuids(nullptr)
|
|
|
|
, mJsDeviceAddresses(nullptr)
|
|
|
|
, mIsRooted(false)
|
2012-06-02 11:23:16 -07:00
|
|
|
{
|
2012-07-31 21:53:04 -07:00
|
|
|
BindToOwner(aOwner);
|
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]);
|
|
|
|
}
|
2012-07-31 21:53:04 -07:00
|
|
|
}
|
2012-06-02 11:23:16 -07:00
|
|
|
|
|
|
|
BluetoothAdapter::~BluetoothAdapter()
|
|
|
|
{
|
2012-07-17 20:41:54 -07:00
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
|
|
|
// We can be null on shutdown, where this might happen
|
|
|
|
if (bs) {
|
|
|
|
if (NS_FAILED(bs->UnregisterBluetoothSignalHandler(mPath, this))) {
|
|
|
|
NS_WARNING("Failed to unregister object with observer!");
|
|
|
|
}
|
|
|
|
}
|
2012-07-31 21:53:04 -07:00
|
|
|
Unroot();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothAdapter::Unroot()
|
|
|
|
{
|
|
|
|
if (!mIsRooted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
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("Enabled")) {
|
|
|
|
mEnabled = value.get_bool();
|
|
|
|
} 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);
|
|
|
|
if (sc) {
|
|
|
|
rv =
|
|
|
|
StringArrayToJSArray(sc->GetNativeContext(),
|
|
|
|
sc->GetNativeGlobal(), mUuids, &mJsUuids);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Cannot set JS UUIDs object!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Root();
|
|
|
|
} else {
|
|
|
|
NS_WARNING("Could not get context!");
|
|
|
|
}
|
|
|
|
} else if (name.EqualsLiteral("Devices")) {
|
|
|
|
mDeviceAddresses = value.get_ArrayOfnsString();
|
|
|
|
nsresult rv;
|
|
|
|
nsIScriptContext* sc = GetContextForEventHandlers(&rv);
|
|
|
|
if (sc) {
|
|
|
|
rv =
|
|
|
|
StringArrayToJSArray(sc->GetNativeContext(),
|
2012-08-01 18:59:03 -07:00
|
|
|
sc->GetNativeGlobal(), mDeviceAddresses, &mJsDeviceAddresses);
|
2012-07-31 21:53:04 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2012-08-01 18:59:03 -07:00
|
|
|
NS_WARNING("Cannot set JS Device Addresses object!");
|
2012-07-31 21:53:04 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Root();
|
|
|
|
} else {
|
|
|
|
NS_WARNING("Could not get context!");
|
|
|
|
}
|
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>
|
2012-08-08 10:47:09 -07:00
|
|
|
BluetoothAdapter::Create(nsPIDOMWindow* aOwner, const BluetoothValue& aValue)
|
2012-07-17 20:41:54 -07:00
|
|
|
{
|
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
2012-07-31 21:53:04 -07:00
|
|
|
if (!bs) {
|
|
|
|
NS_WARNING("BluetoothService not available!");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-07-17 20:41:54 -07:00
|
|
|
|
2012-08-08 10:47:09 -07:00
|
|
|
nsRefPtr<BluetoothAdapter> adapter = new BluetoothAdapter(aOwner, aValue);
|
|
|
|
if (NS_FAILED(bs->RegisterBluetoothSignalHandler(adapter->GetPath(), adapter))) {
|
2012-06-02 11:23:16 -07:00
|
|
|
NS_WARNING("Failed to register object with observer!");
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
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)
|
|
|
|
{
|
|
|
|
if (aData.name().EqualsLiteral("DeviceFound")) {
|
2012-07-31 21:53:04 -07:00
|
|
|
nsRefPtr<BluetoothDevice> d = BluetoothDevice::Create(GetOwner(), mPath, aData.value());
|
2012-07-17 20:41:54 -07:00
|
|
|
nsRefPtr<BluetoothDeviceEvent> e = BluetoothDeviceEvent::Create(d);
|
|
|
|
e->Dispatch(ToIDOMEventTarget(), NS_LITERAL_STRING("devicefound"));
|
2012-07-31 21:53:04 -07:00
|
|
|
} else if (aData.name().EqualsLiteral("PropertyChanged")) {
|
|
|
|
// Get BluetoothNamedValue, make sure array length is 1
|
|
|
|
InfallibleTArray<BluetoothNamedValue> arr = aData.value().get_ArrayOfBluetoothNamedValue();
|
|
|
|
if (arr.Length() != 1) {
|
|
|
|
// This really should not happen
|
|
|
|
NS_ERROR("Got more than one property in a change message!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
BluetoothNamedValue v = arr[0];
|
|
|
|
SetPropertyByValue(v);
|
|
|
|
nsRefPtr<BluetoothPropertyEvent> e = BluetoothPropertyEvent::Create(v.name());
|
|
|
|
e->Dispatch(ToIDOMEventTarget(), NS_LITERAL_STRING("propertychanged"));
|
2012-07-17 20:41:54 -07:00
|
|
|
} else {
|
|
|
|
#ifdef DEBUG
|
|
|
|
nsCString warningMsg;
|
|
|
|
warningMsg.AssignLiteral("Not handling manager signal: ");
|
|
|
|
warningMsg.Append(NS_ConvertUTF16toUTF8(aData.name()));
|
|
|
|
NS_WARNING(warningMsg.get());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
BluetoothAdapter::StartStopDiscovery(bool aStart, nsIDOMDOMRequest** aRequest)
|
|
|
|
{
|
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
2012-07-31 21:53:04 -07:00
|
|
|
if (!bs) {
|
|
|
|
NS_WARNING("BluetoothService not available!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-07-17 20:41:54 -07:00
|
|
|
|
2012-07-31 21:53:04 -07:00
|
|
|
nsCOMPtr<nsIDOMRequestService> rs = do_GetService("@mozilla.org/dom/dom-request-service;1");
|
2012-07-17 20:41:54 -07:00
|
|
|
if (!rs) {
|
|
|
|
NS_WARNING("No DOMRequest Service!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMDOMRequest> req;
|
|
|
|
nsresult rv = rs->CreateRequest(GetOwner(), getter_AddRefs(req));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Can't create DOMRequest!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<BluetoothVoidReplyRunnable> results = new BluetoothVoidReplyRunnable(req);
|
|
|
|
|
|
|
|
if (aStart) {
|
|
|
|
rv = bs->StartDiscoveryInternal(mPath, results);
|
|
|
|
} else {
|
|
|
|
rv = bs->StopDiscoveryInternal(mPath, results);
|
|
|
|
}
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Starting discovery failed!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
req.forget(aRequest);
|
|
|
|
|
|
|
|
// mDiscovering is not set here, we'll get a Property update from our external
|
|
|
|
// protocol to tell us that it's been set.
|
|
|
|
|
|
|
|
return NS_OK;
|
2012-06-02 11:23:16 -07:00
|
|
|
}
|
2012-07-17 20:41:54 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BluetoothAdapter::StartDiscovery(nsIDOMDOMRequest** aRequest)
|
|
|
|
{
|
|
|
|
return StartStopDiscovery(true, aRequest);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BluetoothAdapter::StopDiscovery(nsIDOMDOMRequest** aRequest)
|
|
|
|
{
|
|
|
|
return StartStopDiscovery(false, aRequest);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BluetoothAdapter::GetEnabled(bool* aEnabled)
|
|
|
|
{
|
|
|
|
*aEnabled = mEnabled;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BluetoothAdapter::GetAddress(nsAString& aAddress)
|
|
|
|
{
|
|
|
|
aAddress = mAddress;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BluetoothAdapter::GetAdapterClass(PRUint32* aClass)
|
|
|
|
{
|
|
|
|
*aClass = mClass;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BluetoothAdapter::GetDiscovering(bool* aDiscovering)
|
|
|
|
{
|
|
|
|
*aDiscovering = mDiscovering;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BluetoothAdapter::GetName(nsAString& aName)
|
|
|
|
{
|
|
|
|
aName = mName;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BluetoothAdapter::GetDiscoverable(bool* aDiscoverable)
|
|
|
|
{
|
|
|
|
*aDiscoverable = mDiscoverable;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BluetoothAdapter::GetDiscoverableTimeout(PRUint32* aDiscoverableTimeout)
|
|
|
|
{
|
|
|
|
*aDiscoverableTimeout = mDiscoverableTimeout;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BluetoothAdapter::GetDevices(JSContext* aCx, jsval* aDevices)
|
|
|
|
{
|
2012-07-31 21:53:04 -07:00
|
|
|
if (mJsDeviceAddresses) {
|
|
|
|
aDevices->setObject(*mJsDeviceAddresses);
|
|
|
|
}
|
|
|
|
else {
|
2012-08-01 18:59:03 -07:00
|
|
|
NS_WARNING("Devices not yet set!\n");
|
2012-07-31 21:53:04 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
BluetoothAdapter::GetUuids(JSContext* aCx, jsval* aValue)
|
|
|
|
{
|
|
|
|
if (mJsUuids) {
|
|
|
|
aValue->setObject(*mJsUuids);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_WARNING("UUIDs not yet set!\n");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-07-17 20:41:54 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-07-31 21:53:04 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BluetoothAdapter::SetName(const nsAString& aName,
|
|
|
|
nsIDOMDOMRequest** aRequest)
|
|
|
|
{
|
|
|
|
if (mName.Equals(aName)) {
|
2012-08-13 17:57:54 -07:00
|
|
|
return FirePropertyAlreadySet(GetOwner(), aRequest);
|
2012-07-31 21:53:04 -07:00
|
|
|
}
|
|
|
|
nsString name(aName);
|
|
|
|
BluetoothValue value(name);
|
|
|
|
BluetoothNamedValue property(NS_LITERAL_STRING("Name"), value);
|
|
|
|
return SetProperty(GetOwner(), property, aRequest);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BluetoothAdapter::SetDiscoverable(const bool aDiscoverable,
|
|
|
|
nsIDOMDOMRequest** aRequest)
|
|
|
|
{
|
|
|
|
if (aDiscoverable == mDiscoverable) {
|
2012-08-13 17:57:54 -07:00
|
|
|
return FirePropertyAlreadySet(GetOwner(), aRequest);
|
2012-07-31 21:53:04 -07:00
|
|
|
}
|
|
|
|
BluetoothValue value(aDiscoverable);
|
|
|
|
BluetoothNamedValue property(NS_LITERAL_STRING("Discoverable"), value);
|
|
|
|
return SetProperty(GetOwner(), property, aRequest);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BluetoothAdapter::SetDiscoverableTimeout(const PRUint32 aDiscoverableTimeout,
|
|
|
|
nsIDOMDOMRequest** aRequest)
|
|
|
|
{
|
|
|
|
if (aDiscoverableTimeout == mDiscoverableTimeout) {
|
2012-08-13 17:57:54 -07:00
|
|
|
return FirePropertyAlreadySet(GetOwner(), aRequest);
|
2012-07-31 21:53:04 -07:00
|
|
|
}
|
|
|
|
BluetoothValue value(aDiscoverableTimeout);
|
|
|
|
BluetoothNamedValue property(NS_LITERAL_STRING("DiscoverableTimeout"), value);
|
|
|
|
return SetProperty(GetOwner(), property, aRequest);
|
|
|
|
}
|
|
|
|
|
2012-08-16 19:53:45 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BluetoothAdapter::GetPairedDevices(nsIDOMDOMRequest** aRequest)
|
|
|
|
{
|
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
|
|
|
if (!bs) {
|
|
|
|
NS_WARNING("BluetoothService not available!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMRequestService> rs = do_GetService("@mozilla.org/dom/dom-request-service;1");
|
|
|
|
if (!rs) {
|
|
|
|
NS_WARNING("No DOMRequest Service!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMDOMRequest> request;
|
|
|
|
nsresult rv = rs->CreateRequest(GetOwner(), getter_AddRefs(request));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Can't create DOMRequest!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<BluetoothReplyRunnable> results = new GetPairedDevicesTask(this, request);
|
|
|
|
if (NS_FAILED(bs->GetPairedDevicePropertiesInternal(mDeviceAddresses, results))) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
request.forget(aRequest);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-07-17 20:41:54 -07:00
|
|
|
NS_IMPL_EVENT_HANDLER(BluetoothAdapter, propertychanged)
|
|
|
|
NS_IMPL_EVENT_HANDLER(BluetoothAdapter, devicefound)
|
|
|
|
NS_IMPL_EVENT_HANDLER(BluetoothAdapter, devicedisappeared)
|