| OLD | NEW |
| (Empty) |
| 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 | |
| 5 Cu.import("resource://gre/modules/Services.jsm"); | |
| 6 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); | |
| 7 | |
| 8 exports.WindowObserver = WindowObserver; | |
| 9 | |
| 10 /** | |
| 11 * This class will call listener's method applyToWindow() for all new chrome | |
| 12 * windows being opened. It will also call listener's method removeFromWindow() | |
| 13 * for all windows still open when the extension is shut down. | |
| 14 * @param {Object} listener | |
| 15 * @param {String} [when] when to execute applyToWindow(). "start" means immed
iately | |
| 16 * when the window opens, "ready" when its contents are
available | |
| 17 * and "end" (default) means to wait until the "load" e
vent. | |
| 18 * @constructor | |
| 19 */ | |
| 20 function WindowObserver(listener, when) | |
| 21 { | |
| 22 this._listener = listener; | |
| 23 this._when = when; | |
| 24 | |
| 25 let windows = []; | |
| 26 let e = Services.wm.getZOrderDOMWindowEnumerator(null, true); | |
| 27 while (e.hasMoreElements()) | |
| 28 windows.push(e.getNext()); | |
| 29 | |
| 30 // Check if there are any windows that we missed | |
| 31 let eAll = Services.ww.getWindowEnumerator(); | |
| 32 while (eAll.hasMoreElements()) | |
| 33 { | |
| 34 let element = eAll.getNext(); | |
| 35 if (windows.indexOf(element) < 0) | |
| 36 windows.push(element); | |
| 37 } | |
| 38 | |
| 39 for (let i = 0; i < windows.length; i++) | |
| 40 { | |
| 41 let window = windows[i].QueryInterface(Ci.nsIDOMWindow); | |
| 42 if (when == "start" || window.document.readyState == "complete") | |
| 43 this._listener.applyToWindow(window); | |
| 44 else | |
| 45 this.observe(window, "chrome-document-global-created", null); | |
| 46 } | |
| 47 | |
| 48 Services.obs.addObserver(this, "chrome-document-global-created", true); | |
| 49 | |
| 50 this._shutdownHandler = function() | |
| 51 { | |
| 52 let e = Services.ww.getWindowEnumerator(); | |
| 53 while (e.hasMoreElements()) | |
| 54 this._listener.removeFromWindow(e.getNext().QueryInterface(Ci.nsIDOMWindow
)); | |
| 55 | |
| 56 Services.obs.removeObserver(this, "chrome-document-global-created"); | |
| 57 }.bind(this); | |
| 58 onShutdown.add(this._shutdownHandler); | |
| 59 } | |
| 60 WindowObserver.prototype = | |
| 61 { | |
| 62 _listener: null, | |
| 63 _when: null, | |
| 64 _shutdownHandler: null, | |
| 65 | |
| 66 shutdown: function() | |
| 67 { | |
| 68 if (!this._shutdownHandler) | |
| 69 return; | |
| 70 | |
| 71 onShutdown.remove(this._shutdownHandler); | |
| 72 this._shutdownHandler(); | |
| 73 this._shutdownHandler = null; | |
| 74 }, | |
| 75 | |
| 76 observe: function(subject, topic, data) | |
| 77 { | |
| 78 if (topic == "chrome-document-global-created") | |
| 79 { | |
| 80 let window = subject.QueryInterface(Ci.nsIDOMWindow); | |
| 81 if (this._when == "start") | |
| 82 { | |
| 83 this._listener.applyToWindow(window); | |
| 84 return; | |
| 85 } | |
| 86 | |
| 87 let event = (this._when == "ready" ? "DOMContentLoaded" : "load"); | |
| 88 let listener = function() | |
| 89 { | |
| 90 window.removeEventListener(event, listener, false); | |
| 91 if (this._shutdownHandler) | |
| 92 this._listener.applyToWindow(window); | |
| 93 }.bind(this); | |
| 94 window.addEventListener(event, listener, false); | |
| 95 } | |
| 96 }, | |
| 97 | |
| 98 QueryInterface: XPCOMUtils.generateQI([Ci.nsISupportsWeakReference, Ci.nsIObse
rver]) | |
| 99 }; | |
| OLD | NEW |