Bug 1218937 - Introduce a new option for switchToTabHavingURI's ignoreFragment to not overwrite the fragment portion. Use this new argument to switch to the preferences without changing the currently opened pane. r=mattn
authorJared Wein <jwein@mozilla.com>
Wed, 31 Aug 2016 12:53:23 -0400
changeset 353353 a2e1e6905f22510c11abb41a0cb765cca6508cb0
parent 353352 2796037318bb78006b014c5f0d6db52cc2214364
child 353354 fdf90317de6820adc8c2b9b1e70f8d11fa859636
push id6570
push userraliiev@mozilla.com
push dateMon, 14 Nov 2016 12:26:13 +0000
treeherdermozilla-beta@f455459b2ae5 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmattn
bugs1218937
milestone51.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 1218937 - Introduce a new option for switchToTabHavingURI's ignoreFragment to not overwrite the fragment portion. Use this new argument to switch to the preferences without changing the currently opened pane. r=mattn MozReview-Commit-ID: JZmAsf8k5Qp
browser/base/content/browser-trackingprotection.js
browser/base/content/browser.js
browser/base/content/test/urlbar/browser_bug1025195_switchToTabHavingURI_aOpenParams.js
browser/base/content/utilityOverlay.js
browser/components/sessionstore/test/browser_replace_load.js
--- a/browser/base/content/browser-trackingprotection.js
+++ b/browser/base/content/browser-trackingprotection.js
@@ -206,17 +206,17 @@ var TrackingProtection = {
       this.dontShowIntroPanelAgain();
 
       let nextURL = Services.urlFormatter.formatURLPref("privacy.trackingprotection.introURL") +
                     "?step=2&newtab=true";
       switchToTabHavingURI(nextURL, true, {
         // Ignore the fragment in case the intro is shown on the tour page
         // (e.g. if the user manually visited the tour or clicked the link from
         // about:privatebrowsing) so we can avoid a reload.
-        ignoreFragment: true,
+        ignoreFragment: "whenComparingAndReplace",
       });
     };
 
     let buttons = [
       {
         label: gNavigatorBundle.getString("trackingProtection.intro.step1of3"),
         style: "text",
       },
--- a/browser/base/content/browser.js
+++ b/browser/base/content/browser.js
@@ -7526,36 +7526,38 @@ var gRemoteTabsUI = {
 
     let newNonRemoteWindow = document.getElementById("menu_newNonRemoteWindow");
     let autostart = Services.appinfo.browserTabsRemoteAutostart;
     newNonRemoteWindow.hidden = !autostart;
   }
 };
 
 /**
- * Switch to a tab that has a given URI, and focusses its browser window.
+ * Switch to a tab that has a given URI, and focuses its browser window.
  * If a matching tab is in this window, it will be switched to. Otherwise, other
  * windows will be searched.
  *
  * @param aURI
  *        URI to search for
  * @param aOpenNew
  *        True to open a new tab and switch to it, if no existing tab is found.
  *        If no suitable window is found, a new one will be opened.
  * @param aOpenParams
  *        If switching to this URI results in us opening a tab, aOpenParams
  *        will be the parameter object that gets passed to openUILinkIn. Please
  *        see the documentation for openUILinkIn to see what parameters can be
  *        passed via this object.
  *        This object also allows:
  *        - 'ignoreFragment' property to be set to true to exclude fragment-portion
- *        matching when comparing URIs. Fragment will be replaced.
- *        - 'ignoreQueryString' property to be set to true to exclude query string
  *        matching when comparing URIs.
- *        - 'replaceQueryString' property to be set to true to exclude query string
+ *          If set to "whenComparing", the fragment will be unmodified.
+ *          If set to "whenComparingAndReplace", the fragment will be replaced.
+ *        - 'ignoreQueryString' boolean property to be set to true to exclude query string
+ *        matching when comparing URIs.
+ *        - 'replaceQueryString' boolean property to be set to true to exclude query string
  *        matching when comparing URIs and overwrite the initial query string with
  *        the one from the new URI.
  * @return True if an existing tab was found, false otherwise
  */
 function switchToTabHavingURI(aURI, aOpenNew, aOpenParams={}) {
   // Certain URLs can be switched to irrespective of the source or destination
   // window being in private browsing mode:
   const kPrivateBrowsingWhitelist = new Set([
@@ -7598,26 +7600,28 @@ function switchToTabHavingURI(aURI, aOpe
         ret = ret.split("?")[0].concat(
           (fragment != undefined) ? "#".concat(fragment) : "");
       }
       return ret;
     }
 
     // Need to handle nsSimpleURIs here too (e.g. about:...), which don't
     // work correctly with URL objects - so treat them as strings
+    let ignoreFragmentWhenComparing = typeof ignoreFragment == "string" &&
+                                      ignoreFragment.startsWith("whenComparing");
     let requestedCompare = cleanURL(
-        aURI.spec, ignoreQueryString || replaceQueryString, ignoreFragment);
+        aURI.spec, ignoreQueryString || replaceQueryString, ignoreFragmentWhenComparing);
     let browsers = aWindow.gBrowser.browsers;
     for (let i = 0; i < browsers.length; i++) {
       let browser = browsers[i];
       let browserCompare = cleanURL(
-          browser.currentURI.spec, ignoreQueryString || replaceQueryString, ignoreFragment);
+          browser.currentURI.spec, ignoreQueryString || replaceQueryString, ignoreFragmentWhenComparing);
       if (requestedCompare == browserCompare) {
         aWindow.focus();
-        if (ignoreFragment || replaceQueryString) {
+        if (ignoreFragment == "whenComparingAndReplace" || replaceQueryString) {
           browser.loadURI(aURI.spec);
         }
         aWindow.gBrowser.tabContainer.selectedIndex = i;
         return true;
       }
     }
     return false;
   }
