1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "base/basictypes.h"
8 #include "BluetoothManager.h"
9 #include "BluetoothCommon.h"
10 #include "BluetoothAdapter.h"
11 #include "BluetoothService.h"
12 #include "BluetoothReplyRunnable.h"
14 #include "DOMRequest.h"
15 #include "nsContentUtils.h"
16 #include "nsDOMClassInfo.h"
17 #include "nsIPermissionManager.h"
18 #include "nsThreadUtils.h"
19 #include "mozilla/dom/bluetooth/BluetoothTypes.h"
20 #include "mozilla/dom/BluetoothManagerBinding.h"
21 #include "mozilla/dom/ScriptSettings.h"
22 #include "mozilla/Services.h"
24 using namespace mozilla
;
26 USING_BLUETOOTH_NAMESPACE
28 // QueryInterface implementation for BluetoothManager
29 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(BluetoothManager
)
30 NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper
)
32 NS_IMPL_ADDREF_INHERITED(BluetoothManager
, DOMEventTargetHelper
)
33 NS_IMPL_RELEASE_INHERITED(BluetoothManager
, DOMEventTargetHelper
)
35 class GetAdapterTask
: public BluetoothReplyRunnable
38 GetAdapterTask(BluetoothManager
* aManager
,
39 nsIDOMDOMRequest
* aReq
) :
40 BluetoothReplyRunnable(aReq
),
46 ParseSuccessfulReply(JS::MutableHandle
<JS::Value
> aValue
)
48 aValue
.setUndefined();
50 const BluetoothValue
& v
= mReply
->get_BluetoothReplySuccess().value();
51 if (v
.type() != BluetoothValue::TArrayOfBluetoothNamedValue
) {
52 BT_WARNING("Not a BluetoothNamedValue array!");
53 SetError(NS_LITERAL_STRING("BluetoothReplyTypeError"));
57 if (!mManagerPtr
->GetOwner()) {
58 BT_WARNING("Bluetooth manager was disconnected from owner window.");
60 // Stop to create adapter since owner window of Bluetooth manager was
61 // gone. These is no need to create a DOMEvent target which has no owner
66 const InfallibleTArray
<BluetoothNamedValue
>& values
=
67 v
.get_ArrayOfBluetoothNamedValue();
68 nsRefPtr
<BluetoothAdapter
> adapter
=
69 BluetoothAdapter::Create(mManagerPtr
->GetOwner(), values
);
72 if (!jsapi
.Init(mManagerPtr
->GetOwner())) {
73 BT_WARNING("Failed to initialise AutoJSAPI!");
74 SetError(NS_LITERAL_STRING("BluetoothAutoJSAPIInitError"));
77 JSContext
* cx
= jsapi
.cx();
78 if (NS_FAILED(nsContentUtils::WrapNative(cx
, adapter
, aValue
))) {
79 BT_WARNING("Cannot create native object!");
80 SetError(NS_LITERAL_STRING("BluetoothNativeObjectError"));
90 BluetoothReplyRunnable::ReleaseMembers();
91 mManagerPtr
= nullptr;
95 nsRefPtr
<BluetoothManager
> mManagerPtr
;
98 BluetoothManager::BluetoothManager(nsPIDOMWindow
*aWindow
)
99 : DOMEventTargetHelper(aWindow
)
100 , BluetoothPropertyContainer(BluetoothObjectType::TYPE_MANAGER
)
106 BluetoothService
* bs
= BluetoothService::Get();
107 NS_ENSURE_TRUE_VOID(bs
);
108 bs
->RegisterBluetoothSignalHandler(NS_LITERAL_STRING(KEY_MANAGER
), this);
111 BluetoothManager::~BluetoothManager()
113 BluetoothService
* bs
= BluetoothService::Get();
114 NS_ENSURE_TRUE_VOID(bs
);
115 bs
->UnregisterBluetoothSignalHandler(NS_LITERAL_STRING(KEY_MANAGER
), this);
119 BluetoothManager::DisconnectFromOwner()
121 DOMEventTargetHelper::DisconnectFromOwner();
123 BluetoothService
* bs
= BluetoothService::Get();
124 NS_ENSURE_TRUE_VOID(bs
);
125 bs
->UnregisterBluetoothSignalHandler(NS_LITERAL_STRING(KEY_MANAGER
), this);
129 BluetoothManager::SetPropertyByValue(const BluetoothNamedValue
& aValue
)
132 const nsString
& name
= aValue
.name();
133 nsCString warningMsg
;
134 warningMsg
.AssignLiteral("Not handling manager property: ");
135 warningMsg
.Append(NS_ConvertUTF16toUTF8(name
));
136 BT_WARNING(warningMsg
.get());
141 BluetoothManager::GetEnabled(ErrorResult
& aRv
)
143 BluetoothService
* bs
= BluetoothService::Get();
145 aRv
.Throw(NS_ERROR_FAILURE
);
149 return bs
->IsEnabled();
152 already_AddRefed
<dom::DOMRequest
>
153 BluetoothManager::GetDefaultAdapter(ErrorResult
& aRv
)
155 nsCOMPtr
<nsPIDOMWindow
> win
= GetOwner();
157 aRv
.Throw(NS_ERROR_FAILURE
);
161 nsRefPtr
<DOMRequest
> request
= new DOMRequest(win
);
162 nsRefPtr
<BluetoothReplyRunnable
> results
=
163 new GetAdapterTask(this, request
);
165 BluetoothService
* bs
= BluetoothService::Get();
167 aRv
.Throw(NS_ERROR_FAILURE
);
171 nsresult rv
= bs
->GetDefaultAdapterPathInternal(results
);
177 return request
.forget();
181 already_AddRefed
<BluetoothManager
>
182 BluetoothManager::Create(nsPIDOMWindow
* aWindow
)
184 MOZ_ASSERT(NS_IsMainThread());
187 nsRefPtr
<BluetoothManager
> manager
= new BluetoothManager(aWindow
);
188 return manager
.forget();
192 BluetoothManager::Notify(const BluetoothSignal
& aData
)
194 BT_LOGD("[M] %s: %s", __FUNCTION__
, NS_ConvertUTF16toUTF8(aData
.name()).get());
196 if (aData
.name().EqualsLiteral("AdapterAdded")) {
197 DispatchTrustedEvent(NS_LITERAL_STRING("adapteradded"));
198 } else if (aData
.name().EqualsLiteral("Enabled")) {
199 DispatchTrustedEvent(NS_LITERAL_STRING("enabled"));
200 } else if (aData
.name().EqualsLiteral("Disabled")) {
201 DispatchTrustedEvent(NS_LITERAL_STRING("disabled"));
204 nsCString warningMsg
;
205 warningMsg
.AssignLiteral("Not handling manager signal: ");
206 warningMsg
.Append(NS_ConvertUTF16toUTF8(aData
.name()));
207 BT_WARNING(warningMsg
.get());
213 BluetoothManager::WrapObject(JSContext
* aCx
)
215 return BluetoothManagerBinding::Wrap(aCx
, this);