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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/metro/base/content/contenthandlers/ConsoleAPIObserver.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,118 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +let Ci = Components.interfaces;
     1.9 +let Cc = Components.classes;
    1.10 +
    1.11 +dump("### ConsoleAPIObserver.js loaded\n");
    1.12 +
    1.13 +/*
    1.14 + * ConsoleAPIObserver
    1.15 + *
    1.16 + */ 
    1.17 +
    1.18 +var ConsoleAPIObserver = {
    1.19 +  init: function init() {
    1.20 +    addMessageListener("Browser:TabOpen", this);
    1.21 +    addMessageListener("Browser:TabClose", this);
    1.22 +  },
    1.23 +
    1.24 +  receiveMessage: function receiveMessage(aMessage) {
    1.25 +    let json = aMessage.json;
    1.26 +    switch (aMessage.name) {
    1.27 +      case "Browser:TabOpen":
    1.28 +        Services.obs.addObserver(this, "console-api-log-event", false);
    1.29 +        break;
    1.30 +      case "Browser:TabClose":
    1.31 +        Services.obs.removeObserver(this, "console-api-log-event");
    1.32 +        break;
    1.33 +    }
    1.34 +  },
    1.35 +
    1.36 +  observe: function observe(aMessage, aTopic, aData) {
    1.37 +    let contentWindowId = content.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils).outerWindowID;
    1.38 +    aMessage = aMessage.wrappedJSObject;
    1.39 +    if (aMessage.ID != contentWindowId)
    1.40 +      return;
    1.41 +
    1.42 +    let mappedArguments = Array.map(aMessage.arguments, this.formatResult, this);
    1.43 +    let joinedArguments = Array.join(mappedArguments, " ");
    1.44 +
    1.45 +    if (aMessage.level == "error" || aMessage.level == "warn") {
    1.46 +      let flag = (aMessage.level == "error" ? Ci.nsIScriptError.errorFlag : Ci.nsIScriptError.warningFlag);
    1.47 +      let consoleMsg = Cc["@mozilla.org/scripterror;1"].createInstance(Ci.nsIScriptError);
    1.48 +      consoleMsg.init(joinedArguments, null, null, 0, 0, flag, "content javascript");
    1.49 +      Services.console.logMessage(consoleMsg);
    1.50 +    } else if (aMessage.level == "trace") {
    1.51 +      let bundle = Services.strings.createBundle("chrome://global/locale/headsUpDisplay.properties");
    1.52 +      let args = aMessage.arguments;
    1.53 +      let filename = this.abbreviateSourceURL(args[0].filename);
    1.54 +      let functionName = args[0].functionName || bundle.GetStringFromName("stacktrace.anonymousFunction");
    1.55 +      let lineNumber = args[0].lineNumber;
    1.56 +
    1.57 +      let body = bundle.formatStringFromName("stacktrace.outputMessage", [filename, functionName, lineNumber], 3);
    1.58 +      body += "\n";
    1.59 +      args.forEach(function(aFrame) {
    1.60 +        body += aFrame.filename + " :: " + aFrame.functionName + " :: " + aFrame.lineNumber + "\n";
    1.61 +      });
    1.62 +
    1.63 +      Services.console.logStringMessage(body);
    1.64 +    } else {
    1.65 +      Services.console.logStringMessage(joinedArguments);
    1.66 +    }
    1.67 +  },
    1.68 +
    1.69 +  getResultType: function getResultType(aResult) {
    1.70 +    let type = aResult === null ? "null" : typeof aResult;
    1.71 +    if (type == "object" && aResult.constructor && aResult.constructor.name)
    1.72 +      type = aResult.constructor.name;
    1.73 +    return type.toLowerCase();
    1.74 +  },
    1.75 +
    1.76 +  formatResult: function formatResult(aResult) {
    1.77 +    let output = "";
    1.78 +    let type = this.getResultType(aResult);
    1.79 +    switch (type) {
    1.80 +      case "string":
    1.81 +      case "boolean":
    1.82 +      case "date":
    1.83 +      case "error":
    1.84 +      case "number":
    1.85 +      case "regexp":
    1.86 +        output = aResult.toString();
    1.87 +        break;
    1.88 +      case "null":
    1.89 +      case "undefined":
    1.90 +        output = type;
    1.91 +        break;
    1.92 +      default:
    1.93 +        output = aResult.toString();
    1.94 +        break;
    1.95 +    }
    1.96 +
    1.97 +    return output;
    1.98 +  },
    1.99 +
   1.100 +  abbreviateSourceURL: function abbreviateSourceURL(aSourceURL) {
   1.101 +    // Remove any query parameters.
   1.102 +    let hookIndex = aSourceURL.indexOf("?");
   1.103 +    if (hookIndex > -1)
   1.104 +      aSourceURL = aSourceURL.substring(0, hookIndex);
   1.105 +
   1.106 +    // Remove a trailing "/".
   1.107 +    if (aSourceURL[aSourceURL.length - 1] == "/")
   1.108 +      aSourceURL = aSourceURL.substring(0, aSourceURL.length - 1);
   1.109 +
   1.110 +    // Remove all but the last path component.
   1.111 +    let slashIndex = aSourceURL.lastIndexOf("/");
   1.112 +    if (slashIndex > -1)
   1.113 +      aSourceURL = aSourceURL.substring(slashIndex + 1);
   1.114 +
   1.115 +    return aSourceURL;
   1.116 +  }
   1.117 +};
   1.118 +this.ConsoleAPIObserver = ConsoleAPIObserver;
   1.119 +
   1.120 +ConsoleAPIObserver.init();
   1.121 +

mercurial