toolkit/components/telemetry/UITelemetry.jsm

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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 file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 const Cu = Components.utils;
michael@0 8
michael@0 9 const PREF_BRANCH = "toolkit.telemetry.";
michael@0 10 const PREF_ENABLED = PREF_BRANCH + "enabled";
michael@0 11
michael@0 12 this.EXPORTED_SYMBOLS = [
michael@0 13 "UITelemetry",
michael@0 14 ];
michael@0 15
michael@0 16 Cu.import("resource://gre/modules/Services.jsm", this);
michael@0 17
michael@0 18 /**
michael@0 19 * UITelemetry is a helper JSM used to record UI specific telemetry events.
michael@0 20 *
michael@0 21 * It implements nsIUITelemetryObserver, defined in nsIAndroidBridge.idl.
michael@0 22 */
michael@0 23 this.UITelemetry = {
michael@0 24 _enabled: undefined,
michael@0 25 _activeSessions: {},
michael@0 26 _measurements: [],
michael@0 27
michael@0 28 // Lazily decide whether telemetry is enabled.
michael@0 29 get enabled() {
michael@0 30 if (this._enabled !== undefined) {
michael@0 31 return this._enabled;
michael@0 32 }
michael@0 33
michael@0 34 // Set an observer to watch for changes at runtime.
michael@0 35 Services.prefs.addObserver(PREF_ENABLED, this, false);
michael@0 36 Services.obs.addObserver(this, "profile-before-change", false);
michael@0 37
michael@0 38 // Pick up the current value.
michael@0 39 try {
michael@0 40 this._enabled = Services.prefs.getBoolPref(PREF_ENABLED);
michael@0 41 } catch (e) {
michael@0 42 this._enabled = false;
michael@0 43 }
michael@0 44
michael@0 45 return this._enabled;
michael@0 46 },
michael@0 47
michael@0 48 observe: function(aSubject, aTopic, aData) {
michael@0 49 if (aTopic == "profile-before-change") {
michael@0 50 Services.obs.removeObserver(this, "profile-before-change");
michael@0 51 Services.prefs.removeObserver(PREF_ENABLED, this);
michael@0 52 this._enabled = undefined;
michael@0 53 return;
michael@0 54 }
michael@0 55
michael@0 56 if (aTopic == "nsPref:changed") {
michael@0 57 switch (aData) {
michael@0 58 case PREF_ENABLED:
michael@0 59 let on = Services.prefs.getBoolPref(PREF_ENABLED);
michael@0 60 this._enabled = on;
michael@0 61
michael@0 62 // Wipe ourselves if we were just disabled.
michael@0 63 if (!on) {
michael@0 64 this._activeSessions = {};
michael@0 65 this._measurements = [];
michael@0 66 }
michael@0 67 break;
michael@0 68 }
michael@0 69 }
michael@0 70 },
michael@0 71
michael@0 72 /**
michael@0 73 * This exists exclusively for testing -- our events are not intended to
michael@0 74 * be retrieved via an XPCOM interface.
michael@0 75 */
michael@0 76 get wrappedJSObject() {
michael@0 77 return this;
michael@0 78 },
michael@0 79
michael@0 80 /**
michael@0 81 * Holds the functions that provide UITelemetry's simple
michael@0 82 * measurements. Those functions are mapped to unique names,
michael@0 83 * and should be registered with addSimpleMeasureFunction.
michael@0 84 */
michael@0 85 _simpleMeasureFunctions: {},
michael@0 86
michael@0 87 /**
michael@0 88 * Adds a single event described by a timestamp, an action, and the calling
michael@0 89 * method.
michael@0 90 *
michael@0 91 * Optionally provide a string 'extras', which will be recorded as part of
michael@0 92 * the event.
michael@0 93 *
michael@0 94 * All extant sessions will be recorded by name for each event.
michael@0 95 */
michael@0 96 addEvent: function(aAction, aMethod, aTimestamp, aExtras) {
michael@0 97 if (!this.enabled) {
michael@0 98 return;
michael@0 99 }
michael@0 100
michael@0 101 let sessions = Object.keys(this._activeSessions);
michael@0 102 let aEvent = {
michael@0 103 type: "event",
michael@0 104 action: aAction,
michael@0 105 method: aMethod,
michael@0 106 sessions: sessions,
michael@0 107 timestamp: aTimestamp,
michael@0 108 };
michael@0 109
michael@0 110 if (aExtras) {
michael@0 111 aEvent.extras = aExtras;
michael@0 112 }
michael@0 113
michael@0 114 this._recordEvent(aEvent);
michael@0 115 },
michael@0 116
michael@0 117 /**
michael@0 118 * Begins tracking a session by storing a timestamp for session start.
michael@0 119 */
michael@0 120 startSession: function(aName, aTimestamp) {
michael@0 121 if (!this.enabled) {
michael@0 122 return;
michael@0 123 }
michael@0 124
michael@0 125 if (this._activeSessions[aName]) {
michael@0 126 // Do not overwrite a previous event start if it already exists.
michael@0 127 return;
michael@0 128 }
michael@0 129 this._activeSessions[aName] = aTimestamp;
michael@0 130 },
michael@0 131
michael@0 132 /**
michael@0 133 * Tracks the end of a session with a timestamp.
michael@0 134 */
michael@0 135 stopSession: function(aName, aReason, aTimestamp) {
michael@0 136 if (!this.enabled) {
michael@0 137 return;
michael@0 138 }
michael@0 139
michael@0 140 let sessionStart = this._activeSessions[aName];
michael@0 141 delete this._activeSessions[aName];
michael@0 142
michael@0 143 if (!sessionStart) {
michael@0 144 return;
michael@0 145 }
michael@0 146
michael@0 147 let aEvent = {
michael@0 148 type: "session",
michael@0 149 name: aName,
michael@0 150 reason: aReason,
michael@0 151 start: sessionStart,
michael@0 152 end: aTimestamp,
michael@0 153 };
michael@0 154
michael@0 155 this._recordEvent(aEvent);
michael@0 156 },
michael@0 157
michael@0 158 _recordEvent: function(aEvent) {
michael@0 159 this._measurements.push(aEvent);
michael@0 160 },
michael@0 161
michael@0 162 /**
michael@0 163 * Called by TelemetryPing to populate the simple measurement
michael@0 164 * blob. This function will iterate over all functions added
michael@0 165 * via addSimpleMeasureFunction and return an object with the
michael@0 166 * results of those functions.
michael@0 167 */
michael@0 168 getSimpleMeasures: function() {
michael@0 169 if (!this.enabled) {
michael@0 170 return {};
michael@0 171 }
michael@0 172
michael@0 173 let result = {};
michael@0 174 for (let name in this._simpleMeasureFunctions) {
michael@0 175 result[name] = this._simpleMeasureFunctions[name]();
michael@0 176 }
michael@0 177 return result;
michael@0 178 },
michael@0 179
michael@0 180 /**
michael@0 181 * Allows the caller to register functions that will get called
michael@0 182 * for simple measures during a Telemetry ping. aName is a unique
michael@0 183 * identifier used as they key for the simple measurement in the
michael@0 184 * object that getSimpleMeasures returns.
michael@0 185 *
michael@0 186 * This function throws an exception if aName already has a function
michael@0 187 * registered for it.
michael@0 188 */
michael@0 189 addSimpleMeasureFunction: function(aName, aFunction) {
michael@0 190 if (!this.enabled) {
michael@0 191 return;
michael@0 192 }
michael@0 193
michael@0 194 if (aName in this._simpleMeasureFunctions) {
michael@0 195 throw new Error("A simple measurement function is already registered for " + aName);
michael@0 196 }
michael@0 197
michael@0 198 if (!aFunction || typeof aFunction !== 'function') {
michael@0 199 throw new Error("addSimpleMeasureFunction called with non-function argument.");
michael@0 200 }
michael@0 201
michael@0 202 this._simpleMeasureFunctions[aName] = aFunction;
michael@0 203 },
michael@0 204
michael@0 205 removeSimpleMeasureFunction: function(aName) {
michael@0 206 delete this._simpleMeasureFunctions[aName];
michael@0 207 },
michael@0 208
michael@0 209 getUIMeasurements: function() {
michael@0 210 if (!this.enabled) {
michael@0 211 return [];
michael@0 212 }
michael@0 213
michael@0 214 return this._measurements.slice();
michael@0 215 }
michael@0 216 };

mercurial