Bug 1176941 - Moving console warning to the current window, r=jib
authorMartin Thomson <martin.thomson@gmail.com>
Fri, 28 Aug 2015 15:40:44 -0700
changeset 294897 9a5fd09d1589e8d37c1db651896f5a2e480d9e5d
parent 294896 c4595abfd436b11fd33ddcdb3f63e634543c0115
child 294898 827a12863a6cde252e0fc47199562b78c0eed55e
push id5245
push userraliiev@mozilla.com
push dateThu, 29 Oct 2015 11:30:51 +0000
treeherdermozilla-beta@dac831dc1bd0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjib
bugs1176941
milestone43.0a1
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 1176941 - Moving console warning to the current window, r=jib
dom/media/IdpSandbox.jsm
dom/media/PeerConnectionIdp.jsm
dom/media/tests/mochitest/identity/test_fingerprints.html
dom/media/tests/mochitest/identity/test_idpproxy.html
--- a/dom/media/IdpSandbox.jsm
+++ b/dom/media/IdpSandbox.jsm
@@ -108,24 +108,24 @@ function createLocationFromURI(uri) {
   };
 }
 
 /**
  * A javascript sandbox for running an IdP.
  *
  * @param domain (string) the domain of the IdP
  * @param protocol (string?) the protocol of the IdP [default: 'default']
- * @param doc (obj) the current document
+ * @param win (obj) the current window
  * @throws if the domain or protocol aren't valid
  */
