From 685e5f171496598ef1fb30a61e8eaac6fd066563 Mon Sep 17 00:00:00 2001 From: David Dahl Date: Fri, 10 Sep 2010 17:15:48 -0700 Subject: [PATCH] Bug 592879 - Detect and warn users that the current window.console API is third party r=dietrich a=beta6+ --- .../console/hudservice/HUDService.jsm | 39 ++++++ .../hudservice/tests/browser/Makefile.in | 2 + .../browser_warn_user_about_replaced_api.js | 114 ++++++++++++++++++ .../browser/test-console-replaced-api.html | 15 +++ .../chrome/global/headsUpDisplay.properties | 1 + 5 files changed, 171 insertions(+) create mode 100644 toolkit/components/console/hudservice/tests/browser/browser_warn_user_about_replaced_api.js create mode 100644 toolkit/components/console/hudservice/tests/browser/test-console-replaced-api.html diff --git a/toolkit/components/console/hudservice/HUDService.jsm b/toolkit/components/console/hudservice/HUDService.jsm index beb2e66f59e..03982200b88 100644 --- a/toolkit/components/console/hudservice/HUDService.jsm +++ b/toolkit/components/console/hudservice/HUDService.jsm @@ -1961,6 +1961,45 @@ HUD_SERVICE.prototype = } }, + /** + * Get OutputNode by Id + * + * @param string aId + * @returns nsIDOMNode + */ + getConsoleOutputNode: function HS_getConsoleOutputNode(aId) + { + let displayNode = this.getHeadsUpDisplay(aHUDId); + return displayNode.querySelectorAll(".hud-output-node")[0]; + }, + + /** + * Inform user that the Web Console API has been replaced by a script + * in a content page. + * + * @param string aHUDId + * @returns void + */ + logWarningAboutReplacedAPI: + function HS_logWarningAboutReplacedAPI(aHUDId) + { + let domId = "hud-log-node-" + this.sequenceId(); + let outputNode = this.getConsoleOutputNode(aHUDId); + + let msgFormat = { + logLevel: "error", + activityObject: {}, + hudId: aHUDId, + origin: "console-listener", + domId: domId, + message: this.getStr("ConsoleAPIDisabled"), + }; + + let messageObject = + this.messageFactory(msgFormat, "error", outputNode, msgFormat.activityObject); + this.logMessage(messageObject.messageObject, outputNode, messageObject.messageNode); + }, + /** * report consoleMessages recieved via the HUDConsoleObserver service * @param nsIConsoleMessage aConsoleMessage diff --git a/toolkit/components/console/hudservice/tests/browser/Makefile.in b/toolkit/components/console/hudservice/tests/browser/Makefile.in index 80124c9e590..6c39fdaf4cc 100644 --- a/toolkit/components/console/hudservice/tests/browser/Makefile.in +++ b/toolkit/components/console/hudservice/tests/browser/Makefile.in @@ -54,6 +54,7 @@ _BROWSER_TEST_FILES = \ browser_webconsole_netlogging.js \ browser_webconsole_bug_593003_iframe_wrong_hud.js \ browser_webconsole_bug_581231_close_button.js \ + browser_warn_user_about_replaced_api.js \ $(NULL) _BROWSER_TEST_PAGES = \ @@ -72,6 +73,7 @@ _BROWSER_TEST_PAGES = \ test-encoding-ISO-8859-1.html \ test-bug-593003-iframe-wrong-hud.html \ test-bug-593003-iframe-wrong-hud-iframe.html \ + test-console-replaced-api.html \ $(NULL) libs:: $(_BROWSER_TEST_FILES) diff --git a/toolkit/components/console/hudservice/tests/browser/browser_warn_user_about_replaced_api.js b/toolkit/components/console/hudservice/tests/browser/browser_warn_user_about_replaced_api.js new file mode 100644 index 00000000000..bdcc69c3032 --- /dev/null +++ b/toolkit/components/console/hudservice/tests/browser/browser_warn_user_about_replaced_api.js @@ -0,0 +1,114 @@ +/* vim:set ts=2 sw=2 sts=2 et: */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is DevTools test code. + * + * The Initial Developer of the Original Code is Mozilla Foundation. + * Portions created by the Initial Developer are Copyright (C) 2010 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * David Dahl + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +const Cc = Components.classes; +const Ci = Components.interfaces; +const Cu = Components.utils; + +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); +Cu.import("resource://gre/modules/HUDService.jsm"); + +const TEST_REPLACED_API_URI = "http://example.com/browser/toolkit/components/console/hudservice/tests/browser/test-console-replaced-api.html"; + +function log(aMsg) +{ + dump("*** WebConsoleTest: " + aMsg + "\n"); +} + +function testOpenWebConsole() +{ + HUDService.activateHUDForContext(gBrowser.selectedTab); + is(HUDService.displaysIndex().length, 1, "WebConsole was opened"); + + hudId = HUDService.displaysIndex()[0]; + hud = HUDService.getHeadsUpDisplay(hudId); + + HUDService.logWarningAboutReplacedAPI(hudId); +} + +function testWarning() +{ + const successMsg = "Found the warning message"; + const errMsg = "Could not find the warning message about the replaced API"; + + var display = HUDService.getDisplayByURISpec(content.location.href); + var outputNode = display.querySelectorAll(".hud-output-node")[0]; + executeSoon(function () { + testLogEntry(outputNode, "disabled", { success: successMsg, err: errMsg }); + }); +} + +function testLogEntry(aOutputNode, aMatchString, aSuccessErrObj) +{ + var message = aOutputNode.textContent.indexOf(aMatchString); + if (message > -1) { + ok(true, aSuccessErrObj.success); + return; + } + ok(false, aSuccessErrObj.err); +} + +function finishTest() { + hud = null; + hudId = null; + + executeSoon(function() { + finish(); + }); +} + +let hud, hudId, tab, browser, filterBox, outputNode; +let win = gBrowser.selectedBrowser; + +tab = gBrowser.selectedTab; +browser = gBrowser.getBrowserForTab(tab); + +content.location.href = TEST_REPLACED_API_URI; + +function test() { + waitForExplicitFinish(); + browser.addEventListener("DOMContentLoaded", function onLoad(event) { + browser.removeEventListener("DOMContentLoaded", onLoad, false); + executeSoon(function (){ + testOpenWebConsole(); + executeSoon(function (){ + testWarning(); + }); + }); + }, false); + finishTest(); +} diff --git a/toolkit/components/console/hudservice/tests/browser/test-console-replaced-api.html b/toolkit/components/console/hudservice/tests/browser/test-console-replaced-api.html new file mode 100644 index 00000000000..30a10aff654 --- /dev/null +++ b/toolkit/components/console/hudservice/tests/browser/test-console-replaced-api.html @@ -0,0 +1,15 @@ + + + Console test replaced API + + + +

Web Console Replace API Test

+ + + diff --git a/toolkit/locales/en-US/chrome/global/headsUpDisplay.properties b/toolkit/locales/en-US/chrome/global/headsUpDisplay.properties index c4ad185dfab..2cf2e417310 100644 --- a/toolkit/locales/en-US/chrome/global/headsUpDisplay.properties +++ b/toolkit/locales/en-US/chrome/global/headsUpDisplay.properties @@ -106,3 +106,4 @@ NetworkPanel.durationMS=%Sms # The third %S is replaced by the duration between the response header and the # response body event. NetworkPanel.imageSizeDeltaDurationMS=%Sx%Spx, Δ%Sms +ConsoleAPIDisabled=The Web Console logging API (console.log, console.info, console.warn, console.error) has been disabled by a script on this page.