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 "SmsParent.h"
39 : #include "nsISmsService.h"
40 : #include "nsIObserverService.h"
41 : #include "mozilla/Services.h"
42 : #include "Constants.h"
43 : #include "nsIDOMSmsMessage.h"
44 : #include "mozilla/unused.h"
45 : #include "SmsMessage.h"
46 : #include "nsISmsDatabaseService.h"
47 : #include "SmsFilter.h"
48 :
49 : namespace mozilla {
50 : namespace dom {
51 : namespace sms {
52 :
53 : nsTArray<SmsParent*>* SmsParent::gSmsParents = nsnull;
54 :
55 0 : NS_IMPL_ISUPPORTS1(SmsParent, nsIObserver)
56 :
57 : /* static */ void
58 0 : SmsParent::GetAll(nsTArray<SmsParent*>& aArray)
59 : {
60 0 : if (!gSmsParents) {
61 0 : aArray.Clear();
62 0 : return;
63 : }
64 :
65 0 : aArray = *gSmsParents;
66 : }
67 :
68 0 : SmsParent::SmsParent()
69 : {
70 0 : if (!gSmsParents) {
71 0 : gSmsParents = new nsTArray<SmsParent*>();
72 : }
73 :
74 0 : gSmsParents->AppendElement(this);
75 :
76 0 : nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
77 0 : if (!obs) {
78 : return;
79 : }
80 :
81 0 : obs->AddObserver(this, kSmsReceivedObserverTopic, false);
82 0 : obs->AddObserver(this, kSmsSentObserverTopic, false);
83 0 : obs->AddObserver(this, kSmsDeliveredObserverTopic, false);
84 : }
85 :
86 : void
87 0 : SmsParent::ActorDestroy(ActorDestroyReason why)
88 : {
89 0 : nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
90 0 : if (!obs) {
91 : return;
92 : }
93 :
94 0 : obs->RemoveObserver(this, kSmsReceivedObserverTopic);
95 0 : obs->RemoveObserver(this, kSmsSentObserverTopic);
96 0 : obs->RemoveObserver(this, kSmsDeliveredObserverTopic);
97 :
98 0 : NS_ASSERTION(gSmsParents, "gSmsParents can't be null at that point!");
99 0 : gSmsParents->RemoveElement(this);
100 0 : if (gSmsParents->Length() == 0) {
101 0 : delete gSmsParents;
102 0 : gSmsParents = nsnull;
103 : }
104 : }
105 :
106 : NS_IMETHODIMP
107 0 : SmsParent::Observe(nsISupports* aSubject, const char* aTopic,
108 : const PRUnichar* aData)
109 : {
110 0 : if (!strcmp(aTopic, kSmsReceivedObserverTopic)) {
111 0 : nsCOMPtr<nsIDOMMozSmsMessage> message = do_QueryInterface(aSubject);
112 0 : if (!message) {
113 0 : NS_ERROR("Got a 'sms-received' topic without a valid message!");
114 0 : return NS_OK;
115 : }
116 :
117 0 : unused << SendNotifyReceivedMessage(static_cast<SmsMessage*>(message.get())->GetData());
118 0 : return NS_OK;
119 : }
120 :
121 0 : if (!strcmp(aTopic, kSmsSentObserverTopic)) {
122 0 : nsCOMPtr<nsIDOMMozSmsMessage> message = do_QueryInterface(aSubject);
123 0 : if (!message) {
124 0 : NS_ERROR("Got a 'sms-sent' topic without a valid message!");
125 0 : return NS_OK;
126 : }
127 :
128 0 : unused << SendNotifySentMessage(static_cast<SmsMessage*>(message.get())->GetData());
129 0 : return NS_OK;
130 : }
131 :
132 0 : if (!strcmp(aTopic, kSmsDeliveredObserverTopic)) {
133 0 : nsCOMPtr<nsIDOMMozSmsMessage> message = do_QueryInterface(aSubject);
134 0 : if (!message) {
135 0 : NS_ERROR("Got a 'sms-delivered' topic without a valid message!");
136 0 : return NS_OK;
137 : }
138 :
139 0 : unused << SendNotifyDeliveredMessage(static_cast<SmsMessage*>(message.get())->GetData());
140 0 : return NS_OK;
141 : }
142 :
143 0 : return NS_OK;
144 : }
145 :
146 : bool
147 0 : SmsParent::RecvHasSupport(bool* aHasSupport)
148 : {
149 0 : *aHasSupport = false;
150 :
151 0 : nsCOMPtr<nsISmsService> smsService = do_GetService(SMS_SERVICE_CONTRACTID);
152 0 : NS_ENSURE_TRUE(smsService, true);
153 :
154 0 : smsService->HasSupport(aHasSupport);
155 0 : return true;
156 : }
157 :
158 : bool
159 0 : SmsParent::RecvGetNumberOfMessagesForText(const nsString& aText, PRUint16* aResult)
160 : {
161 0 : *aResult = 0;
162 :
163 0 : nsCOMPtr<nsISmsService> smsService = do_GetService(SMS_SERVICE_CONTRACTID);
164 0 : NS_ENSURE_TRUE(smsService, true);
165 :
166 0 : smsService->GetNumberOfMessagesForText(aText, aResult);
167 0 : return true;
168 : }
169 :
170 : bool
171 0 : SmsParent::RecvSendMessage(const nsString& aNumber, const nsString& aMessage,
172 : const PRInt32& aRequestId, const PRUint64& aProcessId)
173 : {
174 0 : nsCOMPtr<nsISmsService> smsService = do_GetService(SMS_SERVICE_CONTRACTID);
175 0 : NS_ENSURE_TRUE(smsService, true);
176 :
177 0 : smsService->Send(aNumber, aMessage, aRequestId, aProcessId);
178 0 : return true;
179 : }
180 :
181 : bool
182 0 : SmsParent::RecvSaveReceivedMessage(const nsString& aSender,
183 : const nsString& aBody,
184 : const PRUint64& aDate, PRInt32* aId)
185 : {
186 0 : *aId = -1;
187 :
188 : nsCOMPtr<nsISmsDatabaseService> smsDBService =
189 0 : do_GetService(SMS_DATABASE_SERVICE_CONTRACTID);
190 0 : NS_ENSURE_TRUE(smsDBService, true);
191 :
192 0 : smsDBService->SaveReceivedMessage(aSender, aBody, aDate, aId);
193 0 : return true;
194 : }
195 :
196 : bool
197 0 : SmsParent::RecvSaveSentMessage(const nsString& aRecipient,
198 : const nsString& aBody,
199 : const PRUint64& aDate, PRInt32* aId)
200 : {
201 0 : *aId = -1;
202 :
203 : nsCOMPtr<nsISmsDatabaseService> smsDBService =
204 0 : do_GetService(SMS_DATABASE_SERVICE_CONTRACTID);
205 0 : NS_ENSURE_TRUE(smsDBService, true);
206 :
207 0 : smsDBService->SaveSentMessage(aRecipient, aBody, aDate, aId);
208 0 : return true;
209 : }
210 :
211 : bool
212 0 : SmsParent::RecvGetMessage(const PRInt32& aMessageId, const PRInt32& aRequestId,
213 : const PRUint64& aProcessId)
214 : {
215 : nsCOMPtr<nsISmsDatabaseService> smsDBService =
216 0 : do_GetService(SMS_DATABASE_SERVICE_CONTRACTID);
217 0 : NS_ENSURE_TRUE(smsDBService, true);
218 :
219 0 : smsDBService->GetMessageMoz(aMessageId, aRequestId, aProcessId);
220 0 : return true;
221 : }
222 :
223 : bool
224 0 : SmsParent::RecvDeleteMessage(const PRInt32& aMessageId, const PRInt32& aRequestId,
225 : const PRUint64& aProcessId)
226 : {
227 : nsCOMPtr<nsISmsDatabaseService> smsDBService =
228 0 : do_GetService(SMS_DATABASE_SERVICE_CONTRACTID);
229 0 : NS_ENSURE_TRUE(smsDBService, true);
230 :
231 0 : smsDBService->DeleteMessage(aMessageId, aRequestId, aProcessId);
232 0 : return true;
233 : }
234 :
235 : bool
236 0 : SmsParent::RecvCreateMessageList(const SmsFilterData& aFilter,
237 : const bool& aReverse,
238 : const PRInt32& aRequestId,
239 : const PRUint64& aProcessId)
240 : {
241 : nsCOMPtr<nsISmsDatabaseService> smsDBService =
242 0 : do_GetService(SMS_DATABASE_SERVICE_CONTRACTID);
243 0 : NS_ENSURE_TRUE(smsDBService, true);
244 :
245 0 : nsCOMPtr<nsIDOMMozSmsFilter> filter = new SmsFilter(aFilter);
246 0 : smsDBService->CreateMessageList(filter, aReverse, aRequestId, aProcessId);
247 :
248 0 : return true;
249 : }
250 :
251 : bool
252 0 : SmsParent::RecvGetNextMessageInList(const PRInt32& aListId,
253 : const PRInt32& aRequestId,
254 : const PRUint64& aProcessId)
255 : {
256 : nsCOMPtr<nsISmsDatabaseService> smsDBService =
257 0 : do_GetService(SMS_DATABASE_SERVICE_CONTRACTID);
258 0 : NS_ENSURE_TRUE(smsDBService, true);
259 :
260 0 : smsDBService->GetNextMessageInList(aListId, aRequestId, aProcessId);
261 :
262 0 : return true;
263 : }
264 :
265 : bool
266 0 : SmsParent::RecvClearMessageList(const PRInt32& aListId)
267 : {
268 : nsCOMPtr<nsISmsDatabaseService> smsDBService =
269 0 : do_GetService(SMS_DATABASE_SERVICE_CONTRACTID);
270 0 : NS_ENSURE_TRUE(smsDBService, true);
271 :
272 0 : smsDBService->ClearMessageList(aListId);
273 :
274 0 : return true;
275 : }
276 :
277 : } // namespace sms
278 : } // namespace dom
279 : } // namespace mozilla
|