forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ResourceStatsManager.js
480 lines (407 loc) · 14.3 KB
/
ResourceStatsManager.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/* 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";
const DEBUG = false;
function debug(s) { dump("-*- ResourceStatsManager: " + s + "\n"); }
const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/DOMRequestHelper.jsm");
// Constant defines supported statistics.
const resourceTypeList = ["network", "power"];
function NetworkStatsData(aStatsData) {
if (DEBUG) {
debug("NetworkStatsData(): " + JSON.stringify(aStatsData));
}
this.receivedBytes = aStatsData.receivedBytes || 0;
this.sentBytes = aStatsData.sentBytes || 0;
this.timestamp = aStatsData.timestamp;
}
NetworkStatsData.prototype = {
classID: Components.ID("{dce5729a-ba92-4185-8854-e29e71b9e8a2}"),
contractID: "@mozilla.org/networkStatsData;1",
QueryInterface: XPCOMUtils.generateQI([])
};
function PowerStatsData(aStatsData) {
if (DEBUG) {
debug("PowerStatsData(): " + JSON.stringify(aStatsData));
}
this.consumedPower = aStatsData.consumedPower || 0;
this.timestamp = aStatsData.timestamp;
}
PowerStatsData.prototype = {
classID: Components.ID("{acb9af6c-8143-4e59-bc18-4bb1736a4004}"),
contractID: "@mozilla.org/powerStatsData;1",
QueryInterface: XPCOMUtils.generateQI([])
};
function ResourceStats(aWindow, aStats) {
if (DEBUG) {
debug("ResourceStats(): " + JSON.stringify(aStats));
}
this._window = aWindow;
this.type = aStats.type;
this.component = aStats.component || null;
this.serviceType = aStats.serviceType || null;
this.manifestURL = aStats.manifestURL || null;
this.start = aStats.start;
this.end = aStats.end;
this.statsData = new aWindow.Array();
// A function creates a StatsData object according to type.
let createStatsDataObject = null;
let self = this;
switch (this.type) {
case "power":
createStatsDataObject = function(aStats) {
let chromeObj = new PowerStatsData(aStats);
return self._window.PowerStatsData._create(self._window, chromeObj);
};
break;
case "network":
createStatsDataObject = function(aStats) {
let chromeObj = new NetworkStatsData(aStats);
return self._window.NetworkStatsData._create(self._window, chromeObj);
};
break;
}
let sampleRate = aStats.sampleRate;
let queryResult = aStats.statsData;
let stats = queryResult.pop(); // Pop out the last element.
let timestamp = this.start;
// Push query result to statsData, and fill empty elements so that:
// 1. the timestamp of the first element of statsData is equal to start;
// 2. the timestamp of the last element of statsData is equal to end;
// 3. the timestamp difference of every neighboring elements is SAMPLE_RATE.
for (; timestamp <= this.end; timestamp += sampleRate) {
if (!stats || stats.timestamp != timestamp) {
// If dataArray is empty or timestamp are not equal, push a dummy object
// (which stats are set to 0) to statsData.
this.statsData.push(createStatsDataObject({ timestamp: timestamp }));
} else {
// Push stats to statsData and pop a new element form queryResult.
this.statsData.push(createStatsDataObject(stats));
stats = queryResult.pop();
}
}
}
ResourceStats.prototype = {
getData: function() {
return this.statsData;
},
classID: Components.ID("{b7c970f2-3d58-4966-9633-2024feb5132b}"),
contractID: "@mozilla.org/resourceStats;1",
QueryInterface: XPCOMUtils.generateQI([])
};
function ResourceStatsAlarm(aWindow, aAlarm) {
if (DEBUG) {
debug("ResourceStatsAlarm(): " + JSON.stringify(aAlarm));
}
this.alarmId = aAlarm.alarmId;
this.type = aAlarm.type;
this.component = aAlarm.component || null;
this.serviceType = aAlarm.serviceType || null;
this.manifestURL = aAlarm.manifestURL || null;
this.threshold = aAlarm.threshold;
// Clone data object using structured clone algorithm.
this.data = null;
if (aAlarm.data) {
this.data = Cu.cloneInto(aAlarm.data, aWindow);
}
}
ResourceStatsAlarm.prototype = {
classID: Components.ID("{e2b66e7a-0ff1-4015-8690-a2a3f6a5b63a}"),
contractID: "@mozilla.org/resourceStatsAlarm;1",
QueryInterface: XPCOMUtils.generateQI([]),
};
function ResourceStatsManager() {
if (DEBUG) {
debug("constructor()");
}
}
ResourceStatsManager.prototype = {
__proto__: DOMRequestIpcHelper.prototype,
_getPromise: function(aCallback) {
let self = this;
return this.createPromise(function(aResolve, aReject) {
let resolverId = self.getPromiseResolverId({
resolve: aResolve,
reject: aReject
});
aCallback(resolverId);
});
},
// Check time range.
_checkTimeRange: function(aStart, aEnd) {
if (DEBUG) {
debug("_checkTimeRange(): " + aStart + " to " + aEnd);
}
let start = aStart ? aStart : 0;
let end = aEnd ? aEnd : Date.now();
if (start > end) {
throw Cr.NS_ERROR_INVALID_ARG;
}
return { start: start, end: end };
},
getStats: function(aStatsOptions, aStart, aEnd) {
// Check time range.
let { start: start, end: end } = this._checkTimeRange(aStart, aEnd);
// Create Promise.
let self = this;
return this._getPromise(function(aResolverId) {
self.cpmm.sendAsyncMessage("ResourceStats:GetStats", {
resolverId: aResolverId,
type: self.type,
statsOptions: aStatsOptions,
manifestURL: self._manifestURL,
start: start,
end: end
});
});
},
clearStats: function(aStatsOptions, aStart, aEnd) {
// Check time range.
let {start: start, end: end} = this._checkTimeRange(aStart, aEnd);
// Create Promise.
let self = this;
return this._getPromise(function(aResolverId) {
self.cpmm.sendAsyncMessage("ResourceStats:ClearStats", {
resolverId: aResolverId,
type: self.type,
statsOptions: aStatsOptions,
manifestURL: self._manifestURL,
start: start,
end: end
});
});
},
clearAllStats: function() {
// Create Promise.
let self = this;
return this._getPromise(function(aResolverId) {
self.cpmm.sendAsyncMessage("ResourceStats:ClearAllStats", {
resolverId: aResolverId,
type: self.type,
manifestURL: self._manifestURL
});
});
},
addAlarm: function(aThreshold, aStatsOptions, aAlarmOptions) {
if (DEBUG) {
debug("aStatsOptions: " + JSON.stringify(aAlarmOptions));
debug("aAlarmOptions: " + JSON.stringify(aAlarmOptions));
}
// Parse alarm options.
let startTime = aAlarmOptions.startTime || 0;
// Clone data object using structured clone algorithm.
let data = null;
if (aAlarmOptions.data) {
data = Cu.cloneInto(aAlarmOptions.data, this._window);
}
// Create Promise.
let self = this;
return this._getPromise(function(aResolverId) {
self.cpmm.sendAsyncMessage("ResourceStats:AddAlarm", {
resolverId: aResolverId,
type: self.type,
threshold: aThreshold,
statsOptions: aStatsOptions,
manifestURL: self._manifestURL,
startTime: startTime,
data: data
});
});
},
getAlarms: function(aStatsOptions) {
// Create Promise.
let self = this;
return this._getPromise(function(aResolverId) {
self.cpmm.sendAsyncMessage("ResourceStats:GetAlarms", {
resolverId: aResolverId,
type: self.type,
statsOptions: aStatsOptions,
manifestURL: self._manifestURL
});
});
},
removeAlarm: function(aAlarmId) {
// Create Promise.
let self = this;
return this._getPromise(function(aResolverId) {
self.cpmm.sendAsyncMessage("ResourceStats:RemoveAlarm", {
resolverId: aResolverId,
type: self.type,
manifestURL: self._manifestURL,
alarmId: aAlarmId
});
});
},
removeAllAlarms: function() {
// Create Promise.
let self = this;
return this._getPromise(function(aResolverId) {
self.cpmm.sendAsyncMessage("ResourceStats:RemoveAllAlarms", {
resolverId: aResolverId,
type: self.type,
manifestURL: self._manifestURL
});
});
},
getAvailableComponents: function() {
// Create Promise.
let self = this;
return this._getPromise(function(aResolverId) {
self.cpmm.sendAsyncMessage("ResourceStats:GetComponents", {
resolverId: aResolverId,
type: self.type,
manifestURL: self._manifestURL
});
});
},
get resourceTypes() {
let types = new this._window.Array();
resourceTypeList.forEach(function(aType) {
types.push(aType);
});
return types;
},
get sampleRate() {
let msg = { manifestURL: this._manifestURL };
return this.cpmm.sendSyncMessage("ResourceStats:SampleRate", msg)[0];
},
get maxStorageAge() {
let msg = { manifestURL: this._manifestURL };
return this.cpmm.sendSyncMessage("ResourceStats:MaxStorageAge", msg)[0];
},
receiveMessage: function(aMessage) {
let data = aMessage.data;
let chromeObj = null;
let webidlObj = null;
let self = this;
if (DEBUG) {
debug("receiveMessage(): " + aMessage.name + " " + data.resolverId);
}
let resolver = this.takePromiseResolver(data.resolverId);
if (!resolver) {
return;
}
switch (aMessage.name) {
case "ResourceStats:GetStats:Resolved":
if (DEBUG) {
debug("data.value = " + JSON.stringify(data.value));
}
chromeObj = new ResourceStats(this._window, data.value);
webidlObj = this._window.ResourceStats._create(this._window, chromeObj);
resolver.resolve(webidlObj);
break;
case "ResourceStats:AddAlarm:Resolved":
if (DEBUG) {
debug("data.value = " + JSON.stringify(data.value));
}
resolver.resolve(data.value); // data.value is alarmId.
break;
case "ResourceStats:GetAlarms:Resolved":
if (DEBUG) {
debug("data.value = " + JSON.stringify(data.value));
}
let alarmArray = this._window.Array();
data.value.forEach(function(aAlarm) {
chromeObj = new ResourceStatsAlarm(self._window, aAlarm);
webidlObj = self._window.ResourceStatsAlarm._create(self._window,
chromeObj);
alarmArray.push(webidlObj);
});
resolver.resolve(alarmArray);
break;
case "ResourceStats:GetComponents:Resolved":
if (DEBUG) {
debug("data.value = " + JSON.stringify(data.value));
}
let components = this._window.Array();
data.value.forEach(function(aComponent) {
components.push(aComponent);
});
resolver.resolve(components);
break;
case "ResourceStats:ClearStats:Resolved":
case "ResourceStats:ClearAllStats:Resolved":
case "ResourceStats:RemoveAlarm:Resolved":
case "ResourceStats:RemoveAllAlarms:Resolved":
if (DEBUG) {
debug("data.value = " + JSON.stringify(data.value));
}
resolver.resolve(data.value);
break;
case "ResourceStats:GetStats:Rejected":
case "ResourceStats:ClearStats:Rejected":
case "ResourceStats:ClearAllStats:Rejected":
case "ResourceStats:AddAlarm:Rejected":
case "ResourceStats:GetAlarms:Rejected":
case "ResourceStats:RemoveAlarm:Rejected":
case "ResourceStats:RemoveAllAlarms:Rejected":
case "ResourceStats:GetComponents:Rejected":
if (DEBUG) {
debug("data.reason = " + JSON.stringify(data.reason));
}
resolver.reject(data.reason);
break;
default:
if (DEBUG) {
debug("Could not find a handler for " + aMessage.name);
}
resolver.reject();
}
},
__init: function(aType) {
if (resourceTypeList.indexOf(aType) < 0) {
if (DEBUG) {
debug("Do not support resource statistics for " + aType);
}
throw Cr.NS_ERROR_INVALID_ARG;
} else {
if (DEBUG) {
debug("Create " + aType + "Mgr");
}
this.type = aType;
}
},
init: function(aWindow) {
if (DEBUG) {
debug("init()");
}
// Get the manifestURL if this is an installed app
let principal = aWindow.document.nodePrincipal;
let appsService = Cc["@mozilla.org/AppsService;1"]
.getService(Ci.nsIAppsService);
this._manifestURL = appsService.getManifestURLByLocalId(principal.appId);
const messages = ["ResourceStats:GetStats:Resolved",
"ResourceStats:ClearStats:Resolved",
"ResourceStats:ClearAllStats:Resolved",
"ResourceStats:AddAlarm:Resolved",
"ResourceStats:GetAlarms:Resolved",
"ResourceStats:RemoveAlarm:Resolved",
"ResourceStats:RemoveAllAlarms:Resolved",
"ResourceStats:GetComponents:Resolved",
"ResourceStats:GetStats:Rejected",
"ResourceStats:ClearStats:Rejected",
"ResourceStats:ClearAllStats:Rejected",
"ResourceStats:AddAlarm:Rejected",
"ResourceStats:GetAlarms:Rejected",
"ResourceStats:RemoveAlarm:Rejected",
"ResourceStats:RemoveAllAlarms:Rejected",
"ResourceStats:GetComponents:Rejected"];
this.initDOMRequestHelper(aWindow, messages);
this.cpmm = Cc["@mozilla.org/childprocessmessagemanager;1"]
.getService(Ci.nsISyncMessageSender);
},
classID: Components.ID("{101ed1f8-31b3-491c-95ea-04091e6e8027}"),
contractID: "@mozilla.org/resourceStatsManager;1",
QueryInterface: XPCOMUtils.generateQI([Ci.nsIDOMGlobalPropertyInitializer,
Ci.nsISupportsWeakReference,
Ci.nsIObserver])
};
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([NetworkStatsData,
PowerStatsData,
ResourceStats,
ResourceStatsAlarm,
ResourceStatsManager]);