Thu, 15 Jan 2015 15:59:08 +0100
Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
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 } = require('chrome'); |
michael@0 | 7 | const { isPrivate } = require('sdk/private-browsing'); |
michael@0 | 8 | const { isWindowPBSupported } = require('sdk/private-browsing/utils'); |
michael@0 | 9 | const { onFocus, getMostRecentWindow, getWindowTitle, getInnerId, |
michael@0 | 10 | getFrames, windows, open: openWindow, isWindowPrivate } = require('sdk/window/utils'); |
michael@0 | 11 | const { open, close, focus, promise } = require('sdk/window/helpers'); |
michael@0 | 12 | const { browserWindows } = require("sdk/windows"); |
michael@0 | 13 | const winUtils = require("sdk/deprecated/window-utils"); |
michael@0 | 14 | const { fromIterator: toArray } = require('sdk/util/array'); |
michael@0 | 15 | const tabs = require('sdk/tabs'); |
michael@0 | 16 | |
michael@0 | 17 | const WM = Cc['@mozilla.org/appshell/window-mediator;1'].getService(Ci.nsIWindowMediator); |
michael@0 | 18 | |
michael@0 | 19 | const BROWSER = 'chrome://browser/content/browser.xul'; |
michael@0 | 20 | |
michael@0 | 21 | function makeEmptyBrowserWindow(options) { |
michael@0 | 22 | options = options || {}; |
michael@0 | 23 | return open(BROWSER, { |
michael@0 | 24 | features: { |
michael@0 | 25 | chrome: true, |
michael@0 | 26 | private: !!options.private |
michael@0 | 27 | } |
michael@0 | 28 | }).then(focus); |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | exports.testWindowTrackerIgnoresPrivateWindows = function(assert, done) { |
michael@0 | 32 | var myNonPrivateWindowId, myPrivateWindowId; |
michael@0 | 33 | var privateWindowClosed = false; |
michael@0 | 34 | var privateWindowOpened = false; |
michael@0 | 35 | var trackedWindowIds = []; |
michael@0 | 36 | |
michael@0 | 37 | let wt = winUtils.WindowTracker({ |
michael@0 | 38 | onTrack: function(window) { |
michael@0 | 39 | let id = getInnerId(window); |
michael@0 | 40 | trackedWindowIds.push(id); |
michael@0 | 41 | }, |
michael@0 | 42 | onUntrack: function(window) { |
michael@0 | 43 | let id = getInnerId(window); |
michael@0 | 44 | if (id === myPrivateWindowId) { |
michael@0 | 45 | privateWindowClosed = true; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | if (id === myNonPrivateWindowId) { |
michael@0 | 49 | assert.equal(privateWindowClosed, true, 'private window was untracked'); |
michael@0 | 50 | wt.unload(); |
michael@0 | 51 | done(); |
michael@0 | 52 | } |
michael@0 | 53 | } |
michael@0 | 54 | }); |
michael@0 | 55 | |
michael@0 | 56 | // make a new private window |
michael@0 | 57 | makeEmptyBrowserWindow({ private: true }).then(function(window) { |
michael@0 | 58 | myPrivateWindowId = getInnerId(window); |
michael@0 | 59 | |
michael@0 | 60 | assert.ok(trackedWindowIds.indexOf(myPrivateWindowId) >= 0, 'private window was tracked'); |
michael@0 | 61 | assert.equal(isPrivate(window), isWindowPBSupported, 'private window isPrivate'); |
michael@0 | 62 | assert.equal(isWindowPrivate(window), isWindowPBSupported); |
michael@0 | 63 | assert.ok(getFrames(window).length > 1, 'there are frames for private window'); |
michael@0 | 64 | assert.equal(getWindowTitle(window), window.document.title, |
michael@0 | 65 | 'getWindowTitle works'); |
michael@0 | 66 | |
michael@0 | 67 | return close(window).then(function() { |
michael@0 | 68 | assert.pass('private window was closed'); |
michael@0 | 69 | |
michael@0 | 70 | return makeEmptyBrowserWindow().then(function(window) { |
michael@0 | 71 | myNonPrivateWindowId = getInnerId(window); |
michael@0 | 72 | assert.notEqual(myPrivateWindowId, myNonPrivateWindowId, 'non private window was opened'); |
michael@0 | 73 | return close(window); |
michael@0 | 74 | }); |
michael@0 | 75 | }); |
michael@0 | 76 | }).then(null, assert.fail); |
michael@0 | 77 | }; |
michael@0 | 78 | |
michael@0 | 79 | // Test setting activeWIndow and onFocus for private windows |
michael@0 | 80 | exports.testSettingActiveWindowDoesNotIgnorePrivateWindow = function(assert, done) { |
michael@0 | 81 | let browserWindow = WM.getMostRecentWindow("navigator:browser"); |
michael@0 | 82 | let testSteps; |
michael@0 | 83 | |
michael@0 | 84 | assert.equal(winUtils.activeBrowserWindow, browserWindow, |
michael@0 | 85 | "Browser window is the active browser window."); |
michael@0 | 86 | assert.ok(!isPrivate(browserWindow), "Browser window is not private."); |
michael@0 | 87 | |
michael@0 | 88 | // make a new private window |
michael@0 | 89 | makeEmptyBrowserWindow({ |
michael@0 | 90 | private: true |
michael@0 | 91 | }).then(function(window) { |
michael@0 | 92 | let continueAfterFocus = function(window) onFocus(window).then(nextTest); |
michael@0 | 93 | |
michael@0 | 94 | // PWPB case |
michael@0 | 95 | if (isWindowPBSupported) { |
michael@0 | 96 | assert.ok(isPrivate(window), "window is private"); |
michael@0 | 97 | assert.notDeepEqual(winUtils.activeBrowserWindow, browserWindow); |
michael@0 | 98 | } |
michael@0 | 99 | // Global case |
michael@0 | 100 | else { |
michael@0 | 101 | assert.ok(!isPrivate(window), "window is not private"); |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | assert.strictEqual(winUtils.activeBrowserWindow, window, |
michael@0 | 105 | "Correct active browser window pb supported"); |
michael@0 | 106 | assert.notStrictEqual(browserWindow, window, |
michael@0 | 107 | "The window is not the old browser window"); |
michael@0 | 108 | |
michael@0 | 109 | testSteps = [ |
michael@0 | 110 | function() { |
michael@0 | 111 | // test setting a non private window |
michael@0 | 112 | continueAfterFocus(winUtils.activeWindow = browserWindow); |
michael@0 | 113 | }, |
michael@0 | 114 | function() { |
michael@0 | 115 | assert.strictEqual(winUtils.activeWindow, browserWindow, |
michael@0 | 116 | "Correct active window [1]"); |
michael@0 | 117 | assert.strictEqual(winUtils.activeBrowserWindow, browserWindow, |
michael@0 | 118 | "Correct active browser window [1]"); |
michael@0 | 119 | |
michael@0 | 120 | // test focus(window) |
michael@0 | 121 | focus(window).then(nextTest); |
michael@0 | 122 | }, |
michael@0 | 123 | function(w) { |
michael@0 | 124 | assert.strictEqual(w, window, 'require("sdk/window/helpers").focus on window works'); |
michael@0 | 125 | assert.strictEqual(winUtils.activeBrowserWindow, window, |
michael@0 | 126 | "Correct active browser window [2]"); |
michael@0 | 127 | assert.strictEqual(winUtils.activeWindow, window, |
michael@0 | 128 | "Correct active window [2]"); |
michael@0 | 129 | |
michael@0 | 130 | // test setting a private window |
michael@0 | 131 | continueAfterFocus(winUtils.activeWindow = window); |
michael@0 | 132 | }, |
michael@0 | 133 | function() { |
michael@0 | 134 | assert.deepEqual(winUtils.activeBrowserWindow, window, |
michael@0 | 135 | "Correct active browser window [3]"); |
michael@0 | 136 | assert.deepEqual(winUtils.activeWindow, window, |
michael@0 | 137 | "Correct active window [3]"); |
michael@0 | 138 | |
michael@0 | 139 | // just to get back to original state |
michael@0 | 140 | continueAfterFocus(winUtils.activeWindow = browserWindow); |
michael@0 | 141 | }, |
michael@0 | 142 | function() { |
michael@0 | 143 | assert.deepEqual(winUtils.activeBrowserWindow, browserWindow, |
michael@0 | 144 | "Correct active browser window when pb mode is supported [4]"); |
michael@0 | 145 | assert.deepEqual(winUtils.activeWindow, browserWindow, |
michael@0 | 146 | "Correct active window when pb mode is supported [4]"); |
michael@0 | 147 | |
michael@0 | 148 | close(window).then(done).then(null, assert.fail); |
michael@0 | 149 | } |
michael@0 | 150 | ]; |
michael@0 | 151 | |
michael@0 | 152 | function nextTest() { |
michael@0 | 153 | let args = arguments; |
michael@0 | 154 | if (testSteps.length) { |
michael@0 | 155 | require('sdk/timers').setTimeout(function() { |
michael@0 | 156 | (testSteps.shift()).apply(null, args); |
michael@0 | 157 | }, 0); |
michael@0 | 158 | } |
michael@0 | 159 | } |
michael@0 | 160 | nextTest(); |
michael@0 | 161 | }); |
michael@0 | 162 | }; |
michael@0 | 163 | |
michael@0 | 164 | exports.testActiveWindowDoesNotIgnorePrivateWindow = function(assert, done) { |
michael@0 | 165 | // make a new private window |
michael@0 | 166 | makeEmptyBrowserWindow({ |
michael@0 | 167 | private: true |
michael@0 | 168 | }).then(function(window) { |
michael@0 | 169 | // PWPB case |
michael@0 | 170 | if (isWindowPBSupported) { |
michael@0 | 171 | assert.equal(isPrivate(winUtils.activeWindow), true, |
michael@0 | 172 | "active window is private"); |
michael@0 | 173 | assert.equal(isPrivate(winUtils.activeBrowserWindow), true, |
michael@0 | 174 | "active browser window is private"); |
michael@0 | 175 | assert.ok(isWindowPrivate(window), "window is private"); |
michael@0 | 176 | assert.ok(isPrivate(window), "window is private"); |
michael@0 | 177 | |
michael@0 | 178 | // pb mode is supported |
michael@0 | 179 | assert.ok( |
michael@0 | 180 | isWindowPrivate(winUtils.activeWindow), |
michael@0 | 181 | "active window is private when pb mode is supported"); |
michael@0 | 182 | assert.ok( |
michael@0 | 183 | isWindowPrivate(winUtils.activeBrowserWindow), |
michael@0 | 184 | "active browser window is private when pb mode is supported"); |
michael@0 | 185 | assert.ok(isPrivate(winUtils.activeWindow), |
michael@0 | 186 | "active window is private when pb mode is supported"); |
michael@0 | 187 | assert.ok(isPrivate(winUtils.activeBrowserWindow), |
michael@0 | 188 | "active browser window is private when pb mode is supported"); |
michael@0 | 189 | } |
michael@0 | 190 | // Global case |
michael@0 | 191 | else { |
michael@0 | 192 | assert.equal(isPrivate(winUtils.activeWindow), false, |
michael@0 | 193 | "active window is not private"); |
michael@0 | 194 | assert.equal(isPrivate(winUtils.activeBrowserWindow), false, |
michael@0 | 195 | "active browser window is not private"); |
michael@0 | 196 | assert.equal(isWindowPrivate(window), false, "window is not private"); |
michael@0 | 197 | assert.equal(isPrivate(window), false, "window is not private"); |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | return close(window); |
michael@0 | 201 | }).then(done).then(null, assert.fail); |
michael@0 | 202 | } |
michael@0 | 203 | |
michael@0 | 204 | exports.testWindowIteratorIgnoresPrivateWindows = function(assert, done) { |
michael@0 | 205 | // make a new private window |
michael@0 | 206 | makeEmptyBrowserWindow({ |
michael@0 | 207 | private: true |
michael@0 | 208 | }).then(function(window) { |
michael@0 | 209 | assert.equal(isWindowPrivate(window), isWindowPBSupported); |
michael@0 | 210 | assert.ok(toArray(winUtils.windowIterator()).indexOf(window) > -1, |
michael@0 | 211 | "window is in windowIterator()"); |
michael@0 | 212 | |
michael@0 | 213 | return close(window); |
michael@0 | 214 | }).then(done).then(null, assert.fail); |
michael@0 | 215 | }; |
michael@0 | 216 | |
michael@0 | 217 | // test that it is not possible to find a private window in |
michael@0 | 218 | // windows module's iterator |
michael@0 | 219 | exports.testWindowIteratorPrivateDefault = function(assert, done) { |
michael@0 | 220 | // there should only be one window open here, if not give us the |
michael@0 | 221 | // the urls |
michael@0 | 222 | if (browserWindows.length > 1) { |
michael@0 | 223 | for each (let tab in tabs) { |
michael@0 | 224 | assert.fail("TAB URL: " + tab.url); |
michael@0 | 225 | } |
michael@0 | 226 | } |
michael@0 | 227 | else { |
michael@0 | 228 | assert.equal(browserWindows.length, 1, 'only one window open'); |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | open('chrome://browser/content/browser.xul', { |
michael@0 | 232 | features: { |
michael@0 | 233 | private: true, |
michael@0 | 234 | chrome: true |
michael@0 | 235 | } |
michael@0 | 236 | }).then(focus).then(function(window) { |
michael@0 | 237 | // test that there is a private window opened |
michael@0 | 238 | assert.equal(isPrivate(window), isWindowPBSupported, 'there is a private window open'); |
michael@0 | 239 | assert.equal(isPrivate(winUtils.activeWindow), isWindowPBSupported); |
michael@0 | 240 | assert.equal(isPrivate(getMostRecentWindow()), isWindowPBSupported); |
michael@0 | 241 | assert.equal(isPrivate(browserWindows.activeWindow), isWindowPBSupported); |
michael@0 | 242 | |
michael@0 | 243 | assert.equal(browserWindows.length, 2, '2 windows open'); |
michael@0 | 244 | assert.equal(windows(null, { includePrivate: true }).length, 2); |
michael@0 | 245 | |
michael@0 | 246 | return close(window); |
michael@0 | 247 | }).then(done).then(null, assert.fail); |
michael@0 | 248 | }; |