Bug 1410360 - reselect original tab after closing about:devtools;r=ochameau draft
authorJulian Descottes <jdescottes@mozilla.com>
Thu, 26 Oct 2017 19:03:11 +0200
changeset 689920 d31509b2264a82d091cfdeacf8e889635f4a65b8
parent 689919 8afa81f0a700ddee3873e5fafc77daa39bb9dfd4
child 689921 1b4c4f0e1d078f88ab9f4b305d791dfebdb132d5
push id87151
push userjdescottes@mozilla.com
push dateWed, 01 Nov 2017 08:00:42 +0000
reviewersochameau
bugs1410360
milestone58.0a1
Bug 1410360 - reselect original tab after closing about:devtools;r=ochameau MozReview-Commit-ID: 3bInaF7Zeg9
devtools/shim/aboutdevtools/aboutdevtools.js
devtools/shim/devtools-startup.js
--- a/devtools/shim/aboutdevtools/aboutdevtools.js
+++ b/devtools/shim/aboutdevtools/aboutdevtools.js
@@ -16,16 +16,17 @@ const MESSAGES = {
   KeyShortcut: "key-shortcut-message",
   SystemMenu: "menu-message",
 };
 
 // URL constructor doesn't support about: scheme,
 // we have to use http in order to have working searchParams.
 let url = new URL(window.location.href.replace("about:", "http://"));
 let reason = url.searchParams.get("reason");
+let tabid = parseInt(url.searchParams.get("tabid"), 10);
 
 function getToolboxShortcut() {
   const bundleUrl = "chrome://devtools-shim/locale/key-shortcuts.properties";
   const bundle = Services.strings.createBundle(bundleUrl);
   const modifier = Services.appinfo.OS == "Darwin" ? "Cmd+Opt+" : "Ctrl+Shift+";
   return modifier + bundle.GetStringFromName("toggleToolbox.commandkey");
 }
 
@@ -70,13 +71,32 @@ window.addEventListener("load", function
 
   let installButton = document.getElementById("install");
   installButton.addEventListener("click", onInstallButtonClick);
 
   // Update the current page based on the current value of DEVTOOLS_ENABLED_PREF.
   updatePage();
 }, { once: true });
 
+window.addEventListener("beforeunload", function () {
+  // Focus the tab that triggered the DevTools onboarding.
+  if (document.visibilityState != "visible") {
+    // Only try to focus the correct tab if the current tab is the about:devtools page.
+    return;
+  }
+
+  // Retrieve the original tab if it is still available.
+  let browserWindow = Services.wm.getMostRecentWindow("navigator:browser");
+  let { gBrowser } = browserWindow;
+  let originalBrowser = gBrowser.getBrowserForOuterWindowID(tabid);
+  let originalTab = gBrowser.getTabForBrowser(originalBrowser);
+
+  if (originalTab) {
+    // If the original tab was found, select it.
+    gBrowser.selectedTab = originalTab;
+  }
+}, {once: true});
+
 window.addEventListener("unload", function () {
   let installButton = document.getElementById("install");
   installButton.removeEventListener("click", onInstallButtonClick);
   Services.prefs.removeObserver(DEVTOOLS_ENABLED_PREF, updatePage);
 }, {once: true});
--- a/devtools/shim/devtools-startup.js
+++ b/devtools/shim/devtools-startup.js
@@ -524,18 +524,29 @@ DevToolsStartup.prototype = {
           !location.startsWith("about:devtools-toolbox")) {
         // Focus the existing about:devtools tab and bail out.
         gBrowser.selectedTab = tab;
         return;
       }
     }
 
     let url = "about:devtools";
+
+    let params = [];
     if (reason) {
-      url += "?reason=" + encodeURIComponent(reason);
+      params.push("reason=" + encodeURIComponent(reason));
+    }
+
+    let selectedBrowser = gBrowser.selectedBrowser;
+    if (selectedBrowser) {
+      params.push("tabid=" + selectedBrowser.outerWindowID);
+    }
+
+    if (params.length > 0) {
+      url += "?" + params.join("&");
     }
 
     // Set relatedToCurrent: true to open the tab next to the current one.
     gBrowser.selectedTab = gBrowser.addTab(url, {relatedToCurrent: true});
   },
 
   handleConsoleFlag: function (cmdLine) {
     let window = Services.wm.getMostRecentWindow("devtools:webconsole");