Backed out changeset 90247f5e6db5 (bug 1564391) for causing marionette failures....
[gecko.git] / devtools / shared / content-observer.js
blob08271e4572b9780440954d93e4cfacaecf967cd8
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 "use strict";
6 const EventEmitter = require("resource://devtools/shared/event-emitter.js");
8 /**
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.
12  */
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 = {
23   /**
24    * Starts listening for the required observer messages.
25    */
26   startListening() {
27     Services.obs.addObserver(
28       this._onContentGlobalCreated,
29       "content-document-global-created"
30     );
31     Services.obs.addObserver(
32       this._onInnerWindowDestroyed,
33       "inner-window-destroyed"
34     );
35   },
37   /**
38    * Stops listening for the required observer messages.
39    */
40   stopListening() {
41     Services.obs.removeObserver(
42       this._onContentGlobalCreated,
43       "content-document-global-created"
44     );
45     Services.obs.removeObserver(
46       this._onInnerWindowDestroyed,
47       "inner-window-destroyed"
48     );
49   },
51   /**
52    * Fired immediately after a web content document window has been set up.
53    */
54   _onContentGlobalCreated(subject, topic, data) {
55     if (subject == this._contentWindow) {
56       EventEmitter.emit(this, "global-created", subject);
57     }
58   },
60   /**
61    * Fired when an inner window is removed from the backward/forward cache.
62    */
63   _onInnerWindowDestroyed(subject, topic, data) {
64     const id = subject.QueryInterface(Ci.nsISupportsPRUint64).data;
65     EventEmitter.emit(this, "global-destroyed", id);
66   },
69 // Utility functions.
71 ContentObserver.GetInnerWindowID = function (window) {
72   return window.windowGlobalChild.innerWindowId;