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"
12 #include "nsIDirectoryService.h"
15 #include "nsSimpleEnumerator.h"
18 #if defined(MOZ_WIDGET_COCOA)
19 # include <Carbon/Carbon.h>
20 # include "CocoaFileUtils.h"
21 # include "nsILocalFileMac.h"
25 #elif defined(XP_UNIX)
28 # include <sys/param.h>
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
38 # define APP_REGISTRY_NAME "registry.dat"_ns
40 # define APP_REGISTRY_NAME "appreg"_ns
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 //*****************************************************************************
68 nsAppFileLocationProvider::GetFile(const char* aProp
, bool* aPersistent
,
70 if (NS_WARN_IF(!aProp
)) {
71 return NS_ERROR_INVALID_ARG
;
74 nsCOMPtr
<nsIFile
> localFile
;
75 nsresult rv
= NS_ERROR_FAILURE
;
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
);
128 nsresult
nsAppFileLocationProvider::CloneMozBinDirectory(nsIFile
** aLocalFile
) {
129 if (NS_WARN_IF(!aLocalFile
)) {
130 return NS_ERROR_INVALID_ARG
;
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
));
146 directoryService
->Get(NS_XPCOM_CURRENT_PROCESS_DIR
, NS_GET_IID(nsIFile
),
147 getter_AddRefs(mMozBinDirectory
));
149 rv
= directoryService
->Get(NS_OS_CURRENT_PROCESS_DIR
, NS_GET_IID(nsIFile
),
150 getter_AddRefs(mMozBinDirectory
));
157 nsCOMPtr
<nsIFile
> aFile
;
158 rv
= mMozBinDirectory
->Clone(getter_AddRefs(aFile
));
163 NS_IF_ADDREF(*aLocalFile
= aFile
);
167 //----------------------------------------------------------------------------------------
168 // GetProductDirectory - Gets the directory which contains the application data
171 // UNIX : ~/.mozilla/
172 // WIN : <Application Data folder on user's machine>\Mozilla
173 // Mac : :Documents:Mozilla:
174 //----------------------------------------------------------------------------------------
175 nsresult
nsAppFileLocationProvider::GetProductDirectory(nsIFile
** aLocalFile
,
177 if (NS_WARN_IF(!aLocalFile
)) {
178 return NS_ERROR_INVALID_ARG
;
183 nsCOMPtr
<nsIFile
> localDir
;
185 #if defined(MOZ_WIDGET_COCOA)
186 NS_NewLocalFile(u
""_ns
, true, getter_AddRefs(localDir
));
188 return NS_ERROR_FAILURE
;
190 nsCOMPtr
<nsILocalFileMac
> localDirMac(do_QueryInterface(localDir
));
192 rv
= localDirMac
->InitWithCFURL(
193 CocoaFileUtils::GetProductDirectoryCFURLRef(aLocal
));
197 #elif defined(XP_WIN)
198 nsCOMPtr
<nsIProperties
> directoryService
=
199 do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID
, &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
));
209 #elif defined(XP_UNIX)
210 rv
= NS_NewNativeLocalFile(nsDependentCString(PR_GetEnv("HOME")), true,
211 getter_AddRefs(localDir
));
216 # error dont_know_how_to_get_product_dir_on_your_platform
219 rv
= localDir
->AppendRelativeNativePath(DEFAULT_PRODUCT_DIR
);
223 rv
= localDir
->Exists(&exists
);
225 if (NS_SUCCEEDED(rv
) && !exists
) {
226 rv
= localDir
->Create(nsIFile::DIRECTORY_TYPE
, 0700);
233 localDir
.forget(aLocalFile
);
238 //----------------------------------------------------------------------------------------
239 // GetDefaultUserProfileRoot - Gets the directory which contains each user
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
;
253 nsCOMPtr
<nsIFile
> localDir
;
255 rv
= GetProductDirectory(getter_AddRefs(localDir
), aLocal
);
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
);
268 rv
= localDir
->Exists(&exists
);
269 if (NS_SUCCEEDED(rv
) && !exists
) {
270 rv
= localDir
->Create(nsIFile::DIRECTORY_TYPE
, 0775);
277 localDir
.forget(aLocalFile
);
282 //*****************************************************************************
283 // nsAppFileLocationProvider::nsIDirectoryServiceProvider
284 //*****************************************************************************
286 class nsAppDirectoryEnumerator
: public nsSimpleEnumerator
{
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
) {
301 nsCOMPtr
<nsIFile
> testFile
;
302 (void)mProvider
->GetFile(*mCurrentKey
++, &dontCare
,
303 getter_AddRefs(testFile
));
306 *aResult
= mNext
!= nullptr;
310 NS_IMETHOD
GetNext(nsISupports
** aResult
) override
{
311 if (NS_WARN_IF(!aResult
)) {
312 return NS_ERROR_INVALID_ARG
;
317 HasMoreElements(&hasMore
);
319 return NS_ERROR_FAILURE
;
323 NS_IF_ADDREF(*aResult
);
326 return *aResult
? NS_OK
: NS_ERROR_FAILURE
;
330 nsCOMPtr
<nsIDirectoryServiceProvider
> mProvider
;
331 const char** mCurrentKey
;
332 nsCOMPtr
<nsIFile
> mNext
;