|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #include "Voicemail.h" |
|
8 |
|
9 #include "mozilla/dom/MozVoicemailBinding.h" |
|
10 #include "nsIDOMMozVoicemailStatus.h" |
|
11 #include "nsIDOMMozVoicemailEvent.h" |
|
12 |
|
13 #include "mozilla/Preferences.h" |
|
14 #include "mozilla/Services.h" |
|
15 #include "nsDOMClassInfo.h" |
|
16 #include "nsServiceManagerUtils.h" |
|
17 #include "GeneratedEvents.h" |
|
18 |
|
19 #define NS_RILCONTENTHELPER_CONTRACTID "@mozilla.org/ril/content-helper;1" |
|
20 const char* kPrefRilNumRadioInterfaces = "ril.numRadioInterfaces"; |
|
21 |
|
22 using namespace mozilla::dom; |
|
23 |
|
24 class Voicemail::Listener : public nsIVoicemailListener |
|
25 { |
|
26 Voicemail* mVoicemail; |
|
27 |
|
28 public: |
|
29 NS_DECL_ISUPPORTS |
|
30 NS_FORWARD_SAFE_NSIVOICEMAILLISTENER(mVoicemail) |
|
31 |
|
32 Listener(Voicemail* aVoicemail) |
|
33 : mVoicemail(aVoicemail) |
|
34 { |
|
35 MOZ_ASSERT(mVoicemail); |
|
36 } |
|
37 |
|
38 void Disconnect() |
|
39 { |
|
40 MOZ_ASSERT(mVoicemail); |
|
41 mVoicemail = nullptr; |
|
42 } |
|
43 }; |
|
44 |
|
45 NS_IMPL_ISUPPORTS(Voicemail::Listener, nsIVoicemailListener) |
|
46 |
|
47 Voicemail::Voicemail(nsPIDOMWindow* aWindow, |
|
48 nsIVoicemailProvider* aProvider) |
|
49 : DOMEventTargetHelper(aWindow) |
|
50 , mProvider(aProvider) |
|
51 { |
|
52 mListener = new Listener(this); |
|
53 DebugOnly<nsresult> rv = mProvider->RegisterVoicemailMsg(mListener); |
|
54 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), |
|
55 "Failed registering voicemail messages with provider"); |
|
56 } |
|
57 |
|
58 Voicemail::~Voicemail() |
|
59 { |
|
60 MOZ_ASSERT(mProvider && mListener); |
|
61 |
|
62 mListener->Disconnect(); |
|
63 mProvider->UnregisterVoicemailMsg(mListener); |
|
64 } |
|
65 |
|
66 JSObject* |
|
67 Voicemail::WrapObject(JSContext* aCx) |
|
68 { |
|
69 return MozVoicemailBinding::Wrap(aCx, this); |
|
70 } |
|
71 |
|
72 bool |
|
73 Voicemail::IsValidServiceId(uint32_t aServiceId) const |
|
74 { |
|
75 uint32_t numClients = mozilla::Preferences::GetUint(kPrefRilNumRadioInterfaces, 1); |
|
76 |
|
77 return aServiceId < numClients; |
|
78 } |
|
79 |
|
80 bool |
|
81 Voicemail::PassedOrDefaultServiceId(const Optional<uint32_t>& aServiceId, |
|
82 uint32_t& aResult) const |
|
83 { |
|
84 if (aServiceId.WasPassed()) { |
|
85 if (!IsValidServiceId(aServiceId.Value())) { |
|
86 return false; |
|
87 } |
|
88 aResult = aServiceId.Value(); |
|
89 } else { |
|
90 mProvider->GetVoicemailDefaultServiceId(&aResult); |
|
91 } |
|
92 |
|
93 return true; |
|
94 } |
|
95 |
|
96 // MozVoicemail WebIDL |
|
97 |
|
98 already_AddRefed<nsIDOMMozVoicemailStatus> |
|
99 Voicemail::GetStatus(const Optional<uint32_t>& aServiceId, |
|
100 ErrorResult& aRv) const |
|
101 { |
|
102 if (!mProvider) { |
|
103 aRv.Throw(NS_ERROR_UNEXPECTED); |
|
104 return nullptr; |
|
105 } |
|
106 |
|
107 uint32_t id = 0; |
|
108 if (!PassedOrDefaultServiceId(aServiceId, id)) { |
|
109 aRv.Throw(NS_ERROR_INVALID_ARG); |
|
110 return nullptr; |
|
111 } |
|
112 nsCOMPtr<nsIDOMMozVoicemailStatus> status; |
|
113 nsresult rv = mProvider->GetVoicemailStatus(id, getter_AddRefs(status)); |
|
114 if (NS_FAILED(rv)) { |
|
115 aRv.Throw(rv); |
|
116 return nullptr; |
|
117 } |
|
118 |
|
119 return status.forget(); |
|
120 } |
|
121 |
|
122 void |
|
123 Voicemail::GetNumber(const Optional<uint32_t>& aServiceId, nsString& aNumber, |
|
124 ErrorResult& aRv) const |
|
125 { |
|
126 aNumber.SetIsVoid(true); |
|
127 |
|
128 if (!mProvider) { |
|
129 aRv.Throw(NS_ERROR_UNEXPECTED); |
|
130 return; |
|
131 } |
|
132 |
|
133 uint32_t id = 0; |
|
134 if (!PassedOrDefaultServiceId(aServiceId, id)) { |
|
135 aRv.Throw(NS_ERROR_INVALID_ARG); |
|
136 return; |
|
137 } |
|
138 |
|
139 aRv = mProvider->GetVoicemailNumber(id, aNumber); |
|
140 } |
|
141 |
|
142 void |
|
143 Voicemail::GetDisplayName(const Optional<uint32_t>& aServiceId, nsString& aDisplayName, |
|
144 ErrorResult& aRv) const |
|
145 { |
|
146 aDisplayName.SetIsVoid(true); |
|
147 |
|
148 if (!mProvider) { |
|
149 aRv.Throw(NS_ERROR_UNEXPECTED); |
|
150 return; |
|
151 } |
|
152 |
|
153 uint32_t id = 0; |
|
154 if (!PassedOrDefaultServiceId(aServiceId, id)) { |
|
155 aRv.Throw(NS_ERROR_INVALID_ARG); |
|
156 return; |
|
157 } |
|
158 |
|
159 aRv = mProvider->GetVoicemailDisplayName(id, aDisplayName); |
|
160 } |
|
161 |
|
162 // nsIVoicemailListener |
|
163 |
|
164 NS_IMETHODIMP |
|
165 Voicemail::NotifyStatusChanged(nsIDOMMozVoicemailStatus* aStatus) |
|
166 { |
|
167 nsCOMPtr<nsIDOMEvent> event; |
|
168 NS_NewDOMMozVoicemailEvent(getter_AddRefs(event), this, nullptr, nullptr); |
|
169 |
|
170 nsCOMPtr<nsIDOMMozVoicemailEvent> ce = do_QueryInterface(event); |
|
171 nsresult rv = ce->InitMozVoicemailEvent(NS_LITERAL_STRING("statuschanged"), |
|
172 false, false, aStatus); |
|
173 NS_ENSURE_SUCCESS(rv, rv); |
|
174 |
|
175 return DispatchTrustedEvent(ce); |
|
176 } |
|
177 |
|
178 nsresult |
|
179 NS_NewVoicemail(nsPIDOMWindow* aWindow, Voicemail** aVoicemail) |
|
180 { |
|
181 nsPIDOMWindow* innerWindow = aWindow->IsInnerWindow() ? |
|
182 aWindow : |
|
183 aWindow->GetCurrentInnerWindow(); |
|
184 |
|
185 nsCOMPtr<nsIVoicemailProvider> provider = |
|
186 do_GetService(NS_RILCONTENTHELPER_CONTRACTID); |
|
187 NS_ENSURE_STATE(provider); |
|
188 |
|
189 nsRefPtr<Voicemail> voicemail = new Voicemail(innerWindow, provider); |
|
190 voicemail.forget(aVoicemail); |
|
191 return NS_OK; |
|
192 } |