Skip to content
This repository has been archived by the owner on Feb 26, 2022. It is now read-only.

Commit

Permalink
windows() now always ignores private windows by default, unless priva…
Browse files Browse the repository at this point in the history
…te windows are also explicitly requested
  • Loading branch information
erikvold committed Mar 1, 2013
1 parent 16b11bc commit 7dfc495
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 28 deletions.
19 changes: 15 additions & 4 deletions lib/sdk/deprecated/window-utils.js
Expand Up @@ -11,16 +11,21 @@ const { Cc, Ci } = require('chrome');
const { EventEmitter } = require('../deprecated/events');
const { Trait } = require('../deprecated/traits');
const { when } = require('../system/unload');
const { getInnerId, getOuterId, windows, isDocumentLoaded, isBrowser, ignoreWindow,
const { getInnerId, getOuterId, windows, isDocumentLoaded, isBrowser,
getMostRecentBrowserWindow, getMostRecentWindow } = require('../window/utils');
const errors = require('../deprecated/errors');
const { deprecateFunction } = require('../util/deprecate');
const { ignoreWindow } = require('sdk/private-browsing/utils');
const { isPrivateBrowsingSupported } = require('../self');

const windowWatcher = Cc['@mozilla.org/embedcomp/window-watcher;1'].
getService(Ci.nsIWindowWatcher);
const appShellService = Cc['@mozilla.org/appshell/appShellService;1'].
getService(Ci.nsIAppShellService);

// Bug 834961: ignore private windows when they are not supported
function getWindows() windows(null, { includePrivate: isPrivateBrowsingSupported });

/**
* An iterator for XUL windows currently in the application.
*
Expand All @@ -31,7 +36,7 @@ function windowIterator() {
// Bug 752631: We only pass already loaded window in order to avoid
// breaking XUL windows DOM. DOM is broken when some JS code try
// to access DOM during "uninitialized" state of the related document.
let list = windows().filter(isDocumentLoaded);
let list = getWindows().filter(isDocumentLoaded);
for (let i = 0, l = list.length; i < l; i++) {
yield list[i];
}
Expand Down Expand Up @@ -60,7 +65,7 @@ function WindowTracker(delegate) {
this._delegate = delegate;
this._loadingWindows = [];

for each (let window in windows())
for each (let window in getWindows())
this._regWindow(window);
windowWatcher.registerNotification(this);

Expand All @@ -71,8 +76,10 @@ function WindowTracker(delegate) {

WindowTracker.prototype = {
_regLoadingWindow: function _regLoadingWindow(window) {
// Bug 834961: ignore private windows when they are not supported
if (ignoreWindow(window))
return;

this._loadingWindows.push(window);
window.addEventListener('load', this, true);
},
Expand All @@ -87,6 +94,10 @@ WindowTracker.prototype = {
},

_regWindow: function _regWindow(window) {
// Bug 834961: ignore private windows when they are not supported
if (ignoreWindow(window))
return;

if (window.document.readyState == 'complete') {
this._unregLoadingWindow(window);
this._delegate.onTrack(window);
Expand All @@ -105,7 +116,7 @@ WindowTracker.prototype = {

unload: function unload() {
windowWatcher.unregisterNotification(this);
for each (let window in windows())
for each (let window in getWindows())
this._unregWindow(window);
},

Expand Down
6 changes: 6 additions & 0 deletions lib/sdk/private-browsing/utils.js
Expand Up @@ -15,6 +15,7 @@ const events = require('../system/events');
const { deprecateFunction } = require('../util/deprecate');
const { isOneOf, is, satisfiesVersion, version } = require('../system/xul-app');
const { isWindowPrivate } = require('../window/utils');
const { isPrivateBrowsingSupported } = require('../self');

let deferredEmit = defer(emit);
let pbService;
Expand Down Expand Up @@ -51,6 +52,11 @@ let isWindowPBSupported = exports.isWindowPBSupported =
// checks that per-tab private browsing is implemented
let isTabPBSupported = exports.isTabPBSupported =
!pbService && !!PrivateBrowsingUtils && is('Fennec') && satisfiesVersion(version, '>=20.0*');

function ignoreWindow(window) {
return !isPrivateBrowsingSupported && isWindowPrivate(window);
}
exports.ignoreWindow = ignoreWindow;

function onChange() {
// Emit event with in next turn of event loop.
Expand Down
14 changes: 9 additions & 5 deletions lib/sdk/tabs/utils.js
Expand Up @@ -12,9 +12,13 @@ module.metadata = {
// NOTE: This file should only deal with xul/native tabs


const { Ci } = require('chrome');
const { defer } = require("../lang/functional");
const { windows, isBrowser } = require('../window/utils');
const { Ci } = require('chrome');
const { isPrivateBrowsingSupported } = require('../self');

// Bug 834961: ignore private windows when they are not supported
function getWindows() windows(null, { includePrivate: isPrivateBrowsingSupported });

function activateTab(tab, window) {
let gBrowser = getTabBrowserForTab(tab);
Expand Down Expand Up @@ -52,7 +56,7 @@ exports.getTabContainer = getTabContainer;
*/
function getTabs(window) {
if (arguments.length === 0) {
return windows().filter(isBrowser).reduce(function(tabs, window) {
return getWindows().filter(isBrowser).reduce(function(tabs, window) {
return tabs.concat(getTabs(window))
}, []);
}
Expand Down Expand Up @@ -83,7 +87,7 @@ exports.getOwnerWindow = getOwnerWindow;

// fennec
function getWindowHoldingTab(rawTab) {
for each (let window in windows()) {
for each (let window in getWindows()) {
// this function may be called when not using fennec,
// but BrowserApp is only defined on Fennec
if (!window.BrowserApp)
Expand Down Expand Up @@ -243,7 +247,7 @@ exports.getTabForContentWindow = getTabForContentWindow;

// used on fennec
function getTabForWindow(window) {
for each (let { BrowserApp } in windows()) {
for each (let { BrowserApp } in getWindows()) {
if (!BrowserApp)
continue;

Expand Down Expand Up @@ -290,7 +294,7 @@ exports.getSelectedTab = getSelectedTab;


function getTabForBrowser(browser) {
for each (let window in windows()) {
for each (let window in getWindows()) {
// this function may be called when not using fennec
if (!window.BrowserApp)
continue;
Expand Down
26 changes: 9 additions & 17 deletions lib/sdk/window/utils.js
Expand Up @@ -7,9 +7,8 @@ module.metadata = {
'stability': 'unstable'
};

const { Cc, Ci, Cu } = require('chrome');
const { Cc, Ci } = require('chrome');
const array = require('../util/array');
const { isPrivateBrowsingSupported } = require('../self');
const observers = require('../deprecated/observer-service');
const { defer } = require('sdk/core/promise');

Expand All @@ -25,22 +24,20 @@ const BROWSER = 'navigator:browser',
NAME = '_blank',
FEATURES = 'chrome,all,dialog=no';

let PrivateBrowsingUtils;
try {
PrivateBrowsingUtils = Cu.import('resource://gre/modules/PrivateBrowsingUtils.jsm', {}).PrivateBrowsingUtils;
}
catch(e) { /* if this file DNE then an error will be thrown */ }

function isWindowPrivate(win) {
if (!PrivateBrowsingUtils || !win)
if (!win)
return false;

// if the pbService is undefined, the PrivateBrowsingUtils.jsm is available,
// and the app is Firefox, then assume per-window private browsing is
// enabled.
if (win instanceof Ci.nsIDOMWindow) {
return PrivateBrowsingUtils.isWindowPrivate(win);
try {
return win.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsILoadContext)
.usePrivateBrowsing;
}
catch(e) {}

// Sometimes the input is not a nsIDOMWindow.. but it is still a winodw.
try {
Expand All @@ -52,11 +49,6 @@ function isWindowPrivate(win) {
}
exports.isWindowPrivate = isWindowPrivate;

function ignoreWindow(window) {
return !isPrivateBrowsingSupported && isWindowPrivate(window);
}
exports.ignoreWindow = ignoreWindow;

function getMostRecentBrowserWindow() {
return getMostRecentWindow(BROWSER);
}
Expand Down Expand Up @@ -257,7 +249,7 @@ function windows(type, options) {
while (winEnum.hasMoreElements()) {
let window = winEnum.getNext().QueryInterface(Ci.nsIDOMWindow);
// only add unprivate windows when pb is not supported
if (options.isPrivateBrowsing || !ignoreWindow(window)) {
if (options.includePrivate || !isWindowPrivate(window)) {
list.push(window);
}
}
Expand Down
4 changes: 3 additions & 1 deletion lib/sdk/windows/firefox.js
Expand Up @@ -10,7 +10,7 @@ const { Cc, Ci, Cr } = require('chrome'),
{ WindowTabs, WindowTabTracker } = require('./tabs-firefox'),
{ WindowDom } = require('./dom'),
{ WindowLoader } = require('./loader'),
{ isBrowser, getWindowDocShell, windows: windowIterator, ignoreWindow } = require('../window/utils'),
{ isBrowser, getWindowDocShell, windows: windowIterator } = require('../window/utils'),
{ Options } = require('../tabs/common'),
apiUtils = require('../deprecated/api-utils'),
unload = require('../system/unload'),
Expand All @@ -21,6 +21,7 @@ const { Cc, Ci, Cr } = require('chrome'),
{ getOwnerWindow } = require('../private-browsing/window/utils'),
viewNS = require('../core/namespace').ns(),
{ isPrivateBrowsingSupported } = require('../self');
const { ignoreWindow } = require('sdk/private-browsing/utils');

/**
* Window trait composes safe wrappers for browser window that are E10S
Expand Down Expand Up @@ -207,6 +208,7 @@ const browserWindows = Trait.resolve({ toString: null }).compose(
*/
get activeWindow() {
let window = windowUtils.activeBrowserWindow;
// Bug 834961: ignore private windows when they are not supported
if (ignoreWindow(window))
window = windowIterator()[0];
return window ? BrowserWindow({window: window}) : null;
Expand Down
2 changes: 1 addition & 1 deletion test/windows/test-firefox-windows.js
Expand Up @@ -401,7 +401,7 @@ exports.testWindowIteratorPrivateDefault = function(test) {
test.assertEqual(browserWindows.length, 2, 'two windows open');
test.assertEqual(windows().length, 2);
}
test.assertEqual(windows(null, { isPrivateBrowsing: true }).length, 2);
test.assertEqual(windows(null, { includePrivate: true }).length, 2);

for each(let window in browserWindows) {
// test that all windows in iterator are not private
Expand Down

0 comments on commit 7dfc495

Please sign in to comment.