Bug 1055464 - Add RefreshBlocker front-end. r=Mossop
authorMike Conley <mconley@mozilla.com>
Fri, 05 Feb 2016 15:01:38 -0500
changeset 329595 954e2015504392e23118e1ad71506b7e6d7aed78
parent 329594 667d574c768d48f287324f30792309f93ed37668
child 329596 cb036027df84e7e547bf3f9614b71857e5059741
push id10558
push usermconley@mozilla.com
push dateMon, 08 Feb 2016 18:36:50 +0000
reviewersMossop
bugs1055464
milestone47.0a1
Bug 1055464 - Add RefreshBlocker front-end. r=Mossop
browser/base/content/browser-refreshblocker.js
browser/base/content/browser.js
browser/base/content/global-scripts.inc
browser/base/jar.mn
testing/eslint-plugin-mozilla/lib/rules/import-browserjs-globals.js
new file mode 100644
--- /dev/null
+++ b/browser/base/content/browser-refreshblocker.js
@@ -0,0 +1,84 @@
+/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
+ * 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/. */
+
+/**
+ * If the user has opted into blocking refresh and redirect attempts by
+ * default, this handles showing the notification to the user which
+ * gives them the option to let the refresh or redirect proceed.
+ */
+var RefreshBlocker = {
+  init() {
+    gBrowser.addEventListener("RefreshBlocked", this);
+  },
+
+  uninit() {
+    gBrowser.removeEventListener("RefreshBlocked", this);
+  },
+
+  handleEvent: function(event) {
+    if (event.type == "RefreshBlocked") {
+      this.block(event.originalTarget, event.detail);
+    }
+  },
+
+  /**
+   * Shows the blocked refresh / redirect notification for some browser.
+   *
+   * @param browser (<xul:browser>)
+   *        The browser that had the refresh blocked. This will be the browser
+   *        for which we'll show the notification on.
+   * @param data (object)
+   *        An object with the following properties:
+   *
+   *        URI (string)
+   *          The URI that a page is attempting to refresh or redirect to.
+   *
+   *        delay (int)
+   *          The delay (in milliseconds) before the page was going to reload
+   *          or redirect.
+   *
+   *        sameURI (bool)
+   *          true if we're refreshing the page. false if we're redirecting.
+   *
+   *        outerWindowID (int)
+   *          The outerWindowID of the frame that requested the refresh or
+   *          redirect.
+   */
+  block(browser, data) {
+    let brandBundle = document.getElementById("bundle_brand");
+    let brandShortName = brandBundle.getString("brandShortName");
+    let message =
+      gNavigatorBundle.getFormattedString(data.sameURI ? "refreshBlocked.refreshLabel"
+                                                       : "refreshBlocked.redirectLabel",
+                                          [brandShortName]);
+
+    let notificationBox = gBrowser.getNotificationBox(browser);
+    let notification = notificationBox.getNotificationWithValue("refresh-blocked");
+
+    if (notification) {
+      notification.label = message;
+    } else {
+      let refreshButtonText =
+        gNavigatorBundle.getString("refreshBlocked.goButton");
+      let refreshButtonAccesskey =
+        gNavigatorBundle.getString("refreshBlocked.goButton.accesskey");
+
+      let buttons = [{
+        label: refreshButtonText,
+        accessKey: refreshButtonAccesskey,
+        callback: function (notification, button) {
+          if (browser.messageManager) {
+            browser.messageManager.sendAsyncMessage("RefreshBlocker:Refresh", data);
+          }
+        }
+      }];
+
+      notificationBox.appendNotification(message, "refresh-blocked",
+                                         "chrome://browser/skin/Info.png",
+                                         notificationBox.PRIORITY_INFO_MEDIUM,
+                                         buttons);
+    }
+  }
+};
--- a/browser/base/content/browser.js
+++ b/browser/base/content/browser.js
@@ -918,16 +918,17 @@ var gBrowserInit = {
     DOMLinkHandler.init();
     gPageStyleMenu.init();
     LanguageDetectionListener.init();
     BrowserOnClick.init();
     FeedHandler.init();
     DevEdition.init();
     AboutPrivateBrowsingListener.init();
     TrackingProtection.init();
+    RefreshBlocker.init();
 
     let mm = window.getGroupMessageManager("browsers");
     mm.loadFrameScript("chrome://browser/content/tab-content.js", true);
     mm.loadFrameScript("chrome://browser/content/content.js", true);
     mm.loadFrameScript("chrome://browser/content/content-UITour.js", true);
     mm.loadFrameScript("chrome://global/content/manifestMessages.js", true);
 
     window.messageManager.addMessageListener("Browser:LoadURI", RedirectLoad);
@@ -1441,16 +1442,18 @@ var gBrowserInit = {
     BrowserOnClick.uninit();
 
     FeedHandler.uninit();
 
     DevEdition.uninit();
 
     TrackingProtection.uninit();
 
+    RefreshBlocker.uninit();
+
     gMenuButtonUpdateBadge.uninit();
 
     gMenuButtonBadgeManager.uninit();
 
     SidebarUI.uninit();
 
     // Now either cancel delayedStartup, or clean up the services initialized from
     // it.
@@ -4691,64 +4694,16 @@ var TabsProgressListener = {
     // longer exists)
     if (!Object.getOwnPropertyDescriptor(window, "PopupNotifications").get)
       PopupNotifications.locationChange(aBrowser);
 
     gBrowser.getNotificationBox(aBrowser).removeTransientNotifications();
 
     FullZoom.onLocationChange(aLocationURI, false, aBrowser);
   },
-
-  onRefreshAttempted: function (aBrowser, aWebProgress, aURI, aDelay, aSameURI) {
-    if (gPrefService.getBoolPref("accessibility.blockautorefresh")) {
-      let brandBundle = document.getElementById("bundle_brand");
-      let brandShortName = brandBundle.getString("brandShortName");
-      let refreshButtonText =
-        gNavigatorBundle.getString("refreshBlocked.goButton");
-      let refreshButtonAccesskey =
-        gNavigatorBundle.getString("refreshBlocked.goButton.accesskey");
-      let message =
-        gNavigatorBundle.getFormattedString(aSameURI ? "refreshBlocked.refreshLabel"
-                                                     : "refreshBlocked.redirectLabel",
-                                            [brandShortName]);
-      let docShell = aWebProgress.DOMWindow
-                                 .QueryInterface(Ci.nsIInterfaceRequestor)
-                                 .getInterface(Ci.nsIWebNavigation)
-                                 .QueryInterface(Ci.nsIDocShell);
-      let notificationBox = gBrowser.getNotificationBox(aBrowser);
-      let notification = notificationBox.getNotificationWithValue("refresh-blocked");
-      if (notification) {
-        notification.label = message;
-        notification.refreshURI = aURI;
-        notification.delay = aDelay;
-        notification.docShell = docShell;
-      } else {
-        let buttons = [{
-          label: refreshButtonText,
-          accessKey: refreshButtonAccesskey,
-          callback: function (aNotification, aButton) {
-            var refreshURI = aNotification.docShell
-                                          .QueryInterface(Ci.nsIRefreshURI);
-            refreshURI.forceRefreshURI(aNotification.refreshURI,
-                                       aNotification.delay, true);
-          }
-        }];
-        notification =
-          notificationBox.appendNotification(message, "refresh-blocked",
-                                             "chrome://browser/skin/Info.png",
-                                             notificationBox.PRIORITY_INFO_MEDIUM,
-                                             buttons);
-        notification.refreshURI = aURI;
-        notification.delay = aDelay;
-        notification.docShell = docShell;
-      }
-      return false;
-    }
-    return true;
-  }
 }
 
 function nsBrowserAccess() { }
 
 nsBrowserAccess.prototype = {
   QueryInterface: XPCOMUtils.generateQI([Ci.nsIBrowserDOMWindow, Ci.nsISupports]),
 
   _openURIInNewTab: function(aURI, aReferrer, aReferrerPolicy, aIsPrivate,
--- a/browser/base/content/global-scripts.inc
+++ b/browser/base/content/global-scripts.inc
@@ -19,16 +19,17 @@
 <script type="application/javascript" src="chrome://browser/content/browser-devedition.js"/>
 <script type="application/javascript" src="chrome://browser/content/browser-eme.js"/>
 <script type="application/javascript" src="chrome://browser/content/browser-feeds.js"/>
 <script type="application/javascript" src="chrome://browser/content/browser-fullScreen.js"/>
 <script type="application/javascript" src="chrome://browser/content/browser-fullZoom.js"/>
 <script type="application/javascript" src="chrome://browser/content/browser-gestureSupport.js"/>
 <script type="application/javascript" src="chrome://browser/content/browser-places.js"/>
 <script type="application/javascript" src="chrome://browser/content/browser-plugins.js"/>
+<script type="application/javascript" src="chrome://browser/content/browser-refreshblocker.js"/>
 #ifdef MOZ_SAFE_BROWSING
 <script type="application/javascript" src="chrome://browser/content/browser-safebrowsing.js"/>
 #endif
 <script type="application/javascript" src="chrome://browser/content/browser-sidebar.js"/>
 <script type="application/javascript" src="chrome://browser/content/browser-social.js"/>
 <script type="application/javascript" src="chrome://browser/content/browser-syncui.js"/>
 <script type="application/javascript" src="chrome://browser/content/browser-tabsintitlebar.js"/>
 <script type="application/javascript" src="chrome://browser/content/browser-thumbnails.js"/>
--- a/browser/base/jar.mn
+++ b/browser/base/jar.mn
@@ -84,16 +84,17 @@ browser.jar:
         content/browser/browser-eme.js                (content/browser-eme.js)
         content/browser/browser-feeds.js              (content/browser-feeds.js)
         content/browser/browser-fullScreen.js         (content/browser-fullScreen.js)
         content/browser/browser-fullZoom.js           (content/browser-fullZoom.js)
         content/browser/browser-fxaccounts.js         (content/browser-fxaccounts.js)
         content/browser/browser-gestureSupport.js     (content/browser-gestureSupport.js)
         content/browser/browser-places.js             (content/browser-places.js)
         content/browser/browser-plugins.js            (content/browser-plugins.js)
+        content/browser/browser-refreshblocker.js     (content/browser-refreshblocker.js)
 #ifdef MOZ_SAFE_BROWSING
         content/browser/browser-safebrowsing.js       (content/browser-safebrowsing.js)
 #endif
         content/browser/browser-sidebar.js            (content/browser-sidebar.js)
         content/browser/browser-social.js             (content/browser-social.js)
         content/browser/browser-syncui.js             (content/browser-syncui.js)
 *       content/browser/browser-tabPreviews.xml       (content/browser-tabPreviews.xml)
 #ifdef CAN_DRAW_IN_TITLEBAR
--- a/testing/eslint-plugin-mozilla/lib/rules/import-browserjs-globals.js
+++ b/testing/eslint-plugin-mozilla/lib/rules/import-browserjs-globals.js
@@ -32,16 +32,17 @@ const SCRIPTS = [
   "browser/base/content/browser-devedition.js",
   "browser/base/content/browser-eme.js",
   "browser/base/content/browser-feeds.js",
   "browser/base/content/browser-fullScreen.js",
   "browser/base/content/browser-fullZoom.js",
   "browser/base/content/browser-gestureSupport.js",
   "browser/base/content/browser-places.js",
   "browser/base/content/browser-plugins.js",
+  "browser/base/content/browser-refreshblocker.js",
   "browser/base/content/browser-safebrowsing.js",
   "browser/base/content/browser-sidebar.js",
   "browser/base/content/browser-social.js",
   "browser/base/content/browser-syncui.js",
   "browser/base/content/browser-tabsintitlebar.js",
   "browser/base/content/browser-thumbnails.js",
   "browser/base/content/browser-trackingprotection.js",
   "browser/base/content/browser-data-submission-info-bar.js",