Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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 | /** |
michael@0 | 6 | * Telemetry. |
michael@0 | 7 | * |
michael@0 | 8 | * To add metrics for a tool: |
michael@0 | 9 | * |
michael@0 | 10 | * 1. Create boolean, flag and exponential entries in |
michael@0 | 11 | * toolkit/components/telemetry/Histograms.json. Each type is optional but it |
michael@0 | 12 | * is best if all three can be included. |
michael@0 | 13 | * |
michael@0 | 14 | * 2. Add your chart entries to browser/devtools/shared/telemetry.js |
michael@0 | 15 | * (Telemetry.prototype._histograms): |
michael@0 | 16 | * mytoolname: { |
michael@0 | 17 | * histogram: "DEVTOOLS_MYTOOLNAME_OPENED_BOOLEAN", |
michael@0 | 18 | * userHistogram: "DEVTOOLS_MYTOOLNAME_OPENED_PER_USER_FLAG", |
michael@0 | 19 | * timerHistogram: "DEVTOOLS_MYTOOLNAME_TIME_ACTIVE_SECONDS" |
michael@0 | 20 | * }, |
michael@0 | 21 | * |
michael@0 | 22 | * 3. Include this module at the top of your tool. Use: |
michael@0 | 23 | * let Telemetry = require("devtools/shared/telemetry") |
michael@0 | 24 | * |
michael@0 | 25 | * 4. Create a telemetry instance in your tool's constructor: |
michael@0 | 26 | * this._telemetry = new Telemetry(); |
michael@0 | 27 | * |
michael@0 | 28 | * 5. When your tool is opened call: |
michael@0 | 29 | * this._telemetry.toolOpened("mytoolname"); |
michael@0 | 30 | * |
michael@0 | 31 | * 6. When your tool is closed call: |
michael@0 | 32 | * this._telemetry.toolClosed("mytoolname"); |
michael@0 | 33 | * |
michael@0 | 34 | * Note: |
michael@0 | 35 | * You can view telemetry stats for your local Firefox instance via |
michael@0 | 36 | * about:telemetry. |
michael@0 | 37 | * |
michael@0 | 38 | * You can view telemetry stats for large groups of Firefox users at |
michael@0 | 39 | * metrics.mozilla.com. |
michael@0 | 40 | */ |
michael@0 | 41 | |
michael@0 | 42 | const TOOLS_OPENED_PREF = "devtools.telemetry.tools.opened.version"; |
michael@0 | 43 | |
michael@0 | 44 | this.Telemetry = function() { |
michael@0 | 45 | // Bind pretty much all functions so that callers do not need to. |
michael@0 | 46 | this.toolOpened = this.toolOpened.bind(this); |
michael@0 | 47 | this.toolClosed = this.toolClosed.bind(this); |
michael@0 | 48 | this.log = this.log.bind(this); |
michael@0 | 49 | this.logOncePerBrowserVersion = this.logOncePerBrowserVersion.bind(this); |
michael@0 | 50 | this.destroy = this.destroy.bind(this); |
michael@0 | 51 | |
michael@0 | 52 | this._timers = new Map(); |
michael@0 | 53 | }; |
michael@0 | 54 | |
michael@0 | 55 | module.exports = Telemetry; |
michael@0 | 56 | |
michael@0 | 57 | let {Cc, Ci, Cu} = require("chrome"); |
michael@0 | 58 | let {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); |
michael@0 | 59 | let {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {}); |
michael@0 | 60 | |
michael@0 | 61 | Telemetry.prototype = { |
michael@0 | 62 | _histograms: { |
michael@0 | 63 | toolbox: { |
michael@0 | 64 | timerHistogram: "DEVTOOLS_TOOLBOX_TIME_ACTIVE_SECONDS" |
michael@0 | 65 | }, |
michael@0 | 66 | options: { |
michael@0 | 67 | histogram: "DEVTOOLS_OPTIONS_OPENED_BOOLEAN", |
michael@0 | 68 | userHistogram: "DEVTOOLS_OPTIONS_OPENED_PER_USER_FLAG", |
michael@0 | 69 | timerHistogram: "DEVTOOLS_OPTIONS_TIME_ACTIVE_SECONDS" |
michael@0 | 70 | }, |
michael@0 | 71 | webconsole: { |
michael@0 | 72 | histogram: "DEVTOOLS_WEBCONSOLE_OPENED_BOOLEAN", |
michael@0 | 73 | userHistogram: "DEVTOOLS_WEBCONSOLE_OPENED_PER_USER_FLAG", |
michael@0 | 74 | timerHistogram: "DEVTOOLS_WEBCONSOLE_TIME_ACTIVE_SECONDS" |
michael@0 | 75 | }, |
michael@0 | 76 | browserconsole: { |
michael@0 | 77 | histogram: "DEVTOOLS_BROWSERCONSOLE_OPENED_BOOLEAN", |
michael@0 | 78 | userHistogram: "DEVTOOLS_BROWSERCONSOLE_OPENED_PER_USER_FLAG", |
michael@0 | 79 | timerHistogram: "DEVTOOLS_BROWSERCONSOLE_TIME_ACTIVE_SECONDS" |
michael@0 | 80 | }, |
michael@0 | 81 | inspector: { |
michael@0 | 82 | histogram: "DEVTOOLS_INSPECTOR_OPENED_BOOLEAN", |
michael@0 | 83 | userHistogram: "DEVTOOLS_INSPECTOR_OPENED_PER_USER_FLAG", |
michael@0 | 84 | timerHistogram: "DEVTOOLS_INSPECTOR_TIME_ACTIVE_SECONDS" |
michael@0 | 85 | }, |
michael@0 | 86 | ruleview: { |
michael@0 | 87 | histogram: "DEVTOOLS_RULEVIEW_OPENED_BOOLEAN", |
michael@0 | 88 | userHistogram: "DEVTOOLS_RULEVIEW_OPENED_PER_USER_FLAG", |
michael@0 | 89 | timerHistogram: "DEVTOOLS_RULEVIEW_TIME_ACTIVE_SECONDS" |
michael@0 | 90 | }, |
michael@0 | 91 | computedview: { |
michael@0 | 92 | histogram: "DEVTOOLS_COMPUTEDVIEW_OPENED_BOOLEAN", |
michael@0 | 93 | userHistogram: "DEVTOOLS_COMPUTEDVIEW_OPENED_PER_USER_FLAG", |
michael@0 | 94 | timerHistogram: "DEVTOOLS_COMPUTEDVIEW_TIME_ACTIVE_SECONDS" |
michael@0 | 95 | }, |
michael@0 | 96 | layoutview: { |
michael@0 | 97 | histogram: "DEVTOOLS_LAYOUTVIEW_OPENED_BOOLEAN", |
michael@0 | 98 | userHistogram: "DEVTOOLS_LAYOUTVIEW_OPENED_PER_USER_FLAG", |
michael@0 | 99 | timerHistogram: "DEVTOOLS_LAYOUTVIEW_TIME_ACTIVE_SECONDS" |
michael@0 | 100 | }, |
michael@0 | 101 | fontinspector: { |
michael@0 | 102 | histogram: "DEVTOOLS_FONTINSPECTOR_OPENED_BOOLEAN", |
michael@0 | 103 | userHistogram: "DEVTOOLS_FONTINSPECTOR_OPENED_PER_USER_FLAG", |
michael@0 | 104 | timerHistogram: "DEVTOOLS_FONTINSPECTOR_TIME_ACTIVE_SECONDS" |
michael@0 | 105 | }, |
michael@0 | 106 | jsdebugger: { |
michael@0 | 107 | histogram: "DEVTOOLS_JSDEBUGGER_OPENED_BOOLEAN", |
michael@0 | 108 | userHistogram: "DEVTOOLS_JSDEBUGGER_OPENED_PER_USER_FLAG", |
michael@0 | 109 | timerHistogram: "DEVTOOLS_JSDEBUGGER_TIME_ACTIVE_SECONDS" |
michael@0 | 110 | }, |
michael@0 | 111 | jsbrowserdebugger: { |
michael@0 | 112 | histogram: "DEVTOOLS_JSBROWSERDEBUGGER_OPENED_BOOLEAN", |
michael@0 | 113 | userHistogram: "DEVTOOLS_JSBROWSERDEBUGGER_OPENED_PER_USER_FLAG", |
michael@0 | 114 | timerHistogram: "DEVTOOLS_JSBROWSERDEBUGGER_TIME_ACTIVE_SECONDS" |
michael@0 | 115 | }, |
michael@0 | 116 | styleeditor: { |
michael@0 | 117 | histogram: "DEVTOOLS_STYLEEDITOR_OPENED_BOOLEAN", |
michael@0 | 118 | userHistogram: "DEVTOOLS_STYLEEDITOR_OPENED_PER_USER_FLAG", |
michael@0 | 119 | timerHistogram: "DEVTOOLS_STYLEEDITOR_TIME_ACTIVE_SECONDS" |
michael@0 | 120 | }, |
michael@0 | 121 | shadereditor: { |
michael@0 | 122 | histogram: "DEVTOOLS_SHADEREDITOR_OPENED_BOOLEAN", |
michael@0 | 123 | userHistogram: "DEVTOOLS_SHADEREDITOR_OPENED_PER_USER_FLAG", |
michael@0 | 124 | timerHistogram: "DEVTOOLS_SHADEREDITOR_TIME_ACTIVE_SECONDS" |
michael@0 | 125 | }, |
michael@0 | 126 | jsprofiler: { |
michael@0 | 127 | histogram: "DEVTOOLS_JSPROFILER_OPENED_BOOLEAN", |
michael@0 | 128 | userHistogram: "DEVTOOLS_JSPROFILER_OPENED_PER_USER_FLAG", |
michael@0 | 129 | timerHistogram: "DEVTOOLS_JSPROFILER_TIME_ACTIVE_SECONDS" |
michael@0 | 130 | }, |
michael@0 | 131 | netmonitor: { |
michael@0 | 132 | histogram: "DEVTOOLS_NETMONITOR_OPENED_BOOLEAN", |
michael@0 | 133 | userHistogram: "DEVTOOLS_NETMONITOR_OPENED_PER_USER_FLAG", |
michael@0 | 134 | timerHistogram: "DEVTOOLS_NETMONITOR_TIME_ACTIVE_SECONDS" |
michael@0 | 135 | }, |
michael@0 | 136 | tilt: { |
michael@0 | 137 | histogram: "DEVTOOLS_TILT_OPENED_BOOLEAN", |
michael@0 | 138 | userHistogram: "DEVTOOLS_TILT_OPENED_PER_USER_FLAG", |
michael@0 | 139 | timerHistogram: "DEVTOOLS_TILT_TIME_ACTIVE_SECONDS" |
michael@0 | 140 | }, |
michael@0 | 141 | paintflashing: { |
michael@0 | 142 | histogram: "DEVTOOLS_PAINTFLASHING_OPENED_BOOLEAN", |
michael@0 | 143 | userHistogram: "DEVTOOLS_PAINTFLASHING_OPENED_PER_USER_FLAG", |
michael@0 | 144 | timerHistogram: "DEVTOOLS_PAINTFLASHING_TIME_ACTIVE_SECONDS" |
michael@0 | 145 | }, |
michael@0 | 146 | scratchpad: { |
michael@0 | 147 | histogram: "DEVTOOLS_SCRATCHPAD_OPENED_BOOLEAN", |
michael@0 | 148 | userHistogram: "DEVTOOLS_SCRATCHPAD_OPENED_PER_USER_FLAG", |
michael@0 | 149 | timerHistogram: "DEVTOOLS_SCRATCHPAD_TIME_ACTIVE_SECONDS" |
michael@0 | 150 | }, |
michael@0 | 151 | responsive: { |
michael@0 | 152 | histogram: "DEVTOOLS_RESPONSIVE_OPENED_BOOLEAN", |
michael@0 | 153 | userHistogram: "DEVTOOLS_RESPONSIVE_OPENED_PER_USER_FLAG", |
michael@0 | 154 | timerHistogram: "DEVTOOLS_RESPONSIVE_TIME_ACTIVE_SECONDS" |
michael@0 | 155 | }, |
michael@0 | 156 | developertoolbar: { |
michael@0 | 157 | histogram: "DEVTOOLS_DEVELOPERTOOLBAR_OPENED_BOOLEAN", |
michael@0 | 158 | userHistogram: "DEVTOOLS_DEVELOPERTOOLBAR_OPENED_PER_USER_FLAG", |
michael@0 | 159 | timerHistogram: "DEVTOOLS_DEVELOPERTOOLBAR_TIME_ACTIVE_SECONDS" |
michael@0 | 160 | }, |
michael@0 | 161 | custom: { |
michael@0 | 162 | histogram: "DEVTOOLS_CUSTOM_OPENED_BOOLEAN", |
michael@0 | 163 | userHistogram: "DEVTOOLS_CUSTOM_OPENED_PER_USER_FLAG", |
michael@0 | 164 | timerHistogram: "DEVTOOLS_CUSTOM_TIME_ACTIVE_SECONDS" |
michael@0 | 165 | } |
michael@0 | 166 | }, |
michael@0 | 167 | |
michael@0 | 168 | /** |
michael@0 | 169 | * Add an entry to a histogram. |
michael@0 | 170 | * |
michael@0 | 171 | * @param {String} id |
michael@0 | 172 | * Used to look up the relevant histogram ID and log true to that |
michael@0 | 173 | * histogram. |
michael@0 | 174 | */ |
michael@0 | 175 | toolOpened: function(id) { |
michael@0 | 176 | let charts = this._histograms[id] || this._histograms.custom; |
michael@0 | 177 | |
michael@0 | 178 | if (charts.histogram) { |
michael@0 | 179 | this.log(charts.histogram, true); |
michael@0 | 180 | } |
michael@0 | 181 | if (charts.userHistogram) { |
michael@0 | 182 | this.logOncePerBrowserVersion(charts.userHistogram, true); |
michael@0 | 183 | } |
michael@0 | 184 | if (charts.timerHistogram) { |
michael@0 | 185 | this._timers.set(charts.timerHistogram, new Date()); |
michael@0 | 186 | } |
michael@0 | 187 | }, |
michael@0 | 188 | |
michael@0 | 189 | toolClosed: function(id) { |
michael@0 | 190 | let charts = this._histograms[id]; |
michael@0 | 191 | |
michael@0 | 192 | if (!charts || !charts.timerHistogram) { |
michael@0 | 193 | return; |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | let startTime = this._timers.get(charts.timerHistogram); |
michael@0 | 197 | |
michael@0 | 198 | if (startTime) { |
michael@0 | 199 | let time = (new Date() - startTime) / 1000; |
michael@0 | 200 | this.log(charts.timerHistogram, time); |
michael@0 | 201 | this._timers.delete(charts.timerHistogram); |
michael@0 | 202 | } |
michael@0 | 203 | }, |
michael@0 | 204 | |
michael@0 | 205 | /** |
michael@0 | 206 | * Log a value to a histogram. |
michael@0 | 207 | * |
michael@0 | 208 | * @param {String} histogramId |
michael@0 | 209 | * Histogram in which the data is to be stored. |
michael@0 | 210 | * @param value |
michael@0 | 211 | * Value to store. |
michael@0 | 212 | */ |
michael@0 | 213 | log: function(histogramId, value) { |
michael@0 | 214 | if (histogramId) { |
michael@0 | 215 | let histogram; |
michael@0 | 216 | |
michael@0 | 217 | try { |
michael@0 | 218 | let histogram = Services.telemetry.getHistogramById(histogramId); |
michael@0 | 219 | histogram.add(value); |
michael@0 | 220 | } catch(e) { |
michael@0 | 221 | dump("Warning: An attempt was made to write to the " + histogramId + |
michael@0 | 222 | " histogram, which is not defined in Histograms.json\n"); |
michael@0 | 223 | } |
michael@0 | 224 | } |
michael@0 | 225 | }, |
michael@0 | 226 | |
michael@0 | 227 | /** |
michael@0 | 228 | * Log info about usage once per browser version. This allows us to discover |
michael@0 | 229 | * how many individual users are using our tools for each browser version. |
michael@0 | 230 | * |
michael@0 | 231 | * @param {String} perUserHistogram |
michael@0 | 232 | * Histogram in which the data is to be stored. |
michael@0 | 233 | */ |
michael@0 | 234 | logOncePerBrowserVersion: function(perUserHistogram, value) { |
michael@0 | 235 | let currentVersion = appInfo.version; |
michael@0 | 236 | let latest = Services.prefs.getCharPref(TOOLS_OPENED_PREF); |
michael@0 | 237 | let latestObj = JSON.parse(latest); |
michael@0 | 238 | |
michael@0 | 239 | let lastVersionHistogramUpdated = latestObj[perUserHistogram]; |
michael@0 | 240 | |
michael@0 | 241 | if (typeof lastVersionHistogramUpdated == "undefined" || |
michael@0 | 242 | lastVersionHistogramUpdated !== currentVersion) { |
michael@0 | 243 | latestObj[perUserHistogram] = currentVersion; |
michael@0 | 244 | latest = JSON.stringify(latestObj); |
michael@0 | 245 | Services.prefs.setCharPref(TOOLS_OPENED_PREF, latest); |
michael@0 | 246 | this.log(perUserHistogram, value); |
michael@0 | 247 | } |
michael@0 | 248 | }, |
michael@0 | 249 | |
michael@0 | 250 | destroy: function() { |
michael@0 | 251 | for (let [histogram, time] of this._timers) { |
michael@0 | 252 | time = (new Date() - time) / 1000; |
michael@0 | 253 | |
michael@0 | 254 | this.log(histogram, time); |
michael@0 | 255 | this._timers.delete(histogram); |
michael@0 | 256 | } |
michael@0 | 257 | } |
michael@0 | 258 | }; |
michael@0 | 259 | |
michael@0 | 260 | XPCOMUtils.defineLazyGetter(this, "appInfo", function() { |
michael@0 | 261 | return Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULAppInfo); |
michael@0 | 262 | }); |