|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 /** |
|
6 * The eventLoopLag actor emits "event-loop-lag" events when the event |
|
7 * loop gets unresponsive. The event comes with a "time" property (the |
|
8 * duration of the lag in milliseconds). |
|
9 */ |
|
10 |
|
11 const {Ci, Cu} = require("chrome"); |
|
12 const Services = require("Services"); |
|
13 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
|
14 const protocol = require("devtools/server/protocol"); |
|
15 const {method, Arg, RetVal} = protocol; |
|
16 const events = require("sdk/event/core"); |
|
17 |
|
18 exports.register = function(handle) { |
|
19 handle.addGlobalActor(EventLoopLagActor, "eventLoopLagActor"); |
|
20 handle.addTabActor(EventLoopLagActor, "eventLoopLagActor"); |
|
21 }; |
|
22 |
|
23 exports.unregister = function(handle) { |
|
24 handle.removeGlobalActor(EventLoopLagActor); |
|
25 handle.removeTabActor(EventLoopLagActor); |
|
26 }; |
|
27 |
|
28 let EventLoopLagActor = protocol.ActorClass({ |
|
29 |
|
30 typeName: "eventLoopLag", |
|
31 |
|
32 _observerAdded: false, |
|
33 |
|
34 events: { |
|
35 "event-loop-lag" : { |
|
36 type: "event-loop-lag", |
|
37 time: Arg(0, "number") // duration of the lag in milliseconds. |
|
38 } |
|
39 }, |
|
40 |
|
41 /** |
|
42 * Start tracking the event loop lags. |
|
43 */ |
|
44 start: method(function() { |
|
45 if (!this._observerAdded) { |
|
46 Services.obs.addObserver(this, 'event-loop-lag', false); |
|
47 this._observerAdded = true; |
|
48 } |
|
49 return Services.appShell.startEventLoopLagTracking(); |
|
50 }, { |
|
51 request: {}, |
|
52 response: {success: RetVal("number")} |
|
53 }), |
|
54 |
|
55 /** |
|
56 * Stop tracking the event loop lags. |
|
57 */ |
|
58 stop: method(function() { |
|
59 if (this._observerAdded) { |
|
60 Services.obs.removeObserver(this, 'event-loop-lag'); |
|
61 this._observerAdded = false; |
|
62 } |
|
63 Services.appShell.stopEventLoopLagTracking(); |
|
64 }, {request: {},response: {}}), |
|
65 |
|
66 destroy: function() { |
|
67 this.stop(); |
|
68 protocol.Actor.prototype.destroy.call(this); |
|
69 }, |
|
70 |
|
71 // nsIObserver |
|
72 |
|
73 observe: function (subject, topic, data) { |
|
74 if (topic == "event-loop-lag") { |
|
75 // Forward event loop lag event |
|
76 events.emit(this, "event-loop-lag", data); |
|
77 } |
|
78 }, |
|
79 |
|
80 QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]), |
|
81 }); |
|
82 |
|
83 exports.EventLoopLagFront = protocol.FrontClass(EventLoopLagActor, { |
|
84 initialize: function(client, form) { |
|
85 protocol.Front.prototype.initialize.call(this, client); |
|
86 this.actorID = form.eventLoopLagActor; |
|
87 client.addActorPool(this); |
|
88 this.manage(this); |
|
89 }, |
|
90 }); |