Bug 678816 - web console re-attaches console to non-tab-browser contentWindows; r=msucan
authorTim Taubert <tim.taubert@gmx.de>
Wed, 17 Aug 2011 23:03:41 +0200
changeset 75476 c0e7a77e62d85c975092318f2ecbc1b16937d1f1
parent 75475 7de8e7bce4432323fa5af303e52e1f5a0df1e475
child 75477 932c8414512f32c41e0d3eb9c9e483658ed7ab27
push id2
push userbsmedberg@mozilla.com
push dateFri, 19 Aug 2011 14:38:13 +0000
reviewersmsucan
bugs678816
milestone9.0a1
Bug 678816 - web console re-attaches console to non-tab-browser contentWindows; r=msucan
browser/devtools/webconsole/HUDService.jsm
browser/devtools/webconsole/test/browser/Makefile.in
browser/devtools/webconsole/test/browser/browser_webconsole_bug_678816.js
browser/devtools/webconsole/test/browser/test-bug-678816-content.js
--- a/browser/devtools/webconsole/HUDService.jsm
+++ b/browser/devtools/webconsole/HUDService.jsm
@@ -2703,16 +2703,22 @@ HUD_SERVICE.prototype =
       // This may be a view-source window or other type of non-browser window.
       return;
     }
 
     let gBrowser = xulWindow.gBrowser;
 
     let _browser = gBrowser.
       getBrowserForDocument(aContentWindow.top.document);
+
+    // ignore newly created documents that don't belong to a tab's browser
+    if (!_browser) {
+      return;
+    }
+
     let nBox = gBrowser.getNotificationBox(_browser);
     let nBoxId = nBox.getAttribute("id");
     let hudId = "hud_" + nBoxId;
     let windowUI = nBox.ownerDocument.getElementById("console_window_" + hudId);
     if (windowUI) {
       // The Web Console popup is already open, no need to continue.
       if (aContentWindow == aContentWindow.top) {
         let hud = this.hudReferences[hudId];
--- a/browser/devtools/webconsole/test/browser/Makefile.in
+++ b/browser/devtools/webconsole/test/browser/Makefile.in
@@ -139,16 +139,17 @@ include $(topsrcdir)/config/rules.mk
 	browser_webconsole_bug_642615_autocomplete.js \
 	browser_webconsole_bug_585991_autocomplete_popup.js \
 	browser_webconsole_bug_585991_autocomplete_keys.js \
 	browser_webconsole_bug_663443_panel_title.js \
 	browser_webconsole_bug_660806_history_nav.js \
 	browser_webconsole_bug_651501_document_body_autocomplete.js \
 	browser_webconsole_bug_653531_highlighter_console_helper.js \
 	browser_webconsole_bug_659907_console_dir.js \
+	browser_webconsole_bug_678816.js \
 	head.js \
 	$(NULL)
 
 _BROWSER_TEST_PAGES = \
 	test-console.html \
 	test-network.html \
 	test-network-request.html \
 	test-mutation.html \
@@ -207,16 +208,17 @@ include $(topsrcdir)/config/rules.mk
 	test-bug-618078-network-exceptions.html \
 	test-bug-630733-response-redirect-headers.sjs \
 	test-bug-621644-jsterm-dollar.html \
 	test-bug-632347-iterators-generators.html \
 	test-bug-585956-console-trace.html \
 	test-bug-644419-log-limits.html \
 	test-bug-632275-getters.html \
 	test-bug-646025-console-file-location.html \
+	test-bug-678816-content.js \
 	test-file-location.js \
 	$(NULL)
 
 libs:: $(_BROWSER_TEST_FILES)
 	$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/browser/$(relativesrcdir)
 
 libs:: $(_BROWSER_TEST_PAGES)
 	$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/browser/$(relativesrcdir)
new file mode 100644
--- /dev/null
+++ b/browser/devtools/webconsole/test/browser/browser_webconsole_bug_678816.js
@@ -0,0 +1,62 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+const TEST_URI = "http://example.com/browser/browser/devtools/webconsole/test/browser/test-console.html";
+const FRAME_SCRIPT_URI ="chrome://mochitests/content/browser/browser/devtools/webconsole/test/browser/test-bug-678816-content.js";
+
+let HUD;
+let outputItem;
+
+function tabLoad1(aEvent) {
+  browser.removeEventListener(aEvent.type, arguments.callee, true);
+
+  openConsole();
+  HUD = HUDService.getHudByWindow(content);
+
+  browser.addEventListener("load", tabLoad2, true);
+
+  // Reload so we get some output in the console.
+  browser.contentWindow.location.reload();
+}
+
+function tabLoad2(aEvent) {
+  browser.removeEventListener(aEvent.type, tabLoad2, true);
+
+  outputItem = HUD.outputNode.querySelector(".hud-networkinfo .hud-clickable");
+  ok(outputItem, "found a network message");
+  document.addEventListener("popupshown", networkPanelShown, false);
+
+  // Click the network message to open the network panel.
+  EventUtils.synthesizeMouseAtCenter(outputItem, {});
+}
+
+function networkPanelShown(aEvent) {
+  document.removeEventListener(aEvent.type, networkPanelShown, false);
+
+  executeSoon(function() {
+    aEvent.target.addEventListener("popuphidden", networkPanelHidden, false);
+    aEvent.target.hidePopup();
+  });
+}
+
+function networkPanelHidden(aEvent) {
+  this.removeEventListener(aEvent.type, networkPanelHidden, false);
+
+  is(HUD.contentWindow, browser.contentWindow,
+    "console has not been re-attached to the wrong window");
+
+  finishTest();
+}
+
+function test() {
+  messageManager.loadFrameScript(FRAME_SCRIPT_URI, true);
+
+  registerCleanupFunction(function () {
+    // There's no way to unload a frameScript so send a kill signal to
+    // unregister the frame script's webProgressListener
+    messageManager.sendAsyncMessage("bug-678816-kill-webProgressListener");
+  });
+
+  addTab(TEST_URI);
+  browser.addEventListener("load", tabLoad1, true);
+}
new file mode 100644
--- /dev/null
+++ b/browser/devtools/webconsole/test/browser/test-bug-678816-content.js
@@ -0,0 +1,28 @@
+(function () {
+  let ifaceReq = docShell.QueryInterface(Ci.nsIInterfaceRequestor);
+  let webProgress = ifaceReq.getInterface(Ci.nsIWebProgress);
+
+  let WebProgressListener = {
+    onStateChange: function WebProgressListener_onStateChange(
+      webProgress, request, flag, status) {
+
+      if (flag & Ci.nsIWebProgressListener.STATE_START &&
+          flag & Ci.nsIWebProgressListener.STATE_IS_WINDOW) {
+        // ensure the dom window is the top one
+        return (webProgress.DOMWindow.parent == webProgress.DOMWindow);
+      }
+    },
+
+    // ----------
+    // Implements progress listener interface.
+    QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebProgressListener,
+                                           Ci.nsISupportsWeakReference])
+  };
+
+  // add web progress listener
+  webProgress.addProgressListener(WebProgressListener, Ci.nsIWebProgress.NOTIFY_STATE_ALL);
+
+  addMessageListener("bug-678816-kill-webProgressListener", function () {
+    webProgress.removeProgressListener(WebProgressListener);
+  });
+})();