| |
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 |
| |
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| |
5 |
| |
6 #include "MobileMessageCallback.h" |
| |
7 #include "nsContentUtils.h" |
| |
8 #include "nsCxPusher.h" |
| |
9 #include "nsIDOMMozSmsMessage.h" |
| |
10 #include "nsIDOMMozMmsMessage.h" |
| |
11 #include "nsIDOMSmsSegmentInfo.h" |
| |
12 #include "nsIScriptGlobalObject.h" |
| |
13 #include "nsPIDOMWindow.h" |
| |
14 #include "MmsMessage.h" |
| |
15 #include "jsapi.h" |
| |
16 #include "xpcpublic.h" |
| |
17 #include "nsServiceManagerUtils.h" |
| |
18 #include "nsTArrayHelpers.h" |
| |
19 |
| |
20 namespace mozilla { |
| |
21 namespace dom { |
| |
22 namespace mobilemessage { |
| |
23 |
| |
24 NS_IMPL_ADDREF(MobileMessageCallback) |
| |
25 NS_IMPL_RELEASE(MobileMessageCallback) |
| |
26 |
| |
27 NS_INTERFACE_MAP_BEGIN(MobileMessageCallback) |
| |
28 NS_INTERFACE_MAP_ENTRY(nsIMobileMessageCallback) |
| |
29 NS_INTERFACE_MAP_ENTRY(nsISupports) |
| |
30 NS_INTERFACE_MAP_END |
| |
31 |
| |
32 MobileMessageCallback::MobileMessageCallback(DOMRequest* aDOMRequest) |
| |
33 : mDOMRequest(aDOMRequest) |
| |
34 { |
| |
35 } |
| |
36 |
| |
37 MobileMessageCallback::~MobileMessageCallback() |
| |
38 { |
| |
39 } |
| |
40 |
| |
41 |
| |
42 nsresult |
| |
43 MobileMessageCallback::NotifySuccess(JS::Handle<JS::Value> aResult, bool aAsync) |
| |
44 { |
| |
45 if (aAsync) { |
| |
46 nsCOMPtr<nsIDOMRequestService> rs = |
| |
47 do_GetService(DOMREQUEST_SERVICE_CONTRACTID); |
| |
48 NS_ENSURE_TRUE(rs, NS_ERROR_FAILURE); |
| |
49 |
| |
50 return rs->FireSuccessAsync(mDOMRequest, aResult); |
| |
51 } |
| |
52 |
| |
53 mDOMRequest->FireSuccess(aResult); |
| |
54 return NS_OK; |
| |
55 } |
| |
56 |
| |
57 nsresult |
| |
58 MobileMessageCallback::NotifySuccess(nsISupports *aMessage, bool aAsync) |
| |
59 { |
| |
60 nsresult rv; |
| |
61 nsIScriptContext* scriptContext = mDOMRequest->GetContextForEventHandlers(&rv); |
| |
62 NS_ENSURE_SUCCESS(rv, rv); |
| |
63 NS_ENSURE_TRUE(scriptContext, NS_ERROR_FAILURE); |
| |
64 |
| |
65 AutoPushJSContext cx(scriptContext->GetNativeContext()); |
| |
66 NS_ENSURE_TRUE(cx, NS_ERROR_FAILURE); |
| |
67 |
| |
68 JS::Rooted<JSObject*> global(cx, scriptContext->GetWindowProxy()); |
| |
69 NS_ENSURE_TRUE(global, NS_ERROR_FAILURE); |
| |
70 |
| |
71 JSAutoCompartment ac(cx, global); |
| |
72 |
| |
73 JS::Rooted<JS::Value> wrappedMessage(cx); |
| |
74 rv = nsContentUtils::WrapNative(cx, aMessage, &wrappedMessage); |
| |
75 NS_ENSURE_SUCCESS(rv, rv); |
| |
76 |
| |
77 return NotifySuccess(wrappedMessage, aAsync); |
| |
78 } |
| |
79 |
| |
80 nsresult |
| |
81 MobileMessageCallback::NotifyError(int32_t aError, bool aAsync) |
| |
82 { |
| |
83 nsAutoString errorStr; |
| |
84 switch (aError) { |
| |
85 case nsIMobileMessageCallback::NO_SIGNAL_ERROR: |
| |
86 errorStr = NS_LITERAL_STRING("NoSignalError"); |
| |
87 break; |
| |
88 case nsIMobileMessageCallback::NOT_FOUND_ERROR: |
| |
89 errorStr = NS_LITERAL_STRING("NotFoundError"); |
| |
90 break; |
| |
91 case nsIMobileMessageCallback::UNKNOWN_ERROR: |
| |
92 errorStr = NS_LITERAL_STRING("UnknownError"); |
| |
93 break; |
| |
94 case nsIMobileMessageCallback::INTERNAL_ERROR: |
| |
95 errorStr = NS_LITERAL_STRING("InternalError"); |
| |
96 break; |
| |
97 case nsIMobileMessageCallback::NO_SIM_CARD_ERROR: |
| |
98 errorStr = NS_LITERAL_STRING("NoSimCardError"); |
| |
99 break; |
| |
100 case nsIMobileMessageCallback::RADIO_DISABLED_ERROR: |
| |
101 errorStr = NS_LITERAL_STRING("RadioDisabledError"); |
| |
102 break; |
| |
103 case nsIMobileMessageCallback::INVALID_ADDRESS_ERROR: |
| |
104 errorStr = NS_LITERAL_STRING("InvalidAddressError"); |
| |
105 break; |
| |
106 case nsIMobileMessageCallback::FDN_CHECK_ERROR: |
| |
107 errorStr = NS_LITERAL_STRING("FdnCheckError"); |
| |
108 break; |
| |
109 case nsIMobileMessageCallback::NON_ACTIVE_SIM_CARD_ERROR: |
| |
110 errorStr = NS_LITERAL_STRING("NonActiveSimCardError"); |
| |
111 break; |
| |
112 case nsIMobileMessageCallback::STORAGE_FULL_ERROR: |
| |
113 errorStr = NS_LITERAL_STRING("StorageFullError"); |
| |
114 break; |
| |
115 case nsIMobileMessageCallback::SIM_NOT_MATCHED_ERROR: |
| |
116 errorStr = NS_LITERAL_STRING("SimNotMatchedError"); |
| |
117 break; |
| |
118 default: // SUCCESS_NO_ERROR is handled above. |
| |
119 MOZ_CRASH("Should never get here!"); |
| |
120 } |
| |
121 |
| |
122 if (aAsync) { |
| |
123 nsCOMPtr<nsIDOMRequestService> rs = |
| |
124 do_GetService(DOMREQUEST_SERVICE_CONTRACTID); |
| |
125 NS_ENSURE_TRUE(rs, NS_ERROR_FAILURE); |
| |
126 |
| |
127 return rs->FireErrorAsync(mDOMRequest, errorStr); |
| |
128 } |
| |
129 |
| |
130 mDOMRequest->FireError(errorStr); |
| |
131 return NS_OK; |
| |
132 } |
| |
133 |
| |
134 NS_IMETHODIMP |
| |
135 MobileMessageCallback::NotifyMessageSent(nsISupports *aMessage) |
| |
136 { |
| |
137 return NotifySuccess(aMessage); |
| |
138 } |
| |
139 |
| |
140 NS_IMETHODIMP |
| |
141 MobileMessageCallback::NotifySendMessageFailed(int32_t aError) |
| |
142 { |
| |
143 return NotifyError(aError); |
| |
144 } |
| |
145 |
| |
146 NS_IMETHODIMP |
| |
147 MobileMessageCallback::NotifyMessageGot(nsISupports *aMessage) |
| |
148 { |
| |
149 return NotifySuccess(aMessage); |
| |
150 } |
| |
151 |
| |
152 NS_IMETHODIMP |
| |
153 MobileMessageCallback::NotifyGetMessageFailed(int32_t aError) |
| |
154 { |
| |
155 return NotifyError(aError); |
| |
156 } |
| |
157 |
| |
158 NS_IMETHODIMP |
| |
159 MobileMessageCallback::NotifyMessageDeleted(bool *aDeleted, uint32_t aSize) |
| |
160 { |
| |
161 if (aSize == 1) { |
| |
162 AutoJSContext cx; |
| |
163 JS::Rooted<JS::Value> val(cx, aDeleted[0] ? JSVAL_TRUE : JSVAL_FALSE); |
| |
164 return NotifySuccess(val); |
| |
165 } |
| |
166 |
| |
167 nsresult rv; |
| |
168 nsIScriptContext* sc = mDOMRequest->GetContextForEventHandlers(&rv); |
| |
169 NS_ENSURE_SUCCESS(rv, rv); |
| |
170 NS_ENSURE_TRUE(sc, NS_ERROR_FAILURE); |
| |
171 |
| |
172 AutoPushJSContext cx(sc->GetNativeContext()); |
| |
173 NS_ENSURE_TRUE(cx, NS_ERROR_FAILURE); |
| |
174 |
| |
175 JS::Rooted<JSObject*> deleteArrayObj(cx, |
| |
176 JS_NewArrayObject(cx, aSize)); |
| |
177 for (uint32_t i = 0; i < aSize; i++) { |
| |
178 JS_SetElement(cx, deleteArrayObj, i, aDeleted[i]); |
| |
179 } |
| |
180 |
| |
181 JS::Rooted<JS::Value> deleteArrayVal(cx, JS::ObjectValue(*deleteArrayObj)); |
| |
182 return NotifySuccess(deleteArrayVal); |
| |
183 } |
| |
184 |
| |
185 NS_IMETHODIMP |
| |
186 MobileMessageCallback::NotifyDeleteMessageFailed(int32_t aError) |
| |
187 { |
| |
188 return NotifyError(aError); |
| |
189 } |
| |
190 |
| |
191 NS_IMETHODIMP |
| |
192 MobileMessageCallback::NotifyMessageMarkedRead(bool aRead) |
| |
193 { |
| |
194 AutoJSContext cx; |
| |
195 JS::Rooted<JS::Value> val(cx, aRead ? JSVAL_TRUE : JSVAL_FALSE); |
| |
196 return NotifySuccess(val); |
| |
197 } |
| |
198 |
| |
199 NS_IMETHODIMP |
| |
200 MobileMessageCallback::NotifyMarkMessageReadFailed(int32_t aError) |
| |
201 { |
| |
202 return NotifyError(aError); |
| |
203 } |
| |
204 |
| |
205 NS_IMETHODIMP |
| |
206 MobileMessageCallback::NotifySegmentInfoForTextGot(nsIDOMMozSmsSegmentInfo *aInfo) |
| |
207 { |
| |
208 return NotifySuccess(aInfo, true); |
| |
209 } |
| |
210 |
| |
211 NS_IMETHODIMP |
| |
212 MobileMessageCallback::NotifyGetSegmentInfoForTextFailed(int32_t aError) |
| |
213 { |
| |
214 return NotifyError(aError, true); |
| |
215 } |
| |
216 |
| |
217 NS_IMETHODIMP |
| |
218 MobileMessageCallback::NotifyGetSmscAddress(const nsAString& aSmscAddress) |
| |
219 { |
| |
220 AutoJSContext cx; |
| |
221 JSString* smsc = JS_NewUCStringCopyN(cx, |
| |
222 static_cast<const jschar *>(aSmscAddress.BeginReading()), |
| |
223 aSmscAddress.Length()); |
| |
224 |
| |
225 if (!smsc) { |
| |
226 return NotifyError(nsIMobileMessageCallback::INTERNAL_ERROR); |
| |
227 } |
| |
228 |
| |
229 JS::Rooted<JS::Value> val(cx, STRING_TO_JSVAL(smsc)); |
| |
230 return NotifySuccess(val); |
| |
231 } |
| |
232 |
| |
233 NS_IMETHODIMP |
| |
234 MobileMessageCallback::NotifyGetSmscAddressFailed(int32_t aError) |
| |
235 { |
| |
236 return NotifyError(aError); |
| |
237 } |
| |
238 |
| |
239 } // namesapce mobilemessage |
| |
240 } // namespace dom |
| |
241 } // namespace mozilla |