[tor-commits] [tor-browser] 07/48: Bug 9173: Change the default Firefox profile directory to be relative.

gitolite role git at cupani.torproject.org
Tue Sep 27 14:58:40 UTC 2022


This is an automated email from the git hooks/post-receive script.

richard pushed a commit to branch tor-browser-102.3.0esr-12.0-2
in repository tor-browser.

commit 70a342858d62156522359f6a0a7734b3f7e95ecb
Author: Kathy Brade <brade at pearlcrescent.com>
AuthorDate: Fri Oct 18 15:20:06 2013 -0400

    Bug 9173: Change the default Firefox profile directory to be relative.
    
    This should eliminate our need to rely on a wrapper script that
    sets /Users/arthur and launches Firefox with -profile.
---
 browser/config/mozconfigs/base-browser |  2 +
 moz.configure                          | 19 ++++++++
 toolkit/xre/nsAppRunner.cpp            | 14 ++++--
 toolkit/xre/nsXREDirProvider.cpp       | 85 +++++++++++++++-------------------
 toolkit/xre/nsXREDirProvider.h         |  8 ++++
 xpcom/io/nsAppFileLocationProvider.cpp | 61 +++++++++++++-----------
 6 files changed, 110 insertions(+), 79 deletions(-)

diff --git a/browser/config/mozconfigs/base-browser b/browser/config/mozconfigs/base-browser
index 1896b995a8d4..3281543dc71a 100644
--- a/browser/config/mozconfigs/base-browser
+++ b/browser/config/mozconfigs/base-browser
@@ -32,3 +32,5 @@ ac_add_options --disable-system-policies
 
 # Disable telemetry
 ac_add_options MOZ_TELEMETRY_REPORTING=
+
+ac_add_options --with-relative-profile=BaseBrowser/Data/Browser
diff --git a/moz.configure b/moz.configure
index 8b74afbabec1..11d93c563a5d 100755
--- a/moz.configure
+++ b/moz.configure
@@ -1017,6 +1017,25 @@ set_config("ZLIB_IN_MOZGLUE", zlib_in_mozglue)
 set_define("ZLIB_IN_MOZGLUE", zlib_in_mozglue)
 
 
+option(
+    "--with-relative-profile",
+    nargs=1,
+    help="Sets the directory of the profile, relative to the application directory"
+)
+
+
+ at depends("--with-relative-profile", target)
+ at imports("json")
+def relative_profile(value, target):
+    if target.os == "Android":
+        die("--with-relative-profile is not supported on Android")
+    if value:
+        return json.dumps(value[0])
+
+
+set_define("RELATIVE_PROFILE_DIRECTORY", relative_profile)
+
+
 # Please do not add configure checks from here on.
 
 # Fallthrough to autoconf-based configure
diff --git a/toolkit/xre/nsAppRunner.cpp b/toolkit/xre/nsAppRunner.cpp
index 79baf8416083..fca0b5b314d1 100644
--- a/toolkit/xre/nsAppRunner.cpp
+++ b/toolkit/xre/nsAppRunner.cpp
@@ -2777,6 +2777,8 @@ static nsresult ProfileMissingDialog(nsINativeAppSupport* aNative) {
 #endif    // MOZ_WIDGET_ANDROID
 }
 
+// If aUnlocker is NULL, it is also OK for the following arguments to be NULL:
+//   aProfileDir, aProfileLocalDir, aResult.
 static ReturnAbortOnError ProfileLockedDialog(nsIFile* aProfileDir,
                                               nsIFile* aProfileLocalDir,
                                               nsIProfileUnlocker* aUnlocker,
@@ -2784,17 +2786,19 @@ static ReturnAbortOnError ProfileLockedDialog(nsIFile* aProfileDir,
                                               nsIProfileLock** aResult) {
   nsresult rv;
 
-  bool exists;
-  aProfileDir->Exists(&exists);
-  if (!exists) {
-    return ProfileMissingDialog(aNative);
+  if (aProfileDir) {
+    bool exists;
+    aProfileDir->Exists(&exists);
+    if (!exists) {
+      return ProfileMissingDialog(aNative);
+    }
   }
 
   ScopedXPCOMStartup xpcom;
   rv = xpcom.Initialize();
   NS_ENSURE_SUCCESS(rv, rv);
 
-  mozilla::Telemetry::WriteFailedProfileLock(aProfileDir);
+  if (aProfileDir) mozilla::Telemetry::WriteFailedProfileLock(aProfileDir);
 
   rv = xpcom.SetWindowCreator(aNative);
   NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE);
diff --git a/toolkit/xre/nsXREDirProvider.cpp b/toolkit/xre/nsXREDirProvider.cpp
index 5536f4423f4a..0454863ac07a 100644
--- a/toolkit/xre/nsXREDirProvider.cpp
+++ b/toolkit/xre/nsXREDirProvider.cpp
@@ -234,9 +234,6 @@ nsresult nsXREDirProvider::GetUserProfilesRootDir(nsIFile** aResult) {
   nsresult rv = GetUserDataDirectory(getter_AddRefs(file), false);
 
   if (NS_SUCCEEDED(rv)) {
-#if !defined(XP_UNIX) || defined(XP_MACOSX)
-    rv = file->AppendNative("Profiles"_ns);
-#endif
     // We must create the profile directory here if it does not exist.
     nsresult tmp = EnsureDirectoryExists(file);
     if (NS_FAILED(tmp)) {
@@ -252,9 +249,6 @@ nsresult nsXREDirProvider::GetUserProfilesLocalDir(nsIFile** aResult) {
   nsresult rv = GetUserDataDirectory(getter_AddRefs(file), true);
 
   if (NS_SUCCEEDED(rv)) {
-#if !defined(XP_UNIX) || defined(XP_MACOSX)
-    rv = file->AppendNative("Profiles"_ns);
-#endif
     // We must create the profile directory here if it does not exist.
     nsresult tmp = EnsureDirectoryExists(file);
     if (NS_FAILED(tmp)) {
@@ -1328,6 +1322,7 @@ nsresult nsXREDirProvider::GetUserDataDirectoryHome(nsIFile** aFile,
                                                     bool aLocal) {
   // Copied from nsAppFileLocationProvider (more or less)
   nsresult rv;
+  NS_ENSURE_ARG_POINTER(aFile);
   nsCOMPtr<nsIFile> localDir;
 
   if (aLocal && gDataDirHomeLocal) {
@@ -1337,7 +1332,21 @@ nsresult nsXREDirProvider::GetUserDataDirectoryHome(nsIFile** aFile,
     return gDataDirHome->Clone(aFile);
   }
 
-#if defined(XP_MACOSX)
+#if defined(RELATIVE_PROFILE_DIRECTORY)
+  RefPtr<nsXREDirProvider> singleton = GetSingleton();
+  if (!singleton) {
+    return NS_ERROR_OUT_OF_MEMORY;
+  }
+  rv = singleton->GetAppRootDir(getter_AddRefs(localDir));
+  NS_ENSURE_SUCCESS(rv, rv);
+  nsAutoCString profileDir(RELATIVE_PROFILE_DIRECTORY);
+  rv = localDir->SetRelativePath(localDir.get(), profileDir);
+  NS_ENSURE_SUCCESS(rv, rv);
+  if (aLocal) {
+    rv = localDir->AppendNative("Caches"_ns);
+    NS_ENSURE_SUCCESS(rv, rv);
+  }
+#elif defined(XP_MACOSX)
   FSRef fsRef;
   OSType folderType;
   if (aLocal) {
@@ -1480,6 +1489,25 @@ nsresult nsXREDirProvider::GetUserDataDirectory(nsIFile** aFile, bool aLocal) {
   return NS_OK;
 }
 
+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);
 
@@ -1531,13 +1559,8 @@ nsresult nsXREDirProvider::AppendProfilePath(nsIFile* aFile, bool aLocal) {
   }
 
   nsAutoCString profile;
-  nsAutoCString appName;
-  nsAutoCString vendor;
   if (gAppData->profile) {
     profile = gAppData->profile;
-  } else {
-    appName = gAppData->name;
-    vendor = gAppData->vendor;
   }
 
   nsresult rv = NS_OK;
@@ -1545,23 +1568,12 @@ nsresult nsXREDirProvider::AppendProfilePath(nsIFile* aFile, bool aLocal) {
 #if defined(XP_MACOSX)
   if (!profile.IsEmpty()) {
     rv = AppendProfileString(aFile, profile.get());
-  } 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);
   }
   NS_ENSURE_SUCCESS(rv, rv);
 
 #elif defined(XP_WIN)
   if (!profile.IsEmpty()) {
     rv = AppendProfileString(aFile, profile.get());
-  } else {
-    if (!vendor.IsEmpty()) {
-      rv = aFile->AppendNative(vendor);
-      NS_ENSURE_SUCCESS(rv, rv);
-    }
-    rv = aFile->AppendNative(appName);
   }
   NS_ENSURE_SUCCESS(rv, rv);
 
@@ -1573,11 +1585,6 @@ nsresult nsXREDirProvider::AppendProfilePath(nsIFile* aFile, bool aLocal) {
   rv = aFile->AppendNative(nsDependentCString("mozilla"));
   NS_ENSURE_SUCCESS(rv, rv);
 #elif defined(XP_UNIX)
-  nsAutoCString folder;
-  // Make it hidden (by starting with "."), except when local (the
-  // profile is already under ~/.cache or XDG_CACHE_HOME).
-  if (!aLocal) folder.Assign('.');
-
   if (!profile.IsEmpty()) {
     // Skip any leading path characters
     const char* profileStart = profile.get();
@@ -1585,30 +1592,14 @@ nsresult nsXREDirProvider::AppendProfilePath(nsIFile* aFile, bool aLocal) {
 
     // On the off chance that someone wanted their folder to be hidden don't
     // let it become ".."
-    if (*profileStart == '.' && !aLocal) profileStart++;
+    if (*profileStart == '.') profileStart++;
 
+    // Make it hidden (by starting with ".").
+    nsAutoCString folder(".");
     folder.Append(profileStart);
     ToLowerCase(folder);
 
     rv = AppendProfileString(aFile, folder.BeginReading());
-  } else {
-    if (!vendor.IsEmpty()) {
-      folder.Append(vendor);
-      ToLowerCase(folder);
-
-      rv = aFile->AppendNative(folder);
-      NS_ENSURE_SUCCESS(rv, rv);
-
-      folder.Truncate();
-    }
-
-    // This can be the case in tests.
-    if (!appName.IsEmpty()) {
-      folder.Append(appName);
-      ToLowerCase(folder);
-
-      rv = aFile->AppendNative(folder);
-    }
   }
   NS_ENSURE_SUCCESS(rv, rv);
 
diff --git a/toolkit/xre/nsXREDirProvider.h b/toolkit/xre/nsXREDirProvider.h
index 40cd9c8eb06a..a9d017d18e0f 100644
--- a/toolkit/xre/nsXREDirProvider.h
+++ b/toolkit/xre/nsXREDirProvider.h
@@ -109,6 +109,14 @@ class nsXREDirProvider final : public nsIDirectoryServiceProvider2,
    */
   nsresult GetProfileDir(nsIFile** aResult);
 
+  /**
+   * 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);
+
  protected:
   nsresult GetFilesInternal(const char* aProperty,
                             nsISimpleEnumerator** aResult);
diff --git a/xpcom/io/nsAppFileLocationProvider.cpp b/xpcom/io/nsAppFileLocationProvider.cpp
index ef974f99048f..60b1775eda97 100644
--- a/xpcom/io/nsAppFileLocationProvider.cpp
+++ b/xpcom/io/nsAppFileLocationProvider.cpp
@@ -243,11 +243,43 @@ nsresult nsAppFileLocationProvider::GetProductDirectory(nsIFile** aLocalFile,
     return NS_ERROR_INVALID_ARG;
   }
 
-  nsresult rv;
+  nsresult rv = NS_ERROR_UNEXPECTED;
   bool exists;
   nsCOMPtr<nsIFile> localDir;
 
-#if defined(MOZ_WIDGET_COCOA)
+#if defined(RELATIVE_PROFILE_DIRECTORY)
+  nsCOMPtr<nsIProperties> directoryService(
+      do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv));
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  bool persistent = false;
+  nsCOMPtr<nsIFile> file, appRootDir;
+  rv = directoryService->Get(XRE_EXECUTABLE_FILE, NS_GET_IID(nsIFile),
+                             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;
+  }
+
+  localDir = appRootDir;
+  nsAutoCString profileDir(RELATIVE_PROFILE_DIRECTORY);
+  rv = localDir->SetRelativePath(localDir.get(), profileDir);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  if (aLocal) {
+    rv = localDir->AppendNative("Caches"_ns);
+    NS_ENSURE_SUCCESS(rv, rv);
+  }
+
+#elif defined(MOZ_WIDGET_COCOA)
   FSRef fsRef;
   OSType folderType =
       aLocal ? (OSType)kCachedDataFolderType : (OSType)kDomainLibraryFolderType;
@@ -286,10 +318,6 @@ nsresult nsAppFileLocationProvider::GetProductDirectory(nsIFile** aLocalFile,
 #  error dont_know_how_to_get_product_dir_on_your_platform
 #endif
 
-  rv = localDir->AppendRelativeNativePath(DEFAULT_PRODUCT_DIR);
-  if (NS_FAILED(rv)) {
-    return rv;
-  }
   rv = localDir->Exists(&exists);
 
   if (NS_SUCCEEDED(rv) && !exists) {
@@ -308,10 +336,6 @@ nsresult nsAppFileLocationProvider::GetProductDirectory(nsIFile** aLocalFile,
 //----------------------------------------------------------------------------------------
 // GetDefaultUserProfileRoot - Gets the directory which contains each user
 // profile dir
-//
-// UNIX   : ~/.mozilla/
-// WIN    : <Application Data folder on user's machine>\Mozilla\Profiles
-// Mac    : :Documents:Mozilla:Profiles:
 //----------------------------------------------------------------------------------------
 nsresult nsAppFileLocationProvider::GetDefaultUserProfileRoot(
     nsIFile** aLocalFile, bool aLocal) {
@@ -327,23 +351,6 @@ nsresult nsAppFileLocationProvider::GetDefaultUserProfileRoot(
     return rv;
   }
 
-#if defined(MOZ_WIDGET_COCOA) || defined(XP_WIN)
-  // These 3 platforms share this part of the path - do them as one
-  rv = localDir->AppendRelativeNativePath("Profiles"_ns);
-  if (NS_FAILED(rv)) {
-    return rv;
-  }
-
-  bool exists;
-  rv = localDir->Exists(&exists);
-  if (NS_SUCCEEDED(rv) && !exists) {
-    rv = localDir->Create(nsIFile::DIRECTORY_TYPE, 0775);
-  }
-  if (NS_FAILED(rv)) {
-    return rv;
-  }
-#endif
-
   localDir.forget(aLocalFile);
 
   return rv;

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the tor-commits mailing list