[tbb-commits] [Git][tpo/applications/tor-browser][tor-browser-102.7.0esr-12.5-1] 7 commits: Revert "Bug 13252: Do not store data in the app bundle"

Pier Angelo Vendrame (@pierov) git at gitlab.torproject.org
Wed Feb 8 11:23:32 UTC 2023



Pier Angelo Vendrame pushed to branch tor-browser-102.7.0esr-12.5-1 at The Tor Project / Applications / Tor Browser


Commits:
fbc46b57 by Pier Angelo Vendrame at 2023-02-08T12:22:33+01:00
Revert "Bug 13252: Do not store data in the app bundle"

This reverts commit bcbfa12f147da775edd5adda3bf51436bdd94f10.

- - - - -
8029d2b1 by Pier Angelo Vendrame at 2023-02-08T12:22:34+01:00
fixup! Bug 14631: Improve profile access error messages.

Part of Bug 20497.

Move this check to Bug 14631, which seems a better place for it!

- - - - -
1dafa91c by Pier Angelo Vendrame at 2023-02-08T12:22:34+01:00
Revert "Bug 9173: Change the default Firefox profile directory to be relative."

This reverts commit 0ef716110c63ffa26a2c9b29cd954f2c971b5178.

- - - - -
4595d824 by Pier Angelo Vendrame at 2023-02-08T12:22:35+01:00
Bug 9173: Change the default Firefox profile directory to be relative.

This commit makes Firefox look for the default profile directory in a
directory relative to the binary path.
The directory can be specified through the --with-relative-data-dir.
This is relative to the same directory as the firefox main binary for
Linux and Windows.

On macOS, we remove Contents/MacOS from it.
Or, in other words, the directory is relative to the application
bundle.

This behavior can be overriden at runtime, by placing a file called
system-install adjacent to the firefox main binary (also on macOS).

- - - - -
cb3d5136 by Pier Angelo Vendrame at 2023-02-08T12:22:36+01:00
fixup! TB3: Tor Browser's official .mozconfigs.

Part of Bug 20497.

Start using the correct macOS relative data directory, instead of
creating it with code, when possible.

- - - - -
7e272b9d by Pier Angelo Vendrame at 2023-02-08T12:22:36+01:00
Bug 13252: Customize profile management on macOS

On macOS we allow both portable mode and system installation.
However, in the latter case, we customize Firefox's directories to
match the hierarchy we use for the portable mode.

Also, display an informative error message if the TorBrowser-Data
directory cannot be created due to an "access denied" or a
"read only volume" error.

- - - - -
f210d8ca by Pier Angelo Vendrame at 2023-02-08T12:22:37+01:00
fixup! Bug 4234: Use the Firefox Update Process for Tor Browser.

Part of Bug 20497.

Do not use GetAppRootdir. We only need up to .app, so we can query the
path to the firefox executable with the methods Firefox already has.

- - - - -


8 changed files:

- mozconfig-macos-x86_64
- toolkit/xre/nsAppRunner.cpp
- toolkit/xre/nsXREDirProvider.cpp
- toolkit/xre/nsXREDirProvider.h
- − xpcom/io/TorFileUtils.cpp
- − xpcom/io/TorFileUtils.h
- xpcom/io/moz.build
- xpcom/io/nsAppFileLocationProvider.cpp


Changes:

=====================================
mozconfig-macos-x86_64
=====================================
@@ -5,4 +5,5 @@ ac_add_options --enable-strip
 # See bug #41131
 ac_add_options --disable-update-agent
 
+ac_add_options --with-relative-data-dir=../TorBrowser-Data/Browser
 ac_add_options --enable-tor-browser-data-outside-app-dir


=====================================
toolkit/xre/nsAppRunner.cpp
=====================================
@@ -3099,13 +3099,16 @@ static ReturnAbortOnError ShowProfileManager(
   return LaunchChild(false, true);
 }
 
