Files
palemoon27/xpcom/io/nsAppFileLocationProvider.cpp
roytam1 4a4a995d26 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1240330 - mozAlarm debug messages are turned on r=khuey (aa938a4371)
- fix (cdc44e8bc8)
- Bug 1239788 - Don't include non-main-thread assertions in the constructor enabled check for [Exposed=System] WebIDL interfaces; r=bzbarsky (5d05b4066a)
- Bug 1247346. Slot indices on IDL interface members that get a slot should be per-interface, not a single per-member number. r=peterv (640231c37e)
- Bug 1157469 - null check wrapper in case WrapNode fails, r=bholley (9fd9b038db)
- Bug 1242211 - Mark MozMapRooter as final; r=baku (f3b5cc7728)
- Bug 1243438. Don't try to include the filename '<unknown>' when including headers for unions that have a built-in typedef they're referenced by. r=peterv (781d1ec0af)
- Bug 1241021 part 1: Refactor GenerateCSS2PropertiesWebIDL.py to invoke a helper function for each line. r=bz (258b031fd8)
- Bug 1241021 part 2: Make GenerateCSS2PropertiesWebIDL.py produce capitalized as well as uncapitalized WebkitFoo/webkitFoo DOM style accessors. r=bz (0bf2ec95ae)
- Bug 1241021 part 3: Refactor CSS mochitest test_initial_storage.html to perform its DOM accessor consistency-checks via a helper-function. r=bz (3a0c1a1678)
- Bug 1241021 part 4: Extend mochitest test_initial_storage.html to test that uppercase Webkit DOM accessors for css properties are functional. r=bz (5a6a23c7ab)
- Bug 1240592 - Remove unnecessary -Wno-uninitialized from dom/bindings. r=khuey (c854f7b397)
- Bug 1200341 - Implement HKDF for WebCrypto r=rbarnes,smaug (2c830d65a1)
- Bug 1200341 - Use BufferSource instead of CryptoOperationData in the SubtleCrypto WebIDL spec r=smaug (8cece95b5c)
- Bug 1191936 - Implement RSA-PSS key generation r=rbarnes (ec9654595c)
- Bug 1191936 - Implement RSA-PSS signing and verification r=rbarnes,smaug (7653bdfced)
- Bug 1204763 follow-up: Fix the comments, DONTBUILD (8b26e6f3a9)
- Reformat clang-plugin.cpp using clang-format, no bug (9346eda9f4)
- Bug 1214037 - Don't consider the result of an assignment expression MOZ_MUST_USE, r=ehsan (70c7830c3a)
- Bug 1242789 - Allow lambdas to capture raw pointers to refcounted objects by reference, r=ehsan (e7158a7746)
- Bug 1238042 - Add about:checkerboard. r=ehsan,botond (f234ce7fa0)
- Bug 1237177. Make the this-rebinding callable proxy sandboxes use return the prototype of its target, not its own null prototype. r=efaust (694a8081dc)
- Bug 1248521 - Pack PrefHashEntry better. r=froydnj. (55e9d609fe)
- Bug 1240453 - enable shared memory in xpconnect. r=bz (58db0b406e)
- Bug 1202014 - Don't use-after-move in ActorsChild.cpp, r=khuey (382e7183c0)
- Bug 1136762 - TSan: data race xpcom/io/nsPipe3.cpp:1061 CloseWithStatus. r=nfroyd. (68fd82971d)
- Bug 1237668 - Remove nsSimpleUnicharStreamFactory r=froydnj (8bb6ec081c)
- Bug 1234012 - Remove the NS_APP_PROFILE_DEFAULTS_{NLOC_,}50_DIR directory keys. r=bsmedberg (386f5b22ff)
- Bug 1235099 - Remove the NS_APP_BOOKMARKS_50_FILE directory service key. r=dolske (a46afe7211)
- Bug 1189998, Part 4 - Add authentication secret to Push data test. r=dragana (122a0bd5f9)
- Bug 1205137 - Add a PushSubscription serializer. r=mt,smaug (aa9e847c42)
- Bug 1232910 - Use channel.asyncOpen2 within dom/push/PushServiceHttp2.jsm. r=sicking r=dragana (a2f483a983)
- Bug 1246632 - Fix ackMsgRecv in PushServiceHttp2.js. r=kitcambridge (c1559e0539)
- Bug 1240615 - Make PushService protocol init method return a Promise. r=kitcambridge (4d52dc6a41)
- Bug 1211160 - update lastModifiedTime of distribution addons to properly sync roaming profiles, r=dtownsend (ae73a213b8)
- Bug 1232222 - expose isSystem flag if add-on is a system add-on r=Mossop (f057a5c375)
- Bug 1167627 - Part 9: Use mozinfo in services/. r=rnewman (5312e35c13)
- Bug 1205620 - fix default visibility of warning/error buttons in addon manager, r=Mossop (b071fc5cef)
- Bug 1237820: Track whether a user has been offered a sideloaded add-on or not. r=rhelmer (26ca999e3b)
- Bug 1236377: Ignore invalid file descriptors when loading an add-ons database written by a different OS. r=rhelmer (b78f0f5342)
- Bug 1246245 - Webextensions and themes should also block initial e10s rollout. r=Mossop (d4cd588b16)
- Bug 1245916: Turn on no-undef in toolkit/mozapps/extensions. r=rhelmer (4a9d16eff6)
- Bug 1245251 - Repair add-on manager for external XUL apps. r=Mossop (4e525d634c)
- Bug 1244302: Remove the add-ons selection UI. (981e4deaf3)
- Bug 1243935 - "Restartless add-ons that get into the pending disable state never disable". r=aswan (7b320b3930)
- Bug 612168 - Handle uninstalls of restartless addons in XPIProvider. r=Mossop (48943f830a)
- Bug 1239484 - remove use of Object.values() r=mossop (57f9a179bc)
- Bug 1246784 - Expose Console to the WorkerDebuggerGlobalScope, r=khuey (a992fdc555)
- misspatch (33708903c4)
- Bug 1207494 - Part 5: Remove use of expression closure from dom/contacts/. r=reuben (256b3831a4)
- Bug 1207494 - Part 4: Remove use of expression closure from dom/canvas/. r=gw280 (1666e49673)
- Bug 1207494 - Part 3: Remove use of expression closure from dom/browser-element/. r=smaug (a891769373)
- Bug 1207494 - Part 2: Remove use of expression closure from dom/base/test/. r=peterv (01762e3181)
- Bug 1207494 - Part 1: Remove use of expression closure from dom/apps/. r=ferjm (2474c8619b)
- Bug 1247108 - Pass securityFlag when creating a new channel within webapps (r=fabrice) (8305294f26)
- Bug 1219004 - Improve error management in getFullAppByManifestURL r=ferjm (c3ad64df7a)
- revert contains/includes (29c60a77e5)
- Bug 1147562 - Update remaining callsites of newChannel before landing the shim in dom/ (r=sicking) (0cd64e20e1)
- Bug 1243924 - Convert remaining callsites within dom/ to use channel.open2() (r=sicking) (43b01f88ba)
- Bug 1162658 - Update FormData to match latest spec, r=smaug (a42ca52ad5)
- Bug 1208895 - Fix a typo in nsGenericHTMLElement::GetItemId(); r=baku (2d6f2d79f4)
- Bug 1223523. The named getter on document should not return images with an empty name. r=peterv (da3d1f52f5)
- Bug 1183491. Null-check the document we get from our contentWindow in the contentDocument getter, because apparently it can end up null. r=bkelly (3002a4375c)
- Bug 1203430 - Intermittent crash in PNeckoChild::SendHTMLDNSPrefetch. r=jdm (3b82b57cbf)
- Bug 893332, part 1 - Rename misnamed SetValidityState/GetValidityState arguments. r=baku (d403b12c9a)
- Bug 893332, part 2 - Update validity state before asserting range underflow state. r=baku (a4cbce224c)
- Bug 893332, part 3 - Don't assert range underflow state under the parser. r=baku (763e99ed8e)
- Bug 893332, part 4 - Crashtests. r=baku (7cdcdc02c4)
- Bug 1240336 Setting same value to either <input> or <textarea> shouldn't cause committing existing composition r=ehsan (5cc2c35dbb)
- Bug 1228708. Fix UndoManager to correctly suppress exceptions it wants to suppress on ErrorResults. r=smaug (905207a3b4)
- Bug 1230295 - Add telemetry to report when Ogg chaining is used. r=rillian,r=vladan (e47c49f508)
2023-12-29 15:55:20 +08:00

