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