Bug 606678 - use openLinkIn in nsContextMenu ("Open Link in New Tab" shouldn't add tabs to popups). r=gavin a=b
authorDão Gottwald <dao@mozilla.com>
Sat, 25 Dec 2010 00:11:45 +0100
changeset 59669 c84a2abbc66327b83335ee5d75af89af51868eca
parent 59668 133ded6f3ab509fbc23271139a9c0dab8cca2acb
child 59670 5156cf7f241144a2ca5ca63b5dfcae991491f39d
push id1
push usershaver@mozilla.com
push dateTue, 04 Jan 2011 17:58:04 +0000
reviewersgavin, b
bugs606678
milestone2.0b9pre
Bug 606678 - use openLinkIn in nsContextMenu ("Open Link in New Tab" shouldn't add tabs to popups). r=gavin a=b
browser/base/content/browser.js
browser/base/content/nsContextMenu.js
browser/base/content/test/browser_bug423833.js
browser/base/content/utilityOverlay.js
--- a/browser/base/content/browser.js
+++ b/browser/base/content/browser.js
@@ -5195,18 +5195,17 @@ function handleLinkClick(event, href, li
   if (where == "save") {
     saveURL(href, linkNode ? gatherTextUnder(linkNode) : "", null, true,
             true, doc.documentURIObject);
     event.preventDefault();
     return true;
   }
 
   urlSecurityCheck(href, doc.nodePrincipal);
-  openLinkIn(href, where, { fromContent: true,
-                            referrerURI: doc.documentURIObject,
+  openLinkIn(href, where, { referrerURI: doc.documentURIObject,
                             charset: doc.characterSet });
   event.preventDefault();
   return true;
 }
 
 function middleMousePaste(event) {
   let clipboard = readFromClipboard();
   if (!clipboard)
--- a/browser/base/content/nsContextMenu.js
+++ b/browser/base/content/nsContextMenu.js
@@ -678,53 +678,64 @@ nsContextMenu.prototype = {
              this.linkProtocol == "mailto"     ||
              this.linkProtocol == "javascript" ||
              this.linkProtocol == "news"       ||
              this.linkProtocol == "snews"      );
   },
 
   // Open linked-to URL in a new window.
   openLink : function () {
-    openNewWindowWith(this.linkURL, this.target.ownerDocument, null, false);
+    var doc = this.target.ownerDocument;
+    urlSecurityCheck(this.linkURL, doc.nodePrincipal);
+    openLinkIn(this.linkURL, "window",
+               { charset: doc.characterSet,
+                 referrerURI: doc.documentURIObject });
   },
 
   // Open linked-to URL in a new tab.
   openLinkInTab: function() {
-    openNewTabWith(this.linkURL, this.target.ownerDocument, null, null, false);
+    var doc = this.target.ownerDocument;
+    urlSecurityCheck(this.linkURL, doc.nodePrincipal);
+    openLinkIn(this.linkURL, "tab",
+               { charset: doc.characterSet,
+                 referrerURI: doc.documentURIObject });
   },
 
   // open URL in current tab
   openLinkInCurrent: function() {
-    openUILinkIn(this.linkURL, "current", null, null, 
-                 this.target.ownerDocument.documentURIObject);
+    var doc = this.target.ownerDocument;
+    urlSecurityCheck(this.linkURL, doc.nodePrincipal);
+    openLinkIn(this.linkURL, "current",
+               { charset: doc.characterSet,
+                 referrerURI: doc.documentURIObject });
   },
 
   // Open frame in a new tab.
   openFrameInTab: function() {
     var doc = this.target.ownerDocument;
     var frameURL = doc.location.href;
     var referrer = doc.referrer;
-
-    return openNewTabWith(frameURL, null, null, null, false,
-                          referrer ? makeURI(referrer) : null);
+    openLinkIn(frameURL, "tab",
+               { charset: doc.characterSet,
+                 referrerURI: referrer ? makeURI(referrer) : null });
   },
 
   // Reload clicked-in frame.
   reloadFrame: function() {
     this.target.ownerDocument.location.reload();
   },
 
   // Open clicked-in frame in its own window.
   openFrame: function() {
     var doc = this.target.ownerDocument;
     var frameURL = doc.location.href;
     var referrer = doc.referrer;
-
-    return openNewWindowWith(frameURL, null, null, false,
-                             referrer ? makeURI(referrer) : null);
+    openLinkIn(frameURL, "window",
+               { charset: doc.characterSet,
+                 referrerURI: referrer ? makeURI(referrer) : null });
   },
 
   // Open clicked-in frame in the same window.
   showOnlyThisFrame: function() {
     var doc = this.target.ownerDocument;
     var frameURL = doc.location.href;
 
     urlSecurityCheck(frameURL, this.browser.contentPrincipal,
--- a/browser/base/content/test/browser_bug423833.js
+++ b/browser/base/content/test/browser_bug423833.js
@@ -66,19 +66,22 @@ function test2Setup() {
   // Now let's do the whole thing again, but this time for "Open frame in new tab"
   var badFrame = content.frames[1];
 
   document.popupNode = badFrame.document.firstChild;
 
   var contentAreaContextMenu = document.getElementById("contentAreaContextMenu");
   var contextMenu = new nsContextMenu(contentAreaContextMenu, gBrowser);
 
-  test2tab = contextMenu.openFrameInTab();
-  ok(test2tab instanceof XULElement, "openFrameInTab() should return an element (non-null)");
-  is(test2tab.tagName, "tab", "openFrameInTab() should return a *tab* element");
+  gBrowser.tabContainer.addEventListener("TabOpen", function (event) {
+    test2tab = event.target;
+    gBrowser.tabContainer.removeEventListener("TabOpen", arguments.callee, false);
+  }, false);
+  contextMenu.openFrameInTab();
+  ok(test2tab, "openFrameInTab() opened a tab");
 
   gBrowser.selectedTab = test2tab;
 
   intervalID = setInterval(testOpenFrameInTab, 3000);
 }
 
 function testOpenFrameInTab() {
   if (gBrowser.contentDocument.location.href == "about:blank")
@@ -99,26 +102,32 @@ function testOpenFrameInTab() {
 function test3Setup() {
   // One more time, for "Open frame in new window"
   var badFrame = content.frames[1];
   document.popupNode = badFrame.document.firstChild;
 
   var contentAreaContextMenu = document.getElementById("contentAreaContextMenu");
   var contextMenu = new nsContextMenu(contentAreaContextMenu, gBrowser);
 
-  test3window = contextMenu.openFrame();
-  ok(test3window instanceof Window, "openFrame() should return a window (non-null) ");
+  Services.ww.registerNotification(function (aSubject, aTopic, aData) {
+    if (aTopic == "domwindowopened")
+      test3window = aSubject;
+    Services.ww.unregisterNotification(arguments.callee);
+  });
+
+  contextMenu.openFrame();
 
   intervalID = setInterval(testOpenFrame, 3000);
 }
 
 function testOpenFrame() {
-  if (test3window.content.location.href == "about:blank")
-    // Wait another cycle
+  if (!test3window || test3window.content.location.href == "about:blank") {
+    info("testOpenFrame: Wait another cycle");
     return;
+  }
 
   clearInterval(intervalID);
 
   is(test3window.content.location.href, invalidPage, "New window should have page url, not about:neterror");
 
   test3window.close();
   cleanup();
 }
--- a/browser/base/content/utilityOverlay.js
+++ b/browser/base/content/utilityOverlay.js
@@ -169,26 +169,26 @@ function openUILinkIn(url, where, aAllow
   } else {
     params = {
       allowThirdPartyFixup: aAllowThirdPartyFixup,
       postData: aPostData,
       referrerURI: aReferrerURI
     };
   }
 
-  params.fromContent = false;
+  params.fromChrome = true;
 
   openLinkIn(url, where, params);
 }
 
 function openLinkIn(url, where, params) {
   if (!where || !url)
     return;
 
-  var aFromContent          = params.fromContent;
+  var aFromChrome           = params.fromChrome;
   var aAllowThirdPartyFixup = params.allowThirdPartyFixup;
   var aPostData             = params.postData;
   var aCharset              = params.charset;
   var aReferrerURI          = params.referrerURI;
   var aRelatedToCurrent     = params.relatedToCurrent;
 
   if (where == "save") {
     saveURL(url, null, null, true, null, aReferrerURI);
@@ -224,31 +224,24 @@ function openLinkIn(url, where, params) 
     allowThirdPartyFixupSupports.data = aAllowThirdPartyFixup;
 
     sa.AppendElement(wuri);
     sa.AppendElement(charset);
     sa.AppendElement(aReferrerURI);
     sa.AppendElement(aPostData);
     sa.AppendElement(allowThirdPartyFixupSupports);
 
-    var ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
-             getService(Ci.nsIWindowWatcher);
-
-    ww.openWindow(w || window,
-                  getBrowserURL(),
-                  null,
-                  "chrome,dialog=no,all",
-                  sa);
-
+    Services.ww.openWindow(w || window, getBrowserURL(),
+                           null, "chrome,dialog=no,all", sa);
     return;
   }
 
-  var loadInBackground = aFromContent ?
-                         getBoolPref("browser.tabs.loadInBackground") :
-                         getBoolPref("browser.tabs.loadBookmarksInBackground");
+  var loadInBackground = aFromChrome ?
+                         getBoolPref("browser.tabs.loadBookmarksInBackground") :
+                         getBoolPref("browser.tabs.loadInBackground");
 
   if (where == "current" && w.gBrowser.selectedTab.pinned) {
     try {
       let uriObj = Services.io.newURI(url, null, null);
       if (!uriObj.schemeIs("javascript") &&
           w.gBrowser.currentURI.host != uriObj.host) {
         where = "tab";
         loadInBackground = false;
@@ -530,63 +523,52 @@ function makeURLAbsolute(aBase, aUrl)
  *        If true, then we allow the URL text to be sent to third party services
  *        (e.g., Google's I Feel Lucky) for interpretation. This parameter may
  *        be undefined in which case it is treated as false.
  * @param [optional] aReferrer
  *        If aDocument is null, then this will be used as the referrer.
  *        There will be no security check.
  */ 
 function openNewTabWith(aURL, aDocument, aPostData, aEvent,
-                        aAllowThirdPartyFixup, aReferrer)
-{
+                        aAllowThirdPartyFixup, aReferrer) {
   if (aDocument)
     urlSecurityCheck(aURL, aDocument.nodePrincipal);
 
-  var loadInBackground = getBoolPref("browser.tabs.loadInBackground");
-
-  if (aEvent && aEvent.shiftKey)
-    loadInBackground = !loadInBackground;
-
   // As in openNewWindowWith(), we want to pass the charset of the
-  // current document over to a new tab. 
-  var wintype = document.documentElement.getAttribute("windowtype");
-  var originCharset;
-  if (wintype == "navigator:browser")
+  // current document over to a new tab.
+  var originCharset = aDocument && aDocument.characterSet;
+  if (!originCharset &&
+      document.documentElement.getAttribute("windowtype") == "navigator:browser")
     originCharset = window.content.document.characterSet;
 
-  // open link in new tab
-  var referrerURI = aDocument ? aDocument.documentURIObject : aReferrer;
-  var browser = top.document.getElementById("content");
-  return browser.loadOneTab(aURL, {
-                            referrerURI: referrerURI,
-                            charset: originCharset,
-                            postData: aPostData,
-                            inBackground: loadInBackground,
-                            allowThirdPartyFixup: aAllowThirdPartyFixup});
+  openLinkIn(aURL, aEvent && aEvent.shiftKey ? "tabshifted" : "tab",
+             { charset: originCharset,
+               postData: aPostData,
+               allowThirdPartyFixup: aAllowThirdPartyFixup,
+               referrerURI: aDocument ? aDocument.documentURIObject : aReferrer });
 }
 
-function openNewWindowWith(aURL, aDocument, aPostData, aAllowThirdPartyFixup,
-                           aReferrer)
-{
+function openNewWindowWith(aURL, aDocument, aPostData, aAllowThirdPartyFixup, aReferrer) {
   if (aDocument)
     urlSecurityCheck(aURL, aDocument.nodePrincipal);
 
   // if and only if the current window is a browser window and it has a
   // document with a character set, then extract the current charset menu
   // setting from the current document and use it to initialize the new browser
   // window...
-  var charsetArg = null;
-  var wintype = document.documentElement.getAttribute("windowtype");
-  if (wintype == "navigator:browser")
-    charsetArg = "charset=" + window.content.document.characterSet;
+  var originCharset = aDocument && aDocument.characterSet;
+  if (!originCharset &&
+      document.documentElement.getAttribute("windowtype") == "navigator:browser")
+    originCharset = window.content.document.characterSet;
 
-  var referrerURI = aDocument ? aDocument.documentURIObject : aReferrer;
-  return window.openDialog(getBrowserURL(), "_blank", "chrome,all,dialog=no",
-                           aURL, charsetArg, referrerURI, aPostData,
-                           aAllowThirdPartyFixup);
+  openLinkIn(aURL, "window",
+             { charset: originCharset,
+               postData: aPostData,
+               allowThirdPartyFixup: aAllowThirdPartyFixup,
+               referrerURI: aDocument ? aDocument.documentURIObject : aReferrer });
 }
 
 /**
  * isValidFeed: checks whether the given data represents a valid feed.
  *
  * @param  aLink
  *         An object representing a feed with title, href and type.
  * @param  aPrincipal