michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsAppRunner.h" michael@0: #include "nsToolkitCompsCID.h" michael@0: #include "nsXREDirProvider.h" michael@0: michael@0: #include "jsapi.h" michael@0: michael@0: #include "nsIJSRuntimeService.h" michael@0: #include "nsIAppStartup.h" michael@0: #include "nsIDirectoryEnumerator.h" michael@0: #include "nsIFile.h" michael@0: #include "nsIObserver.h" michael@0: #include "nsIObserverService.h" michael@0: #include "nsISimpleEnumerator.h" michael@0: #include "nsIToolkitChromeRegistry.h" michael@0: michael@0: #include "nsAppDirectoryServiceDefs.h" michael@0: #include "nsDirectoryServiceDefs.h" michael@0: #include "nsDirectoryServiceUtils.h" michael@0: #include "nsXULAppAPI.h" michael@0: #include "nsCategoryManagerUtils.h" michael@0: michael@0: #include "nsINIParser.h" michael@0: #include "nsDependentString.h" michael@0: #include "nsCOMArray.h" michael@0: #include "nsArrayEnumerator.h" michael@0: #include "nsEnumeratorUtils.h" michael@0: #include "nsReadableUtils.h" michael@0: #include "nsXPCOMPrivate.h" // for XPCOM_FILE_PATH_SEPARATOR michael@0: #include "mozilla/Services.h" michael@0: #include "mozilla/Omnijar.h" michael@0: #include "mozilla/Preferences.h" michael@0: #include "mozilla/Telemetry.h" michael@0: michael@0: #include michael@0: michael@0: #ifdef XP_WIN michael@0: #include michael@0: #include michael@0: #endif michael@0: #ifdef XP_MACOSX michael@0: #include "nsILocalFileMac.h" michael@0: // for chflags() michael@0: #include michael@0: #include michael@0: #endif michael@0: #ifdef XP_UNIX michael@0: #include michael@0: #endif michael@0: michael@0: #if defined(XP_MACOSX) michael@0: #define APP_REGISTRY_NAME "Application Registry" michael@0: #elif defined(XP_WIN) michael@0: #define APP_REGISTRY_NAME "registry.dat" michael@0: #else michael@0: #define APP_REGISTRY_NAME "appreg" michael@0: #endif michael@0: michael@0: #define PREF_OVERRIDE_DIRNAME "preferences" michael@0: michael@0: static already_AddRefed michael@0: CloneAndAppend(nsIFile* aFile, const char* name) michael@0: { michael@0: nsCOMPtr file; michael@0: aFile->Clone(getter_AddRefs(file)); michael@0: file->AppendNative(nsDependentCString(name)); michael@0: return file.forget(); michael@0: } michael@0: michael@0: nsXREDirProvider* gDirServiceProvider = nullptr; michael@0: michael@0: nsXREDirProvider::nsXREDirProvider() : michael@0: mProfileNotified(false) michael@0: { michael@0: gDirServiceProvider = this; michael@0: } michael@0: michael@0: nsXREDirProvider::~nsXREDirProvider() michael@0: { michael@0: gDirServiceProvider = nullptr; michael@0: } michael@0: michael@0: nsXREDirProvider* michael@0: nsXREDirProvider::GetSingleton() michael@0: { michael@0: return gDirServiceProvider; michael@0: } michael@0: michael@0: nsresult michael@0: nsXREDirProvider::Initialize(nsIFile *aXULAppDir, michael@0: nsIFile *aGREDir, michael@0: nsIDirectoryServiceProvider* aAppProvider) michael@0: { michael@0: NS_ENSURE_ARG(aXULAppDir); michael@0: NS_ENSURE_ARG(aGREDir); michael@0: michael@0: mAppProvider = aAppProvider; michael@0: mXULAppDir = aXULAppDir; michael@0: mGREDir = aGREDir; michael@0: michael@0: if (!mProfileDir) { michael@0: nsCOMPtr app(do_QueryInterface(mAppProvider)); michael@0: if (app) { michael@0: bool per = false; michael@0: app->GetFile(NS_APP_USER_PROFILE_50_DIR, &per, getter_AddRefs(mProfileDir)); michael@0: NS_ASSERTION(per, "NS_APP_USER_PROFILE_50_DIR must be persistent!"); michael@0: NS_ASSERTION(mProfileDir, "NS_APP_USER_PROFILE_50_DIR not defined! This shouldn't happen!"); michael@0: } michael@0: } michael@0: michael@0: LoadAppBundleDirs(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsXREDirProvider::SetProfile(nsIFile* aDir, nsIFile* aLocalDir) michael@0: { michael@0: NS_ASSERTION(aDir && aLocalDir, "We don't support no-profile apps yet!"); michael@0: michael@0: nsresult rv; michael@0: michael@0: rv = EnsureDirectoryExists(aDir); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: rv = EnsureDirectoryExists(aLocalDir); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: #ifdef XP_MACOSX michael@0: bool same; michael@0: if (NS_SUCCEEDED(aDir->Equals(aLocalDir, &same)) && !same) { michael@0: // Ensure that the cache directory is not indexed by Spotlight michael@0: // (bug 718910). At least on OS X, the cache directory (under michael@0: // ~/Library/Caches/) is always the "local" user profile michael@0: // directory. This is confusing, since *both* user profile michael@0: // directories are "local" (they both exist under the user's michael@0: // home directory). But this usage dates back at least as far michael@0: // as the patch for bug 291033, where "local" seems to mean michael@0: // "suitable for temporary storage". Don't hide the cache michael@0: // directory if by some chance it and the "non-local" profile michael@0: // directory are the same -- there are bad side effects from michael@0: // hiding a profile directory under /Library/Application Support/ michael@0: // (see bug 801883). michael@0: nsAutoCString cacheDir; michael@0: if (NS_SUCCEEDED(aLocalDir->GetNativePath(cacheDir))) { michael@0: if (chflags(cacheDir.get(), UF_HIDDEN)) { michael@0: NS_WARNING("Failed to set Cache directory to HIDDEN."); michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: mProfileDir = aDir; michael@0: mProfileLocalDir = aLocalDir; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMPL_QUERY_INTERFACE(nsXREDirProvider, michael@0: nsIDirectoryServiceProvider, michael@0: nsIDirectoryServiceProvider2, michael@0: nsIProfileStartup) michael@0: michael@0: NS_IMETHODIMP_(MozExternalRefCountType) michael@0: nsXREDirProvider::AddRef() michael@0: { michael@0: return 1; michael@0: } michael@0: michael@0: NS_IMETHODIMP_(MozExternalRefCountType) michael@0: nsXREDirProvider::Release() michael@0: { michael@0: return 0; michael@0: } michael@0: michael@0: nsresult michael@0: nsXREDirProvider::GetUserProfilesRootDir(nsIFile** aResult, michael@0: const nsACString* aProfileName, michael@0: const nsACString* aAppName, michael@0: const nsACString* aVendorName) michael@0: { michael@0: nsCOMPtr file; michael@0: nsresult rv = GetUserDataDirectory(getter_AddRefs(file), michael@0: false, michael@0: aProfileName, aAppName, aVendorName); michael@0: michael@0: if (NS_SUCCEEDED(rv)) { michael@0: // We must create the profile directory here if it does not exist. michael@0: nsresult tmp = EnsureDirectoryExists(file); michael@0: if (NS_FAILED(tmp)) { michael@0: rv = tmp; michael@0: } michael@0: } michael@0: file.swap(*aResult); michael@0: return rv; michael@0: } michael@0: michael@0: nsresult michael@0: nsXREDirProvider::GetUserProfilesLocalDir(nsIFile** aResult, michael@0: const nsACString* aProfileName, michael@0: const nsACString* aAppName, michael@0: const nsACString* aVendorName) michael@0: { michael@0: nsCOMPtr file; michael@0: nsresult rv = GetUserDataDirectory(getter_AddRefs(file), michael@0: true, michael@0: aProfileName, aAppName, aVendorName); michael@0: michael@0: if (NS_SUCCEEDED(rv)) { michael@0: // We must create the profile directory here if it does not exist. michael@0: nsresult tmp = EnsureDirectoryExists(file); michael@0: if (NS_FAILED(tmp)) { michael@0: rv = tmp; michael@0: } michael@0: } michael@0: file.swap(*aResult); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsXREDirProvider::GetFile(const char* aProperty, bool* aPersistent, michael@0: nsIFile** aFile) michael@0: { michael@0: nsresult rv; michael@0: michael@0: bool gettingProfile = false; michael@0: michael@0: if (!strcmp(aProperty, NS_APP_USER_PROFILE_LOCAL_50_DIR)) { michael@0: // If XRE_NotifyProfile hasn't been called, don't fall through to michael@0: // mAppProvider on the profile keys. michael@0: if (!mProfileNotified) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: if (mProfileLocalDir) michael@0: return mProfileLocalDir->Clone(aFile); michael@0: michael@0: if (mAppProvider) michael@0: return mAppProvider->GetFile(aProperty, aPersistent, aFile); michael@0: michael@0: // This falls through to the case below michael@0: gettingProfile = true; michael@0: } michael@0: if (!strcmp(aProperty, NS_APP_USER_PROFILE_50_DIR) || gettingProfile) { michael@0: if (!mProfileNotified) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: if (mProfileDir) michael@0: return mProfileDir->Clone(aFile); michael@0: michael@0: if (mAppProvider) michael@0: return mAppProvider->GetFile(aProperty, aPersistent, aFile); michael@0: michael@0: // If we don't succeed here, bail early so that we aren't reentrant michael@0: // through the "GetProfileDir" call below. michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: if (mAppProvider) { michael@0: rv = mAppProvider->GetFile(aProperty, aPersistent, aFile); michael@0: if (NS_SUCCEEDED(rv) && *aFile) michael@0: return rv; michael@0: } michael@0: michael@0: *aPersistent = true; michael@0: michael@0: if (!strcmp(aProperty, NS_GRE_DIR)) { michael@0: return mGREDir->Clone(aFile); michael@0: } michael@0: else if (!strcmp(aProperty, NS_OS_CURRENT_PROCESS_DIR) || michael@0: !strcmp(aProperty, NS_APP_INSTALL_CLEANUP_DIR)) { michael@0: return GetAppDir()->Clone(aFile); michael@0: } michael@0: michael@0: rv = NS_ERROR_FAILURE; michael@0: nsCOMPtr file; michael@0: michael@0: if (!strcmp(aProperty, NS_APP_PROFILE_DEFAULTS_50_DIR) || michael@0: !strcmp(aProperty, NS_APP_PROFILE_DEFAULTS_NLOC_50_DIR)) { michael@0: return GetProfileDefaultsDir(aFile); michael@0: } michael@0: else if (!strcmp(aProperty, NS_APP_PREF_DEFAULTS_50_DIR)) michael@0: { michael@0: // return the GRE default prefs directory here, and the app default prefs michael@0: // directory (if applicable) in NS_APP_PREFS_DEFAULTS_DIR_LIST. michael@0: rv = mGREDir->Clone(getter_AddRefs(file)); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: rv = file->AppendNative(NS_LITERAL_CSTRING("defaults")); michael@0: if (NS_SUCCEEDED(rv)) michael@0: rv = file->AppendNative(NS_LITERAL_CSTRING("pref")); michael@0: } michael@0: } michael@0: else if (!strcmp(aProperty, NS_APP_APPLICATION_REGISTRY_DIR) || michael@0: !strcmp(aProperty, XRE_USER_APP_DATA_DIR)) { michael@0: rv = GetUserAppDataDirectory(getter_AddRefs(file)); michael@0: } michael@0: else if (!strcmp(aProperty, XRE_UPDATE_ROOT_DIR)) { michael@0: rv = GetUpdateRootDir(getter_AddRefs(file)); michael@0: } michael@0: else if (!strcmp(aProperty, NS_APP_APPLICATION_REGISTRY_FILE)) { michael@0: rv = GetUserAppDataDirectory(getter_AddRefs(file)); michael@0: if (NS_SUCCEEDED(rv)) michael@0: rv = file->AppendNative(NS_LITERAL_CSTRING(APP_REGISTRY_NAME)); michael@0: } michael@0: else if (!strcmp(aProperty, NS_APP_USER_PROFILES_ROOT_DIR)) { michael@0: rv = GetUserProfilesRootDir(getter_AddRefs(file), nullptr, nullptr, nullptr); michael@0: } michael@0: else if (!strcmp(aProperty, NS_APP_USER_PROFILES_LOCAL_ROOT_DIR)) { michael@0: rv = GetUserProfilesLocalDir(getter_AddRefs(file), nullptr, nullptr, nullptr); michael@0: } michael@0: else if (!strcmp(aProperty, XRE_EXECUTABLE_FILE) && gArgv[0]) { michael@0: nsCOMPtr lf; michael@0: rv = XRE_GetBinaryPath(gArgv[0], getter_AddRefs(lf)); michael@0: if (NS_SUCCEEDED(rv)) michael@0: file = lf; michael@0: } michael@0: michael@0: else if (!strcmp(aProperty, NS_APP_PROFILE_DIR_STARTUP) && mProfileDir) { michael@0: return mProfileDir->Clone(aFile); michael@0: } michael@0: else if (!strcmp(aProperty, NS_APP_PROFILE_LOCAL_DIR_STARTUP)) { michael@0: if (mProfileLocalDir) michael@0: return mProfileLocalDir->Clone(aFile); michael@0: michael@0: if (mProfileDir) michael@0: return mProfileDir->Clone(aFile); michael@0: michael@0: if (mAppProvider) michael@0: return mAppProvider->GetFile(NS_APP_PROFILE_DIR_STARTUP, aPersistent, michael@0: aFile); michael@0: } michael@0: #if defined(XP_UNIX) || defined(XP_MACOSX) michael@0: else if (!strcmp(aProperty, XRE_SYS_LOCAL_EXTENSION_PARENT_DIR)) { michael@0: #ifdef ENABLE_SYSTEM_EXTENSION_DIRS michael@0: return GetSystemExtensionsDirectory(aFile); michael@0: #else michael@0: return NS_ERROR_FAILURE; michael@0: #endif michael@0: } michael@0: #endif michael@0: #if defined(XP_UNIX) && !defined(XP_MACOSX) michael@0: else if (!strcmp(aProperty, XRE_SYS_SHARE_EXTENSION_PARENT_DIR)) { michael@0: #ifdef ENABLE_SYSTEM_EXTENSION_DIRS michael@0: #if defined(__OpenBSD__) || defined(__FreeBSD__) michael@0: static const char *const sysLExtDir = "/usr/local/share/mozilla/extensions"; michael@0: #else michael@0: static const char *const sysLExtDir = "/usr/share/mozilla/extensions"; michael@0: #endif michael@0: return NS_NewNativeLocalFile(nsDependentCString(sysLExtDir), michael@0: false, aFile); michael@0: #else michael@0: return NS_ERROR_FAILURE; michael@0: #endif michael@0: } michael@0: #endif michael@0: else if (!strcmp(aProperty, XRE_USER_SYS_EXTENSION_DIR)) { michael@0: #ifdef ENABLE_SYSTEM_EXTENSION_DIRS michael@0: return GetSysUserExtensionsDirectory(aFile); michael@0: #else michael@0: return NS_ERROR_FAILURE; michael@0: #endif michael@0: } michael@0: else if (!strcmp(aProperty, XRE_APP_DISTRIBUTION_DIR)) { michael@0: bool persistent = false; michael@0: rv = GetFile(XRE_EXECUTABLE_FILE, &persistent, getter_AddRefs(file)); michael@0: if (NS_SUCCEEDED(rv)) michael@0: rv = file->SetNativeLeafName(NS_LITERAL_CSTRING("distribution")); michael@0: } michael@0: else if (NS_SUCCEEDED(GetProfileStartupDir(getter_AddRefs(file)))) { michael@0: // We need to allow component, xpt, and chrome registration to michael@0: // occur prior to the profile-after-change notification. michael@0: if (!strcmp(aProperty, NS_APP_USER_CHROME_DIR)) { michael@0: rv = file->AppendNative(NS_LITERAL_CSTRING("chrome")); michael@0: } michael@0: } michael@0: michael@0: if (NS_SUCCEEDED(rv) && file) { michael@0: NS_ADDREF(*aFile = file); michael@0: return NS_OK; michael@0: } michael@0: michael@0: bool ensureFilePermissions = false; michael@0: michael@0: if (NS_SUCCEEDED(GetProfileDir(getter_AddRefs(file)))) { michael@0: if (!strcmp(aProperty, NS_APP_PREFS_50_DIR)) { michael@0: rv = NS_OK; michael@0: } michael@0: else if (!strcmp(aProperty, NS_APP_PREFS_50_FILE)) { michael@0: rv = file->AppendNative(NS_LITERAL_CSTRING("prefs.js")); michael@0: } michael@0: else if (!strcmp(aProperty, NS_METRO_APP_PREFS_50_FILE)) { michael@0: rv = file->AppendNative(NS_LITERAL_CSTRING("metro-prefs.js")); michael@0: } michael@0: else if (!strcmp(aProperty, NS_LOCALSTORE_UNSAFE_FILE)) { michael@0: rv = file->AppendNative(NS_LITERAL_CSTRING("localstore.rdf")); michael@0: } michael@0: else if (!strcmp(aProperty, NS_APP_LOCALSTORE_50_FILE)) { michael@0: if (gSafeMode) { michael@0: rv = file->AppendNative(NS_LITERAL_CSTRING("localstore-safe.rdf")); michael@0: file->Remove(false); michael@0: } michael@0: else { michael@0: rv = file->AppendNative(NS_LITERAL_CSTRING("localstore.rdf")); michael@0: EnsureProfileFileExists(file); michael@0: ensureFilePermissions = true; michael@0: } michael@0: } michael@0: else if (!strcmp(aProperty, NS_APP_USER_MIMETYPES_50_FILE)) { michael@0: rv = file->AppendNative(NS_LITERAL_CSTRING("mimeTypes.rdf")); michael@0: EnsureProfileFileExists(file); michael@0: ensureFilePermissions = true; michael@0: } michael@0: else if (!strcmp(aProperty, NS_APP_DOWNLOADS_50_FILE)) { michael@0: rv = file->AppendNative(NS_LITERAL_CSTRING("downloads.rdf")); michael@0: } michael@0: else if (!strcmp(aProperty, NS_APP_PREFS_OVERRIDE_DIR)) { michael@0: rv = mProfileDir->Clone(getter_AddRefs(file)); michael@0: nsresult tmp = file->AppendNative(NS_LITERAL_CSTRING(PREF_OVERRIDE_DIRNAME)); michael@0: if (NS_FAILED(tmp)) { michael@0: rv = tmp; michael@0: } michael@0: tmp = EnsureDirectoryExists(file); michael@0: if (NS_FAILED(tmp)) { michael@0: rv = tmp; michael@0: } michael@0: } michael@0: } michael@0: if (NS_FAILED(rv) || !file) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: if (ensureFilePermissions) { michael@0: bool fileToEnsureExists; michael@0: bool isWritable; michael@0: if (NS_SUCCEEDED(file->Exists(&fileToEnsureExists)) && fileToEnsureExists michael@0: && NS_SUCCEEDED(file->IsWritable(&isWritable)) && !isWritable) { michael@0: uint32_t permissions; michael@0: if (NS_SUCCEEDED(file->GetPermissions(&permissions))) { michael@0: rv = file->SetPermissions(permissions | 0600); michael@0: NS_ASSERTION(NS_SUCCEEDED(rv), "failed to ensure file permissions"); michael@0: } michael@0: } michael@0: } michael@0: michael@0: NS_ADDREF(*aFile = file); michael@0: return NS_OK; michael@0: } michael@0: michael@0: static void michael@0: LoadDirIntoArray(nsIFile* dir, michael@0: const char *const *aAppendList, michael@0: nsCOMArray& aDirectories) michael@0: { michael@0: if (!dir) michael@0: return; michael@0: michael@0: nsCOMPtr subdir; michael@0: dir->Clone(getter_AddRefs(subdir)); michael@0: if (!subdir) michael@0: return; michael@0: michael@0: for (const char *const *a = aAppendList; *a; ++a) { michael@0: subdir->AppendNative(nsDependentCString(*a)); michael@0: } michael@0: michael@0: bool exists; michael@0: if (NS_SUCCEEDED(subdir->Exists(&exists)) && exists) { michael@0: aDirectories.AppendObject(subdir); michael@0: } michael@0: } michael@0: michael@0: static void michael@0: LoadDirsIntoArray(nsCOMArray& aSourceDirs, michael@0: const char *const* aAppendList, michael@0: nsCOMArray& aDirectories) michael@0: { michael@0: nsCOMPtr appended; michael@0: bool exists; michael@0: michael@0: for (int32_t i = 0; i < aSourceDirs.Count(); ++i) { michael@0: aSourceDirs[i]->Clone(getter_AddRefs(appended)); michael@0: if (!appended) michael@0: continue; michael@0: michael@0: nsAutoCString leaf; michael@0: appended->GetNativeLeafName(leaf); michael@0: if (!Substring(leaf, leaf.Length() - 4).Equals(NS_LITERAL_CSTRING(".xpi"))) { michael@0: LoadDirIntoArray(appended, michael@0: aAppendList, michael@0: aDirectories); michael@0: } michael@0: else if (NS_SUCCEEDED(appended->Exists(&exists)) && exists) michael@0: aDirectories.AppendObject(appended); michael@0: } michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsXREDirProvider::GetFiles(const char* aProperty, nsISimpleEnumerator** aResult) michael@0: { michael@0: nsresult rv; michael@0: michael@0: nsCOMPtr appEnum; michael@0: nsCOMPtr michael@0: appP2(do_QueryInterface(mAppProvider)); michael@0: if (appP2) { michael@0: rv = appP2->GetFiles(aProperty, getter_AddRefs(appEnum)); michael@0: if (NS_FAILED(rv)) { michael@0: appEnum = nullptr; michael@0: } michael@0: else if (rv != NS_SUCCESS_AGGREGATE_RESULT) { michael@0: NS_ADDREF(*aResult = appEnum); michael@0: return NS_OK; michael@0: } michael@0: } michael@0: michael@0: nsCOMPtr xreEnum; michael@0: rv = GetFilesInternal(aProperty, getter_AddRefs(xreEnum)); michael@0: if (NS_FAILED(rv)) { michael@0: if (appEnum) { michael@0: NS_ADDREF(*aResult = appEnum); michael@0: return NS_SUCCESS_AGGREGATE_RESULT; michael@0: } michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: rv = NS_NewUnionEnumerator(aResult, appEnum, xreEnum); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: return NS_SUCCESS_AGGREGATE_RESULT; michael@0: } michael@0: michael@0: static void michael@0: LoadExtensionDirectories(nsINIParser &parser, michael@0: const char *aSection, michael@0: nsCOMArray &aDirectories, michael@0: NSLocationType aType) michael@0: { michael@0: nsresult rv; michael@0: int32_t i = 0; michael@0: do { michael@0: nsAutoCString buf("Extension"); michael@0: buf.AppendInt(i++); michael@0: michael@0: nsAutoCString path; michael@0: rv = parser.GetString(aSection, buf.get(), path); michael@0: if (NS_FAILED(rv)) michael@0: return; michael@0: michael@0: nsCOMPtr dir = do_CreateInstance("@mozilla.org/file/local;1", &rv); michael@0: if (NS_FAILED(rv)) michael@0: continue; michael@0: michael@0: rv = dir->SetPersistentDescriptor(path); michael@0: if (NS_FAILED(rv)) michael@0: continue; michael@0: michael@0: aDirectories.AppendObject(dir); michael@0: if (Substring(path, path.Length() - 4).Equals(NS_LITERAL_CSTRING(".xpi"))) { michael@0: XRE_AddJarManifestLocation(aType, dir); michael@0: } michael@0: else { michael@0: nsCOMPtr manifest = michael@0: CloneAndAppend(dir, "chrome.manifest"); michael@0: XRE_AddManifestLocation(aType, manifest); michael@0: } michael@0: } michael@0: while (true); michael@0: } michael@0: michael@0: void michael@0: nsXREDirProvider::LoadExtensionBundleDirectories() michael@0: { michael@0: if (!mozilla::Preferences::GetBool("extensions.defaultProviders.enabled", true)) michael@0: return; michael@0: michael@0: if (mProfileDir && !gSafeMode) { michael@0: nsCOMPtr extensionsINI; michael@0: mProfileDir->Clone(getter_AddRefs(extensionsINI)); michael@0: if (!extensionsINI) michael@0: return; michael@0: michael@0: extensionsINI->AppendNative(NS_LITERAL_CSTRING("extensions.ini")); michael@0: michael@0: nsCOMPtr extensionsINILF = michael@0: do_QueryInterface(extensionsINI); michael@0: if (!extensionsINILF) michael@0: return; michael@0: michael@0: nsINIParser parser; michael@0: nsresult rv = parser.Init(extensionsINILF); michael@0: if (NS_FAILED(rv)) michael@0: return; michael@0: michael@0: LoadExtensionDirectories(parser, "ExtensionDirs", mExtensionDirectories, michael@0: NS_COMPONENT_LOCATION); michael@0: LoadExtensionDirectories(parser, "ThemeDirs", mThemeDirectories, michael@0: NS_SKIN_LOCATION); michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsXREDirProvider::LoadAppBundleDirs() michael@0: { michael@0: nsCOMPtr dir; michael@0: bool persistent = false; michael@0: nsresult rv = GetFile(XRE_EXECUTABLE_FILE, &persistent, getter_AddRefs(dir)); michael@0: if (NS_FAILED(rv)) michael@0: return; michael@0: michael@0: dir->SetNativeLeafName(NS_LITERAL_CSTRING("distribution")); michael@0: dir->AppendNative(NS_LITERAL_CSTRING("bundles")); michael@0: michael@0: nsCOMPtr e; michael@0: rv = dir->GetDirectoryEntries(getter_AddRefs(e)); michael@0: if (NS_FAILED(rv)) michael@0: return; michael@0: michael@0: nsCOMPtr files = do_QueryInterface(e); michael@0: if (!files) michael@0: return; michael@0: michael@0: nsCOMPtr subdir; michael@0: while (NS_SUCCEEDED(files->GetNextFile(getter_AddRefs(subdir))) && subdir) { michael@0: mAppBundleDirectories.AppendObject(subdir); michael@0: michael@0: nsCOMPtr manifest = michael@0: CloneAndAppend(subdir, "chrome.manifest"); michael@0: XRE_AddManifestLocation(NS_COMPONENT_LOCATION, manifest); michael@0: } michael@0: } michael@0: michael@0: static const char *const kAppendPrefDir[] = { "defaults", "preferences", nullptr }; michael@0: michael@0: #ifdef DEBUG_bsmedberg michael@0: static void michael@0: DumpFileArray(const char *key, michael@0: nsCOMArray dirs) michael@0: { michael@0: fprintf(stderr, "nsXREDirProvider::GetFilesInternal(%s)\n", key); michael@0: michael@0: nsAutoCString path; michael@0: for (int32_t i = 0; i < dirs.Count(); ++i) { michael@0: dirs[i]->GetNativePath(path); michael@0: fprintf(stderr, " %s\n", path.get()); michael@0: } michael@0: } michael@0: #endif // DEBUG_bsmedberg michael@0: michael@0: nsresult michael@0: nsXREDirProvider::GetFilesInternal(const char* aProperty, michael@0: nsISimpleEnumerator** aResult) michael@0: { michael@0: nsresult rv = NS_OK; michael@0: *aResult = nullptr; michael@0: michael@0: if (!strcmp(aProperty, XRE_EXTENSIONS_DIR_LIST)) { michael@0: nsCOMArray directories; michael@0: michael@0: static const char *const kAppendNothing[] = { nullptr }; michael@0: michael@0: LoadDirsIntoArray(mAppBundleDirectories, michael@0: kAppendNothing, directories); michael@0: LoadDirsIntoArray(mExtensionDirectories, michael@0: kAppendNothing, directories); michael@0: michael@0: rv = NS_NewArrayEnumerator(aResult, directories); michael@0: } michael@0: else if (!strcmp(aProperty, NS_APP_PREFS_DEFAULTS_DIR_LIST)) { michael@0: nsCOMArray directories; michael@0: michael@0: LoadDirIntoArray(mXULAppDir, kAppendPrefDir, directories); michael@0: LoadDirsIntoArray(mAppBundleDirectories, michael@0: kAppendPrefDir, directories); michael@0: michael@0: rv = NS_NewArrayEnumerator(aResult, directories); michael@0: } michael@0: else if (!strcmp(aProperty, NS_EXT_PREFS_DEFAULTS_DIR_LIST)) { michael@0: nsCOMArray directories; michael@0: michael@0: LoadDirsIntoArray(mExtensionDirectories, michael@0: kAppendPrefDir, directories); michael@0: michael@0: if (mProfileDir) { michael@0: nsCOMPtr overrideFile; michael@0: mProfileDir->Clone(getter_AddRefs(overrideFile)); michael@0: overrideFile->AppendNative(NS_LITERAL_CSTRING(PREF_OVERRIDE_DIRNAME)); michael@0: michael@0: bool exists; michael@0: if (NS_SUCCEEDED(overrideFile->Exists(&exists)) && exists) michael@0: directories.AppendObject(overrideFile); michael@0: } michael@0: michael@0: rv = NS_NewArrayEnumerator(aResult, directories); michael@0: } michael@0: else if (!strcmp(aProperty, NS_APP_CHROME_DIR_LIST)) { michael@0: // NS_APP_CHROME_DIR_LIST is only used to get default (native) icons michael@0: // for OS window decoration. michael@0: michael@0: static const char *const kAppendChromeDir[] = { "chrome", nullptr }; michael@0: nsCOMArray directories; michael@0: LoadDirIntoArray(mXULAppDir, michael@0: kAppendChromeDir, michael@0: directories); michael@0: LoadDirsIntoArray(mAppBundleDirectories, michael@0: kAppendChromeDir, michael@0: directories); michael@0: LoadDirsIntoArray(mExtensionDirectories, michael@0: kAppendChromeDir, michael@0: directories); michael@0: michael@0: rv = NS_NewArrayEnumerator(aResult, directories); michael@0: } michael@0: else if (!strcmp(aProperty, NS_APP_PLUGINS_DIR_LIST)) { michael@0: nsCOMArray directories; michael@0: michael@0: if (mozilla::Preferences::GetBool("plugins.load_appdir_plugins", false)) { michael@0: nsCOMPtr appdir; michael@0: rv = XRE_GetBinaryPath(gArgv[0], getter_AddRefs(appdir)); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: appdir->SetNativeLeafName(NS_LITERAL_CSTRING("plugins")); michael@0: directories.AppendObject(appdir); michael@0: } michael@0: } michael@0: michael@0: static const char *const kAppendPlugins[] = { "plugins", nullptr }; michael@0: michael@0: // The root dirserviceprovider does quite a bit for us: we're mainly michael@0: // interested in xulapp and extension-provided plugins. michael@0: LoadDirsIntoArray(mAppBundleDirectories, michael@0: kAppendPlugins, michael@0: directories); michael@0: LoadDirsIntoArray(mExtensionDirectories, michael@0: kAppendPlugins, michael@0: directories); michael@0: michael@0: if (mProfileDir) { michael@0: nsCOMArray profileDir; michael@0: profileDir.AppendObject(mProfileDir); michael@0: LoadDirsIntoArray(profileDir, michael@0: kAppendPlugins, michael@0: directories); michael@0: } michael@0: michael@0: rv = NS_NewArrayEnumerator(aResult, directories); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: rv = NS_SUCCESS_AGGREGATE_RESULT; michael@0: } michael@0: else michael@0: rv = NS_ERROR_FAILURE; michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsXREDirProvider::GetDirectory(nsIFile* *aResult) michael@0: { michael@0: NS_ENSURE_TRUE(mProfileDir, NS_ERROR_NOT_INITIALIZED); michael@0: michael@0: return mProfileDir->Clone(aResult); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsXREDirProvider::DoStartup() michael@0: { michael@0: if (!mProfileNotified) { michael@0: nsCOMPtr obsSvc = michael@0: mozilla::services::GetObserverService(); michael@0: if (!obsSvc) return NS_ERROR_FAILURE; michael@0: michael@0: mProfileNotified = true; michael@0: michael@0: /* michael@0: Setup prefs before profile-do-change to be able to use them to track michael@0: crashes and because we want to begin crash tracking before other code run michael@0: from this notification since they may cause crashes. michael@0: */ michael@0: nsresult rv = mozilla::Preferences::ResetAndReadUserPrefs(); michael@0: if (NS_FAILED(rv)) NS_WARNING("Failed to setup pref service."); michael@0: michael@0: bool safeModeNecessary = false; michael@0: nsCOMPtr appStartup (do_GetService(NS_APPSTARTUP_CONTRACTID)); michael@0: if (appStartup) { michael@0: rv = appStartup->TrackStartupCrashBegin(&safeModeNecessary); michael@0: if (NS_FAILED(rv) && rv != NS_ERROR_NOT_AVAILABLE) michael@0: NS_WARNING("Error while beginning startup crash tracking"); michael@0: michael@0: if (!gSafeMode && safeModeNecessary) { michael@0: appStartup->RestartInSafeMode(nsIAppStartup::eForceQuit); michael@0: return NS_OK; michael@0: } michael@0: } michael@0: michael@0: static const char16_t kStartup[] = {'s','t','a','r','t','u','p','\0'}; michael@0: obsSvc->NotifyObservers(nullptr, "profile-do-change", kStartup); michael@0: // Init the Extension Manager michael@0: nsCOMPtr em = do_GetService("@mozilla.org/addons/integration;1"); michael@0: if (em) { michael@0: em->Observe(nullptr, "addons-startup", nullptr); michael@0: } else { michael@0: NS_WARNING("Failed to create Addons Manager."); michael@0: } michael@0: michael@0: LoadExtensionBundleDirectories(); michael@0: michael@0: obsSvc->NotifyObservers(nullptr, "load-extension-defaults", nullptr); michael@0: obsSvc->NotifyObservers(nullptr, "profile-after-change", kStartup); michael@0: michael@0: // Any component that has registered for the profile-after-change category michael@0: // should also be created at this time. michael@0: (void)NS_CreateServicesFromCategory("profile-after-change", nullptr, michael@0: "profile-after-change"); michael@0: michael@0: if (gSafeMode && safeModeNecessary) { michael@0: static const char16_t kCrashed[] = {'c','r','a','s','h','e','d','\0'}; michael@0: obsSvc->NotifyObservers(nullptr, "safemode-forced", kCrashed); michael@0: } michael@0: michael@0: // 1 = Regular mode, 2 = Safe mode, 3 = Safe mode forced michael@0: int mode = 1; michael@0: if (gSafeMode) { michael@0: if (safeModeNecessary) michael@0: mode = 3; michael@0: else michael@0: mode = 2; michael@0: } michael@0: mozilla::Telemetry::Accumulate(mozilla::Telemetry::SAFE_MODE_USAGE, mode); michael@0: michael@0: obsSvc->NotifyObservers(nullptr, "profile-initial-state", nullptr); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: nsXREDirProvider::DoShutdown() michael@0: { michael@0: if (mProfileNotified) { michael@0: nsCOMPtr obsSvc = michael@0: mozilla::services::GetObserverService(); michael@0: NS_ASSERTION(obsSvc, "No observer service?"); michael@0: if (obsSvc) { michael@0: static const char16_t kShutdownPersist[] = michael@0: {'s','h','u','t','d','o','w','n','-','p','e','r','s','i','s','t','\0'}; michael@0: obsSvc->NotifyObservers(nullptr, "profile-change-net-teardown", kShutdownPersist); michael@0: obsSvc->NotifyObservers(nullptr, "profile-change-teardown", kShutdownPersist); michael@0: michael@0: // Phase 2c: Now that things are torn down, force JS GC so that things which depend on michael@0: // resources which are about to go away in "profile-before-change" are destroyed first. michael@0: michael@0: nsCOMPtr rtsvc michael@0: (do_GetService("@mozilla.org/js/xpc/RuntimeService;1")); michael@0: if (rtsvc) michael@0: { michael@0: JSRuntime *rt = nullptr; michael@0: rtsvc->GetRuntime(&rt); michael@0: if (rt) michael@0: ::JS_GC(rt); michael@0: } michael@0: michael@0: // Phase 3: Notify observers of a profile change michael@0: obsSvc->NotifyObservers(nullptr, "profile-before-change", kShutdownPersist); michael@0: obsSvc->NotifyObservers(nullptr, "profile-before-change2", kShutdownPersist); michael@0: } michael@0: mProfileNotified = false; michael@0: } michael@0: } michael@0: michael@0: #ifdef XP_WIN michael@0: static nsresult michael@0: GetShellFolderPath(int folder, nsAString& _retval) michael@0: { michael@0: wchar_t* buf; michael@0: uint32_t bufLength = _retval.GetMutableData(&buf, MAXPATHLEN + 3); michael@0: NS_ENSURE_TRUE(bufLength >= (MAXPATHLEN + 3), NS_ERROR_OUT_OF_MEMORY); michael@0: michael@0: nsresult rv = NS_OK; michael@0: michael@0: LPITEMIDLIST pItemIDList = nullptr; michael@0: michael@0: if (SUCCEEDED(SHGetSpecialFolderLocation(nullptr, folder, &pItemIDList)) && michael@0: SHGetPathFromIDListW(pItemIDList, buf)) { michael@0: // We're going to use wcslen (wcsnlen not available in msvc7.1) so make michael@0: // sure to null terminate. michael@0: buf[bufLength - 1] = L'\0'; michael@0: _retval.SetLength(wcslen(buf)); michael@0: } else { michael@0: _retval.SetLength(0); michael@0: rv = NS_ERROR_NOT_AVAILABLE; michael@0: } michael@0: michael@0: CoTaskMemFree(pItemIDList); michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: /** michael@0: * Provides a fallback for getting the path to APPDATA or LOCALAPPDATA by michael@0: * querying the registry when the call to SHGetSpecialFolderLocation or michael@0: * SHGetPathFromIDListW is unable to provide these paths (Bug 513958). michael@0: */ michael@0: static nsresult michael@0: GetRegWindowsAppDataFolder(bool aLocal, nsAString& _retval) michael@0: { michael@0: HKEY key; michael@0: NS_NAMED_LITERAL_STRING(keyName, michael@0: "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"); michael@0: DWORD res = ::RegOpenKeyExW(HKEY_CURRENT_USER, keyName.get(), 0, KEY_READ, michael@0: &key); michael@0: if (res != ERROR_SUCCESS) { michael@0: _retval.SetLength(0); michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: } michael@0: michael@0: DWORD type, size; michael@0: res = RegQueryValueExW(key, (aLocal ? L"Local AppData" : L"AppData"), michael@0: nullptr, &type, nullptr, &size); michael@0: // The call to RegQueryValueExW must succeed, the type must be REG_SZ, the michael@0: // buffer size must not equal 0, and the buffer size be a multiple of 2. michael@0: if (res != ERROR_SUCCESS || type != REG_SZ || size == 0 || size % 2 != 0) { michael@0: ::RegCloseKey(key); michael@0: _retval.SetLength(0); michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: } michael@0: michael@0: // |size| may or may not include room for the terminating null character michael@0: DWORD resultLen = size / 2; michael@0: michael@0: _retval.SetLength(resultLen); michael@0: nsAString::iterator begin; michael@0: _retval.BeginWriting(begin); michael@0: if (begin.size_forward() != resultLen) { michael@0: ::RegCloseKey(key); michael@0: _retval.SetLength(0); michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: } michael@0: michael@0: res = RegQueryValueExW(key, (aLocal ? L"Local AppData" : L"AppData"), michael@0: nullptr, nullptr, (LPBYTE) begin.get(), &size); michael@0: ::RegCloseKey(key); michael@0: if (res != ERROR_SUCCESS) { michael@0: _retval.SetLength(0); michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: } michael@0: michael@0: if (!_retval.CharAt(resultLen - 1)) { michael@0: // It was already null terminated. michael@0: _retval.Truncate(resultLen - 1); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: static bool michael@0: GetCachedHash(HKEY rootKey, const nsAString ®Path, const nsAString &path, michael@0: nsAString &cachedHash) michael@0: { michael@0: HKEY baseKey; michael@0: if (RegOpenKeyExW(rootKey, reinterpret_cast(regPath.BeginReading()), 0, KEY_READ, &baseKey) != michael@0: ERROR_SUCCESS) { michael@0: return false; michael@0: } michael@0: michael@0: wchar_t cachedHashRaw[512]; michael@0: DWORD bufferSize = sizeof(cachedHashRaw); michael@0: LONG result = RegQueryValueExW(baseKey, reinterpret_cast(path.BeginReading()), 0, nullptr, michael@0: (LPBYTE)cachedHashRaw, &bufferSize); michael@0: RegCloseKey(baseKey); michael@0: if (result == ERROR_SUCCESS) { michael@0: cachedHash.Assign(cachedHashRaw); michael@0: } michael@0: return ERROR_SUCCESS == result; michael@0: } michael@0: michael@0: #endif michael@0: michael@0: nsresult michael@0: nsXREDirProvider::GetUpdateRootDir(nsIFile* *aResult) michael@0: { michael@0: nsCOMPtr updRoot; michael@0: #if defined(MOZ_WIDGET_GONK) michael@0: michael@0: nsresult rv = NS_NewNativeLocalFile(nsDependentCString("/data/local"), michael@0: true, michael@0: getter_AddRefs(updRoot)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: #else michael@0: nsCOMPtr appFile; michael@0: bool per = false; michael@0: nsresult rv = GetFile(XRE_EXECUTABLE_FILE, &per, getter_AddRefs(appFile)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: rv = appFile->GetParent(getter_AddRefs(updRoot)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: #ifdef XP_WIN michael@0: michael@0: nsAutoString pathHash; michael@0: bool pathHashResult = false; michael@0: bool hasVendor = gAppData->vendor && strlen(gAppData->vendor) != 0; michael@0: michael@0: nsAutoString appDirPath; michael@0: if (SUCCEEDED(updRoot->GetPath(appDirPath))) { michael@0: michael@0: // Figure out where we should check for a cached hash value. If the michael@0: // application doesn't have the nsXREAppData vendor value defined check michael@0: // under SOFTWARE\Mozilla. michael@0: wchar_t regPath[1024] = { L'\0' }; michael@0: swprintf_s(regPath, mozilla::ArrayLength(regPath), L"SOFTWARE\\%S\\%S\\TaskBarIDs", michael@0: (hasVendor ? gAppData->vendor : "Mozilla"), MOZ_APP_BASENAME); michael@0: michael@0: // If we pre-computed the hash, grab it from the registry. michael@0: pathHashResult = GetCachedHash(HKEY_LOCAL_MACHINE, michael@0: nsDependentString(regPath), appDirPath, michael@0: pathHash); michael@0: if (!pathHashResult) { michael@0: pathHashResult = GetCachedHash(HKEY_CURRENT_USER, michael@0: nsDependentString(regPath), appDirPath, michael@0: pathHash); michael@0: } michael@0: } michael@0: michael@0: // Get the local app data directory and if a vendor name exists append it. michael@0: // If only a product name exists, append it. If neither exist fallback to michael@0: // old handling. We don't use the product name on purpose because we want a michael@0: // shared update directory for different apps run from the same path (like michael@0: // Metro & Desktop). michael@0: nsCOMPtr localDir; michael@0: if (pathHashResult && (hasVendor || gAppData->name) && michael@0: NS_SUCCEEDED(GetUserDataDirectoryHome(getter_AddRefs(localDir), true)) && michael@0: NS_SUCCEEDED(localDir->AppendNative(nsDependentCString(hasVendor ? michael@0: gAppData->vendor : gAppData->name))) && michael@0: NS_SUCCEEDED(localDir->Append(NS_LITERAL_STRING("updates"))) && michael@0: NS_SUCCEEDED(localDir->Append(pathHash))) { michael@0: NS_ADDREF(*aResult = localDir); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsAutoString appPath; michael@0: rv = updRoot->GetPath(appPath); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // AppDir may be a short path. Convert to long path to make sure michael@0: // the consistency of the update folder location michael@0: nsString longPath; michael@0: wchar_t* buf; michael@0: michael@0: uint32_t bufLength = longPath.GetMutableData(&buf, MAXPATHLEN); michael@0: NS_ENSURE_TRUE(bufLength >= MAXPATHLEN, NS_ERROR_OUT_OF_MEMORY); michael@0: michael@0: DWORD len = GetLongPathNameW(appPath.get(), buf, bufLength); michael@0: michael@0: // Failing GetLongPathName() is not fatal. michael@0: if (len <= 0 || len >= bufLength) michael@0: longPath.Assign(appPath); michael@0: else michael@0: longPath.SetLength(len); michael@0: michael@0: // Use \updates\ if app dir is under Program Files to avoid the michael@0: // folder virtualization mess on Windows Vista michael@0: nsAutoString programFiles; michael@0: rv = GetShellFolderPath(CSIDL_PROGRAM_FILES, programFiles); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: programFiles.AppendLiteral("\\"); michael@0: uint32_t programFilesLen = programFiles.Length(); michael@0: michael@0: nsAutoString programName; michael@0: if (_wcsnicmp(programFiles.get(), longPath.get(), programFilesLen) == 0) { michael@0: programName = Substring(longPath, programFilesLen); michael@0: } else { michael@0: // We need the update root directory to live outside of the installation michael@0: // directory, because otherwise the updater writing the log file can cause michael@0: // the directory to be locked, which prevents it from being replaced after michael@0: // background updates. michael@0: programName.AssignASCII(MOZ_APP_NAME); michael@0: } michael@0: michael@0: rv = GetUserLocalDataDirectory(getter_AddRefs(updRoot)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: rv = updRoot->AppendRelativePath(programName); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: #endif michael@0: #endif michael@0: NS_ADDREF(*aResult = updRoot); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsXREDirProvider::GetProfileStartupDir(nsIFile* *aResult) michael@0: { michael@0: if (mProfileDir) michael@0: return mProfileDir->Clone(aResult); michael@0: michael@0: if (mAppProvider) { michael@0: nsCOMPtr needsclone; michael@0: bool dummy; michael@0: nsresult rv = mAppProvider->GetFile(NS_APP_PROFILE_DIR_STARTUP, michael@0: &dummy, michael@0: getter_AddRefs(needsclone)); michael@0: if (NS_SUCCEEDED(rv)) michael@0: return needsclone->Clone(aResult); michael@0: } michael@0: michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: nsresult michael@0: nsXREDirProvider::GetProfileDir(nsIFile* *aResult) michael@0: { michael@0: if (mProfileDir) { michael@0: if (!mProfileNotified) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: return mProfileDir->Clone(aResult); michael@0: } michael@0: michael@0: if (mAppProvider) { michael@0: nsCOMPtr needsclone; michael@0: bool dummy; michael@0: nsresult rv = mAppProvider->GetFile(NS_APP_USER_PROFILE_50_DIR, michael@0: &dummy, michael@0: getter_AddRefs(needsclone)); michael@0: if (NS_SUCCEEDED(rv)) michael@0: return needsclone->Clone(aResult); michael@0: } michael@0: michael@0: return NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, aResult); michael@0: } michael@0: michael@0: nsresult michael@0: nsXREDirProvider::GetUserDataDirectoryHome(nsIFile** aFile, bool aLocal) michael@0: { michael@0: // Copied from nsAppFileLocationProvider (more or less) michael@0: NS_ENSURE_ARG_POINTER(aFile); michael@0: nsCOMPtr localDir; michael@0: michael@0: nsresult rv = GetAppDir()->Clone(getter_AddRefs(localDir)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: int levelsToRemove = 1; // In FF21+, appDir points to browser subdirectory. michael@0: #if defined(XP_MACOSX) michael@0: levelsToRemove += 2; michael@0: #endif michael@0: while (localDir && (levelsToRemove > 0)) { michael@0: // When crawling up the hierarchy, components named "." do not count. michael@0: nsAutoCString removedName; michael@0: rv = localDir->GetNativeLeafName(removedName); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: bool didRemove = !removedName.Equals("."); michael@0: michael@0: // Remove a directory component. michael@0: nsCOMPtr parentDir; michael@0: rv = localDir->GetParent(getter_AddRefs(parentDir)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: localDir = parentDir; michael@0: michael@0: if (didRemove) michael@0: --levelsToRemove; michael@0: } michael@0: michael@0: if (!localDir) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: rv = localDir->AppendRelativeNativePath(NS_LITERAL_CSTRING("TorBrowser" michael@0: XPCOM_FILE_PATH_SEPARATOR "Data" michael@0: XPCOM_FILE_PATH_SEPARATOR "Browser")); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: if (aLocal) { michael@0: rv = localDir->AppendNative(NS_LITERAL_CSTRING("Caches")); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: NS_IF_ADDREF(*aFile = localDir); michael@0: return rv; michael@0: } michael@0: michael@0: nsresult michael@0: nsXREDirProvider::GetSysUserExtensionsDirectory(nsIFile** aFile) michael@0: { michael@0: nsCOMPtr localDir; michael@0: nsresult rv = GetUserDataDirectoryHome(getter_AddRefs(localDir), false); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: rv = AppendSysUserExtensionPath(localDir); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: rv = EnsureDirectoryExists(localDir); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: NS_ADDREF(*aFile = localDir); michael@0: return NS_OK; michael@0: } michael@0: michael@0: #if defined(XP_UNIX) || defined(XP_MACOSX) michael@0: nsresult michael@0: nsXREDirProvider::GetSystemExtensionsDirectory(nsIFile** aFile) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr localDir; michael@0: #if defined(XP_MACOSX) michael@0: FSRef fsRef; michael@0: OSErr err = ::FSFindFolder(kOnSystemDisk, kApplicationSupportFolderType, kCreateFolder, &fsRef); michael@0: NS_ENSURE_FALSE(err, NS_ERROR_FAILURE); michael@0: michael@0: rv = NS_NewNativeLocalFile(EmptyCString(), true, getter_AddRefs(localDir)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsCOMPtr dirFileMac = do_QueryInterface(localDir); michael@0: NS_ENSURE_TRUE(dirFileMac, NS_ERROR_UNEXPECTED); michael@0: michael@0: rv = dirFileMac->InitWithFSRef(&fsRef); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: localDir = do_QueryInterface(dirFileMac, &rv); michael@0: michael@0: static const char* const sXR = "Mozilla"; michael@0: rv = localDir->AppendNative(nsDependentCString(sXR)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: static const char* const sExtensions = "Extensions"; michael@0: rv = localDir->AppendNative(nsDependentCString(sExtensions)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: #elif defined(XP_UNIX) michael@0: static const char *const sysSExtDir = michael@0: #ifdef HAVE_USR_LIB64_DIR michael@0: "/usr/lib64/mozilla/extensions"; michael@0: #elif defined(__OpenBSD__) || defined(__FreeBSD__) michael@0: "/usr/local/lib/mozilla/extensions"; michael@0: #else michael@0: "/usr/lib/mozilla/extensions"; michael@0: #endif michael@0: michael@0: rv = NS_NewNativeLocalFile(nsDependentCString(sysSExtDir), false, michael@0: getter_AddRefs(localDir)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: #endif michael@0: michael@0: NS_ADDREF(*aFile = localDir); michael@0: return NS_OK; michael@0: } michael@0: #endif michael@0: michael@0: nsresult michael@0: nsXREDirProvider::GetUserDataDirectory(nsIFile** aFile, bool aLocal, michael@0: const nsACString* aProfileName, michael@0: const nsACString* aAppName, michael@0: const nsACString* aVendorName) michael@0: { michael@0: nsCOMPtr localDir; michael@0: nsresult rv = GetUserDataDirectoryHome(getter_AddRefs(localDir), aLocal); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: rv = AppendProfilePath(localDir, aProfileName, aAppName, aVendorName, aLocal); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: #ifdef DEBUG_jungshik michael@0: nsAutoCString cwd; michael@0: localDir->GetNativePath(cwd); michael@0: printf("nsXREDirProvider::GetUserDataDirectory: %s\n", cwd.get()); michael@0: #endif michael@0: rv = EnsureDirectoryExists(localDir); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: NS_ADDREF(*aFile = localDir); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsXREDirProvider::EnsureDirectoryExists(nsIFile* aDirectory) michael@0: { michael@0: bool exists; michael@0: nsresult rv = aDirectory->Exists(&exists); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: #ifdef DEBUG_jungshik michael@0: if (!exists) { michael@0: nsAutoCString cwd; michael@0: aDirectory->GetNativePath(cwd); michael@0: printf("nsXREDirProvider::EnsureDirectoryExists: %s does not\n", cwd.get()); michael@0: } michael@0: #endif michael@0: if (!exists) michael@0: rv = aDirectory->Create(nsIFile::DIRECTORY_TYPE, 0700); michael@0: #ifdef DEBUG_jungshik michael@0: if (NS_FAILED(rv)) michael@0: NS_WARNING("nsXREDirProvider::EnsureDirectoryExists: create failed"); michael@0: #endif michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: void michael@0: nsXREDirProvider::EnsureProfileFileExists(nsIFile *aFile) michael@0: { michael@0: nsresult rv; michael@0: bool exists; michael@0: michael@0: rv = aFile->Exists(&exists); michael@0: if (NS_FAILED(rv) || exists) return; michael@0: michael@0: nsAutoCString leafName; michael@0: rv = aFile->GetNativeLeafName(leafName); michael@0: if (NS_FAILED(rv)) return; michael@0: michael@0: nsCOMPtr defaultsFile; michael@0: rv = GetProfileDefaultsDir(getter_AddRefs(defaultsFile)); michael@0: if (NS_FAILED(rv)) return; michael@0: michael@0: rv = defaultsFile->AppendNative(leafName); michael@0: if (NS_FAILED(rv)) return; michael@0: michael@0: defaultsFile->CopyToNative(mProfileDir, EmptyCString()); michael@0: } michael@0: michael@0: nsresult michael@0: nsXREDirProvider::GetProfileDefaultsDir(nsIFile* *aResult) michael@0: { michael@0: NS_ASSERTION(mGREDir, "nsXREDirProvider not initialized."); michael@0: NS_PRECONDITION(aResult, "Null out-param"); michael@0: michael@0: nsresult rv; michael@0: nsCOMPtr defaultsDir; michael@0: michael@0: rv = GetAppDir()->Clone(getter_AddRefs(defaultsDir)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: rv = defaultsDir->AppendNative(NS_LITERAL_CSTRING("defaults")); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: rv = defaultsDir->AppendNative(NS_LITERAL_CSTRING("profile")); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: NS_ADDREF(*aResult = defaultsDir); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsXREDirProvider::AppendSysUserExtensionPath(nsIFile* aFile) michael@0: { michael@0: NS_ASSERTION(aFile, "Null pointer!"); michael@0: michael@0: nsresult rv; michael@0: michael@0: #if defined (XP_MACOSX) || defined(XP_WIN) michael@0: michael@0: static const char* const sXR = "Mozilla"; michael@0: rv = aFile->AppendNative(nsDependentCString(sXR)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: static const char* const sExtensions = "Extensions"; michael@0: rv = aFile->AppendNative(nsDependentCString(sExtensions)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: #elif defined(XP_UNIX) michael@0: michael@0: static const char* const sXR = ".mozilla"; michael@0: rv = aFile->AppendNative(nsDependentCString(sXR)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: static const char* const sExtensions = "extensions"; michael@0: rv = aFile->AppendNative(nsDependentCString(sExtensions)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: #else michael@0: #error "Don't know how to get XRE user extension path on your platform" michael@0: #endif michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: nsresult michael@0: nsXREDirProvider::AppendProfilePath(nsIFile* aFile, michael@0: const nsACString* aProfileName, michael@0: const nsACString* aAppName, michael@0: const nsACString* aVendorName, michael@0: bool aLocal) michael@0: { michael@0: NS_ASSERTION(aFile, "Null pointer!"); michael@0: michael@0: if (!gAppData) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: nsAutoCString profile; michael@0: if (aProfileName && !aProfileName->IsEmpty()) { michael@0: profile = *aProfileName; michael@0: } else if (gAppData->profile) { michael@0: profile = gAppData->profile; michael@0: } michael@0: michael@0: nsresult rv = NS_ERROR_FAILURE; michael@0: michael@0: #if defined (XP_MACOSX) michael@0: if (!profile.IsEmpty()) { michael@0: rv = AppendProfileString(aFile, profile.get()); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: #elif defined(XP_WIN) michael@0: if (!profile.IsEmpty()) { michael@0: rv = AppendProfileString(aFile, profile.get()); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: #elif defined(ANDROID) michael@0: // The directory used for storing profiles michael@0: // The parent of this directory is set in GetUserDataDirectoryHome michael@0: // XXX: handle gAppData->profile properly michael@0: // XXXsmaug ...and the rest of the profile creation! michael@0: MOZ_ASSERT(!aAppName, michael@0: "Profile creation for external applications is not implemented!"); michael@0: rv = aFile->AppendNative(nsDependentCString("mozilla")); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: #elif defined(XP_UNIX) michael@0: if (!profile.IsEmpty()) { michael@0: // Skip any leading path characters michael@0: const char* profileStart = profile.get(); michael@0: while (*profileStart == '/' || *profileStart == '\\') michael@0: profileStart++; michael@0: michael@0: // On the off chance that someone wanted their folder to be hidden don't michael@0: // let it become ".." michael@0: if (*profileStart == '.') michael@0: profileStart++; michael@0: michael@0: // Make it hidden (by starting with "."). michael@0: nsAutoCString folder("."); michael@0: folder.Append(profileStart); michael@0: ToLowerCase(folder); michael@0: michael@0: rv = AppendProfileString(aFile, folder.BeginReading()); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: #else michael@0: #error "Don't know how to get profile path on your platform" michael@0: #endif michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsXREDirProvider::AppendProfileString(nsIFile* aFile, const char* aPath) michael@0: { michael@0: NS_ASSERTION(aFile, "Null file!"); michael@0: NS_ASSERTION(aPath, "Null path!"); michael@0: michael@0: nsAutoCString pathDup(aPath); michael@0: michael@0: char* path = pathDup.BeginWriting(); michael@0: michael@0: nsresult rv; michael@0: char* subdir; michael@0: while ((subdir = NS_strtok("/\\", &path))) { michael@0: rv = aFile->AppendNative(nsDependentCString(subdir)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: return NS_OK; michael@0: }