-function IdpSandbox(domain, protocol, doc) {
+function IdpSandbox(domain, protocol, win) {
   this.source = IdpSandbox.createIdpUri(domain, protocol || "default");
   this.active = null;
   this.sandbox = null;
-  this.document = doc;
+  this.window = win;
 }
 
 IdpSandbox.checkDomain = function(domain) {
   if (!domain || typeof domain !== 'string') {
     throw new Error('Invalid domain for identity provider: ' +
                     'must be a non-zero length string');
   }
 };
@@ -176,17 +176,17 @@ IdpSandbox.createIdpUri = function(domai
 
 IdpSandbox.prototype = {
   isSame: function(domain, protocol) {
     return this.source.spec === IdpSandbox.createIdpUri(domain, protocol).spec;
   },
 
   start: function() {
     if (!this.active) {
-      this.active = ResourceLoader.load(this.source, this.document)
+      this.active = ResourceLoader.load(this.source, this.window.document)
         .then(result => this._createSandbox(result));
     }
     return this.active;
   },
 
   // Provides the sandbox with some useful facilities.  Initially, this is only
   // a minimal set; it is far easier to add more as the need arises, than to
   // take them back if we discover a mistake.
@@ -217,38 +217,48 @@ IdpSandbox.prototype = {
 
     // have to use the ultimate URI, not the starting one to avoid
     // that origin stealing from the one that redirected to it
     this._populateSandbox(result.request.URI);
     try {
       Cu.evalInSandbox(result.data, this.sandbox,
                        'latest', result.request.URI.spec, 1);
     } catch (e) {
+      // These can be passed straight on, because they are explicitly labelled
+      // as being IdP errors by the IdP and we drop line numbers as a result.
       if (e.name === 'IdpError' || e.name === 'IdpLoginError') {
         throw e;
       }
-      // Capture all the details from the error and log them to the console.
-      // This can't rethrow anything else because that could leak information
-      // about the internal workings of the IdP across origins.
-      let scriptErrorClass = Cc["@mozilla.org/scripterror;1"];
-      let scriptError = scriptErrorClass.createInstance(Ci.nsIScriptError);
-      scriptError.init(e.message, e.fileName, null, e.lineNumber, e.columnNumber,
-                       Ci.nsIScriptError.errorFlag, "content javascript");
-      let consoleService = Cc['@mozilla.org/consoleservice;1']
-          .getService(Ci.nsIConsoleService);
-      consoleService.logMessage(scriptError);
+      this._logError(e);
       throw new Error('Error in IdP, check console for details');
     }
 
     if (!registrar.idp) {
       throw new Error('IdP failed to call rtcIdentityProvider.register()');
     }
     return registrar;
   },
 
+  // Capture all the details from the error and log them to the console.  This
+  // can't rethrow anything else because that could leak information about the
+  // internal workings of the IdP across origins.
+  _logError: function(e) {
+    let winID = this.window.QueryInterface(Ci.nsIInterfaceRequestor)
+        .getInterface(Ci.nsIDOMWindowUtils).currentInnerWindowID;
+    let scriptError = Cc["@mozilla.org/scripterror;1"]
+        .createInstance(Ci.nsIScriptError);
+    scriptError.initWithWindowID(e.message, e.fileName, null,
+                                 e.lineNumber, e.columnNumber,
+                                 Ci.nsIScriptError.errorFlag,
+                                 "content javascript", winID);
+    let consoleService = Cc['@mozilla.org/consoleservice;1']
+        .getService(Ci.nsIConsoleService);
+    consoleService.logMessage(scriptError);
+  },
+
   stop: function() {
     if (this.sandbox) {
       Cu.nukeSandbox(this.sandbox);
     }
     this.sandbox = null;
     this.active = null;
   },
 
--- a/dom/media/PeerConnectionIdp.jsm
+++ b/dom/media/PeerConnectionIdp.jsm
@@ -51,17 +51,17 @@ PeerConnectionIdp.prototype = {
     this.protocol = protocol || 'default';
     this.username = username;
     if (this._idp) {
       if (this._idp.isSame(provider, protocol)) {
         return; // noop
       }
       this._idp.stop();
     }
-    this._idp = new IdpSandbox(provider, protocol, this._win.document);
+    this._idp = new IdpSandbox(provider, protocol, this._win);
   },
 
   // start the IdP and do some error fixup
   start: function() {
     return this._idp.start()
       .catch(e => {
         throw new this._win.DOMException(e.message, 'IdpError');
       });
--- a/dom/media/tests/mochitest/identity/test_fingerprints.html
+++ b/dom/media/tests/mochitest/identity/test_fingerprints.html
@@ -7,17 +7,17 @@
 <body>
   <script class="testbody" type="application/javascript">
 'use strict';
 
 // here we call the identity provider directly
 function getIdentityAssertion(fpArray) {
   var Cu = SpecialPowers.Cu;
   var rtcid = Cu.import('resource://gre/modules/media/IdpSandbox.jsm');
-  var sandbox = new rtcid.IdpSandbox('example.com', 'idp.js', window.document);
+  var sandbox = new rtcid.IdpSandbox('example.com', 'idp.js', window);
   return sandbox.start()
     .then(idp => SpecialPowers.wrap(idp)
                    .generateAssertion(JSON.stringify({ fingerprint: fpArray }),
                                       'https://example.com'))
     .then(assertion => {
       assertion = SpecialPowers.wrap(assertion);
       var assertionString = btoa(JSON.stringify(assertion));
       sandbox.stop();
--- a/dom/media/tests/mochitest/identity/test_idpproxy.html
+++ b/dom/media/tests/mochitest/identity/test_idpproxy.html
@@ -21,47 +21,47 @@ function test_domain_sandbox() {
     toString : function() {
       return 'example.com/path';
     }
   };
   var domains = [ 'ex/foo', 'user@ex', 'user:pass@ex', 'ex#foo', 'ex?foo',
                   '', 12, null, diabolical, true ];
   domains.forEach(function(domain) {
     try {
-      var idp = new IdpSandbox(domain, undefined, window.document);
+      var idp = new IdpSandbox(domain, undefined, window);
       ok(false, 'IdpSandbox allowed a bad domain: ' + domain);
     } catch (e) {
       var str = (typeof domain === 'string') ? domain : typeof domain;
       ok(true, 'Evil domain "' + str + '" raises exception');
     }
   });
 }
 
 function test_protocol_sandbox() {
   var protos = [ '../evil/proto', '..%2Fevil%2Fproto',
                  '\\evil', '%5cevil', 12, true, {} ];
   protos.forEach(function(proto) {
     try {
-      var idp = new IdpSandbox('example.com', proto, window.document);
+      var idp = new IdpSandbox('example.com', proto, window);
       ok(false, 'IdpSandbox allowed a bad protocol: ' + proto);
     } catch (e) {
       var str = (typeof proto === 'string') ? proto : typeof proto;
       ok(true, 'Evil protocol "' + proto + '" raises exception');
     }
   });
 }
 
 function idpName(hash) {
   return 'idp.js' + (hash ? ('#' + hash) : '');
 }
 
 function makeSandbox(js) {
   var name = js || idpName();
   info('Creating a sandbox for the protocol: ' + name);
-  var sandbox = new IdpSandbox('example.com', name, window.document);
+  var sandbox = new IdpSandbox('example.com', name, window);
   return sandbox.start().then(idp => SpecialPowers.wrap(idp));
 }
 
 function test_generate_assertion() {
   return makeSandbox()
     .then(idp => idp.generateAssertion(dummyPayload,
                                        'https://example.net'))
     .then(response => {