browser/metro/base/content/contenthandlers/ConsoleAPIObserver.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 let Ci = Components.interfaces;
     6 let Cc = Components.classes;
     8 dump("### ConsoleAPIObserver.js loaded\n");
    10 /*
    11  * ConsoleAPIObserver
    12  *
    13  */ 
    15 var ConsoleAPIObserver = {
    16   init: function init() {
    17     addMessageListener("Browser:TabOpen", this);
    18     addMessageListener("Browser:TabClose", this);
    19   },
    21   receiveMessage: function receiveMessage(aMessage) {
    22     let json = aMessage.json;
    23     switch (aMessage.name) {
    24       case "Browser:TabOpen":
    25         Services.obs.addObserver(this, "console-api-log-event", false);
    26         break;
    27       case "Browser:TabClose":
    28         Services.obs.removeObserver(this, "console-api-log-event");
    29         break;
    30     }
    31   },
    33   observe: function observe(aMessage, aTopic, aData) {
    34     let contentWindowId = content.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils).outerWindowID;
    35     aMessage = aMessage.wrappedJSObject;
    36     if (aMessage.ID != contentWindowId)
    37       return;
    39     let mappedArguments = Array.map(aMessage.arguments, this.formatResult, this);
    40     let joinedArguments = Array.join(mappedArguments, " ");
    42     if (aMessage.level == "error" || aMessage.level == "warn") {
    43       let flag = (aMessage.level == "error" ? Ci.nsIScriptError.errorFlag : Ci.nsIScriptError.warningFlag);
    44       let consoleMsg = Cc["@mozilla.org/scripterror;1"].createInstance(Ci.nsIScriptError);
    45       consoleMsg.init(joinedArguments, null, null, 0, 0, flag, "content javascript");
    46       Services.console.logMessage(consoleMsg);
    47     } else if (aMessage.level == "trace") {
    48       let bundle = Services.strings.createBundle("chrome://global/locale/headsUpDisplay.properties");
    49       let args = aMessage.arguments;
    50       let filename = this.abbreviateSourceURL(args[0].filename);
    51       let functionName = args[0].functionName || bundle.GetStringFromName("stacktrace.anonymousFunction");
    52       let lineNumber = args[0].lineNumber;
    54       let body = bundle.formatStringFromName("stacktrace.outputMessage", [filename, functionName, lineNumber], 3);
    55       body += "\n";
    56       args.forEach(function(aFrame) {
    57         body += aFrame.filename + " :: " + aFrame.functionName + " :: " + aFrame.lineNumber + "\n";
    58       });
    60       Services.console.logStringMessage(body);
    61     } else {
    62       Services.console.logStringMessage(joinedArguments);
    63     }
    64   },
    66   getResultType: function getResultType(aResult) {
    67     let type = aResult === null ? "null" : typeof aResult;
    68     if (type == "object" && aResult.constructor && aResult.constructor.name)
    69       type = aResult.constructor.name;
    70     return type.toLowerCase();
    71   },
    73   formatResult: function formatResult(aResult) {
    74     let output = "";
    75     let type = this.getResultType(aResult);
    76     switch (type) {
    77       case "string":
    78       case "boolean":
    79       case "date":
    80       case "error":
    81       case "number":
    82       case "regexp":
    83         output = aResult.toString();
    84         break;
    85       case "null":
    86       case "undefined":
    87         output = type;
    88         break;
    89       default:
    90         output = aResult.toString();
    91         break;
    92     }
    94     return output;
    95   },
    97   abbreviateSourceURL: function abbreviateSourceURL(aSourceURL) {
    98     // Remove any query parameters.
    99     let hookIndex = aSourceURL.indexOf("?");
   100     if (hookIndex > -1)
   101       aSourceURL = aSourceURL.substring(0, hookIndex);
   103     // Remove a trailing "/".
   104     if (aSourceURL[aSourceURL.length - 1] == "/")
   105       aSourceURL = aSourceURL.substring(0, aSourceURL.length - 1);
   107     // Remove all but the last path component.
   108     let slashIndex = aSourceURL.lastIndexOf("/");
   109     if (slashIndex > -1)
   110       aSourceURL = aSourceURL.substring(slashIndex + 1);
   112     return aSourceURL;
   113   }
   114 };
   115 this.ConsoleAPIObserver = ConsoleAPIObserver;
   117 ConsoleAPIObserver.init();

mercurial