Bug 1418287 - Add preference to disable the system notification service r?baku draft
authorRob Wu <rob@robwu.nl>
Fri, 17 Nov 2017 13:29:15 +0100
changeset 699654 1c10a5ecdd7c5b592d546e2a3d76e090a56dc517
parent 697096 fc194660762d1b92e1679d860a8bf41116d0f54f
child 740684 a7180a6f639f330aea5df004c21a2db4e2ddc991
push id89631
push userbmo:rob@robwu.nl
push dateFri, 17 Nov 2017 13:46:52 +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
@@ -5048,16 +5048,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__ */