VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/io/nsAppFileLocationProvider.cpp@ 102315

最後變更 在這個檔案從102315是 102315,由 vboxsync 提交於 16 月 前

libs/xpcom: Remove VBOX_MACOSX_FOLLOWS_UNIX_IO which is the default for a long time now and get rid of the OSX specific code which is not used, bugref:10545

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 16.6 KB
 
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is Mozilla Communicator client code, released
16 * March 31, 1998.
17 *
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 2000
21 * the Initial Developer. All Rights Reserved.
22 *
23 * Contributor(s):
24 * Conrad Carlen <[email protected]>
25 *
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
37 *
38 * ***** END LICENSE BLOCK ***** */
39
40#include "nsAppFileLocationProvider.h"
41#include "nsAppDirectoryServiceDefs.h"
42#include "nsDirectoryServiceDefs.h"
43#include "nsIAtom.h"
44#include "nsILocalFile.h"
45#include "nsString.h"
46#include "nsXPIDLString.h"
47#include "nsISimpleEnumerator.h"
48#include "nsCRT.h"
49
50#if defined(XP_UNIX)
51#include <unistd.h>
52#include <stdlib.h>
53#include <sys/param.h>
54#else
55# error "Not implemented"
56#endif
57
58#include <iprt/env.h>
59
60// WARNING: These hard coded names need to go away. They need to
61// come from localizable resources
62
63#if defined(XP_MAC) || defined(XP_MACOSX)
64#define APP_REGISTRY_NAME NS_LITERAL_CSTRING("Application Registry")
65#define ESSENTIAL_FILES NS_LITERAL_CSTRING("Essential Files")
66#elif defined(XP_WIN) || defined(XP_OS2)
67#define APP_REGISTRY_NAME NS_LITERAL_CSTRING("registry.dat")
68#else
69#define APP_REGISTRY_NAME NS_LITERAL_CSTRING("appreg")
70#endif
71
72// define default product directory
73#ifdef XP_MAC
74#define DEFAULT_PRODUCT_DIR NS_LITERAL_CSTRING("Mozilla")
75#else
76#define DEFAULT_PRODUCT_DIR NS_LITERAL_CSTRING(MOZ_USER_DIR)
77#endif
78
79// Locally defined keys used by nsAppDirectoryEnumerator
80#define NS_ENV_PLUGINS_DIR "EnvPlugins" // env var MOZ_PLUGIN_PATH
81#define NS_USER_PLUGINS_DIR "UserPlugins"
82
83#if defined(XP_MAC) || defined(XP_MACOSX)
84#define NS_MACOSX_USER_PLUGIN_DIR "OSXUserPlugins"
85#define NS_MACOSX_LOCAL_PLUGIN_DIR "OSXLocalPlugins"
86#define NS_MAC_CLASSIC_PLUGIN_DIR "MacSysPlugins"
87#endif
88
89#if defined(XP_MAC)
90#define DEFAULTS_DIR_NAME NS_LITERAL_CSTRING("Defaults")
91#define DEFAULTS_PREF_DIR_NAME NS_LITERAL_CSTRING("Pref")
92#define DEFAULTS_PROFILE_DIR_NAME NS_LITERAL_CSTRING("Profile")
93#define RES_DIR_NAME NS_LITERAL_CSTRING("Res")
94#define CHROME_DIR_NAME NS_LITERAL_CSTRING("Chrome")
95#define PLUGINS_DIR_NAME NS_LITERAL_CSTRING("Plug-ins")
96#define SEARCH_DIR_NAME NS_LITERAL_CSTRING("Search Plugins")
97#else
98#define DEFAULTS_DIR_NAME NS_LITERAL_CSTRING("defaults")
99#define DEFAULTS_PREF_DIR_NAME NS_LITERAL_CSTRING("pref")
100#define DEFAULTS_PROFILE_DIR_NAME NS_LITERAL_CSTRING("profile")
101#define RES_DIR_NAME NS_LITERAL_CSTRING("res")
102#define CHROME_DIR_NAME NS_LITERAL_CSTRING("chrome")
103#define PLUGINS_DIR_NAME NS_LITERAL_CSTRING("plugins")
104#define SEARCH_DIR_NAME NS_LITERAL_CSTRING("searchplugins")
105#endif
106
107//*****************************************************************************
108// nsAppFileLocationProvider::Constructor/Destructor
109//*****************************************************************************
110
111nsAppFileLocationProvider::nsAppFileLocationProvider()
112{
113}
114
115//*****************************************************************************
116// nsAppFileLocationProvider::nsISupports
117//*****************************************************************************
118
119NS_IMPL_THREADSAFE_ISUPPORTS2(nsAppFileLocationProvider, nsIDirectoryServiceProvider, nsIDirectoryServiceProvider2)
120
121//*****************************************************************************
122// nsAppFileLocationProvider::nsIDirectoryServiceProvider
123//*****************************************************************************
124
125NS_IMETHODIMP
126nsAppFileLocationProvider::GetFile(const char *prop, PRBool *persistant, nsIFile **_retval)
127{
128 nsCOMPtr<nsILocalFile> localFile;
129 nsresult rv = NS_ERROR_FAILURE;
130
131 NS_ENSURE_ARG(prop);
132 *_retval = nsnull;
133 *persistant = PR_TRUE;
134
135 if (nsCRT::strcmp(prop, NS_APP_APPLICATION_REGISTRY_DIR) == 0)
136 {
137 rv = GetProductDirectory(getter_AddRefs(localFile));
138 }
139 else if (nsCRT::strcmp(prop, NS_APP_APPLICATION_REGISTRY_FILE) == 0)
140 {
141 rv = GetProductDirectory(getter_AddRefs(localFile));
142 if (NS_SUCCEEDED(rv))
143 rv = localFile->AppendNative(APP_REGISTRY_NAME);
144 }
145 else if (nsCRT::strcmp(prop, NS_APP_DEFAULTS_50_DIR) == 0)
146 {
147 rv = CloneMozBinDirectory(getter_AddRefs(localFile));
148 if (NS_SUCCEEDED(rv))
149 rv = localFile->AppendRelativeNativePath(DEFAULTS_DIR_NAME);
150 }
151 else if (nsCRT::strcmp(prop, NS_APP_PREF_DEFAULTS_50_DIR) == 0)
152 {
153 rv = CloneMozBinDirectory(getter_AddRefs(localFile));
154 if (NS_SUCCEEDED(rv)) {
155 rv = localFile->AppendRelativeNativePath(DEFAULTS_DIR_NAME);
156 if (NS_SUCCEEDED(rv))
157 rv = localFile->AppendRelativeNativePath(DEFAULTS_PREF_DIR_NAME);
158 }
159 }
160 else if (nsCRT::strcmp(prop, NS_APP_PROFILE_DEFAULTS_50_DIR) == 0 ||
161 nsCRT::strcmp(prop, NS_APP_PROFILE_DEFAULTS_NLOC_50_DIR) == 0)
162 {
163 rv = CloneMozBinDirectory(getter_AddRefs(localFile));
164 if (NS_SUCCEEDED(rv)) {
165 rv = localFile->AppendRelativeNativePath(DEFAULTS_DIR_NAME);
166 if (NS_SUCCEEDED(rv))
167 rv = localFile->AppendRelativeNativePath(DEFAULTS_PROFILE_DIR_NAME);
168 }
169 }
170 else if (nsCRT::strcmp(prop, NS_APP_USER_PROFILES_ROOT_DIR) == 0)
171 {
172 rv = GetDefaultUserProfileRoot(getter_AddRefs(localFile));
173 }
174 else if (nsCRT::strcmp(prop, NS_APP_RES_DIR) == 0)
175 {
176 rv = CloneMozBinDirectory(getter_AddRefs(localFile));
177 if (NS_SUCCEEDED(rv))
178 rv = localFile->AppendRelativeNativePath(RES_DIR_NAME);
179 }
180 else if (nsCRT::strcmp(prop, NS_APP_CHROME_DIR) == 0)
181 {
182 rv = CloneMozBinDirectory(getter_AddRefs(localFile));
183 if (NS_SUCCEEDED(rv))
184 rv = localFile->AppendRelativeNativePath(CHROME_DIR_NAME);
185 }
186 else if (nsCRT::strcmp(prop, NS_APP_PLUGINS_DIR) == 0)
187 {
188 rv = CloneMozBinDirectory(getter_AddRefs(localFile));
189 if (NS_SUCCEEDED(rv))
190 rv = localFile->AppendRelativeNativePath(PLUGINS_DIR_NAME);
191 }
192 else if (nsCRT::strcmp(prop, NS_ENV_PLUGINS_DIR) == 0)
193 {
194 NS_ERROR("Don't use nsAppFileLocationProvider::GetFile(NS_ENV_PLUGINS_DIR, ...). "
195 "Use nsAppFileLocationProvider::GetFiles(...).");
196 const char *pathVar = RTEnvGet("VBOX_XPCOM_PLUGIN_PATH");
197 if (pathVar)
198 rv = NS_NewNativeLocalFile(nsDependentCString(pathVar), PR_TRUE, getter_AddRefs(localFile));
199 }
200 else if (nsCRT::strcmp(prop, NS_USER_PLUGINS_DIR) == 0)
201 {
202 rv = GetProductDirectory(getter_AddRefs(localFile));
203 if (NS_SUCCEEDED(rv))
204 rv = localFile->AppendRelativeNativePath(PLUGINS_DIR_NAME);
205 }
206 else if (nsCRT::strcmp(prop, NS_APP_SEARCH_DIR) == 0)
207 {
208 rv = CloneMozBinDirectory(getter_AddRefs(localFile));
209 if (NS_SUCCEEDED(rv))
210 rv = localFile->AppendRelativeNativePath(SEARCH_DIR_NAME);
211 }
212 else if (nsCRT::strcmp(prop, NS_APP_INSTALL_CLEANUP_DIR) == 0)
213 {
214 // This is cloned so that embeddors will have a hook to override
215 // with their own cleanup dir. See bugzilla bug #105087
216 rv = CloneMozBinDirectory(getter_AddRefs(localFile));
217 }
218
219 if (localFile && NS_SUCCEEDED(rv))
220 return localFile->QueryInterface(NS_GET_IID(nsIFile), (void**)_retval);
221
222 return rv;
223}
224
225
226NS_METHOD nsAppFileLocationProvider::CloneMozBinDirectory(nsILocalFile **aLocalFile)
227{
228 NS_ENSURE_ARG_POINTER(aLocalFile);
229 nsresult rv;
230
231 if (!mMozBinDirectory)
232 {
233 // Get the mozilla bin directory
234 // 1. Check the directory service first for NS_XPCOM_CURRENT_PROCESS_DIR
235 // This will be set if a directory was passed to NS_InitXPCOM
236 // 2. If that doesn't work, set it to be the current process directory
237 nsCOMPtr<nsIProperties>
238 directoryService(do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv));
239 if (NS_FAILED(rv))
240 return rv;
241
242 rv = directoryService->Get(NS_XPCOM_CURRENT_PROCESS_DIR, NS_GET_IID(nsIFile), getter_AddRefs(mMozBinDirectory));
243 if (NS_FAILED(rv)) {
244 rv = directoryService->Get(NS_OS_CURRENT_PROCESS_DIR, NS_GET_IID(nsIFile), getter_AddRefs(mMozBinDirectory));
245 if (NS_FAILED(rv))
246 return rv;
247 }
248 }
249
250 nsCOMPtr<nsIFile> aFile;
251 rv = mMozBinDirectory->Clone(getter_AddRefs(aFile));
252 if (NS_FAILED(rv))
253 return rv;
254
255 nsCOMPtr<nsILocalFile> lfile = do_QueryInterface (aFile);
256 if (!lfile)
257 return NS_ERROR_FAILURE;
258
259 NS_IF_ADDREF(*aLocalFile = lfile);
260 return NS_OK;
261}
262
263
264//----------------------------------------------------------------------------------------
265// GetProductDirectory - Gets the directory which contains the application data folder
266//
267// UNIX : ~/.mozilla/
268// WIN : <Application Data folder on user's machine>\Mozilla
269// Mac : :Documents:Mozilla:
270//----------------------------------------------------------------------------------------
271NS_METHOD nsAppFileLocationProvider::GetProductDirectory(nsILocalFile **aLocalFile)
272{
273 NS_ENSURE_ARG_POINTER(aLocalFile);
274
275 nsresult rv;
276 PRBool exists;
277 nsCOMPtr<nsILocalFile> localDir;
278
279#if defined(XP_UNIX)
280 rv = NS_NewNativeLocalFile(nsDependentCString(RTEnvGet("HOME")), PR_TRUE, getter_AddRefs(localDir));
281 if (NS_FAILED(rv)) return rv;
282#else
283#error dont_know_how_to_get_product_dir_on_your_platform
284#endif
285
286 rv = localDir->AppendRelativeNativePath(DEFAULT_PRODUCT_DIR);
287 if (NS_FAILED(rv)) return rv;
288 rv = localDir->Exists(&exists);
289 if (NS_SUCCEEDED(rv) && !exists)
290 rv = localDir->Create(nsIFile::DIRECTORY_TYPE, 0700);
291 if (NS_FAILED(rv)) return rv;
292
293 *aLocalFile = localDir;
294 NS_ADDREF(*aLocalFile);
295
296 return rv;
297}
298
299
300//----------------------------------------------------------------------------------------
301// GetDefaultUserProfileRoot - Gets the directory which contains each user profile dir
302//
303// UNIX : ~/.mozilla/
304// WIN : <Application Data folder on user's machine>\Mozilla\Profiles
305// Mac : :Documents:Mozilla:Profiles:
306//----------------------------------------------------------------------------------------
307NS_METHOD nsAppFileLocationProvider::GetDefaultUserProfileRoot(nsILocalFile **aLocalFile)
308{
309 NS_ENSURE_ARG_POINTER(aLocalFile);
310
311 nsresult rv;
312 nsCOMPtr<nsILocalFile> localDir;
313
314 rv = GetProductDirectory(getter_AddRefs(localDir));
315 if (NS_FAILED(rv)) return rv;
316
317#if defined(XP_MAC) || defined(XP_MACOSX) || defined(XP_OS2) || defined(XP_WIN)
318 // These 3 platforms share this part of the path - do them as one
319 rv = localDir->AppendRelativeNativePath(NS_LITERAL_CSTRING("Profiles"));
320 if (NS_FAILED(rv)) return rv;
321
322 PRBool exists;
323 rv = localDir->Exists(&exists);
324 if (NS_SUCCEEDED(rv) && !exists)
325 rv = localDir->Create(nsIFile::DIRECTORY_TYPE, 0775);
326 if (NS_FAILED(rv)) return rv;
327#endif
328
329 *aLocalFile = localDir;
330 NS_ADDREF(*aLocalFile);
331
332 return rv;
333}
334
335//*****************************************************************************
336// nsAppFileLocationProvider::nsIDirectoryServiceProvider2
337//*****************************************************************************
338
339class nsAppDirectoryEnumerator : public nsISimpleEnumerator
340{
341 public:
342 NS_DECL_ISUPPORTS
343
344 /**
345 * aKeyList is a null-terminated list of properties which are provided by aProvider
346 * They do not need to be publicly defined keys.
347 */
348 nsAppDirectoryEnumerator(nsIDirectoryServiceProvider *aProvider,
349 const char* aKeyList[]) :
350 mProvider(aProvider),
351 mCurrentKey(aKeyList)
352 {
353 }
354
355 NS_IMETHOD HasMoreElements(PRBool *result)
356 {
357 while (!mNext && *mCurrentKey)
358 {
359 PRBool dontCare;
360 nsCOMPtr<nsIFile> testFile;
361 (void)mProvider->GetFile(*mCurrentKey++, &dontCare, getter_AddRefs(testFile));
362 // Don't return a file which does not exist.
363 PRBool exists;
364 if (testFile && NS_SUCCEEDED(testFile->Exists(&exists)) && exists)
365 mNext = testFile;
366 }
367 *result = mNext != nsnull;
368 return NS_OK;
369 }
370
371 NS_IMETHOD GetNext(nsISupports **result)
372 {
373 NS_ENSURE_ARG_POINTER(result);
374 *result = nsnull;
375
376 PRBool hasMore;
377 HasMoreElements(&hasMore);
378 if (!hasMore)
379 return NS_ERROR_FAILURE;
380
381 *result = mNext;
382 NS_IF_ADDREF(*result);
383 mNext = nsnull;
384
385 return *result ? NS_OK : NS_ERROR_FAILURE;
386 }
387
388 // Virtual destructor since subclass nsPathsDirectoryEnumerator
389 // does not re-implement Release()
390
391 virtual ~nsAppDirectoryEnumerator()
392 {
393 }
394
395 protected:
396 nsIDirectoryServiceProvider *mProvider;
397 const char** mCurrentKey;
398 nsCOMPtr<nsIFile> mNext;
399};
400
401NS_IMPL_ISUPPORTS1(nsAppDirectoryEnumerator, nsISimpleEnumerator)
402
403/* nsPathsDirectoryEnumerator and PATH_SEPARATOR
404 * are not used on MacOS/X. */
405
406#if defined(XP_WIN) || defined(XP_OS2)/* Win32, Win16, and OS/2 */
407#define PATH_SEPARATOR ';'
408#else /*if defined(XP_UNIX) || defined(XP_BEOS)*/
409#define PATH_SEPARATOR ':'
410#endif
411
412class nsPathsDirectoryEnumerator : public nsAppDirectoryEnumerator
413{
414 public:
415 /**
416 * aKeyList is a null-terminated list.
417 * The first element is a path list.
418 * The remainder are properties provided by aProvider.
419 * They do not need to be publicly defined keys.
420 */
421 nsPathsDirectoryEnumerator(nsIDirectoryServiceProvider *aProvider,
422 const char* aKeyList[]) :
423 nsAppDirectoryEnumerator(aProvider, aKeyList+1),
424 mEndPath(aKeyList[0])
425 {
426 }
427
428 NS_IMETHOD HasMoreElements(PRBool *result)
429 {
430 if (mEndPath)
431 while (!mNext && *mEndPath)
432 {
433 const char *pathVar = mEndPath;
434 do { ++mEndPath; } while (*mEndPath && *mEndPath != PATH_SEPARATOR);
435
436 nsCOMPtr<nsILocalFile> localFile;
437 NS_NewNativeLocalFile(Substring(pathVar, mEndPath),
438 PR_TRUE,
439 getter_AddRefs(localFile));
440 if (*mEndPath == PATH_SEPARATOR)
441 ++mEndPath;
442 // Don't return a "file" (directory) which does not exist.
443 PRBool exists;
444 if (localFile &&
445 NS_SUCCEEDED(localFile->Exists(&exists)) &&
446 exists)
447 mNext = localFile;
448 }
449 if (mNext)
450 *result = PR_TRUE;
451 else
452 nsAppDirectoryEnumerator::HasMoreElements(result);
453
454 return NS_OK;
455 }
456
457 protected:
458 const char *mEndPath;
459};
460
461NS_IMETHODIMP
462nsAppFileLocationProvider::GetFiles(const char *prop, nsISimpleEnumerator **_retval)
463{
464 NS_ENSURE_ARG_POINTER(_retval);
465 *_retval = nsnull;
466 nsresult rv = NS_ERROR_FAILURE;
467
468 if (!nsCRT::strcmp(prop, NS_APP_PLUGINS_DIR_LIST))
469 {
470 static const char* keys[] = { nsnull, NS_APP_PLUGINS_DIR, nsnull };
471 if (!keys[0] && !(keys[0] = RTEnvGet("VBOX_XPCOM_PLUGIN_PATH"))) {
472 static const char nullstr = 0;
473 keys[0] = &nullstr;
474 }
475 *_retval = new nsPathsDirectoryEnumerator(this, keys);
476
477 NS_IF_ADDREF(*_retval);
478 rv = *_retval ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
479 }
480 return rv;
481}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette