2012-05-23 21:02:23 -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-05-23 21:02:23 -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/. */
|
|
|
|
|
2012-07-17 20:41:54 -07:00
|
|
|
#include "base/basictypes.h"
|
2012-05-23 21:02:23 -07:00
|
|
|
#include "BluetoothManager.h"
|
|
|
|
#include "BluetoothCommon.h"
|
|
|
|
#include "BluetoothAdapter.h"
|
2012-07-17 20:41:54 -07:00
|
|
|
#include "BluetoothService.h"
|
|
|
|
#include "BluetoothReplyRunnable.h"
|
2012-05-23 21:02:23 -07:00
|
|
|
|
2012-11-20 23:33:07 -08:00
|
|
|
#include "DOMRequest.h"
|
2012-07-17 20:41:54 -07:00
|
|
|
#include "nsContentUtils.h"
|
2012-05-23 21:02:23 -07:00
|
|
|
#include "nsDOMClassInfo.h"
|
2012-08-29 23:08:50 -07:00
|
|
|
#include "nsIPermissionManager.h"
|
2012-05-23 21:02:23 -07:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "mozilla/Util.h"
|
2012-09-13 09:37:14 -07:00
|
|
|
#include "mozilla/dom/bluetooth/BluetoothTypes.h"
|
2013-08-12 01:31:58 -07:00
|
|
|
#include "mozilla/dom/BluetoothManagerBinding.h"
|
2012-05-23 21:02:23 -07:00
|
|
|
|
2012-06-02 11:23:16 -07:00
|
|
|
using namespace mozilla;
|
2012-05-23 21:02:23 -07:00
|
|
|
|
|
|
|
USING_BLUETOOTH_NAMESPACE
|
|
|
|
|
2013-08-12 01:31:58 -07:00
|
|
|
// QueryInterface implementation for BluetoothManager
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(BluetoothManager)
|
2012-05-23 21:02:23 -07:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(BluetoothManager, nsDOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(BluetoothManager, nsDOMEventTargetHelper)
|
|
|
|
|
2012-07-17 20:41:54 -07:00
|
|
|
class GetAdapterTask : public BluetoothReplyRunnable
|
2012-05-23 21:02:23 -07:00
|
|
|
{
|
2012-07-17 20:41:54 -07:00
|
|
|
public:
|
|
|
|
GetAdapterTask(BluetoothManager* aManager,
|
|
|
|
nsIDOMDOMRequest* aReq) :
|
|
|
|
BluetoothReplyRunnable(aReq),
|
|
|
|
mManagerPtr(aManager)
|
|
|
|
{
|
|
|
|
}
|
2012-05-23 21:02:23 -07:00
|
|
|
|
2012-07-17 20:41:54 -07:00
|
|
|
bool
|
2013-04-02 16:06:14 -07:00
|
|
|
ParseSuccessfulReply(JS::Value* aValue)
|
2012-07-17 20:41:54 -07:00
|
|
|
{
|
|
|
|
*aValue = JSVAL_VOID;
|
|
|
|
|
2012-11-20 23:33:07 -08:00
|
|
|
const BluetoothValue& v = mReply->get_BluetoothReplySuccess().value();
|
2013-01-28 22:52:48 -08:00
|
|
|
if (v.type() != BluetoothValue::TArrayOfBluetoothNamedValue) {
|
2013-09-13 15:51:00 -07:00
|
|
|
BT_WARNING("Not a BluetoothNamedValue array!");
|
2013-01-28 22:52:48 -08:00
|
|
|
SetError(NS_LITERAL_STRING("BluetoothReplyTypeError"));
|
|
|
|
return false;
|
|
|
|
}
|
2012-11-20 23:33:07 -08:00
|
|
|
|
|
|
|
const InfallibleTArray<BluetoothNamedValue>& values =
|
|
|
|
v.get_ArrayOfBluetoothNamedValue();
|
2013-08-12 01:32:53 -07:00
|
|
|
nsRefPtr<BluetoothAdapter> adapter =
|
|
|
|
BluetoothAdapter::Create(mManagerPtr->GetOwner(), values);
|
2012-07-17 20:41:54 -07:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsIScriptContext* sc = mManagerPtr->GetContextForEventHandlers(&rv);
|
|
|
|
if (!sc) {
|
2013-09-13 15:51:00 -07:00
|
|
|
BT_WARNING("Cannot create script context!");
|
2012-07-17 20:41:54 -07:00
|
|
|
SetError(NS_LITERAL_STRING("BluetoothScriptContextError"));
|
|
|
|
return false;
|
2012-05-23 21:02:23 -07:00
|
|
|
}
|
|
|
|
|
2013-05-24 01:50:00 -07:00
|
|
|
AutoPushJSContext cx(sc->GetNativeContext());
|
|
|
|
|
2013-09-04 14:06:57 -07:00
|
|
|
JS::Rooted<JSObject*> global(cx, sc->GetWindowProxy());
|
2013-05-02 02:12:46 -07:00
|
|
|
rv = nsContentUtils::WrapNative(cx, global, adapter, aValue);
|
2012-11-20 23:33:07 -08:00
|
|
|
if (NS_FAILED(rv)) {
|
2013-09-13 15:51:00 -07:00
|
|
|
BT_WARNING("Cannot create native object!");
|
2012-07-17 20:41:54 -07:00
|
|
|
SetError(NS_LITERAL_STRING("BluetoothNativeObjectError"));
|
2012-11-20 23:33:07 -08:00
|
|
|
return false;
|
2012-05-23 21:02:23 -07:00
|
|
|
}
|
|
|
|
|
2012-11-20 23:33:07 -08:00
|
|
|
return true;
|
2012-07-17 20:41:54 -07:00
|
|
|
}
|
2012-05-23 21:02:23 -07:00
|
|
|
|
2012-07-17 20:41:54 -07:00
|
|
|
void
|
|
|
|
ReleaseMembers()
|
|
|
|
{
|
|
|
|
BluetoothReplyRunnable::ReleaseMembers();
|
2012-07-30 07:20:58 -07:00
|
|
|
mManagerPtr = nullptr;
|
2012-07-17 20:41:54 -07:00
|
|
|
}
|
2013-05-24 01:50:00 -07:00
|
|
|
|
2012-07-17 20:41:54 -07:00
|
|
|
private:
|
|
|
|
nsRefPtr<BluetoothManager> mManagerPtr;
|
|
|
|
};
|
2012-05-23 21:02:23 -07:00
|
|
|
|
2012-09-06 07:15:36 -07:00
|
|
|
BluetoothManager::BluetoothManager(nsPIDOMWindow *aWindow)
|
2013-08-12 01:31:58 -07:00
|
|
|
: nsDOMEventTargetHelper(aWindow)
|
|
|
|
, BluetoothPropertyContainer(BluetoothObjectType::TYPE_MANAGER)
|
2012-05-23 21:02:23 -07:00
|
|
|
{
|
2012-09-06 07:15:36 -07:00
|
|
|
MOZ_ASSERT(aWindow);
|
2013-08-12 01:31:58 -07:00
|
|
|
MOZ_ASSERT(IsDOMBinding());
|
2012-09-06 07:15:36 -07:00
|
|
|
|
2012-05-23 21:02:23 -07:00
|
|
|
BindToOwner(aWindow);
|
2012-07-31 21:53:04 -07:00
|
|
|
mPath.AssignLiteral("/");
|
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(NS_LITERAL_STRING(KEY_MANAGER), this);
|
2012-05-23 21:02:23 -07:00
|
|
|
}
|
|
|
|
|
2012-06-02 11:23:16 -07:00
|
|
|
BluetoothManager::~BluetoothManager()
|
|
|
|
{
|
2012-07-17 20:41:54 -07:00
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
2013-02-08 02:03:09 -08:00
|
|
|
NS_ENSURE_TRUE_VOID(bs);
|
2013-03-15 00:15:47 -07:00
|
|
|
bs->UnregisterBluetoothSignalHandler(NS_LITERAL_STRING(KEY_MANAGER), this);
|
2012-08-29 23:08:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
2013-09-13 15:51:00 -07:00
|
|
|
BT_WARNING(warningMsg.get());
|
2012-08-29 23:08:50 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-08-12 01:31:58 -07:00
|
|
|
bool
|
|
|
|
BluetoothManager::GetEnabled(ErrorResult& aRv)
|
2012-05-23 21:02:23 -07:00
|
|
|
{
|
2012-09-06 07:15:36 -07:00
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
2013-08-12 01:31:58 -07:00
|
|
|
if (!bs) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return false;
|
|
|
|
}
|
2012-09-06 07:15:36 -07:00
|
|
|
|
2013-08-12 01:31:58 -07:00
|
|
|
return bs->IsEnabled();
|
2012-05-23 21:02:23 -07:00
|
|
|
}
|
|
|
|
|
2013-08-12 01:31:58 -07:00
|
|
|
already_AddRefed<dom::DOMRequest>
|
|
|
|
BluetoothManager::GetDefaultAdapter(ErrorResult& aRv)
|
2012-05-23 21:02:23 -07:00
|
|
|
{
|
2013-08-12 01:31:58 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win = GetOwner();
|
|
|
|
if (!win) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-07-17 20:41:54 -07:00
|
|
|
|
2013-08-12 01:31:58 -07:00
|
|
|
nsRefPtr<DOMRequest> request = new DOMRequest(win);
|
|
|
|
nsRefPtr<BluetoothReplyRunnable> results =
|
|
|
|
new GetAdapterTask(this, request);
|
2013-08-12 01:31:58 -07:00
|
|
|
|
2013-08-12 09:08:19 -07:00
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
2013-08-12 01:31:58 -07:00
|
|
|
if (!bs) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = bs->GetDefaultAdapterPathInternal(results);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return nullptr;
|
2012-07-17 20:41:54 -07:00
|
|
|
}
|
2012-11-20 23:33:07 -08:00
|
|
|
|
2013-08-12 01:31:58 -07:00
|
|
|
return request.forget();
|
2012-06-02 11:23:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<BluetoothManager>
|
2012-09-06 07:15:36 -07:00
|
|
|
BluetoothManager::Create(nsPIDOMWindow* aWindow)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aWindow);
|
|
|
|
|
|
|
|
nsRefPtr<BluetoothManager> manager = new BluetoothManager(aWindow);
|
2012-06-02 11:23:16 -07:00
|
|
|
return manager.forget();
|
2012-05-23 21:02:23 -07:00
|
|
|
}
|
|
|
|
|
2013-07-12 07:36:32 -07:00
|
|
|
// static
|
|
|
|
bool
|
|
|
|
BluetoothManager::CheckPermission(nsPIDOMWindow* aWindow)
|
2012-05-23 21:02:23 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aWindow, "Null pointer!");
|
|
|
|
|
2012-08-07 21:10:36 -07:00
|
|
|
nsCOMPtr<nsIPermissionManager> permMgr =
|
|
|
|
do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
|
2013-07-12 07:36:32 -07:00
|
|
|
NS_ENSURE_TRUE(permMgr, false);
|
2012-08-07 21:10:36 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t permission;
|
2012-08-07 21:10:36 -07:00
|
|
|
nsresult rv =
|
2012-11-27 12:10:47 -08:00
|
|
|
permMgr->TestPermissionFromWindow(aWindow, "bluetooth",
|
2012-09-21 03:32:18 -07:00
|
|
|
&permission);
|
2013-07-12 07:36:32 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
2012-05-23 21:02:23 -07:00
|
|
|
|
2013-07-12 07:36:32 -07:00
|
|
|
return permission == nsIPermissionManager::ALLOW_ACTION;
|
2012-05-23 21:02:23 -07:00
|
|
|
}
|
2012-06-02 11:23:16 -07:00
|
|
|
|
2012-07-17 20:41:54 -07:00
|
|
|
void
|
|
|
|
BluetoothManager::Notify(const BluetoothSignal& aData)
|
|
|
|
{
|
2013-09-13 15:51:00 -07:00
|
|
|
BT_LOGD("[M] %s: %s", __FUNCTION__, NS_ConvertUTF16toUTF8(aData.name()).get());
|
2013-01-29 02:37:51 -08:00
|
|
|
|
2012-09-02 23:38:44 -07:00
|
|
|
if (aData.name().EqualsLiteral("AdapterAdded")) {
|
2012-09-27 13:11:31 -07:00
|
|
|
DispatchTrustedEvent(NS_LITERAL_STRING("adapteradded"));
|
2013-02-08 02:03:09 -08:00
|
|
|
} else if (aData.name().EqualsLiteral("Enabled")) {
|
|
|
|
DispatchTrustedEvent(NS_LITERAL_STRING("enabled"));
|
|
|
|
} else if (aData.name().EqualsLiteral("Disabled")) {
|
|
|
|
DispatchTrustedEvent(NS_LITERAL_STRING("disabled"));
|
2012-09-02 23:38:44 -07:00
|
|
|
} else {
|
2012-07-17 20:41:54 -07:00
|
|
|
#ifdef DEBUG
|
2012-09-02 23:38:44 -07:00
|
|
|
nsCString warningMsg;
|
|
|
|
warningMsg.AssignLiteral("Not handling manager signal: ");
|
|
|
|
warningMsg.Append(NS_ConvertUTF16toUTF8(aData.name()));
|
2013-09-13 15:51:00 -07:00
|
|
|
BT_WARNING(warningMsg.get());
|
2012-07-17 20:41:54 -07:00
|
|
|
#endif
|
2012-09-02 23:38:44 -07:00
|
|
|
}
|
2012-06-02 11:23:16 -07:00
|
|
|
}
|
2012-08-29 23:08:50 -07:00
|
|
|
|
2013-08-12 01:31:58 -07:00
|
|
|
bool
|
|
|
|
BluetoothManager::IsConnected(uint16_t aProfileId, ErrorResult& aRv)
|
2012-11-07 10:12:41 -08:00
|
|
|
{
|
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
|
|
|
if (!bs) {
|
2013-08-12 01:31:58 -07:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return false;
|
2012-11-07 10:12:41 -08:00
|
|
|
}
|
|
|
|
|
2013-08-12 01:31:58 -07:00
|
|
|
return bs->IsConnected(aProfileId);
|
2012-11-07 10:12:41 -08:00
|
|
|
}
|
|
|
|
|
2013-08-12 01:31:58 -07:00
|
|
|
JSObject*
|
|
|
|
BluetoothManager::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
|
|
|
|
{
|
|
|
|
return BluetoothManagerBinding::Wrap(aCx, aScope, this);
|
|
|
|
}
|