Bug 1266372 - De-dup code to ReaderMode.leaveReaderMode, r=gijs
authorTimothy Guan-tin Chien <timdream@gmail.com>
Tue, 26 Apr 2016 12:52:50 +0800
changeset 294910 a965f8da8ed177a36fc913841039d5b8af8ad5f0
parent 294909 146e4f2558c9120ada43a695ced5a4c0a15fae01
child 294911 62646acf6872b596b59dcb737c34a53a61fb9e64
push id30214
push userkwierso@gmail.com
push dateTue, 26 Apr 2016 21:17:19 +0000
treeherdermozilla-central@6a441b2b2997 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgijs
bugs1266372
milestone49.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1266372 - De-dup code to ReaderMode.leaveReaderMode, r=gijs 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?")) {