Bug 1867190 - Add prefs for PHC probablities r=glandium
[gecko.git] / xpcom / io / nsAppFileLocationProvider.cpp
blob189f1efe130f404c09fc9697a9e4ea656d1159e1
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsAppFileLocationProvider.h"
8 #include "nsAppDirectoryServiceDefs.h"
9 #include "nsDirectoryServiceDefs.h"
10 #include "nsEnumeratorUtils.h"
11 #include "nsAtom.h"
12 #include "nsIDirectoryService.h"
13 #include "nsIFile.h"
14 #include "nsString.h"
15 #include "nsSimpleEnumerator.h"
16 #include "prenv.h"
17 #include "nsCRT.h"
18 #if defined(MOZ_WIDGET_COCOA)
19 # include <Carbon/Carbon.h>
20 # include "CocoaFileUtils.h"
21 # include "nsILocalFileMac.h"
22 #elif defined(XP_WIN)
23 # include <windows.h>
24 # include <shlobj.h>
25 #elif defined(XP_UNIX)
26 # include <unistd.h>
27 # include <stdlib.h>
28 # include <sys/param.h>
29 #endif
31 // WARNING: These hard coded names need to go away. They need to
32 // come from localizable resources
34 #if defined(MOZ_WIDGET_COCOA)
35 # define APP_REGISTRY_NAME "Application Registry"_ns
36 # define ESSENTIAL_FILES "Essential Files"_ns
37 #elif defined(XP_WIN)
38 # define APP_REGISTRY_NAME "registry.dat"_ns
39 #else
40 # define APP_REGISTRY_NAME "appreg"_ns
41 #endif
43 // define default product directory
44 #define DEFAULT_PRODUCT_DIR nsLiteralCString(MOZ_USER_DIR)
46 #define DEFAULTS_DIR_NAME "defaults"_ns
47 #define DEFAULTS_PREF_DIR_NAME "pref"_ns
48 #define RES_DIR_NAME "res"_ns
49 #define CHROME_DIR_NAME "chrome"_ns
51 //*****************************************************************************
52 // nsAppFileLocationProvider::Constructor/Destructor
53 //*****************************************************************************
55 nsAppFileLocationProvider::nsAppFileLocationProvider() = default;
57 //*****************************************************************************
58 // nsAppFileLocationProvider::nsISupports
59 //*****************************************************************************
61 NS_IMPL_ISUPPORTS(nsAppFileLocationProvider, nsIDirectoryServiceProvider)
63 //*****************************************************************************
64 // nsAppFileLocationProvider::nsIDirectoryServiceProvider
65 //*****************************************************************************
67 NS_IMETHODIMP
68 nsAppFileLocationProvider::GetFile(const char* aProp, bool* aPersistent,
69 nsIFile** aResult) {
70 if (NS_WARN_IF(!aProp)) {
71 return NS_ERROR_INVALID_ARG;
74 nsCOMPtr<nsIFile> localFile;
75 nsresult rv = NS_ERROR_FAILURE;
77 *aResult = nullptr;
78 *aPersistent = true;
80 if (nsCRT::strcmp(aProp, NS_APP_APPLICATION_REGISTRY_DIR) == 0) {
81 rv = GetProductDirectory(getter_AddRefs(localFile));
82 } else if (nsCRT::strcmp(aProp, NS_APP_APPLICATION_REGISTRY_FILE) == 0) {
83 rv = GetProductDirectory(getter_AddRefs(localFile));
84 if (NS_SUCCEEDED(rv)) {
85 rv = localFile->AppendNative(APP_REGISTRY_NAME);
87 } else if (nsCRT::strcmp(aProp, NS_APP_DEFAULTS_50_DIR) == 0) {
88 rv = CloneMozBinDirectory(getter_AddRefs(localFile));
89 if (NS_SUCCEEDED(rv)) {
90 rv = localFile->AppendRelativeNativePath(DEFAULTS_DIR_NAME);
92 } else if (nsCRT::strcmp(aProp, NS_APP_PREF_DEFAULTS_50_DIR) == 0) {
93 rv = CloneMozBinDirectory(getter_AddRefs(localFile));
94 if (NS_SUCCEEDED(rv)) {
95 rv = localFile->AppendRelativeNativePath(DEFAULTS_DIR_NAME);
96 if (NS_SUCCEEDED(rv)) {
97 rv = localFile->AppendRelativeNativePath(DEFAULTS_PREF_DIR_NAME);
100 } else if (nsCRT::strcmp(aProp, NS_APP_USER_PROFILES_ROOT_DIR) == 0) {
101 rv = GetDefaultUserProfileRoot(getter_AddRefs(localFile));
102 } else if (nsCRT::strcmp(aProp, NS_APP_USER_PROFILES_LOCAL_ROOT_DIR) == 0) {
103 rv = GetDefaultUserProfileRoot(getter_AddRefs(localFile), true);
104 } else if (nsCRT::strcmp(aProp, NS_APP_RES_DIR) == 0) {
105 rv = CloneMozBinDirectory(getter_AddRefs(localFile));
106 if (NS_SUCCEEDED(rv)) {
107 rv = localFile->AppendRelativeNativePath(RES_DIR_NAME);
109 } else if (nsCRT::strcmp(aProp, NS_APP_CHROME_DIR) == 0) {
110 rv = CloneMozBinDirectory(getter_AddRefs(localFile));
111 if (NS_SUCCEEDED(rv)) {
112 rv = localFile->AppendRelativeNativePath(CHROME_DIR_NAME);
114 } else if (nsCRT::strcmp(aProp, NS_APP_INSTALL_CLEANUP_DIR) == 0) {
115 // This is cloned so that embeddors will have a hook to override
116 // with their own cleanup dir. See bugzilla bug #105087
117 rv = CloneMozBinDirectory(getter_AddRefs(localFile));
120 if (localFile && NS_SUCCEEDED(rv)) {
121 localFile.forget(aResult);
122 return NS_OK;
125 return rv;
128 nsresult nsAppFileLocationProvider::CloneMozBinDirectory(nsIFile** aLocalFile) {
129 if (NS_WARN_IF(!aLocalFile)) {
130 return NS_ERROR_INVALID_ARG;
132 nsresult rv;
134 if (!mMozBinDirectory) {
135 // Get the mozilla bin directory
136 // 1. Check the directory service first for NS_XPCOM_CURRENT_PROCESS_DIR
137 // This will be set if a directory was passed to NS_InitXPCOM
138 // 2. If that doesn't work, set it to be the current process directory
139 nsCOMPtr<nsIProperties> directoryService(
140 do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv));
141 if (NS_FAILED(rv)) {
142 return rv;
145 rv =
146 directoryService->Get(NS_XPCOM_CURRENT_PROCESS_DIR, NS_GET_IID(nsIFile),
147 getter_AddRefs(mMozBinDirectory));
148 if (NS_FAILED(rv)) {
149 rv = directoryService->Get(NS_OS_CURRENT_PROCESS_DIR, NS_GET_IID(nsIFile),
150 getter_AddRefs(mMozBinDirectory));
151 if (NS_FAILED(rv)) {
152 return rv;
157 nsCOMPtr<nsIFile> aFile;
158 rv = mMozBinDirectory->Clone(getter_AddRefs(aFile));
159 if (NS_FAILED(rv)) {
160 return rv;
163 NS_IF_ADDREF(*aLocalFile = aFile);
164 return NS_OK;
167 //----------------------------------------------------------------------------------------
168 // GetProductDirectory - Gets the directory which contains the application data
169 // folder
171 // UNIX : ~/.mozilla/
172 // WIN : <Application Data folder on user's machine>\Mozilla
173 // Mac : :Documents:Mozilla:
174 //----------------------------------------------------------------------------------------
175 nsresult nsAppFileLocationProvider::GetProductDirectory(nsIFile** aLocalFile,
176 bool aLocal) {
177 if (NS_WARN_IF(!aLocalFile)) {
178 return NS_ERROR_INVALID_ARG;
181 nsresult rv;
182 bool exists;
183 nsCOMPtr<nsIFile> localDir;
185 #if defined(MOZ_WIDGET_COCOA)
186 NS_NewLocalFile(u""_ns, true, getter_AddRefs(localDir));
187 if (!localDir) {
188 return NS_ERROR_FAILURE;
190 nsCOMPtr<nsILocalFileMac> localDirMac(do_QueryInterface(localDir));
192 rv = localDirMac->InitWithCFURL(
193 CocoaFileUtils::GetProductDirectory(aLocal).get());
194 if (NS_FAILED(rv)) {
195 return rv;
197 #elif defined(XP_WIN)
198 nsCOMPtr<nsIProperties> directoryService =
199 do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv);
200 if (NS_FAILED(rv)) {
201 return rv;
203 const char* prop = aLocal ? NS_WIN_LOCAL_APPDATA_DIR : NS_WIN_APPDATA_DIR;
204 rv = directoryService->Get(prop, NS_GET_IID(nsIFile),
205 getter_AddRefs(localDir));
206 if (NS_FAILED(rv)) {
207 return rv;
209 #elif defined(XP_UNIX)
210 rv = NS_NewNativeLocalFile(nsDependentCString(PR_GetEnv("HOME")), true,
211 getter_AddRefs(localDir));
212 if (NS_FAILED(rv)) {
213 return rv;
215 #else
216 # error dont_know_how_to_get_product_dir_on_your_platform
217 #endif
219 rv = localDir->AppendRelativeNativePath(DEFAULT_PRODUCT_DIR);
220 if (NS_FAILED(rv)) {
221 return rv;
223 rv = localDir->Exists(&exists);
225 if (NS_SUCCEEDED(rv) && !exists) {
226 rv = localDir->Create(nsIFile::DIRECTORY_TYPE, 0700);
229 if (NS_FAILED(rv)) {
230 return rv;
233 localDir.forget(aLocalFile);
235 return rv;
238 //----------------------------------------------------------------------------------------
239 // GetDefaultUserProfileRoot - Gets the directory which contains each user
240 // profile dir
242 // UNIX : ~/.mozilla/
243 // WIN : <Application Data folder on user's machine>\Mozilla\Profiles
244 // Mac : :Documents:Mozilla:Profiles:
245 //----------------------------------------------------------------------------------------
246 nsresult nsAppFileLocationProvider::GetDefaultUserProfileRoot(
247 nsIFile** aLocalFile, bool aLocal) {
248 if (NS_WARN_IF(!aLocalFile)) {
249 return NS_ERROR_INVALID_ARG;
252 nsresult rv;
253 nsCOMPtr<nsIFile> localDir;
255 rv = GetProductDirectory(getter_AddRefs(localDir), aLocal);
256 if (NS_FAILED(rv)) {
257 return rv;
260 #if defined(MOZ_WIDGET_COCOA) || defined(XP_WIN)
261 // These 3 platforms share this part of the path - do them as one
262 rv = localDir->AppendRelativeNativePath("Profiles"_ns);
263 if (NS_FAILED(rv)) {
264 return rv;
267 bool exists;
268 rv = localDir->Exists(&exists);
269 if (NS_SUCCEEDED(rv) && !exists) {
270 rv = localDir->Create(nsIFile::DIRECTORY_TYPE, 0775);
272 if (NS_FAILED(rv)) {
273 return rv;
275 #endif
277 localDir.forget(aLocalFile);
279 return rv;
282 //*****************************************************************************
283 // nsAppFileLocationProvider::nsIDirectoryServiceProvider
284 //*****************************************************************************
286 class nsAppDirectoryEnumerator : public nsSimpleEnumerator {
287 public:
289 * aKeyList is a null-terminated list of properties which are provided by
290 * aProvider They do not need to be publicly defined keys.
292 nsAppDirectoryEnumerator(nsIDirectoryServiceProvider* aProvider,
293 const char* aKeyList[])
294 : mProvider(aProvider), mCurrentKey(aKeyList) {}
296 const nsID& DefaultInterface() override { return NS_GET_IID(nsIFile); }
298 NS_IMETHOD HasMoreElements(bool* aResult) override {
299 while (!mNext && *mCurrentKey) {
300 bool dontCare;
301 nsCOMPtr<nsIFile> testFile;
302 (void)mProvider->GetFile(*mCurrentKey++, &dontCare,
303 getter_AddRefs(testFile));
304 mNext = testFile;
306 *aResult = mNext != nullptr;
307 return NS_OK;
310 NS_IMETHOD GetNext(nsISupports** aResult) override {
311 if (NS_WARN_IF(!aResult)) {
312 return NS_ERROR_INVALID_ARG;
314 *aResult = nullptr;
316 bool hasMore;
317 HasMoreElements(&hasMore);
318 if (!hasMore) {
319 return NS_ERROR_FAILURE;
322 *aResult = mNext;
323 NS_IF_ADDREF(*aResult);
324 mNext = nullptr;
326 return *aResult ? NS_OK : NS_ERROR_FAILURE;
329 protected:
330 nsCOMPtr<nsIDirectoryServiceProvider> mProvider;
331 const char** mCurrentKey;
332 nsCOMPtr<nsIFile> mNext;