2014-01-31 10:01:58 -08:00
|
|
|
/* 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';
|
|
|
|
|
2014-08-18 12:40:00 -07:00
|
|
|
// settings.js loads this file when the HUD setting is enabled.
|
|
|
|
|
2014-02-26 13:58:37 -08:00
|
|
|
const DEVELOPER_HUD_LOG_PREFIX = 'DeveloperHUD';
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyGetter(this, 'devtools', function() {
|
2014-04-16 10:50:37 -07:00
|
|
|
const {devtools} = Cu.import('resource://gre/modules/devtools/Loader.jsm', {});
|
2014-02-26 13:58:37 -08:00
|
|
|
return devtools;
|
|
|
|
});
|
2014-01-31 10:01:58 -08:00
|
|
|
|
|
|
|
XPCOMUtils.defineLazyGetter(this, 'DebuggerClient', function() {
|
|
|
|
return Cu.import('resource://gre/modules/devtools/dbg-client.jsm', {}).DebuggerClient;
|
|
|
|
});
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyGetter(this, 'WebConsoleUtils', function() {
|
2014-04-16 10:50:37 -07:00
|
|
|
return devtools.require('devtools/toolkit/webconsole/utils').Utils;
|
2014-01-31 10:01:58 -08:00
|
|
|
});
|
|
|
|
|
2014-02-14 08:52:14 -08:00
|
|
|
XPCOMUtils.defineLazyGetter(this, 'EventLoopLagFront', function() {
|
2014-04-16 10:50:37 -07:00
|
|
|
return devtools.require('devtools/server/actors/eventlooplag').EventLoopLagFront;
|
2014-02-14 08:52:14 -08:00
|
|
|
});
|
|
|
|
|
2014-02-26 13:58:37 -08:00
|
|
|
XPCOMUtils.defineLazyGetter(this, 'MemoryFront', function() {
|
2014-04-16 10:50:37 -07:00
|
|
|
return devtools.require('devtools/server/actors/memory').MemoryFront;
|
2014-02-26 13:58:37 -08:00
|
|
|
});
|
|
|
|
|
2014-10-12 23:43:00 -07:00
|
|
|
Cu.import('resource://gre/modules/Frames.jsm');
|
2014-02-14 08:52:14 -08:00
|
|
|
|
2014-01-31 10:01:58 -08:00
|
|
|
/**
|
2014-02-26 13:58:37 -08:00
|
|
|
* The Developer HUD is an on-device developer tool that displays widgets,
|
2014-01-31 10:01:58 -08:00
|
|
|
* showing visual debug information about apps. Each widget corresponds to a
|
|
|
|
* metric as tracked by a metric watcher (e.g. consoleWatcher).
|
|
|
|
*/
|
2014-02-26 13:58:37 -08:00
|
|
|
let developerHUD = {
|
2014-01-31 10:01:58 -08:00
|
|
|
|
2014-03-03 11:50:56 -08:00
|
|
|
_targets: new Map(),
|
2014-01-31 10:01:58 -08:00
|
|
|
_client: null,
|
2014-03-25 06:14:22 -07:00
|
|
|
_conn: null,
|
2014-01-31 10:01:58 -08:00
|
|
|
_watchers: [],
|
2014-03-11 14:01:29 -07:00
|
|
|
_logging: true,
|
2014-01-31 10:01:58 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This method registers a metric watcher that will watch one or more metrics
|
2014-03-17 07:43:33 -07:00
|
|
|
* on app frames that are being tracked. A watcher must implement the
|
|
|
|
* `trackTarget(target)` and `untrackTarget(target)` methods, register
|
|
|
|
* observed metrics with `target.register(metric)`, and keep them up-to-date
|
2014-04-16 10:50:37 -07:00
|
|
|
* with `target.update(metric, message)` when necessary.
|
2014-01-31 10:01:58 -08:00
|
|
|
*/
|
|
|
|
registerWatcher: function dwp_registerWatcher(watcher) {
|
|
|
|
this._watchers.unshift(watcher);
|
|
|
|
},
|
|
|
|
|
|
|
|
init: function dwp_init() {
|
2014-10-09 03:20:00 -07:00
|
|
|
if (this._client) {
|
2014-01-31 10:01:58 -08:00
|
|
|
return;
|
2014-10-09 03:20:00 -07:00
|
|
|
}
|
2014-01-31 10:01:58 -08:00
|
|
|
|
|
|
|
if (!DebuggerServer.initialized) {
|
2014-07-21 05:39:00 -07:00
|
|
|
RemoteDebugger.initServer();
|
2014-01-31 10:01:58 -08:00
|
|
|
}
|
|
|
|
|
2014-03-25 06:14:22 -07:00
|
|
|
// We instantiate a local debugger connection so that watchers can use our
|
|
|
|
// DebuggerClient to send requests to tab actors (e.g. the consoleActor).
|
|
|
|
// Note the special usage of the private _serverConnection, which we need
|
|
|
|
// to call connectToChild and set up child process actors on a frame we
|
|
|
|
// intend to track. These actors will use the connection to communicate with
|
|
|
|
// our DebuggerServer in the parent process.
|
|
|
|
let transport = DebuggerServer.connectPipe();
|
|
|
|
this._conn = transport._serverConnection;
|
|
|
|
this._client = new DebuggerClient(transport);
|
|
|
|
|
|
|
|
for (let w of this._watchers) {
|
|
|
|
if (w.init) {
|
|
|
|
w.init(this._client);
|
|
|
|
}
|
|
|
|
}
|
2014-02-14 16:45:45 -08:00
|
|
|
|
2014-10-12 23:43:00 -07:00
|
|
|
Frames.addObserver(this);
|
2014-03-26 03:09:13 -07:00
|
|
|
|
2014-10-12 23:43:00 -07:00
|
|
|
let appFrames = Frames.list().filter(frame => frame.getAttribute('mozapp'));
|
|
|
|
for (let frame of appFrames) {
|
2014-03-25 06:14:22 -07:00
|
|
|
this.trackFrame(frame);
|
|
|
|
}
|
2014-03-11 14:01:29 -07:00
|
|
|
|
2014-03-17 07:43:33 -07:00
|
|
|
SettingsListener.observe('hud.logging', this._logging, enabled => {
|
2014-03-11 14:01:29 -07:00
|
|
|
this._logging = enabled;
|
|
|
|
});
|
2014-01-31 10:01:58 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
uninit: function dwp_uninit() {
|
2014-10-09 03:20:00 -07:00
|
|
|
if (!this._client) {
|
2014-01-31 10:01:58 -08:00
|
|
|
return;
|
2014-10-09 03:20:00 -07:00
|
|
|
}
|
2014-01-31 10:01:58 -08:00
|
|
|
|
2014-03-03 11:50:56 -08:00
|
|
|
for (let frame of this._targets.keys()) {
|
|
|
|
this.untrackFrame(frame);
|
2014-01-31 10:01:58 -08:00
|
|
|
}
|
|
|
|
|
2014-10-12 23:43:00 -07:00
|
|
|
Frames.removeObserver(this);
|
2014-01-31 10:01:58 -08:00
|
|
|
|
|
|
|
this._client.close();
|
|
|
|
delete this._client;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method will ask all registered watchers to track and update metrics
|
2014-03-03 11:50:56 -08:00
|
|
|
* on an app frame.
|
2014-01-31 10:01:58 -08:00
|
|
|
*/
|
2014-03-03 11:50:56 -08:00
|
|
|
trackFrame: function dwp_trackFrame(frame) {
|
2014-10-09 03:20:00 -07:00
|
|
|
if (this._targets.has(frame)) {
|
2014-01-31 10:01:58 -08:00
|
|
|
return;
|
2014-10-09 03:20:00 -07:00
|
|
|
}
|
2014-01-31 10:01:58 -08:00
|
|
|
|
2014-04-03 05:45:25 -07:00
|
|
|
DebuggerServer.connectToChild(this._conn, frame).then(actor => {
|
2014-03-25 06:14:22 -07:00
|
|
|
let target = new Target(frame, actor);
|
2014-03-03 11:50:56 -08:00
|
|
|
this._targets.set(frame, target);
|
2014-01-31 10:01:58 -08:00
|
|
|
|
|
|
|
for (let w of this._watchers) {
|
2014-03-03 11:50:56 -08:00
|
|
|
w.trackTarget(target);
|
2014-01-31 10:01:58 -08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-03-03 11:50:56 -08:00
|
|
|
untrackFrame: function dwp_untrackFrame(frame) {
|
|
|
|
let target = this._targets.get(frame);
|
|
|
|
if (target) {
|
2014-01-31 10:01:58 -08:00
|
|
|
for (let w of this._watchers) {
|
2014-03-03 11:50:56 -08:00
|
|
|
w.untrackTarget(target);
|
2014-01-31 10:01:58 -08:00
|
|
|
}
|
|
|
|
|
2014-03-11 14:01:29 -07:00
|
|
|
target.destroy();
|
2014-03-04 06:51:34 -08:00
|
|
|
this._targets.delete(frame);
|
2014-01-31 10:01:58 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-10-12 23:43:00 -07:00
|
|
|
onFrameCreated: function (frame, isFirstAppFrame) {
|
|
|
|
let mozapp = frame.getAttribute('mozapp');
|
|
|
|
if (!mozapp) {
|
|
|
|
return;
|
|
|
|
}
|
2014-06-30 03:16:00 -07:00
|
|
|
this.trackFrame(frame);
|
|
|
|
},
|
2014-01-31 10:01:58 -08:00
|
|
|
|
2014-10-12 23:43:00 -07:00
|
|
|
onFrameDestroyed: function (frame, isLastAppFrame) {
|
|
|
|
let mozapp = frame.getAttribute('mozapp');
|
|
|
|
if (!mozapp) {
|
|
|
|
return;
|
|
|
|
}
|
2014-06-30 03:16:00 -07:00
|
|
|
this.untrackFrame(frame);
|
2014-01-31 10:01:58 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
log: function dwp_log(message) {
|
2014-03-11 14:01:29 -07:00
|
|
|
if (this._logging) {
|
|
|
|
dump(DEVELOPER_HUD_LOG_PREFIX + ': ' + message + '\n');
|
|
|
|
}
|
2014-01-31 10:01:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-17 07:43:33 -07:00
|
|
|
* A Target object represents all there is to know about a Firefox OS app frame
|
|
|
|
* that is being tracked, e.g. a pointer to the frame, current values of watched
|
|
|
|
* metrics, and how to notify the front-end when metrics have changed.
|
2014-01-31 10:01:58 -08:00
|
|
|
*/
|
2014-03-03 11:50:56 -08:00
|
|
|
function Target(frame, actor) {
|
|
|
|
this.frame = frame;
|
2014-01-31 10:01:58 -08:00
|
|
|
this.actor = actor;
|
|
|
|
this.metrics = new Map();
|
|
|
|
}
|
|
|
|
|
2014-03-03 11:50:56 -08:00
|
|
|
Target.prototype = {
|
2014-03-11 14:01:29 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a metric that can later be updated. Does not update the front-end.
|
|
|
|
*/
|
|
|
|
register: function target_register(metric) {
|
|
|
|
this.metrics.set(metric, 0);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Modify one of a target's metrics, and send out an event to notify relevant
|
|
|
|
* parties (e.g. the developer HUD, automated tests, etc).
|
|
|
|
*/
|
2014-04-16 10:50:37 -07:00
|
|
|
update: function target_update(metric, message) {
|
|
|
|
if (!metric.name) {
|
|
|
|
throw new Error('Missing metric.name');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!metric.value) {
|
|
|
|
metric.value = 0;
|
|
|
|
}
|
|
|
|
|
2014-03-11 14:01:29 -07:00
|
|
|
let metrics = this.metrics;
|
2014-04-16 10:50:37 -07:00
|
|
|
if (metrics) {
|
|
|
|
metrics.set(metric.name, metric.value);
|
|
|
|
}
|
2014-03-11 14:01:29 -07:00
|
|
|
|
2014-03-03 11:50:56 -08:00
|
|
|
let data = {
|
2014-03-11 14:01:29 -07:00
|
|
|
metrics: [], // FIXME(Bug 982066) Remove this field.
|
|
|
|
manifest: this.frame.appManifestURL,
|
|
|
|
metric: metric,
|
|
|
|
message: message
|
2014-03-03 11:50:56 -08:00
|
|
|
};
|
2014-01-31 10:01:58 -08:00
|
|
|
|
2014-03-11 14:01:29 -07:00
|
|
|
// FIXME(Bug 982066) Remove this loop.
|
2014-01-31 10:01:58 -08:00
|
|
|
if (metrics && metrics.size > 0) {
|
|
|
|
for (let name of metrics.keys()) {
|
|
|
|
data.metrics.push({name: name, value: metrics.get(name)});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-11 14:01:29 -07:00
|
|
|
if (message) {
|
|
|
|
developerHUD.log('[' + data.manifest + '] ' + data.message);
|
|
|
|
}
|
|
|
|
this._send(data);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Nicer way to call update() when the metric value is a number that needs
|
|
|
|
* to be incremented.
|
|
|
|
*/
|
|
|
|
bump: function target_bump(metric, message) {
|
2014-04-16 10:50:37 -07:00
|
|
|
metric.value = (this.metrics.get(metric.name) || 0) + 1;
|
|
|
|
this.update(metric, message);
|
2014-03-11 14:01:29 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Void a metric value and make sure it isn't displayed on the front-end
|
|
|
|
* anymore.
|
|
|
|
*/
|
|
|
|
clear: function target_clear(metric) {
|
2014-04-16 10:50:37 -07:00
|
|
|
metric.value = 0;
|
|
|
|
this.update(metric);
|
2014-03-11 14:01:29 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tear everything down, including the front-end by sending a message without
|
|
|
|
* widgets.
|
|
|
|
*/
|
|
|
|
destroy: function target_destroy() {
|
|
|
|
delete this.metrics;
|
|
|
|
this._send({});
|
|
|
|
},
|
2014-03-03 11:50:56 -08:00
|
|
|
|
2014-03-11 14:01:29 -07:00
|
|
|
_send: function target_send(data) {
|
2014-06-04 11:11:06 -07:00
|
|
|
let frame = this.frame;
|
|
|
|
|
|
|
|
let systemapp = document.querySelector('#systemapp');
|
|
|
|
if (this.frame === systemapp) {
|
|
|
|
frame = getContentWindow();
|
|
|
|
}
|
|
|
|
|
|
|
|
shell.sendEvent(frame, 'developer-hud-update', Cu.cloneInto(data, frame));
|
2014-01-31 10:01:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-02-12 06:54:09 -08:00
|
|
|
* The Console Watcher tracks the following metrics in apps: reflows, warnings,
|
2014-03-17 07:43:33 -07:00
|
|
|
* and errors, with security errors reported separately.
|
2014-01-31 10:01:58 -08:00
|
|
|
*/
|
|
|
|
let consoleWatcher = {
|
|
|
|
|
2014-03-17 07:43:33 -07:00
|
|
|
_client: null,
|
2014-03-03 11:50:56 -08:00
|
|
|
_targets: new Map(),
|
2014-02-12 06:54:09 -08:00
|
|
|
_watching: {
|
|
|
|
reflows: false,
|
|
|
|
warnings: false,
|
2014-03-17 07:43:33 -07:00
|
|
|
errors: false,
|
|
|
|
security: false
|
2014-02-12 06:54:09 -08:00
|
|
|
},
|
2014-03-17 07:43:33 -07:00
|
|
|
_security: [
|
|
|
|
'Mixed Content Blocker',
|
|
|
|
'Mixed Content Message',
|
|
|
|
'CSP',
|
|
|
|
'Invalid HSTS Headers',
|
2014-09-03 10:24:12 -07:00
|
|
|
'Invalid HPKP Headers',
|
2014-03-17 07:43:33 -07:00
|
|
|
'Insecure Password Field',
|
|
|
|
'SSL',
|
|
|
|
'CORS'
|
|
|
|
],
|
2014-01-31 10:01:58 -08:00
|
|
|
|
|
|
|
init: function cw_init(client) {
|
|
|
|
this._client = client;
|
|
|
|
this.consoleListener = this.consoleListener.bind(this);
|
|
|
|
|
2014-02-12 06:54:09 -08:00
|
|
|
let watching = this._watching;
|
|
|
|
|
|
|
|
for (let key in watching) {
|
|
|
|
let metric = key;
|
2014-03-17 07:43:33 -07:00
|
|
|
SettingsListener.observe('hud.' + metric, watching[metric], watch => {
|
2014-02-12 06:54:09 -08:00
|
|
|
// Watch or unwatch the metric.
|
2014-02-26 13:58:37 -08:00
|
|
|
if (watching[metric] = watch) {
|
2014-02-12 06:54:09 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If unwatched, remove any existing widgets for that metric.
|
2014-03-03 11:50:56 -08:00
|
|
|
for (let target of this._targets.values()) {
|
2014-04-16 10:50:37 -07:00
|
|
|
target.clear({name: metric});
|
2014-02-12 06:54:09 -08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-01-31 10:01:58 -08:00
|
|
|
client.addListener('logMessage', this.consoleListener);
|
|
|
|
client.addListener('pageError', this.consoleListener);
|
|
|
|
client.addListener('consoleAPICall', this.consoleListener);
|
|
|
|
client.addListener('reflowActivity', this.consoleListener);
|
|
|
|
},
|
|
|
|
|
2014-03-03 11:50:56 -08:00
|
|
|
trackTarget: function cw_trackTarget(target) {
|
2014-03-11 14:01:29 -07:00
|
|
|
target.register('reflows');
|
|
|
|
target.register('warnings');
|
|
|
|
target.register('errors');
|
2014-03-17 07:43:33 -07:00
|
|
|
target.register('security');
|
2014-01-31 10:01:58 -08:00
|
|
|
|
|
|
|
this._client.request({
|
2014-03-03 11:50:56 -08:00
|
|
|
to: target.actor.consoleActor,
|
2014-01-31 10:01:58 -08:00
|
|
|
type: 'startListeners',
|
|
|
|
listeners: ['LogMessage', 'PageError', 'ConsoleAPI', 'ReflowActivity']
|
|
|
|
}, (res) => {
|
2014-03-03 11:50:56 -08:00
|
|
|
this._targets.set(target.actor.consoleActor, target);
|
2014-01-31 10:01:58 -08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-03-03 11:50:56 -08:00
|
|
|
untrackTarget: function cw_untrackTarget(target) {
|
2014-01-31 10:01:58 -08:00
|
|
|
this._client.request({
|
2014-03-03 11:50:56 -08:00
|
|
|
to: target.actor.consoleActor,
|
2014-01-31 10:01:58 -08:00
|
|
|
type: 'stopListeners',
|
|
|
|
listeners: ['LogMessage', 'PageError', 'ConsoleAPI', 'ReflowActivity']
|
|
|
|
}, (res) => { });
|
|
|
|
|
2014-03-03 11:50:56 -08:00
|
|
|
this._targets.delete(target.actor.consoleActor);
|
2014-01-31 10:01:58 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
consoleListener: function cw_consoleListener(type, packet) {
|
2014-03-03 11:50:56 -08:00
|
|
|
let target = this._targets.get(packet.from);
|
2014-04-16 10:50:37 -07:00
|
|
|
let metric = {};
|
2014-01-31 10:01:58 -08:00
|
|
|
let output = '';
|
|
|
|
|
|
|
|
switch (packet.type) {
|
|
|
|
|
|
|
|
case 'pageError':
|
|
|
|
let pageError = packet.pageError;
|
2014-02-12 06:54:09 -08:00
|
|
|
|
2014-01-31 10:01:58 -08:00
|
|
|
if (pageError.warning || pageError.strict) {
|
2014-04-16 10:50:37 -07:00
|
|
|
metric.name = 'warnings';
|
2014-10-09 03:20:00 -07:00
|
|
|
output += 'Warning (';
|
2014-01-31 10:01:58 -08:00
|
|
|
} else {
|
2014-04-16 10:50:37 -07:00
|
|
|
metric.name = 'errors';
|
2014-10-09 03:20:00 -07:00
|
|
|
output += 'Error (';
|
2014-01-31 10:01:58 -08:00
|
|
|
}
|
2014-02-12 06:54:09 -08:00
|
|
|
|
2014-03-17 07:43:33 -07:00
|
|
|
if (this._security.indexOf(pageError.category) > -1) {
|
2014-04-16 10:50:37 -07:00
|
|
|
metric.name = 'security';
|
2014-03-17 07:43:33 -07:00
|
|
|
}
|
|
|
|
|
2014-01-31 10:01:58 -08:00
|
|
|
let {errorMessage, sourceName, category, lineNumber, columnNumber} = pageError;
|
|
|
|
output += category + '): "' + (errorMessage.initial || errorMessage) +
|
|
|
|
'" in ' + sourceName + ':' + lineNumber + ':' + columnNumber;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'consoleAPICall':
|
2014-03-17 07:43:33 -07:00
|
|
|
switch (packet.message.level) {
|
2014-02-12 06:54:09 -08:00
|
|
|
|
2014-01-31 10:01:58 -08:00
|
|
|
case 'error':
|
2014-04-16 10:50:37 -07:00
|
|
|
metric.name = 'errors';
|
2014-10-09 03:20:00 -07:00
|
|
|
output += 'Error (console)';
|
2014-01-31 10:01:58 -08:00
|
|
|
break;
|
2014-02-12 06:54:09 -08:00
|
|
|
|
2014-01-31 10:01:58 -08:00
|
|
|
case 'warn':
|
2014-04-16 10:50:37 -07:00
|
|
|
metric.name = 'warnings';
|
2014-10-09 03:20:00 -07:00
|
|
|
output += 'Warning (console)';
|
2014-01-31 10:01:58 -08:00
|
|
|
break;
|
2014-02-12 06:54:09 -08:00
|
|
|
|
2014-01-31 10:01:58 -08:00
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'reflowActivity':
|
2014-04-16 10:50:37 -07:00
|
|
|
metric.name = 'reflows';
|
2014-02-12 06:54:09 -08:00
|
|
|
|
2014-04-16 10:50:37 -07:00
|
|
|
let {start, end, sourceURL, interruptible} = packet;
|
|
|
|
metric.interruptible = interruptible;
|
2014-01-31 10:01:58 -08:00
|
|
|
let duration = Math.round((end - start) * 100) / 100;
|
2014-10-09 03:20:00 -07:00
|
|
|
output += 'Reflow: ' + duration + 'ms';
|
2014-01-31 10:01:58 -08:00
|
|
|
if (sourceURL) {
|
|
|
|
output += ' ' + this.formatSourceURL(packet);
|
|
|
|
}
|
|
|
|
break;
|
2014-04-16 10:50:37 -07:00
|
|
|
|
|
|
|
default:
|
|
|
|
return;
|
2014-01-31 10:01:58 -08:00
|
|
|
}
|
|
|
|
|
2014-04-16 10:50:37 -07:00
|
|
|
if (!this._watching[metric.name]) {
|
2014-03-11 14:01:29 -07:00
|
|
|
return;
|
2014-01-31 10:01:58 -08:00
|
|
|
}
|
2014-03-11 14:01:29 -07:00
|
|
|
|
|
|
|
target.bump(metric, output);
|
2014-01-31 10:01:58 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
formatSourceURL: function cw_formatSourceURL(packet) {
|
|
|
|
// Abbreviate source URL
|
|
|
|
let source = WebConsoleUtils.abbreviateSourceURL(packet.sourceURL);
|
|
|
|
|
|
|
|
// Add function name and line number
|
|
|
|
let {functionName, sourceLine} = packet;
|
|
|
|
source = 'in ' + (functionName || '<anonymousFunction>') +
|
|
|
|
', ' + source + ':' + sourceLine;
|
|
|
|
|
|
|
|
return source;
|
|
|
|
}
|
|
|
|
};
|
2014-02-26 13:58:37 -08:00
|
|
|
developerHUD.registerWatcher(consoleWatcher);
|
2014-02-14 08:52:14 -08:00
|
|
|
|
|
|
|
|
2014-02-26 13:58:37 -08:00
|
|
|
let eventLoopLagWatcher = {
|
2014-02-14 08:52:14 -08:00
|
|
|
_client: null,
|
|
|
|
_fronts: new Map(),
|
|
|
|
_active: false,
|
|
|
|
|
|
|
|
init: function(client) {
|
|
|
|
this._client = client;
|
|
|
|
|
2014-02-26 13:58:37 -08:00
|
|
|
SettingsListener.observe('hud.jank', false, this.settingsListener.bind(this));
|
2014-02-14 08:52:14 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
settingsListener: function(value) {
|
|
|
|
if (this._active == value) {
|
|
|
|
return;
|
|
|
|
}
|
2014-10-09 03:20:00 -07:00
|
|
|
|
2014-02-14 08:52:14 -08:00
|
|
|
this._active = value;
|
|
|
|
|
|
|
|
// Toggle the state of existing fronts.
|
|
|
|
let fronts = this._fronts;
|
2014-03-03 11:50:56 -08:00
|
|
|
for (let target of fronts.keys()) {
|
2014-02-14 08:52:14 -08:00
|
|
|
if (value) {
|
2014-03-03 11:50:56 -08:00
|
|
|
fronts.get(target).start();
|
2014-02-14 08:52:14 -08:00
|
|
|
} else {
|
2014-03-03 11:50:56 -08:00
|
|
|
fronts.get(target).stop();
|
2014-04-16 10:50:37 -07:00
|
|
|
target.clear({name: 'jank'});
|
2014-02-14 08:52:14 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-03-03 11:50:56 -08:00
|
|
|
trackTarget: function(target) {
|
2014-03-11 14:01:29 -07:00
|
|
|
target.register('jank');
|
2014-02-14 08:52:14 -08:00
|
|
|
|
2014-03-03 11:50:56 -08:00
|
|
|
let front = new EventLoopLagFront(this._client, target.actor);
|
|
|
|
this._fronts.set(target, front);
|
2014-02-14 08:52:14 -08:00
|
|
|
|
|
|
|
front.on('event-loop-lag', time => {
|
2014-10-09 03:20:00 -07:00
|
|
|
target.update({name: 'jank', value: time}, 'Jank: ' + time + 'ms');
|
2014-02-14 08:52:14 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
if (this._active) {
|
|
|
|
front.start();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-03-03 11:50:56 -08:00
|
|
|
untrackTarget: function(target) {
|
2014-02-14 08:52:14 -08:00
|
|
|
let fronts = this._fronts;
|
2014-03-03 11:50:56 -08:00
|
|
|
if (fronts.has(target)) {
|
|
|
|
fronts.get(target).destroy();
|
|
|
|
fronts.delete(target);
|
2014-02-14 08:52:14 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2014-02-26 13:58:37 -08:00
|
|
|
developerHUD.registerWatcher(eventLoopLagWatcher);
|
2014-02-14 08:52:14 -08:00
|
|
|
|
2014-02-26 13:58:37 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Memory Watcher uses devtools actors to track memory usage.
|
|
|
|
*/
|
|
|
|
let memoryWatcher = {
|
|
|
|
|
|
|
|
_client: null,
|
|
|
|
_fronts: new Map(),
|
|
|
|
_timers: new Map(),
|
|
|
|
_watching: {
|
2014-06-12 21:38:00 -07:00
|
|
|
uss: false,
|
|
|
|
appmemory: false,
|
2014-02-26 13:58:37 -08:00
|
|
|
jsobjects: false,
|
|
|
|
jsstrings: false,
|
|
|
|
jsother: false,
|
|
|
|
dom: false,
|
|
|
|
style: false,
|
|
|
|
other: false
|
|
|
|
},
|
|
|
|
_active: false,
|
|
|
|
|
|
|
|
init: function mw_init(client) {
|
|
|
|
this._client = client;
|
|
|
|
let watching = this._watching;
|
|
|
|
|
|
|
|
for (let key in watching) {
|
|
|
|
let category = key;
|
|
|
|
SettingsListener.observe('hud.' + category, false, watch => {
|
|
|
|
watching[category] = watch;
|
2014-06-12 21:38:00 -07:00
|
|
|
this.update();
|
2014-02-26 13:58:37 -08:00
|
|
|
});
|
|
|
|
}
|
2014-06-12 21:38:00 -07:00
|
|
|
},
|
2014-02-26 13:58:37 -08:00
|
|
|
|
2014-06-12 21:38:00 -07:00
|
|
|
update: function mw_update() {
|
|
|
|
let watching = this._watching;
|
2014-10-09 03:20:00 -07:00
|
|
|
let active = watching.appmemory || watching.uss;
|
2014-06-12 21:38:00 -07:00
|
|
|
|
|
|
|
if (this._active) {
|
|
|
|
for (let target of this._fronts.keys()) {
|
|
|
|
if (!watching.appmemory) target.clear({name: 'memory'});
|
|
|
|
if (!watching.uss) target.clear({name: 'uss'});
|
|
|
|
if (!active) clearTimeout(this._timers.get(target));
|
2014-02-26 13:58:37 -08:00
|
|
|
}
|
2014-06-12 21:38:00 -07:00
|
|
|
} else if (active) {
|
|
|
|
for (let target of this._fronts.keys()) {
|
|
|
|
this.measure(target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._active = active;
|
2014-02-26 13:58:37 -08:00
|
|
|
},
|
|
|
|
|
2014-03-03 11:50:56 -08:00
|
|
|
measure: function mw_measure(target) {
|
2014-02-26 13:58:37 -08:00
|
|
|
let watch = this._watching;
|
2014-03-03 11:50:56 -08:00
|
|
|
let front = this._fronts.get(target);
|
2014-10-09 03:20:00 -07:00
|
|
|
let format = this.formatMemory;
|
2014-02-26 13:58:37 -08:00
|
|
|
|
2014-06-12 21:38:00 -07:00
|
|
|
if (watch.uss) {
|
|
|
|
front.residentUnique().then(value => {
|
2014-10-09 03:20:00 -07:00
|
|
|
target.update({name: 'uss', value: value}, 'USS: ' + format(value));
|
2014-06-12 21:38:00 -07:00
|
|
|
}, err => {
|
|
|
|
console.error(err);
|
|
|
|
});
|
|
|
|
}
|
2014-02-26 13:58:37 -08:00
|
|
|
|
2014-06-12 21:38:00 -07:00
|
|
|
if (watch.appmemory) {
|
|
|
|
front.measure().then(data => {
|
|
|
|
let total = 0;
|
2014-10-09 03:20:00 -07:00
|
|
|
let details = [];
|
|
|
|
|
|
|
|
function item(name, condition, value) {
|
|
|
|
if (!condition) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let v = parseInt(value);
|
|
|
|
total += v;
|
|
|
|
details.push(name + ': ' + format(v));
|
2014-06-12 21:38:00 -07:00
|
|
|
}
|
2014-10-09 03:20:00 -07:00
|
|
|
|
|
|
|
item('JS objects', watch.jsobjects, data.jsObjectsSize);
|
|
|
|
item('JS strings', watch.jsstrings, data.jsStringsSize);
|
|
|
|
item('JS other', watch.jsother, data.jsOtherSize);
|
|
|
|
item('DOM', watch.dom, data.domSize);
|
|
|
|
item('Style', watch.style, data.styleSize);
|
|
|
|
item('Other', watch.other, data.otherSize);
|
2014-06-12 21:38:00 -07:00
|
|
|
// TODO Also count images size (bug #976007).
|
|
|
|
|
2014-10-09 03:20:00 -07:00
|
|
|
target.update({name: 'memory', value: total},
|
|
|
|
'App Memory: ' + format(total) + ' (' + details.join(', ') + ')');
|
2014-06-12 21:38:00 -07:00
|
|
|
}, err => {
|
|
|
|
console.error(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-10-09 03:20:00 -07:00
|
|
|
let timer = setTimeout(() => this.measure(target), 800);
|
2014-06-12 21:38:00 -07:00
|
|
|
this._timers.set(target, timer);
|
2014-02-26 13:58:37 -08:00
|
|
|
},
|
|
|
|
|
2014-10-09 03:20:00 -07:00
|
|
|
formatMemory: function mw_formatMemory(bytes) {
|
|
|
|
var prefix = ['','K','M','G','T','P','E','Z','Y'];
|
|
|
|
var i = 0;
|
|
|
|
for (; bytes > 1024 && i < prefix.length; ++i) {
|
|
|
|
bytes /= 1024;
|
|
|
|
}
|
|
|
|
return (Math.round(bytes * 100) / 100) + ' ' + prefix[i] + 'B';
|
|
|
|
},
|
|
|
|
|
2014-03-03 11:50:56 -08:00
|
|
|
trackTarget: function mw_trackTarget(target) {
|
2014-03-11 14:01:29 -07:00
|
|
|
target.register('uss');
|
|
|
|
target.register('memory');
|
2014-03-03 11:50:56 -08:00
|
|
|
this._fronts.set(target, MemoryFront(this._client, target.actor));
|
2014-02-26 13:58:37 -08:00
|
|
|
if (this._active) {
|
2014-03-03 11:50:56 -08:00
|
|
|
this.measure(target);
|
2014-02-26 13:58:37 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-03-03 11:50:56 -08:00
|
|
|
untrackTarget: function mw_untrackTarget(target) {
|
|
|
|
let front = this._fronts.get(target);
|
2014-02-26 13:58:37 -08:00
|
|
|
if (front) {
|
|
|
|
front.destroy();
|
2014-03-03 11:50:56 -08:00
|
|
|
clearTimeout(this._timers.get(target));
|
|
|
|
this._fronts.delete(target);
|
|
|
|
this._timers.delete(target);
|
2014-02-26 13:58:37 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
developerHUD.registerWatcher(memoryWatcher);
|