toolkit/devtools/server/actors/eventlooplag.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/devtools/server/actors/eventlooplag.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,90 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +/**
     1.9 + * The eventLoopLag actor emits "event-loop-lag" events when the event
    1.10 + * loop gets unresponsive. The event comes with a "time" property (the
    1.11 + * duration of the lag in milliseconds).
    1.12 + */
    1.13 +
    1.14 +const {Ci, Cu} = require("chrome");
    1.15 +const Services = require("Services");
    1.16 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.17 +const protocol = require("devtools/server/protocol");
    1.18 +const {method, Arg, RetVal} = protocol;
    1.19 +const events = require("sdk/event/core");
    1.20 +
    1.21 +exports.register = function(handle) {
    1.22 +  handle.addGlobalActor(EventLoopLagActor, "eventLoopLagActor");
    1.23 +  handle.addTabActor(EventLoopLagActor, "eventLoopLagActor");
    1.24 +};
    1.25 +
    1.26 +exports.unregister = function(handle) {
    1.27 +  handle.removeGlobalActor(EventLoopLagActor);
    1.28 +  handle.removeTabActor(EventLoopLagActor);
    1.29 +};
    1.30 +
    1.31 +let EventLoopLagActor = protocol.ActorClass({
    1.32 +
    1.33 +  typeName: "eventLoopLag",
    1.34 +
    1.35 +  _observerAdded: false,
    1.36 +
    1.37 +  events: {
    1.38 +    "event-loop-lag" : {
    1.39 +      type: "event-loop-lag",
    1.40 +      time: Arg(0, "number") // duration of the lag in milliseconds.
    1.41 +    }
    1.42 +  },
    1.43 +
    1.44 +  /**
    1.45 +   * Start tracking the event loop lags.
    1.46 +   */
    1.47 +  start: method(function() {
    1.48 +    if (!this._observerAdded) {
    1.49 +      Services.obs.addObserver(this, 'event-loop-lag', false);
    1.50 +      this._observerAdded = true;
    1.51 +    }
    1.52 +    return Services.appShell.startEventLoopLagTracking();
    1.53 +  }, {
    1.54 +    request: {},
    1.55 +    response: {success: RetVal("number")}
    1.56 +  }),
    1.57 +
    1.58 +  /**
    1.59 +   * Stop tracking the event loop lags.
    1.60 +   */
    1.61 +  stop: method(function() {
    1.62 +    if (this._observerAdded) {
    1.63 +      Services.obs.removeObserver(this, 'event-loop-lag');
    1.64 +      this._observerAdded = false;
    1.65 +    }
    1.66 +    Services.appShell.stopEventLoopLagTracking();
    1.67 +  }, {request: {},response: {}}),
    1.68 +
    1.69 +  destroy: function() {
    1.70 +    this.stop();
    1.71 +    protocol.Actor.prototype.destroy.call(this);
    1.72 +  },
    1.73 +
    1.74 +  // nsIObserver
    1.75 +
    1.76 +  observe: function (subject, topic, data) {
    1.77 +    if (topic == "event-loop-lag") {
    1.78 +      // Forward event loop lag event
    1.79 +      events.emit(this, "event-loop-lag", data);
    1.80 +    }
    1.81 +  },
    1.82 +
    1.83 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
    1.84 +});
    1.85 +
    1.86 +exports.EventLoopLagFront = protocol.FrontClass(EventLoopLagActor, {
    1.87 +  initialize: function(client, form) {
    1.88 +    protocol.Front.prototype.initialize.call(this, client);
    1.89 +    this.actorID = form.eventLoopLagActor;
    1.90 +    client.addActorPool(this);
    1.91 +    this.manage(this);
    1.92 +  },
    1.93 +});

mercurial