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 | |
michael@0 | 5 | 'use strict'; |
michael@0 | 6 | |
michael@0 | 7 | const { platform, pathFor } = require('sdk/system'); |
michael@0 | 8 | const { defer } = require('sdk/core/promise'); |
michael@0 | 9 | const { emit } = require('sdk/event/core'); |
michael@0 | 10 | const { join } = require('sdk/fs/path'); |
michael@0 | 11 | const { writeFile, unlinkSync, existsSync } = require('sdk/io/fs'); |
michael@0 | 12 | const PROFILE_DIR= pathFor('ProfD'); |
michael@0 | 13 | const isWindows = platform.toLowerCase().indexOf('win') === 0; |
michael@0 | 14 | const isOSX = platform.toLowerCase().indexOf('darwin') === 0; |
michael@0 | 15 | |
michael@0 | 16 | let scripts = { |
michael@0 | 17 | 'args.sh': 'echo $1 $2 $3 $4', |
michael@0 | 18 | 'args.bat': 'echo %1 %2 %3 %4', |
michael@0 | 19 | 'check-env.sh': 'echo $CHILD_PROCESS_ENV_TEST', |
michael@0 | 20 | 'check-env.bat': 'echo %CHILD_PROCESS_ENV_TEST%', |
michael@0 | 21 | 'check-pwd.sh': 'echo $PWD', |
michael@0 | 22 | 'check-pwd.bat': 'cd', |
michael@0 | 23 | 'large-err.sh': 'for n in `seq 0 $1` ; do echo "E" 1>&2; done', |
michael@0 | 24 | 'large-err-mac.sh': 'for ((i=0; i<$1; i=i+1)); do echo "E" 1>&2; done', |
michael@0 | 25 | 'large-err.bat': 'FOR /l %%i in (0,1,%1) DO echo "E" 1>&2', |
michael@0 | 26 | 'large-out.sh': 'for n in `seq 0 $1` ; do echo "O"; done', |
michael@0 | 27 | 'large-out-mac.sh': 'for ((i=0; i<$1; i=i+1)); do echo "O"; done', |
michael@0 | 28 | 'large-out.bat': 'FOR /l %%i in (0,1,%1) DO echo "O"', |
michael@0 | 29 | 'wait.sh': 'sleep 2', |
michael@0 | 30 | // Use `ping` to an invalid IP address because `timeout` isn't |
michael@0 | 31 | // on all environments? http://stackoverflow.com/a/1672349/1785755 |
michael@0 | 32 | 'wait.bat': 'ping 1.1.1.1 -n 1 -w 2000 > nul' |
michael@0 | 33 | }; |
michael@0 | 34 | |
michael@0 | 35 | Object.keys(scripts).forEach(filename => { |
michael@0 | 36 | if (/\.sh$/.test(filename)) |
michael@0 | 37 | scripts[filename] = '#!/bin/sh\n' + scripts[filename]; |
michael@0 | 38 | else if (/\.bat$/.test(filename)) |
michael@0 | 39 | scripts[filename] = '@echo off\n' + scripts[filename]; |
michael@0 | 40 | }); |
michael@0 | 41 | |
michael@0 | 42 | function getScript (name) { |
michael@0 | 43 | // Use specific OSX script if exists |
michael@0 | 44 | if (isOSX && scripts[name + '-mac.sh']) |
michael@0 | 45 | name = name + '-mac'; |
michael@0 | 46 | let fileName = name + (isWindows ? '.bat' : '.sh'); |
michael@0 | 47 | return createFile(fileName, scripts[fileName]); |
michael@0 | 48 | } |
michael@0 | 49 | exports.getScript = getScript; |
michael@0 | 50 | |
michael@0 | 51 | function createFile (name, data) { |
michael@0 | 52 | let { promise, resolve, reject } = defer(); |
michael@0 | 53 | let fileName = join(PROFILE_DIR, name); |
michael@0 | 54 | writeFile(fileName, data, function (err) { |
michael@0 | 55 | if (err) reject(); |
michael@0 | 56 | else { |
michael@0 | 57 | makeExecutable(fileName); |
michael@0 | 58 | resolve(fileName); |
michael@0 | 59 | } |
michael@0 | 60 | }); |
michael@0 | 61 | return promise; |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | // TODO Use fs.chmod once implemented, bug 914606 |
michael@0 | 65 | function makeExecutable (name) { |
michael@0 | 66 | let { CC } = require('chrome'); |
michael@0 | 67 | let nsILocalFile = CC('@mozilla.org/file/local;1', 'nsILocalFile', 'initWithPath'); |
michael@0 | 68 | let file = nsILocalFile(name); |
michael@0 | 69 | file.permissions = parseInt('0777', 8); |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | function deleteFile (name) { |
michael@0 | 73 | let file = join(PROFILE_DIR, name); |
michael@0 | 74 | if (existsSync(file)) |
michael@0 | 75 | unlinkSync(file); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | function cleanUp () { |
michael@0 | 79 | Object.keys(scripts).forEach(deleteFile); |
michael@0 | 80 | } |
michael@0 | 81 | exports.cleanUp = cleanUp; |