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 EventEmitter = require("resource://devtools/shared/event-emitter.js");
9 * Handles adding an observer for the creation of content document globals,
10 * event sent immediately after a web content document window has been set up,
11 * but before any script code has been executed.
13 function ContentObserver(targetActor) {
14 this._contentWindow = targetActor.window;
15 this._onContentGlobalCreated = this._onContentGlobalCreated.bind(this);
16 this._onInnerWindowDestroyed = this._onInnerWindowDestroyed.bind(this);
17 this.startListening();
20 module.exports.ContentObserver = ContentObserver;
22 ContentObserver.prototype = {
24 * Starts listening for the required observer messages.
27 Services.obs.addObserver(
28 this._onContentGlobalCreated,
29 "content-document-global-created"
31 Services.obs.addObserver(
32 this._onInnerWindowDestroyed,
33 "inner-window-destroyed"
38 * Stops listening for the required observer messages.
41 Services.obs.removeObserver(
42 this._onContentGlobalCreated,
43 "content-document-global-created"
45 Services.obs.removeObserver(
46 this._onInnerWindowDestroyed,
47 "inner-window-destroyed"
52 * Fired immediately after a web content document window has been set up.
54 _onContentGlobalCreated(subject, topic, data) {
55 if (subject == this._contentWindow) {
56 EventEmitter.emit(this, "global-created", subject);
61 * Fired when an inner window is removed from the backward/forward cache.
63 _onInnerWindowDestroyed(subject, topic, data) {
64 const id = subject.QueryInterface(Ci.nsISupportsPRUint64).data;
65 EventEmitter.emit(this, "global-destroyed", id);
71 ContentObserver.GetInnerWindowID = function (window) {
72 return window.windowGlobalChild.innerWindowId;