Ticket #9173: 9173-profile-location-patch.txt

File 9173-profile-location-patch.txt, 14.9 KB (added by mcs, 6 years ago)

proposed fix (change Firefox user data location)

Line 
1diff --git a/toolkit/xre/nsXREDirProvider.cpp b/toolkit/xre/nsXREDirProvider.cpp
2index ecf56c2..8432cca 100644
3--- a/toolkit/xre/nsXREDirProvider.cpp
4+++ b/toolkit/xre/nsXREDirProvider.cpp
5@@ -26,16 +26,17 @@
6 #include "nsCategoryManagerUtils.h"
7 
8 #include "nsINIParser.h"
9 #include "nsDependentString.h"
10 #include "nsCOMArray.h"
11 #include "nsArrayEnumerator.h"
12 #include "nsEnumeratorUtils.h"
13 #include "nsReadableUtils.h"
14+#include "nsXPCOMPrivate.h"  // for XPCOM_FILE_PATH_SEPARATOR
15 #include "mozilla/Services.h"
16 #include "mozilla/Omnijar.h"
17 #include "mozilla/Preferences.h"
18 #include "mozilla/Telemetry.h"
19 
20 #include <stdlib.h>
21 
22 #ifdef XP_WIN
23@@ -161,19 +162,16 @@ nsXREDirProvider::GetUserProfilesRootDir(nsIFile** aResult,
24                                          const nsACString* aVendorName)
25 {
26   nsCOMPtr<nsIFile> file;
27   nsresult rv = GetUserDataDirectory(getter_AddRefs(file),
28                                      false,
29                                      aProfileName, aAppName, aVendorName);
30 
31   if (NS_SUCCEEDED(rv)) {
32-#if !defined(XP_UNIX) || defined(XP_MACOSX)
33-    rv = file->AppendNative(NS_LITERAL_CSTRING("Profiles"));
34-#endif
35     // We must create the profile directory here if it does not exist.
36     nsresult tmp = EnsureDirectoryExists(file);
37     if (NS_FAILED(tmp)) {
38       rv = tmp;
39     }
40   }
41   file.swap(*aResult);
42   return rv;
43@@ -186,19 +184,16 @@ nsXREDirProvider::GetUserProfilesLocalDir(nsIFile** aResult,
44                                           const nsACString* aVendorName)
45 {
46   nsCOMPtr<nsIFile> file;
47   nsresult rv = GetUserDataDirectory(getter_AddRefs(file),
48                                      true,
49                                      aProfileName, aAppName, aVendorName);
50 
51   if (NS_SUCCEEDED(rv)) {
52-#if !defined(XP_UNIX) || defined(XP_MACOSX)
53-    rv = file->AppendNative(NS_LITERAL_CSTRING("Profiles"));
54-#endif
55     // We must create the profile directory here if it does not exist.
56     nsresult tmp = EnsureDirectoryExists(file);
57     if (NS_FAILED(tmp)) {
58       rv = tmp;
59     }
60   }
61   file.swap(*aResult);
62   return NS_OK;
63@@ -1046,98 +1041,45 @@ nsXREDirProvider::GetProfileDir(nsIFile* *aResult)
64 
65   return NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, aResult);
66 }
67 
68 nsresult
69 nsXREDirProvider::GetUserDataDirectoryHome(nsIFile** aFile, bool aLocal)
70 {
71   // Copied from nsAppFileLocationProvider (more or less)
72-  nsresult rv;
73+  NS_ENSURE_ARG_POINTER(aFile);
74   nsCOMPtr<nsIFile> localDir;
75 
76+  nsresult rv = GetAppDir()->Clone(getter_AddRefs(localDir));
77+  NS_ENSURE_SUCCESS(rv, rv);
78+
79+  int levelsToRemove = 1;
80 #if defined(XP_MACOSX)
81-  FSRef fsRef;
82-  OSType folderType;
83-  if (aLocal) {
84-    folderType = kCachedDataFolderType;
85-  } else {
86-#ifdef MOZ_THUNDERBIRD
87-    folderType = kDomainLibraryFolderType;
88-#else
89-    folderType = kApplicationSupportFolderType;
90+  levelsToRemove += 4;
91 #endif
92+  while (localDir && (levelsToRemove > 0)) {
93+    nsCOMPtr<nsIFile> parentDir;
94+    rv = localDir->GetParent(getter_AddRefs(parentDir));
95+    NS_ENSURE_SUCCESS(rv, rv);
96+    localDir = parentDir;
97+    --levelsToRemove;
98   }
99-  OSErr err = ::FSFindFolder(kUserDomain, folderType, kCreateFolder, &fsRef);
100-  NS_ENSURE_FALSE(err, NS_ERROR_FAILURE);
101 
102-  rv = NS_NewNativeLocalFile(EmptyCString(), true, getter_AddRefs(localDir));
103-  NS_ENSURE_SUCCESS(rv, rv);
104-
105-  nsCOMPtr<nsILocalFileMac> dirFileMac = do_QueryInterface(localDir);
106-  NS_ENSURE_TRUE(dirFileMac, NS_ERROR_UNEXPECTED);
107+  if (!localDir)
108+    return NS_ERROR_FAILURE;
109 
110-  rv = dirFileMac->InitWithFSRef(&fsRef);
111+  rv = localDir->AppendRelativeNativePath(NS_LITERAL_CSTRING("Data"
112+                                     XPCOM_FILE_PATH_SEPARATOR "Browser"));
113   NS_ENSURE_SUCCESS(rv, rv);
114 
115-  localDir = do_QueryInterface(dirFileMac, &rv);
116-#elif defined(XP_WIN)
117-  nsString path;
118   if (aLocal) {
119-    rv = GetShellFolderPath(CSIDL_LOCAL_APPDATA, path);
120-    if (NS_FAILED(rv))
121-      rv = GetRegWindowsAppDataFolder(aLocal, path);
122-  }
123-  if (!aLocal || NS_FAILED(rv)) {
124-    rv = GetShellFolderPath(CSIDL_APPDATA, path);
125-    if (NS_FAILED(rv)) {
126-      if (!aLocal)
127-        rv = GetRegWindowsAppDataFolder(aLocal, path);
128-    }
129+    rv = localDir->AppendNative(NS_LITERAL_CSTRING("Caches"));
130+    NS_ENSURE_SUCCESS(rv, rv);
131   }
132-  NS_ENSURE_SUCCESS(rv, rv);
133-
134-  rv = NS_NewLocalFile(path, true, getter_AddRefs(localDir));
135-#elif defined(XP_OS2)
136-#if 0 /* For OS/2 we want to always use MOZILLA_HOME */
137-  // we want an environment variable of the form
138-  // FIREFOX_HOME, etc
139-  if (!gAppData)
140-    return NS_ERROR_FAILURE;
141-  nsDependentCString envVar(nsDependentCString(gAppData->name));
142-  envVar.Append("_HOME");
143-  char *pHome = getenv(envVar.get());
144-#endif
145-  char *pHome = getenv("MOZILLA_HOME");
146-  if (pHome && *pHome) {
147-    rv = NS_NewNativeLocalFile(nsDependentCString(pHome), true,
148-                               getter_AddRefs(localDir));
149-  } else {
150-    PPIB ppib;
151-    PTIB ptib;
152-    char appDir[CCHMAXPATH];
153-
154-    DosGetInfoBlocks(&ptib, &ppib);
155-    DosQueryModuleName(ppib->pib_hmte, CCHMAXPATH, appDir);
156-    *strrchr(appDir, '\\') = '\0';
157-    rv = NS_NewNativeLocalFile(nsDependentCString(appDir), true, getter_AddRefs(localDir));
158-  }
159-#elif defined(MOZ_WIDGET_GONK)
160-  rv = NS_NewNativeLocalFile(NS_LITERAL_CSTRING("/data/b2g"), true,
161-                             getter_AddRefs(localDir));
162-#elif defined(XP_UNIX)
163-  const char* homeDir = getenv("HOME");
164-  if (!homeDir || !*homeDir)
165-    return NS_ERROR_FAILURE;
166-
167-  rv = NS_NewNativeLocalFile(nsDependentCString(homeDir), true,
168-                             getter_AddRefs(localDir));
169-#else
170-#error "Don't know how to get product dir on your platform"
171-#endif
172 
173   NS_IF_ADDREF(*aFile = localDir);
174   return rv;
175 }
176 
177 nsresult
178 nsXREDirProvider::GetSysUserExtensionsDirectory(nsIFile** aFile)
179 {
180@@ -1341,26 +1283,18 @@ nsXREDirProvider::AppendProfilePath(nsIFile* aFile,
181     return NS_ERROR_FAILURE;
182   }
183 
184   nsCAutoString profile;
185   nsCAutoString appName;
186   nsCAutoString vendor;
187   if (aProfileName && !aProfileName->IsEmpty()) {
188     profile = *aProfileName;
189-  } else if (aAppName) {
190-    appName = *aAppName;
191-    if (aVendorName) {
192-      vendor = *aVendorName;
193-    }
194   } else if (gAppData->profile) {
195     profile = gAppData->profile;
196-  } else {
197-    appName = gAppData->name;
198-    vendor = gAppData->vendor;
199   }
200 
201   nsresult rv;
202 
203 #if defined (XP_MACOSX)
204   if (!profile.IsEmpty()) {
205     rv = AppendProfileString(aFile, profile.get());
206   }
207diff --git a/toolkit/xre/nsXREDirProvider.h b/toolkit/xre/nsXREDirProvider.h
208index 77d109f..d574f59 100644
209--- a/toolkit/xre/nsXREDirProvider.h
210+++ b/toolkit/xre/nsXREDirProvider.h
211@@ -51,26 +51,26 @@ public:
212   // We also don't fire profile-changed notifications... that is
213   // the responsibility of the apprunner.
214   nsresult SetProfile(nsIFile* aProfileDir, nsIFile* aProfileLocalDir);
215 
216   void DoShutdown();
217 
218   nsresult GetProfileDefaultsDir(nsIFile* *aResult);
219 
220-  static nsresult GetUserAppDataDirectory(nsIFile* *aFile) {
221+  nsresult GetUserAppDataDirectory(nsIFile* *aFile) {
222     return GetUserDataDirectory(aFile, false, nullptr, nullptr, nullptr);
223   }
224-  static nsresult GetUserLocalDataDirectory(nsIFile* *aFile) {
225+  nsresult GetUserLocalDataDirectory(nsIFile* *aFile) {
226     return GetUserDataDirectory(aFile, true, nullptr, nullptr, nullptr);
227   }
228 
229   // By default GetUserDataDirectory gets profile path from gAppData,
230   // but that can be overridden by using aProfileName/aAppName/aVendorName.
231-  static nsresult GetUserDataDirectory(nsIFile** aFile, bool aLocal,
232+  nsresult GetUserDataDirectory(nsIFile** aFile, bool aLocal,
233                                        const nsACString* aProfileName,
234                                        const nsACString* aAppName,
235                                        const nsACString* aVendorName);
236 
237   /* make sure you clone it, if you need to do stuff to it */
238   nsIFile* GetGREDir() { return mGREDir; }
239   nsIFile* GetAppDir() {
240     if (mXULAppDir)
241@@ -96,18 +96,18 @@ public:
242    * Get the profile directory as determined by this class or by an
243    * embedder-provided XPCOM directory provider. Only call this method
244    * when XPCOM is initialized! aResult is a clone, it may be modified.
245    */
246   nsresult GetProfileDir(nsIFile* *aResult);
247 
248 protected:
249   nsresult GetFilesInternal(const char* aProperty, nsISimpleEnumerator** aResult);
250-  static nsresult GetUserDataDirectoryHome(nsIFile* *aFile, bool aLocal);
251-  static nsresult GetSysUserExtensionsDirectory(nsIFile* *aFile);
252+  nsresult GetUserDataDirectoryHome(nsIFile* *aFile, bool aLocal);
253+  nsresult GetSysUserExtensionsDirectory(nsIFile* *aFile);
254 #if defined(XP_UNIX) || defined(XP_MACOSX)
255   static nsresult GetSystemExtensionsDirectory(nsIFile** aFile);
256 #endif
257   static nsresult EnsureDirectoryExists(nsIFile* aDirectory);
258   void EnsureProfileFileExists(nsIFile* aFile);
259 
260   // Determine the profile path within the UAppData directory. This is different
261   // on every major platform.
262diff --git a/xpcom/io/Makefile.in b/xpcom/io/Makefile.in
263index 79ad5de..3fba891 100644
264--- a/xpcom/io/Makefile.in
265+++ b/xpcom/io/Makefile.in
266@@ -157,17 +157,19 @@ include $(topsrcdir)/ipc/chromium/chromium-config.mk
267 DEFINES                += -D_IMPL_NS_COM
268 
269 ifeq ($(OS_ARCH),Linux)
270 ifneq (,$(findstring lib64,$(libdir)))
271 DEFINES     += -DHAVE_USR_LIB64_DIR
272 endif
273 endif
274 
275-LOCAL_INCLUDES += -I..
276+LOCAL_INCLUDES +=      \
277+               -I.. \
278+               -I$(srcdir)/../build
279 
280 ifeq ($(MOZ_PLATFORM_MAEMO),5)
281 CFLAGS          += $(MOZ_DBUS_CFLAGS)
282 CXXFLAGS        += $(MOZ_DBUS_CFLAGS)
283 endif
284 
285 ifdef MOZ_PLATFORM_MAEMO
286 CFLAGS          += $(MOZ_PLATFORM_MAEMO_CFLAGS) $(MOZ_QT_CFLAGS)
287diff --git a/xpcom/io/nsAppFileLocationProvider.cpp b/xpcom/io/nsAppFileLocationProvider.cpp
288index 1e667b8..3828eed 100644
289--- a/xpcom/io/nsAppFileLocationProvider.cpp
290+++ b/xpcom/io/nsAppFileLocationProvider.cpp
291@@ -8,16 +8,17 @@
292 #include "nsDirectoryServiceDefs.h"
293 #include "nsIAtom.h"
294 #include "nsIFile.h"
295 #include "nsString.h"
296 #include "nsXPIDLString.h"
297 #include "nsISimpleEnumerator.h"
298 #include "prenv.h"
299 #include "nsCRT.h"
300+#include "nsXPCOMPrivate.h"  // for XPCOM_FILE_PATH_SEPARATOR
301 
302 #if defined(MOZ_WIDGET_COCOA)
303 #include <Carbon/Carbon.h>
304 #include "nsILocalFileMac.h"
305 #elif defined(XP_OS2)
306 #define INCL_DOSPROCESS
307 #define INCL_DOSMODULEMGR
308 #include <os2.h>
309@@ -278,103 +279,81 @@ NS_METHOD nsAppFileLocationProvider::CloneMozBinDirectory(nsIFile **aLocalFile)
310     NS_IF_ADDREF(*aLocalFile = aFile);
311     return NS_OK;
312 }
313 
314 
315 //----------------------------------------------------------------------------------------
316 // GetProductDirectory - Gets the directory which contains the application data folder
317 //
318-// UNIX   : ~/.mozilla/
319-// WIN    : <Application Data folder on user's machine>\Mozilla
320-// Mac    : :Documents:Mozilla:
321+// UNIX and WIN   : <App Folder>/../Data/Browser
322+// Mac            : <App Folder>/../../../../../Data/Browser
323 //----------------------------------------------------------------------------------------
324 NS_METHOD nsAppFileLocationProvider::GetProductDirectory(nsIFile **aLocalFile, bool aLocal)
325 {
326     NS_ENSURE_ARG_POINTER(aLocalFile);
327 
328     nsresult rv;
329     bool exists;
330     nsCOMPtr<nsIFile> localDir;
331 
332-#if defined(MOZ_WIDGET_COCOA)
333-    FSRef fsRef;
334-    OSType folderType = aLocal ? (OSType) kCachedDataFolderType : (OSType) kDomainLibraryFolderType;
335-    OSErr err = ::FSFindFolder(kUserDomain, folderType, kCreateFolder, &fsRef);
336-    if (err) return NS_ERROR_FAILURE;
337-    NS_NewLocalFile(EmptyString(), true, getter_AddRefs(localDir));
338-    if (!localDir) return NS_ERROR_FAILURE;
339-    nsCOMPtr<nsILocalFileMac> localDirMac(do_QueryInterface(localDir));
340-    rv = localDirMac->InitWithFSRef(&fsRef);
341-    if (NS_FAILED(rv)) return rv;
342-#elif defined(XP_OS2)
343-    nsCOMPtr<nsIProperties> directoryService =
344-             do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv);
345-    if (NS_FAILED(rv)) return rv;
346-    rv = directoryService->Get(NS_OS2_HOME_DIR, NS_GET_IID(nsIFile), getter_AddRefs(localDir));
347-    if (NS_FAILED(rv)) return rv;
348-#elif defined(XP_WIN)
349-    nsCOMPtr<nsIProperties> directoryService =
350-             do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv);
351-    if (NS_FAILED(rv)) return rv;
352-    const char* prop = aLocal ? NS_WIN_LOCAL_APPDATA_DIR : NS_WIN_APPDATA_DIR;
353-    rv = directoryService->Get(prop, NS_GET_IID(nsIFile), getter_AddRefs(localDir));
354-    if (NS_FAILED(rv)) return rv;
355-#elif defined(XP_UNIX)
356-    rv = NS_NewNativeLocalFile(nsDependentCString(PR_GetEnv("HOME")), true, getter_AddRefs(localDir));
357-    if (NS_FAILED(rv)) return rv;
358-#else
359-#error dont_know_how_to_get_product_dir_on_your_platform
360+    rv = CloneMozBinDirectory(getter_AddRefs(localDir));
361+    NS_ENSURE_SUCCESS(rv, rv);
362+
363+    int levelsToRemove = 1;
364+#if defined(XP_MACOSX)
365+    levelsToRemove += 4;
366 #endif
367+    while (localDir && (levelsToRemove > 0)) {
368+        nsCOMPtr<nsIFile> parentDir;
369+        rv = localDir->GetParent(getter_AddRefs(parentDir));
370+        NS_ENSURE_SUCCESS(rv, rv);
371+        localDir = parentDir;
372+        --levelsToRemove;
373+    }
374+
375+    if (!localDir)
376+        return NS_ERROR_FAILURE;
377+
378+    rv = localDir->AppendRelativeNativePath(NS_LITERAL_CSTRING("Data"
379+                                       XPCOM_FILE_PATH_SEPARATOR "Browser"));
380+    NS_ENSURE_SUCCESS(rv, rv);
381+
382+    if (aLocal) {
383+        rv = localDir->AppendNative(NS_LITERAL_CSTRING("Caches"));
384+        NS_ENSURE_SUCCESS(rv, rv);
385+    }
386 
387-    rv = localDir->AppendRelativeNativePath(DEFAULT_PRODUCT_DIR);
388-    if (NS_FAILED(rv)) return rv;
389     rv = localDir->Exists(&exists);
390 
391     if (NS_SUCCEEDED(rv) && !exists)
392         rv = localDir->Create(nsIFile::DIRECTORY_TYPE, 0700);
393 
394     if (NS_FAILED(rv)) return rv;
395 
396     *aLocalFile = localDir;
397     NS_ADDREF(*aLocalFile);
398 
399    return rv;
400 }
401 
402 
403 //----------------------------------------------------------------------------------------
404 // GetDefaultUserProfileRoot - Gets the directory which contains each user profile dir
405-//
406-// UNIX   : ~/.mozilla/
407-// WIN    : <Application Data folder on user's machine>\Mozilla\Profiles
408-// Mac    : :Documents:Mozilla:Profiles:
409 //----------------------------------------------------------------------------------------
410 NS_METHOD nsAppFileLocationProvider::GetDefaultUserProfileRoot(nsIFile **aLocalFile, bool aLocal)
411 {
412     NS_ENSURE_ARG_POINTER(aLocalFile);
413 
414     nsresult rv;
415     nsCOMPtr<nsIFile> localDir;
416 
417     rv = GetProductDirectory(getter_AddRefs(localDir), aLocal);
418     if (NS_FAILED(rv)) return rv;
419 
420-#if defined(MOZ_WIDGET_COCOA) || defined(XP_OS2) || defined(XP_WIN)
421-    // These 3 platforms share this part of the path - do them as one
422-    rv = localDir->AppendRelativeNativePath(NS_LITERAL_CSTRING("Profiles"));
423-    if (NS_FAILED(rv)) return rv;
424-
425-    bool exists;
426-    rv = localDir->Exists(&exists);
427-    if (NS_SUCCEEDED(rv) && !exists)
428-        rv = localDir->Create(nsIFile::DIRECTORY_TYPE, 0775);
429-    if (NS_FAILED(rv)) return rv;
430-#endif
431-
432     *aLocalFile = localDir;
433     NS_ADDREF(*aLocalFile);
434 
435    return rv;
436 }
437 
438 //*****************************************************************************
439 // nsAppFileLocationProvider::nsIDirectoryServiceProvider2