dom/bluetooth/ipc/BluetoothParent.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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"
     9 #include "BluetoothParent.h"
    11 #include "mozilla/Assertions.h"
    12 #include "mozilla/unused.h"
    13 #include "nsDebug.h"
    14 #include "nsISupportsImpl.h"
    15 #include "nsThreadUtils.h"
    17 #include "BluetoothReplyRunnable.h"
    18 #include "BluetoothService.h"
    20 using mozilla::unused;
    21 USING_BLUETOOTH_NAMESPACE
    23 /*******************************************************************************
    24  * BluetoothRequestParent::ReplyRunnable
    25  ******************************************************************************/
    27 class BluetoothRequestParent::ReplyRunnable : public BluetoothReplyRunnable
    28 {
    29   BluetoothRequestParent* mRequest;
    31 public:
    32   ReplyRunnable(BluetoothRequestParent* aRequest)
    33   : BluetoothReplyRunnable(nullptr), mRequest(aRequest)
    34   {
    35     MOZ_ASSERT(NS_IsMainThread());
    36     MOZ_ASSERT(aRequest);
    37   }
    39   NS_IMETHOD
    40   Run() MOZ_OVERRIDE
    41   {
    42     MOZ_ASSERT(NS_IsMainThread());
    43     MOZ_ASSERT(mReply);
    45     if (mRequest) {
    46       // Must do this first because Send__delete__ will delete mRequest.
    47       mRequest->RequestComplete();
    49       if (!mRequest->Send__delete__(mRequest, *mReply)) {
    50         BT_WARNING("Failed to send response to child process!");
    51         return NS_ERROR_FAILURE;
    52       }
    53     }
    55     ReleaseMembers();
    56     return NS_OK;
    57   }
    59   void
    60   Revoke()
    61   {
    62     MOZ_ASSERT(NS_IsMainThread());
    63     mRequest = nullptr;
    64   }
    66   virtual bool
    67   ParseSuccessfulReply(JS::MutableHandle<JS::Value> aValue) MOZ_OVERRIDE
    68   {
    69     MOZ_CRASH("This should never be called!");
    70   }
    71 };
    73 /*******************************************************************************
    74  * BluetoothParent
    75  ******************************************************************************/
    77 BluetoothParent::BluetoothParent()
    78 : mShutdownState(Running)
    79 {
    80   MOZ_COUNT_CTOR(BluetoothParent);
    81 }
    83 BluetoothParent::~BluetoothParent()
    84 {
    85   MOZ_COUNT_DTOR(BluetoothParent);
    86   MOZ_ASSERT(!mService);
    87   MOZ_ASSERT(mShutdownState == Dead);
    88 }
    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 }
   100 bool
   101 BluetoothParent::InitWithService(BluetoothService* aService)
   102 {
   103   MOZ_ASSERT(aService);
   104   MOZ_ASSERT(!mService);
   106   if (!SendEnabled(aService->IsEnabled())) {
   107     return false;
   108   }
   110   mService = aService;
   111   return true;
   112 }
   114 void
   115 BluetoothParent::UnregisterAllSignalHandlers()
   116 {
   117   MOZ_ASSERT(mService);
   118   mService->UnregisterAllSignalHandlers(this);
   119 }
   121 void
   122 BluetoothParent::ActorDestroy(ActorDestroyReason aWhy)
   123 {
   124   if (mService) {
   125     UnregisterAllSignalHandlers();
   126 #ifdef DEBUG
   127     mService = nullptr;
   128 #endif
   129   }
   131 #ifdef DEBUG
   132   mShutdownState = Dead;
   133 #endif
   134 }
   136 bool
   137 BluetoothParent::RecvRegisterSignalHandler(const nsString& aNode)
   138 {
   139   MOZ_ASSERT(mService);
   140   mService->RegisterBluetoothSignalHandler(aNode, this);
   141   return true;
   142 }
   144 bool
   145 BluetoothParent::RecvUnregisterSignalHandler(const nsString& aNode)
   146 {
   147   MOZ_ASSERT(mService);
   148   mService->UnregisterBluetoothSignalHandler(aNode, this);
   149   return true;
   150 }
   152 bool
   153 BluetoothParent::RecvStopNotifying()
   154 {
   155   MOZ_ASSERT(mService);
   157   if (mShutdownState != Running && mShutdownState != SentBeginShutdown) {
   158     MOZ_ASSERT(false, "Bad state!");
   159     return false;
   160   }
   162   mShutdownState = ReceivedStopNotifying;
   164   UnregisterAllSignalHandlers();
   166   if (SendNotificationsStopped()) {
   167     mShutdownState = SentNotificationsStopped;
   168     return true;
   169   }
   171   return false;
   172 }
   174 bool
   175 BluetoothParent::RecvPBluetoothRequestConstructor(
   176                                                 PBluetoothRequestParent* aActor,
   177                                                 const Request& aRequest)
   178 {
   179   BluetoothRequestParent* actor = static_cast<BluetoothRequestParent*>(aActor);
   181 #ifdef DEBUG
   182   actor->mRequestType = aRequest.type();
   183 #endif
   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   }
   244   MOZ_CRASH("Should never get here!");
   245 }
   247 PBluetoothRequestParent*
   248 BluetoothParent::AllocPBluetoothRequestParent(const Request& aRequest)
   249 {
   250   MOZ_ASSERT(mService);
   251   return new BluetoothRequestParent(mService);
   252 }
   254 bool
   255 BluetoothParent::DeallocPBluetoothRequestParent(PBluetoothRequestParent* aActor)
   256 {
   257   delete aActor;
   258   return true;
   259 }
   261 void
   262 BluetoothParent::Notify(const BluetoothSignal& aSignal)
   263 {
   264   unused << SendNotify(aSignal);
   265 }
   267 /*******************************************************************************
   268  * BluetoothRequestParent
   269  ******************************************************************************/
   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);
   280   mReplyRunnable = new ReplyRunnable(this);
   281 }
   283 BluetoothRequestParent::~BluetoothRequestParent()
   284 {
   285   MOZ_COUNT_DTOR(BluetoothRequestParent);
   287   // mReplyRunnable will be automatically revoked.
   288 }
   290 void
   291 BluetoothRequestParent::ActorDestroy(ActorDestroyReason aWhy)
   292 {
   293   mReplyRunnable.Revoke();
   294 }
   296 void
   297 BluetoothRequestParent::RequestComplete()
   298 {
   299   MOZ_ASSERT(NS_IsMainThread());
   300   MOZ_ASSERT(mReplyRunnable.IsPending());
   302   mReplyRunnable.Forget();
   303 }
   305 bool
   306 BluetoothRequestParent::DoRequest(const DefaultAdapterPathRequest& aRequest)
   307 {
   308   MOZ_ASSERT(mService);
   309   MOZ_ASSERT(mRequestType == Request::TDefaultAdapterPathRequest);
   311   nsresult rv = mService->GetDefaultAdapterPathInternal(mReplyRunnable.get());
   312   NS_ENSURE_SUCCESS(rv, false);
   314   return true;
   315 }
   317 bool
   318 BluetoothRequestParent::DoRequest(const SetPropertyRequest& aRequest)
   319 {
   320   MOZ_ASSERT(mService);
   321   MOZ_ASSERT(mRequestType == Request::TSetPropertyRequest);
   323   nsresult rv =
   324     mService->SetProperty(aRequest.type(), aRequest.value(),
   325                           mReplyRunnable.get());
   326   NS_ENSURE_SUCCESS(rv, false);
   328   return true;
   329 }
   331 bool
   332 BluetoothRequestParent::DoRequest(const StartDiscoveryRequest& aRequest)
   333 {
   334   MOZ_ASSERT(mService);
   335   MOZ_ASSERT(mRequestType == Request::TStartDiscoveryRequest);
   337   nsresult rv =
   338     mService->StartDiscoveryInternal(mReplyRunnable.get());
   339   NS_ENSURE_SUCCESS(rv, false);
   341   return true;
   342 }
   344 bool
   345 BluetoothRequestParent::DoRequest(const StopDiscoveryRequest& aRequest)
   346 {
   347   MOZ_ASSERT(mService);
   348   MOZ_ASSERT(mRequestType == Request::TStopDiscoveryRequest);
   350   nsresult rv =
   351     mService->StopDiscoveryInternal(mReplyRunnable.get());
   352   NS_ENSURE_SUCCESS(rv, false);
   354   return true;
   355 }
   357 bool
   358 BluetoothRequestParent::DoRequest(const PairRequest& aRequest)
   359 {
   360   MOZ_ASSERT(mService);
   361   MOZ_ASSERT(mRequestType == Request::TPairRequest);
   363   nsresult rv =
   364     mService->CreatePairedDeviceInternal(aRequest.address(),
   365                                          aRequest.timeoutMS(),
   366                                          mReplyRunnable.get());
   367   NS_ENSURE_SUCCESS(rv, false);
   369   return true;
   370 }
   372 bool
   373 BluetoothRequestParent::DoRequest(const UnpairRequest& aRequest)
   374 {
   375   MOZ_ASSERT(mService);
   376   MOZ_ASSERT(mRequestType == Request::TUnpairRequest);
   378   nsresult rv =
   379     mService->RemoveDeviceInternal(aRequest.address(),
   380                                    mReplyRunnable.get());
   381   NS_ENSURE_SUCCESS(rv, false);
   383   return true;
   384 }
   386 bool
   387 BluetoothRequestParent::DoRequest(const PairedDevicePropertiesRequest& aRequest)
   388 {
   389   MOZ_ASSERT(mService);
   390   MOZ_ASSERT(mRequestType == Request::TPairedDevicePropertiesRequest);
   392   nsresult rv =
   393     mService->GetPairedDevicePropertiesInternal(aRequest.addresses(),
   394                                                 mReplyRunnable.get());
   395   NS_ENSURE_SUCCESS(rv, false);
   396   return true;
   397 }
   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);
   409   return true;
   410 }
   412 bool
   413 BluetoothRequestParent::DoRequest(const SetPinCodeRequest& aRequest)
   414 {
   415   MOZ_ASSERT(mService);
   416   MOZ_ASSERT(mRequestType == Request::TSetPinCodeRequest);
   418   bool result =
   419     mService->SetPinCodeInternal(aRequest.path(),
   420                                  aRequest.pincode(),
   421                                  mReplyRunnable.get());
   423   NS_ENSURE_TRUE(result, false);
   425   return true;
   426 }
   428 bool
   429 BluetoothRequestParent::DoRequest(const SetPasskeyRequest& aRequest)
   430 {
   431   MOZ_ASSERT(mService);
   432   MOZ_ASSERT(mRequestType == Request::TSetPasskeyRequest);
   434   bool result =
   435     mService->SetPasskeyInternal(aRequest.path(),
   436                                  aRequest.passkey(),
   437                                  mReplyRunnable.get());
   439   NS_ENSURE_TRUE(result, false);
   441   return true;
   442 }
   444 bool
   445 BluetoothRequestParent::DoRequest(const ConfirmPairingConfirmationRequest&
   446                                   aRequest)
   447 {
   448   MOZ_ASSERT(mService);
   449   MOZ_ASSERT(mRequestType == Request::TConfirmPairingConfirmationRequest);
   451   bool result =
   452     mService->SetPairingConfirmationInternal(aRequest.path(),
   453                                              true,
   454                                              mReplyRunnable.get());
   456   NS_ENSURE_TRUE(result, false);
   458   return true;
   459 }
   461 bool
   462 BluetoothRequestParent::DoRequest(const DenyPairingConfirmationRequest&
   463                                   aRequest)
   464 {
   465   MOZ_ASSERT(mService);
   466   MOZ_ASSERT(mRequestType == Request::TDenyPairingConfirmationRequest);
   468   bool result =
   469     mService->SetPairingConfirmationInternal(aRequest.path(),
   470                                              false,
   471                                              mReplyRunnable.get());
   473   NS_ENSURE_TRUE(result, false);
   475   return true;
   476 }
   478 bool
   479 BluetoothRequestParent::DoRequest(const ConnectRequest& aRequest)
   480 {
   481   MOZ_ASSERT(mService);
   482   MOZ_ASSERT(mRequestType == Request::TConnectRequest);
   484   mService->Connect(aRequest.address(),
   485                     aRequest.cod(),
   486                     aRequest.serviceUuid(),
   487                     mReplyRunnable.get());
   489   return true;
   490 }
   492 bool
   493 BluetoothRequestParent::DoRequest(const DisconnectRequest& aRequest)
   494 {
   495   MOZ_ASSERT(mService);
   496   MOZ_ASSERT(mRequestType == Request::TDisconnectRequest);
   498   mService->Disconnect(aRequest.address(),
   499                        aRequest.serviceUuid(),
   500                        mReplyRunnable.get());
   502   return true;
   503 }
   505 bool
   506 BluetoothRequestParent::DoRequest(const SendFileRequest& aRequest)
   507 {
   508   MOZ_ASSERT(mService);
   509   MOZ_ASSERT(mRequestType == Request::TSendFileRequest);
   511   mService->SendFile(aRequest.devicePath(),
   512                      (BlobParent*)aRequest.blobParent(),
   513                      (BlobChild*)aRequest.blobChild(),
   514                      mReplyRunnable.get());
   516   return true;
   517 }
   519 bool
   520 BluetoothRequestParent::DoRequest(const StopSendingFileRequest& aRequest)
   521 {
   522   MOZ_ASSERT(mService);
   523   MOZ_ASSERT(mRequestType == Request::TStopSendingFileRequest);
   525   mService->StopSendingFile(aRequest.devicePath(),
   526                             mReplyRunnable.get());
   528   return true;
   529 }
   531 bool
   532 BluetoothRequestParent::DoRequest(const ConfirmReceivingFileRequest& aRequest)
   533 {
   534   MOZ_ASSERT(mService);
   535   MOZ_ASSERT(mRequestType == Request::TConfirmReceivingFileRequest);
   537   mService->ConfirmReceivingFile(aRequest.devicePath(),
   538                                  true,
   539                                  mReplyRunnable.get());
   540   return true;
   541 }
   543 bool
   544 BluetoothRequestParent::DoRequest(const DenyReceivingFileRequest& aRequest)
   545 {
   546   MOZ_ASSERT(mService);
   547   MOZ_ASSERT(mRequestType == Request::TDenyReceivingFileRequest);
   549   mService->ConfirmReceivingFile(aRequest.devicePath(),
   550                                  false,
   551                                  mReplyRunnable.get());
   552   return true;
   553 }
   555 bool
   556 BluetoothRequestParent::DoRequest(const ConnectScoRequest& aRequest)
   557 {
   558   MOZ_ASSERT(mService);
   559   MOZ_ASSERT(mRequestType == Request::TConnectScoRequest);
   561   mService->ConnectSco(mReplyRunnable.get());
   562   return true;
   563 }
   565 bool
   566 BluetoothRequestParent::DoRequest(const DisconnectScoRequest& aRequest)
   567 {
   568   MOZ_ASSERT(mService);
   569   MOZ_ASSERT(mRequestType == Request::TDisconnectScoRequest);
   571   mService->DisconnectSco(mReplyRunnable.get());
   572   return true;
   573 }
   575 bool
   576 BluetoothRequestParent::DoRequest(const IsScoConnectedRequest& aRequest)
   577 {
   578   MOZ_ASSERT(mService);
   579   MOZ_ASSERT(mRequestType == Request::TIsScoConnectedRequest);
   581   mService->IsScoConnected(mReplyRunnable.get());
   582   return true;
   583 }
   585 #ifdef MOZ_B2G_RIL
   586 bool
   587 BluetoothRequestParent::DoRequest(const AnswerWaitingCallRequest& aRequest)
   588 {
   589   MOZ_ASSERT(mService);
   590   MOZ_ASSERT(mRequestType == Request::TAnswerWaitingCallRequest);
   592   mService->AnswerWaitingCall(mReplyRunnable.get());
   594   return true;
   595 }
   597 bool
   598 BluetoothRequestParent::DoRequest(const IgnoreWaitingCallRequest& aRequest)
   599 {
   600   MOZ_ASSERT(mService);
   601   MOZ_ASSERT(mRequestType == Request::TAnswerWaitingCallRequest);
   603   mService->IgnoreWaitingCall(mReplyRunnable.get());
   605   return true;
   606 }
   608 bool
   609 BluetoothRequestParent::DoRequest(const ToggleCallsRequest& aRequest)
   610 {
   611   MOZ_ASSERT(mService);
   612   MOZ_ASSERT(mRequestType == Request::TAnswerWaitingCallRequest);
   614   mService->ToggleCalls(mReplyRunnable.get());
   616   return true;
   617 }
   618 #endif // MOZ_B2G_RIL
   620 bool
   621 BluetoothRequestParent::DoRequest(const SendMetaDataRequest& aRequest)
   622 {
   623   MOZ_ASSERT(mService);
   624   MOZ_ASSERT(mRequestType == Request::TSendMetaDataRequest);
   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 }
   636 bool
   637 BluetoothRequestParent::DoRequest(const SendPlayStatusRequest& aRequest)
   638 {
   639   MOZ_ASSERT(mService);
   640   MOZ_ASSERT(mRequestType == Request::TSendPlayStatusRequest);
   642   mService->SendPlayStatus(aRequest.duration(),
   643                            aRequest.position(),
   644                            aRequest.playStatus(),
   645                            mReplyRunnable.get());
   646   return true;
   647 }

mercurial