Bug 592879 - Detect and warn users that the current window.console API is third party r=dietrich a=beta6+

This commit is contained in:
David Dahl 2010-09-10 17:15:48 -07:00
parent 9cee6eeabc
commit 685e5f1714
5 changed files with 171 additions and 0 deletions

View File

@ -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

View File

@ -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)

View File

@ -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 <ddahl@mozilla.com>
*
* 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();
}

View File

@ -0,0 +1,15 @@
<!DOCTYPE HTML>
<html dir="ltr" xml:lang="en-US" lang="en-US"><head>
<title>Console test replaced API</title>
<script type="text/javascript">
function replaceAPI() {
var console = {log: function (msg){}, info: function (msg){}, warn: function (msg){}, error: function (msg){}};
window.console = console;
}
</script>
</head>
<body>
<h1 id="header">Web Console Replace API Test</h1>
<button onclick="replaceAPI();">ReplaceAPI</button>
</body>
</html>

View File

@ -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.