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
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 2002
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Scott MacGregor <mscott@netscape.com>
24 : * Jens Bannmann <jens.b@web.de>
25 : * Alex Pakhotin <alexp@mozilla.com>
26 : *
27 : * Alternatively, the contents of this file may be used under the terms of
28 : * either the GNU General Public License Version 2 or later (the "GPL"), or
29 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 : * in which case the provisions of the GPL or the LGPL are applicable instead
31 : * of those above. If you wish to allow use of your version of this file only
32 : * under the terms of either the GPL or the LGPL, and not to allow others to
33 : * use your version of this file under the terms of the MPL, indicate your
34 : * decision by deleting the provisions above and replace them with the notice
35 : * and other provisions required by the GPL or the LGPL. If you do not delete
36 : * the provisions above, a recipient may use your version of this file under
37 : * the terms of any one of the MPL, the GPL or the LGPL.
38 : *
39 : * ***** END LICENSE BLOCK ***** */
40 :
41 : #include "mozilla/dom/ContentChild.h"
42 : #include "nsXULAppAPI.h"
43 :
44 : #include "nsAlertsService.h"
45 :
46 : #ifdef ANDROID
47 : #include "AndroidBridge.h"
48 : #else
49 :
50 : #include "nsISupportsArray.h"
51 : #include "nsXPCOM.h"
52 : #include "nsISupportsPrimitives.h"
53 : #include "nsIServiceManager.h"
54 : #include "nsIDOMWindow.h"
55 : #include "nsIWindowWatcher.h"
56 : #include "nsPromiseFlatString.h"
57 : #include "nsToolkitCompsCID.h"
58 : #include "mozilla/LookAndFeel.h"
59 :
60 : #define ALERT_CHROME_URL "chrome://global/content/alerts/alert.xul"
61 :
62 : #endif // !ANDROID
63 :
64 : using namespace mozilla;
65 :
66 : using mozilla::dom::ContentChild;
67 :
68 0 : NS_IMPL_THREADSAFE_ISUPPORTS2(nsAlertsService, nsIAlertsService, nsIAlertsProgressListener)
69 :
70 0 : nsAlertsService::nsAlertsService()
71 : {
72 0 : }
73 :
74 0 : nsAlertsService::~nsAlertsService()
75 0 : {}
76 :
77 0 : bool nsAlertsService::ShouldShowAlert()
78 : {
79 0 : bool result = true;
80 :
81 : #ifdef XP_WIN
82 : HMODULE shellDLL = ::LoadLibraryW(L"shell32.dll");
83 : if (!shellDLL)
84 : return result;
85 :
86 : SHQueryUserNotificationStatePtr pSHQueryUserNotificationState =
87 : (SHQueryUserNotificationStatePtr) ::GetProcAddress(shellDLL, "SHQueryUserNotificationState");
88 :
89 : if (pSHQueryUserNotificationState) {
90 : MOZ_QUERY_USER_NOTIFICATION_STATE qstate;
91 : if (SUCCEEDED(pSHQueryUserNotificationState(&qstate))) {
92 : if (qstate != QUNS_ACCEPTS_NOTIFICATIONS) {
93 : result = false;
94 : }
95 : }
96 : }
97 :
98 : ::FreeLibrary(shellDLL);
99 : #endif
100 :
101 0 : return result;
102 : }
103 :
104 0 : NS_IMETHODIMP nsAlertsService::ShowAlertNotification(const nsAString & aImageUrl, const nsAString & aAlertTitle,
105 : const nsAString & aAlertText, bool aAlertTextClickable,
106 : const nsAString & aAlertCookie,
107 : nsIObserver * aAlertListener,
108 : const nsAString & aAlertName)
109 : {
110 0 : if (XRE_GetProcessType() == GeckoProcessType_Content) {
111 0 : ContentChild* cpc = ContentChild::GetSingleton();
112 :
113 0 : if (aAlertListener)
114 0 : cpc->AddRemoteAlertObserver(PromiseFlatString(aAlertCookie), aAlertListener);
115 :
116 0 : cpc->SendShowAlertNotification(nsAutoString(aImageUrl),
117 0 : nsAutoString(aAlertTitle),
118 0 : nsAutoString(aAlertText),
119 : aAlertTextClickable,
120 0 : nsAutoString(aAlertCookie),
121 0 : nsAutoString(aAlertName));
122 0 : return NS_OK;
123 : }
124 :
125 : #ifdef ANDROID
126 : mozilla::AndroidBridge::Bridge()->ShowAlertNotification(aImageUrl, aAlertTitle, aAlertText, aAlertCookie,
127 : aAlertListener, aAlertName);
128 : return NS_OK;
129 : #else
130 : // Check if there is an optional service that handles system-level notifications
131 0 : nsCOMPtr<nsIAlertsService> sysAlerts(do_GetService(NS_SYSTEMALERTSERVICE_CONTRACTID));
132 : nsresult rv;
133 0 : if (sysAlerts) {
134 0 : rv = sysAlerts->ShowAlertNotification(aImageUrl, aAlertTitle, aAlertText, aAlertTextClickable,
135 0 : aAlertCookie, aAlertListener, aAlertName);
136 0 : if (NS_SUCCEEDED(rv))
137 0 : return rv;
138 : }
139 :
140 0 : if (!ShouldShowAlert()) {
141 : // Do not display the alert. Instead call alertfinished and get out.
142 0 : if (aAlertListener)
143 0 : aAlertListener->Observe(NULL, "alertfinished", PromiseFlatString(aAlertCookie).get());
144 0 : return NS_OK;
145 : }
146 :
147 0 : nsCOMPtr<nsIWindowWatcher> wwatch(do_GetService(NS_WINDOWWATCHER_CONTRACTID));
148 0 : nsCOMPtr<nsIDOMWindow> newWindow;
149 :
150 0 : nsCOMPtr<nsISupportsArray> argsArray;
151 0 : rv = NS_NewISupportsArray(getter_AddRefs(argsArray));
152 0 : NS_ENSURE_SUCCESS(rv, rv);
153 :
154 : // create scriptable versions of our strings that we can store in our nsISupportsArray....
155 0 : nsCOMPtr<nsISupportsString> scriptableImageUrl (do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID));
156 0 : NS_ENSURE_TRUE(scriptableImageUrl, NS_ERROR_FAILURE);
157 :
158 0 : scriptableImageUrl->SetData(aImageUrl);
159 0 : rv = argsArray->AppendElement(scriptableImageUrl);
160 0 : NS_ENSURE_SUCCESS(rv, rv);
161 :
162 0 : nsCOMPtr<nsISupportsString> scriptableAlertTitle (do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID));
163 0 : NS_ENSURE_TRUE(scriptableAlertTitle, NS_ERROR_FAILURE);
164 :
165 0 : scriptableAlertTitle->SetData(aAlertTitle);
166 0 : rv = argsArray->AppendElement(scriptableAlertTitle);
167 0 : NS_ENSURE_SUCCESS(rv, rv);
168 :
169 0 : nsCOMPtr<nsISupportsString> scriptableAlertText (do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID));
170 0 : NS_ENSURE_TRUE(scriptableAlertText, NS_ERROR_FAILURE);
171 :
172 0 : scriptableAlertText->SetData(aAlertText);
173 0 : rv = argsArray->AppendElement(scriptableAlertText);
174 0 : NS_ENSURE_SUCCESS(rv, rv);
175 :
176 0 : nsCOMPtr<nsISupportsPRBool> scriptableIsClickable (do_CreateInstance(NS_SUPPORTS_PRBOOL_CONTRACTID));
177 0 : NS_ENSURE_TRUE(scriptableIsClickable, NS_ERROR_FAILURE);
178 :
179 0 : scriptableIsClickable->SetData(aAlertTextClickable);
180 0 : rv = argsArray->AppendElement(scriptableIsClickable);
181 0 : NS_ENSURE_SUCCESS(rv, rv);
182 :
183 0 : nsCOMPtr<nsISupportsString> scriptableAlertCookie (do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID));
184 0 : NS_ENSURE_TRUE(scriptableAlertCookie, NS_ERROR_FAILURE);
185 :
186 0 : scriptableAlertCookie->SetData(aAlertCookie);
187 0 : rv = argsArray->AppendElement(scriptableAlertCookie);
188 0 : NS_ENSURE_SUCCESS(rv, rv);
189 :
190 0 : nsCOMPtr<nsISupportsPRInt32> scriptableOrigin (do_CreateInstance(NS_SUPPORTS_PRINT32_CONTRACTID));
191 0 : NS_ENSURE_TRUE(scriptableOrigin, NS_ERROR_FAILURE);
192 :
193 : PRInt32 origin =
194 0 : LookAndFeel::GetInt(LookAndFeel::eIntID_AlertNotificationOrigin);
195 0 : scriptableOrigin->SetData(origin);
196 :
197 0 : rv = argsArray->AppendElement(scriptableOrigin);
198 0 : NS_ENSURE_SUCCESS(rv, rv);
199 :
200 0 : if (aAlertListener)
201 : {
202 0 : nsCOMPtr<nsISupportsInterfacePointer> ifptr = do_CreateInstance(NS_SUPPORTS_INTERFACE_POINTER_CONTRACTID, &rv);
203 0 : NS_ENSURE_SUCCESS(rv, rv);
204 :
205 0 : nsCOMPtr<nsISupports> iSupports (do_QueryInterface(aAlertListener));
206 0 : ifptr->SetData(iSupports);
207 0 : ifptr->SetDataIID(&NS_GET_IID(nsIObserver));
208 0 : rv = argsArray->AppendElement(ifptr);
209 0 : NS_ENSURE_SUCCESS(rv, rv);
210 : }
211 :
212 0 : rv = wwatch->OpenWindow(0, ALERT_CHROME_URL, "_blank",
213 : "chrome,dialog=yes,titlebar=no,popup=yes", argsArray,
214 0 : getter_AddRefs(newWindow));
215 0 : return rv;
216 : #endif // !ANDROID
217 : }
218 :
219 0 : NS_IMETHODIMP nsAlertsService::OnProgress(const nsAString & aAlertName,
220 : PRInt64 aProgress,
221 : PRInt64 aProgressMax,
222 : const nsAString & aAlertText)
223 : {
224 : #ifdef ANDROID
225 : mozilla::AndroidBridge::Bridge()->AlertsProgressListener_OnProgress(aAlertName, aProgress, aProgressMax, aAlertText);
226 : return NS_OK;
227 : #else
228 0 : return NS_ERROR_NOT_IMPLEMENTED;
229 : #endif // !ANDROID
230 : }
231 :
232 0 : NS_IMETHODIMP nsAlertsService::OnCancel(const nsAString & aAlertName)
233 : {
234 : #ifdef ANDROID
235 : mozilla::AndroidBridge::Bridge()->AlertsProgressListener_OnCancel(aAlertName);
236 : return NS_OK;
237 : #else
238 0 : return NS_ERROR_NOT_IMPLEMENTED;
239 : #endif // !ANDROID
240 : }
|