2003-11-27 03:54:33 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode:nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2003-11-27 03:54:33 +03:00
|
|
|
|
2010-09-15 20:44:57 +04:00
|
|
|
#include "mozilla/dom/ContentChild.h"
|
2013-11-12 03:56:21 +04:00
|
|
|
#include "mozilla/dom/PermissionMessageUtils.h"
|
2015-11-14 08:22:59 +03:00
|
|
|
#include "mozilla/Telemetry.h"
|
2010-09-15 20:44:57 +04:00
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
|
2003-11-27 03:54:33 +03:00
|
|
|
#include "nsAlertsService.h"
|
2010-06-15 06:17:37 +04:00
|
|
|
|
2012-07-27 02:25:02 +04:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2010-06-15 06:17:37 +04:00
|
|
|
#include "AndroidBridge.h"
|
|
|
|
#else
|
|
|
|
|
2003-11-27 03:54:33 +03:00
|
|
|
#include "nsXPCOM.h"
|
|
|
|
#include "nsIServiceManager.h"
|
2011-07-15 14:31:34 +04:00
|
|
|
#include "nsIDOMWindow.h"
|
2011-08-09 14:11:48 +04:00
|
|
|
#include "nsPromiseFlatString.h"
|
2009-01-08 11:12:16 +03:00
|
|
|
#include "nsToolkitCompsCID.h"
|
2003-11-27 03:54:33 +03:00
|
|
|
|
2012-07-27 02:25:02 +04:00
|
|
|
#endif // !MOZ_WIDGET_ANDROID
|
2010-06-15 06:17:37 +04:00
|
|
|
|
2011-09-09 06:27:13 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2011-07-15 14:31:34 +04:00
|
|
|
using mozilla::dom::ContentChild;
|
|
|
|
|
2015-10-19 06:04:56 +03:00
|
|
|
NS_IMPL_ISUPPORTS(nsAlertsService, nsIAlertsService, nsIAlertsDoNotDisturb, nsIAlertsProgressListener)
|
2003-11-27 03:54:33 +03:00
|
|
|
|
|
|
|
nsAlertsService::nsAlertsService()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAlertsService::~nsAlertsService()
|
|
|
|
{}
|
|
|
|
|
2012-03-08 01:09:04 +04:00
|
|
|
bool nsAlertsService::ShouldShowAlert()
|
|
|
|
{
|
|
|
|
bool result = true;
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
HMODULE shellDLL = ::LoadLibraryW(L"shell32.dll");
|
|
|
|
if (!shellDLL)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
SHQueryUserNotificationStatePtr pSHQueryUserNotificationState =
|
|
|
|
(SHQueryUserNotificationStatePtr) ::GetProcAddress(shellDLL, "SHQueryUserNotificationState");
|
|
|
|
|
|
|
|
if (pSHQueryUserNotificationState) {
|
|
|
|
MOZ_QUERY_USER_NOTIFICATION_STATE qstate;
|
|
|
|
if (SUCCEEDED(pSHQueryUserNotificationState(&qstate))) {
|
|
|
|
if (qstate != QUNS_ACCEPTS_NOTIFICATIONS) {
|
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
::FreeLibrary(shellDLL);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2005-03-15 22:41:41 +03:00
|
|
|
NS_IMETHODIMP nsAlertsService::ShowAlertNotification(const nsAString & aImageUrl, const nsAString & aAlertTitle,
|
2011-09-29 10:19:26 +04:00
|
|
|
const nsAString & aAlertText, bool aAlertTextClickable,
|
2005-03-15 22:41:41 +03:00
|
|
|
const nsAString & aAlertCookie,
|
2007-09-22 06:08:28 +04:00
|
|
|
nsIObserver * aAlertListener,
|
2013-03-18 17:24:53 +04:00
|
|
|
const nsAString & aAlertName,
|
|
|
|
const nsAString & aBidi,
|
2013-11-12 03:56:21 +04:00
|
|
|
const nsAString & aLang,
|
2014-08-21 04:56:12 +04:00
|
|
|
const nsAString & aData,
|
2014-11-28 22:08:29 +03:00
|
|
|
nsIPrincipal * aPrincipal,
|
|
|
|
bool aInPrivateBrowsing)
|
2003-11-27 03:54:33 +03:00
|
|
|
{
|
2015-12-31 22:04:24 +03:00
|
|
|
nsCOMPtr<nsIAlertNotification> alert =
|
|
|
|
do_CreateInstance(ALERT_NOTIFICATION_CONTRACTID);
|
|
|
|
NS_ENSURE_TRUE(alert, NS_ERROR_FAILURE);
|
|
|
|
nsresult rv = alert->Init(aAlertName, aImageUrl, aAlertTitle,
|
|
|
|
aAlertText, aAlertTextClickable,
|
|
|
|
aAlertCookie, aBidi, aLang, aData,
|
|
|
|
aPrincipal, aInPrivateBrowsing);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
return ShowAlert(alert, aAlertListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsAlertsService::ShowAlert(nsIAlertNotification * aAlert,
|
|
|
|
nsIObserver * aAlertListener)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aAlert);
|
|
|
|
|
|
|
|
nsAutoString cookie;
|
|
|
|
nsresult rv = aAlert->GetCookie(cookie);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2015-07-04 04:29:00 +03:00
|
|
|
if (XRE_IsContentProcess()) {
|
2010-09-15 20:44:57 +04:00
|
|
|
ContentChild* cpc = ContentChild::GetSingleton();
|
|
|
|
|
|
|
|
if (aAlertListener)
|
2015-12-31 22:04:24 +03:00
|
|
|
cpc->AddRemoteAlertObserver(cookie, aAlertListener);
|
|
|
|
|
|
|
|
cpc->SendShowAlert(aAlert);
|
2010-09-15 20:44:57 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-12-31 22:04:24 +03:00
|
|
|
nsAutoString imageUrl;
|
|
|
|
rv = aAlert->GetImageURL(imageUrl);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsAutoString title;
|
|
|
|
rv = aAlert->GetTitle(title);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsAutoString text;
|
|
|
|
rv = aAlert->GetText(text);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsAutoString name;
|
|
|
|
rv = aAlert->GetName(name);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
|
|
|
rv = aAlert->GetPrincipal(getter_AddRefs(principal));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-07-27 02:25:02 +04:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2015-12-31 22:04:24 +03:00
|
|
|
mozilla::AndroidBridge::Bridge()->ShowAlertNotification(imageUrl, title, text, cookie,
|
|
|
|
aAlertListener, name, principal);
|
2010-06-15 06:17:37 +04:00
|
|
|
return NS_OK;
|
|
|
|
#else
|
2009-01-08 11:12:16 +03:00
|
|
|
// Check if there is an optional service that handles system-level notifications
|
|
|
|
nsCOMPtr<nsIAlertsService> sysAlerts(do_GetService(NS_SYSTEMALERTSERVICE_CONTRACTID));
|
2009-05-04 12:11:11 +04:00
|
|
|
if (sysAlerts) {
|
2015-12-31 22:04:24 +03:00
|
|
|
rv = sysAlerts->ShowAlert(aAlert, aAlertListener);
|
2014-11-14 06:04:00 +03:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
return NS_OK;
|
2009-05-04 12:11:11 +04:00
|
|
|
}
|
2009-01-08 11:12:16 +03:00
|
|
|
|
2012-03-08 01:09:04 +04:00
|
|
|
if (!ShouldShowAlert()) {
|
|
|
|
// Do not display the alert. Instead call alertfinished and get out.
|
|
|
|
if (aAlertListener)
|
2015-12-31 22:04:24 +03:00
|
|
|
aAlertListener->Observe(nullptr, "alertfinished", cookie.get());
|
2012-03-08 01:09:04 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-12-31 22:04:24 +03:00
|
|
|
bool textClickable;
|
|
|
|
rv = aAlert->GetTextClickable(&textClickable);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsAutoString bidi;
|
|
|
|
rv = aAlert->GetDir(bidi);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsAutoString lang;
|
|
|
|
rv = aAlert->GetLang(lang);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
bool inPrivateBrowsing;
|
|
|
|
rv = aAlert->GetInPrivateBrowsing(&inPrivateBrowsing);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-03-18 17:24:54 +04:00
|
|
|
// Use XUL notifications as a fallback if above methods have failed.
|
2015-12-31 22:04:24 +03:00
|
|
|
rv = mXULAlerts.ShowAlertNotification(imageUrl, title, text, textClickable,
|
|
|
|
cookie, aAlertListener, name,
|
|
|
|
bidi, lang, principal, inPrivateBrowsing);
|
2003-11-27 03:54:33 +03:00
|
|
|
return rv;
|
2012-07-27 02:25:02 +04:00
|
|
|
#endif // !MOZ_WIDGET_ANDROID
|
2003-11-27 03:54:33 +03:00
|
|
|
}
|
2010-10-02 01:21:21 +04:00
|
|
|
|
2013-11-12 03:56:21 +04:00
|
|
|
NS_IMETHODIMP nsAlertsService::CloseAlert(const nsAString& aAlertName,
|
|
|
|
nsIPrincipal* aPrincipal)
|
2013-03-18 17:24:53 +04:00
|
|
|
{
|
2015-07-04 04:29:00 +03:00
|
|
|
if (XRE_IsContentProcess()) {
|
2013-03-18 17:24:53 +04:00
|
|
|
ContentChild* cpc = ContentChild::GetSingleton();
|
2013-11-12 03:56:21 +04:00
|
|
|
cpc->SendCloseAlert(nsAutoString(aAlertName), IPC::Principal(aPrincipal));
|
2013-03-18 17:24:53 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2015-01-10 03:33:57 +03:00
|
|
|
widget::GeckoAppShell::CloseNotification(aAlertName);
|
2013-03-18 17:24:53 +04:00
|
|
|
return NS_OK;
|
|
|
|
#else
|
|
|
|
|
|
|
|
// Try the system notification service.
|
|
|
|
nsCOMPtr<nsIAlertsService> sysAlerts(do_GetService(NS_SYSTEMALERTSERVICE_CONTRACTID));
|
|
|
|
if (sysAlerts) {
|
2013-11-12 03:56:21 +04:00
|
|
|
return sysAlerts->CloseAlert(aAlertName, nullptr);
|
2013-03-18 17:24:53 +04:00
|
|
|
}
|
|
|
|
|
2013-03-18 17:24:54 +04:00
|
|
|
return mXULAlerts.CloseAlert(aAlertName);
|
2013-03-18 17:24:53 +04:00
|
|
|
#endif // !MOZ_WIDGET_ANDROID
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-19 06:04:56 +03:00
|
|
|
// nsIAlertsDoNotDisturb
|
|
|
|
NS_IMETHODIMP nsAlertsService::GetManualDoNotDisturb(bool* aRetVal)
|
|
|
|
{
|
2015-10-19 06:39:35 +03:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
#else
|
2015-10-19 06:04:56 +03:00
|
|
|
// Try the system notification service.
|
|
|
|
nsCOMPtr<nsIAlertsService> sysAlerts(do_GetService(NS_SYSTEMALERTSERVICE_CONTRACTID));
|
|
|
|
if (sysAlerts) {
|
|
|
|
nsCOMPtr<nsIAlertsDoNotDisturb> alertsDND(do_GetService(NS_SYSTEMALERTSERVICE_CONTRACTID));
|
|
|
|
if (!alertsDND) {
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
return alertsDND->GetManualDoNotDisturb(aRetVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mXULAlerts.GetManualDoNotDisturb(aRetVal);
|
2015-10-19 06:39:35 +03:00
|
|
|
#endif
|
2015-10-19 06:04:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsAlertsService::SetManualDoNotDisturb(bool aDoNotDisturb)
|
|
|
|
{
|
2015-10-19 06:39:35 +03:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
#else
|
2015-10-19 06:04:56 +03:00
|
|
|
// Try the system notification service.
|
|
|
|
nsCOMPtr<nsIAlertsService> sysAlerts(do_GetService(NS_SYSTEMALERTSERVICE_CONTRACTID));
|
2015-11-14 08:22:59 +03:00
|
|
|
nsresult rv;
|
2015-10-19 06:04:56 +03:00
|
|
|
if (sysAlerts) {
|
|
|
|
nsCOMPtr<nsIAlertsDoNotDisturb> alertsDND(do_GetService(NS_SYSTEMALERTSERVICE_CONTRACTID));
|
|
|
|
if (!alertsDND) {
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
2015-11-14 08:22:59 +03:00
|
|
|
rv = alertsDND->SetManualDoNotDisturb(aDoNotDisturb);
|
|
|
|
} else {
|
|
|
|
rv = mXULAlerts.SetManualDoNotDisturb(aDoNotDisturb);
|
2015-10-19 06:04:56 +03:00
|
|
|
}
|
|
|
|
|
2015-11-14 08:22:59 +03:00
|
|
|
Telemetry::Accumulate(Telemetry::ALERTS_SERVICE_DND_ENABLED, 1);
|
|
|
|
return rv;
|
2015-10-19 06:39:35 +03:00
|
|
|
#endif
|
2015-10-19 06:04:56 +03:00
|
|
|
}
|
|
|
|
|
2010-10-02 01:21:21 +04:00
|
|
|
NS_IMETHODIMP nsAlertsService::OnProgress(const nsAString & aAlertName,
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t aProgress,
|
|
|
|
int64_t aProgressMax,
|
2010-10-02 01:21:21 +04:00
|
|
|
const nsAString & aAlertText)
|
|
|
|
{
|
2012-07-27 02:25:02 +04:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2015-01-10 03:33:57 +03:00
|
|
|
widget::GeckoAppShell::AlertsProgressListener_OnProgress(aAlertName,
|
|
|
|
aProgress, aProgressMax,
|
|
|
|
aAlertText);
|
2010-10-02 01:21:21 +04:00
|
|
|
return NS_OK;
|
|
|
|
#else
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2012-07-27 02:25:02 +04:00
|
|
|
#endif // !MOZ_WIDGET_ANDROID
|
2010-10-02 01:21:21 +04:00
|
|
|
}
|
2010-10-13 06:17:12 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP nsAlertsService::OnCancel(const nsAString & aAlertName)
|
|
|
|
{
|
2012-07-27 02:25:02 +04:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2015-01-10 03:33:57 +03:00
|
|
|
widget::GeckoAppShell::CloseNotification(aAlertName);
|
2010-10-13 06:17:12 +04:00
|
|
|
return NS_OK;
|
|
|
|
#else
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2012-07-27 02:25:02 +04:00
|
|
|
#endif // !MOZ_WIDGET_ANDROID
|
2010-10-13 06:17:12 +04:00
|
|
|
}
|