mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-29 15:52:07 +00:00
963858ac23
--HG-- extra : rebase_source : 74569bd08dbc0d6755ac45999cfe99725ef6bc0a
217 lines
5.8 KiB
C++
217 lines
5.8 KiB
C++
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
/* 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 "BluetoothManager.h"
|
|
#include "BluetoothCommon.h"
|
|
#include "BluetoothAdapter.h"
|
|
#include "BluetoothService.h"
|
|
#include "BluetoothReplyRunnable.h"
|
|
|
|
#include "DOMRequest.h"
|
|
#include "nsContentUtils.h"
|
|
#include "nsDOMClassInfo.h"
|
|
#include "nsIPermissionManager.h"
|
|
#include "nsThreadUtils.h"
|
|
#include "mozilla/dom/bluetooth/BluetoothTypes.h"
|
|
#include "mozilla/dom/BluetoothManagerBinding.h"
|
|
#include "mozilla/dom/ScriptSettings.h"
|
|
#include "mozilla/Services.h"
|
|
|
|
using namespace mozilla;
|
|
|
|
USING_BLUETOOTH_NAMESPACE
|
|
|
|
// QueryInterface implementation for BluetoothManager
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(BluetoothManager)
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
|
|
|
NS_IMPL_ADDREF_INHERITED(BluetoothManager, DOMEventTargetHelper)
|
|
NS_IMPL_RELEASE_INHERITED(BluetoothManager, DOMEventTargetHelper)
|
|
|
|
class GetAdapterTask : public BluetoothReplyRunnable
|
|
{
|
|
public:
|
|
GetAdapterTask(BluetoothManager* aManager,
|
|
nsIDOMDOMRequest* aReq) :
|
|
BluetoothReplyRunnable(aReq),
|
|
mManagerPtr(aManager)
|
|
{
|
|
}
|
|
|
|
bool
|
|
ParseSuccessfulReply(JS::MutableHandle<JS::Value> aValue)
|
|
{
|
|
aValue.setUndefined();
|
|
|
|
const BluetoothValue& v = mReply->get_BluetoothReplySuccess().value();
|
|
if (v.type() != BluetoothValue::TArrayOfBluetoothNamedValue) {
|
|
BT_WARNING("Not a BluetoothNamedValue array!");
|
|
SetError(NS_LITERAL_STRING("BluetoothReplyTypeError"));
|
|
return false;
|
|
}
|
|
|
|
if (!mManagerPtr->GetOwner()) {
|
|
BT_WARNING("Bluetooth manager was disconnected from owner window.");
|
|
|
|
// Stop to create adapter since owner window of Bluetooth manager was
|
|
// gone. These is no need to create a DOMEvent target which has no owner
|
|
// to reply to.
|
|
return false;
|
|
}
|
|
|
|
const InfallibleTArray<BluetoothNamedValue>& values =
|
|
v.get_ArrayOfBluetoothNamedValue();
|
|
nsRefPtr<BluetoothAdapter> adapter =
|
|
BluetoothAdapter::Create(mManagerPtr->GetOwner(), values);
|
|
|
|
dom::AutoJSAPI jsapi;
|
|
if (!jsapi.Init(mManagerPtr->GetOwner())) {
|
|
BT_WARNING("Failed to initialise AutoJSAPI!");
|
|
SetError(NS_LITERAL_STRING("BluetoothAutoJSAPIInitError"));
|
|
return false;
|
|
}
|
|
JSContext* cx = jsapi.cx();
|
|
if (NS_FAILED(nsContentUtils::WrapNative(cx, adapter, aValue))) {
|
|
BT_WARNING("Cannot create native object!");
|
|
SetError(NS_LITERAL_STRING("BluetoothNativeObjectError"));
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void
|
|
ReleaseMembers()
|
|
{
|
|
BluetoothReplyRunnable::ReleaseMembers();
|
|
mManagerPtr = nullptr;
|
|
}
|
|
|
|
private:
|
|
nsRefPtr<BluetoothManager> mManagerPtr;
|
|
};
|
|
|
|
BluetoothManager::BluetoothManager(nsPIDOMWindow *aWindow)
|
|
: DOMEventTargetHelper(aWindow)
|
|
, BluetoothPropertyContainer(BluetoothObjectType::TYPE_MANAGER)
|
|
{
|
|
MOZ_ASSERT(aWindow);
|
|
|
|
mPath.Assign('/');
|
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
|
NS_ENSURE_TRUE_VOID(bs);
|
|
bs->RegisterBluetoothSignalHandler(NS_LITERAL_STRING(KEY_MANAGER), this);
|
|
}
|
|
|
|
BluetoothManager::~BluetoothManager()
|
|
{
|
|
BluetoothService* bs = BluetoothService::Get();
|
|
NS_ENSURE_TRUE_VOID(bs);
|
|
bs->UnregisterBluetoothSignalHandler(NS_LITERAL_STRING(KEY_MANAGER), this);
|
|
}
|
|
|
|
void
|
|
BluetoothManager::DisconnectFromOwner()
|
|
{
|
|
DOMEventTargetHelper::DisconnectFromOwner();
|
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
|
NS_ENSURE_TRUE_VOID(bs);
|
|
bs->UnregisterBluetoothSignalHandler(NS_LITERAL_STRING(KEY_MANAGER), this);
|
|
}
|
|
|
|
void
|
|
BluetoothManager::SetPropertyByValue(const BluetoothNamedValue& aValue)
|
|
{
|
|
#ifdef DEBUG
|
|
const nsString& name = aValue.name();
|
|
nsCString warningMsg;
|
|
warningMsg.AssignLiteral("Not handling manager property: ");
|
|
warningMsg.Append(NS_ConvertUTF16toUTF8(name));
|
|
BT_WARNING(warningMsg.get());
|
|
#endif
|
|
}
|
|
|
|
bool
|
|
BluetoothManager::GetEnabled(ErrorResult& aRv)
|
|
{
|
|
BluetoothService* bs = BluetoothService::Get();
|
|
if (!bs) {
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
return false;
|
|
}
|
|
|
|
return bs->IsEnabled();
|
|
}
|
|
|
|
already_AddRefed<dom::DOMRequest>
|
|
BluetoothManager::GetDefaultAdapter(ErrorResult& aRv)
|
|
{
|
|
nsCOMPtr<nsPIDOMWindow> win = GetOwner();
|
|
if (!win) {
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
return nullptr;
|
|
}
|
|
|
|
nsRefPtr<DOMRequest> request = new DOMRequest(win);
|
|
nsRefPtr<BluetoothReplyRunnable> results =
|
|
new GetAdapterTask(this, request);
|
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
|
if (!bs) {
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
return nullptr;
|
|
}
|
|
|
|
nsresult rv = bs->GetDefaultAdapterPathInternal(results);
|
|
if (NS_FAILED(rv)) {
|
|
aRv.Throw(rv);
|
|
return nullptr;
|
|
}
|
|
|
|
return request.forget();
|
|
}
|
|
|
|
// static
|
|
already_AddRefed<BluetoothManager>
|
|
BluetoothManager::Create(nsPIDOMWindow* aWindow)
|
|
{
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
MOZ_ASSERT(aWindow);
|
|
|
|
nsRefPtr<BluetoothManager> manager = new BluetoothManager(aWindow);
|
|
return manager.forget();
|
|
}
|
|
|
|
void
|
|
BluetoothManager::Notify(const BluetoothSignal& aData)
|
|
{
|
|
BT_LOGD("[M] %s: %s", __FUNCTION__, NS_ConvertUTF16toUTF8(aData.name()).get());
|
|
|
|
if (aData.name().EqualsLiteral("AdapterAdded")) {
|
|
DispatchTrustedEvent(NS_LITERAL_STRING("adapteradded"));
|
|
} else if (aData.name().EqualsLiteral("Enabled")) {
|
|
DispatchTrustedEvent(NS_LITERAL_STRING("enabled"));
|
|
} else if (aData.name().EqualsLiteral("Disabled")) {
|
|
DispatchTrustedEvent(NS_LITERAL_STRING("disabled"));
|
|
} else {
|
|
#ifdef DEBUG
|
|
nsCString warningMsg;
|
|
warningMsg.AssignLiteral("Not handling manager signal: ");
|
|
warningMsg.Append(NS_ConvertUTF16toUTF8(aData.name()));
|
|
BT_WARNING(warningMsg.get());
|
|
#endif
|
|
}
|
|
}
|
|
|
|
JSObject*
|
|
BluetoothManager::WrapObject(JSContext* aCx)
|
|
{
|
|
return BluetoothManagerBinding::Wrap(aCx, this);
|
|
}
|