| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * This file is part of the Adblock Plus build tools, | 2 * This file is part of the Adblock Plus build tools, |
| 3 * Copyright (C) 2006-2013 Eyeo GmbH | 3 * Copyright (C) 2006-2013 Eyeo GmbH |
| 4 * | 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
| 8 * | 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 28 * @param {String} [when] when to execute applyToWindow(). "start" means immed iately | 28 * @param {String} [when] when to execute applyToWindow(). "start" means immed iately |
| 29 * when the window opens, "ready" when its contents are available | 29 * when the window opens, "ready" when its contents are available |
| 30 * and "end" (default) means to wait until the "load" e vent. | 30 * and "end" (default) means to wait until the "load" e vent. |
| 31 * @constructor | 31 * @constructor |
| 32 */ | 32 */ |
| 33 function WindowObserver(listener, when) | 33 function WindowObserver(listener, when) |
| 34 { | 34 { |
| 35 this._listener = listener; | 35 this._listener = listener; |
| 36 this._when = when; | 36 this._when = when; |
| 37 | 37 |
| 38 let windows = []; | |
| 38 let e = Services.wm.getZOrderDOMWindowEnumerator(null, true); | 39 let e = Services.wm.getZOrderDOMWindowEnumerator(null, true); |
| 39 if (!e.hasMoreElements()) | 40 if (!e.hasMoreElements()) |
| 40 { | 41 { |
| 41 // On Linux the list returned will be empty, see bug 156333. Fall back to ra ndom order. | 42 // On Linux the list returned will be empty, see bug 156333. Fall back to ra ndom order. |
| 42 e = Services.wm.getEnumerator(null); | 43 e = Services.wm.getEnumerator(null); |
| 43 } | 44 } |
|
Wladimir Palant
2013/09/11 11:31:26
I think we can drop the fallback here, ww.getWindo
| |
| 44 while (e.hasMoreElements()) | 45 while (e.hasMoreElements()) |
| 45 { | 46 { |
| 46 let window = e.getNext().QueryInterface(Ci.nsIDOMWindow); | 47 let element = e.getNext(); |
| 47 if (when == "start" || window.document.readyState == "complete") | 48 this._addWindowListener(element); |
| 48 this._listener.applyToWindow(window); | 49 windows.push(element); |
| 49 else | 50 } |
| 50 this.observe(window, "chrome-document-global-created", null); | 51 |
| 52 // Check if there are any windows that we missed because they are not visible yet | |
| 53 let eAll = Services.ww.getWindowEnumerator(); | |
| 54 while (eAll.hasMoreElements()) | |
| 55 { | |
| 56 let element = eAll.getNext(); | |
| 57 if (windows.indexOf(element) < 0) | |
| 58 this._addWindowListener(element); | |
|
Wladimir Palant
2013/09/11 11:31:26
I'd rather have windows.push(element) here - simpl
| |
| 51 } | 59 } |
| 52 | 60 |
| 53 Services.obs.addObserver(this, "chrome-document-global-created", true); | 61 Services.obs.addObserver(this, "chrome-document-global-created", true); |
| 54 | 62 |
| 55 this._shutdownHandler = function() | 63 this._shutdownHandler = function() |
| 56 { | 64 { |
| 57 let e = Services.ww.getWindowEnumerator(); | 65 let e = Services.ww.getWindowEnumerator(); |
| 58 while (e.hasMoreElements()) | 66 while (e.hasMoreElements()) |
| 59 this._listener.removeFromWindow(e.getNext().QueryInterface(Ci.nsIDOMWindow )); | 67 this._listener.removeFromWindow(e.getNext().QueryInterface(Ci.nsIDOMWindow )); |
| 60 | 68 |
| 61 Services.obs.removeObserver(this, "chrome-document-global-created"); | 69 Services.obs.removeObserver(this, "chrome-document-global-created"); |
| 62 }.bind(this); | 70 }.bind(this); |
| 63 onShutdown.add(this._shutdownHandler); | 71 onShutdown.add(this._shutdownHandler); |
| 64 } | 72 } |
| 65 WindowObserver.prototype = | 73 WindowObserver.prototype = |
| 66 { | 74 { |
| 67 _listener: null, | 75 _listener: null, |
| 68 _when: null, | 76 _when: null, |
| 69 _shutdownHandler: null, | 77 _shutdownHandler: null, |
| 70 | 78 |
| 79 _addWindowListener: function(e) | |
| 80 { | |
| 81 let window = e.QueryInterface(Ci.nsIDOMWindow); | |
| 82 if (this._when == "start" || window.document.readyState == "complete") | |
| 83 this._listener.applyToWindow(window); | |
| 84 else | |
| 85 this.observe(window, "chrome-document-global-created", null); | |
| 86 }, | |
| 87 | |
| 71 shutdown: function() | 88 shutdown: function() |
| 72 { | 89 { |
| 73 if (!this._shutdownHandler) | 90 if (!this._shutdownHandler) |
| 74 return; | 91 return; |
| 75 | 92 |
| 76 onShutdown.remove(this._shutdownHandler); | 93 onShutdown.remove(this._shutdownHandler); |
| 77 this._shutdownHandler(); | 94 this._shutdownHandler(); |
| 78 this._shutdownHandler = null; | 95 this._shutdownHandler = null; |
| 79 }, | 96 }, |
| 80 | 97 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 95 window.removeEventListener(event, listener, false); | 112 window.removeEventListener(event, listener, false); |
| 96 if (this._shutdownHandler) | 113 if (this._shutdownHandler) |
| 97 this._listener.applyToWindow(window); | 114 this._listener.applyToWindow(window); |
| 98 }.bind(this); | 115 }.bind(this); |
| 99 window.addEventListener(event, listener, false); | 116 window.addEventListener(event, listener, false); |
| 100 } | 117 } |
| 101 }, | 118 }, |
| 102 | 119 |
| 103 QueryInterface: XPCOMUtils.generateQI([Ci.nsISupportsWeakReference, Ci.nsIObse rver]) | 120 QueryInterface: XPCOMUtils.generateQI([Ci.nsISupportsWeakReference, Ci.nsIObse rver]) |
| 104 }; | 121 }; |
| OLD | NEW |