dom/bluetooth/ipc/BluetoothParent.cpp

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:7cad3b122470
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/. */
6
7 #include "base/basictypes.h"
8
9 #include "BluetoothParent.h"
10
11 #include "mozilla/Assertions.h"
12 #include "mozilla/unused.h"
13 #include "nsDebug.h"
14 #include "nsISupportsImpl.h"
15 #include "nsThreadUtils.h"
16
17 #include "BluetoothReplyRunnable.h"
18 #include "BluetoothService.h"
19
20 using mozilla::unused;
21 USING_BLUETOOTH_NAMESPACE
22
23 /*******************************************************************************
24 * BluetoothRequestParent::ReplyRunnable
25 ******************************************************************************/
26
27 class BluetoothRequestParent::ReplyRunnable : public BluetoothReplyRunnable
28 {
29 BluetoothRequestParent* mRequest;
30
31 public:
32 ReplyRunnable(BluetoothRequestParent* aRequest)
33 : BluetoothReplyRunnable(nullptr), mRequest(aRequest)
34 {
35 MOZ_ASSERT(NS_IsMainThread());
36 MOZ_ASSERT(aRequest);
37 }
38
39 NS_IMETHOD
40 Run() MOZ_OVERRIDE
41 {
42 MOZ_ASSERT(NS_IsMainThread());
43 MOZ_ASSERT(mReply);
44
45 if (mRequest) {
46 // Must do this first because Send__delete__ will delete mRequest.
47 mRequest->RequestComplete();
48
49 if (!mRequest->Send__delete__(mRequest, *mReply)) {
50 BT_WARNING("Failed to send response to child process!");
51 return NS_ERROR_FAILURE;
52 }
53 }
54
55 ReleaseMembers();
56 return NS_OK;
57 }
58
59 void
60 Revoke()
61 {
62 MOZ_ASSERT(NS_IsMainThread());
63 mRequest = nullptr;
64 }
65
66 virtual bool
67 ParseSuccessfulReply(JS::MutableHandle<JS::Value> aValue) MOZ_OVERRIDE
68 {
69 MOZ_CRASH("This should never be called!");
70 }
71 };
72
73 /*******************************************************************************
74 * BluetoothParent
75 ******************************************************************************/
76
77 BluetoothParent::BluetoothParent()
78 : mShutdownState(Running)
79 {
80 MOZ_COUNT_CTOR(BluetoothParent);
81 }
82
83 BluetoothParent::~BluetoothParent()
84 {
85 MOZ_COUNT_DTOR(BluetoothParent);
86 MOZ_ASSERT(!mService);
87 MOZ_ASSERT(mShutdownState == Dead);
88 }
89
90 void
91 BluetoothParent::BeginShutdown()
92 {
93 // Only do something here if we haven't yet begun the shutdown sequence.
94 if (mShutdownState == Running) {
95 unused << SendBeginShutdown();
96 mShutdownState = SentBeginShutdown;
97 }
98 }
99
100 bool
101 BluetoothParent::InitWithService(BluetoothService* aService)
102 {
103 MOZ_ASSERT(aService);
104 MOZ_ASSERT(!mService);
105
106 if (!SendEnabled(aService->IsEnabled())) {
107 return false;
108 }
109
110 mService = aService;
111 return true;
112 }
113
114 void
115 BluetoothParent::UnregisterAllSignalHandlers()
116 {
117 MOZ_ASSERT(mService);
118 mService->UnregisterAllSignalHandlers(this);
119 }
120
121 void
122 BluetoothParent::ActorDestroy(ActorDestroyReason aWhy)
123 {
124 if (mService) {
125 UnregisterAllSignalHandlers();
126 #ifdef DEBUG
127 mService = nullptr;
128 #endif
129 }
130
131 #ifdef DEBUG
132 mShutdownState = Dead;
133 #endif
134 }
135
136 bool
137 BluetoothParent::RecvRegisterSignalHandler(const nsString& aNode)
138 {
139 MOZ_ASSERT(mService);
140 mService->RegisterBluetoothSignalHandler(aNode, this);
141 return true;
142 }
143
144 bool
145 BluetoothParent::RecvUnregisterSignalHandler(const nsString& aNode)
146 {
147 MOZ_ASSERT(mService);
148 mService->UnregisterBluetoothSignalHandler(aNode, this);
149 return true;
150 }
151
152 bool
153 BluetoothParent::RecvStopNotifying()
154 {
155 MOZ_ASSERT(mService);
156
157 if (mShutdownState != Running && mShutdownState != SentBeginShutdown) {
158 MOZ_ASSERT(false, "Bad state!");
159 return false;
160 }
161
162 mShutdownState = ReceivedStopNotifying;
163
164 UnregisterAllSignalHandlers();
165
166 if (SendNotificationsStopped()) {
167 mShutdownState = SentNotificationsStopped;
168 return true;
169 }
170
171 return false;
172 }
173
174 bool
175 BluetoothParent::RecvPBluetoothRequestConstructor(
176 PBluetoothRequestParent* aActor,
177 const Request& aRequest)
178 {
179 BluetoothRequestParent* actor = static_cast<BluetoothRequestParent*>(aActor);
180
181 #ifdef DEBUG
182 actor->mRequestType = aRequest.type();
183 #endif
184
185 switch (aRequest.type()) {
186 case Request::TDefaultAdapterPathRequest:
187 return actor->DoRequest(aRequest.get_DefaultAdapterPathRequest());
188 case Request::TSetPropertyRequest:
189 return actor->DoRequest(aRequest.get_SetPropertyRequest());
190 case Request::TStartDiscoveryRequest:
191 return actor->DoRequest(aRequest.get_StartDiscoveryRequest());
192 case Request::TStopDiscoveryRequest:
193 return actor->DoRequest(aRequest.get_StopDiscoveryRequest());
194 case Request::TPairRequest:
195 return actor->DoRequest(aRequest.get_PairRequest());
196 case Request::TUnpairRequest:
197 return actor->DoRequest(aRequest.get_UnpairRequest());
198 case Request::TPairedDevicePropertiesRequest:
199 return actor->DoRequest(aRequest.get_PairedDevicePropertiesRequest());
200 case Request::TConnectedDevicePropertiesRequest:
201 return actor->DoRequest(aRequest.get_ConnectedDevicePropertiesRequest());
202 case Request::TSetPinCodeRequest:
203 return actor->DoRequest(aRequest.get_SetPinCodeRequest());
204 case Request::TSetPasskeyRequest:
205 return actor->DoRequest(aRequest.get_SetPasskeyRequest());
206 case Request::TConfirmPairingConfirmationRequest:
207 return actor->DoRequest(aRequest.get_ConfirmPairingConfirmationRequest());
208 case Request::TDenyPairingConfirmationRequest:
209 return actor->DoRequest(aRequest.get_DenyPairingConfirmationRequest());
210 case Request::TConnectRequest:
211 return actor->DoRequest(aRequest.get_ConnectRequest());
212 case Request::TDisconnectRequest:
213 return actor->DoRequest(aRequest.get_DisconnectRequest());
214 case Request::TSendFileRequest:
215 return actor->DoRequest(aRequest.get_SendFileRequest());
216 case Request::TStopSendingFileRequest:
217 return actor->DoRequest(aRequest.get_StopSendingFileRequest());
218 case Request::TConfirmReceivingFileRequest:
219 return actor->DoRequest(aRequest.get_ConfirmReceivingFileRequest());
220 case Request::TDenyReceivingFileRequest:
221 return actor->DoRequest(aRequest.get_DenyReceivingFileRequest());
222 case Request::TConnectScoRequest:
223 return actor->DoRequest(aRequest.get_ConnectScoRequest());
224 case Request::TDisconnectScoRequest:
225 return actor->DoRequest(aRequest.get_DisconnectScoRequest());
226 case Request::TIsScoConnectedRequest:
227 return actor->DoRequest(aRequest.get_IsScoConnectedRequest());
228 #ifdef MOZ_B2G_RIL
229 case Request::TAnswerWaitingCallRequest:
230 return actor->DoRequest(aRequest.get_AnswerWaitingCallRequest());
231 case Request::TIgnoreWaitingCallRequest:
232 return actor->DoRequest(aRequest.get_IgnoreWaitingCallRequest());
233 case Request::TToggleCallsRequest:
234 return actor->DoRequest(aRequest.get_ToggleCallsRequest());
235 #endif
236 case Request::TSendMetaDataRequest:
237 return actor->DoRequest(aRequest.get_SendMetaDataRequest());
238 case Request::TSendPlayStatusRequest:
239 return actor->DoRequest(aRequest.get_SendPlayStatusRequest());
240 default:
241 MOZ_CRASH("Unknown type!");
242 }
243
244 MOZ_CRASH("Should never get here!");
245 }
246
247 PBluetoothRequestParent*
248 BluetoothParent::AllocPBluetoothRequestParent(const Request& aRequest)
249 {
250 MOZ_ASSERT(mService);
251 return new BluetoothRequestParent(mService);
252 }
253
254 bool
255 BluetoothParent::DeallocPBluetoothRequestParent(PBluetoothRequestParent* aActor)
256 {
257 delete aActor;
258 return true;
259 }
260
261 void
262 BluetoothParent::Notify(const BluetoothSignal& aSignal)
263 {
264 unused << SendNotify(aSignal);
265 }
266
267 /*******************************************************************************
268 * BluetoothRequestParent
269 ******************************************************************************/
270
271 BluetoothRequestParent::BluetoothRequestParent(BluetoothService* aService)
272 : mService(aService)
273 #ifdef DEBUG
274 , mRequestType(Request::T__None)
275 #endif
276 {
277 MOZ_COUNT_CTOR(BluetoothRequestParent);
278 MOZ_ASSERT(aService);
279
280 mReplyRunnable = new ReplyRunnable(this);
281 }
282
283 BluetoothRequestParent::~BluetoothRequestParent()
284 {
285 MOZ_COUNT_DTOR(BluetoothRequestParent);
286
287 // mReplyRunnable will be automatically revoked.
288 }
289
290 void
291 BluetoothRequestParent::ActorDestroy(ActorDestroyReason aWhy)
292 {
293 mReplyRunnable.Revoke();
294 }
295
296 void
297 BluetoothRequestParent::RequestComplete()
298 {
299 MOZ_ASSERT(NS_IsMainThread());
300 MOZ_ASSERT(mReplyRunnable.IsPending());
301
302 mReplyRunnable.Forget();
303 }
304
305 bool
306 BluetoothRequestParent::DoRequest(const DefaultAdapterPathRequest& aRequest)
307 {
308 MOZ_ASSERT(mService);
309 MOZ_ASSERT(mRequestType == Request::TDefaultAdapterPathRequest);
310
311 nsresult rv = mService->GetDefaultAdapterPathInternal(mReplyRunnable.get());
312 NS_ENSURE_SUCCESS(rv, false);
313
314 return true;
315 }
316
317 bool
318 BluetoothRequestParent::DoRequest(const SetPropertyRequest& aRequest)
319 {
320 MOZ_ASSERT(mService);
321 MOZ_ASSERT(mRequestType == Request::TSetPropertyRequest);
322
323 nsresult rv =
324 mService->SetProperty(aRequest.type(), aRequest.value(),
325 mReplyRunnable.get());
326 NS_ENSURE_SUCCESS(rv, false);
327
328 return true;
329 }
330
331 bool
332 BluetoothRequestParent::DoRequest(const StartDiscoveryRequest& aRequest)
333 {
334 MOZ_ASSERT(mService);
335 MOZ_ASSERT(mRequestType == Request::TStartDiscoveryRequest);
336
337 nsresult rv =
338 mService->StartDiscoveryInternal(mReplyRunnable.get());
339 NS_ENSURE_SUCCESS(rv, false);
340
341 return true;
342 }
343
344 bool
345 BluetoothRequestParent::DoRequest(const StopDiscoveryRequest& aRequest)
346 {
347 MOZ_ASSERT(mService);
348 MOZ_ASSERT(mRequestType == Request::TStopDiscoveryRequest);
349
350 nsresult rv =
351 mService->StopDiscoveryInternal(mReplyRunnable.get());
352 NS_ENSURE_SUCCESS(rv, false);
353
354 return true;
355 }
356
357 bool
358 BluetoothRequestParent::DoRequest(const PairRequest& aRequest)
359 {
360 MOZ_ASSERT(mService);
361 MOZ_ASSERT(mRequestType == Request::TPairRequest);
362
363 nsresult rv =
364 mService->CreatePairedDeviceInternal(aRequest.address(),
365 aRequest.timeoutMS(),
366 mReplyRunnable.get());
367 NS_ENSURE_SUCCESS(rv, false);
368
369 return true;
370 }
371
372 bool
373 BluetoothRequestParent::DoRequest(const UnpairRequest& aRequest)
374 {
375 MOZ_ASSERT(mService);
376 MOZ_ASSERT(mRequestType == Request::TUnpairRequest);
377
378 nsresult rv =
379 mService->RemoveDeviceInternal(aRequest.address(),
380 mReplyRunnable.get());
381 NS_ENSURE_SUCCESS(rv, false);
382
383 return true;
384 }
385
386 bool
387 BluetoothRequestParent::DoRequest(const PairedDevicePropertiesRequest& aRequest)
388 {
389 MOZ_ASSERT(mService);
390 MOZ_ASSERT(mRequestType == Request::TPairedDevicePropertiesRequest);
391
392 nsresult rv =
393 mService->GetPairedDevicePropertiesInternal(aRequest.addresses(),
394 mReplyRunnable.get());
395 NS_ENSURE_SUCCESS(rv, false);
396 return true;
397 }
398
399 bool
400 BluetoothRequestParent::DoRequest(const ConnectedDevicePropertiesRequest& aRequest)
401 {
402 MOZ_ASSERT(mService);
403 MOZ_ASSERT(mRequestType == Request::TConnectedDevicePropertiesRequest);
404 nsresult rv =
405 mService->GetConnectedDevicePropertiesInternal(aRequest.serviceUuid(),
406 mReplyRunnable.get());
407 NS_ENSURE_SUCCESS(rv, false);
408
409 return true;
410 }
411
412 bool
413 BluetoothRequestParent::DoRequest(const SetPinCodeRequest& aRequest)
414 {
415 MOZ_ASSERT(mService);
416 MOZ_ASSERT(mRequestType == Request::TSetPinCodeRequest);
417
418 bool result =
419 mService->SetPinCodeInternal(aRequest.path(),
420 aRequest.pincode(),
421 mReplyRunnable.get());
422
423 NS_ENSURE_TRUE(result, false);
424
425 return true;
426 }
427
428 bool
429 BluetoothRequestParent::DoRequest(const SetPasskeyRequest& aRequest)
430 {
431 MOZ_ASSERT(mService);
432 MOZ_ASSERT(mRequestType == Request::TSetPasskeyRequest);
433
434 bool result =
435 mService->SetPasskeyInternal(aRequest.path(),
436 aRequest.passkey(),
437 mReplyRunnable.get());
438
439 NS_ENSURE_TRUE(result, false);
440
441 return true;
442 }
443
444 bool
445 BluetoothRequestParent::DoRequest(const ConfirmPairingConfirmationRequest&
446 aRequest)
447 {
448 MOZ_ASSERT(mService);
449 MOZ_ASSERT(mRequestType == Request::TConfirmPairingConfirmationRequest);
450
451 bool result =
452 mService->SetPairingConfirmationInternal(aRequest.path(),
453 true,
454 mReplyRunnable.get());
455
456 NS_ENSURE_TRUE(result, false);
457
458 return true;
459 }
460
461 bool
462 BluetoothRequestParent::DoRequest(const DenyPairingConfirmationRequest&
463 aRequest)
464 {
465 MOZ_ASSERT(mService);
466 MOZ_ASSERT(mRequestType == Request::TDenyPairingConfirmationRequest);
467
468 bool result =
469 mService->SetPairingConfirmationInternal(aRequest.path(),
470 false,
471 mReplyRunnable.get());
472
473 NS_ENSURE_TRUE(result, false);
474
475 return true;
476 }
477
478 bool
479 BluetoothRequestParent::DoRequest(const ConnectRequest& aRequest)
480 {
481 MOZ_ASSERT(mService);
482 MOZ_ASSERT(mRequestType == Request::TConnectRequest);
483
484 mService->Connect(aRequest.address(),
485 aRequest.cod(),
486 aRequest.serviceUuid(),
487 mReplyRunnable.get());
488
489 return true;
490 }
491
492 bool
493 BluetoothRequestParent::DoRequest(const DisconnectRequest& aRequest)
494 {
495 MOZ_ASSERT(mService);
496 MOZ_ASSERT(mRequestType == Request::TDisconnectRequest);
497
498 mService->Disconnect(aRequest.address(),
499 aRequest.serviceUuid(),
500 mReplyRunnable.get());
501
502 return true;
503 }
504
505 bool
506 BluetoothRequestParent::DoRequest(const SendFileRequest& aRequest)
507 {
508 MOZ_ASSERT(mService);
509 MOZ_ASSERT(mRequestType == Request::TSendFileRequest);
510
511 mService->SendFile(aRequest.devicePath(),
512 (BlobParent*)aRequest.blobParent(),
513 (BlobChild*)aRequest.blobChild(),
514 mReplyRunnable.get());
515
516 return true;
517 }
518
519 bool
520 BluetoothRequestParent::DoRequest(const StopSendingFileRequest& aRequest)
521 {
522 MOZ_ASSERT(mService);
523 MOZ_ASSERT(mRequestType == Request::TStopSendingFileRequest);
524
525 mService->StopSendingFile(aRequest.devicePath(),
526 mReplyRunnable.get());
527
528 return true;
529 }
530
531 bool
532 BluetoothRequestParent::DoRequest(const ConfirmReceivingFileRequest& aRequest)
533 {
534 MOZ_ASSERT(mService);
535 MOZ_ASSERT(mRequestType == Request::TConfirmReceivingFileRequest);
536
537 mService->ConfirmReceivingFile(aRequest.devicePath(),
538 true,
539 mReplyRunnable.get());
540 return true;
541 }
542
543 bool
544 BluetoothRequestParent::DoRequest(const DenyReceivingFileRequest& aRequest)
545 {
546 MOZ_ASSERT(mService);
547 MOZ_ASSERT(mRequestType == Request::TDenyReceivingFileRequest);
548
549 mService->ConfirmReceivingFile(aRequest.devicePath(),
550 false,
551 mReplyRunnable.get());
552 return true;
553 }
554
555 bool
556 BluetoothRequestParent::DoRequest(const ConnectScoRequest& aRequest)
557 {
558 MOZ_ASSERT(mService);
559 MOZ_ASSERT(mRequestType == Request::TConnectScoRequest);
560
561 mService->ConnectSco(mReplyRunnable.get());
562 return true;
563 }
564
565 bool
566 BluetoothRequestParent::DoRequest(const DisconnectScoRequest& aRequest)
567 {
568 MOZ_ASSERT(mService);
569 MOZ_ASSERT(mRequestType == Request::TDisconnectScoRequest);
570
571 mService->DisconnectSco(mReplyRunnable.get());
572 return true;
573 }
574
575 bool
576 BluetoothRequestParent::DoRequest(const IsScoConnectedRequest& aRequest)
577 {
578 MOZ_ASSERT(mService);
579 MOZ_ASSERT(mRequestType == Request::TIsScoConnectedRequest);
580
581 mService->IsScoConnected(mReplyRunnable.get());
582 return true;
583 }
584
585 #ifdef MOZ_B2G_RIL
586 bool
587 BluetoothRequestParent::DoRequest(const AnswerWaitingCallRequest& aRequest)
588 {
589 MOZ_ASSERT(mService);
590 MOZ_ASSERT(mRequestType == Request::TAnswerWaitingCallRequest);
591
592 mService->AnswerWaitingCall(mReplyRunnable.get());
593
594 return true;
595 }
596
597 bool
598 BluetoothRequestParent::DoRequest(const IgnoreWaitingCallRequest& aRequest)
599 {
600 MOZ_ASSERT(mService);
601 MOZ_ASSERT(mRequestType == Request::TAnswerWaitingCallRequest);
602
603 mService->IgnoreWaitingCall(mReplyRunnable.get());
604
605 return true;
606 }
607
608 bool
609 BluetoothRequestParent::DoRequest(const ToggleCallsRequest& aRequest)
610 {
611 MOZ_ASSERT(mService);
612 MOZ_ASSERT(mRequestType == Request::TAnswerWaitingCallRequest);
613
614 mService->ToggleCalls(mReplyRunnable.get());
615
616 return true;
617 }
618 #endif // MOZ_B2G_RIL
619
620 bool
621 BluetoothRequestParent::DoRequest(const SendMetaDataRequest& aRequest)
622 {
623 MOZ_ASSERT(mService);
624 MOZ_ASSERT(mRequestType == Request::TSendMetaDataRequest);
625
626 mService->SendMetaData(aRequest.title(),
627 aRequest.artist(),
628 aRequest.album(),
629 aRequest.mediaNumber(),
630 aRequest.totalMediaCount(),
631 aRequest.duration(),
632 mReplyRunnable.get());
633 return true;
634 }
635
636 bool
637 BluetoothRequestParent::DoRequest(const SendPlayStatusRequest& aRequest)
638 {
639 MOZ_ASSERT(mService);
640 MOZ_ASSERT(mRequestType == Request::TSendPlayStatusRequest);
641
642 mService->SendPlayStatus(aRequest.duration(),
643 aRequest.position(),
644 aRequest.playStatus(),
645 mReplyRunnable.get());
646 return true;
647 }

mercurial