addon-sdk/source/lib/sdk/deprecated/window-utils.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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 module.metadata = {
michael@0 7 'stability': 'deprecated'
michael@0 8 };
michael@0 9
michael@0 10 const { Cc, Ci } = require('chrome');
michael@0 11 const { EventEmitter } = require('../deprecated/events');
michael@0 12 const { Trait } = require('../deprecated/traits');
michael@0 13 const { when } = require('../system/unload');
michael@0 14 const events = require('../system/events');
michael@0 15 const { getInnerId, getOuterId, windows, isDocumentLoaded, isBrowser,
michael@0 16 getMostRecentBrowserWindow, getMostRecentWindow } = require('../window/utils');
michael@0 17 const errors = require('../deprecated/errors');
michael@0 18 const { deprecateFunction } = require('../util/deprecate');
michael@0 19 const { ignoreWindow, isGlobalPBSupported } = require('sdk/private-browsing/utils');
michael@0 20 const { isPrivateBrowsingSupported } = require('../self');
michael@0 21
michael@0 22 const windowWatcher = Cc['@mozilla.org/embedcomp/window-watcher;1'].
michael@0 23 getService(Ci.nsIWindowWatcher);
michael@0 24 const appShellService = Cc['@mozilla.org/appshell/appShellService;1'].
michael@0 25 getService(Ci.nsIAppShellService);
michael@0 26
michael@0 27 // Bug 834961: ignore private windows when they are not supported
michael@0 28 function getWindows() windows(null, { includePrivate: isPrivateBrowsingSupported || isGlobalPBSupported });
michael@0 29
michael@0 30 /**
michael@0 31 * An iterator for XUL windows currently in the application.
michael@0 32 *
michael@0 33 * @return A generator that yields XUL windows exposing the
michael@0 34 * nsIDOMWindow interface.
michael@0 35 */
michael@0 36 function windowIterator() {
michael@0 37 // Bug 752631: We only pass already loaded window in order to avoid
michael@0 38 // breaking XUL windows DOM. DOM is broken when some JS code try
michael@0 39 // to access DOM during "uninitialized" state of the related document.
michael@0 40 let list = getWindows().filter(isDocumentLoaded);
michael@0 41 for (let i = 0, l = list.length; i < l; i++) {
michael@0 42 yield list[i];
michael@0 43 }
michael@0 44 };
michael@0 45 exports.windowIterator = windowIterator;
michael@0 46
michael@0 47 /**
michael@0 48 * An iterator for browser windows currently open in the application.
michael@0 49 * @returns {Function}
michael@0 50 * A generator that yields browser windows exposing the `nsIDOMWindow`
michael@0 51 * interface.
michael@0 52 */
michael@0 53 function browserWindowIterator() {
michael@0 54 for each (let window in windowIterator()) {
michael@0 55 if (isBrowser(window))
michael@0 56 yield window;
michael@0 57 }
michael@0 58 }
michael@0 59 exports.browserWindowIterator = browserWindowIterator;
michael@0 60
michael@0 61 function WindowTracker(delegate) {
michael@0 62 if (!(this instanceof WindowTracker)) {
michael@0 63 return new WindowTracker(delegate);
michael@0 64 }
michael@0 65
michael@0 66 this._delegate = delegate;
michael@0 67 this._loadingWindows = [];
michael@0 68
michael@0 69 for each (let window in getWindows())
michael@0 70 this._regWindow(window);
michael@0 71 windowWatcher.registerNotification(this);
michael@0 72 this._onToplevelWindowReady = this._onToplevelWindowReady.bind(this);
michael@0 73 events.on('toplevel-window-ready', this._onToplevelWindowReady);
michael@0 74
michael@0 75 require('../system/unload').ensure(this);
michael@0 76
michael@0 77 return this;
michael@0 78 };
michael@0 79
michael@0 80 WindowTracker.prototype = {
michael@0 81 _regLoadingWindow: function _regLoadingWindow(window) {
michael@0 82 // Bug 834961: ignore private windows when they are not supported
michael@0 83 if (ignoreWindow(window))
michael@0 84 return;
michael@0 85
michael@0 86 this._loadingWindows.push(window);
michael@0 87 window.addEventListener('load', this, true);
michael@0 88 },
michael@0 89
michael@0 90 _unregLoadingWindow: function _unregLoadingWindow(window) {
michael@0 91 var index = this._loadingWindows.indexOf(window);
michael@0 92
michael@0 93 if (index != -1) {
michael@0 94 this._loadingWindows.splice(index, 1);
michael@0 95 window.removeEventListener('load', this, true);
michael@0 96 }
michael@0 97 },
michael@0 98
michael@0 99 _regWindow: function _regWindow(window) {
michael@0 100 // Bug 834961: ignore private windows when they are not supported
michael@0 101 if (ignoreWindow(window))
michael@0 102 return;
michael@0 103
michael@0 104 if (window.document.readyState == 'complete') {
michael@0 105 this._unregLoadingWindow(window);
michael@0 106 this._delegate.onTrack(window);
michael@0 107 } else
michael@0 108 this._regLoadingWindow(window);
michael@0 109 },
michael@0 110
michael@0 111 _unregWindow: function _unregWindow(window) {
michael@0 112 if (window.document.readyState == 'complete') {
michael@0 113 if (this._delegate.onUntrack)
michael@0 114 this._delegate.onUntrack(window);
michael@0 115 } else {
michael@0 116 this._unregLoadingWindow(window);
michael@0 117 }
michael@0 118 },
michael@0 119
michael@0 120 unload: function unload() {
michael@0 121 windowWatcher.unregisterNotification(this);
michael@0 122 events.off('toplevel-window-ready', this._onToplevelWindowReady);
michael@0 123 for each (let window in getWindows())
michael@0 124 this._unregWindow(window);
michael@0 125 },
michael@0 126
michael@0 127 handleEvent: errors.catchAndLog(function handleEvent(event) {
michael@0 128 if (event.type == 'load' && event.target) {
michael@0 129 var window = event.target.defaultView;
michael@0 130 if (window)
michael@0 131 this._regWindow(window);
michael@0 132 }
michael@0 133 }),
michael@0 134
michael@0 135 _onToplevelWindowReady: function _onToplevelWindowReady({subject}) {
michael@0 136 let window = subject;
michael@0 137 // ignore private windows if they are not supported
michael@0 138 if (ignoreWindow(window))
michael@0 139 return;
michael@0 140 this._regWindow(window);
michael@0 141 },
michael@0 142
michael@0 143 observe: errors.catchAndLog(function observe(subject, topic, data) {
michael@0 144 var window = subject.QueryInterface(Ci.nsIDOMWindow);
michael@0 145 // ignore private windows if they are not supported
michael@0 146 if (ignoreWindow(window))
michael@0 147 return;
michael@0 148 if (topic == 'domwindowclosed')
michael@0 149 this._unregWindow(window);
michael@0 150 })
michael@0 151 };
michael@0 152 exports.WindowTracker = WindowTracker;
michael@0 153
michael@0 154 const WindowTrackerTrait = Trait.compose({
michael@0 155 _onTrack: Trait.required,
michael@0 156 _onUntrack: Trait.required,
michael@0 157 constructor: function WindowTrackerTrait() {
michael@0 158 WindowTracker({
michael@0 159 onTrack: this._onTrack.bind(this),
michael@0 160 onUntrack: this._onUntrack.bind(this)
michael@0 161 });
michael@0 162 }
michael@0 163 });
michael@0 164 exports.WindowTrackerTrait = WindowTrackerTrait;
michael@0 165
michael@0 166 var gDocsToClose = [];
michael@0 167
michael@0 168 function onDocUnload(event) {
michael@0 169 var index = gDocsToClose.indexOf(event.target);
michael@0 170 if (index == -1)
michael@0 171 throw new Error('internal error: unloading document not found');
michael@0 172 var document = gDocsToClose.splice(index, 1)[0];
michael@0 173 // Just in case, let's remove the event listener too.
michael@0 174 document.defaultView.removeEventListener('unload', onDocUnload, false);
michael@0 175 }
michael@0 176
michael@0 177 onDocUnload = require('./errors').catchAndLog(onDocUnload);
michael@0 178
michael@0 179 exports.closeOnUnload = function closeOnUnload(window) {
michael@0 180 window.addEventListener('unload', onDocUnload, false);
michael@0 181 gDocsToClose.push(window.document);
michael@0 182 };
michael@0 183
michael@0 184 Object.defineProperties(exports, {
michael@0 185 activeWindow: {
michael@0 186 enumerable: true,
michael@0 187 get: function() {
michael@0 188 return getMostRecentWindow(null);
michael@0 189 },
michael@0 190 set: function(window) {
michael@0 191 try {
michael@0 192 window.focus();
michael@0 193 } catch (e) {}
michael@0 194 }
michael@0 195 },
michael@0 196 activeBrowserWindow: {
michael@0 197 enumerable: true,
michael@0 198 get: getMostRecentBrowserWindow
michael@0 199 }
michael@0 200 });
michael@0 201
michael@0 202
michael@0 203 /**
michael@0 204 * Returns the ID of the window's current inner window.
michael@0 205 */
michael@0 206 exports.getInnerId = deprecateFunction(getInnerId,
michael@0 207 'require("window-utils").getInnerId is deprecated, ' +
michael@0 208 'please use require("sdk/window/utils").getInnerId instead'
michael@0 209 );
michael@0 210
michael@0 211 exports.getOuterId = deprecateFunction(getOuterId,
michael@0 212 'require("window-utils").getOuterId is deprecated, ' +
michael@0 213 'please use require("sdk/window/utils").getOuterId instead'
michael@0 214 );
michael@0 215
michael@0 216 exports.isBrowser = deprecateFunction(isBrowser,
michael@0 217 'require("window-utils").isBrowser is deprecated, ' +
michael@0 218 'please use require("sdk/window/utils").isBrowser instead'
michael@0 219 );
michael@0 220
michael@0 221 exports.hiddenWindow = appShellService.hiddenDOMWindow;
michael@0 222
michael@0 223 when(
michael@0 224 function() {
michael@0 225 gDocsToClose.slice().forEach(
michael@0 226 function(doc) { doc.defaultView.close(); });
michael@0 227 });

mercurial