|
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"; |
|
5 |
|
6 const {Cc, Ci, Cu, Cr} = require("chrome"); |
|
7 const {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); |
|
8 |
|
9 const events = require("sdk/event/core"); |
|
10 const {Promise: promise} = Cu.import("resource://gre/modules/Promise.jsm", {}); |
|
11 |
|
12 /** |
|
13 * Handles adding an observer for the creation of content document globals, |
|
14 * event sent immediately after a web content document window has been set up, |
|
15 * but before any script code has been executed. |
|
16 */ |
|
17 function ContentObserver(tabActor) { |
|
18 this._contentWindow = tabActor.window; |
|
19 this._onContentGlobalCreated = this._onContentGlobalCreated.bind(this); |
|
20 this._onInnerWindowDestroyed = this._onInnerWindowDestroyed.bind(this); |
|
21 this.startListening(); |
|
22 } |
|
23 |
|
24 module.exports.ContentObserver = ContentObserver; |
|
25 |
|
26 ContentObserver.prototype = { |
|
27 /** |
|
28 * Starts listening for the required observer messages. |
|
29 */ |
|
30 startListening: function() { |
|
31 Services.obs.addObserver( |
|
32 this._onContentGlobalCreated, "content-document-global-created", false); |
|
33 Services.obs.addObserver( |
|
34 this._onInnerWindowDestroyed, "inner-window-destroyed", false); |
|
35 }, |
|
36 |
|
37 /** |
|
38 * Stops listening for the required observer messages. |
|
39 */ |
|
40 stopListening: function() { |
|
41 Services.obs.removeObserver( |
|
42 this._onContentGlobalCreated, "content-document-global-created", false); |
|
43 Services.obs.removeObserver( |
|
44 this._onInnerWindowDestroyed, "inner-window-destroyed", false); |
|
45 }, |
|
46 |
|
47 /** |
|
48 * Fired immediately after a web content document window has been set up. |
|
49 */ |
|
50 _onContentGlobalCreated: function(subject, topic, data) { |
|
51 if (subject == this._contentWindow) { |
|
52 events.emit(this, "global-created", subject); |
|
53 } |
|
54 }, |
|
55 |
|
56 /** |
|
57 * Fired when an inner window is removed from the backward/forward cache. |
|
58 */ |
|
59 _onInnerWindowDestroyed: function(subject, topic, data) { |
|
60 let id = subject.QueryInterface(Ci.nsISupportsPRUint64).data; |
|
61 events.emit(this, "global-destroyed", id); |
|
62 } |
|
63 }; |
|
64 |
|
65 // Utility functions. |
|
66 |
|
67 ContentObserver.GetInnerWindowID = function(window) { |
|
68 return window |
|
69 .QueryInterface(Ci.nsIInterfaceRequestor) |
|
70 .getInterface(Ci.nsIDOMWindowUtils) |
|
71 .currentInnerWindowID; |
|
72 }; |