Wed, 31 Dec 2014 13:27:57 +0100
Ignore runtime configuration files generated during quality assurance.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | "use strict"; |
michael@0 | 5 | |
michael@0 | 6 | const {Cc, Ci, Cu, Cr} = require("chrome"); |
michael@0 | 7 | const {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); |
michael@0 | 8 | |
michael@0 | 9 | const events = require("sdk/event/core"); |
michael@0 | 10 | const {Promise: promise} = Cu.import("resource://gre/modules/Promise.jsm", {}); |
michael@0 | 11 | |
michael@0 | 12 | /** |
michael@0 | 13 | * Handles adding an observer for the creation of content document globals, |
michael@0 | 14 | * event sent immediately after a web content document window has been set up, |
michael@0 | 15 | * but before any script code has been executed. |
michael@0 | 16 | */ |
michael@0 | 17 | function ContentObserver(tabActor) { |
michael@0 | 18 | this._contentWindow = tabActor.window; |
michael@0 | 19 | this._onContentGlobalCreated = this._onContentGlobalCreated.bind(this); |
michael@0 | 20 | this._onInnerWindowDestroyed = this._onInnerWindowDestroyed.bind(this); |
michael@0 | 21 | this.startListening(); |
michael@0 | 22 | } |
michael@0 | 23 | |
michael@0 | 24 | module.exports.ContentObserver = ContentObserver; |
michael@0 | 25 | |
michael@0 | 26 | ContentObserver.prototype = { |
michael@0 | 27 | /** |
michael@0 | 28 | * Starts listening for the required observer messages. |
michael@0 | 29 | */ |
michael@0 | 30 | startListening: function() { |
michael@0 | 31 | Services.obs.addObserver( |
michael@0 | 32 | this._onContentGlobalCreated, "content-document-global-created", false); |
michael@0 | 33 | Services.obs.addObserver( |
michael@0 | 34 | this._onInnerWindowDestroyed, "inner-window-destroyed", false); |
michael@0 | 35 | }, |
michael@0 | 36 | |
michael@0 | 37 | /** |
michael@0 | 38 | * Stops listening for the required observer messages. |
michael@0 | 39 | */ |
michael@0 | 40 | stopListening: function() { |
michael@0 | 41 | Services.obs.removeObserver( |
michael@0 | 42 | this._onContentGlobalCreated, "content-document-global-created", false); |
michael@0 | 43 | Services.obs.removeObserver( |
michael@0 | 44 | this._onInnerWindowDestroyed, "inner-window-destroyed", false); |
michael@0 | 45 | }, |
michael@0 | 46 | |
michael@0 | 47 | /** |
michael@0 | 48 | * Fired immediately after a web content document window has been set up. |
michael@0 | 49 | */ |
michael@0 | 50 | _onContentGlobalCreated: function(subject, topic, data) { |
michael@0 | 51 | if (subject == this._contentWindow) { |
michael@0 | 52 | events.emit(this, "global-created", subject); |
michael@0 | 53 | } |
michael@0 | 54 | }, |
michael@0 | 55 | |
michael@0 | 56 | /** |
michael@0 | 57 | * Fired when an inner window is removed from the backward/forward cache. |
michael@0 | 58 | */ |
michael@0 | 59 | _onInnerWindowDestroyed: function(subject, topic, data) { |
michael@0 | 60 | let id = subject.QueryInterface(Ci.nsISupportsPRUint64).data; |
michael@0 | 61 | events.emit(this, "global-destroyed", id); |
michael@0 | 62 | } |
michael@0 | 63 | }; |
michael@0 | 64 | |
michael@0 | 65 | // Utility functions. |
michael@0 | 66 | |
michael@0 | 67 | ContentObserver.GetInnerWindowID = function(window) { |
michael@0 | 68 | return window |
michael@0 | 69 | .QueryInterface(Ci.nsIInterfaceRequestor) |
michael@0 | 70 | .getInterface(Ci.nsIDOMWindowUtils) |
michael@0 | 71 | .currentInnerWindowID; |
michael@0 | 72 | }; |