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/. */
6 const { Ci } = require("chrome");
7 const Services = require("Services");
9 const EventEmitter = require("devtools/shared/event-emitter");
12 * Handles adding an observer for the creation of content document globals,
13 * event sent immediately after a web content document window has been set up,
14 * but before any script code has been executed.
16 function ContentObserver(targetActor) {
17 this._contentWindow = targetActor.window;
18 this._onContentGlobalCreated = this._onContentGlobalCreated.bind(this);
19 this._onInnerWindowDestroyed = this._onInnerWindowDestroyed.bind(this);
20 this.startListening();
23 module.exports.ContentObserver = ContentObserver;
25 ContentObserver.prototype = {
27 * Starts listening for the required observer messages.
29 startListening: function() {
30 Services.obs.addObserver(
31 this._onContentGlobalCreated,
32 "content-document-global-created"
34 Services.obs.addObserver(
35 this._onInnerWindowDestroyed,
36 "inner-window-destroyed"
41 * Stops listening for the required observer messages.
43 stopListening: function() {
44 Services.obs.removeObserver(
45 this._onContentGlobalCreated,
46 "content-document-global-created"
48 Services.obs.removeObserver(
49 this._onInnerWindowDestroyed,
50 "inner-window-destroyed"
55 * Fired immediately after a web content document window has been set up.
57 _onContentGlobalCreated: function(subject, topic, data) {
58 if (subject == this._contentWindow) {
59 EventEmitter.emit(this, "global-created", subject);
64 * Fired when an inner window is removed from the backward/forward cache.
66 _onInnerWindowDestroyed: function(subject, topic, data) {
67 const id = subject.QueryInterface(Ci.nsISupportsPRUint64).data;
68 EventEmitter.emit(this, "global-destroyed", id);
74 ContentObserver.GetInnerWindowID = function(window) {
75 return window.windowUtils.currentInnerWindowID;