Bug 1227300, Part 2 - Deprecate `showAlertNotification` in favor of `showAlert`. r?MattN,wchen draft
authorKit Cambridge <kcambridge@mozilla.com>
Mon, 23 Nov 2015 14:58:54 -0800
changeset 310812 45216d7464302da0ecd9eefd48eb356b3231b68a
parent 310811 6d5c1875bb523c1e9599f94eddd0b54190bfcc5b
child 310813 4345b94a31543860ce2bbddcd4234390f6e0b99a
push id7799
push userkcambridge@mozilla.com
push dateTue, 24 Nov 2015 02:34:45 +0000
reviewersMattN, wchen
bugs1227300
milestone45.0a1
Bug 1227300, Part 2 - Deprecate `showAlertNotification` in favor of `showAlert`. r?MattN,wchen
dom/ipc/ContentParent.cpp
dom/ipc/ContentParent.h
dom/ipc/PContent.ipdl
toolkit/components/alerts/nsAlertsService.cpp
toolkit/components/alerts/nsIAlertsService.idl
--- a/dom/ipc/ContentParent.cpp
+++ b/dom/ipc/ContentParent.cpp
@@ -4311,33 +4311,30 @@ ContentParent::HasNotificationPermission
         return false;
     }
 #endif /* MOZ_CHILD_PERMISSIONS */
 
     return true;
 }
 
 bool
-ContentParent::RecvShowAlertNotification(const nsString& aImageUrl, const nsString& aTitle,
-                                         const nsString& aText, const bool& aTextClickable,
-                                         const nsString& aCookie, const nsString& aName,
-                                         const nsString& aBidi, const nsString& aLang,
-                                         const nsString& aData,
-                                         const IPC::Principal& aPrincipal,
-                                         const bool& aInPrivateBrowsing)
-{
-    if (!HasNotificationPermission(aPrincipal)) {
+ContentParent::RecvShowAlert(const nsString& aName,
+                             const Alert& aAlert)
+{
+    nsCOMPtr<nsIPrincipal> principal;
+    nsresult rv = aAlert->GetPrincipal(getter_AddRefs(principal));
+    if (NS_WARN_IF(NS_FAILED(rv)) ||
+        !HasNotificationPermission(IPC::Principal(principal))) {
+
         return true;
     }
 
     nsCOMPtr<nsIAlertsService> sysAlerts(do_GetService(NS_ALERTSERVICE_CONTRACTID));
     if (sysAlerts) {
-        sysAlerts->ShowAlertNotification(aImageUrl, aTitle, aText, aTextClickable,
-                                         aCookie, this, aName, aBidi, aLang,
-                                         aData, aPrincipal, aInPrivateBrowsing);
+        sysAlerts->ShowAlert(aName, aAlert, this);
     }
     return true;
 }
 
 bool
 ContentParent::RecvCloseAlert(const nsString& aName,
                               const IPC::Principal& aPrincipal)
 {
--- a/dom/ipc/ContentParent.h
+++ b/dom/ipc/ContentParent.h
@@ -775,23 +775,18 @@ private:
                               const OptionalURIParams& referrer,
                               const uint32_t& flags) override;
 
     virtual bool RecvSetURITitle(const URIParams& uri,
                                  const nsString& title) override;
 
     bool HasNotificationPermission(const IPC::Principal& aPrincipal);
 
-    virtual bool RecvShowAlertNotification(const nsString& aImageUrl, const nsString& aTitle,
-                                           const nsString& aText, const bool& aTextClickable,
-                                           const nsString& aCookie, const nsString& aName,
-                                           const nsString& aBidi, const nsString& aLang,
-                                           const nsString& aData,
-                                           const IPC::Principal& aPrincipal,
-                                           const bool& aInPrivateBrowsing) override;
+    virtual bool RecvShowAlert(const nsString& aName,
+                               const Alert& aAlert) override;
 
     virtual bool RecvCloseAlert(const nsString& aName,
                                 const IPC::Principal& aPrincipal) override;
 
     virtual bool RecvDisableNotifications(const IPC::Principal& aPrincipal) override;
 
     virtual bool RecvOpenNotificationSettings(const IPC::Principal& aPrincipal) override;
 
--- a/dom/ipc/PContent.ipdl
+++ b/dom/ipc/PContent.ipdl
@@ -67,16 +67,17 @@ include BrowserConfiguration;
 include GraphicsMessages;
 
 // Workaround to prevent error if PContentChild.cpp & PContentBridgeParent.cpp
 // are put into different UnifiedProtocolsXX.cpp files.
 // XXX Remove this once bug 1069073 is fixed
 include "mozilla/dom/PContentBridgeParent.h";
 
 using GeoPosition from "nsGeoPositionIPCSerialiser.h";
+using Alert from "nsAlertIPCSerializer.h";
 
 using struct ChromePackage from "mozilla/chrome/RegistryMessageUtils.h";
 using struct SubstitutionMapping from "mozilla/chrome/RegistryMessageUtils.h";
 using struct OverrideMapping from "mozilla/chrome/RegistryMessageUtils.h";
 using base::ChildPrivileges from "base/process_util.h";
 using base::ProcessId from "base/process.h";
 using struct IPC::Permission from "mozilla/net/NeckoMessageUtils.h";
 using class IPC::Principal from "mozilla/dom/PermissionMessageUtils.h";
@@ -855,27 +856,17 @@ parent:
     sync SyncMessage(nsString aMessage, ClonedMessageData aData,
                      CpowEntry[] aCpows, Principal aPrincipal)
       returns (StructuredCloneData[] retval);
 
     prio(high) sync RpcMessage(nsString aMessage, ClonedMessageData aData,
                                CpowEntry[] aCpows, Principal aPrincipal)
       returns (StructuredCloneData[] retval);
 
-    ShowAlertNotification(nsString imageUrl,
-                          nsString title,
-                          nsString text,
-                          bool textClickable,
-                          nsString cookie,
-                          nsString name,
-                          nsString bidi,
-                          nsString lang,
-                          nsString data,
-                          Principal principal,
-                          bool inPrivateBrowsing);
+    ShowAlert(nsString name, Alert alert);
 
     CloseAlert(nsString name, Principal principal);
 
     DisableNotifications(Principal principal);
 
     OpenNotificationSettings(Principal principal);
 
     PPSMContentDownloader(uint32_t aCertType);
--- a/toolkit/components/alerts/nsAlertsService.cpp
+++ b/toolkit/components/alerts/nsAlertsService.cpp
@@ -56,76 +56,107 @@ bool nsAlertsService::ShouldShowAlert()
   }
 
   ::FreeLibrary(shellDLL);
 #endif
 
   return result;
 }
 
