1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "TelephonyDialCallback.h"
7 #include "mozilla/dom/MozMobileConnectionBinding.h"
8 #include "nsIMobileCallForwardingOptions.h"
9 #include "nsIMobileConnectionService.h"
11 using namespace mozilla::dom
;
12 using namespace mozilla::dom::telephony
;
14 NS_IMPL_ISUPPORTS_INHERITED(TelephonyDialCallback
, TelephonyCallback
,
15 nsITelephonyDialCallback
)
17 TelephonyDialCallback::TelephonyDialCallback(nsPIDOMWindow
* aWindow
,
18 Telephony
* aTelephony
,
20 : TelephonyCallback(aPromise
), mWindow(aWindow
), mTelephony(aTelephony
)
22 MOZ_ASSERT(mTelephony
);
26 TelephonyDialCallback::NotifyDialMMISuccess(JSContext
* aCx
,
27 const MozMMIResult
& aResult
)
29 JS::Rooted
<JS::Value
> jsResult(aCx
);
31 if (!ToJSValue(aCx
, aResult
, &jsResult
)) {
32 JS_ClearPendingException(aCx
);
33 return NS_ERROR_TYPE_ERR
;
36 mMMICall
->NotifyResult(jsResult
);
40 // nsITelephonyDialCallback
43 TelephonyDialCallback::NotifyDialMMI(const nsAString
& aServiceCode
)
45 mServiceCode
.Assign(aServiceCode
);
47 mMMICall
= new MMICall(mWindow
, aServiceCode
);
48 mPromise
->MaybeResolve(mMMICall
);
53 TelephonyDialCallback::NotifyDialCallSuccess(uint32_t aClientId
,
55 const nsAString
& aNumber
)
57 nsRefPtr
<TelephonyCallId
> id
= mTelephony
->CreateCallId(aNumber
);
58 nsRefPtr
<TelephonyCall
> call
=
59 mTelephony
->CreateCall(id
, aClientId
, aCallIndex
,
60 nsITelephonyService::CALL_STATE_DIALING
);
62 mPromise
->MaybeResolve(call
);
67 TelephonyDialCallback::NotifyDialMMISuccess(const nsAString
& aStatusMessage
)
70 if (NS_WARN_IF(!jsapi
.Init(mWindow
))) {
71 return NS_ERROR_FAILURE
;
74 JSContext
* cx
= jsapi
.cx();
77 result
.mSuccess
= true;
78 result
.mServiceCode
.Assign(mServiceCode
);
79 result
.mStatusMessage
.Assign(aStatusMessage
);
81 return NotifyDialMMISuccess(cx
, result
);
85 TelephonyDialCallback::NotifyDialMMISuccessWithInteger(const nsAString
& aStatusMessage
,
86 uint16_t aAdditionalInformation
)
89 if (NS_WARN_IF(!jsapi
.Init(mWindow
))) {
90 return NS_ERROR_FAILURE
;
93 JSContext
* cx
= jsapi
.cx();
96 result
.mSuccess
= true;
97 result
.mServiceCode
.Assign(mServiceCode
);
98 result
.mStatusMessage
.Assign(aStatusMessage
);
99 result
.mAdditionalInformation
.Construct().SetAsUnsignedShort() = aAdditionalInformation
;
101 return NotifyDialMMISuccess(cx
, result
);
105 TelephonyDialCallback::NotifyDialMMISuccessWithStrings(const nsAString
& aStatusMessage
,
107 const char16_t
** aAdditionalInformation
)
110 if (NS_WARN_IF(!jsapi
.Init(mWindow
))) {
111 return NS_ERROR_FAILURE
;
114 JSContext
* cx
= jsapi
.cx();
116 RootedDictionary
<MozMMIResult
> result(cx
);
117 result
.mSuccess
= true;
118 result
.mServiceCode
.Assign(mServiceCode
);
119 result
.mStatusMessage
.Assign(aStatusMessage
);
121 nsTArray
<nsString
> additionalInformation
;
122 for (uint32_t i
= 0; i
< aCount
; i
++) {
123 additionalInformation
.AppendElement(nsDependentString(aAdditionalInformation
[i
]));
126 JS::Rooted
<JS::Value
> jsAdditionalInformation(cx
);
127 if (!ToJSValue(cx
, additionalInformation
, &jsAdditionalInformation
)) {
128 JS_ClearPendingException(cx
);
129 return NS_ERROR_TYPE_ERR
;
132 result
.mAdditionalInformation
.Construct().SetAsObject() =
133 &jsAdditionalInformation
.toObject();
135 return NotifyDialMMISuccess(cx
, result
);
139 TelephonyDialCallback::NotifyDialMMISuccessWithCallForwardingOptions(const nsAString
& aStatusMessage
,
141 nsIMobileCallForwardingOptions
** aResults
)
144 if (NS_WARN_IF(!jsapi
.Init(mWindow
))) {
145 return NS_ERROR_FAILURE
;
148 JSContext
* cx
= jsapi
.cx();
150 RootedDictionary
<MozMMIResult
> result(cx
);
151 result
.mSuccess
= true;
152 result
.mServiceCode
.Assign(mServiceCode
);
153 result
.mStatusMessage
.Assign(aStatusMessage
);
155 nsTArray
<MozCallForwardingOptions
> additionalInformation
;
156 for (uint32_t i
= 0; i
< aCount
; i
++) {
157 MozCallForwardingOptions options
;
160 aResults
[i
]->GetActive(&active
);
161 options
.mActive
.Construct(active
);
163 int16_t action
= nsIMobileConnection::CALL_FORWARD_ACTION_UNKNOWN
;
164 aResults
[i
]->GetAction(&action
);
165 if (action
!= nsIMobileConnection::CALL_FORWARD_ACTION_UNKNOWN
) {
166 options
.mAction
.Construct(action
);
169 int16_t reason
= nsIMobileConnection::CALL_FORWARD_REASON_UNKNOWN
;
170 aResults
[i
]->GetReason(&reason
);
171 if (reason
!= nsIMobileConnection::CALL_FORWARD_REASON_UNKNOWN
) {
172 options
.mReason
.Construct(reason
);
176 aResults
[i
]->GetNumber(number
);
177 options
.mNumber
.Construct(number
.get());
179 int16_t timeSeconds
= -1;
180 aResults
[i
]->GetTimeSeconds(&timeSeconds
);
181 if (timeSeconds
>= 0) {
182 options
.mTimeSeconds
.Construct(timeSeconds
);
185 int16_t serviceClass
= nsIMobileConnection::ICC_SERVICE_CLASS_NONE
;
186 aResults
[i
]->GetServiceClass(&serviceClass
);
187 if (serviceClass
!= nsIMobileConnection::ICC_SERVICE_CLASS_NONE
) {
188 options
.mServiceClass
.Construct(serviceClass
);
191 additionalInformation
.AppendElement(options
);
194 JS::Rooted
<JS::Value
> jsAdditionalInformation(cx
);
195 if (!ToJSValue(cx
, additionalInformation
, &jsAdditionalInformation
)) {
196 JS_ClearPendingException(cx
);
197 return NS_ERROR_TYPE_ERR
;
200 result
.mAdditionalInformation
.Construct().SetAsObject() =
201 &jsAdditionalInformation
.toObject();
203 return NotifyDialMMISuccess(cx
, result
);
207 TelephonyDialCallback::NotifyDialMMIError(const nsAString
& aError
)
210 if (NS_WARN_IF(!jsapi
.Init(mWindow
))) {
211 return NS_ERROR_FAILURE
;
214 JSContext
* cx
= jsapi
.cx();
217 result
.mSuccess
= false;
218 result
.mServiceCode
.Assign(mServiceCode
);
219 result
.mStatusMessage
.Assign(aError
);
221 return NotifyDialMMISuccess(cx
, result
);
225 TelephonyDialCallback::NotifyDialMMIErrorWithInfo(const nsAString
& aError
,
229 if (NS_WARN_IF(!jsapi
.Init(mWindow
))) {
230 return NS_ERROR_FAILURE
;
233 JSContext
* cx
= jsapi
.cx();
236 result
.mSuccess
= false;
237 result
.mServiceCode
.Assign(mServiceCode
);
238 result
.mStatusMessage
.Assign(aError
);
239 result
.mAdditionalInformation
.Construct().SetAsUnsignedShort() = aInfo
;
241 return NotifyDialMMISuccess(cx
, result
);