From c99d83fa88c714254ff93215788c2a9a1a2c55aa Mon Sep 17 00:00:00 2001 From: Martin Thomson Date: Fri, 28 Aug 2015 15:40:44 -0700 Subject: [PATCH] Bug 1176941 - Moving console warning to the current window, r=jib --HG-- extra : rebase_source : 2f5845ec7055c8b748cda64a61a2cb313e980a57 --- dom/media/IdpSandbox.jsm | 38 ++++++++++++------- dom/media/PeerConnectionIdp.jsm | 2 +- .../mochitest/identity/test_fingerprints.html | 2 +- .../mochitest/identity/test_idpproxy.html | 6 +-- 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/dom/media/IdpSandbox.jsm b/dom/media/IdpSandbox.jsm index 61069223bc1e..96b12eeebab2 100644 --- a/dom/media/IdpSandbox.jsm +++ b/dom/media/IdpSandbox.jsm @@ -113,14 +113,14 @@ function createLocationFromURI(uri) { * * @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) { @@ -181,7 +181,7 @@ IdpSandbox.prototype = { 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; @@ -222,19 +222,12 @@ IdpSandbox.prototype = { 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'); } @@ -244,6 +237,23 @@ IdpSandbox.prototype = { 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); diff --git a/dom/media/PeerConnectionIdp.jsm b/dom/media/PeerConnectionIdp.jsm index 0248346e2113..471d09815485 100644 --- a/dom/media/PeerConnectionIdp.jsm +++ b/dom/media/PeerConnectionIdp.jsm @@ -56,7 +56,7 @@ PeerConnectionIdp.prototype = { } 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 diff --git a/dom/media/tests/mochitest/identity/test_fingerprints.html b/dom/media/tests/mochitest/identity/test_fingerprints.html index ffe6f072c7ab..8aa4ad4ead25 100644 --- a/dom/media/tests/mochitest/identity/test_fingerprints.html +++ b/dom/media/tests/mochitest/identity/test_fingerprints.html @@ -12,7 +12,7 @@ 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 }), diff --git a/dom/media/tests/mochitest/identity/test_idpproxy.html b/dom/media/tests/mochitest/identity/test_idpproxy.html index b5d2a3b31158..c8860d1f4e38 100644 --- a/dom/media/tests/mochitest/identity/test_idpproxy.html +++ b/dom/media/tests/mochitest/identity/test_idpproxy.html @@ -26,7 +26,7 @@ function test_domain_sandbox() { '', 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; @@ -40,7 +40,7 @@ function test_protocol_sandbox() { '\\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; @@ -56,7 +56,7 @@ function idpName(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)); }