1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is Mozilla Foundation
18 : * Portions created by the Initial Developer are Copyright (C) 2011
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Mounir Lamouri <mounir.lamouri@mozilla.com> (Original Author)
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either of the GNU General Public License Version 2 or later (the "GPL"),
26 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "SmsRequestManager.h"
39 : #include "nsIDOMSmsMessage.h"
40 : #include "nsDOMEvent.h"
41 : #include "SmsCursor.h"
42 : #include "SmsManager.h"
43 :
44 : /**
45 : * We have to use macros here because our leak analysis tool things we are
46 : * leaking strings when we have |static const nsString|. Sad :(
47 : */
48 : #define SUCCESS_EVENT_NAME NS_LITERAL_STRING("success")
49 : #define ERROR_EVENT_NAME NS_LITERAL_STRING("error")
50 :
51 : namespace mozilla {
52 : namespace dom {
53 : namespace sms {
54 :
55 0 : NS_IMPL_ISUPPORTS1(SmsRequestManager, nsISmsRequestManager)
56 :
57 : NS_IMETHODIMP
58 0 : SmsRequestManager::AddRequest(nsIDOMMozSmsRequest* aRequest,
59 : PRInt32* aRequestId)
60 : {
61 : // TODO: merge with CreateRequest
62 0 : PRInt32 size = mRequests.Count();
63 :
64 : // Look for empty slots.
65 0 : for (PRInt32 i=0; i<size; ++i) {
66 0 : if (mRequests[i]) {
67 0 : continue;
68 : }
69 :
70 0 : mRequests.ReplaceObjectAt(aRequest, i);
71 0 : *aRequestId = i;
72 0 : return NS_OK;
73 : }
74 :
75 0 : mRequests.AppendObject(aRequest);
76 0 : *aRequestId = size;
77 0 : return NS_OK;
78 : }
79 :
80 :
81 : NS_IMETHODIMP
82 0 : SmsRequestManager::CreateRequest(nsIDOMMozSmsManager* aManager,
83 : nsIDOMMozSmsRequest** aRequest,
84 : PRInt32* aRequestId)
85 : {
86 : nsCOMPtr<nsIDOMMozSmsRequest> request =
87 0 : new SmsRequest(static_cast<SmsManager*>(aManager));
88 :
89 0 : PRInt32 size = mRequests.Count();
90 :
91 : // Look for empty slots.
92 0 : for (PRInt32 i=0; i<size; ++i) {
93 0 : if (mRequests[i]) {
94 0 : continue;
95 : }
96 :
97 0 : mRequests.ReplaceObjectAt(request, i);
98 0 : NS_ADDREF(*aRequest = request);
99 0 : *aRequestId = i;
100 0 : return NS_OK;
101 : }
102 :
103 0 : mRequests.AppendObject(request);
104 0 : NS_ADDREF(*aRequest = request);
105 0 : *aRequestId = size;
106 0 : return NS_OK;
107 : }
108 :
109 : nsresult
110 0 : SmsRequestManager::DispatchTrustedEventToRequest(const nsAString& aEventName,
111 : nsIDOMMozSmsRequest* aRequest)
112 : {
113 0 : nsRefPtr<nsDOMEvent> event = new nsDOMEvent(nsnull, nsnull);
114 0 : nsresult rv = event->InitEvent(aEventName, false, false);
115 0 : NS_ENSURE_SUCCESS(rv, rv);
116 :
117 0 : rv = event->SetTrusted(PR_TRUE);
118 0 : NS_ENSURE_SUCCESS(rv, rv);
119 :
120 : bool dummy;
121 0 : return aRequest->DispatchEvent(event, &dummy);
122 : }
123 :
124 : SmsRequest*
125 0 : SmsRequestManager::GetRequest(PRInt32 aRequestId)
126 : {
127 0 : NS_ASSERTION(mRequests.Count() > aRequestId && mRequests[aRequestId],
128 : "Got an invalid request id or it has been already deleted!");
129 :
130 : // It's safe to use the static_cast here given that we did call
131 : // |new SmsRequest()|.
132 0 : return static_cast<SmsRequest*>(mRequests[aRequestId]);
133 : }
134 :
135 : template <class T>
136 : nsresult
137 0 : SmsRequestManager::NotifySuccess(PRInt32 aRequestId, T aParam)
138 : {
139 0 : SmsRequest* request = GetRequest(aRequestId);
140 0 : request->SetSuccess(aParam);
141 :
142 0 : nsresult rv = DispatchTrustedEventToRequest(SUCCESS_EVENT_NAME, request);
143 :
144 0 : mRequests.ReplaceObjectAt(nsnull, aRequestId);
145 0 : return rv;
146 : }
147 :
148 : nsresult
149 0 : SmsRequestManager::NotifyError(PRInt32 aRequestId, PRInt32 aError)
150 : {
151 0 : SmsRequest* request = GetRequest(aRequestId);
152 0 : request->SetError(aError);
153 :
154 0 : nsresult rv = DispatchTrustedEventToRequest(ERROR_EVENT_NAME, request);
155 :
156 0 : mRequests.ReplaceObjectAt(nsnull, aRequestId);
157 0 : return rv;
158 : }
159 :
160 : NS_IMETHODIMP
161 0 : SmsRequestManager::NotifySmsSent(PRInt32 aRequestId, nsIDOMMozSmsMessage* aMessage)
162 : {
163 0 : return NotifySuccess<nsIDOMMozSmsMessage*>(aRequestId, aMessage);
164 : }
165 :
166 : NS_IMETHODIMP
167 0 : SmsRequestManager::NotifySmsSendFailed(PRInt32 aRequestId, PRInt32 aError)
168 : {
169 0 : return NotifyError(aRequestId, aError);
170 : }
171 :
172 : NS_IMETHODIMP
173 0 : SmsRequestManager::NotifyGotSms(PRInt32 aRequestId, nsIDOMMozSmsMessage* aMessage)
174 : {
175 0 : return NotifySuccess<nsIDOMMozSmsMessage*>(aRequestId, aMessage);
176 : }
177 :
178 : NS_IMETHODIMP
179 0 : SmsRequestManager::NotifyGetSmsFailed(PRInt32 aRequestId, PRInt32 aError)
180 : {
181 0 : return NotifyError(aRequestId, aError);
182 : }
183 :
184 : NS_IMETHODIMP
185 0 : SmsRequestManager::NotifySmsDeleted(PRInt32 aRequestId, bool aDeleted)
186 : {
187 0 : return NotifySuccess<bool>(aRequestId, aDeleted);
188 : }
189 :
190 : NS_IMETHODIMP
191 0 : SmsRequestManager::NotifySmsDeleteFailed(PRInt32 aRequestId, PRInt32 aError)
192 : {
193 0 : return NotifyError(aRequestId, aError);
194 : }
195 :
196 : NS_IMETHODIMP
197 0 : SmsRequestManager::NotifyNoMessageInList(PRInt32 aRequestId)
198 : {
199 0 : SmsRequest* request = GetRequest(aRequestId);
200 :
201 0 : nsCOMPtr<nsIDOMMozSmsCursor> cursor = request->GetCursor();
202 0 : if (!cursor) {
203 0 : cursor = new SmsCursor();
204 : } else {
205 0 : static_cast<SmsCursor*>(cursor.get())->Disconnect();
206 : }
207 :
208 0 : return NotifySuccess<nsIDOMMozSmsCursor*>(aRequestId, cursor);
209 : }
210 :
211 : NS_IMETHODIMP
212 0 : SmsRequestManager::NotifyCreateMessageList(PRInt32 aRequestId, PRInt32 aListId,
213 : nsIDOMMozSmsMessage* aMessage)
214 : {
215 0 : SmsRequest* request = GetRequest(aRequestId);
216 :
217 0 : nsCOMPtr<SmsCursor> cursor = new SmsCursor(aListId, request);
218 0 : cursor->SetMessage(aMessage);
219 :
220 0 : return NotifySuccess<nsIDOMMozSmsCursor*>(aRequestId, cursor);
221 : }
222 :
223 : NS_IMETHODIMP
224 0 : SmsRequestManager::NotifyGotNextMessage(PRInt32 aRequestId, nsIDOMMozSmsMessage* aMessage)
225 : {
226 0 : SmsRequest* request = GetRequest(aRequestId);
227 :
228 0 : nsCOMPtr<SmsCursor> cursor = static_cast<SmsCursor*>(request->GetCursor());
229 0 : NS_ASSERTION(cursor, "Request should have an cursor in that case!");
230 0 : cursor->SetMessage(aMessage);
231 :
232 0 : return NotifySuccess<nsIDOMMozSmsCursor*>(aRequestId, cursor);
233 : }
234 :
235 : NS_IMETHODIMP
236 0 : SmsRequestManager::NotifyReadMessageListFailed(PRInt32 aRequestId, PRInt32 aError)
237 : {
238 0 : SmsRequest* request = GetRequest(aRequestId);
239 :
240 0 : nsCOMPtr<nsIDOMMozSmsCursor> cursor = request->GetCursor();
241 0 : if (cursor) {
242 0 : static_cast<SmsCursor*>(cursor.get())->Disconnect();
243 : }
244 :
245 0 : return NotifyError(aRequestId, aError);
246 : }
247 :
248 : } // namespace sms
249 : } // namespace dom
250 : } // namespace mozilla
|