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 "mozilla/dom/Icc.h"
7 #include "mozilla/dom/DOMRequest.h"
8 #include "mozilla/dom/IccInfo.h"
9 #include "mozilla/dom/MozStkCommandEvent.h"
10 #include "mozilla/dom/Promise.h"
11 #include "mozilla/dom/ScriptSettings.h"
12 #include "nsIIccInfo.h"
13 #include "nsIIccProvider.h"
15 #include "nsRadioInterfaceLayer.h"
16 #include "nsServiceManagerUtils.h"
24 IsPukCardLockType(IccLockType aLockType
)
27 case IccLockType::Puk
:
28 case IccLockType::Puk2
:
29 case IccLockType::NckPuk
:
30 case IccLockType::Nck1Puk
:
31 case IccLockType::Nck2Puk
:
32 case IccLockType::HnckPuk
:
33 case IccLockType::CckPuk
:
34 case IccLockType::SpckPuk
:
35 case IccLockType::RcckPuk
:
36 case IccLockType::RspckPuk
:
37 case IccLockType::NsckPuk
:
38 case IccLockType::PckPuk
:
45 } // anonymous namespace
47 NS_IMPL_CYCLE_COLLECTION_INHERITED(Icc
, DOMEventTargetHelper
, mIccInfo
)
49 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(Icc
)
50 NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper
)
52 NS_IMPL_ADDREF_INHERITED(Icc
, DOMEventTargetHelper
)
53 NS_IMPL_RELEASE_INHERITED(Icc
, DOMEventTargetHelper
)
55 Icc::Icc(nsPIDOMWindow
* aWindow
, long aClientId
, nsIIccInfo
* aIccInfo
)
57 , mClientId(aClientId
)
61 mProvider
= do_GetService(NS_RILCONTENTHELPER_CONTRACTID
);
64 aIccInfo
->GetIccid(mIccId
);
65 UpdateIccInfo(aIccInfo
);
68 // Not being able to acquire the provider isn't fatal since we check
69 // for it explicitly below.
71 NS_WARNING("Could not acquire nsIIccProvider!");
84 Icc::NotifyEvent(const nsAString
& aName
)
86 return DispatchTrustedEvent(aName
);
90 Icc::NotifyStkEvent(const nsAString
& aName
, const nsAString
& aMessage
)
93 if (NS_WARN_IF(!jsapi
.InitWithLegacyErrorReporting(GetOwner()))) {
94 return NS_ERROR_UNEXPECTED
;
96 JSContext
* cx
= jsapi
.cx();
97 JS::Rooted
<JS::Value
> value(cx
);
99 if (!aMessage
.IsEmpty()) {
100 nsCOMPtr
<nsIJSON
> json(new nsJSON());
101 nsresult rv
= json
->DecodeToJSVal(aMessage
, cx
, &value
);
102 NS_ENSURE_SUCCESS(rv
, rv
);
104 value
= JS::NullValue();
107 MozStkCommandEventInit init
;
108 init
.mBubbles
= false;
109 init
.mCancelable
= false;
110 init
.mCommand
= value
;
112 nsRefPtr
<MozStkCommandEvent
> event
=
113 MozStkCommandEvent::Constructor(this, aName
, init
);
115 return DispatchTrustedEvent(event
);
119 Icc::UpdateIccInfo(nsIIccInfo
* aIccInfo
)
126 nsCOMPtr
<nsIGsmIccInfo
> gsmIccInfo(do_QueryInterface(aIccInfo
));
128 if (mIccInfo
.IsNull() || !mIccInfo
.Value().IsMozGsmIccInfo()) {
129 mIccInfo
.SetValue().SetAsMozGsmIccInfo() = new GsmIccInfo(GetOwner());
131 mIccInfo
.Value().GetAsMozGsmIccInfo().get()->Update(gsmIccInfo
);
135 nsCOMPtr
<nsICdmaIccInfo
> cdmaIccInfo(do_QueryInterface(aIccInfo
));
137 if (mIccInfo
.IsNull() || !mIccInfo
.Value().IsMozCdmaIccInfo()) {
138 mIccInfo
.SetValue().SetAsMozCdmaIccInfo() = new CdmaIccInfo(GetOwner());
140 mIccInfo
.Value().GetAsMozCdmaIccInfo().get()->Update(cdmaIccInfo
);
144 if (mIccInfo
.IsNull() || !mIccInfo
.Value().IsMozIccInfo()) {
145 mIccInfo
.SetValue().SetAsMozIccInfo() = new IccInfo(GetOwner());
147 mIccInfo
.Value().GetAsMozIccInfo().get()->Update(aIccInfo
);
153 Icc::WrapObject(JSContext
* aCx
)
155 return MozIccBinding::Wrap(aCx
, this);
161 Icc::GetIccInfo(Nullable
<OwningMozIccInfoOrMozGsmIccInfoOrMozCdmaIccInfo
>& aIccInfo
) const
166 Nullable
<IccCardState
>
167 Icc::GetCardState() const
169 Nullable
<IccCardState
> result
;
171 uint32_t cardState
= nsIIccProvider::CARD_STATE_UNDETECTED
;
173 NS_SUCCEEDED(mProvider
->GetCardState(mClientId
, &cardState
)) &&
174 cardState
!= nsIIccProvider::CARD_STATE_UNDETECTED
) {
175 MOZ_ASSERT(cardState
< static_cast<uint32_t>(IccCardState::EndGuard_
));
176 result
.SetValue(static_cast<IccCardState
>(cardState
));
183 Icc::SendStkResponse(const JSContext
* aCx
, JS::Handle
<JS::Value
> aCommand
,
184 JS::Handle
<JS::Value
> aResponse
, ErrorResult
& aRv
)
187 aRv
.Throw(NS_ERROR_FAILURE
);
191 nsresult rv
= mProvider
->SendStkResponse(mClientId
, GetOwner(), aCommand
,
199 Icc::SendStkMenuSelection(uint16_t aItemIdentifier
, bool aHelpRequested
,
203 aRv
.Throw(NS_ERROR_FAILURE
);
207 nsresult rv
= mProvider
->SendStkMenuSelection(mClientId
,
217 Icc::SendStkTimerExpiration(const JSContext
* aCx
, JS::Handle
<JS::Value
> aTimer
,
221 aRv
.Throw(NS_ERROR_FAILURE
);
225 nsresult rv
= mProvider
->SendStkTimerExpiration(mClientId
, GetOwner(),
233 Icc::SendStkEventDownload(const JSContext
* aCx
, JS::Handle
<JS::Value
> aEvent
,
237 aRv
.Throw(NS_ERROR_FAILURE
);
241 nsresult rv
= mProvider
->SendStkEventDownload(mClientId
, GetOwner(), aEvent
);
247 already_AddRefed
<DOMRequest
>
248 Icc::GetCardLock(IccLockType aLockType
, ErrorResult
& aRv
)
251 aRv
.Throw(NS_ERROR_FAILURE
);
255 nsRefPtr
<nsIDOMDOMRequest
> request
;
256 nsresult rv
= mProvider
->GetCardLockEnabled(mClientId
, GetOwner(),
257 static_cast<uint32_t>(aLockType
),
258 getter_AddRefs(request
));
264 return request
.forget().downcast
<DOMRequest
>();
267 already_AddRefed
<DOMRequest
>
268 Icc::UnlockCardLock(const IccUnlockCardLockOptions
& aOptions
, ErrorResult
& aRv
)
271 aRv
.Throw(NS_ERROR_FAILURE
);
275 nsRefPtr
<nsIDOMDOMRequest
> request
;
276 const nsString
& password
= IsPukCardLockType(aOptions
.mLockType
)
277 ? aOptions
.mPuk
: aOptions
.mPin
;
278 nsresult rv
= mProvider
->UnlockCardLock(mClientId
, GetOwner(),
279 static_cast<uint32_t>(aOptions
.mLockType
),
280 password
, aOptions
.mNewPin
,
281 getter_AddRefs(request
));
287 return request
.forget().downcast
<DOMRequest
>();
290 already_AddRefed
<DOMRequest
>
291 Icc::SetCardLock(const IccSetCardLockOptions
& aOptions
, ErrorResult
& aRv
)
294 aRv
.Throw(NS_ERROR_FAILURE
);
299 nsRefPtr
<nsIDOMDOMRequest
> request
;
301 if (aOptions
.mEnabled
.WasPassed()) {
303 const nsString
& password
= (aOptions
.mLockType
== IccLockType::Fdn
) ?
304 aOptions
.mPin2
: aOptions
.mPin
;
306 rv
= mProvider
->SetCardLockEnabled(mClientId
, GetOwner(),
307 static_cast<uint32_t>(aOptions
.mLockType
),
308 password
, aOptions
.mEnabled
.Value(),
309 getter_AddRefs(request
));
311 // Change card lock password.
312 rv
= mProvider
->ChangeCardLockPassword(mClientId
, GetOwner(),
313 static_cast<uint32_t>(aOptions
.mLockType
),
314 aOptions
.mPin
, aOptions
.mNewPin
,
315 getter_AddRefs(request
));
323 return request
.forget().downcast
<DOMRequest
>();
326 already_AddRefed
<DOMRequest
>
327 Icc::GetCardLockRetryCount(IccLockType aLockType
, ErrorResult
& aRv
)
330 aRv
.Throw(NS_ERROR_FAILURE
);
334 nsRefPtr
<nsIDOMDOMRequest
> request
;
335 nsresult rv
= mProvider
->GetCardLockRetryCount(mClientId
, GetOwner(),
336 static_cast<uint32_t>(aLockType
),
337 getter_AddRefs(request
));
343 return request
.forget().downcast
<DOMRequest
>();
346 already_AddRefed
<DOMRequest
>
347 Icc::ReadContacts(IccContactType aContactType
, ErrorResult
& aRv
)
350 aRv
.Throw(NS_ERROR_FAILURE
);
354 nsRefPtr
<nsIDOMDOMRequest
> request
;
355 nsresult rv
= mProvider
->ReadContacts(mClientId
, GetOwner(),
356 static_cast<uint32_t>(aContactType
),
357 getter_AddRefs(request
));
363 return request
.forget().downcast
<DOMRequest
>();
366 already_AddRefed
<DOMRequest
>
367 Icc::UpdateContact(const JSContext
* aCx
, IccContactType aContactType
,
368 JS::Handle
<JS::Value
> aContact
, const nsAString
& aPin2
,
372 aRv
.Throw(NS_ERROR_FAILURE
);
376 nsRefPtr
<nsIDOMDOMRequest
> request
;
377 nsresult rv
= mProvider
->UpdateContact(mClientId
, GetOwner(),
378 static_cast<uint32_t>(aContactType
),
380 getter_AddRefs(request
));
386 return request
.forget().downcast
<DOMRequest
>();
389 already_AddRefed
<DOMRequest
>
390 Icc::MatchMvno(IccMvnoType aMvnoType
, const nsAString
& aMvnoData
,
394 aRv
.Throw(NS_ERROR_FAILURE
);
398 nsRefPtr
<nsIDOMDOMRequest
> request
;
399 nsresult rv
= mProvider
->MatchMvno(mClientId
, GetOwner(),
400 static_cast<uint32_t>(aMvnoType
),
401 aMvnoData
, getter_AddRefs(request
));
407 return request
.forget().downcast
<DOMRequest
>();
410 already_AddRefed
<Promise
>
411 Icc::GetServiceState(IccService aService
, ErrorResult
& aRv
)
414 aRv
.Throw(NS_ERROR_FAILURE
);
418 nsCOMPtr
<nsISupports
> supports
;
419 nsresult rv
= mProvider
->GetServiceState(mClientId
, GetOwner(),
420 static_cast<uint32_t>(aService
),
421 getter_AddRefs(supports
));
427 nsCOMPtr
<Promise
> promise
= do_QueryInterface(supports
);
428 return promise
.forget();
432 } // namespace mozilla