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 /*******************************************************************************
13 ******************************************************************************/
15 NS_IMPL_ISUPPORTS(TelephonyParent
, nsITelephonyListener
)
17 TelephonyParent::TelephonyParent()
18 : mActorDestroyed(false)
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();
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());
49 MOZ_CRASH("Unknown type!");
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().
67 TelephonyParent::DeallocPTelephonyRequestParent(PTelephonyRequestParent
* aActor
)
69 // TelephonyRequestParent is refcounted, must not be freed manually.
70 static_cast<TelephonyRequestParent
*>(aActor
)->Release();
75 TelephonyParent::Recv__delete__()
77 return true; // Unregister listener in TelephonyParent::ActorDestroy().
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));
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));
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
234 TelephonyParent::RecvGetMicrophoneMuted(bool* aMuted
)
238 nsCOMPtr
<nsITelephonyService
> service
=
239 do_GetService(TELEPHONY_SERVICE_CONTRACTID
);
240 NS_ENSURE_TRUE(service
, true);
242 service
->GetMicrophoneMuted(aMuted
);
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
);
258 TelephonyParent::RecvGetSpeakerEnabled(bool* aEnabled
)
262 nsCOMPtr
<nsITelephonyService
> service
=
263 do_GetService(TELEPHONY_SERVICE_CONTRACTID
);
264 NS_ENSURE_TRUE(service
, true);
266 service
->GetSpeakerEnabled(aEnabled
);
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
);
281 // nsITelephonyListener
284 TelephonyParent::CallStateChanged(uint32_t aClientId
,
287 const nsAString
& aNumber
,
288 uint16_t aNumberPresentation
,
289 const nsAString
& aName
,
290 uint16_t aNamePresentation
,
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
;
307 TelephonyParent::ConferenceCallStateChanged(uint16_t aCallState
)
309 NS_ENSURE_TRUE(!mActorDestroyed
, NS_ERROR_FAILURE
);
311 return SendNotifyConferenceCallStateChanged(aCallState
) ? NS_OK
316 TelephonyParent::EnumerateCallStateComplete()
318 MOZ_CRASH("Not a EnumerateCalls request!");
322 TelephonyParent::EnumerateCallState(uint32_t aClientId
,
325 const nsAString
& aNumber
,
326 uint16_t aNumberPresentation
,
327 const nsAString
& aName
,
328 uint16_t aNamePresentation
,
335 MOZ_CRASH("Not a EnumerateCalls request!");
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
;
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
363 TelephonyParent::NotifyError(uint32_t aClientId
,
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
;
374 TelephonyParent::SupplementaryServiceNotification(uint32_t aClientId
,
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)
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;
408 TelephonyRequestParent::DoRequest(const EnumerateCallsRequest
& aRequest
)
410 nsresult rv
= NS_ERROR_FAILURE
;
412 nsCOMPtr
<nsITelephonyService
> service
=
413 do_GetService(TELEPHONY_SERVICE_CONTRACTID
);
415 rv
= service
->EnumerateCalls(this);
419 return NS_SUCCEEDED(EnumerateCallStateComplete());
426 TelephonyRequestParent::DoRequest(const DialRequest
& aRequest
)
428 nsCOMPtr
<nsITelephonyService
> service
=
429 do_GetService(TELEPHONY_SERVICE_CONTRACTID
);
431 service
->Dial(aRequest
.clientId(), aRequest
.number(),
432 aRequest
.isEmergency(), this);
434 return NS_SUCCEEDED(NotifyError(NS_LITERAL_STRING("InvalidStateError")));
441 TelephonyRequestParent::DoRequest(const HangUpConferenceRequest
& aRequest
)
443 nsCOMPtr
<nsITelephonyService
> service
=
444 do_GetService(TELEPHONY_SERVICE_CONTRACTID
);
446 service
->HangUpConference(aRequest
.clientId(), this);
448 return NS_SUCCEEDED(NotifyError(NS_LITERAL_STRING("InvalidStateError")));
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
465 TelephonyRequestParent::CallStateChanged(uint32_t aClientId
,
468 const nsAString
& aNumber
,
469 uint16_t aNumberPresentation
,
470 const nsAString
& aName
,
471 uint16_t aNamePresentation
,
478 MOZ_CRASH("Not a TelephonyParent!");
482 TelephonyRequestParent::ConferenceCallStateChanged(uint16_t aCallState
)
484 MOZ_CRASH("Not a TelephonyParent!");
488 TelephonyRequestParent::EnumerateCallStateComplete()
490 NS_ENSURE_TRUE(!mActorDestroyed
, NS_ERROR_FAILURE
);
492 return Send__delete__(this, EnumerateCallsResponse()) ? NS_OK
: NS_ERROR_FAILURE
;
496 TelephonyRequestParent::EnumerateCallState(uint32_t aClientId
,
499 const nsAString
& aNumber
,
500 uint16_t aNumberPresentation
,
501 const nsAString
& aName
,
502 uint16_t aNamePresentation
,
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
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!");
530 TelephonyRequestParent::NotifyConferenceError(const nsAString
& aName
,
531 const nsAString
& aMessage
)
533 MOZ_CRASH("Not a TelephonyParent!");
537 TelephonyRequestParent::NotifyError(uint32_t aClientId
,
539 const nsAString
& aError
)
541 MOZ_CRASH("Not a TelephonyParent!");
545 TelephonyRequestParent::SupplementaryServiceNotification(uint32_t aClientId
,
547 uint16_t aNotification
)
549 MOZ_CRASH("Not a TelephonyParent!");
552 // nsITelephonyDialCallback
555 TelephonyRequestParent::NotifySuccess()
557 return SendResponse(SuccessResponse());
561 TelephonyRequestParent::NotifyError(const nsAString
& aError
)
563 return SendResponse(ErrorResponse(nsAutoString(aError
)));
567 TelephonyRequestParent::NotifyDialCallSuccess(uint32_t aClientId
,
569 const nsAString
& aNumber
)
571 return SendResponse(DialResponseCallSuccess(aClientId
, aCallIndex
,
572 nsAutoString(aNumber
)));