--- a/browser/base/content/test/urlbar/browser_bug1025195_switchToTabHavingURI_aOpenParams.js
+++ b/browser/base/content/test/urlbar/browser_bug1025195_switchToTabHavingURI_aOpenParams.js
@@ -12,31 +12,35 @@ add_task(function *test_ignoreFragment()
   let numTabsAtStart = gBrowser.tabs.length;
 
   switchTab("about:home#1", true);
   switchTab("about:mozilla", true);
 
   let hashChangePromise = new Promise(resolve => {
     tabRefAboutHome.linkedBrowser.contentWindow.addEventListener("hashchange", resolve, false);
   });
-  switchTab("about:home#2", true, { ignoreFragment: true });
+  switchTab("about:home#2", true, { ignoreFragment: "whenComparingAndReplace" });
   is(tabRefAboutHome, gBrowser.selectedTab, "The same about:home tab should be switched to");
   yield hashChangePromise;
   is(gBrowser.currentURI.ref, "2", "The ref should be updated to the new ref");
   switchTab("about:mozilla", true);
+  switchTab("about:home#3", true, { ignoreFragment: "whenComparing" });
+  is(tabRefAboutHome, gBrowser.selectedTab, "The same about:home tab should be switched to");
+  is(gBrowser.currentURI.ref, "2", "The ref should be unchanged since the fragment is only ignored when comparing");
+  switchTab("about:mozilla", true);
   switchTab("about:home#1", false);
   isnot(tabRefAboutHome, gBrowser.selectedTab, "Selected tab should not be initial about:blank tab");
   is(gBrowser.tabs.length, numTabsAtStart + 1, "Should have one new tab opened");
   switchTab("about:mozilla", true);
-  switchTab("about:home", true, {ignoreFragment: true});
+  switchTab("about:home", true, {ignoreFragment: "whenComparingAndReplace"});
   yield promiseWaitForCondition(function() {
     return tabRefAboutHome.linkedBrowser.currentURI.spec == "about:home";
   });
   is(tabRefAboutHome.linkedBrowser.currentURI.spec, "about:home", "about:home shouldn't have hash");
-  switchTab("about:about", false, { ignoreFragment: true });
+  switchTab("about:about", false, { ignoreFragment: "whenComparingAndReplace" });
   cleanupTestTabs();
 });
 
 add_task(function* test_ignoreQueryString() {
   let tabRefAboutHome = gBrowser.addTab("about:home?hello=firefox");
   yield promiseTabLoaded(tabRefAboutHome);
   let tabRefAboutMozilla = gBrowser.addTab("about:mozilla");
   yield promiseTabLoaded(tabRefAboutMozilla);
@@ -78,19 +82,19 @@ add_task(function* test_replaceQueryStri
   let tabRefAboutMozilla = gBrowser.addTab("about:mozilla?hello=firefoxos#aaa");
   yield promiseTabLoaded(tabRefAboutMozilla);
   gBrowser.selectedTab = tabRefAboutMozilla;
 
   switchTab("about:home", false);
   gBrowser.removeCurrentTab();
   switchTab("about:home?hello=firefox#aaa", true);
   is(tabRefAboutHome, gBrowser.selectedTab, "Selected tab should be the initial about:home tab");
-  switchTab("about:mozilla?hello=firefox#bbb", true, { replaceQueryString: true, ignoreFragment: true });
+  switchTab("about:mozilla?hello=firefox#bbb", true, { replaceQueryString: true, ignoreFragment: "whenComparingAndReplace" });
   is(tabRefAboutMozilla, gBrowser.selectedTab, "Selected tab should be the initial about:mozilla tab");
-  switchTab("about:home?hello=firefoxos#bbb", true, { ignoreQueryString: true, ignoreFragment: true });
+  switchTab("about:home?hello=firefoxos#bbb", true, { ignoreQueryString: true, ignoreFragment: "whenComparingAndReplace" });
   is(tabRefAboutHome, gBrowser.selectedTab, "Selected tab should be the initial about:home tab");
   cleanupTestTabs();
 });
 
 add_task(function* test_ignoreQueryStringIgnoresFragment() {
   let tabRefAboutHome = gBrowser.addTab("about:home?hello=firefox#aaa");
   yield promiseTabLoaded(tabRefAboutHome);
   let tabRefAboutMozilla = gBrowser.addTab("about:mozilla?hello=firefoxos#aaa");
--- a/browser/base/content/utilityOverlay.js
+++ b/browser/base/content/utilityOverlay.js
@@ -669,17 +669,18 @@ function openPreferences(paneID, extraAr
     let supportsStringPrefURL = Cc["@mozilla.org/supports-string;1"]
                                   .createInstance(Ci.nsISupportsString);
     supportsStringPrefURL.data = preferencesURL;
     windowArguments.AppendElement(supportsStringPrefURL);
 
     win = Services.ww.openWindow(null, Services.prefs.getCharPref("browser.chromeURL"),
                                  "_blank", "chrome,dialog=no,all", windowArguments);
   } else {
-    newLoad = !win.switchToTabHavingURI(preferencesURL, true, { ignoreFragment: true, replaceQueryString: true });
+    let shouldReplaceFragment = friendlyCategoryName ? "whenComparingAndReplace" : "whenComparing";
+    newLoad = !win.switchToTabHavingURI(preferencesURL, true, { ignoreFragment: shouldReplaceFragment, replaceQueryString: true });
     browser = win.gBrowser.selectedBrowser;
   }
 
   if (newLoad) {
     Services.obs.addObserver(function advancedPaneLoadedObs(prefWin, topic, data) {
       if (!browser) {
         browser = win.gBrowser.selectedBrowser;
       }
--- a/browser/components/sessionstore/test/browser_replace_load.js
+++ b/browser/components/sessionstore/test/browser_replace_load.js
@@ -8,17 +8,17 @@ const STATE = {
 /**
  * Bug 1100223. Calling browser.loadURI() while a tab is loading causes
  * sessionstore to override the desired target URL. This test ensures that
  * calling loadURI() on a pending tab causes the tab to no longer be marked
  * as pending and correctly finish the instructed load while keeping the
  * restored history around.
  */
 add_task(function* () {
-  yield testSwitchToTab("about:mozilla#fooobar", {ignoreFragment: true});
+  yield testSwitchToTab("about:mozilla#fooobar", {ignoreFragment: "whenComparingAndReplace"});
   yield testSwitchToTab("about:mozilla?foo=bar", {replaceQueryString: true});
 });
 
 var testSwitchToTab = Task.async(function* (url, options) {
   // Create a background tab.
   let tab = gBrowser.addTab("about:blank");
   let browser = tab.linkedBrowser;
   yield promiseBrowserLoaded(browser);