-NS_IMETHODIMP nsAlertsService::ShowAlertNotification(const nsAString & aImageUrl, const nsAString & aAlertTitle, 
+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,
                                                      nsIPrincipal * aPrincipal,
                                                      bool aInPrivateBrowsing)
 {
+  nsCOMPtr<nsIAlert> alert = new nsAlert(aImageUrl, aAlertTitle, aAlertText,
+                                         aAlertTextClickable, aAlertCookie,
+                                         aBidi, aLang, aData, aPrincipal,
+                                         aInPrivateBrowsing);
+  return ShowAlert(aAlertName, alert, aAlertListener);
+}
+
+
+NS_IMETHODIMP nsAlertsService::ShowAlert(const nsAString & aAlertName,
+                                         nsIAlert * aAlert,
+                                         nsIObserver * aAlertListener)
+{
+  nsString alertCookie;
+  nsresult rv = aAlert->GetCookie(alertCookie);
+  NS_ENSURE_SUCCESS(rv, rv);
+
   if (XRE_IsContentProcess()) {
     ContentChild* cpc = ContentChild::GetSingleton();
 
     if (aAlertListener)
-      cpc->AddRemoteAlertObserver(PromiseFlatString(aAlertCookie), aAlertListener);
+      cpc->AddRemoteAlertObserver(alertCookie, aAlertListener);
 
-    cpc->SendShowAlertNotification(PromiseFlatString(aImageUrl),
-                                   PromiseFlatString(aAlertTitle),
-                                   PromiseFlatString(aAlertText),
-                                   aAlertTextClickable,
-                                   PromiseFlatString(aAlertCookie),
-                                   PromiseFlatString(aAlertName),
-                                   PromiseFlatString(aBidi),
-                                   PromiseFlatString(aLang),
-                                   PromiseFlatString(aData),
-                                   IPC::Principal(aPrincipal),
-                                   aInPrivateBrowsing);
+    cpc->SendShowAlert(PromiseFlatString(aAlertName), aAlert);
     return NS_OK;
   }
 
+  nsString imageUrl, alertTitle, alertText;
+
+  rv = aAlert->GetImageUrl(imageUrl);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  rv = aAlert->GetTitle(alertTitle);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  rv = aAlert->GetText(alertText);
+  NS_ENSURE_SUCCESS(rv, rv);
+
 #ifdef MOZ_WIDGET_ANDROID
-  mozilla::AndroidBridge::Bridge()->ShowAlertNotification(aImageUrl, aAlertTitle, aAlertText, aAlertCookie,
+  mozilla::AndroidBridge::Bridge()->ShowAlertNotification(imageUrl, alertTitle, alertText, alertCookie,
                                                           aAlertListener, aAlertName);
   return NS_OK;
 #else
   // Check if there is an optional service that handles system-level notifications
   nsCOMPtr<nsIAlertsService> sysAlerts(do_GetService(NS_SYSTEMALERTSERVICE_CONTRACTID));
-  nsresult rv;
   if (sysAlerts) {
-    rv = sysAlerts->ShowAlertNotification(aImageUrl, aAlertTitle, aAlertText, aAlertTextClickable,
-                                          aAlertCookie, aAlertListener, aAlertName,
-                                          aBidi, aLang, aData,
-                                          IPC::Principal(aPrincipal),
-                                          aInPrivateBrowsing);
+    rv = sysAlerts->ShowAlert(aAlertName, aAlert, aAlertListener);
     if (NS_SUCCEEDED(rv))
       return NS_OK;
   }
 
   if (!ShouldShowAlert()) {
     // Do not display the alert. Instead call alertfinished and get out.
     if (aAlertListener)
-      aAlertListener->Observe(nullptr, "alertfinished", PromiseFlatString(aAlertCookie).get());
+      aAlertListener->Observe(nullptr, "alertfinished", alertCookie.get());
     return NS_OK;
   }
 
+  nsString bidi, lang;
+  bool alertTextClickable, inPrivateBrowsing;
+  nsCOMPtr<nsIPrincipal> principal;
+
+  rv = aAlert->GetTextClickable(&alertTextClickable);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  rv = aAlert->GetDir(bidi);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  rv = aAlert->GetLang(lang);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  rv = aAlert->GetPrincipal(getter_AddRefs(principal));
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  rv = aAlert->GetInPrivateBrowsing(&inPrivateBrowsing);
+  NS_ENSURE_SUCCESS(rv, rv);
+
   // Use XUL notifications as a fallback if above methods have failed.
-  rv = mXULAlerts.ShowAlertNotification(aImageUrl, aAlertTitle, aAlertText, aAlertTextClickable,
-                                        aAlertCookie, aAlertListener, aAlertName,
-                                        aBidi, aLang, aPrincipal, aInPrivateBrowsing);
+  rv = mXULAlerts.ShowAlertNotification(imageUrl, alertTitle, alertText, alertTextClickable,
+                                        alertCookie, aAlertListener, aAlertName,
+                                        bidi, lang, principal, inPrivateBrowsing);
   return rv;
 #endif // !MOZ_WIDGET_ANDROID
 }
 
 NS_IMETHODIMP nsAlertsService::CloseAlert(const nsAString& aAlertName,
                                           nsIPrincipal* aPrincipal)
 {
   if (XRE_IsContentProcess()) {
--- a/toolkit/components/alerts/nsIAlertsService.idl
+++ b/toolkit/components/alerts/nsIAlertsService.idl
@@ -18,19 +18,22 @@ interface nsIAlert : nsISupports
   readonly attribute DOMString cookie;
   readonly attribute DOMString dir;
   readonly attribute DOMString lang;
   readonly attribute DOMString data;
   readonly attribute nsIPrincipal principal;
   readonly attribute boolean inPrivateBrowsing;
 };
 
-[scriptable, uuid(9d0284bf-db40-42da-8f0d-c2769dbde7aa)]
+[scriptable, uuid(f7a36392-d98b-4141-a7d7-4e46642684e3)]
 interface nsIAlertsService : nsISupports
 {
+  void showAlert([optional] in AString name,
+                 [optional] in nsIAlert alert,
+                 [optional] in nsIObserver alertListener);
   /**
    * Displays a sliding notification window.
    *
    * @param imageUrl       A URL identifying the image to put in the alert.
    *                       The OS X implemenation limits the amount of time it
    *                       will wait for an icon to load to six seconds. After
    *                       that time the alert will show with no icon.
    * @param title          The title for the alert.
@@ -65,16 +68,17 @@ interface nsIAlertsService : nsISupports
    *             "alertshow" when the alert is shown
    *   data    - the value of the cookie parameter passed to showAlertNotification.
    *
    * @note Depending on current circumstances (if the user's in a fullscreen
    *       application, for instance), the alert might not be displayed at all.
    *       In that case, if an alert listener is passed in it will receive the
    *       "alertfinished" notification immediately.
    */
+  [deprecated]
   void showAlertNotification(in AString  imageUrl,
                              in AString  title,
                              in AString  text,
                              [optional] in boolean textClickable,
                              [optional] in AString cookie,
                              [optional] in nsIObserver alertListener,
                              [optional] in AString name,
                              [optional] in AString dir,