-#ifdef TOR_BROWSER_DATA_OUTSIDE_APP_DIR
-static ProfileStatus CheckTorBrowserDataWriteAccess(nsIFile* aAppDir) {
+#ifdef XP_MACOSX
+static ProfileStatus CheckTorBrowserDataWriteAccess() {
   // Check whether we can write to the directory that will contain
   // TorBrowser-Data.
+  RefPtr<nsXREDirProvider> singleton = nsXREDirProvider::GetSingleton();
+  if (!singleton) {
+    return PROFILE_STATUS_OTHER_ERROR;
+  }
   nsCOMPtr<nsIFile> tbDataDir;
-  nsresult rv =
-      nsXREDirProvider::GetTorBrowserUserDataDir(getter_AddRefs(tbDataDir));
+  nsresult rv = singleton->GetTorBrowserUserDataDir(getter_AddRefs(tbDataDir));
   NS_ENSURE_SUCCESS(rv, PROFILE_STATUS_OTHER_ERROR);
   nsCOMPtr<nsIFile> tbDataDirParent;
   rv = tbDataDir->GetParent(getter_AddRefs(tbDataDirParent));
@@ -5081,25 +5084,14 @@ int XREMain::XRE_mainStartup(bool* aExitFlag) {
   }
 #endif
 
-#if (defined(MOZ_UPDATER) && !defined(MOZ_WIDGET_ANDROID)) || \
-    defined(TOR_BROWSER_DATA_OUTSIDE_APP_DIR)
-  nsCOMPtr<nsIFile> exeFile, exeDir;
-  bool persistent;
-  rv = mDirProvider.GetFile(XRE_EXECUTABLE_FILE, &persistent,
-                            getter_AddRefs(exeFile));
-  NS_ENSURE_SUCCESS(rv, 1);
-  rv = exeFile->GetParent(getter_AddRefs(exeDir));
-  NS_ENSURE_SUCCESS(rv, 1);
-#endif
-
   rv = NS_NewToolkitProfileService(getter_AddRefs(mProfileSvc));
-#ifdef TOR_BROWSER_DATA_OUTSIDE_APP_DIR
+#ifdef XP_MACOSX
   if (NS_FAILED(rv)) {
     // NS_NewToolkitProfileService() returns a generic NS_ERROR_FAILURE error
     // if creation of the TorBrowser-Data directory fails due to access denied
     // or because of a read-only disk volume. Do an extra check here to detect
     // these errors so we can display an informative error message.
-    ProfileStatus status = CheckTorBrowserDataWriteAccess(exeDir);
+    ProfileStatus status = CheckTorBrowserDataWriteAccess();
     if ((PROFILE_STATUS_ACCESS_DENIED == status) ||
         (PROFILE_STATUS_READ_ONLY == status)) {
       ProfileErrorDialog(nullptr, nullptr, status, nullptr, mNativeApp,
@@ -5216,14 +5208,12 @@ int XREMain::XRE_mainStartup(bool* aExitFlag) {
     if (CheckArg("test-process-updates")) {
       SaveToEnv("MOZ_TEST_PROCESS_UPDATES=1");
     }
-#  ifndef TOR_BROWSER_DATA_OUTSIDE_APP_DIR
     nsCOMPtr<nsIFile> exeFile, exeDir;
     rv = mDirProvider.GetFile(XRE_EXECUTABLE_FILE, &persistent,
                               getter_AddRefs(exeFile));
     NS_ENSURE_SUCCESS(rv, 1);
     rv = exeFile->GetParent(getter_AddRefs(exeDir));
     NS_ENSURE_SUCCESS(rv, 1);
-#  endif
 
 #  ifdef TOR_BROWSER_UPDATE
     nsAutoCString compatVersion(TOR_BROWSER_VERSION_QUOTED);


=====================================
toolkit/xre/nsXREDirProvider.cpp
=====================================
@@ -56,8 +56,6 @@
 #  include "nsIPK11Token.h"
 #endif
 
-#include "TorFileUtils.h"
-
 #include <stdlib.h>
 
 #ifdef XP_WIN
@@ -113,6 +111,10 @@ nsIFile* gDataDirHome = nullptr;
 nsCOMPtr<nsIFile> gDataDirProfileLocal = nullptr;
 nsCOMPtr<nsIFile> gDataDirProfile = nullptr;
 
+#if defined(RELATIVE_DATA_DIR)
+mozilla::Maybe<nsCOMPtr<nsIFile>> gDataDirPortable;
+#endif
+
 // These are required to allow nsXREDirProvider to be usable in xpcshell tests.
 // where gAppData is null.
 #if defined(XP_MACOSX) || defined(XP_UNIX)
@@ -1205,14 +1207,19 @@ nsresult nsXREDirProvider::GetUpdateRootDir(nsIFile** aResult,
   }
 #endif
   nsCOMPtr<nsIFile> updRoot;
+  nsCOMPtr<nsIFile> appFile;
+  bool per = false;
+  nsresult rv = GetFile(XRE_EXECUTABLE_FILE, &per, getter_AddRefs(appFile));
+  NS_ENSURE_SUCCESS(rv, rv);
+
 #if defined(TOR_BROWSER_UPDATE)
   // For Tor Browser, we store update history, etc. within the UpdateInfo
   // directory under the user data directory.
-  nsresult rv = GetTorBrowserUserDataDir(getter_AddRefs(updRoot));
+  rv = GetTorBrowserUserDataDir(getter_AddRefs(updRoot));
   NS_ENSURE_SUCCESS(rv, rv);
   rv = updRoot->AppendNative("UpdateInfo"_ns);
   NS_ENSURE_SUCCESS(rv, rv);
-#  if defined(XP_MACOSX) && defined(TOR_BROWSER_DATA_OUTSIDE_APP_DIR)
+#  if defined(XP_MACOSX)
   // Since the TorBrowser-Data directory may be shared among different
   // installations of the application, embed the app path in the update dir
   // so that the update history is partitioned. This is much less likely to
@@ -1220,29 +1227,20 @@ nsresult nsXREDirProvider::GetUpdateRootDir(nsIFile** aResult,
   // those platforms include a "container" folder that provides partitioning
   // by default, and we do not support use of a shared, OS-recommended area
   // for user data on those platforms.
-  nsAutoString appDirPath;
-  nsCOMPtr<nsIFile> appRootDir;
-  rv = GetAppRootDir(getter_AddRefs(appRootDir));
+  nsAutoString appPath;
+  rv = appFile->GetPath(appPath);
   NS_ENSURE_SUCCESS(rv, rv);
-  rv = appRootDir->GetPath(appDirPath);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  int32_t dotIndex = appDirPath.RFind(".app");
+  int32_t dotIndex = appPath.RFind(".app");
   if (dotIndex == kNotFound) {
-    dotIndex = appDirPath.Length();
+    dotIndex = appPath.Length();
   }
-  appDirPath = Substring(appDirPath, 1, dotIndex - 1);
-  rv = updRoot->AppendRelativePath(appDirPath);
+  appPath = Substring(appPath, 1, dotIndex - 1);
+  rv = updRoot->AppendRelativePath(appPath);
   NS_ENSURE_SUCCESS(rv, rv);
 #  endif
 #else  // ! TOR_BROWSER_UPDATE
-  nsCOMPtr<nsIFile> appFile;
-  bool per = false;
-  nsresult rv = GetFile(XRE_EXECUTABLE_FILE, &per, getter_AddRefs(appFile));
-  NS_ENSURE_SUCCESS(rv, rv);
   rv = appFile->GetParent(getter_AddRefs(updRoot));
   NS_ENSURE_SUCCESS(rv, rv);
-
 #  ifdef XP_MACOSX
   nsCOMPtr<nsIFile> appRootDirFile;
   nsCOMPtr<nsIFile> localDir;
@@ -1360,6 +1358,91 @@ nsresult nsXREDirProvider::SetUserDataProfileDirectory(nsCOMPtr<nsIFile>& aFile,
   return NS_OK;
 }
 
+#if defined(RELATIVE_DATA_DIR)
+nsresult nsXREDirProvider::GetPortableDataDir(nsIFile** aFile,
+                                              bool& aIsPortable) {
+  if (gDataDirPortable) {
+    if (*gDataDirPortable) {
+      nsresult rv = (*gDataDirPortable)->Clone(aFile);
+      NS_ENSURE_SUCCESS(rv, rv);
+      aIsPortable = true;
+    } else {
+      aIsPortable = false;
+    }
+    return NS_OK;
+  }
+
+  nsCOMPtr<nsIFile> exeFile, exeDir;
+  bool persistent = false;
+  nsresult rv =
+      GetFile(XRE_EXECUTABLE_FILE, &persistent, getter_AddRefs(exeFile));
+  NS_ENSURE_SUCCESS(rv, rv);
+  rv = exeFile->Normalize();
+  NS_ENSURE_SUCCESS(rv, rv);
+  rv = exeFile->GetParent(getter_AddRefs(exeDir));
+  NS_ENSURE_SUCCESS(rv, rv);
+
+#  if defined(XP_MACOSX)
+  nsAutoString exeDirPath;
+  rv = exeDir->GetPath(exeDirPath);
+  NS_ENSURE_SUCCESS(rv, rv);
+  // When the browser is installed in /Applications, we never run in portable
+  // mode.
+  if (exeDirPath.Find("/Applications/", true /* ignore case */) == 0) {
+    aIsPortable = false;
+    return NS_OK;
+  }
+#  endif
+
+  nsCOMPtr<nsIFile> systemInstallFile;
+  rv = exeDir->Clone(getter_AddRefs(systemInstallFile));
+  NS_ENSURE_SUCCESS(rv, rv);
+  rv = systemInstallFile->AppendNative("system-install"_ns);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  bool exists = false;
+  rv = systemInstallFile->Exists(&exists);
+  NS_ENSURE_SUCCESS(rv, rv);
+  if (exists) {
+    aIsPortable = false;
+    gDataDirPortable.emplace(nullptr);
+    return NS_OK;
+  }
+
+  nsCOMPtr<nsIFile> localDir = exeDir;
+#  if defined(XP_MACOSX)
+  rv = exeDir->GetParent(getter_AddRefs(localDir));
+  NS_ENSURE_SUCCESS(rv, rv);
+  exeDir = localDir;
+  rv = exeDir->GetParent(getter_AddRefs(localDir));
+  NS_ENSURE_SUCCESS(rv, rv);
+#  endif
+  rv = localDir->SetRelativePath(localDir.get(),
+                                 nsLiteralCString(RELATIVE_DATA_DIR));
+  NS_ENSURE_SUCCESS(rv, rv);
+
+#  if defined(XP_MACOSX)
+  // On macOS we try to create the directory immediately to switch to
+  // system-install mode if needed (e.g., when running from the DMG).
+  rv = localDir->Exists(&exists);
+  NS_ENSURE_SUCCESS(rv, rv);
+  if (!exists) {
+    rv = localDir->Create(nsIFile::DIRECTORY_TYPE, 0700);
+    if (NS_FAILED(rv)) {
+      aIsPortable = false;
+      return NS_OK;
+    }
+  }
+#  endif
+
+  gDataDirPortable.emplace(localDir);
+  rv = (*gDataDirPortable)->Clone(aFile);
+  NS_ENSURE_SUCCESS(rv, rv);
+  aIsPortable = true;
+  return rv;
+}
+#endif
+
 nsresult nsXREDirProvider::GetUserDataDirectoryHome(nsIFile** aFile,
                                                     bool aLocal) {
   // Copied from nsAppFileLocationProvider (more or less)
@@ -1374,38 +1457,40 @@ nsresult nsXREDirProvider::GetUserDataDirectoryHome(nsIFile** aFile,
     return gDataDirHome->Clone(aFile);
   }
 
-#if defined(RELATIVE_DATA_DIR) || defined(TOR_BROWSER_DATA_OUTSIDE_APP_DIR)
-#  ifdef TOR_BROWSER_DATA_OUTSIDE_APP_DIR
-  rv = GetTorBrowserUserDataDir(getter_AddRefs(localDir));
-  NS_ENSURE_SUCCESS(rv, rv);
-  rv = localDir->AppendNative("Browser"_ns);
-#  else
+#if defined(RELATIVE_DATA_DIR)
   RefPtr<nsXREDirProvider> singleton = GetSingleton();
   if (!singleton) {
     return NS_ERROR_OUT_OF_MEMORY;
   }
-  rv = singleton->GetAppRootDir(getter_AddRefs(localDir));
+  bool isPortable = false;
+  rv = singleton->GetPortableDataDir(getter_AddRefs(localDir), isPortable);
   NS_ENSURE_SUCCESS(rv, rv);
-  nsAutoCString profileDir(RELATIVE_DATA_DIR);
-  rv = localDir->SetRelativePath(localDir.get(), profileDir);
-  NS_ENSURE_SUCCESS(rv, rv);
-#  endif
-  if (aLocal) {
-    rv = localDir->AppendNative("Caches"_ns);
-    NS_ENSURE_SUCCESS(rv, rv);
+  if (isPortable) {
+    if (aLocal) {
+      rv = localDir->AppendNative("Caches"_ns);
+      NS_ENSURE_SUCCESS(rv, rv);
+    }
+    NS_IF_ADDREF(*aFile = localDir);
+    return rv;
   }
-#elif defined(XP_MACOSX)
+#endif
+
+#if defined(XP_MACOSX)
   FSRef fsRef;
+#  if defined(TOR_BROWSER_VERSION)
+  OSType folderType = kApplicationSupportFolderType;
+#  else
   OSType folderType;
   if (aLocal) {
     folderType = kCachedDataFolderType;
   } else {
-#  ifdef MOZ_THUNDERBIRD
+#    ifdef MOZ_THUNDERBIRD
     folderType = kDomainLibraryFolderType;
-#  else
+#    else
     folderType = kApplicationSupportFolderType;
-#  endif
+#    endif
   }
+#  endif
   OSErr err = ::FSFindFolder(kUserDomain, folderType, kCreateFolder, &fsRef);
   NS_ENSURE_FALSE(err, NS_ERROR_FAILURE);
 
@@ -1418,6 +1503,17 @@ nsresult nsXREDirProvider::GetUserDataDirectoryHome(nsIFile** aFile,
   rv = dirFileMac->InitWithFSRef(&fsRef);
   NS_ENSURE_SUCCESS(rv, rv);
 
+#  if defined(TOR_BROWSER_VERSION)
+  rv = dirFileMac->AppendNative("TorBrowser-Data"_ns);
+  NS_ENSURE_SUCCESS(rv, rv);
+  rv = dirFileMac->AppendNative("Browser"_ns);
+  NS_ENSURE_SUCCESS(rv, rv);
+  if (aLocal) {
+    rv = dirFileMac->AppendNative("Caches"_ns);
+    NS_ENSURE_SUCCESS(rv, rv);
+  }
+#  endif
+
   localDir = dirFileMac;
 #elif defined(XP_IOS)
   nsAutoCString userDir;
@@ -1537,37 +1633,6 @@ nsresult nsXREDirProvider::GetUserDataDirectory(nsIFile** aFile, bool aLocal) {
   return NS_OK;
 }
 
-nsresult nsXREDirProvider::GetTorBrowserUserDataDir(nsIFile** aFile) {
-  NS_ENSURE_ARG_POINTER(aFile);
-  nsCOMPtr<nsIFile> appRootDir;
-  RefPtr<nsXREDirProvider> singleton = GetSingleton();
-  if (!singleton) {
-    return NS_ERROR_OUT_OF_MEMORY;
-  }
-  nsresult rv = singleton->GetAppRootDir(getter_AddRefs(appRootDir));
-  NS_ENSURE_SUCCESS(rv, rv);
-  return TorBrowser_GetUserDataDir(appRootDir, aFile);
-}
-
-nsresult nsXREDirProvider::GetAppRootDir(nsIFile** aFile) {
-  bool persistent = false;
-  nsCOMPtr<nsIFile> file, appRootDir;
-  nsresult rv = GetFile(XRE_EXECUTABLE_FILE, &persistent, getter_AddRefs(file));
-  NS_ENSURE_SUCCESS(rv, rv);
-  rv = file->Normalize();
-  NS_ENSURE_SUCCESS(rv, rv);
-  int levelsToRemove = 1;
-#if defined(XP_MACOSX)
-  levelsToRemove += 2;
-#endif
-  while (levelsToRemove-- > 0) {
-    rv = file->GetParent(getter_AddRefs(appRootDir));
-    NS_ENSURE_SUCCESS(rv, rv);
-    file = appRootDir;
-  }
-  return appRootDir->Clone(aFile);
-}
-
 nsresult nsXREDirProvider::EnsureDirectoryExists(nsIFile* aDirectory) {
   nsresult rv = aDirectory->Create(nsIFile::DIRECTORY_TYPE, 0700);
 
@@ -1618,6 +1683,13 @@ nsresult nsXREDirProvider::AppendProfilePath(nsIFile* aFile, bool aLocal) {
     return NS_OK;
   }
 
+#if defined(RELATIVE_DATA_DIR)
+  if (gDataDirPortable && *gDataDirPortable) {
+    // Do nothing in portable mode
+    return NS_OK;
+  }
+#endif
+
   nsAutoCString profile;
   nsAutoCString appName;
   nsAutoCString vendor;
@@ -1631,29 +1703,29 @@ nsresult nsXREDirProvider::AppendProfilePath(nsIFile* aFile, bool aLocal) {
   nsresult rv = NS_OK;
 
 #if defined(XP_MACOSX)
+#  ifndef TOR_BROWSER_VERSION
+  // For Tor Browser we already prepare the data directory as we need it, even
+  // when we are running a system install.
   if (!profile.IsEmpty()) {
     rv = AppendProfileString(aFile, profile.get());
-#  ifndef RELATIVE_DATA_DIR
   } else {
     // Note that MacOS ignores the vendor when creating the profile hierarchy -
     // all application preferences directories live alongside one another in
     // ~/Library/Application Support/
     rv = aFile->AppendNative(appName);
-#  endif
   }
   NS_ENSURE_SUCCESS(rv, rv);
+#  endif
 
 #elif defined(XP_WIN)
   if (!profile.IsEmpty()) {
     rv = AppendProfileString(aFile, profile.get());
-#  ifndef RELATIVE_DATA_DIR
   } else {
     if (!vendor.IsEmpty()) {
       rv = aFile->AppendNative(vendor);
       NS_ENSURE_SUCCESS(rv, rv);
     }
     rv = aFile->AppendNative(appName);
-#  endif
   }
   NS_ENSURE_SUCCESS(rv, rv);
 
@@ -1668,26 +1740,21 @@ nsresult nsXREDirProvider::AppendProfilePath(nsIFile* aFile, bool aLocal) {
   nsAutoCString folder;
   // Make it hidden (by starting with "."), except when local (the
   // profile is already under ~/.cache or XDG_CACHE_HOME).
-#  ifndef RELATIVE_DATA_DIR
   if (!aLocal) folder.Assign('.');
-#  endif
 
   if (!profile.IsEmpty()) {
     // Skip any leading path characters
     const char* profileStart = profile.get();
     while (*profileStart == '/' || *profileStart == '\\') profileStart++;
 
-#  ifndef RELATIVE_DATA_DIR
     // On the off chance that someone wanted their folder to be hidden don't
     // let it become ".."
     if (*profileStart == '.' && !aLocal) profileStart++;
-#  endif
 
     folder.Append(profileStart);
     ToLowerCase(folder);
 
     rv = AppendProfileString(aFile, folder.BeginReading());
-#  ifndef RELATIVE_DATA_DIR
   } else {
     if (!vendor.IsEmpty()) {
       folder.Append(vendor);
@@ -1706,7 +1773,6 @@ nsresult nsXREDirProvider::AppendProfilePath(nsIFile* aFile, bool aLocal) {
 
       rv = aFile->AppendNative(folder);
     }
-#  endif
   }
   NS_ENSURE_SUCCESS(rv, rv);
 
@@ -1734,3 +1800,21 @@ nsresult nsXREDirProvider::AppendProfileString(nsIFile* aFile,
 
   return NS_OK;
 }
+
+nsresult nsXREDirProvider::GetTorBrowserUserDataDir(nsIFile** aFile) {
+#ifdef ANDROID
+  // We expect this function not to be called on Android.
+  // But, for sake of completeness, we handle also this case.
+  const char* homeDir = getenv("HOME");
+  if (!homeDir || !*homeDir) {
+    return NS_ERROR_FAILURE;
+  }
+  return NS_NewNativeLocalFile(nsDependentCString(homeDir), true, aFile);
+#endif
+
+  NS_ENSURE_ARG_POINTER(aFile);
+  nsCOMPtr<nsIFile> dataDir;
+  nsresult rv = GetUserDataDirectoryHome(getter_AddRefs(dataDir), false);
+  NS_ENSURE_SUCCESS(rv, rv);
+  return dataDir->GetParent(aFile);
+}


=====================================
toolkit/xre/nsXREDirProvider.h
=====================================
@@ -110,18 +110,11 @@ class nsXREDirProvider final : public nsIDirectoryServiceProvider2,
   nsresult GetProfileDir(nsIFile** aResult);
 
   /**
-   * Get the TorBrowser user data directory by calling the
-   * TorBrowser_GetUserDataDir() utility function.
+   * Get the Tor Browser user data directory.
+   * We take for granted that for Tor Browser we can take the parent directory
+   * of the one returned by GetUserDataDirectoryHome (with aLocal = false).
    */
-  static nsresult GetTorBrowserUserDataDir(nsIFile** aFile);
-
-  /**
-   * Get the path to the base application directory.
-   *
-   * In almost all platforms it is the directory that contains the Firefox
-   * executable; on macOS we remove also Contents/MacOS from it.
-   */
-  nsresult GetAppRootDir(nsIFile** aFile);
+  nsresult GetTorBrowserUserDataDir(nsIFile** aFile);
 
  protected:
   nsresult GetFilesInternal(const char* aProperty,
@@ -169,6 +162,14 @@ class nsXREDirProvider final : public nsIDirectoryServiceProvider2,
  private:
   static nsresult SetUserDataProfileDirectory(nsCOMPtr<nsIFile>& aFile,
                                               bool aLocal);
+
+#if defined(RELATIVE_DATA_DIR)
+  /**
+   * Get the path to the portable data dir, if the application is running in
+   * portable mode.
+   */
+  nsresult GetPortableDataDir(nsIFile** aFile, bool& aIsPortable);
+#endif
 };
 
 #endif


=====================================
xpcom/io/TorFileUtils.cpp deleted
=====================================
@@ -1,96 +0,0 @@
-/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
-/* vim: set ts=8 sts=2 et sw=2 tw=80: */
-/* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-
-#include "TorFileUtils.h"
-#include "nsString.h"
-#ifdef MOZ_WIDGET_COCOA
-#  include <Carbon/Carbon.h>
-#  include "nsILocalFileMac.h"
-#endif
-
-nsresult TorBrowser_GetUserDataDir(nsIFile* aAppDir, nsIFile** aFile) {
-  NS_ENSURE_ARG_POINTER(aFile);
-  nsCOMPtr<nsIFile> tbDataDir;
-
-#ifdef ANDROID
-  const char* homeDir = getenv("HOME");
-  if (!homeDir || !*homeDir) return NS_ERROR_FAILURE;
-  nsresult rv = NS_NewNativeLocalFile(nsDependentCString(homeDir), true,
-                                      getter_AddRefs(tbDataDir));
-  NS_ENSURE_SUCCESS(rv, rv);
-#elif defined(TOR_BROWSER_DATA_OUTSIDE_APP_DIR)
-  nsAutoCString tbDataLeafName("TorBrowser-Data"_ns);
-#  ifndef XP_MACOSX
-  // On all platforms except Mac OS, we always operate in a "portable" mode
-  // where the TorBrowser-Data directory is located next to the application.
-  nsresult rv = aAppDir->GetParent(getter_AddRefs(tbDataDir));
-  NS_ENSURE_SUCCESS(rv, rv);
-  rv = tbDataDir->AppendNative(tbDataLeafName);
-  NS_ENSURE_SUCCESS(rv, rv);
-#  else
-  // For Mac OS, determine whether we should store user data in the OS's
-  // standard location (i.e., under ~/Library/Application Support). We use
-  // the OS location if (1) the application is installed in a directory whose
-  // path contains "/Applications" or (2) the TorBrowser-Data directory does
-  // not exist and cannot be created (which probably means we lack write
-  // permission to the directory that contains the application).
-  nsAutoString appRootPath;
-  nsresult rv = aAppDir->GetPath(appRootPath);
-  NS_ENSURE_SUCCESS(rv, rv);
-  bool useOSLocation =
-      (appRootPath.Find("/Applications", true /* ignore case */) >= 0);
-  if (!useOSLocation) {
-    // We hope to use the portable (aka side-by-side) approach, but before we
-    // commit to that, let's ensure that we can create the TorBrowser-Data
-    // directory. If it already exists, we will try to use it; if not and we
-    // fail to create it, we will switch to ~/Library/Application Support.
-    rv = aAppDir->GetParent(getter_AddRefs(tbDataDir));
-    NS_ENSURE_SUCCESS(rv, rv);
-    rv = tbDataDir->AppendNative(tbDataLeafName);
-    NS_ENSURE_SUCCESS(rv, rv);
-    bool exists = false;
-    rv = tbDataDir->Exists(&exists);
-    if (NS_SUCCEEDED(rv) && !exists)
-      rv = tbDataDir->Create(nsIFile::DIRECTORY_TYPE, 0700);
-    useOSLocation = NS_FAILED(rv);
-  }
-
-  if (useOSLocation) {
-    // We are using ~/Library/Application Support/TorBrowser-Data. We do not
-    // need to create that directory here because the code in nsXREDirProvider
-    // will do so (and the user should always have write permission for
-    // ~/Library/Application Support; if they do not we have no more options).
-    FSRef fsRef;
-    OSErr err = ::FSFindFolder(kUserDomain, kApplicationSupportFolderType,
-                               kCreateFolder, &fsRef);
-    NS_ENSURE_FALSE(err, NS_ERROR_FAILURE);
-    // To convert the FSRef returned by FSFindFolder() into an nsIFile that
-    // points to ~/Library/Application Support, we first create an empty
-    // nsIFile object (no path) and then use InitWithFSRef() to set the
-    // path.
-    rv = NS_NewNativeLocalFile(""_ns, true, getter_AddRefs(tbDataDir));
-    NS_ENSURE_SUCCESS(rv, rv);
-    nsCOMPtr<nsILocalFileMac> dirFileMac = do_QueryInterface(tbDataDir);
-    if (!dirFileMac) return NS_ERROR_UNEXPECTED;
-    rv = dirFileMac->InitWithFSRef(&fsRef);
-    NS_ENSURE_SUCCESS(rv, rv);
-    rv = tbDataDir->AppendNative(tbDataLeafName);
-    NS_ENSURE_SUCCESS(rv, rv);
-  }
-#  endif
-
-#else
-  // User data is embedded within the application directory (i.e.,
-  // TOR_BROWSER_DATA_OUTSIDE_APP_DIR is not defined).
-  nsresult rv = aAppDir->Clone(getter_AddRefs(tbDataDir));
-  NS_ENSURE_SUCCESS(rv, rv);
-  rv = tbDataDir->AppendNative("TorBrowser"_ns);
-  NS_ENSURE_SUCCESS(rv, rv);
-#endif
-
-  tbDataDir.forget(aFile);
-  return NS_OK;
-}


=====================================
xpcom/io/TorFileUtils.h deleted
=====================================
@@ -1,32 +0,0 @@
-/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
-/* vim: set ts=8 sts=2 et sw=2 tw=80: */
-/* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-
-#ifndef TorFileUtils_h__
-#define TorFileUtils_h__
-
-#include "nsIFile.h"
-
-/**
- * TorBrowser_GetUserDataDir
- *
- * Retrieve the Tor Browser user data directory.
- * When built with --enable-tor-browser-data-outside-app-dir, the directory
- * is next to the application directory, except on Mac OS where it may be
- * there or it may be at ~/Library/Application Support/TorBrowser-Data (the
- * latter location is used if the .app bundle is in a directory whose path
- * contains /Applications or if we lack write access to the directory that
- * contains the .app).
- * When built without --enable-tor-browser-data-outside-app-dir, this
- * directory is TorBrowser.app/TorBrowser.
- *
- * @param aAppDir   The path to Tor Browser directory
- * @param aFile     Out parameter that is set to the Tor Browser user data
- *                  directory.
- * @return NS_OK on success.  Error otherwise.
- */
-extern nsresult TorBrowser_GetUserDataDir(nsIFile* aAppDir, nsIFile** aFile);
-
-#endif  // !TorFileUtils_h__


=====================================
xpcom/io/moz.build
=====================================
@@ -86,7 +86,6 @@ EXPORTS += [
     "nsUnicharInputStream.h",
     "nsWildCard.h",
     "SpecialSystemDirectory.h",
-    "TorFileUtils.h",
 ]
 
 EXPORTS.mozilla += [
@@ -137,10 +136,6 @@ UNIFIED_SOURCES += [
     "SpecialSystemDirectory.cpp",
 ]
 
-SOURCES += [
-    "TorFileUtils.cpp",
-]
-
 if CONFIG["MOZ_WIDGET_TOOLKIT"] == "cocoa":
     SOURCES += [
         "CocoaFileUtils.mm",


=====================================
xpcom/io/nsAppFileLocationProvider.cpp
=====================================
@@ -27,8 +27,6 @@
 #  include <sys/param.h>
 #endif
 
-#include "TorFileUtils.h"
-
 // WARNING: These hard coded names need to go away. They need to
 // come from localizable resources
 
@@ -231,66 +229,137 @@ nsresult nsAppFileLocationProvider::CloneMozBinDirectory(nsIFile** aLocalFile) {
   return NS_OK;
 }
 
-//----------------------------------------------------------------------------------------
-// GetProductDirectory - Gets the directory which contains the application data
-// folder
-//
-// UNIX   : ~/.mozilla/
-// WIN    : <Application Data folder on user's machine>\Mozilla
-// Mac    : :Documents:Mozilla:
-//----------------------------------------------------------------------------------------
-nsresult nsAppFileLocationProvider::GetProductDirectory(nsIFile** aLocalFile,
-                                                        bool aLocal) {
-  if (NS_WARN_IF(!aLocalFile)) {
-    return NS_ERROR_INVALID_ARG;
-  }
+#ifdef RELATIVE_DATA_DIR
+static nsresult SetupPortableMode(nsIFile** aDirectory, bool aLocal,
+                                  bool& aIsPortable) {
+  // This is almost the same as nsXREDirProvider::GetPortableDataDir.
+  // However, it seems that this is never called, at least during simple usage
+  // of the browser.
 
   nsresult rv = NS_ERROR_UNEXPECTED;
-  bool exists;
-  nsCOMPtr<nsIFile> localDir;
-
-#if defined(RELATIVE_DATA_DIR) || defined(TOR_BROWSER_DATA_OUTSIDE_APP_DIR)
   nsCOMPtr<nsIProperties> directoryService(
       do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv));
   NS_ENSURE_SUCCESS(rv, rv);
-
-  bool persistent = false;
-  nsCOMPtr<nsIFile> file, appRootDir;
+  nsCOMPtr<nsIFile> exeFile, exeDir;
   rv = directoryService->Get(XRE_EXECUTABLE_FILE, NS_GET_IID(nsIFile),
-                             getter_AddRefs(file));
+                             getter_AddRefs(exeFile));
+  rv = exeFile->Normalize();
   NS_ENSURE_SUCCESS(rv, rv);
-  rv = file->Normalize();
+  rv = exeFile->GetParent(getter_AddRefs(exeDir));
   NS_ENSURE_SUCCESS(rv, rv);
-  int levelsToRemove = 1;
+
 #  if defined(XP_MACOSX)
-  levelsToRemove += 2;
+  nsAutoString exeDirPath;
+  rv = exeDir->GetPath(exeDirPath);
+  NS_ENSURE_SUCCESS(rv, rv);
+  // When the browser is installed in /Applications, we never run in portable
+  // mode.
+  if (exeDirPath.Find("/Applications/", true /* ignore case */) == 0) {
+    aIsPortable = false;
+    return NS_OK;
+  }
 #  endif
-  while (levelsToRemove-- > 0) {
-    rv = file->GetParent(getter_AddRefs(appRootDir));
-    NS_ENSURE_SUCCESS(rv, rv);
-    file = appRootDir;
+
+  nsCOMPtr<nsIFile> systemInstallFile;
+  rv = exeDir->Clone(getter_AddRefs(systemInstallFile));
+  NS_ENSURE_SUCCESS(rv, rv);
+  rv = systemInstallFile->AppendNative("system-install"_ns);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  bool exists = false;
+  rv = systemInstallFile->Exists(&exists);
+  NS_ENSURE_SUCCESS(rv, rv);
+  if (exists) {
+    aIsPortable = false;
+    return NS_OK;
   }
 
-#  ifdef TOR_BROWSER_DATA_OUTSIDE_APP_DIR
-  rv = TorBrowser_GetUserDataDir(appRootDir, getter_AddRefs(localDir));
+  nsCOMPtr<nsIFile> localDir = exeDir;
+#  if defined(XP_MACOSX)
+  rv = exeDir->GetParent(getter_AddRefs(localDir));
   NS_ENSURE_SUCCESS(rv, rv);
-  rv = localDir->AppendNative("Browser"_ns);
-#  else
-  localDir = appRootDir;
-  nsAutoCString profileDir(RELATIVE_DATA_DIR);
-  rv = localDir->SetRelativePath(localDir.get(), profileDir);
-#  endif
+  exeDir = localDir;
+  rv = exeDir->GetParent(getter_AddRefs(localDir));
   NS_ENSURE_SUCCESS(rv, rv);
+#  endif
 
+  rv = localDir->SetRelativePath(localDir.get(),
+                                 nsLiteralCString(RELATIVE_DATA_DIR));
+  NS_ENSURE_SUCCESS(rv, rv);
   if (aLocal) {
     rv = localDir->AppendNative("Caches"_ns);
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
-#elif defined(MOZ_WIDGET_COCOA)
+  rv = localDir->Exists(&exists);
+  NS_ENSURE_SUCCESS(rv, rv);
+  if (!exists) {
+    rv = localDir->Create(nsIFile::DIRECTORY_TYPE, 0700);
+#  if defined(XP_MACOSX)
+    if (NS_FAILED(rv)) {
+      // On macOS, we forgive this failure to allow running from the DMG..
+      aIsPortable = false;
+      return NS_OK;
+    }
+#  else
+    NS_ENSURE_SUCCESS(rv, rv);
+#  endif
+  }
+
+  localDir.forget(aDirectory);
+  aIsPortable = true;
+  return rv;
+}
+#endif
+
+//----------------------------------------------------------------------------------------
+// GetProductDirectory - Gets the directory which contains the application data
+// folder
+//
+// If portable mode is enabled:
+//  - aLocal == false: $APP_ROOT/$RELATIVE_DATA_DIR
+//  - aLocal == true:  $APP_ROOT/$RELATIVE_DATA_DIR/Caches
+// where $APP_ROOT is:
+//  - the parent directory of the executable on Windows and Linux
+//  - the root of the app bundle on macOS
+//
+// Otherwise:
+//  - Windows:
+//    - aLocal == false: %APPDATA%/$MOZ_USER_DIR
+//    - aLocal == true: %LOCALAPPDATA%/$MOZ_USER_DIR
+//  - macOS:
+//    - aLocal == false: kDomainLibraryFolderType/$MOZ_USER_DIR
+//    - aLocal == true: kCachedDataFolderType/$MOZ_USER_DIR
+//  - Unix: ~/$MOZ_USER_DIR
+//----------------------------------------------------------------------------------------
+nsresult nsAppFileLocationProvider::GetProductDirectory(nsIFile** aLocalFile,
+                                                        bool aLocal) {
+  if (NS_WARN_IF(!aLocalFile)) {
+    return NS_ERROR_INVALID_ARG;
+  }
+
+  nsresult rv = NS_ERROR_UNEXPECTED;
+  bool exists;
+  nsCOMPtr<nsIFile> localDir;
+
+#if defined(RELATIVE_DATA_DIR)
+  bool isPortable = false;
+  rv = SetupPortableMode(aLocalFile, aLocal, isPortable);
+  // If portable mode is enabled, we absolutely want it (e.g., to be sure there
+  // will not be disk leaks), so a failure is to be propagated.
+  if (NS_FAILED(rv) || isPortable) {
+    return rv;
+  }
+#endif
+
+#if defined(MOZ_WIDGET_COCOA)
   FSRef fsRef;
+#  if defined(TOR_BROWSER_VERSION)
+  OSType folderType = kApplicationSupportFolderType;
+#  else
   OSType folderType =
       aLocal ? (OSType)kCachedDataFolderType : (OSType)kDomainLibraryFolderType;
+#  endif
   OSErr err = ::FSFindFolder(kUserDomain, folderType, kCreateFolder, &fsRef);
   if (err) {
     return NS_ERROR_FAILURE;
@@ -304,6 +373,17 @@ nsresult nsAppFileLocationProvider::GetProductDirectory(nsIFile** aLocalFile,
   if (NS_FAILED(rv)) {
     return rv;
   }
+
+#  if defined(TOR_BROWSER_VERSION)
+  rv = localDir->AppendNative("TorBrowser-Data"_ns);
+  NS_ENSURE_SUCCESS(rv, rv);
+  rv = localDir->AppendNative("Browser"_ns);
+  NS_ENSURE_SUCCESS(rv, rv);
+  if (aLocal) {
+    rv = localDir->AppendNative("Caches"_ns);
+    NS_ENSURE_SUCCESS(rv, rv);
+  }
+#  endif
 #elif defined(XP_WIN)
   nsCOMPtr<nsIProperties> directoryService =
       do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv);
@@ -326,7 +406,7 @@ nsresult nsAppFileLocationProvider::GetProductDirectory(nsIFile** aLocalFile,
 #  error dont_know_how_to_get_product_dir_on_your_platform
 #endif
 
-#if !defined(RELATIVE_DATA_DIR) && !defined(TOR_BROWSER_VERSION)
+#if !defined(TOR_BROWSER_VERSION)
   rv = localDir->AppendRelativeNativePath(DEFAULT_PRODUCT_DIR);
   if (NS_FAILED(rv)) {
     return rv;
@@ -350,6 +430,11 @@ nsresult nsAppFileLocationProvider::GetProductDirectory(nsIFile** aLocalFile,
 //----------------------------------------------------------------------------------------
 // GetDefaultUserProfileRoot - Gets the directory which contains each user
 // profile dir
+//
+// - Windows and macOS: $PRODUCT_DIRECTORY/Profiles
+// - Unix: $PRODUCT_DIRECTORY
+// See also GetProductDirectory for instructions on how $PRODUCT_DIRECTORY is
+// generated.
 //----------------------------------------------------------------------------------------
 nsresult nsAppFileLocationProvider::GetDefaultUserProfileRoot(
     nsIFile** aLocalFile, bool aLocal) {



View it on GitLab: https://gitlab.torproject.org/tpo/applications/tor-browser/-/compare/93a409f1321d07b2cea869834da8ddcf4d16a357...f210d8ca892be5515f481c9477230814f20cdb93

-- 
View it on GitLab: https://gitlab.torproject.org/tpo/applications/tor-browser/-/compare/93a409f1321d07b2cea869834da8ddcf4d16a357...f210d8ca892be5515f481c9477230814f20cdb93
You're receiving this email because of your account on gitlab.torproject.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.torproject.org/pipermail/tbb-commits/attachments/20230208/1a5769c9/attachment-0001.htm>


More information about the tbb-commits mailing list