Bug 612861: don't throw when accessing random properties off window.console, r=bz, a=blocking
authorGavin Sharp <gavin@gavinsharp.com>
Wed, 17 Nov 2010 15:20:20 -0500
changeset 57654 f31dab55e41a57bed39fe809a764935b830bacf9
parent 57653 0281473dbd79e0e01e52f130e96b790a0b60d416
child 57655 a65974f1daad1b2131870ee9e6613519d4527150
push id17026
push usergsharp@mozilla.com
push dateWed, 17 Nov 2010 20:24:26 +0000
treeherdermozilla-central@f31dab55e41a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbz, blocking
bugs612861
milestone2.0b8pre
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 612861: don't throw when accessing random properties off window.console, r=bz, a=blocking
dom/base/ConsoleAPI.js
dom/tests/mochitest/general/Makefile.in
dom/tests/mochitest/general/test_consoleAPI.html
--- a/dom/base/ConsoleAPI.js
+++ b/dom/base/ConsoleAPI.js
@@ -43,64 +43,60 @@ Cu.import("resource://gre/modules/Servic
 
 function ConsoleAPI() {}
 ConsoleAPI.prototype = {
 
   classID: Components.ID("{b49c18f8-3379-4fc0-8c90-d7772c1a9ff3}"),
 
   QueryInterface: XPCOMUtils.generateQI([Ci.nsIDOMGlobalPropertyInitializer]),
 
-  // The ID of our attached window
-  id: null,
-
   // nsIDOMGlobalPropertyInitializer
-  // Associates us with the window we're attached to.
   init: function CA_init(aWindow) {
+    let id;
     try {
-      this.id = aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
-                       .getInterface(Ci.nsIDOMWindowUtils)
-                       .outerWindowID;
+      id = aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
+                  .getInterface(Ci.nsIDOMWindowUtils)
+                  .outerWindowID;
     } catch (ex) {
       Cu.reportError(ex);
     }
 
-    return this;
-  },
-
-  // window.console API
-  log: function CA_log() {
-    this.notifyObservers("log", arguments);
-  },
-  info: function CA_info() {
-    this.notifyObservers("info", arguments);
-  },
-  warn: function CA_warn() {
-    this.notifyObservers("warn", arguments);
-  },
-  error: function CA_error() {
-    this.notifyObservers("error", arguments);
-  },
-
-  __exposedProps__: {
-    log: "r",
-    info: "r",
-    warn: "r",
-    error: "r"
+    let self = this;
+    return {
+      // window.console API
+      log: function CA_log() {
+        self.notifyObservers(id, "log", arguments);
+      },
+      info: function CA_info() {
+        self.notifyObservers(id, "info", arguments);
+      },
+      warn: function CA_warn() {
+        self.notifyObservers(id, "warn", arguments);
+      },
+      error: function CA_error() {
+        self.notifyObservers(id, "error", arguments);
+      },
+      // TODO: remove this once bug 612405 is fixed
+      classID: self.classID
+    };
   },
 
   /**
    * Notify all observers of any console API call
    **/
-  notifyObservers: function CA_notifyObservers(aLevel, aArguments) {
+  notifyObservers: function CA_notifyObservers(aID, aLevel, aArguments) {
+    if (!aID)
+      return;
+
     let consoleEvent = {
-      ID: this.id,
+      ID: aID,
       level: aLevel,
       arguments: aArguments
     };
 
     consoleEvent.wrappedJSObject = consoleEvent;
 
     Services.obs.notifyObservers(consoleEvent,
-                                 "console-api-log-event", this.id);
+                                 "console-api-log-event", aID);
   }
 };
 
 let NSGetFactory = XPCOMUtils.generateNSGetFactory([ConsoleAPI]);
--- a/dom/tests/mochitest/general/Makefile.in
+++ b/dom/tests/mochitest/general/Makefile.in
@@ -44,16 +44,17 @@ relativesrcdir  = dom/tests/mochitest/ge
 include $(DEPTH)/config/autoconf.mk
 include $(topsrcdir)/config/rules.mk
 
 _TEST_FILES = \
 		497633.html \
 		489127.html \
 		test_497898.html \
 		test_bug504220.html \
+		test_consoleAPI.html \
 		test_domWindowUtils.html \
 		test_domWindowUtils_scrollXY.html \
 		test_innerScreen.xul \
 		test_offsets.html \
 		test_offsets.js \
 		test_offsets.xul \
 		test_windowProperties.html \
 		test_clipboard_events.html \
new file mode 100644
--- /dev/null
+++ b/dom/tests/mochitest/general/test_consoleAPI.html
@@ -0,0 +1,49 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <title>window.console test</title>
+  <script type="text/javascript" src="/MochiKit/packed.js"></script>
+  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css">
+</head>
+
+<body id="body">
+
+<script type="application/javascript;version=1.8">
+
+function doTest() {
+  ok(window.console, "console exists");
+
+  try {
+    ok(!console.foo, "random property doesn't throw");
+  } catch (ex) {
+    ok(false, "random property threw: " + ex);
+  }
+
+  var expectedProps = {
+    "log": "function",
+    "info": "function",
+    "warn": "function",
+    "error": "function",
+    "classID": "object" // XXX Bug 612405
+  };
+
+  var foundProps = 0;
+  for (var prop in console) {
+    foundProps++;
+    is(typeof(console[prop]), expectedProps[prop], "expect console prop " + prop + " exists");
+  }
+  is(foundProps, Object.keys(expectedProps).length, "found correct number of properties");
+
+  SimpleTest.finish();
+}
+
+SimpleTest.waitForExplicitFinish();
+addLoadEvent(doTest);
+
+</script>
+
+<p id="display"></p>
+
+</body>
+</html>