-
Notifications
You must be signed in to change notification settings - Fork 54
/
ext-telemetry.js
167 lines (162 loc) · 4.9 KB
/
ext-telemetry.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
ChromeUtils.defineModuleGetter(
this,
"TelemetryController",
"resource://gre/modules/TelemetryController.jsm"
);
ChromeUtils.defineModuleGetter(
this,
"TelemetryUtils",
"resource://gre/modules/TelemetryUtils.jsm"
);
ChromeUtils.defineModuleGetter(
this,
"Services",
"resource://gre/modules/Services.jsm"
);
ChromeUtils.defineModuleGetter(
this,
"ExtensionUtils",
"resource://gre/modules/ExtensionUtils.jsm"
);
const SCALAR_TYPES = {
count: Ci.nsITelemetry.SCALAR_TYPE_COUNT,
string: Ci.nsITelemetry.SCALAR_TYPE_STRING,
boolean: Ci.nsITelemetry.SCALAR_TYPE_BOOLEAN,
};
// Currently unsupported on Android: blocked on 1220177.
// See 1280234 c67 for discussion.
function desktopCheck() {
if (AppConstants.MOZ_BUILD_APP !== "browser") {
throw new ExtensionUtils.ExtensionError(
"This API is only supported on desktop"
);
}
}
this.telemetry = class extends ExtensionAPI {
getAPI(context) {
return {
telemetry: {
submitPing(type, payload, options) {
desktopCheck();
try {
TelemetryController.submitExternalPing(type, payload, options);
} catch (ex) {
throw new ExtensionUtils.ExtensionError(ex);
}
},
canUpload() {
desktopCheck();
// Note: remove the ternary and direct pref check when
// TelemetryController.canUpload() is implemented (bug 1440089).
try {
const result =
"canUpload" in TelemetryController
? TelemetryController.canUpload()
: Services.prefs.getBoolPref(
TelemetryUtils.Preferences.FhrUploadEnabled,
false
);
return result;
} catch (ex) {
throw new ExtensionUtils.ExtensionError(ex);
}
},
scalarAdd(name, value) {
desktopCheck();
try {
Services.telemetry.scalarAdd(name, value);
} catch (ex) {
throw new ExtensionUtils.ExtensionError(ex);
}
},
scalarSet(name, value) {
desktopCheck();
try {
Services.telemetry.scalarSet(name, value);
} catch (ex) {
throw new ExtensionUtils.ExtensionError(ex);
}
},
scalarSetMaximum(name, value) {
desktopCheck();
try {
Services.telemetry.scalarSetMaximum(name, value);
} catch (ex) {
throw new ExtensionUtils.ExtensionError(ex);
}
},
keyedScalarAdd(name, key, value) {
desktopCheck();
try {
Services.telemetry.keyedScalarAdd(name, key, value);
} catch (ex) {
throw new ExtensionUtils.ExtensionError(ex);
}
},
keyedScalarSet(name, key, value) {
desktopCheck();
try {
Services.telemetry.keyedScalarSet(name, key, value);
} catch (ex) {
throw new ExtensionUtils.ExtensionError(ex);
}
},
keyedScalarSetMaximum(name, key, value) {
desktopCheck();
try {
Services.telemetry.keyedScalarSetMaximum(name, key, value);
} catch (ex) {
throw new ExtensionUtils.ExtensionError(ex);
}
},
recordEvent(category, method, object, value, extra) {
desktopCheck();
try {
Services.telemetry.recordEvent(
category,
method,
object,
value,
extra
);
} catch (ex) {
throw new ExtensionUtils.ExtensionError(ex);
}
},
registerScalars(category, data) {
desktopCheck();
try {
// For each scalar in `data`, replace scalar.kind with
// the appropriate nsITelemetry constant.
Object.keys(data).forEach(scalar => {
data[scalar].kind = SCALAR_TYPES[data[scalar].kind];
});
Services.telemetry.registerScalars(category, data);
} catch (ex) {
throw new ExtensionUtils.ExtensionError(ex);
}
},
setEventRecordingEnabled(category, enabled) {
desktopCheck();
try {
Services.telemetry.setEventRecordingEnabled(category, enabled);
} catch (ex) {
throw new ExtensionUtils.ExtensionError(ex);
}
},
registerEvents(category, data) {
desktopCheck();
try {
Services.telemetry.registerEvents(category, data);
} catch (ex) {
throw new ExtensionUtils.ExtensionError(ex);
}
},
},
};
}
};