dom/icc/src/IccListener.cpp

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:50170e8f28b8
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/. */
4
5 #include "IccListener.h"
6
7 #include "Icc.h"
8 #include "IccManager.h"
9 #include "nsIDOMClassInfo.h"
10 #include "nsIDOMIccInfo.h"
11 #include "nsRadioInterfaceLayer.h"
12
13 using namespace mozilla::dom;
14
15 NS_IMPL_ISUPPORTS(IccListener, nsIIccListener)
16
17 IccListener::IccListener(IccManager* aIccManager, uint32_t aClientId)
18 : mClientId(aClientId)
19 , mIccManager(aIccManager)
20 {
21 MOZ_ASSERT(mIccManager);
22
23 mProvider = do_GetService(NS_RILCONTENTHELPER_CONTRACTID);
24
25 if (!mProvider) {
26 NS_WARNING("Could not acquire nsIIccProvider!");
27 return;
28 }
29
30 nsCOMPtr<nsIDOMMozIccInfo> iccInfo;
31 mProvider->GetIccInfo(mClientId, getter_AddRefs(iccInfo));
32 if (iccInfo) {
33 nsString iccId;
34 iccInfo->GetIccid(iccId);
35 if (!iccId.IsEmpty()) {
36 mIcc = new Icc(mIccManager->GetOwner(), mClientId, iccId);
37 }
38 }
39
40 DebugOnly<nsresult> rv = mProvider->RegisterIccMsg(mClientId, this);
41 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
42 "Failed registering icc messages with provider");
43 }
44
45 IccListener::~IccListener()
46 {
47 Shutdown();
48 }
49
50 void
51 IccListener::Shutdown()
52 {
53 if (mProvider) {
54 mProvider->UnregisterIccMsg(mClientId, this);
55 mProvider = nullptr;
56 }
57
58 if (mIcc) {
59 mIcc->Shutdown();
60 mIcc = nullptr;
61 }
62
63 mIccManager = nullptr;
64 }
65
66 // nsIIccListener
67
68 NS_IMETHODIMP
69 IccListener::NotifyStkCommand(const nsAString& aMessage)
70 {
71 if (!mIcc) {
72 return NS_OK;
73 }
74
75 return mIcc->NotifyStkEvent(NS_LITERAL_STRING("stkcommand"), aMessage);
76 }
77
78 NS_IMETHODIMP
79 IccListener::NotifyStkSessionEnd()
80 {
81 if (!mIcc) {
82 return NS_OK;
83 }
84
85 return mIcc->NotifyEvent(NS_LITERAL_STRING("stksessionend"));
86 }
87
88 NS_IMETHODIMP
89 IccListener::NotifyCardStateChanged()
90 {
91 if (!mIcc) {
92 return NS_OK;
93 }
94
95 return mIcc->NotifyEvent(NS_LITERAL_STRING("cardstatechange"));
96 }
97
98 NS_IMETHODIMP
99 IccListener::NotifyIccInfoChanged()
100 {
101 nsCOMPtr<nsIDOMMozIccInfo> iccInfo;
102 mProvider->GetIccInfo(mClientId, getter_AddRefs(iccInfo));
103
104 // Create/delete icc object based on current iccInfo.
105 // 1. If the mIcc is nullptr and iccInfo has valid data, create icc object and
106 // notify mIccManager a new icc is added.
107 // 2. If the mIcc is not nullptr and iccInfo becomes to null, delete existed
108 // icc object and notify mIccManager the icc is removed.
109 if (!mIcc) {
110 if (iccInfo) {
111 nsString iccId;
112 iccInfo->GetIccid(iccId);
113 if (!iccId.IsEmpty()) {
114 mIcc = new Icc(mIccManager->GetOwner(), mClientId, iccId);
115 mIccManager->NotifyIccAdd(iccId);
116 mIcc->NotifyEvent(NS_LITERAL_STRING("iccinfochange"));
117 }
118 }
119 } else {
120 mIcc->NotifyEvent(NS_LITERAL_STRING("iccinfochange"));
121 if (!iccInfo) {
122 nsString iccId = mIcc->GetIccId();
123 mIcc->Shutdown();
124 mIcc = nullptr;
125 mIccManager->NotifyIccRemove(iccId);
126 }
127 }
128
129 return NS_OK;
130 }

mercurial