toolkit/devtools/content-observer.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/devtools/content-observer.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,72 @@
     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 +"use strict";
     1.8 +
     1.9 +const {Cc, Ci, Cu, Cr} = require("chrome");
    1.10 +const {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
    1.11 +
    1.12 +const events = require("sdk/event/core");
    1.13 +const {Promise: promise} = Cu.import("resource://gre/modules/Promise.jsm", {});
    1.14 +
    1.15 +/**
    1.16 + * Handles adding an observer for the creation of content document globals,
    1.17 + * event sent immediately after a web content document window has been set up,
    1.18 + * but before any script code has been executed.
    1.19 + */
    1.20 +function ContentObserver(tabActor) {
    1.21 +  this._contentWindow = tabActor.window;
    1.22 +  this._onContentGlobalCreated = this._onContentGlobalCreated.bind(this);
    1.23 +  this._onInnerWindowDestroyed = this._onInnerWindowDestroyed.bind(this);
    1.24 +  this.startListening();
    1.25 +}
    1.26 +
    1.27 +module.exports.ContentObserver = ContentObserver;
    1.28 +
    1.29 +ContentObserver.prototype = {
    1.30 +  /**
    1.31 +   * Starts listening for the required observer messages.
    1.32 +   */
    1.33 +  startListening: function() {
    1.34 +    Services.obs.addObserver(
    1.35 +      this._onContentGlobalCreated, "content-document-global-created", false);
    1.36 +    Services.obs.addObserver(
    1.37 +      this._onInnerWindowDestroyed, "inner-window-destroyed", false);
    1.38 +  },
    1.39 +
    1.40 +  /**
    1.41 +   * Stops listening for the required observer messages.
    1.42 +   */
    1.43 +  stopListening: function() {
    1.44 +    Services.obs.removeObserver(
    1.45 +      this._onContentGlobalCreated, "content-document-global-created", false);
    1.46 +    Services.obs.removeObserver(
    1.47 +      this._onInnerWindowDestroyed, "inner-window-destroyed", false);
    1.48 +  },
    1.49 +
    1.50 +  /**
    1.51 +   * Fired immediately after a web content document window has been set up.
    1.52 +   */
    1.53 +  _onContentGlobalCreated: function(subject, topic, data) {
    1.54 +    if (subject == this._contentWindow) {
    1.55 +      events.emit(this, "global-created", subject);
    1.56 +    }
    1.57 +  },
    1.58 +
    1.59 +  /**
    1.60 +   * Fired when an inner window is removed from the backward/forward cache.
    1.61 +   */
    1.62 +  _onInnerWindowDestroyed: function(subject, topic, data) {
    1.63 +    let id = subject.QueryInterface(Ci.nsISupportsPRUint64).data;
    1.64 +    events.emit(this, "global-destroyed", id);
    1.65 +  }
    1.66 +};
    1.67 +
    1.68 +// Utility functions.
    1.69 +
    1.70 +ContentObserver.GetInnerWindowID = function(window) {
    1.71 +  return window
    1.72 +    .QueryInterface(Ci.nsIInterfaceRequestor)
    1.73 +    .getInterface(Ci.nsIDOMWindowUtils)
    1.74 +    .currentInnerWindowID;
    1.75 +};

mercurial