Bumping manifests a=b2g-bump
[gecko.git] / dom / telephony / ipc / TelephonyParent.cpp
blobd6dd6e34f3874c94ce4abe896ff5cc73d94fe9a3
1 /* -*- Mode: C++ tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "mozilla/dom/telephony/TelephonyParent.h"
7 #include "nsServiceManagerUtils.h"
9 USING_TELEPHONY_NAMESPACE
11 /*******************************************************************************
12 * TelephonyParent
13 ******************************************************************************/
15 NS_IMPL_ISUPPORTS(TelephonyParent, nsITelephonyListener)
17 TelephonyParent::TelephonyParent()
18 : mActorDestroyed(false)
19 , mRegistered(false)
23 void
24 TelephonyParent::ActorDestroy(ActorDestroyReason why)
26 // The child process could die before this asynchronous notification, in which
27 // case ActorDestroy() was called and mActorDestroyed is set to true. Return
28 // an error here to avoid sending a message to the dead process.
29 mActorDestroyed = true;
31 // Try to unregister listener if we're still registered.
32 RecvUnregisterListener();
35 bool
36 TelephonyParent::RecvPTelephonyRequestConstructor(PTelephonyRequestParent* aActor,
37 const IPCTelephonyRequest& aRequest)
39 TelephonyRequestParent* actor = static_cast<TelephonyRequestParent*>(aActor);
41 switch (aRequest.type()) {
42 case IPCTelephonyRequest::TEnumerateCallsRequest:
43 return actor->DoRequest(aRequest.get_EnumerateCallsRequest());
44 case IPCTelephonyRequest::TDialRequest:
45 return actor->DoRequest(aRequest.get_DialRequest());
46 case IPCTelephonyRequest::THangUpConferenceRequest:
47 return actor->DoRequest(aRequest.get_HangUpConferenceRequest());
48 default:
49 MOZ_CRASH("Unknown type!");
52 return false;
55 PTelephonyRequestParent*
56 TelephonyParent::AllocPTelephonyRequestParent(const IPCTelephonyRequest& aRequest)
58 TelephonyRequestParent* actor = new TelephonyRequestParent();
59 // Add an extra ref for IPDL. Will be released in
60 // TelephonyParent::DeallocPTelephonyRequestParent().
61 NS_ADDREF(actor);
63 return actor;
66 bool
67 TelephonyParent::DeallocPTelephonyRequestParent(PTelephonyRequestParent* aActor)
69 // TelephonyRequestParent is refcounted, must not be freed manually.
70 static_cast<TelephonyRequestParent*>(aActor)->Release();
71 return true;
74 bool
75 TelephonyParent::Recv__delete__()
77 return true; // Unregister listener in TelephonyParent::ActorDestroy().
80 bool
81 TelephonyParent::RecvRegisterListener()
83 NS_ENSURE_TRUE(!mRegistered, true);
85 nsCOMPtr<nsITelephonyService> service =
86 do_GetService(TELEPHONY_SERVICE_CONTRACTID);
87 NS_ENSURE_TRUE(service, true);
89 mRegistered = NS_SUCCEEDED(service->RegisterListener(this));
90 return true;
93 bool
94 TelephonyParent::RecvUnregisterListener()
96 NS_ENSURE_TRUE(mRegistered, true);
98 nsCOMPtr<nsITelephonyService> service =
99 do_GetService(TELEPHONY_SERVICE_CONTRACTID);
100 NS_ENSURE_TRUE(service, true);
102 mRegistered = !NS_SUCCEEDED(service->UnregisterListener(this));
103 return true;
106 bool
107 TelephonyParent::RecvHangUpCall(const uint32_t& aClientId,
108 const uint32_t& aCallIndex)
110 nsCOMPtr<nsITelephonyService> service =
111 do_GetService(TELEPHONY_SERVICE_CONTRACTID);
112 NS_ENSURE_TRUE(service, true);
114 service->HangUp(aClientId, aCallIndex);
115 return true;
118 bool
119 TelephonyParent::RecvAnswerCall(const uint32_t& aClientId,
120 const uint32_t& aCallIndex)
122 nsCOMPtr<nsITelephonyService> service =
123 do_GetService(TELEPHONY_SERVICE_CONTRACTID);
124 NS_ENSURE_TRUE(service, true);
126 service->AnswerCall(aClientId, aCallIndex);
127 return true;
130 bool
131 TelephonyParent::RecvRejectCall(const uint32_t& aClientId,
132 const uint32_t& aCallIndex)
134 nsCOMPtr<nsITelephonyService> service =
135 do_GetService(TELEPHONY_SERVICE_CONTRACTID);
136 NS_ENSURE_TRUE(service, true);
138 service->RejectCall(aClientId, aCallIndex);
139 return true;
142 bool
143 TelephonyParent::RecvHoldCall(const uint32_t& aClientId,
144 const uint32_t& aCallIndex)
146 nsCOMPtr<nsITelephonyService> service =
147 do_GetService(TELEPHONY_SERVICE_CONTRACTID);
148 NS_ENSURE_TRUE(service, true);
150 service->HoldCall(aClientId, aCallIndex);
151 return true;
154 bool
155 TelephonyParent::RecvResumeCall(const uint32_t& aClientId,
156 const uint32_t& aCallIndex)
158 nsCOMPtr<nsITelephonyService> service =
159 do_GetService(TELEPHONY_SERVICE_CONTRACTID);
160 NS_ENSURE_TRUE(service, true);
162 service->ResumeCall(aClientId, aCallIndex);
163 return true;
166 bool
167 TelephonyParent::RecvConferenceCall(const uint32_t& aClientId)
169 nsCOMPtr<nsITelephonyService> service =
170 do_GetService(TELEPHONY_SERVICE_CONTRACTID);
171 NS_ENSURE_TRUE(service, true);
173 service->ConferenceCall(aClientId);
174 return true;
177 bool
178 TelephonyParent::RecvSeparateCall(const uint32_t& aClientId,
179 const uint32_t& aCallIndex)
181 nsCOMPtr<nsITelephonyService> service =
182 do_GetService(TELEPHONY_SERVICE_CONTRACTID);
183 NS_ENSURE_TRUE(service, true);
185 service->SeparateCall(aClientId, aCallIndex);
186 return true;
189 bool
190 TelephonyParent::RecvHoldConference(const uint32_t& aClientId)
192 nsCOMPtr<nsITelephonyService> service =
193 do_GetService(TELEPHONY_SERVICE_CONTRACTID);
194 NS_ENSURE_TRUE(service, true);
196 service->HoldConference(aClientId);
197 return true;
200 bool
201 TelephonyParent::RecvResumeConference(const uint32_t& aClientId)
203 nsCOMPtr<nsITelephonyService> service =
204 do_GetService(TELEPHONY_SERVICE_CONTRACTID);
205 NS_ENSURE_TRUE(service, true);
207 service->ResumeConference(aClientId);
208 return true;
211 bool
212 TelephonyParent::RecvStartTone(const uint32_t& aClientId, const nsString& aTone)
214 nsCOMPtr<nsITelephonyService> service =
215 do_GetService(TELEPHONY_SERVICE_CONTRACTID);
216 NS_ENSURE_TRUE(service, true);
218 service->StartTone(aClientId, aTone);
219 return true;
222 bool
223 TelephonyParent::RecvStopTone(const uint32_t& aClientId)
225 nsCOMPtr<nsITelephonyService> service =
226 do_GetService(TELEPHONY_SERVICE_CONTRACTID);
227 NS_ENSURE_TRUE(service, true);
229 service->StopTone(aClientId);
230 return true;
233 bool
234 TelephonyParent::RecvGetMicrophoneMuted(bool* aMuted)
236 *aMuted = false;
238 nsCOMPtr<nsITelephonyService> service =
239 do_GetService(TELEPHONY_SERVICE_CONTRACTID);
240 NS_ENSURE_TRUE(service, true);
242 service->GetMicrophoneMuted(aMuted);
243 return true;
246 bool
247 TelephonyParent::RecvSetMicrophoneMuted(const bool& aMuted)
249 nsCOMPtr<nsITelephonyService> service =
250 do_GetService(TELEPHONY_SERVICE_CONTRACTID);
251 NS_ENSURE_TRUE(service, true);
253 service->SetMicrophoneMuted(aMuted);
254 return true;
257 bool
258 TelephonyParent::RecvGetSpeakerEnabled(bool* aEnabled)
260 *aEnabled = false;
262 nsCOMPtr<nsITelephonyService> service =
263 do_GetService(TELEPHONY_SERVICE_CONTRACTID);
264 NS_ENSURE_TRUE(service, true);
266 service->GetSpeakerEnabled(aEnabled);
267 return true;
270 bool
271 TelephonyParent::RecvSetSpeakerEnabled(const bool& aEnabled)
273 nsCOMPtr<nsITelephonyService> service =
274 do_GetService(TELEPHONY_SERVICE_CONTRACTID);
275 NS_ENSURE_TRUE(service, true);
277 service->SetSpeakerEnabled(aEnabled);
278 return true;
281 // nsITelephonyListener
283 NS_IMETHODIMP
284 TelephonyParent::CallStateChanged(uint32_t aClientId,
285 uint32_t aCallIndex,
286 uint16_t aCallState,
287 const nsAString& aNumber,
288 uint16_t aNumberPresentation,
289 const nsAString& aName,
290 uint16_t aNamePresentation,
291 bool aIsOutgoing,
292 bool aIsEmergency,
293 bool aIsConference,
294 bool aIsSwitchable,
295 bool aIsMergeable)
297 NS_ENSURE_TRUE(!mActorDestroyed, NS_ERROR_FAILURE);
299 IPCCallStateData data(aCallIndex, aCallState, nsString(aNumber),
300 aNumberPresentation, nsString(aName), aNamePresentation,
301 aIsOutgoing, aIsEmergency, aIsConference,
302 aIsSwitchable, aIsMergeable);
303 return SendNotifyCallStateChanged(aClientId, data) ? NS_OK : NS_ERROR_FAILURE;
306 NS_IMETHODIMP
307 TelephonyParent::ConferenceCallStateChanged(uint16_t aCallState)
309 NS_ENSURE_TRUE(!mActorDestroyed, NS_ERROR_FAILURE);
311 return SendNotifyConferenceCallStateChanged(aCallState) ? NS_OK
312 : NS_ERROR_FAILURE;
315 NS_IMETHODIMP
316 TelephonyParent::EnumerateCallStateComplete()
318 MOZ_CRASH("Not a EnumerateCalls request!");
321 NS_IMETHODIMP
322 TelephonyParent::EnumerateCallState(uint32_t aClientId,
323 uint32_t aCallIndex,
324 uint16_t aCallState,
325 const nsAString& aNumber,
326 uint16_t aNumberPresentation,
327 const nsAString& aName,
328 uint16_t aNamePresentation,
329 bool aIsOutgoing,
330 bool aIsEmergency,
331 bool aIsConference,
332 bool aIsSwitchable,
333 bool aIsMergeable)
335 MOZ_CRASH("Not a EnumerateCalls request!");
338 NS_IMETHODIMP
339 TelephonyParent::NotifyCdmaCallWaiting(uint32_t aClientId,
340 const nsAString& aNumber,
341 uint16_t aNumberPresentation,
342 const nsAString& aName,
343 uint16_t aNamePresentation)
345 NS_ENSURE_TRUE(!mActorDestroyed, NS_ERROR_FAILURE);
347 IPCCdmaWaitingCallData data(nsString(aNumber), aNumberPresentation,
348 nsString(aName), aNamePresentation);
349 return SendNotifyCdmaCallWaiting(aClientId, data) ? NS_OK : NS_ERROR_FAILURE;
352 NS_IMETHODIMP
353 TelephonyParent::NotifyConferenceError(const nsAString& aName,
354 const nsAString& aMessage)
356 NS_ENSURE_TRUE(!mActorDestroyed, NS_ERROR_FAILURE);
358 return SendNotifyConferenceError(nsString(aName), nsString(aMessage)) ? NS_OK
359 : NS_ERROR_FAILURE;
362 NS_IMETHODIMP
363 TelephonyParent::NotifyError(uint32_t aClientId,
364 int32_t aCallIndex,
365 const nsAString& aError)
367 NS_ENSURE_TRUE(!mActorDestroyed, NS_ERROR_FAILURE);
369 return SendNotifyCallError(aClientId, aCallIndex, nsString(aError))
370 ? NS_OK : NS_ERROR_FAILURE;
373 NS_IMETHODIMP
374 TelephonyParent::SupplementaryServiceNotification(uint32_t aClientId,
375 int32_t aCallIndex,
376 uint16_t aNotification)
378 NS_ENSURE_TRUE(!mActorDestroyed, NS_ERROR_FAILURE);
380 return SendNotifySupplementaryService(aClientId, aCallIndex, aNotification)
381 ? NS_OK : NS_ERROR_FAILURE;
384 /*******************************************************************************
385 * TelephonyRequestParent
386 ******************************************************************************/
388 NS_IMPL_ISUPPORTS(TelephonyRequestParent,
389 nsITelephonyListener,
390 nsITelephonyCallback,
391 nsITelephonyDialCallback)
393 TelephonyRequestParent::TelephonyRequestParent()
394 : mActorDestroyed(false)
398 void
399 TelephonyRequestParent::ActorDestroy(ActorDestroyReason why)
401 // The child process could die before this asynchronous notification, in which
402 // case ActorDestroy() was called and mActorDestroyed is set to true. Return
403 // an error here to avoid sending a message to the dead process.
404 mActorDestroyed = true;
407 bool
408 TelephonyRequestParent::DoRequest(const EnumerateCallsRequest& aRequest)
410 nsresult rv = NS_ERROR_FAILURE;
412 nsCOMPtr<nsITelephonyService> service =
413 do_GetService(TELEPHONY_SERVICE_CONTRACTID);
414 if (service) {
415 rv = service->EnumerateCalls(this);
418 if (NS_FAILED(rv)) {
419 return NS_SUCCEEDED(EnumerateCallStateComplete());
422 return true;
425 bool
426 TelephonyRequestParent::DoRequest(const DialRequest& aRequest)
428 nsCOMPtr<nsITelephonyService> service =
429 do_GetService(TELEPHONY_SERVICE_CONTRACTID);
430 if (service) {
431 service->Dial(aRequest.clientId(), aRequest.number(),
432 aRequest.isEmergency(), this);
433 } else {
434 return NS_SUCCEEDED(NotifyError(NS_LITERAL_STRING("InvalidStateError")));
437 return true;
440 bool
441 TelephonyRequestParent::DoRequest(const HangUpConferenceRequest& aRequest)
443 nsCOMPtr<nsITelephonyService> service =
444 do_GetService(TELEPHONY_SERVICE_CONTRACTID);
445 if (service) {
446 service->HangUpConference(aRequest.clientId(), this);
447 } else {
448 return NS_SUCCEEDED(NotifyError(NS_LITERAL_STRING("InvalidStateError")));
451 return true;
454 nsresult
455 TelephonyRequestParent::SendResponse(const IPCTelephonyResponse& aResponse)
457 NS_ENSURE_TRUE(!mActorDestroyed, NS_ERROR_FAILURE);
459 return Send__delete__(this, aResponse) ? NS_OK : NS_ERROR_FAILURE;
462 // nsITelephonyListener
464 NS_IMETHODIMP
465 TelephonyRequestParent::CallStateChanged(uint32_t aClientId,
466 uint32_t aCallIndex,
467 uint16_t aCallState,
468 const nsAString& aNumber,
469 uint16_t aNumberPresentation,
470 const nsAString& aName,
471 uint16_t aNamePresentation,
472 bool aIsOutgoing,
473 bool aIsEmergency,
474 bool aIsConference,
475 bool aIsSwitchable,
476 bool aIsMergeable)
478 MOZ_CRASH("Not a TelephonyParent!");
481 NS_IMETHODIMP
482 TelephonyRequestParent::ConferenceCallStateChanged(uint16_t aCallState)
484 MOZ_CRASH("Not a TelephonyParent!");
487 NS_IMETHODIMP
488 TelephonyRequestParent::EnumerateCallStateComplete()
490 NS_ENSURE_TRUE(!mActorDestroyed, NS_ERROR_FAILURE);
492 return Send__delete__(this, EnumerateCallsResponse()) ? NS_OK : NS_ERROR_FAILURE;
495 NS_IMETHODIMP
496 TelephonyRequestParent::EnumerateCallState(uint32_t aClientId,
497 uint32_t aCallIndex,
498 uint16_t aCallState,
499 const nsAString& aNumber,
500 uint16_t aNumberPresentation,
501 const nsAString& aName,
502 uint16_t aNamePresentation,
503 bool aIsOutgoing,
504 bool aIsEmergency,
505 bool aIsConference,
506 bool aIsSwitchable,
507 bool aIsMergeable)
509 NS_ENSURE_TRUE(!mActorDestroyed, NS_ERROR_FAILURE);
511 IPCCallStateData data(aCallIndex, aCallState, nsString(aNumber),
512 aNumberPresentation, nsString(aName), aNamePresentation,
513 aIsOutgoing, aIsEmergency, aIsConference,
514 aIsSwitchable, aIsMergeable);
515 return SendNotifyEnumerateCallState(aClientId, data) ? NS_OK
516 : NS_ERROR_FAILURE;
519 NS_IMETHODIMP
520 TelephonyRequestParent::NotifyCdmaCallWaiting(uint32_t aClientId,
521 const nsAString& aNumber,
522 uint16_t aNumberPresentation,
523 const nsAString& aName,
524 uint16_t aNamePresentation)
526 MOZ_CRASH("Not a TelephonyParent!");
529 NS_IMETHODIMP
530 TelephonyRequestParent::NotifyConferenceError(const nsAString& aName,
531 const nsAString& aMessage)
533 MOZ_CRASH("Not a TelephonyParent!");
536 NS_IMETHODIMP
537 TelephonyRequestParent::NotifyError(uint32_t aClientId,
538 int32_t aCallIndex,
539 const nsAString& aError)
541 MOZ_CRASH("Not a TelephonyParent!");
544 NS_IMETHODIMP
545 TelephonyRequestParent::SupplementaryServiceNotification(uint32_t aClientId,
546 int32_t aCallIndex,
547 uint16_t aNotification)
549 MOZ_CRASH("Not a TelephonyParent!");
552 // nsITelephonyDialCallback
554 NS_IMETHODIMP
555 TelephonyRequestParent::NotifySuccess()
557 return SendResponse(SuccessResponse());
560 NS_IMETHODIMP
561 TelephonyRequestParent::NotifyError(const nsAString& aError)
563 return SendResponse(ErrorResponse(nsAutoString(aError)));
566 NS_IMETHODIMP
567 TelephonyRequestParent::NotifyDialCallSuccess(uint32_t aClientId,
568 uint32_t aCallIndex,
569 const nsAString& aNumber)
571 return SendResponse(DialResponseCallSuccess(aClientId, aCallIndex,
572 nsAutoString(aNumber)));