toolkit/components/alerts/nsAlertsService.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode:nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "mozilla/dom/ContentChild.h"
michael@0 7 #include "mozilla/dom/PermissionMessageUtils.h"
michael@0 8 #include "nsXULAppAPI.h"
michael@0 9
michael@0 10 #include "nsAlertsService.h"
michael@0 11
michael@0 12 #ifdef MOZ_WIDGET_ANDROID
michael@0 13 #include "AndroidBridge.h"
michael@0 14 using namespace mozilla::widget::android;
michael@0 15 #else
michael@0 16
michael@0 17 #include "nsXPCOM.h"
michael@0 18 #include "nsIServiceManager.h"
michael@0 19 #include "nsIDOMWindow.h"
michael@0 20 #include "nsPromiseFlatString.h"
michael@0 21 #include "nsToolkitCompsCID.h"
michael@0 22
michael@0 23 #endif // !MOZ_WIDGET_ANDROID
michael@0 24
michael@0 25 using namespace mozilla;
michael@0 26
michael@0 27 using mozilla::dom::ContentChild;
michael@0 28
michael@0 29 NS_IMPL_ISUPPORTS(nsAlertsService, nsIAlertsService, nsIAlertsProgressListener)
michael@0 30
michael@0 31 nsAlertsService::nsAlertsService()
michael@0 32 {
michael@0 33 }
michael@0 34
michael@0 35 nsAlertsService::~nsAlertsService()
michael@0 36 {}
michael@0 37
michael@0 38 bool nsAlertsService::ShouldShowAlert()
michael@0 39 {
michael@0 40 bool result = true;
michael@0 41
michael@0 42 #ifdef XP_WIN
michael@0 43 HMODULE shellDLL = ::LoadLibraryW(L"shell32.dll");
michael@0 44 if (!shellDLL)
michael@0 45 return result;
michael@0 46
michael@0 47 SHQueryUserNotificationStatePtr pSHQueryUserNotificationState =
michael@0 48 (SHQueryUserNotificationStatePtr) ::GetProcAddress(shellDLL, "SHQueryUserNotificationState");
michael@0 49
michael@0 50 if (pSHQueryUserNotificationState) {
michael@0 51 MOZ_QUERY_USER_NOTIFICATION_STATE qstate;
michael@0 52 if (SUCCEEDED(pSHQueryUserNotificationState(&qstate))) {
michael@0 53 if (qstate != QUNS_ACCEPTS_NOTIFICATIONS) {
michael@0 54 result = false;
michael@0 55 }
michael@0 56 }
michael@0 57 }
michael@0 58
michael@0 59 ::FreeLibrary(shellDLL);
michael@0 60 #endif
michael@0 61
michael@0 62 return result;
michael@0 63 }
michael@0 64
michael@0 65 NS_IMETHODIMP nsAlertsService::ShowAlertNotification(const nsAString & aImageUrl, const nsAString & aAlertTitle,
michael@0 66 const nsAString & aAlertText, bool aAlertTextClickable,
michael@0 67 const nsAString & aAlertCookie,
michael@0 68 nsIObserver * aAlertListener,
michael@0 69 const nsAString & aAlertName,
michael@0 70 const nsAString & aBidi,
michael@0 71 const nsAString & aLang,
michael@0 72 nsIPrincipal * aPrincipal)
michael@0 73 {
michael@0 74 if (XRE_GetProcessType() == GeckoProcessType_Content) {
michael@0 75 ContentChild* cpc = ContentChild::GetSingleton();
michael@0 76
michael@0 77 if (aAlertListener)
michael@0 78 cpc->AddRemoteAlertObserver(PromiseFlatString(aAlertCookie), aAlertListener);
michael@0 79
michael@0 80 cpc->SendShowAlertNotification(PromiseFlatString(aImageUrl),
michael@0 81 PromiseFlatString(aAlertTitle),
michael@0 82 PromiseFlatString(aAlertText),
michael@0 83 aAlertTextClickable,
michael@0 84 PromiseFlatString(aAlertCookie),
michael@0 85 PromiseFlatString(aAlertName),
michael@0 86 PromiseFlatString(aBidi),
michael@0 87 PromiseFlatString(aLang),
michael@0 88 IPC::Principal(aPrincipal));
michael@0 89 return NS_OK;
michael@0 90 }
michael@0 91
michael@0 92 #ifdef MOZ_WIDGET_ANDROID
michael@0 93 mozilla::AndroidBridge::Bridge()->ShowAlertNotification(aImageUrl, aAlertTitle, aAlertText, aAlertCookie,
michael@0 94 aAlertListener, aAlertName);
michael@0 95 return NS_OK;
michael@0 96 #else
michael@0 97 // Check if there is an optional service that handles system-level notifications
michael@0 98 nsCOMPtr<nsIAlertsService> sysAlerts(do_GetService(NS_SYSTEMALERTSERVICE_CONTRACTID));
michael@0 99 nsresult rv;
michael@0 100 if (sysAlerts) {
michael@0 101 return sysAlerts->ShowAlertNotification(aImageUrl, aAlertTitle, aAlertText, aAlertTextClickable,
michael@0 102 aAlertCookie, aAlertListener, aAlertName,
michael@0 103 aBidi, aLang, IPC::Principal(aPrincipal));
michael@0 104 }
michael@0 105
michael@0 106 if (!ShouldShowAlert()) {
michael@0 107 // Do not display the alert. Instead call alertfinished and get out.
michael@0 108 if (aAlertListener)
michael@0 109 aAlertListener->Observe(nullptr, "alertfinished", PromiseFlatString(aAlertCookie).get());
michael@0 110 return NS_OK;
michael@0 111 }
michael@0 112
michael@0 113 // Use XUL notifications as a fallback if above methods have failed.
michael@0 114 rv = mXULAlerts.ShowAlertNotification(aImageUrl, aAlertTitle, aAlertText, aAlertTextClickable,
michael@0 115 aAlertCookie, aAlertListener, aAlertName,
michael@0 116 aBidi, aLang);
michael@0 117 return rv;
michael@0 118 #endif // !MOZ_WIDGET_ANDROID
michael@0 119 }
michael@0 120
michael@0 121 NS_IMETHODIMP nsAlertsService::CloseAlert(const nsAString& aAlertName,
michael@0 122 nsIPrincipal* aPrincipal)
michael@0 123 {
michael@0 124 if (XRE_GetProcessType() == GeckoProcessType_Content) {
michael@0 125 ContentChild* cpc = ContentChild::GetSingleton();
michael@0 126 cpc->SendCloseAlert(nsAutoString(aAlertName), IPC::Principal(aPrincipal));
michael@0 127 return NS_OK;
michael@0 128 }
michael@0 129
michael@0 130 #ifdef MOZ_WIDGET_ANDROID
michael@0 131 mozilla::widget::android::GeckoAppShell::CloseNotification(aAlertName);
michael@0 132 return NS_OK;
michael@0 133 #else
michael@0 134
michael@0 135 // Try the system notification service.
michael@0 136 nsCOMPtr<nsIAlertsService> sysAlerts(do_GetService(NS_SYSTEMALERTSERVICE_CONTRACTID));
michael@0 137 if (sysAlerts) {
michael@0 138 return sysAlerts->CloseAlert(aAlertName, nullptr);
michael@0 139 }
michael@0 140
michael@0 141 return mXULAlerts.CloseAlert(aAlertName);
michael@0 142 #endif // !MOZ_WIDGET_ANDROID
michael@0 143 }
michael@0 144
michael@0 145
michael@0 146 NS_IMETHODIMP nsAlertsService::OnProgress(const nsAString & aAlertName,
michael@0 147 int64_t aProgress,
michael@0 148 int64_t aProgressMax,
michael@0 149 const nsAString & aAlertText)
michael@0 150 {
michael@0 151 #ifdef MOZ_WIDGET_ANDROID
michael@0 152 mozilla::widget::android::GeckoAppShell::AlertsProgressListener_OnProgress(aAlertName,
michael@0 153 aProgress, aProgressMax,
michael@0 154 aAlertText);
michael@0 155 return NS_OK;
michael@0 156 #else
michael@0 157 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 158 #endif // !MOZ_WIDGET_ANDROID
michael@0 159 }
michael@0 160
michael@0 161 NS_IMETHODIMP nsAlertsService::OnCancel(const nsAString & aAlertName)
michael@0 162 {
michael@0 163 #ifdef MOZ_WIDGET_ANDROID
michael@0 164 mozilla::widget::android::GeckoAppShell::CloseNotification(aAlertName);
michael@0 165 return NS_OK;
michael@0 166 #else
michael@0 167 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 168 #endif // !MOZ_WIDGET_ANDROID
michael@0 169 }

mercurial