2012-05-24 04:02:23 +00:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
2012-08-21 03:21:24 +00:00
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
2012-05-24 04:02:23 +00: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-18 03:41:54 +00:00
|
|
|
#include "base/basictypes.h"
|
2012-05-24 04:02:23 +00:00
|
|
|
#include "BluetoothManager.h"
|
|
|
|
#include "BluetoothCommon.h"
|
|
|
|
#include "BluetoothAdapter.h"
|
2012-07-18 03:41:54 +00:00
|
|
|
#include "BluetoothService.h"
|
|
|
|
#include "BluetoothReplyRunnable.h"
|
2012-05-24 04:02:23 +00:00
|
|
|
|
2012-11-21 07:33:07 +00:00
|
|
|
#include "DOMRequest.h"
|
2012-07-18 03:41:54 +00:00
|
|
|
#include "nsContentUtils.h"
|
2012-05-24 04:02:23 +00:00
|
|
|
#include "nsDOMClassInfo.h"
|
2012-08-30 06:08:50 +00:00
|
|
|
#include "nsIPermissionManager.h"
|
2012-05-24 04:02:23 +00:00
|
|
|
#include "nsThreadUtils.h"
|
2012-09-13 16:37:14 +00:00
|
|
|
#include "mozilla/dom/bluetooth/BluetoothTypes.h"
|
2013-08-12 08:31:58 +00:00
|
|
|
#include "mozilla/dom/BluetoothManagerBinding.h"
|
2014-06-19 19:17:59 +00:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2014-04-29 17:27:26 +00:00
|
|
|
#include "mozilla/Services.h"
|
2012-05-24 04:02:23 +00:00
|
|
|
|
2012-06-02 18:23:16 +00:00
|
|
|
using namespace mozilla;
|
2012-05-24 04:02:23 +00:00
|
|
|
|
|
|
|
USING_BLUETOOTH_NAMESPACE
|
|
|
|
|
2013-08-12 08:31:58 +00:00
|
|
|
// QueryInterface implementation for BluetoothManager
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(BluetoothManager)
|
2014-04-01 06:13:50 +00:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
2012-05-24 04:02:23 +00:00
|
|
|
|
2014-04-01 06:13:50 +00:00
|
|
|
NS_IMPL_ADDREF_INHERITED(BluetoothManager, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(BluetoothManager, DOMEventTargetHelper)
|
2012-05-24 04:02:23 +00:00
|
|
|
|
2012-07-18 03:41:54 +00:00
|
|
|
class GetAdapterTask : public BluetoothReplyRunnable
|
2012-05-24 04:02:23 +00:00
|
|
|
{
|
2012-07-18 03:41:54 +00:00
|
|
|
public:
|
|
|
|
GetAdapterTask(BluetoothManager* aManager,
|
|
|
|
nsIDOMDOMRequest* aReq) :
|
|
|
|
BluetoothReplyRunnable(aReq),
|
|
|
|
mManagerPtr(aManager)
|
|
|
|
{
|
|
|
|
}
|
2012-05-24 04:02:23 +00:00
|
|
|
|
2012-07-18 03:41:54 +00:00
|
|
|
bool
|
2013-10-16 17:44:50 +00:00
|
|
|
ParseSuccessfulReply(JS::MutableHandle<JS::Value> aValue)
|
2012-07-18 03:41:54 +00:00
|
|
|
{
|
2013-10-16 17:44:50 +00:00
|
|
|
aValue.setUndefined();
|
2012-07-18 03:41:54 +00:00
|
|
|
|
2012-11-21 07:33:07 +00:00
|
|
|
const BluetoothValue& v = mReply->get_BluetoothReplySuccess().value();
|
2013-01-29 06:52:48 +00:00
|
|
|
if (v.type() != BluetoothValue::TArrayOfBluetoothNamedValue) {
|
2013-09-13 22:51:00 +00:00
|
|
|
BT_WARNING("Not a BluetoothNamedValue array!");
|
2013-01-29 06:52:48 +00:00
|
|
|
SetError(NS_LITERAL_STRING("BluetoothReplyTypeError"));
|
|
|
|
return false;
|
|
|
|
}
|
2012-11-21 07:33:07 +00:00
|
|
|
|
2014-04-01 12:58:55 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-11-21 07:33:07 +00:00
|
|
|
const InfallibleTArray<BluetoothNamedValue>& values =
|
|
|
|
v.get_ArrayOfBluetoothNamedValue();
|
2013-08-12 08:32:53 +00:00
|
|
|
nsRefPtr<BluetoothAdapter> adapter =
|
|
|
|
BluetoothAdapter::Create(mManagerPtr->GetOwner(), values);
|
2012-07-18 03:41:54 +00:00
|
|
|
|
2014-06-19 19:17:59 +00:00
|
|
|
dom::AutoJSAPI jsapi;
|
2014-06-25 10:17:17 +00:00
|
|
|
if (!jsapi.Init(mManagerPtr->GetOwner())) {
|
2014-06-19 19:17:59 +00:00
|
|
|
BT_WARNING("Failed to initialise AutoJSAPI!");
|
|
|
|
SetError(NS_LITERAL_STRING("BluetoothAutoJSAPIInitError"));
|
2012-07-18 03:41:54 +00:00
|
|
|
return false;
|
2012-05-24 04:02:23 +00:00
|
|
|
}
|
2014-06-19 19:17:59 +00:00
|
|
|
JSContext* cx = jsapi.cx();
|
|
|
|
if (NS_FAILED(nsContentUtils::WrapNative(cx, adapter, aValue))) {
|
2013-09-13 22:51:00 +00:00
|
|
|
BT_WARNING("Cannot create native object!");
|
2012-07-18 03:41:54 +00:00
|
|
|
SetError(NS_LITERAL_STRING("BluetoothNativeObjectError"));
|
2012-11-21 07:33:07 +00:00
|
|
|
return false;
|
2012-05-24 04:02:23 +00:00
|
|
|
}
|
|
|
|
|
2012-11-21 07:33:07 +00:00
|
|
|
return true;
|
2012-07-18 03:41:54 +00:00
|
|
|
}
|
2012-05-24 04:02:23 +00:00
|
|
|
|
2012-07-18 03:41:54 +00:00
|
|
|
void
|
|
|
|
ReleaseMembers()
|
|
|
|
{
|
|
|
|
BluetoothReplyRunnable::ReleaseMembers();
|
2012-07-30 14:20:58 +00:00
|
|
|
mManagerPtr = nullptr;
|
2012-07-18 03:41:54 +00:00
|
|
|
}
|
2013-05-24 08:50:00 +00:00
|
|
|
|
2012-07-18 03:41:54 +00:00
|
|
|
private:
|
|
|
|
nsRefPtr<BluetoothManager> mManagerPtr;
|
|
|
|
};
|
2012-05-24 04:02:23 +00:00
|
|
|
|
2012-09-06 14:15:36 +00:00
|
|
|
BluetoothManager::BluetoothManager(nsPIDOMWindow *aWindow)
|
2014-04-01 06:13:50 +00:00
|
|
|
: DOMEventTargetHelper(aWindow)
|
2013-08-12 08:31:58 +00:00
|
|
|
, BluetoothPropertyContainer(BluetoothObjectType::TYPE_MANAGER)
|
2012-05-24 04:02:23 +00:00
|
|
|
{
|
2012-09-06 14:15:36 +00:00
|
|
|
MOZ_ASSERT(aWindow);
|
|
|
|
|
2014-05-26 18:55:08 +00:00
|
|
|
mPath.Assign('/');
|
2013-02-08 10:03:09 +00:00
|
|
|
|
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
|
|
|
NS_ENSURE_TRUE_VOID(bs);
|
2013-03-15 07:15:47 +00:00
|
|
|
bs->RegisterBluetoothSignalHandler(NS_LITERAL_STRING(KEY_MANAGER), this);
|
2012-05-24 04:02:23 +00:00
|
|
|
}
|
|
|
|
|
2012-06-02 18:23:16 +00:00
|
|
|
BluetoothManager::~BluetoothManager()
|
|
|
|
{
|
2012-07-18 03:41:54 +00:00
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
2013-02-08 10:03:09 +00:00
|
|
|
NS_ENSURE_TRUE_VOID(bs);
|
2014-04-01 12:58:56 +00:00
|
|
|
bs->UnregisterBluetoothSignalHandler(NS_LITERAL_STRING(KEY_MANAGER), this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BluetoothManager::DisconnectFromOwner()
|
|
|
|
{
|
2014-04-01 22:10:06 +00:00
|
|
|
DOMEventTargetHelper::DisconnectFromOwner();
|
2014-04-01 12:58:56 +00:00
|
|
|
|
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
|
|
|
NS_ENSURE_TRUE_VOID(bs);
|
2013-03-15 07:15:47 +00:00
|
|
|
bs->UnregisterBluetoothSignalHandler(NS_LITERAL_STRING(KEY_MANAGER), this);
|
2012-08-30 06:08:50 +00: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 22:51:00 +00:00
|
|
|
BT_WARNING(warningMsg.get());
|
2012-08-30 06:08:50 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-08-12 08:31:58 +00:00
|
|
|
bool
|
|
|
|
BluetoothManager::GetEnabled(ErrorResult& aRv)
|
2012-05-24 04:02:23 +00:00
|
|
|
{
|
2012-09-06 14:15:36 +00:00
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
2013-08-12 08:31:58 +00:00
|
|
|
if (!bs) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return false;
|
|
|
|
}
|
2012-09-06 14:15:36 +00:00
|
|
|
|
2013-08-12 08:31:58 +00:00
|
|
|
return bs->IsEnabled();
|
2012-05-24 04:02:23 +00:00
|
|
|
}
|
|
|
|
|
2013-08-12 08:31:58 +00:00
|
|
|
already_AddRefed<dom::DOMRequest>
|
|
|
|
BluetoothManager::GetDefaultAdapter(ErrorResult& aRv)
|
2012-05-24 04:02:23 +00:00
|
|
|
{
|
2013-08-12 08:31:58 +00:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win = GetOwner();
|
|
|
|
if (!win) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-07-18 03:41:54 +00:00
|
|
|
|
2013-08-12 08:31:58 +00:00
|
|
|
nsRefPtr<DOMRequest> request = new DOMRequest(win);
|
|
|
|
nsRefPtr<BluetoothReplyRunnable> results =
|
|
|
|
new GetAdapterTask(this, request);
|
2013-08-12 08:31:58 +00:00
|
|
|
|
2013-08-12 16:08:19 +00:00
|
|
|
BluetoothService* bs = BluetoothService::Get();
|
2013-08-12 08:31:58 +00: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-18 03:41:54 +00:00
|
|
|
}
|
2012-11-21 07:33:07 +00:00
|
|
|
|
2013-08-12 08:31:58 +00:00
|
|
|
return request.forget();
|
2012-06-02 18:23:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<BluetoothManager>
|
2012-09-06 14:15:36 +00:00
|
|
|
BluetoothManager::Create(nsPIDOMWindow* aWindow)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aWindow);
|
|
|
|
|
|
|
|
nsRefPtr<BluetoothManager> manager = new BluetoothManager(aWindow);
|
2012-06-02 18:23:16 +00:00
|
|
|
return manager.forget();
|
2012-05-24 04:02:23 +00:00
|
|
|
}
|
|
|
|
|
2012-07-18 03:41:54 +00:00
|
|
|
void
|
|
|
|
BluetoothManager::Notify(const BluetoothSignal& aData)
|
|
|
|
{
|
2013-09-13 22:51:00 +00:00
|
|
|
BT_LOGD("[M] %s: %s", __FUNCTION__, NS_ConvertUTF16toUTF8(aData.name()).get());
|
2013-01-29 10:37:51 +00:00
|
|
|
|
2012-09-03 06:38:44 +00:00
|
|
|
if (aData.name().EqualsLiteral("AdapterAdded")) {
|
2012-09-27 20:11:31 +00:00
|
|
|
DispatchTrustedEvent(NS_LITERAL_STRING("adapteradded"));
|
2013-02-08 10:03:09 +00: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-03 06:38:44 +00:00
|
|
|
} else {
|
2012-07-18 03:41:54 +00:00
|
|
|
#ifdef DEBUG
|
2012-09-03 06:38:44 +00:00
|
|
|
nsCString warningMsg;
|
|
|
|
warningMsg.AssignLiteral("Not handling manager signal: ");
|
|
|
|
warningMsg.Append(NS_ConvertUTF16toUTF8(aData.name()));
|
2013-09-13 22:51:00 +00:00
|
|
|
BT_WARNING(warningMsg.get());
|
2012-07-18 03:41:54 +00:00
|
|
|
#endif
|
2012-09-03 06:38:44 +00:00
|
|
|
}
|
2012-06-02 18:23:16 +00:00
|
|
|
}
|
2012-08-30 06:08:50 +00:00
|
|
|
|
2013-08-12 08:31:58 +00:00
|
|
|
JSObject*
|
2014-04-08 22:27:18 +00:00
|
|
|
BluetoothManager::WrapObject(JSContext* aCx)
|
2013-08-12 08:31:58 +00: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 22:27:17 +00:00
|
|
|
return BluetoothManagerBinding::Wrap(aCx, this);
|
2013-08-12 08:31:58 +00:00
|
|
|
}
|