Bug 1266372 - De-dup code to ReaderMode.leaveReaderMode, r=gijs a=ritu
authorTimothy Guan-tin Chien <timdream@gmail.com>
Tue, 26 Apr 2016 12:52:50 +0800
changeset 332672 13fda2d4925ed657c90a25a78fd734016a97e13c
parent 332671 2f5b903562626c82d700c9cfa7902758588717e2
child 332673 2312ffc9586602df777c20b1ed1ede61e65c4100
push id6048
push userkmoir@mozilla.com
push dateMon, 06 Jun 2016 19:02:08 +0000
treeherdermozilla-beta@46d72a56c57d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgijs, ritu
bugs1266372
milestone48.0a2
Bug 1266372 - De-dup code to ReaderMode.leaveReaderMode, r=gijs a=ritu Additionally, implements ReaderMode.enterReaderMode. MozReview-Commit-ID: 8MPvFhX4ws7
browser/base/content/tab-content.js
mobile/android/chrome/content/content.js
toolkit/components/reader/AboutReader.jsm
toolkit/components/reader/ReaderMode.jsm
--- a/browser/base/content/tab-content.js
+++ b/browser/base/content/tab-content.js
@@ -256,31 +256,19 @@ var AboutReaderListener = {
   },
 
   receiveMessage: function(message) {
     switch (message.name) {
       case "Reader:ToggleReaderMode":
         let url = content.document.location.href;
         if (!this.isAboutReader) {
           this._articlePromise = ReaderMode.parseDocument(content.document).catch(Cu.reportError);
-          content.document.location = "about:reader?url=" + encodeURIComponent(url);
+          ReaderMode.enterReaderMode(docShell, content);
         } else {
-          let originalURL = ReaderMode.getOriginalUrl(url);
-          let webNav = docShell.QueryInterface(Ci.nsIWebNavigation);
-          let sh = webNav.sessionHistory;
-          if (webNav.canGoBack) {
-            let prevEntry = sh.getEntryAtIndex(sh.index - 1, false);
-            let prevURL = prevEntry.URI.spec;
-            if (prevURL && (prevURL == originalURL || !originalURL)) {
-              webNav.goBack();
-              break;
-            }
-          }
-
-          content.document.location = originalURL;
+          ReaderMode.leaveReaderMode(docShell, content);
         }
         break;
 
       case "Reader:PushState":
         this.updateReaderButton(!!(message.data && message.data.isArticle));
         break;
     }
   },
--- a/mobile/android/chrome/content/content.js
+++ b/mobile/android/chrome/content/content.js
@@ -32,31 +32,19 @@ var AboutReaderListener = {
   },
 
   receiveMessage: function(message) {
     switch (message.name) {
       case "Reader:ToggleReaderMode":
         let url = content.document.location.href;
         if (!this.isAboutReader) {
           this._articlePromise = ReaderMode.parseDocument(content.document).catch(Cu.reportError);
-          content.document.location = "about:reader?url=" + encodeURIComponent(url);
+          ReaderMode.enterReaderMode(docShell, content);
         } else {
-          let originalURL = ReaderMode.getOriginalUrl(url);
-          let webNav = docShell.QueryInterface(Ci.nsIWebNavigation);
-          let sh = webNav.sessionHistory;
-          if (webNav.canGoBack) {
-            let prevEntry = sh.getEntryAtIndex(sh.index - 1, false);
-            let prevURL = prevEntry.URI.spec;
-            if (prevURL && (prevURL == originalURL || !originalURL)) {
-              webNav.goBack();
-              break;
-            }
-          }
-
-          content.document.location = originalURL;
+          ReaderMode.leaveReaderMode(docShell, content);
         }
         break;
 
       case "Reader:PushState":
         this.updateReaderButton(!!(message.data && message.data.isArticle));
         break;
     }
   },
--- a/toolkit/components/reader/AboutReader.jsm
+++ b/toolkit/components/reader/AboutReader.jsm
@@ -254,17 +254,17 @@ AboutReader.prototype = {
         this._mm.removeMessageListener("Reader:AddButton", this);
         this._mm.removeMessageListener("Reader:RemoveButton", this);
         this._windowUnloaded = true;
         break;
     }
   },
 
   _onReaderClose: function() {
-    this._win.location.href = this._getOriginalUrl();
+    ReaderMode.leaveReaderMode(this._mm.docShell, this._win);
   },
 
   _setFontSize: function(newFontSize) {
     let containerClasses = this._doc.getElementById("container").classList;
 
     if (this._fontSize > 0)
       containerClasses.remove("font-size" + this._fontSize);
 
--- a/toolkit/components/reader/ReaderMode.jsm
+++ b/toolkit/components/reader/ReaderMode.jsm
@@ -83,16 +83,58 @@ this.ReaderMode = {
         } else if (aData === "reader.parse-node-limit") {
           this.parseNodeLimit = Services.prefs.getIntPref(aData);
         }
         break;
     }
   },
 
   /**
+   * Enter the reader mode by going forward one step in history if applicable,
+   * if not, append the about:reader page in the history instead.
+   */
+  enterReaderMode: function(docShell, win) {
+    let url = win.document.location.href;
+    let readerURL = "about:reader?url=" + encodeURIComponent(url);
+    let webNav = docShell.QueryInterface(Ci.nsIWebNavigation);
+    let sh = webNav.sessionHistory;
+    if (webNav.canGoForward) {
+      let forwardEntry = sh.getEntryAtIndex(sh.index + 1, false);
+      let forwardURL = forwardEntry.URI.spec;
+      if (forwardURL && (forwardURL == readerURL || !readerURL)) {
+        webNav.goForward();
+        return;
+      }
+    }
+
+    win.document.location = readerURL;
+  },
+
+  /**
+   * Exit the reader mode by going back one step in history if applicable,
+   * if not, append the original page in the history instead.
+   */
+  leaveReaderMode: function(docShell, win) {
+    let url = win.document.location.href;
+    let originalURL = this.getOriginalUrl(url);
+    let webNav = docShell.QueryInterface(Ci.nsIWebNavigation);
+    let sh = webNav.sessionHistory;
+    if (webNav.canGoBack) {
+      let prevEntry = sh.getEntryAtIndex(sh.index - 1, false);
+      let prevURL = prevEntry.URI.spec;
+      if (prevURL && (prevURL == originalURL || !originalURL)) {
+        webNav.goBack();
+        return;
+      }
+    }
+
+    win.document.location = originalURL;
+  },
+
+  /**
    * Returns original URL from an about:reader URL.
    *
    * @param url An about:reader URL.
    * @return The original URL for the article, or null if we did not find
    *         a properly formatted about:reader URL.
    */
   getOriginalUrl: function(url) {
     if (!url.startsWith("about:reader?")) {