xpcom/io/nsAppFileLocationProvider.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/io/nsAppFileLocationProvider.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,570 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "nsAppFileLocationProvider.h"
    1.10 +#include "nsAppDirectoryServiceDefs.h"
    1.11 +#include "nsDirectoryServiceDefs.h"
    1.12 +#include "nsIAtom.h"
    1.13 +#include "nsIFile.h"
    1.14 +#include "nsString.h"
    1.15 +#include "nsXPIDLString.h"
    1.16 +#include "nsISimpleEnumerator.h"
    1.17 +#include "prenv.h"
    1.18 +#include "nsCRT.h"
    1.19 +#include "nsXPCOMPrivate.h"  // for XPCOM_FILE_PATH_SEPARATOR
    1.20 +
    1.21 +#if defined(MOZ_WIDGET_COCOA)
    1.22 +#include <Carbon/Carbon.h>
    1.23 +#include "nsILocalFileMac.h"
    1.24 +#elif defined(XP_WIN)
    1.25 +#include <windows.h>
    1.26 +#include <shlobj.h>
    1.27 +#elif defined(XP_UNIX)
    1.28 +#include <unistd.h>
    1.29 +#include <stdlib.h>
    1.30 +#include <sys/param.h>
    1.31 +#endif
    1.32 +
    1.33 +
    1.34 +// WARNING: These hard coded names need to go away. They need to
    1.35 +// come from localizable resources
    1.36 +
    1.37 +#if defined(MOZ_WIDGET_COCOA)
    1.38 +#define APP_REGISTRY_NAME NS_LITERAL_CSTRING("Application Registry")
    1.39 +#define ESSENTIAL_FILES   NS_LITERAL_CSTRING("Essential Files")
    1.40 +#elif defined(XP_WIN)
    1.41 +#define APP_REGISTRY_NAME NS_LITERAL_CSTRING("registry.dat")
    1.42 +#else
    1.43 +#define APP_REGISTRY_NAME NS_LITERAL_CSTRING("appreg")
    1.44 +#endif
    1.45 +
    1.46 +// define default product directory
    1.47 +#define DEFAULT_PRODUCT_DIR NS_LITERAL_CSTRING(MOZ_USER_DIR)
    1.48 +
    1.49 +// Locally defined keys used by nsAppDirectoryEnumerator
    1.50 +#define NS_ENV_PLUGINS_DIR          "EnvPlugins"    // env var MOZ_PLUGIN_PATH
    1.51 +#define NS_USER_PLUGINS_DIR         "UserPlugins"
    1.52 +
    1.53 +#ifdef MOZ_WIDGET_COCOA
    1.54 +#define NS_MACOSX_USER_PLUGIN_DIR   "OSXUserPlugins"
    1.55 +#define NS_MACOSX_LOCAL_PLUGIN_DIR  "OSXLocalPlugins"
    1.56 +#define NS_MACOSX_JAVA2_PLUGIN_DIR  "OSXJavaPlugins"
    1.57 +#elif XP_UNIX
    1.58 +#define NS_SYSTEM_PLUGINS_DIR       "SysPlugins"
    1.59 +#endif
    1.60 +
    1.61 +#define DEFAULTS_DIR_NAME           NS_LITERAL_CSTRING("defaults")
    1.62 +#define DEFAULTS_PREF_DIR_NAME      NS_LITERAL_CSTRING("pref")
    1.63 +#define DEFAULTS_PROFILE_DIR_NAME   NS_LITERAL_CSTRING("profile")
    1.64 +#define RES_DIR_NAME                NS_LITERAL_CSTRING("res")
    1.65 +#define CHROME_DIR_NAME             NS_LITERAL_CSTRING("chrome")
    1.66 +#define PLUGINS_DIR_NAME            NS_LITERAL_CSTRING("plugins")
    1.67 +#define SEARCH_DIR_NAME             NS_LITERAL_CSTRING("searchplugins")
    1.68 +
    1.69 +//*****************************************************************************
    1.70 +// nsAppFileLocationProvider::Constructor/Destructor
    1.71 +//*****************************************************************************
    1.72 +
    1.73 +nsAppFileLocationProvider::nsAppFileLocationProvider()
    1.74 +{
    1.75 +}
    1.76 +
    1.77 +//*****************************************************************************
    1.78 +// nsAppFileLocationProvider::nsISupports
    1.79 +//*****************************************************************************
    1.80 +
    1.81 +NS_IMPL_ISUPPORTS(nsAppFileLocationProvider, nsIDirectoryServiceProvider, nsIDirectoryServiceProvider2)
    1.82 +
    1.83 +//*****************************************************************************
    1.84 +// nsAppFileLocationProvider::nsIDirectoryServiceProvider
    1.85 +//*****************************************************************************
    1.86 +
    1.87 +NS_IMETHODIMP
    1.88 +nsAppFileLocationProvider::GetFile(const char *prop, bool *persistent, nsIFile **_retval)
    1.89 +{
    1.90 +    if (NS_WARN_IF(!prop))
    1.91 +        return NS_ERROR_INVALID_ARG;
    1.92 +
    1.93 +    nsCOMPtr<nsIFile>  localFile;
    1.94 +    nsresult rv = NS_ERROR_FAILURE;
    1.95 +
    1.96 +    *_retval = nullptr;
    1.97 +    *persistent = true;
    1.98 +
    1.99 +#ifdef MOZ_WIDGET_COCOA
   1.100 +    FSRef fileRef;
   1.101 +    nsCOMPtr<nsILocalFileMac> macFile;
   1.102 +#endif
   1.103 +    
   1.104 +    if (nsCRT::strcmp(prop, NS_APP_APPLICATION_REGISTRY_DIR) == 0)
   1.105 +    {
   1.106 +        rv = GetProductDirectory(getter_AddRefs(localFile));
   1.107 +    }
   1.108 +    else if (nsCRT::strcmp(prop, NS_APP_APPLICATION_REGISTRY_FILE) == 0)
   1.109 +    {
   1.110 +        rv = GetProductDirectory(getter_AddRefs(localFile));
   1.111 +        if (NS_SUCCEEDED(rv))
   1.112 +            rv = localFile->AppendNative(APP_REGISTRY_NAME);
   1.113 +    }
   1.114 +    else if (nsCRT::strcmp(prop, NS_APP_DEFAULTS_50_DIR) == 0)
   1.115 +    {
   1.116 +        rv = CloneMozBinDirectory(getter_AddRefs(localFile));
   1.117 +        if (NS_SUCCEEDED(rv))
   1.118 +            rv = localFile->AppendRelativeNativePath(DEFAULTS_DIR_NAME);
   1.119 +    }
   1.120 +    else if (nsCRT::strcmp(prop, NS_APP_PREF_DEFAULTS_50_DIR) == 0)
   1.121 +    {
   1.122 +        rv = CloneMozBinDirectory(getter_AddRefs(localFile));
   1.123 +        if (NS_SUCCEEDED(rv)) {
   1.124 +            rv = localFile->AppendRelativeNativePath(DEFAULTS_DIR_NAME);
   1.125 +            if (NS_SUCCEEDED(rv))
   1.126 +                rv = localFile->AppendRelativeNativePath(DEFAULTS_PREF_DIR_NAME);
   1.127 +        }
   1.128 +    }
   1.129 +    else if (nsCRT::strcmp(prop, NS_APP_PROFILE_DEFAULTS_50_DIR) == 0 ||
   1.130 +             nsCRT::strcmp(prop, NS_APP_PROFILE_DEFAULTS_NLOC_50_DIR) == 0)
   1.131 +    {
   1.132 +        rv = CloneMozBinDirectory(getter_AddRefs(localFile));
   1.133 +        if (NS_SUCCEEDED(rv)) {
   1.134 +            rv = localFile->AppendRelativeNativePath(DEFAULTS_DIR_NAME);
   1.135 +            if (NS_SUCCEEDED(rv))
   1.136 +                rv = localFile->AppendRelativeNativePath(DEFAULTS_PROFILE_DIR_NAME);
   1.137 +        }
   1.138 +    }
   1.139 +    else if (nsCRT::strcmp(prop, NS_APP_USER_PROFILES_ROOT_DIR) == 0)
   1.140 +    {
   1.141 +        rv = GetDefaultUserProfileRoot(getter_AddRefs(localFile));
   1.142 +    }
   1.143 +    else if (nsCRT::strcmp(prop, NS_APP_USER_PROFILES_LOCAL_ROOT_DIR) == 0)
   1.144 +    {
   1.145 +        rv = GetDefaultUserProfileRoot(getter_AddRefs(localFile), true);
   1.146 +    }
   1.147 +    else if (nsCRT::strcmp(prop, NS_APP_RES_DIR) == 0)
   1.148 +    {
   1.149 +        rv = CloneMozBinDirectory(getter_AddRefs(localFile));
   1.150 +        if (NS_SUCCEEDED(rv))
   1.151 +            rv = localFile->AppendRelativeNativePath(RES_DIR_NAME);
   1.152 +    }
   1.153 +    else if (nsCRT::strcmp(prop, NS_APP_CHROME_DIR) == 0)
   1.154 +    {
   1.155 +        rv = CloneMozBinDirectory(getter_AddRefs(localFile));
   1.156 +        if (NS_SUCCEEDED(rv))
   1.157 +            rv = localFile->AppendRelativeNativePath(CHROME_DIR_NAME);
   1.158 +    }
   1.159 +    else if (nsCRT::strcmp(prop, NS_APP_PLUGINS_DIR) == 0)
   1.160 +    {
   1.161 +        rv = CloneMozBinDirectory(getter_AddRefs(localFile));
   1.162 +        if (NS_SUCCEEDED(rv))
   1.163 +            rv = localFile->AppendRelativeNativePath(PLUGINS_DIR_NAME);
   1.164 +    }
   1.165 +#ifdef MOZ_WIDGET_COCOA
   1.166 +    else if (nsCRT::strcmp(prop, NS_MACOSX_USER_PLUGIN_DIR) == 0)
   1.167 +    {
   1.168 +        if (::FSFindFolder(kUserDomain, kInternetPlugInFolderType, false, &fileRef) == noErr) {
   1.169 +            rv = NS_NewLocalFileWithFSRef(&fileRef, true, getter_AddRefs(macFile));
   1.170 +            if (NS_SUCCEEDED(rv))
   1.171 +                localFile = macFile;
   1.172 +        }
   1.173 +    }
   1.174 +    else if (nsCRT::strcmp(prop, NS_MACOSX_LOCAL_PLUGIN_DIR) == 0)
   1.175 +    {
   1.176 +        if (::FSFindFolder(kLocalDomain, kInternetPlugInFolderType, false, &fileRef) == noErr) {
   1.177 +            rv = NS_NewLocalFileWithFSRef(&fileRef, true, getter_AddRefs(macFile));
   1.178 +            if (NS_SUCCEEDED(rv))
   1.179 +                localFile = macFile;
   1.180 +        }
   1.181 +    }
   1.182 +    else if (nsCRT::strcmp(prop, NS_MACOSX_JAVA2_PLUGIN_DIR) == 0)
   1.183 +    {
   1.184 +      static const char *const java2PluginDirPath =
   1.185 +        "/System/Library/Java/Support/Deploy.bundle/Contents/Resources/";
   1.186 +      rv = NS_NewNativeLocalFile(nsDependentCString(java2PluginDirPath), true, getter_AddRefs(localFile));
   1.187 +    }
   1.188 +#else
   1.189 +    else if (nsCRT::strcmp(prop, NS_ENV_PLUGINS_DIR) == 0)
   1.190 +    {
   1.191 +        NS_ERROR("Don't use nsAppFileLocationProvider::GetFile(NS_ENV_PLUGINS_DIR, ...). "
   1.192 +                 "Use nsAppFileLocationProvider::GetFiles(...).");
   1.193 +        const char *pathVar = PR_GetEnv("MOZ_PLUGIN_PATH");
   1.194 +        if (pathVar && *pathVar)
   1.195 +            rv = NS_NewNativeLocalFile(nsDependentCString(pathVar), true,
   1.196 +                                       getter_AddRefs(localFile));
   1.197 +    }
   1.198 +    else if (nsCRT::strcmp(prop, NS_USER_PLUGINS_DIR) == 0)
   1.199 +    {
   1.200 +#ifdef ENABLE_SYSTEM_EXTENSION_DIRS
   1.201 +        rv = GetProductDirectory(getter_AddRefs(localFile));
   1.202 +        if (NS_SUCCEEDED(rv))
   1.203 +            rv = localFile->AppendRelativeNativePath(PLUGINS_DIR_NAME);
   1.204 +#else
   1.205 +        rv = NS_ERROR_FAILURE;
   1.206 +#endif
   1.207 +    }
   1.208 +#ifdef XP_UNIX
   1.209 +    else if (nsCRT::strcmp(prop, NS_SYSTEM_PLUGINS_DIR) == 0) {
   1.210 +#ifdef ENABLE_SYSTEM_EXTENSION_DIRS
   1.211 +        static const char *const sysLPlgDir =
   1.212 +#if defined(HAVE_USR_LIB64_DIR) && defined(__LP64__)
   1.213 +          "/usr/lib64/mozilla/plugins";
   1.214 +#elif defined(__OpenBSD__) || defined (__FreeBSD__)
   1.215 +          "/usr/local/lib/mozilla/plugins";
   1.216 +#else
   1.217 +          "/usr/lib/mozilla/plugins";
   1.218 +#endif
   1.219 +        rv = NS_NewNativeLocalFile(nsDependentCString(sysLPlgDir),
   1.220 +                                   false, getter_AddRefs(localFile));
   1.221 +#else
   1.222 +        rv = NS_ERROR_FAILURE;
   1.223 +#endif
   1.224 +    }
   1.225 +#endif
   1.226 +#endif
   1.227 +    else if (nsCRT::strcmp(prop, NS_APP_SEARCH_DIR) == 0)
   1.228 +    {
   1.229 +        rv = CloneMozBinDirectory(getter_AddRefs(localFile));
   1.230 +        if (NS_SUCCEEDED(rv))
   1.231 +            rv = localFile->AppendRelativeNativePath(SEARCH_DIR_NAME);
   1.232 +    }
   1.233 +    else if (nsCRT::strcmp(prop, NS_APP_USER_SEARCH_DIR) == 0)
   1.234 +    {
   1.235 +        rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, _retval);
   1.236 +        if (NS_SUCCEEDED(rv))
   1.237 +            rv = (*_retval)->AppendNative(SEARCH_DIR_NAME);
   1.238 +    }
   1.239 +    else if (nsCRT::strcmp(prop, NS_APP_INSTALL_CLEANUP_DIR) == 0)
   1.240 +    {   
   1.241 +        // This is cloned so that embeddors will have a hook to override
   1.242 +        // with their own cleanup dir.  See bugzilla bug #105087 
   1.243 +        rv = CloneMozBinDirectory(getter_AddRefs(localFile));
   1.244 +    } 
   1.245 +
   1.246 +    if (localFile && NS_SUCCEEDED(rv))
   1.247 +        return localFile->QueryInterface(NS_GET_IID(nsIFile), (void**)_retval);
   1.248 +        
   1.249 +    return rv;
   1.250 +}
   1.251 +
   1.252 +
   1.253 +NS_METHOD nsAppFileLocationProvider::CloneMozBinDirectory(nsIFile **aLocalFile)
   1.254 +{
   1.255 +    if (NS_WARN_IF(!aLocalFile))
   1.256 +        return NS_ERROR_INVALID_ARG;
   1.257 +    nsresult rv;
   1.258 +
   1.259 +    if (!mMozBinDirectory)
   1.260 +    {
   1.261 +        // Get the mozilla bin directory
   1.262 +        // 1. Check the directory service first for NS_XPCOM_CURRENT_PROCESS_DIR
   1.263 +        //    This will be set if a directory was passed to NS_InitXPCOM
   1.264 +        // 2. If that doesn't work, set it to be the current process directory
   1.265 +        nsCOMPtr<nsIProperties>
   1.266 +          directoryService(do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv));
   1.267 +        if (NS_FAILED(rv))
   1.268 +            return rv;
   1.269 +
   1.270 +        rv = directoryService->Get(NS_XPCOM_CURRENT_PROCESS_DIR, NS_GET_IID(nsIFile), getter_AddRefs(mMozBinDirectory));
   1.271 +        if (NS_FAILED(rv)) {
   1.272 +            rv = directoryService->Get(NS_OS_CURRENT_PROCESS_DIR, NS_GET_IID(nsIFile), getter_AddRefs(mMozBinDirectory));
   1.273 +            if (NS_FAILED(rv))
   1.274 +                return rv;
   1.275 +        }
   1.276 +    }
   1.277 +
   1.278 +    nsCOMPtr<nsIFile> aFile;
   1.279 +    rv = mMozBinDirectory->Clone(getter_AddRefs(aFile));
   1.280 +    if (NS_FAILED(rv))
   1.281 +        return rv;
   1.282 +
   1.283 +    NS_IF_ADDREF(*aLocalFile = aFile);
   1.284 +    return NS_OK;
   1.285 +}
   1.286 +
   1.287 +
   1.288 +//----------------------------------------------------------------------------------------
   1.289 +// GetProductDirectory - Gets the directory which contains the application data folder
   1.290 +//
   1.291 +// UNIX and WIN   : <App Folder>/TorBrowser/Data/Browser
   1.292 +// Mac            : <App Folder>/../../TorBrowser/Data/Browser
   1.293 +//----------------------------------------------------------------------------------------
   1.294 +NS_METHOD nsAppFileLocationProvider::GetProductDirectory(nsIFile **aLocalFile, bool aLocal)
   1.295 +{
   1.296 +    if (NS_WARN_IF(!aLocalFile))
   1.297 +        return NS_ERROR_INVALID_ARG;
   1.298 +
   1.299 +    nsresult rv;
   1.300 +    bool exists;
   1.301 +    nsCOMPtr<nsIFile> localDir;
   1.302 +
   1.303 +    rv = CloneMozBinDirectory(getter_AddRefs(localDir));
   1.304 +    NS_ENSURE_SUCCESS(rv, rv);
   1.305 +
   1.306 +    int levelsToRemove = 1; // In FF21+, bin dir points to browser subdirectory.
   1.307 +#if defined(XP_MACOSX)
   1.308 +    levelsToRemove += 2;
   1.309 +#endif
   1.310 +    while (localDir && (levelsToRemove > 0)) {
   1.311 +        // When crawling up the hierarchy, components named "." do not count.
   1.312 +        nsAutoCString removedName;
   1.313 +        rv = localDir->GetNativeLeafName(removedName);
   1.314 +        NS_ENSURE_SUCCESS(rv, rv);
   1.315 +        bool didRemove = !removedName.Equals(".");
   1.316 +
   1.317 +        // Remove a directory component.
   1.318 +        nsCOMPtr<nsIFile> parentDir;
   1.319 +        rv = localDir->GetParent(getter_AddRefs(parentDir));
   1.320 +        NS_ENSURE_SUCCESS(rv, rv);
   1.321 +        localDir = parentDir;
   1.322 +
   1.323 +        if (didRemove)
   1.324 +          --levelsToRemove;
   1.325 +    }
   1.326 +
   1.327 +    if (!localDir)
   1.328 +        return NS_ERROR_FAILURE;
   1.329 +
   1.330 +    rv = localDir->AppendRelativeNativePath(NS_LITERAL_CSTRING("TorBrowser"
   1.331 +                                       XPCOM_FILE_PATH_SEPARATOR "Data"
   1.332 +                                       XPCOM_FILE_PATH_SEPARATOR "Browser"));
   1.333 +    NS_ENSURE_SUCCESS(rv, rv);
   1.334 +
   1.335 +    if (aLocal) {
   1.336 +        rv = localDir->AppendNative(NS_LITERAL_CSTRING("Caches"));
   1.337 +        NS_ENSURE_SUCCESS(rv, rv);
   1.338 +    }
   1.339 +
   1.340 +    rv = localDir->Exists(&exists);
   1.341 +
   1.342 +    if (NS_SUCCEEDED(rv) && !exists)
   1.343 +        rv = localDir->Create(nsIFile::DIRECTORY_TYPE, 0700);
   1.344 +
   1.345 +    if (NS_FAILED(rv)) return rv;
   1.346 +
   1.347 +    *aLocalFile = localDir;
   1.348 +    NS_ADDREF(*aLocalFile);
   1.349 +
   1.350 +   return rv;
   1.351 +}
   1.352 +
   1.353 +
   1.354 +//----------------------------------------------------------------------------------------
   1.355 +// GetDefaultUserProfileRoot - Gets the directory which contains each user profile dir
   1.356 +//----------------------------------------------------------------------------------------
   1.357 +NS_METHOD nsAppFileLocationProvider::GetDefaultUserProfileRoot(nsIFile **aLocalFile, bool aLocal)
   1.358 +{
   1.359 +    if (NS_WARN_IF(!aLocalFile))
   1.360 +        return NS_ERROR_INVALID_ARG;
   1.361 +
   1.362 +    nsresult rv;
   1.363 +    nsCOMPtr<nsIFile> localDir;
   1.364 +
   1.365 +    rv = GetProductDirectory(getter_AddRefs(localDir), aLocal);
   1.366 +    if (NS_FAILED(rv)) return rv;
   1.367 +
   1.368 +    *aLocalFile = localDir;
   1.369 +    NS_ADDREF(*aLocalFile);
   1.370 +
   1.371 +   return rv;
   1.372 +}
   1.373 +
   1.374 +//*****************************************************************************
   1.375 +// nsAppFileLocationProvider::nsIDirectoryServiceProvider2
   1.376 +//*****************************************************************************
   1.377 +
   1.378 +class nsAppDirectoryEnumerator : public nsISimpleEnumerator
   1.379 +{
   1.380 +  public:
   1.381 +    NS_DECL_ISUPPORTS
   1.382 +
   1.383 +    /**
   1.384 +     * aKeyList is a null-terminated list of properties which are provided by aProvider
   1.385 +     * They do not need to be publicly defined keys.
   1.386 +     */
   1.387 +    nsAppDirectoryEnumerator(nsIDirectoryServiceProvider *aProvider,
   1.388 +                             const char* aKeyList[]) :
   1.389 +        mProvider(aProvider),
   1.390 +        mCurrentKey(aKeyList)
   1.391 +    {
   1.392 +    }
   1.393 +
   1.394 +    NS_IMETHOD HasMoreElements(bool *result) 
   1.395 +    {
   1.396 +        while (!mNext && *mCurrentKey)
   1.397 +        {
   1.398 +            bool dontCare;
   1.399 +            nsCOMPtr<nsIFile> testFile;
   1.400 +            (void)mProvider->GetFile(*mCurrentKey++, &dontCare, getter_AddRefs(testFile));
   1.401 +            // Don't return a file which does not exist.
   1.402 +            bool exists;
   1.403 +            if (testFile && NS_SUCCEEDED(testFile->Exists(&exists)) && exists)
   1.404 +                mNext = testFile;
   1.405 +        }
   1.406 +        *result = mNext != nullptr;
   1.407 +        return NS_OK;
   1.408 +    }
   1.409 +
   1.410 +    NS_IMETHOD GetNext(nsISupports **result) 
   1.411 +    {
   1.412 +        if (NS_WARN_IF(!result))
   1.413 +            return NS_ERROR_INVALID_ARG;
   1.414 +        *result = nullptr;
   1.415 +
   1.416 +        bool hasMore;
   1.417 +        HasMoreElements(&hasMore);
   1.418 +        if (!hasMore)
   1.419 +            return NS_ERROR_FAILURE;
   1.420 +            
   1.421 +        *result = mNext;
   1.422 +        NS_IF_ADDREF(*result);
   1.423 +        mNext = nullptr;
   1.424 +        
   1.425 +        return *result ? NS_OK : NS_ERROR_FAILURE;
   1.426 +    }
   1.427 +
   1.428 +    // Virtual destructor since subclass nsPathsDirectoryEnumerator
   1.429 +    // does not re-implement Release()
   1.430 +
   1.431 +    virtual ~nsAppDirectoryEnumerator()
   1.432 +    {
   1.433 +    }
   1.434 +
   1.435 +  protected:
   1.436 +    nsIDirectoryServiceProvider *mProvider;
   1.437 +    const char** mCurrentKey;
   1.438 +    nsCOMPtr<nsIFile> mNext;
   1.439 +};
   1.440 +
   1.441 +NS_IMPL_ISUPPORTS(nsAppDirectoryEnumerator, nsISimpleEnumerator)
   1.442 +
   1.443 +/* nsPathsDirectoryEnumerator and PATH_SEPARATOR
   1.444 + * are not used on MacOS/X. */
   1.445 +
   1.446 +#if defined(XP_WIN) /* Win32 */
   1.447 +#define PATH_SEPARATOR ';'
   1.448 +#else
   1.449 +#define PATH_SEPARATOR ':'
   1.450 +#endif
   1.451 +
   1.452 +class nsPathsDirectoryEnumerator : public nsAppDirectoryEnumerator
   1.453 +{
   1.454 +  public:
   1.455 +    /**
   1.456 +     * aKeyList is a null-terminated list.
   1.457 +     * The first element is a path list.
   1.458 +     * The remainder are properties provided by aProvider.
   1.459 +     * They do not need to be publicly defined keys.
   1.460 +     */
   1.461 +    nsPathsDirectoryEnumerator(nsIDirectoryServiceProvider *aProvider,
   1.462 +                               const char* aKeyList[]) :
   1.463 +    nsAppDirectoryEnumerator(aProvider, aKeyList+1),
   1.464 +    mEndPath(aKeyList[0])
   1.465 +    {
   1.466 +    }
   1.467 +
   1.468 +    NS_IMETHOD HasMoreElements(bool *result) 
   1.469 +    {
   1.470 +        if (mEndPath)
   1.471 +            while (!mNext && *mEndPath)
   1.472 +            {
   1.473 +                const char *pathVar = mEndPath;
   1.474 +           
   1.475 +                // skip PATH_SEPARATORs at the begining of the mEndPath
   1.476 +                while (*pathVar == PATH_SEPARATOR) pathVar++;
   1.477 +
   1.478 +                do { ++mEndPath; } while (*mEndPath && *mEndPath != PATH_SEPARATOR);
   1.479 +
   1.480 +                nsCOMPtr<nsIFile> localFile;
   1.481 +                NS_NewNativeLocalFile(Substring(pathVar, mEndPath),
   1.482 +                                      true,
   1.483 +                                      getter_AddRefs(localFile));
   1.484 +                if (*mEndPath == PATH_SEPARATOR)
   1.485 +                    ++mEndPath;
   1.486 +                // Don't return a "file" (directory) which does not exist.
   1.487 +                bool exists;
   1.488 +                if (localFile &&
   1.489 +                    NS_SUCCEEDED(localFile->Exists(&exists)) &&
   1.490 +                    exists)
   1.491 +                    mNext = localFile;
   1.492 +            }
   1.493 +        if (mNext)
   1.494 +            *result = true;
   1.495 +        else
   1.496 +            nsAppDirectoryEnumerator::HasMoreElements(result);
   1.497 +
   1.498 +        return NS_OK;
   1.499 +    }
   1.500 +
   1.501 +  protected:
   1.502 +    const char *mEndPath;
   1.503 +};
   1.504 +
   1.505 +NS_IMETHODIMP
   1.506 +nsAppFileLocationProvider::GetFiles(const char *prop, nsISimpleEnumerator **_retval)
   1.507 +{
   1.508 +    if (NS_WARN_IF(!_retval))
   1.509 +        return NS_ERROR_INVALID_ARG;
   1.510 +    *_retval = nullptr;
   1.511 +    nsresult rv = NS_ERROR_FAILURE;
   1.512 +    
   1.513 +    if (!nsCRT::strcmp(prop, NS_APP_PLUGINS_DIR_LIST))
   1.514 +    {
   1.515 +#ifdef MOZ_WIDGET_COCOA
   1.516 +        // As of Java for Mac OS X 10.5 Update 10, Apple has (in effect) deprecated Java Plugin2 on
   1.517 +        // on OS X 10.5, and removed the soft link to it from /Library/Internet Plug-Ins/.  Java
   1.518 +        // Plugin2 is still present and usable, but there are no longer any links to it in the
   1.519 +        // "normal" locations.  So we won't be able to find it unless we look in the "non-normal"
   1.520 +        // location where it actually is.  Safari can use the WebKit-specific JavaPluginCocoa.bundle,
   1.521 +        // which (of course) is still fully supported on OS X 10.5.  But we have no alternative to
   1.522 +        // using Java Plugin2.  For more information see bug 668639.
   1.523 +        static const char* keys[] = { NS_APP_PLUGINS_DIR, NS_MACOSX_USER_PLUGIN_DIR,
   1.524 +                                      NS_MACOSX_LOCAL_PLUGIN_DIR,
   1.525 +                                      IsOSXLeopard() ? NS_MACOSX_JAVA2_PLUGIN_DIR : nullptr, nullptr };
   1.526 +        *_retval = new nsAppDirectoryEnumerator(this, keys);
   1.527 +#else
   1.528 +#ifdef XP_UNIX
   1.529 +        static const char* keys[] = { nullptr, NS_USER_PLUGINS_DIR, NS_APP_PLUGINS_DIR, NS_SYSTEM_PLUGINS_DIR, nullptr };
   1.530 +#else
   1.531 +        static const char* keys[] = { nullptr, NS_USER_PLUGINS_DIR, NS_APP_PLUGINS_DIR, nullptr };
   1.532 +#endif
   1.533 +        if (!keys[0] && !(keys[0] = PR_GetEnv("MOZ_PLUGIN_PATH"))) {
   1.534 +            static const char nullstr = 0;
   1.535 +            keys[0] = &nullstr;
   1.536 +        }
   1.537 +        *_retval = new nsPathsDirectoryEnumerator(this, keys);
   1.538 +#endif
   1.539 +        NS_IF_ADDREF(*_retval);
   1.540 +        rv = *_retval ? NS_OK : NS_ERROR_OUT_OF_MEMORY;        
   1.541 +    }
   1.542 +    if (!nsCRT::strcmp(prop, NS_APP_SEARCH_DIR_LIST))
   1.543 +    {
   1.544 +        static const char* keys[] = { nullptr, NS_APP_SEARCH_DIR, NS_APP_USER_SEARCH_DIR, nullptr };
   1.545 +        if (!keys[0] && !(keys[0] = PR_GetEnv("MOZ_SEARCH_ENGINE_PATH"))) {
   1.546 +            static const char nullstr = 0;
   1.547 +            keys[0] = &nullstr;
   1.548 +        }
   1.549 +        *_retval = new nsPathsDirectoryEnumerator(this, keys);
   1.550 +        NS_IF_ADDREF(*_retval);
   1.551 +        rv = *_retval ? NS_OK : NS_ERROR_OUT_OF_MEMORY;        
   1.552 +    }
   1.553 +    return rv;
   1.554 +}
   1.555 +
   1.556 +#if defined(MOZ_WIDGET_COCOA)
   1.557 +bool
   1.558 +nsAppFileLocationProvider::IsOSXLeopard()
   1.559 +{
   1.560 +    static SInt32 version = 0;
   1.561 +
   1.562 +    if (!version) {
   1.563 +        OSErr err = ::Gestalt(gestaltSystemVersion, &version);
   1.564 +        if (err != noErr) {
   1.565 +            version = 0;
   1.566 +        } else {
   1.567 +            version &= 0xFFFF; // The system version is in the low order word
   1.568 +        }
   1.569 +    }
   1.570 +
   1.571 +    return ((version >= 0x1050) && (version < 0x1060));
   1.572 +}
   1.573 +#endif

mercurial