610 lines
19 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsAppFileLocationProvider.h"
#include "nsAppDirectoryServiceDefs.h"
#include "nsDirectoryServiceDefs.h"
#include "nsEnumeratorUtils.h"
#include "nsIAtom.h"
#include "nsIFile.h"
#include "nsString.h"
#include "nsXPIDLString.h"
#include "nsISimpleEnumerator.h"
#include "prenv.h"
#include "nsCRT.h"
#if defined(MOZ_WIDGET_COCOA)
#include <Carbon/Carbon.h>
#include "nsILocalFileMac.h"
#elif defined(XP_WIN)
#include <windows.h>
#include <shlobj.h>
#elif defined(XP_UNIX)
#include <unistd.h>
#include <stdlib.h>
#include <sys/param.h>
#endif
// WARNING: These hard coded names need to go away. They need to
// come from localizable resources
#if defined(MOZ_WIDGET_COCOA)
#define APP_REGISTRY_NAME NS_LITERAL_CSTRING("Application Registry")
#define ESSENTIAL_FILES NS_LITERAL_CSTRING("Essential Files")
#elif defined(XP_WIN)
#define APP_REGISTRY_NAME NS_LITERAL_CSTRING("registry.dat")
#else
#define APP_REGISTRY_NAME NS_LITERAL_CSTRING("appreg")
#endif
// define default product directory
#define DEFAULT_PRODUCT_DIR NS_LITERAL_CSTRING(MOZ_USER_DIR)
// Locally defined keys used by nsAppDirectoryEnumerator
#define NS_ENV_PLUGINS_DIR "EnvPlugins" // env var MOZ_PLUGIN_PATH
#define NS_USER_PLUGINS_DIR "UserPlugins"
#ifdef MOZ_WIDGET_COCOA
#define NS_MACOSX_USER_PLUGIN_DIR "OSXUserPlugins"
#define NS_MACOSX_LOCAL_PLUGIN_DIR "OSXLocalPlugins"
#define NS_MACOSX_JAVA2_PLUGIN_DIR "OSXJavaPlugins"
#elif XP_UNIX
#define NS_SYSTEM_PLUGINS_DIR "SysPlugins"
#endif
#define DEFAULTS_DIR_NAME NS_LITERAL_CSTRING("defaults")
#define DEFAULTS_PREF_DIR_NAME NS_LITERAL_CSTRING("pref")
#define RES_DIR_NAME NS_LITERAL_CSTRING("res")
#define CHROME_DIR_NAME NS_LITERAL_CSTRING("chrome")
#define PLUGINS_DIR_NAME NS_LITERAL_CSTRING("plugins")
#define SEARCH_DIR_NAME NS_LITERAL_CSTRING("searchplugins")
//*****************************************************************************
// nsAppFileLocationProvider::Constructor/Destructor
//*****************************************************************************
nsAppFileLocationProvider::nsAppFileLocationProvider()
{
}
//*****************************************************************************
// nsAppFileLocationProvider::nsISupports
//*****************************************************************************
NS_IMPL_ISUPPORTS(nsAppFileLocationProvider,
nsIDirectoryServiceProvider,
nsIDirectoryServiceProvider2)
//*****************************************************************************
// nsAppFileLocationProvider::nsIDirectoryServiceProvider
//*****************************************************************************
NS_IMETHODIMP
nsAppFileLocationProvider::GetFile(const char* aProp, bool* aPersistent,
nsIFile** aResult)
{
if (NS_WARN_IF(!aProp)) {
return NS_ERROR_INVALID_ARG;
}
nsCOMPtr<nsIFile> localFile;
nsresult rv = NS_ERROR_FAILURE;
*aResult = nullptr;
*aPersistent = true;
#ifdef MOZ_WIDGET_COCOA
FSRef fileRef;
nsCOMPtr<nsILocalFileMac> macFile;
#endif
if (nsCRT::strcmp(aProp, NS_APP_APPLICATION_REGISTRY_DIR) == 0) {
rv = GetProductDirectory(getter_AddRefs(localFile));
} else if (nsCRT::strcmp(aProp, NS_APP_APPLICATION_REGISTRY_FILE) == 0) {
rv = GetProductDirectory(getter_AddRefs(localFile));
if (NS_SUCCEEDED(rv)) {
rv = localFile->AppendNative(APP_REGISTRY_NAME);
}
} else if (nsCRT::strcmp(aProp, NS_APP_DEFAULTS_50_DIR) == 0) {
rv = CloneMozBinDirectory(getter_AddRefs(localFile));
if (NS_SUCCEEDED(rv)) {
rv = localFile->AppendRelativeNativePath(DEFAULTS_DIR_NAME);
}
} else if (nsCRT::strcmp(aProp, NS_APP_PREF_DEFAULTS_50_DIR) == 0) {
rv = CloneMozBinDirectory(getter_AddRefs(localFile));
if (NS_SUCCEEDED(rv)) {
rv = localFile->AppendRelativeNativePath(DEFAULTS_DIR_NAME);
if (NS_SUCCEEDED(rv)) {
rv = localFile->AppendRelativeNativePath(DEFAULTS_PREF_DIR_NAME);
}
}
} else if (nsCRT::strcmp(aProp, NS_APP_USER_PROFILES_ROOT_DIR) == 0) {
rv = GetDefaultUserProfileRoot(getter_AddRefs(localFile));
} else if (nsCRT::strcmp(aProp, NS_APP_USER_PROFILES_LOCAL_ROOT_DIR) == 0) {
rv = GetDefaultUserProfileRoot(getter_AddRefs(localFile), true);
} else if (nsCRT::strcmp(aProp, NS_APP_RES_DIR) == 0) {
rv = CloneMozBinDirectory(getter_AddRefs(localFile));
if (NS_SUCCEEDED(rv)) {
rv = localFile->AppendRelativeNativePath(RES_DIR_NAME);
}
} else if (nsCRT::strcmp(aProp, NS_APP_CHROME_DIR) == 0) {
rv = CloneMozBinDirectory(getter_AddRefs(localFile));
if (NS_SUCCEEDED(rv)) {
rv = localFile->AppendRelativeNativePath(CHROME_DIR_NAME);
}
} else if (nsCRT::strcmp(aProp, NS_APP_PLUGINS_DIR) == 0) {
rv = CloneMozBinDirectory(getter_AddRefs(localFile));
if (NS_SUCCEEDED(rv)) {
rv = localFile->AppendRelativeNativePath(PLUGINS_DIR_NAME);
}
}
#ifdef MOZ_WIDGET_COCOA
else if (nsCRT::strcmp(aProp, NS_MACOSX_USER_PLUGIN_DIR) == 0) {
if (::FSFindFolder(kUserDomain, kInternetPlugInFolderType, false,
&fileRef) == noErr) {
rv = NS_NewLocalFileWithFSRef(&fileRef, true, getter_AddRefs(macFile));
if (NS_SUCCEEDED(rv)) {
localFile = macFile;
}
}
} else if (nsCRT::strcmp(aProp, NS_MACOSX_LOCAL_PLUGIN_DIR) == 0) {
if (::FSFindFolder(kLocalDomain, kInternetPlugInFolderType, false,
&fileRef) == noErr) {
rv = NS_NewLocalFileWithFSRef(&fileRef, true, getter_AddRefs(macFile));
if (NS_SUCCEEDED(rv)) {
localFile = macFile;
}
}
} else if (nsCRT::strcmp(aProp, NS_MACOSX_JAVA2_PLUGIN_DIR) == 0) {
static const char* const java2PluginDirPath =
"/System/Library/Java/Support/Deploy.bundle/Contents/Resources/";
rv = NS_NewNativeLocalFile(nsDependentCString(java2PluginDirPath), true,
getter_AddRefs(localFile));
}
#else
else if (nsCRT::strcmp(aProp, NS_ENV_PLUGINS_DIR) == 0) {
NS_ERROR("Don't use nsAppFileLocationProvider::GetFile(NS_ENV_PLUGINS_DIR, ...). "
"Use nsAppFileLocationProvider::GetFiles(...).");
const char* pathVar = PR_GetEnv("MOZ_PLUGIN_PATH");
if (pathVar && *pathVar)
rv = NS_NewNativeLocalFile(nsDependentCString(pathVar), true,
getter_AddRefs(localFile));
} else if (nsCRT::strcmp(aProp, NS_USER_PLUGINS_DIR) == 0) {
#ifdef ENABLE_SYSTEM_EXTENSION_DIRS
rv = GetProductDirectory(getter_AddRefs(localFile));
if (NS_SUCCEEDED(rv)) {
rv = localFile->AppendRelativeNativePath(PLUGINS_DIR_NAME);
}
#else
rv = NS_ERROR_FAILURE;
#endif
}
#ifdef XP_UNIX
else if (nsCRT::strcmp(aProp, NS_SYSTEM_PLUGINS_DIR) == 0) {
#ifdef ENABLE_SYSTEM_EXTENSION_DIRS
static const char* const sysLPlgDir =
#if defined(HAVE_USR_LIB64_DIR) && defined(__LP64__)
"/usr/lib64/mozilla/plugins";
#elif defined(__OpenBSD__) || defined (__FreeBSD__)
"/usr/local/lib/mozilla/plugins";
#else
"/usr/lib/mozilla/plugins";
#endif
rv = NS_NewNativeLocalFile(nsDependentCString(sysLPlgDir),
false, getter_AddRefs(localFile));
#else
rv = NS_ERROR_FAILURE;
#endif
}
#endif
#endif
else if (nsCRT::strcmp(aProp, NS_APP_SEARCH_DIR) == 0) {
rv = CloneMozBinDirectory(getter_AddRefs(localFile));
if (NS_SUCCEEDED(rv)) {
rv = localFile->AppendRelativeNativePath(SEARCH_DIR_NAME);
}
} else if (nsCRT::strcmp(aProp, NS_APP_USER_SEARCH_DIR) == 0) {
rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, aResult);
if (NS_SUCCEEDED(rv)) {
rv = (*aResult)->AppendNative(SEARCH_DIR_NAME);
}
} else if (nsCRT::strcmp(aProp, NS_APP_INSTALL_CLEANUP_DIR) == 0) {
// This is cloned so that embeddors will have a hook to override
// with their own cleanup dir. See bugzilla bug #105087
rv = CloneMozBinDirectory(getter_AddRefs(localFile));
}
if (localFile && NS_SUCCEEDED(rv)) {
localFile.forget(aResult);
return NS_OK;
}
return rv;
}
NS_METHOD
nsAppFileLocationProvider::CloneMozBinDirectory(nsIFile** aLocalFile)
{
if (NS_WARN_IF(!aLocalFile)) {
return NS_ERROR_INVALID_ARG;
}
nsresult rv;
if (!mMozBinDirectory) {
// Get the mozilla bin directory
// 1. Check the directory service first for NS_XPCOM_CURRENT_PROCESS_DIR
// This will be set if a directory was passed to NS_InitXPCOM
// 2. If that doesn't work, set it to be the current process directory
nsCOMPtr<nsIProperties>
directoryService(do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv));
if (NS_FAILED(rv)) {
return rv;
}
rv = directoryService->Get(NS_XPCOM_CURRENT_PROCESS_DIR, NS_GET_IID(nsIFile),
getter_AddRefs(mMozBinDirectory));
if (NS_FAILED(rv)) {
rv = directoryService->Get(NS_OS_CURRENT_PROCESS_DIR, NS_GET_IID(nsIFile),
getter_AddRefs(mMozBinDirectory));
if (NS_FAILED(rv)) {
return rv;
}
}
}
nsCOMPtr<nsIFile> aFile;
rv = mMozBinDirectory->Clone(getter_AddRefs(aFile));
if (NS_FAILED(rv)) {
return rv;
}
NS_IF_ADDREF(*aLocalFile = aFile);
return NS_OK;
}
//----------------------------------------------------------------------------------------
// GetProductDirectory - Gets the directory which contains the application data folder
//
// UNIX : ~/.mozilla/
// WIN : <Application Data folder on user's machine>\Mozilla
// Mac : :Documents:Mozilla:
//----------------------------------------------------------------------------------------
NS_METHOD
nsAppFileLocationProvider::GetProductDirectory(nsIFile** aLocalFile,
bool aLocal)
{
if (NS_WARN_IF(!aLocalFile)) {
return NS_ERROR_INVALID_ARG;
}
nsresult rv;
bool exists;
nsCOMPtr<nsIFile> localDir;
#if defined(MOZ_WIDGET_COCOA)
FSRef fsRef;
OSType folderType = aLocal ? (OSType)kCachedDataFolderType :
(OSType)kDomainLibraryFolderType;
OSErr err = ::FSFindFolder(kUserDomain, folderType, kCreateFolder, &fsRef);
if (err) {
return NS_ERROR_FAILURE;
}
NS_NewLocalFile(EmptyString(), true, getter_AddRefs(localDir));
if (!localDir) {
return NS_ERROR_FAILURE;
}
nsCOMPtr<nsILocalFileMac> localDirMac(do_QueryInterface(localDir));
rv = localDirMac->InitWithFSRef(&fsRef);
if (NS_FAILED(rv)) {
return rv;
}
#elif defined(XP_WIN)
nsCOMPtr<nsIProperties> directoryService =
do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv);
if (NS_FAILED(rv)) {
return rv;
}
const char* prop = aLocal ? NS_WIN_LOCAL_APPDATA_DIR : NS_WIN_APPDATA_DIR;
rv = directoryService->Get(prop, NS_GET_IID(nsIFile), getter_AddRefs(localDir));
if (NS_FAILED(rv)) {
return rv;
}
#elif defined(XP_UNIX)
rv = NS_NewNativeLocalFile(nsDependentCString(PR_GetEnv("HOME")), true,
getter_AddRefs(localDir));
if (NS_FAILED(rv)) {
return rv;
}
#else
#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) {
rv = localDir->Create(nsIFile::DIRECTORY_TYPE, 0700);
}
if (NS_FAILED(rv)) {
return rv;
}
localDir.forget(aLocalFile);
return rv;
}
//----------------------------------------------------------------------------------------
// 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:
//----------------------------------------------------------------------------------------
NS_METHOD
nsAppFileLocationProvider::GetDefaultUserProfileRoot(nsIFile** aLocalFile,
bool aLocal)
{
if (NS_WARN_IF(!aLocalFile)) {
return NS_ERROR_INVALID_ARG;
}
nsresult rv;
nsCOMPtr<nsIFile> localDir;
rv = GetProductDirectory(getter_AddRefs(localDir), aLocal);
if (NS_FAILED(rv)) {
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(NS_LITERAL_CSTRING("Profiles"));
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;
}
//*****************************************************************************
// nsAppFileLocationProvider::nsIDirectoryServiceProvider2
//*****************************************************************************
class nsAppDirectoryEnumerator : public nsISimpleEnumerator
{
public:
NS_DECL_ISUPPORTS
/**
* aKeyList is a null-terminated list of properties which are provided by aProvider
* They do not need to be publicly defined keys.
*/
nsAppDirectoryEnumerator(nsIDirectoryServiceProvider* aProvider,
const char* aKeyList[]) :
mProvider(aProvider),
mCurrentKey(aKeyList)
{
}
NS_IMETHOD HasMoreElements(bool* aResult) override
{
while (!mNext && *mCurrentKey) {
bool dontCare;
nsCOMPtr<nsIFile> testFile;
(void)mProvider->GetFile(*mCurrentKey++, &dontCare, getter_AddRefs(testFile));
// Don't return a file which does not exist.
bool exists;
if (testFile && NS_SUCCEEDED(testFile->Exists(&exists)) && exists) {
mNext = testFile;
}
}
*aResult = mNext != nullptr;
return NS_OK;
}
NS_IMETHOD GetNext(nsISupports** aResult) override
{
if (NS_WARN_IF(!aResult)) {
return NS_ERROR_INVALID_ARG;
}
*aResult = nullptr;
bool hasMore;
HasMoreElements(&hasMore);
if (!hasMore) {
return NS_ERROR_FAILURE;
}
*aResult = mNext;
NS_IF_ADDREF(*aResult);
mNext = nullptr;
return *aResult ? NS_OK : NS_ERROR_FAILURE;
}
protected:
nsCOMPtr<nsIDirectoryServiceProvider> mProvider;
const char** mCurrentKey;
nsCOMPtr<nsIFile> mNext;
// Virtual destructor since subclass nsPathsDirectoryEnumerator
// does not re-implement Release()
virtual ~nsAppDirectoryEnumerator()
{
}
};
NS_IMPL_ISUPPORTS(nsAppDirectoryEnumerator, nsISimpleEnumerator)
/* nsPathsDirectoryEnumerator and PATH_SEPARATOR
* are not used on MacOS/X. */
#if defined(XP_WIN) /* Win32 */
#define PATH_SEPARATOR ';'
#else
#define PATH_SEPARATOR ':'
#endif
class nsPathsDirectoryEnumerator final
: public nsAppDirectoryEnumerator
{
~nsPathsDirectoryEnumerator() {}
public:
/**
* aKeyList is a null-terminated list.
* The first element is a path list.
* The remainder are properties provided by aProvider.
* They do not need to be publicly defined keys.
*/
nsPathsDirectoryEnumerator(nsIDirectoryServiceProvider* aProvider,
const char* aKeyList[]) :
nsAppDirectoryEnumerator(aProvider, aKeyList + 1),
mEndPath(aKeyList[0])
{
}
NS_IMETHOD HasMoreElements(bool* aResult)
{
if (mEndPath)
while (!mNext && *mEndPath) {
const char* pathVar = mEndPath;
// skip PATH_SEPARATORs at the begining of the mEndPath
while (*pathVar == PATH_SEPARATOR) {
++pathVar;
}
do {
++mEndPath;
} while (*mEndPath && *mEndPath != PATH_SEPARATOR);
nsCOMPtr<nsIFile> localFile;
NS_NewNativeLocalFile(Substring(pathVar, mEndPath),
true,
getter_AddRefs(localFile));
if (*mEndPath == PATH_SEPARATOR) {
++mEndPath;
}
// Don't return a "file" (directory) which does not exist.
bool exists;
if (localFile &&
NS_SUCCEEDED(localFile->Exists(&exists)) &&
exists) {
mNext = localFile;
}
}
if (mNext) {
*aResult = true;
} else {
nsAppDirectoryEnumerator::HasMoreElements(aResult);
}
return NS_OK;
}
protected:
const char* mEndPath;
};
NS_IMETHODIMP
nsAppFileLocationProvider::GetFiles(const char* aProp,
nsISimpleEnumerator** aResult)
{
if (NS_WARN_IF(!aResult)) {
return NS_ERROR_INVALID_ARG;
}
*aResult = nullptr;
nsresult rv = NS_ERROR_FAILURE;
if (!nsCRT::strcmp(aProp, NS_APP_PLUGINS_DIR_LIST)) {
#ifdef MOZ_WIDGET_COCOA
// As of Java for Mac OS X 10.5 Update 10, Apple has (in effect) deprecated Java Plugin2 on
// on OS X 10.5, and removed the soft link to it from /Library/Internet Plug-Ins/. Java
// Plugin2 is still present and usable, but there are no longer any links to it in the
// "normal" locations. So we won't be able to find it unless we look in the "non-normal"
// location where it actually is. Safari can use the WebKit-specific JavaPluginCocoa.bundle,
// which (of course) is still fully supported on OS X 10.5. But we have no alternative to
// using Java Plugin2. For more information see bug 668639.
static const char* keys[] = {
NS_APP_PLUGINS_DIR,
NS_MACOSX_USER_PLUGIN_DIR,
NS_MACOSX_LOCAL_PLUGIN_DIR,
IsOSXLeopard() ? NS_MACOSX_JAVA2_PLUGIN_DIR : nullptr,
nullptr
};
*aResult = new nsAppDirectoryEnumerator(this, keys);
#else
#ifdef XP_UNIX
static const char* keys[] = { nullptr, NS_USER_PLUGINS_DIR, NS_APP_PLUGINS_DIR, NS_SYSTEM_PLUGINS_DIR, nullptr };
#else
static const char* keys[] = { nullptr, NS_USER_PLUGINS_DIR, NS_APP_PLUGINS_DIR, nullptr };
#endif
if (!keys[0] && !(keys[0] = PR_GetEnv("MOZ_PLUGIN_PATH"))) {
static const char nullstr = 0;
keys[0] = &nullstr;
}
*aResult = new nsPathsDirectoryEnumerator(this, keys);
#endif
NS_ADDREF(*aResult);
rv = NS_OK;
}
if (!nsCRT::strcmp(aProp, NS_APP_SEARCH_DIR_LIST)) {
static const char* keys[] = { nullptr, NS_APP_USER_SEARCH_DIR, nullptr };
if (!keys[0] && !(keys[0] = PR_GetEnv("MOZ_SEARCH_ENGINE_PATH"))) {
static const char nullstr = 0;
keys[0] = &nullstr;
}
*aResult = new nsPathsDirectoryEnumerator(this, keys);
NS_ADDREF(*aResult);
rv = NS_OK;
}
if (!strcmp(aProp, NS_APP_DISTRIBUTION_SEARCH_DIR_LIST)) {
return NS_NewEmptyEnumerator(aResult);
}
return rv;
}
#if defined(MOZ_WIDGET_COCOA)
bool
nsAppFileLocationProvider::IsOSXLeopard()
{
static SInt32 version = 0;
if (!version) {
OSErr err = ::Gestalt(gestaltSystemVersion, &version);
if (err != noErr) {
version = 0;
} else {
version &= 0xFFFF; // The system version is in the low order word
}
}
return ((version >= 0x1050) && (version < 0x1060));
}
#endif