Bug 1578067 - Part 1: Update openLinkIn() and use more - getTopWin patch. r=frg
authorIan Neal <iann_cvs@blueyonder.co.uk>
Sun, 13 Oct 2019 13:20:58 +0200
changeset 37139 cf230e323194115fc8f8a3f750ecfd67cb500d2d
parent 37138 7a012f5b023e7fbc20d44109f4af4606bceab891
child 37140 aafc1fdafb54c5ae4cd9ae6034823d58dea85d98
push id395
push userclokep@gmail.com
push dateMon, 02 Dec 2019 19:38:57 +0000
reviewersfrg
bugs1578067
Bug 1578067 - Part 1: Update openLinkIn() and use more - getTopWin patch. r=frg
suite/base/content/utilityOverlay.js
--- a/suite/base/content/utilityOverlay.js
+++ b/suite/base/content/utilityOverlay.js
@@ -6,17 +6,23 @@
 /**
  * Communicator Shared Utility Library
  * for shared application glue for the Communicator suite of applications
  **/
 
 // Services = object with smart getters for common XPCOM services
 var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
 var {XPCOMUtils} = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
-var {BrowserUtils} = ChromeUtils.import("resource://gre/modules/BrowserUtils.jsm");
+
+
+XPCOMUtils.defineLazyModuleGetters(this, {
+  BrowserUtils: "resource://gre/modules/BrowserUtils.jsm",
+  PrivateBrowsingUtils: "resource://gre/modules/PrivateBrowsingUtils.jsm",
+  RecentWindow: "resource:///modules/RecentWindow.jsm",
+});
 
 // XPCOMUtils.defineLazyGetter(this, "Weave", function() {
 //   let tmp = {};
 //   ChromeUtils.import("resource://services-sync/main.js", tmp);
 //   return tmp.Weave;
 // });
 
 /*
@@ -561,19 +567,27 @@ function toolboxCustomizeChange(toolbox,
 
 function goClickThrobber(urlPref, aEvent)
 {
   var url = GetLocalizedStringPref(urlPref);
   if (url)
     openUILinkIn(url, whereToOpenLink(aEvent, false, true, true));
 }
 
-function getTopWin()
-{
-  return top.gPrivate || Services.wm.getMostRecentWindow("navigator:browser");
+function getTopWin(skipPopups) {
+  // If this is called in a browser window, use that window regardless of
+  // whether it's the frontmost window, since commands can be executed in
+  // background windows (bug 626148).
+  if (top.document.documentElement.getAttribute("windowtype") == "navigator:browser" &&
+      (!skipPopups || top.toolbar.visible))
+    return top;
+
+  let isPrivate = PrivateBrowsingUtils.isWindowPrivate(window);
+  return RecentWindow.getMostRecentBrowserWindow({private: isPrivate,
+                                                  allowPopups: !skipPopups});
 }
 
 function isRestricted( url )
 {
   try {
     const nsIURIFixup = Ci.nsIURIFixup;
     var uri = Cc["@mozilla.org/docshell/urifixup;1"]
                 .getService(nsIURIFixup)
@@ -1520,17 +1534,24 @@ function openLinkIn(url, where, params)
 
   // TODO fix this properly with pricipals and referrers intact
   if (where == "private") {
     window.openDialog(getBrowserURL(), "_blank", "private,chrome,all,dialog=no",
                       url, null, null, aPostData, aAllowThirdPartyFixup);
     return null;
   }
 
+  // Establish which window we'll load the link in.
   var w = getTopWin();
+  // We don't want to open tabs in popups, so try to find a non-popup window in
+  // that case.
+  if ((where == "tab" || where == "tabshifted") && w && !w.toolbar.visible) {
+    w = getTopWin(true);
+    aRelatedToCurrent = false;
+  }
 
   // Teach the principal about the right OA to use, e.g. in case when
   // opening a link in a new private window, or in a new container tab.
   // Please note we do not have to do that for SystemPrincipals and we
   // can not do it for NullPrincipals since NullPrincipals are only
   // identical if they actually are the same object (See Bug: 1346759)
   function useOAForPrincipal(principal) {
     if (principal && principal.isCodebasePrincipal) {