This repository has been archived by the owner on Apr 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 305
/
BluetoothManager.cpp
320 lines (268 loc) · 8.58 KB
/
BluetoothManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/* -*- 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 "BluetoothTypes.h"
#include "BluetoothReplyRunnable.h"
#include "nsContentUtils.h"
#include "nsIDOMDOMRequest.h"
#include "nsIPermissionManager.h"
#include "nsDOMClassInfo.h"
#include "nsDOMEvent.h"
#include "nsThreadUtils.h"
#include "nsXPCOMCIDInternal.h"
#include "mozilla/LazyIdleThread.h"
#include "mozilla/Util.h"
#include "nsIDOMDOMRequest.h"
using namespace mozilla;
USING_BLUETOOTH_NAMESPACE
DOMCI_DATA(BluetoothManager, BluetoothManager)
NS_IMPL_CYCLE_COLLECTION_CLASS(BluetoothManager)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(BluetoothManager,
nsDOMEventTargetHelper)
NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(enabled)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(BluetoothManager,
nsDOMEventTargetHelper)
NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(enabled)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(BluetoothManager)
NS_INTERFACE_MAP_ENTRY(nsIDOMBluetoothManager)
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(BluetoothManager)
NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
NS_IMPL_ADDREF_INHERITED(BluetoothManager, nsDOMEventTargetHelper)
NS_IMPL_RELEASE_INHERITED(BluetoothManager, nsDOMEventTargetHelper)
class GetAdapterTask : public BluetoothReplyRunnable
{
public:
GetAdapterTask(BluetoothManager* aManager,
nsIDOMDOMRequest* aReq) :
BluetoothReplyRunnable(aReq),
mManagerPtr(aManager)
{
}
bool
ParseSuccessfulReply(jsval* aValue)
{
nsCOMPtr<nsIDOMBluetoothAdapter> adapter;
*aValue = JSVAL_VOID;
const InfallibleTArray<BluetoothNamedValue>& v =
mReply->get_BluetoothReplySuccess().value().get_ArrayOfBluetoothNamedValue();
adapter = BluetoothAdapter::Create(mManagerPtr->GetOwner(), v);
nsresult rv;
nsIScriptContext* sc = mManagerPtr->GetContextForEventHandlers(&rv);
if (!sc) {
NS_WARNING("Cannot create script context!");
SetError(NS_LITERAL_STRING("BluetoothScriptContextError"));
return false;
}
rv = nsContentUtils::WrapNative(sc->GetNativeContext(),
sc->GetNativeGlobal(),
adapter,
aValue);
bool result = NS_SUCCEEDED(rv) ? true : false;
if (!result) {
NS_WARNING("Cannot create native object!");
SetError(NS_LITERAL_STRING("BluetoothNativeObjectError"));
}
return result;
}
void
ReleaseMembers()
{
BluetoothReplyRunnable::ReleaseMembers();
mManagerPtr = nullptr;
}
private:
nsRefPtr<BluetoothManager> mManagerPtr;
};
class ToggleBtResultTask : public BluetoothReplyRunnable
{
public:
ToggleBtResultTask(BluetoothManager* aManager,
nsIDOMDOMRequest* aReq,
bool aEnabled)
: BluetoothReplyRunnable(aReq),
mManagerPtr(aManager),
mEnabled(aEnabled)
{
}
~ToggleBtResultTask()
{
}
bool
ParseSuccessfulReply(jsval* aValue)
{
MOZ_ASSERT(NS_IsMainThread());
*aValue = JSVAL_VOID;
mManagerPtr->SetEnabledInternal(mEnabled);
return true;
}
void
ReleaseMembers()
{
BluetoothReplyRunnable::ReleaseMembers();
// mManagerPtr must be null before returning to prevent the background
// thread from racing to release it during the destruction of this runnable.
mManagerPtr = nullptr;
}
private:
nsRefPtr<BluetoothManager> mManagerPtr;
bool mEnabled;
};
BluetoothManager::BluetoothManager(nsPIDOMWindow *aWindow) :
BluetoothPropertyContainer(BluetoothObjectType::TYPE_MANAGER),
mEnabled(false)
{
BindToOwner(aWindow);
mPath.AssignLiteral("/");
}
BluetoothManager::~BluetoothManager()
{
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!");
}
}
}
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));
NS_WARNING(warningMsg.get());
#endif
}
NS_IMETHODIMP
BluetoothManager::SetEnabled(bool aEnabled, nsIDOMDOMRequest** aDomRequest)
{
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 DOM request!");
return NS_ERROR_FAILURE;
}
nsRefPtr<BluetoothReplyRunnable> results = new ToggleBtResultTask(this, request, aEnabled);
if (aEnabled) {
if (NS_FAILED(bs->Start(results))) {
return NS_ERROR_FAILURE;
}
}
else {
if (NS_FAILED(bs->Stop(results))) {
return NS_ERROR_FAILURE;
}
}
request.forget(aDomRequest);
return NS_OK;
}
NS_IMETHODIMP
BluetoothManager::GetEnabled(bool* aEnabled)
{
*aEnabled = mEnabled;
return NS_OK;
}
NS_IMETHODIMP
BluetoothManager::GetDefaultAdapter(nsIDOMDOMRequest** aAdapter)
{
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 GetAdapterTask(this, request);
if (NS_FAILED(bs->GetDefaultAdapterPathInternal(results))) {
return NS_ERROR_FAILURE;
}
request.forget(aAdapter);
return NS_OK;
}
// static
already_AddRefed<BluetoothManager>
BluetoothManager::Create(nsPIDOMWindow* aWindow) {
nsRefPtr<BluetoothManager> manager = new BluetoothManager(aWindow);
BluetoothService* bs = BluetoothService::Get();
if (!bs) {
NS_WARNING("BluetoothService not available!");
return nullptr;
}
if (NS_FAILED(bs->RegisterBluetoothSignalHandler(NS_LITERAL_STRING("/"), manager))) {
NS_ERROR("Failed to register object with observer!");
return nullptr;
}
return manager.forget();
}
nsresult
NS_NewBluetoothManager(nsPIDOMWindow* aWindow,
nsIDOMBluetoothManager** aBluetoothManager)
{
NS_ASSERTION(aWindow, "Null pointer!");
nsPIDOMWindow* innerWindow = aWindow->IsInnerWindow() ?
aWindow :
aWindow->GetCurrentInnerWindow();
// Need the document for security check.
nsCOMPtr<nsIDocument> document = innerWindow->GetExtantDoc();
NS_ENSURE_TRUE(document, NS_NOINTERFACE);
nsCOMPtr<nsIPrincipal> principal = document->NodePrincipal();
NS_ENSURE_TRUE(principal, NS_ERROR_UNEXPECTED);
nsCOMPtr<nsIPermissionManager> permMgr =
do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
NS_ENSURE_TRUE(permMgr, NS_ERROR_UNEXPECTED);
uint32_t permission;
nsresult rv =
permMgr->TestPermissionFromPrincipal(principal, "mozBluetooth", &permission);
NS_ENSURE_SUCCESS(rv, rv);
if (permission != nsIPermissionManager::ALLOW_ACTION) {
*aBluetoothManager = nullptr;
return NS_OK;
}
nsRefPtr<BluetoothManager> bluetoothManager = BluetoothManager::Create(aWindow);
if (!bluetoothManager) {
NS_ERROR("Cannot create bluetooth manager!");
return NS_ERROR_FAILURE;
}
bluetoothManager.forget(aBluetoothManager);
return NS_OK;
}
void
BluetoothManager::Notify(const BluetoothSignal& aData)
{
#ifdef DEBUG
nsCString warningMsg;
warningMsg.AssignLiteral("Not handling manager signal: ");
warningMsg.Append(NS_ConvertUTF16toUTF8(aData.name()));
NS_WARNING(warningMsg.get());
#endif
}