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 { Loader } = require('sdk/test/loader'); |
michael@0 | 7 | |
michael@0 | 8 | const { loader } = LoaderWithHookedConsole(module); |
michael@0 | 9 | |
michael@0 | 10 | const pb = loader.require('sdk/private-browsing'); |
michael@0 | 11 | const pbUtils = loader.require('sdk/private-browsing/utils'); |
michael@0 | 12 | const xulApp = require("sdk/system/xul-app"); |
michael@0 | 13 | const { open: openWindow, getMostRecentBrowserWindow } = require('sdk/window/utils'); |
michael@0 | 14 | const { openTab, getTabContentWindow, getActiveTab, setTabURL, closeTab } = require('sdk/tabs/utils'); |
michael@0 | 15 | const promise = require("sdk/core/promise"); |
michael@0 | 16 | const windowHelpers = require('sdk/window/helpers'); |
michael@0 | 17 | const events = require("sdk/system/events"); |
michael@0 | 18 | |
michael@0 | 19 | function LoaderWithHookedConsole(module) { |
michael@0 | 20 | let globals = {}; |
michael@0 | 21 | let errors = []; |
michael@0 | 22 | |
michael@0 | 23 | globals.console = Object.create(console, { |
michael@0 | 24 | error: { |
michael@0 | 25 | value: function(e) { |
michael@0 | 26 | errors.push(e); |
michael@0 | 27 | if (!/DEPRECATED:/.test(e)) { |
michael@0 | 28 | console.error(e); |
michael@0 | 29 | } |
michael@0 | 30 | } |
michael@0 | 31 | } |
michael@0 | 32 | }); |
michael@0 | 33 | |
michael@0 | 34 | let loader = Loader(module, globals); |
michael@0 | 35 | |
michael@0 | 36 | return { |
michael@0 | 37 | loader: loader, |
michael@0 | 38 | errors: errors |
michael@0 | 39 | } |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | function deactivate(callback) { |
michael@0 | 43 | if (pbUtils.isGlobalPBSupported) { |
michael@0 | 44 | if (callback) |
michael@0 | 45 | pb.once('stop', callback); |
michael@0 | 46 | pb.deactivate(); |
michael@0 | 47 | } |
michael@0 | 48 | } |
michael@0 | 49 | exports.deactivate = deactivate; |
michael@0 | 50 | |
michael@0 | 51 | exports.pb = pb; |
michael@0 | 52 | exports.pbUtils = pbUtils; |
michael@0 | 53 | exports.LoaderWithHookedConsole = LoaderWithHookedConsole; |
michael@0 | 54 | |
michael@0 | 55 | exports.openWebpage = function openWebpage(url, enablePrivate) { |
michael@0 | 56 | if (xulApp.is("Fennec")) { |
michael@0 | 57 | let chromeWindow = getMostRecentBrowserWindow(); |
michael@0 | 58 | let rawTab = openTab(chromeWindow, url, { |
michael@0 | 59 | isPrivate: enablePrivate |
michael@0 | 60 | }); |
michael@0 | 61 | |
michael@0 | 62 | return { |
michael@0 | 63 | ready: promise.resolve(getTabContentWindow(rawTab)), |
michael@0 | 64 | close: function () { |
michael@0 | 65 | closeTab(rawTab); |
michael@0 | 66 | // Returns a resolved promise as there is no need to wait |
michael@0 | 67 | return promise.resolve(); |
michael@0 | 68 | } |
michael@0 | 69 | }; |
michael@0 | 70 | } |
michael@0 | 71 | else { |
michael@0 | 72 | let win = openWindow(null, { |
michael@0 | 73 | features: { |
michael@0 | 74 | private: enablePrivate |
michael@0 | 75 | } |
michael@0 | 76 | }); |
michael@0 | 77 | let deferred = promise.defer(); |
michael@0 | 78 | |
michael@0 | 79 | // Wait for delayed startup code to be executed, in order to ensure |
michael@0 | 80 | // that the window is really ready |
michael@0 | 81 | events.on("browser-delayed-startup-finished", function onReady({subject}) { |
michael@0 | 82 | if (subject == win) { |
michael@0 | 83 | events.off("browser-delayed-startup-finished", onReady); |
michael@0 | 84 | deferred.resolve(win); |
michael@0 | 85 | |
michael@0 | 86 | let rawTab = getActiveTab(win); |
michael@0 | 87 | setTabURL(rawTab, url); |
michael@0 | 88 | deferred.resolve(getTabContentWindow(rawTab)); |
michael@0 | 89 | } |
michael@0 | 90 | }, true); |
michael@0 | 91 | |
michael@0 | 92 | return { |
michael@0 | 93 | ready: deferred.promise, |
michael@0 | 94 | close: function () { |
michael@0 | 95 | return windowHelpers.close(win); |
michael@0 | 96 | } |
michael@0 | 97 | }; |
michael@0 | 98 | } |
michael@0 | 99 | return null; |
michael@0 | 100 | } |