Add explanations for browser environment variables
[tails.git] / config / chroot_local-includes / usr / local / lib / tails-shell-library / tor-browser.sh
blob6e2965da660a99fa4c13fb0209353305ff05a408
1 #!/bin/sh
3 TBB_INSTALL=/usr/local/lib/tor-browser
4 # shellcheck disable=SC2034
5 TBB_PROFILE=/etc/tor-browser/profile
6 # shellcheck disable=SC2034
7 TBB_EXT=/usr/local/share/tor-browser-extensions
9 # For strings it's up to the caller to add double-quotes ("") around
10 # the value.
11 set_mozilla_pref() {
12 local file name value prefix
13 file="${1}"
14 name="${2}"
15 value="${3}"
16 # Sometimes we might want to do e.g. user_pref
17 prefix="${4:-pref}"
18 [ -e "${file}" ] && sed -i "/^${prefix}(\"${name}\",/d" "${file}"
19 echo "${prefix}(\"${name}\", ${value});" >> "${file}"
22 exec_firefox_helper() {
23 local binary="${1}"; shift
25 export LD_LIBRARY_PATH="${TBB_INSTALL}"
26 export GNOME_ACCESSIBILITY=1
28 # Don't let Tor Browser manage the tor daemon: we do it ourselves.
29 export TOR_SKIP_LAUNCH=1
31 # The Tor Browser often assumes that the current directory is
32 # where the browser lives, e.g. for the fixed set of fonts set by
33 # fontconfig above.
34 cd "${TBB_INSTALL}"
36 # Environment stuff from the upstream start-browser script:
38 # Do not (try to) connect to the session manager. Otherwise it
39 # does (and fails) but if it succeeded it would be "very bad",
40 # although details on why are missing.
41 # https://gitlab.torproject.org/legacy/trac/-/issues/5261
42 unset SESSION_MANAGER
44 # Enable bundled fonts to decrease fingerprint.
45 # https://gitlab.torproject.org/tpo/applications/tor-browser/-/issues/13313
46 export FONTCONFIG_PATH="${TBB_INSTALL}/fontconfig"
47 export FONTCONFIG_FILE="fonts.conf"
49 # Avoid overwriting user's dconf values.
50 # https://gitlab.torproject.org/legacy/trac/-/issues/27903
51 export GSETTINGS_BACKEND=memory
53 exec "${TBB_INSTALL}"/"${binary}" "${@}"
56 exec_firefox() {
57 exec_firefox_helper firefox.real "${@}"
60 guess_best_tor_browser_locale() {
61 local long_locale short_locale similar_locale
62 long_locale="$(echo "${LANG}" | sed -e 's/\..*$//' -e 's/_/-/')"
63 short_locale="$(echo "${long_locale}" | cut -d"-" -f1)"
64 if locale_is_supported_by_tor_browser "$long_locale"; then
65 echo "${long_locale}"
66 return
67 elif locale_is_supported_by_tor_browser "$short_locale"; then
68 echo "${short_locale}"
69 return
71 # If we use locale xx-YY and Tor Browser supports neither xx-YY nor xx,
72 # there may be a similar locale xx-ZZ that we should use instead.
73 # shellcheck disable=SC2012
74 similar_locale=$(
75 supported_tor_browser_locales | \
76 grep --max-count=1 --extended-regexp --line-regexp \
77 "${short_locale}-[A-Z]+" \
78 ) || :
79 if [ -n "${similar_locale:-}" ]; then
80 echo "${similar_locale}"
81 return
84 echo 'en-US'
87 configure_best_tor_browser_locale() {
88 local profile best_locale
89 profile="${1}"
90 best_locale="$(guess_best_tor_browser_locale)"
91 cat "/etc/tor-browser/locale-profiles/${best_locale}.js" \
92 >> "${profile}/prefs.js"
95 supported_tor_browser_locales() {
96 tmp=$(mktemp -d)
97 7z e -tzip -o"$tmp" -- "${TBB_INSTALL}/omni.ja" res/multilocale.txt >/dev/null
98 tr ',' "\n" < "$tmp"/multilocale.txt
99 rm -rf "$tmp"
102 locale_is_supported_by_tor_browser() {
103 local mozilla_locale
104 mozilla_locale="${1}"
106 supported_tor_browser_locales \
107 | grep --quiet --fixed-strings --line-regexp "$mozilla_locale"
110 set_firefox_content_process_count() {
111 local profile="$1"
112 local count="$2"
114 set_mozilla_pref "${profile}/prefs.js" \
115 "dom.ipc.processCount" "$count" \
116 user_pref
119 configure_tor_browser_memory_usage() {
120 local profile="${1}"
122 # Unit: KiB
123 system_ram=$(awk '/^MemTotal:/ { print $2 }' /proc/meminfo)
125 if [ "$system_ram" -lt "$((3 * 1024 * 1024))" ]; then
126 set_firefox_content_process_count "$profile" 2