tor-browser.sh 4.46 KB
Newer Older
1 2 3 4
#!/bin/sh

TBB_INSTALL=/usr/local/lib/tor-browser
TBB_PROFILE=/etc/tor-browser/profile
5
TBB_EXT=/usr/local/share/tor-browser-extensions
6 7
TOR_LAUNCHER_INSTALL=/usr/local/lib/tor-launcher-standalone
TOR_LAUNCHER_LOCALES_DIR="${TOR_LAUNCHER_INSTALL}/chrome/locale"
8

anonym's avatar
anonym committed
9 10 11 12 13 14 15 16 17
# For strings it's up to the caller to add double-quotes ("") around
# the value.
set_mozilla_pref() {
    local file name value prefix
    file="${1}"
    name="${2}"
    value="${3}"
    # Sometimes we might want to do e.g. user_pref
    prefix="${4:-pref}"
18
    [ -e "${file}" ] && sed -i "/^${prefix}(\"${name}\",/d" "${file}"
anonym's avatar
anonym committed
19 20 21
    echo "${prefix}(\"${name}\", ${value});" >> "${file}"
}

22 23 24 25 26 27
exec_firefox_helper() {
    local binary="${1}"; shift

    export LD_LIBRARY_PATH="${TBB_INSTALL}"
    export FONTCONFIG_PATH="${TBB_INSTALL}/TorBrowser/Data/fontconfig"
    export FONTCONFIG_FILE="fonts.conf"
28
    export GNOME_ACCESSIBILITY=1
29

30 31 32 33 34 35 36
    # Since Tor Browser 9.0 it has become integrated into the browser,
    # so let's make it the responsibility of callers to explicitly set
    # this variable to 0 if they want to enable Tor Launcher.
    if [ -z "${TOR_SKIP_LAUNCH:-}" ]; then
        export TOR_SKIP_LAUNCH=1
    fi

37 38 39
    # New in 9.5: Avoid overwriting user's dconf values. Fixes #27903.
    export GSETTINGS_BACKEND=memory

40 41 42 43 44 45 46 47 48 49 50
    # The Tor Browser often assumes that the current directory is
    # where the browser lives, e.g. for the fixed set of fonts set by
    # fontconfig above.
    cd "${TBB_INSTALL}"

    # From start-tor-browser:
    unset SESSION_MANAGER

    exec "${TBB_INSTALL}"/"${binary}" "${@}"
}

51
exec_firefox() {
52
    exec_firefox_helper firefox.real "${@}"
53
}
54

55
exec_unconfined_firefox() {
56
    exec_firefox_helper firefox-unconfined "${@}"
57 58
}

59
guess_best_tor_browser_locale() {
60
    local long_locale short_locale similar_locale
61 62 63
    long_locale="$(echo ${LANG} | sed -e 's/\..*$//' -e 's/_/-/')"
    short_locale="$(echo ${long_locale} | cut -d"-" -f1)"
    if [ -e "${TBB_EXT}/langpack-${long_locale}@firefox.mozilla.org.xpi" ]; then
64
        echo "${long_locale}"
65 66
        return
    elif [ -e "${TBB_EXT}/langpack-${short_locale}@firefox.mozilla.org.xpi" ]; then
67
        echo "${short_locale}"
68
        return
69
    fi
70 71 72 73 74
    # If we use locale xx-YY and there is no langpack for xx-YY nor xx
    # there may be a similar locale xx-ZZ that we should use instead.
    similar_locale="$(ls -1 "${TBB_EXT}" | \
        sed -n "s,^langpack-\(${short_locale}-[A-Z]\+\)@firefox.mozilla.org.xpi$,\1,p" | \
        head -n 1)" || :
75
    if [ -n "${similar_locale:-}" ]; then
76
        echo "${similar_locale}"
77 78 79
        return
    fi

80
    echo 'en-US'
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
}

guess_best_tor_launcher_locale() {
    local long_locale short_locale
    long_locale="$(echo ${LANG} | sed -e 's/\..*$//' -e 's/_/-/')"
    short_locale="$(echo ${long_locale} | cut -d"-" -f1)"
    if [ -e "${TOR_LAUNCHER_LOCALES_DIR}/${long_locale}" ]; then
        echo ${long_locale}
    elif ls -1 "${TOR_LAUNCHER_LOCALES_DIR}" | grep -q "^${short_locale}\(-[A-Z]\+\)\?$"; then
        # See comment in guess_best_firefox_locale()
        echo ${short_locale}
    else
        echo en-US
    fi
}

configure_xulrunner_app_locale() {
    local profile locale
    profile="${1}"
    locale="${2}"
    mkdir -p "${profile}"/preferences
102 103 104
    set_mozilla_pref "${profile}"/prefs.js \
                     "intl.locale.requested" "\"${locale}\"" \
                     "user_pref"
105 106 107
}

configure_best_tor_browser_locale() {
108 109 110 111 112
    local profile best_locale
    profile="${1}"
    best_locale="$(guess_best_tor_browser_locale)"
    configure_xulrunner_app_locale "${profile}" "${best_locale}"
    cat "/etc/tor-browser/locale-profiles/${best_locale}.js" \
113
        >> "${profile}/prefs.js"
114 115 116 117 118
}

configure_best_tor_launcher_locale() {
    configure_xulrunner_app_locale "${1}" "$(guess_best_tor_launcher_locale)"
}
anonym's avatar
anonym committed
119 120 121 122 123 124 125 126

supported_tor_browser_locales() {
    # The default is always supported
    echo en-US
    for langpack in "${TBB_EXT}"/langpack-*@firefox.mozilla.org.xpi; do
        basename "${langpack}" | sed 's,^langpack-\([^@]\+\)@.*$,\1,'
    done
}
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

set_firefox_content_process_count() {
    local profile="$1"
    local count="$2"

        set_mozilla_pref "${profile}/prefs.js" \
                         "dom.ipc.processCount" "$count" \
                         user_pref
}

configure_tor_browser_memory_usage() {
    local profile="${1}"

    # Unit: KiB
    system_ram=$(awk '/^MemTotal:/ { print $2 }' /proc/meminfo)

    if [ "$system_ram" -lt "$((3 * 1024 * 1024))" ]; then
        set_firefox_content_process_count "$profile" 2
    fi
}