Bug 965860 - patch 3 - Console replacable in IDL, r=bz
authorAndrea Marchesini <amarchesini@mozilla.com>
Thu, 27 Feb 2014 23:39:06 +0000
changeset 171459 428fa1a58cf365e5844c50bcb68c60f4fa2e0bd8
parent 171458 3f528e61aacfeb743a6fab6fb47a9bfc10e282e2
child 171460 39be88a5ce66f4e806515f607cc9b13352ad3870
push id270
push userpvanderbeken@mozilla.com
push dateThu, 06 Mar 2014 09:24:21 +0000
reviewersbz
bugs965860
milestone30.0a1
Bug 965860 - patch 3 - Console replacable in IDL, r=bz
dom/base/nsGlobalWindow.cpp
dom/base/test/chrome.ini
dom/base/test/test_console.xul
dom/interfaces/base/nsIDOMWindow.idl
--- a/dom/base/nsGlobalWindow.cpp
+++ b/dom/base/nsGlobalWindow.cpp
@@ -13361,22 +13361,54 @@ nsGlobalWindow::SetHasAudioAvailableEven
   MOZ_ASSERT(IsInnerWindow());
 
   if (mDoc) {
     mDoc->NotifyAudioAvailableListener();
   }
 }
 
 NS_IMETHODIMP
-nsGlobalWindow::GetConsole(nsISupports** aConsole)
+nsGlobalWindow::GetConsole(JSContext* aCx,
+                           JS::MutableHandle<JS::Value> aConsole)
 {
   ErrorResult rv;
   nsRefPtr<Console> console = GetConsole(rv);
-  console.forget(aConsole);
-  return rv.ErrorCode();
+  if (rv.Failed()) {
+    return rv.ErrorCode();
+  }
+
+  JS::Rooted<JSObject*> thisObj(aCx, mJSObject);
+  if (!mJSObject) {
+    return NS_ERROR_UNEXPECTED;
+  }
+
+  if (!JS_WrapObject(aCx, &thisObj) ||
+      !WrapNewBindingObject(aCx, thisObj, console, aConsole)) {
+    return NS_ERROR_FAILURE;
+  }
+
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+nsGlobalWindow::SetConsole(JSContext* aCx, JS::Handle<JS::Value> aValue)
+{
+  JS::Rooted<JSObject*> thisObj(aCx, mJSObject);
+  if (!mJSObject) {
+    return NS_ERROR_UNEXPECTED;
+  }
+
+  if (!JS_WrapObject(aCx, &thisObj) ||
+      !JS_DefineProperty(aCx, thisObj, "console", aValue,
+                         JS_PropertyStub, JS_StrictPropertyStub,
+                         JSPROP_ENUMERATE)) {
+    return NS_ERROR_FAILURE;
+  }
+
+  return NS_OK;
 }
 
 Console*
 nsGlobalWindow::GetConsole(ErrorResult& aRv)
 {
   FORWARD_TO_INNER_OR_THROW(GetConsole, (aRv), aRv, nullptr);
 
   if (!mConsole) {
--- a/dom/base/test/chrome.ini
+++ b/dom/base/test/chrome.ini
@@ -1,8 +1,9 @@
 [DEFAULT]
 support-files =
   file_url.jsm
 
 [test_bug715041.xul]
 [test_bug715041_removal.xul]
 [test_domrequesthelper.xul]
 [test_url.xul]
+[test_console.xul]
new file mode 100644
--- /dev/null
+++ b/dom/base/test/test_console.xul
@@ -0,0 +1,34 @@
+<?xml version="1.0"?>
+<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
+<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
+<window title="Test for URL API"
+        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
+  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
+
+  <!-- test results are displayed in the html:body -->
+  <body xmlns="http://www.w3.org/1999/xhtml">
+    <iframe id="iframe" />
+  </body>
+
+  <!-- test code goes here -->
+  <script type="application/javascript"><![CDATA[
+
+  ok("console" in window, "Console exists");
+  window.console.log(42);
+  window.console = 42;
+  is(window.console, 42, "Console is replacable");
+
+  var frame = document.getElementById("iframe");
+  ok(frame, "Frame must exist");
+  frame.src="http://mochi.test:8888/tests/dom/base/test/file_empty.html";
+  frame.onload = function() {
+    ok("console" in frame.contentWindow, "Console exists in the iframe");
+    frame.contentWindow.console.log(42);
+    frame.contentWindow.console = 42;
+    is(frame.contentWindow.console, 42, "Console is replacable in the iframe");
+    SimpleTest.finish();
+  }
+
+  SimpleTest.waitForExplicitFinish();
+  ]]></script>
+</window>
--- a/dom/interfaces/base/nsIDOMWindow.idl
+++ b/dom/interfaces/base/nsIDOMWindow.idl
@@ -503,17 +503,17 @@ interface nsIDOMWindow : nsISupports
   [implicit_jscontext] attribute jsval ondevicelight;
 
   [implicit_jscontext] attribute jsval onmouseenter;
   [implicit_jscontext] attribute jsval onmouseleave;
 
   /**
    * Console API
    */
-  readonly attribute nsISupports console;
+  [implicit_jscontext] attribute jsval console;
 };
 
 [scriptable, uuid(2146c906-57f7-486c-a1b4-8cdb57ef577f)]
 interface nsIDOMWindowPerformance : nsISupports
 {
   /**
    * A namespace to hold performance related data and statistics.
    */