1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/telephony/ipc/TelephonyParent.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,528 @@ 1.4 +/* -*- Mode: C++ tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.7 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "mozilla/dom/telephony/TelephonyParent.h" 1.10 +#include "nsServiceManagerUtils.h" 1.11 + 1.12 +USING_TELEPHONY_NAMESPACE 1.13 + 1.14 +/******************************************************************************* 1.15 + * TelephonyParent 1.16 + ******************************************************************************/ 1.17 + 1.18 +NS_IMPL_ISUPPORTS(TelephonyParent, nsITelephonyListener) 1.19 + 1.20 +TelephonyParent::TelephonyParent() 1.21 + : mActorDestroyed(false) 1.22 + , mRegistered(false) 1.23 +{ 1.24 +} 1.25 + 1.26 +void 1.27 +TelephonyParent::ActorDestroy(ActorDestroyReason why) 1.28 +{ 1.29 + // The child process could die before this asynchronous notification, in which 1.30 + // case ActorDestroy() was called and mActorDestroyed is set to true. Return 1.31 + // an error here to avoid sending a message to the dead process. 1.32 + mActorDestroyed = true; 1.33 + 1.34 + // Try to unregister listener if we're still registered. 1.35 + RecvUnregisterListener(); 1.36 +} 1.37 + 1.38 +bool 1.39 +TelephonyParent::RecvPTelephonyRequestConstructor(PTelephonyRequestParent* aActor, 1.40 + const IPCTelephonyRequest& aRequest) 1.41 +{ 1.42 + TelephonyRequestParent* actor = static_cast<TelephonyRequestParent*>(aActor); 1.43 + 1.44 + switch (aRequest.type()) { 1.45 + case IPCTelephonyRequest::TEnumerateCallsRequest: 1.46 + return actor->DoRequest(aRequest.get_EnumerateCallsRequest()); 1.47 + case IPCTelephonyRequest::TDialRequest: 1.48 + return actor->DoRequest(aRequest.get_DialRequest()); 1.49 + default: 1.50 + MOZ_CRASH("Unknown type!"); 1.51 + } 1.52 + 1.53 + return false; 1.54 +} 1.55 + 1.56 +PTelephonyRequestParent* 1.57 +TelephonyParent::AllocPTelephonyRequestParent(const IPCTelephonyRequest& aRequest) 1.58 +{ 1.59 + TelephonyRequestParent* actor = new TelephonyRequestParent(); 1.60 + // Add an extra ref for IPDL. Will be released in 1.61 + // TelephonyParent::DeallocPTelephonyRequestParent(). 1.62 + NS_ADDREF(actor); 1.63 + 1.64 + return actor; 1.65 +} 1.66 + 1.67 +bool 1.68 +TelephonyParent::DeallocPTelephonyRequestParent(PTelephonyRequestParent* aActor) 1.69 +{ 1.70 + // TelephonyRequestParent is refcounted, must not be freed manually. 1.71 + static_cast<TelephonyRequestParent*>(aActor)->Release(); 1.72 + return true; 1.73 +} 1.74 + 1.75 +bool 1.76 +TelephonyParent::Recv__delete__() 1.77 +{ 1.78 + return true; // Unregister listener in TelephonyParent::ActorDestroy(). 1.79 +} 1.80 + 1.81 +bool 1.82 +TelephonyParent::RecvRegisterListener() 1.83 +{ 1.84 + NS_ENSURE_TRUE(!mRegistered, true); 1.85 + 1.86 + nsCOMPtr<nsITelephonyProvider> provider = 1.87 + do_GetService(TELEPHONY_PROVIDER_CONTRACTID); 1.88 + NS_ENSURE_TRUE(provider, true); 1.89 + 1.90 + mRegistered = NS_SUCCEEDED(provider->RegisterListener(this)); 1.91 + return true; 1.92 +} 1.93 + 1.94 +bool 1.95 +TelephonyParent::RecvUnregisterListener() 1.96 +{ 1.97 + NS_ENSURE_TRUE(mRegistered, true); 1.98 + 1.99 + nsCOMPtr<nsITelephonyProvider> provider = 1.100 + do_GetService(TELEPHONY_PROVIDER_CONTRACTID); 1.101 + NS_ENSURE_TRUE(provider, true); 1.102 + 1.103 + mRegistered = !NS_SUCCEEDED(provider->UnregisterListener(this)); 1.104 + return true; 1.105 +} 1.106 + 1.107 +bool 1.108 +TelephonyParent::RecvHangUpCall(const uint32_t& aClientId, 1.109 + const uint32_t& aCallIndex) 1.110 +{ 1.111 + nsCOMPtr<nsITelephonyProvider> provider = 1.112 + do_GetService(TELEPHONY_PROVIDER_CONTRACTID); 1.113 + NS_ENSURE_TRUE(provider, true); 1.114 + 1.115 + provider->HangUp(aClientId, aCallIndex); 1.116 + return true; 1.117 +} 1.118 + 1.119 +bool 1.120 +TelephonyParent::RecvAnswerCall(const uint32_t& aClientId, 1.121 + const uint32_t& aCallIndex) 1.122 +{ 1.123 + nsCOMPtr<nsITelephonyProvider> provider = 1.124 + do_GetService(TELEPHONY_PROVIDER_CONTRACTID); 1.125 + NS_ENSURE_TRUE(provider, true); 1.126 + 1.127 + provider->AnswerCall(aClientId, aCallIndex); 1.128 + return true; 1.129 +} 1.130 + 1.131 +bool 1.132 +TelephonyParent::RecvRejectCall(const uint32_t& aClientId, 1.133 + const uint32_t& aCallIndex) 1.134 +{ 1.135 + nsCOMPtr<nsITelephonyProvider> provider = 1.136 + do_GetService(TELEPHONY_PROVIDER_CONTRACTID); 1.137 + NS_ENSURE_TRUE(provider, true); 1.138 + 1.139 + provider->RejectCall(aClientId, aCallIndex); 1.140 + return true; 1.141 +} 1.142 + 1.143 +bool 1.144 +TelephonyParent::RecvHoldCall(const uint32_t& aClientId, 1.145 + const uint32_t& aCallIndex) 1.146 +{ 1.147 + nsCOMPtr<nsITelephonyProvider> provider = 1.148 + do_GetService(TELEPHONY_PROVIDER_CONTRACTID); 1.149 + NS_ENSURE_TRUE(provider, true); 1.150 + 1.151 + provider->HoldCall(aClientId, aCallIndex); 1.152 + return true; 1.153 +} 1.154 + 1.155 +bool 1.156 +TelephonyParent::RecvResumeCall(const uint32_t& aClientId, 1.157 + const uint32_t& aCallIndex) 1.158 +{ 1.159 + nsCOMPtr<nsITelephonyProvider> provider = 1.160 + do_GetService(TELEPHONY_PROVIDER_CONTRACTID); 1.161 + NS_ENSURE_TRUE(provider, true); 1.162 + 1.163 + provider->ResumeCall(aClientId, aCallIndex); 1.164 + return true; 1.165 +} 1.166 + 1.167 +bool 1.168 +TelephonyParent::RecvConferenceCall(const uint32_t& aClientId) 1.169 +{ 1.170 + nsCOMPtr<nsITelephonyProvider> provider = 1.171 + do_GetService(TELEPHONY_PROVIDER_CONTRACTID); 1.172 + NS_ENSURE_TRUE(provider, true); 1.173 + 1.174 + provider->ConferenceCall(aClientId); 1.175 + return true; 1.176 +} 1.177 + 1.178 +bool 1.179 +TelephonyParent::RecvSeparateCall(const uint32_t& aClientId, 1.180 + const uint32_t& aCallIndex) 1.181 +{ 1.182 + nsCOMPtr<nsITelephonyProvider> provider = 1.183 + do_GetService(TELEPHONY_PROVIDER_CONTRACTID); 1.184 + NS_ENSURE_TRUE(provider, true); 1.185 + 1.186 + provider->SeparateCall(aClientId, aCallIndex); 1.187 + return true; 1.188 +} 1.189 + 1.190 +bool 1.191 +TelephonyParent::RecvHoldConference(const uint32_t& aClientId) 1.192 +{ 1.193 + nsCOMPtr<nsITelephonyProvider> provider = 1.194 + do_GetService(TELEPHONY_PROVIDER_CONTRACTID); 1.195 + NS_ENSURE_TRUE(provider, true); 1.196 + 1.197 + provider->HoldConference(aClientId); 1.198 + return true; 1.199 +} 1.200 + 1.201 +bool 1.202 +TelephonyParent::RecvResumeConference(const uint32_t& aClientId) 1.203 +{ 1.204 + nsCOMPtr<nsITelephonyProvider> provider = 1.205 + do_GetService(TELEPHONY_PROVIDER_CONTRACTID); 1.206 + NS_ENSURE_TRUE(provider, true); 1.207 + 1.208 + provider->ResumeConference(aClientId); 1.209 + return true; 1.210 +} 1.211 + 1.212 +bool 1.213 +TelephonyParent::RecvStartTone(const uint32_t& aClientId, const nsString& aTone) 1.214 +{ 1.215 + nsCOMPtr<nsITelephonyProvider> provider = 1.216 + do_GetService(TELEPHONY_PROVIDER_CONTRACTID); 1.217 + NS_ENSURE_TRUE(provider, true); 1.218 + 1.219 + provider->StartTone(aClientId, aTone); 1.220 + return true; 1.221 +} 1.222 + 1.223 +bool 1.224 +TelephonyParent::RecvStopTone(const uint32_t& aClientId) 1.225 +{ 1.226 + nsCOMPtr<nsITelephonyProvider> provider = 1.227 + do_GetService(TELEPHONY_PROVIDER_CONTRACTID); 1.228 + NS_ENSURE_TRUE(provider, true); 1.229 + 1.230 + provider->StopTone(aClientId); 1.231 + return true; 1.232 +} 1.233 + 1.234 +bool 1.235 +TelephonyParent::RecvGetMicrophoneMuted(bool* aMuted) 1.236 +{ 1.237 + *aMuted = false; 1.238 + 1.239 + nsCOMPtr<nsITelephonyProvider> provider = 1.240 + do_GetService(TELEPHONY_PROVIDER_CONTRACTID); 1.241 + NS_ENSURE_TRUE(provider, true); 1.242 + 1.243 + provider->GetMicrophoneMuted(aMuted); 1.244 + return true; 1.245 +} 1.246 + 1.247 +bool 1.248 +TelephonyParent::RecvSetMicrophoneMuted(const bool& aMuted) 1.249 +{ 1.250 + nsCOMPtr<nsITelephonyProvider> provider = 1.251 + do_GetService(TELEPHONY_PROVIDER_CONTRACTID); 1.252 + NS_ENSURE_TRUE(provider, true); 1.253 + 1.254 + provider->SetMicrophoneMuted(aMuted); 1.255 + return true; 1.256 +} 1.257 + 1.258 +bool 1.259 +TelephonyParent::RecvGetSpeakerEnabled(bool* aEnabled) 1.260 +{ 1.261 + *aEnabled = false; 1.262 + 1.263 + nsCOMPtr<nsITelephonyProvider> provider = 1.264 + do_GetService(TELEPHONY_PROVIDER_CONTRACTID); 1.265 + NS_ENSURE_TRUE(provider, true); 1.266 + 1.267 + provider->GetSpeakerEnabled(aEnabled); 1.268 + return true; 1.269 +} 1.270 + 1.271 +bool 1.272 +TelephonyParent::RecvSetSpeakerEnabled(const bool& aEnabled) 1.273 +{ 1.274 + nsCOMPtr<nsITelephonyProvider> provider = 1.275 + do_GetService(TELEPHONY_PROVIDER_CONTRACTID); 1.276 + NS_ENSURE_TRUE(provider, true); 1.277 + 1.278 + provider->SetSpeakerEnabled(aEnabled); 1.279 + return true; 1.280 +} 1.281 + 1.282 +// nsITelephonyListener 1.283 + 1.284 +NS_IMETHODIMP 1.285 +TelephonyParent::CallStateChanged(uint32_t aClientId, 1.286 + uint32_t aCallIndex, 1.287 + uint16_t aCallState, 1.288 + const nsAString& aNumber, 1.289 + bool aIsActive, 1.290 + bool aIsOutgoing, 1.291 + bool aIsEmergency, 1.292 + bool aIsConference, 1.293 + bool aIsSwitchable, 1.294 + bool aIsMergeable) 1.295 +{ 1.296 + NS_ENSURE_TRUE(!mActorDestroyed, NS_ERROR_FAILURE); 1.297 + 1.298 + IPCCallStateData data(aCallIndex, aCallState, nsString(aNumber), 1.299 + aIsActive, aIsOutgoing, aIsEmergency, aIsConference, 1.300 + aIsSwitchable, aIsMergeable); 1.301 + return SendNotifyCallStateChanged(aClientId, data) ? NS_OK : NS_ERROR_FAILURE; 1.302 +} 1.303 + 1.304 +NS_IMETHODIMP 1.305 +TelephonyParent::ConferenceCallStateChanged(uint16_t aCallState) 1.306 +{ 1.307 + NS_ENSURE_TRUE(!mActorDestroyed, NS_ERROR_FAILURE); 1.308 + 1.309 + return SendNotifyConferenceCallStateChanged(aCallState) ? NS_OK 1.310 + : NS_ERROR_FAILURE; 1.311 +} 1.312 + 1.313 +NS_IMETHODIMP 1.314 +TelephonyParent::EnumerateCallStateComplete() 1.315 +{ 1.316 + MOZ_CRASH("Not a EnumerateCalls request!"); 1.317 +} 1.318 + 1.319 +NS_IMETHODIMP 1.320 +TelephonyParent::EnumerateCallState(uint32_t aClientId, 1.321 + uint32_t aCallIndex, 1.322 + uint16_t aCallState, 1.323 + const nsAString& aNumber, 1.324 + bool aIsActive, 1.325 + bool aIsOutgoing, 1.326 + bool aIsEmergency, 1.327 + bool aIsConference, 1.328 + bool aIsSwitchable, 1.329 + bool aIsMergeable) 1.330 +{ 1.331 + MOZ_CRASH("Not a EnumerateCalls request!"); 1.332 +} 1.333 + 1.334 +NS_IMETHODIMP 1.335 +TelephonyParent::NotifyCdmaCallWaiting(uint32_t aClientId, 1.336 + const nsAString& aNumber) 1.337 +{ 1.338 + NS_ENSURE_TRUE(!mActorDestroyed, NS_ERROR_FAILURE); 1.339 + 1.340 + return SendNotifyCdmaCallWaiting(aClientId, nsString(aNumber)) 1.341 + ? NS_OK : NS_ERROR_FAILURE; 1.342 +} 1.343 + 1.344 +NS_IMETHODIMP 1.345 +TelephonyParent::NotifyConferenceError(const nsAString& aName, 1.346 + const nsAString& aMessage) 1.347 +{ 1.348 + NS_ENSURE_TRUE(!mActorDestroyed, NS_ERROR_FAILURE); 1.349 + 1.350 + return SendNotifyConferenceError(nsString(aName), nsString(aMessage)) ? NS_OK 1.351 + : NS_ERROR_FAILURE; 1.352 +} 1.353 + 1.354 +NS_IMETHODIMP 1.355 +TelephonyParent::NotifyError(uint32_t aClientId, 1.356 + int32_t aCallIndex, 1.357 + const nsAString& aError) 1.358 +{ 1.359 + NS_ENSURE_TRUE(!mActorDestroyed, NS_ERROR_FAILURE); 1.360 + 1.361 + return SendNotifyCallError(aClientId, aCallIndex, nsString(aError)) 1.362 + ? NS_OK : NS_ERROR_FAILURE; 1.363 +} 1.364 + 1.365 +NS_IMETHODIMP 1.366 +TelephonyParent::SupplementaryServiceNotification(uint32_t aClientId, 1.367 + int32_t aCallIndex, 1.368 + uint16_t aNotification) 1.369 +{ 1.370 + NS_ENSURE_TRUE(!mActorDestroyed, NS_ERROR_FAILURE); 1.371 + 1.372 + return SendNotifySupplementaryService(aClientId, aCallIndex, aNotification) 1.373 + ? NS_OK : NS_ERROR_FAILURE; 1.374 +} 1.375 + 1.376 +/******************************************************************************* 1.377 + * TelephonyRequestParent 1.378 + ******************************************************************************/ 1.379 + 1.380 +NS_IMPL_ISUPPORTS(TelephonyRequestParent, 1.381 + nsITelephonyListener, 1.382 + nsITelephonyCallback) 1.383 + 1.384 +TelephonyRequestParent::TelephonyRequestParent() 1.385 + : mActorDestroyed(false) 1.386 +{ 1.387 +} 1.388 + 1.389 +void 1.390 +TelephonyRequestParent::ActorDestroy(ActorDestroyReason why) 1.391 +{ 1.392 + // The child process could die before this asynchronous notification, in which 1.393 + // case ActorDestroy() was called and mActorDestroyed is set to true. Return 1.394 + // an error here to avoid sending a message to the dead process. 1.395 + mActorDestroyed = true; 1.396 +} 1.397 + 1.398 +bool 1.399 +TelephonyRequestParent::DoRequest(const EnumerateCallsRequest& aRequest) 1.400 +{ 1.401 + nsresult rv = NS_ERROR_FAILURE; 1.402 + 1.403 + nsCOMPtr<nsITelephonyProvider> provider = 1.404 + do_GetService(TELEPHONY_PROVIDER_CONTRACTID); 1.405 + if (provider) { 1.406 + rv = provider->EnumerateCalls(this); 1.407 + } 1.408 + 1.409 + if (NS_FAILED(rv)) { 1.410 + return NS_SUCCEEDED(EnumerateCallStateComplete()); 1.411 + } 1.412 + 1.413 + return true; 1.414 +} 1.415 + 1.416 +bool 1.417 +TelephonyRequestParent::DoRequest(const DialRequest& aRequest) 1.418 +{ 1.419 + nsCOMPtr<nsITelephonyProvider> provider = 1.420 + do_GetService(TELEPHONY_PROVIDER_CONTRACTID); 1.421 + if (provider) { 1.422 + provider->Dial(aRequest.clientId(), aRequest.number(), 1.423 + aRequest.isEmergency(), this); 1.424 + } else { 1.425 + return NS_SUCCEEDED(NotifyDialError(NS_LITERAL_STRING("InvalidStateError"))); 1.426 + } 1.427 + 1.428 + return true; 1.429 +} 1.430 + 1.431 +// nsITelephonyListener 1.432 + 1.433 +NS_IMETHODIMP 1.434 +TelephonyRequestParent::CallStateChanged(uint32_t aClientId, 1.435 + uint32_t aCallIndex, 1.436 + uint16_t aCallState, 1.437 + const nsAString& aNumber, 1.438 + bool aIsActive, 1.439 + bool aIsOutgoing, 1.440 + bool aIsEmergency, 1.441 + bool aIsConference, 1.442 + bool aIsSwitchable, 1.443 + bool aIsMergeable) 1.444 +{ 1.445 + MOZ_CRASH("Not a TelephonyParent!"); 1.446 +} 1.447 + 1.448 +NS_IMETHODIMP 1.449 +TelephonyRequestParent::ConferenceCallStateChanged(uint16_t aCallState) 1.450 +{ 1.451 + MOZ_CRASH("Not a TelephonyParent!"); 1.452 +} 1.453 + 1.454 +NS_IMETHODIMP 1.455 +TelephonyRequestParent::EnumerateCallStateComplete() 1.456 +{ 1.457 + NS_ENSURE_TRUE(!mActorDestroyed, NS_ERROR_FAILURE); 1.458 + 1.459 + return Send__delete__(this, EnumerateCallsResponse()) ? NS_OK : NS_ERROR_FAILURE; 1.460 +} 1.461 + 1.462 +NS_IMETHODIMP 1.463 +TelephonyRequestParent::EnumerateCallState(uint32_t aClientId, 1.464 + uint32_t aCallIndex, 1.465 + uint16_t aCallState, 1.466 + const nsAString& aNumber, 1.467 + bool aIsActive, 1.468 + bool aIsOutgoing, 1.469 + bool aIsEmergency, 1.470 + bool aIsConference, 1.471 + bool aIsSwitchable, 1.472 + bool aIsMergeable) 1.473 +{ 1.474 + NS_ENSURE_TRUE(!mActorDestroyed, NS_ERROR_FAILURE); 1.475 + 1.476 + IPCCallStateData data(aCallIndex, aCallState, nsString(aNumber), 1.477 + aIsActive, aIsOutgoing, aIsEmergency, aIsConference, 1.478 + aIsSwitchable, aIsMergeable); 1.479 + return SendNotifyEnumerateCallState(aClientId, data) ? NS_OK 1.480 + : NS_ERROR_FAILURE; 1.481 +} 1.482 + 1.483 +NS_IMETHODIMP 1.484 +TelephonyRequestParent::NotifyCdmaCallWaiting(uint32_t aClientId, 1.485 + const nsAString& aNumber) 1.486 +{ 1.487 + MOZ_CRASH("Not a TelephonyParent!"); 1.488 +} 1.489 + 1.490 +NS_IMETHODIMP 1.491 +TelephonyRequestParent::NotifyConferenceError(const nsAString& aName, 1.492 + const nsAString& aMessage) 1.493 +{ 1.494 + MOZ_CRASH("Not a TelephonyParent!"); 1.495 +} 1.496 + 1.497 +NS_IMETHODIMP 1.498 +TelephonyRequestParent::NotifyError(uint32_t aClientId, 1.499 + int32_t aCallIndex, 1.500 + const nsAString& aError) 1.501 +{ 1.502 + MOZ_CRASH("Not a TelephonyParent!"); 1.503 +} 1.504 + 1.505 +NS_IMETHODIMP 1.506 +TelephonyRequestParent::SupplementaryServiceNotification(uint32_t aClientId, 1.507 + int32_t aCallIndex, 1.508 + uint16_t aNotification) 1.509 +{ 1.510 + MOZ_CRASH("Not a TelephonyParent!"); 1.511 +} 1.512 + 1.513 +// nsITelephonyCallback 1.514 + 1.515 +NS_IMETHODIMP 1.516 +TelephonyRequestParent::NotifyDialError(const nsAString& aError) 1.517 +{ 1.518 + NS_ENSURE_TRUE(!mActorDestroyed, NS_ERROR_FAILURE); 1.519 + 1.520 + return (SendNotifyDialError(nsString(aError)) && 1.521 + Send__delete__(this, DialResponse())) ? NS_OK : NS_ERROR_FAILURE; 1.522 +} 1.523 + 1.524 +NS_IMETHODIMP 1.525 +TelephonyRequestParent::NotifyDialSuccess() 1.526 +{ 1.527 + NS_ENSURE_TRUE(!mActorDestroyed, NS_ERROR_FAILURE); 1.528 + 1.529 + return (SendNotifyDialSuccess() && 1.530 + Send__delete__(this, DialResponse())) ? NS_OK : NS_ERROR_FAILURE; 1.531 +}