Bug 1418287 - Add preference to disable the system notification service r=baku
authorRob Wu <rob@robwu.nl>
Fri, 17 Nov 2017 13:29:15 +0100
changeset 437026 490462a742891f90452c9cd73a9bc7dcf2ba46a1
parent 437025 8053ce4e6ab8131f8dbe9ecde24c13426050f10d
child 437027 8d16bdf7d0fc6e6f7071a63e4173609b498e25da
push id117
push userfmarier@mozilla.com
push dateTue, 28 Nov 2017 20:17:16 +0000
reviewersbaku
bugs1418287
milestone59.0a1
Bug 1418287 - Add preference to disable the system notification service r=baku MozReview-Commit-ID: 5D1yJ8o0BgA
modules/libpref/init/all.js
toolkit/components/alerts/nsAlertsService.cpp
toolkit/components/alerts/nsAlertsService.h
--- a/modules/libpref/init/all.js
+++ b/modules/libpref/init/all.js
@@ -5031,16 +5031,21 @@ pref("dom.webnotifications.requireintera
 pref("dom.webnotifications.requireinteraction.enabled", true);
 #else
 pref("dom.webnotifications.requireinteraction.enabled", false);
 #endif
 
 // Show favicons in web notifications.
 pref("alerts.showFavicons", false);
 
+// Whether to use platform-specific backends for showing desktop notifications.
+// If no such backend is available, or if the pref is false, then XUL
+// notifications are used.
+pref("alerts.useSystemBackend", true);
+
 // DOM full-screen API.
 pref("full-screen-api.enabled", false);
 #ifdef RELEASE_OR_BETA
 pref("full-screen-api.unprefix.enabled", false);
 #else
 pref("full-screen-api.unprefix.enabled", true);
 #endif
 pref("full-screen-api.allow-trusted-requests-only", true);
--- a/toolkit/components/alerts/nsAlertsService.cpp
+++ b/toolkit/components/alerts/nsAlertsService.cpp
@@ -164,16 +164,30 @@ bool nsAlertsService::ShouldShowAlert()
        result = false;
     }
   }
 #endif
 
   return result;
 }
 
+bool nsAlertsService::ShouldUseSystemBackend()
+{
+  if (!mBackend) {
+    return false;
+  }
+  static bool sAlertsUseSystemBackend;
+  static bool sAlertsUseSystemBackendCached = false;
+  if (!sAlertsUseSystemBackendCached) {
+    sAlertsUseSystemBackendCached = true;
+    Preferences::AddBoolVarCache(&sAlertsUseSystemBackend, "alerts.useSystemBackend", true);
+  }
+  return sAlertsUseSystemBackend;
+}
+
 NS_IMETHODIMP nsAlertsService::ShowAlertNotification(const nsAString & aImageUrl, const nsAString & aAlertTitle,
                                                      const nsAString & aAlertText, bool aAlertTextClickable,
                                                      const nsAString & aAlertCookie,
                                                      nsIObserver * aAlertListener,
                                                      const nsAString & aAlertName,
                                                      const nsAString & aBidi,
                                                      const nsAString & aLang,
                                                      const nsAString & aData,
@@ -216,17 +230,17 @@ NS_IMETHODIMP nsAlertsService::ShowPersi
     if (aAlertListener)
       cpc->AddRemoteAlertObserver(cookie, aAlertListener);
 
     cpc->SendShowAlert(aAlert);
     return NS_OK;
   }
 
   // Check if there is an optional service that handles system-level notifications
-  if (mBackend) {
+  if (ShouldUseSystemBackend()) {
     rv = ShowWithBackend(mBackend, aAlert, aAlertListener, aPersistentData);
     if (NS_SUCCEEDED(rv)) {
       return rv;
     }
     // If the system backend failed to show the alert, clear the backend and
     // retry with XUL notifications. Future alerts will always use XUL.
     mBackend = nullptr;
   }
@@ -250,17 +264,17 @@ NS_IMETHODIMP nsAlertsService::CloseAler
   if (XRE_IsContentProcess()) {
     ContentChild* cpc = ContentChild::GetSingleton();
     cpc->SendCloseAlert(nsAutoString(aAlertName), IPC::Principal(aPrincipal));
     return NS_OK;
   }
 
   nsresult rv;
   // Try the system notification service.
-  if (mBackend) {
+  if (ShouldUseSystemBackend()) {
     rv = mBackend->CloseAlert(aAlertName, aPrincipal);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       // If the system backend failed to close the alert, fall back to XUL for
       // future alerts.
       mBackend = nullptr;
     }
   } else {
     nsCOMPtr<nsIAlertsService> xulBackend(nsXULAlerts::GetInstance());
@@ -297,17 +311,20 @@ NS_IMETHODIMP nsAlertsService::SetManual
   }
   return rv;
 #endif
 }
 
 already_AddRefed<nsIAlertsDoNotDisturb>
 nsAlertsService::GetDNDBackend()
 {
+  nsCOMPtr<nsIAlertsService> backend;
   // Try the system notification service.
-  nsCOMPtr<nsIAlertsService> backend = mBackend;
+  if (ShouldUseSystemBackend()) {
+    backend = mBackend;
+  }
   if (!backend) {
     backend = nsXULAlerts::GetInstance();
   }
 
   nsCOMPtr<nsIAlertsDoNotDisturb> alertsDND(do_QueryInterface(backend));
   return alertsDND.forget();
 }
--- a/toolkit/components/alerts/nsAlertsService.h
+++ b/toolkit/components/alerts/nsAlertsService.h
@@ -19,13 +19,14 @@ public:
   NS_DECL_ISUPPORTS
 
   nsAlertsService();
 
 protected:
   virtual ~nsAlertsService();
 
   bool ShouldShowAlert();
+  bool ShouldUseSystemBackend();
   already_AddRefed<nsIAlertsDoNotDisturb> GetDNDBackend();
   nsCOMPtr<nsIAlertsService> mBackend;
 };
 
 #endif /* nsAlertsService_h__ */