xpcom/io/nsDirectoryService.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
michael@0 2 * vim: sw=4 ts=4 sts=4 et
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "mozilla/ArrayUtils.h"
michael@0 8
michael@0 9 #include "nsCOMPtr.h"
michael@0 10 #include "nsAutoPtr.h"
michael@0 11 #include "nsDirectoryService.h"
michael@0 12 #include "nsDirectoryServiceDefs.h"
michael@0 13 #include "nsLocalFile.h"
michael@0 14 #include "nsDebug.h"
michael@0 15 #include "nsStaticAtom.h"
michael@0 16 #include "nsEnumeratorUtils.h"
michael@0 17
michael@0 18 #include "nsICategoryManager.h"
michael@0 19 #include "nsISimpleEnumerator.h"
michael@0 20 #include "nsIStringEnumerator.h"
michael@0 21
michael@0 22 #if defined(XP_WIN)
michael@0 23 #include <windows.h>
michael@0 24 #include <shlobj.h>
michael@0 25 #include <stdlib.h>
michael@0 26 #include <stdio.h>
michael@0 27 #elif defined(XP_UNIX)
michael@0 28 #include <unistd.h>
michael@0 29 #include <stdlib.h>
michael@0 30 #include <sys/param.h>
michael@0 31 #include "prenv.h"
michael@0 32 #ifdef MOZ_WIDGET_COCOA
michael@0 33 #include <CoreServices/CoreServices.h>
michael@0 34 #include <Carbon/Carbon.h>
michael@0 35 #endif
michael@0 36 #endif
michael@0 37
michael@0 38 #include "SpecialSystemDirectory.h"
michael@0 39 #include "nsAppFileLocationProvider.h"
michael@0 40
michael@0 41 using namespace mozilla;
michael@0 42
michael@0 43 // define home directory
michael@0 44 // For Windows platform, We are choosing Appdata folder as HOME
michael@0 45 #if defined (XP_WIN)
michael@0 46 #define HOME_DIR NS_WIN_APPDATA_DIR
michael@0 47 #elif defined (MOZ_WIDGET_COCOA)
michael@0 48 #define HOME_DIR NS_OSX_HOME_DIR
michael@0 49 #elif defined (XP_UNIX)
michael@0 50 #define HOME_DIR NS_UNIX_HOME_DIR
michael@0 51 #endif
michael@0 52
michael@0 53 //----------------------------------------------------------------------------------------
michael@0 54 nsresult
michael@0 55 nsDirectoryService::GetCurrentProcessDirectory(nsIFile** aFile)
michael@0 56 //----------------------------------------------------------------------------------------
michael@0 57 {
michael@0 58 if (NS_WARN_IF(!aFile))
michael@0 59 return NS_ERROR_INVALID_ARG;
michael@0 60 *aFile = nullptr;
michael@0 61
michael@0 62 // Set the component registry location:
michael@0 63 if (!gService)
michael@0 64 return NS_ERROR_FAILURE;
michael@0 65
michael@0 66 nsresult rv;
michael@0 67
michael@0 68 nsCOMPtr<nsIProperties> dirService;
michael@0 69 rv = nsDirectoryService::Create(nullptr,
michael@0 70 NS_GET_IID(nsIProperties),
michael@0 71 getter_AddRefs(dirService)); // needs to be around for life of product
michael@0 72 if (NS_FAILED(rv))
michael@0 73 return rv;
michael@0 74
michael@0 75 if (dirService)
michael@0 76 {
michael@0 77 nsCOMPtr <nsIFile> aLocalFile;
michael@0 78 dirService->Get(NS_XPCOM_INIT_CURRENT_PROCESS_DIR, NS_GET_IID(nsIFile), getter_AddRefs(aLocalFile));
michael@0 79 if (aLocalFile)
michael@0 80 {
michael@0 81 *aFile = aLocalFile;
michael@0 82 NS_ADDREF(*aFile);
michael@0 83 return NS_OK;
michael@0 84 }
michael@0 85 }
michael@0 86
michael@0 87 nsLocalFile* localFile = new nsLocalFile;
michael@0 88
michael@0 89 if (localFile == nullptr)
michael@0 90 return NS_ERROR_OUT_OF_MEMORY;
michael@0 91 NS_ADDREF(localFile);
michael@0 92
michael@0 93
michael@0 94
michael@0 95 #ifdef XP_WIN
michael@0 96 wchar_t buf[MAX_PATH + 1];
michael@0 97 SetLastError(ERROR_SUCCESS);
michael@0 98 if (GetModuleFileNameW(0, buf, mozilla::ArrayLength(buf)) &&
michael@0 99 GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
michael@0 100 // chop off the executable name by finding the rightmost backslash
michael@0 101 wchar_t* lastSlash = wcsrchr(buf, L'\\');
michael@0 102 if (lastSlash)
michael@0 103 *(lastSlash + 1) = L'\0';
michael@0 104
michael@0 105 localFile->InitWithPath(nsDependentString(buf));
michael@0 106 *aFile = localFile;
michael@0 107 return NS_OK;
michael@0 108 }
michael@0 109
michael@0 110 #elif defined(MOZ_WIDGET_COCOA)
michael@0 111 // Works even if we're not bundled.
michael@0 112 CFBundleRef appBundle = CFBundleGetMainBundle();
michael@0 113 if (appBundle != nullptr)
michael@0 114 {
michael@0 115 CFURLRef bundleURL = CFBundleCopyExecutableURL(appBundle);
michael@0 116 if (bundleURL != nullptr)
michael@0 117 {
michael@0 118 CFURLRef parentURL = CFURLCreateCopyDeletingLastPathComponent(kCFAllocatorDefault, bundleURL);
michael@0 119 if (parentURL)
michael@0 120 {
michael@0 121 // Pass true for the "resolveAgainstBase" arg to CFURLGetFileSystemRepresentation.
michael@0 122 // This will resolve the relative portion of the CFURL against it base, giving a full
michael@0 123 // path, which CFURLCopyFileSystemPath doesn't do.
michael@0 124 char buffer[PATH_MAX];
michael@0 125 if (CFURLGetFileSystemRepresentation(parentURL, true, (UInt8 *)buffer, sizeof(buffer)))
michael@0 126 {
michael@0 127 #ifdef DEBUG_conrad
michael@0 128 printf("nsDirectoryService - CurrentProcessDir is: %s\n", buffer);
michael@0 129 #endif
michael@0 130 rv = localFile->InitWithNativePath(nsDependentCString(buffer));
michael@0 131 if (NS_SUCCEEDED(rv))
michael@0 132 *aFile = localFile;
michael@0 133 }
michael@0 134 CFRelease(parentURL);
michael@0 135 }
michael@0 136 CFRelease(bundleURL);
michael@0 137 }
michael@0 138 }
michael@0 139
michael@0 140 NS_ASSERTION(*aFile, "nsDirectoryService - Could not determine CurrentProcessDir.\n");
michael@0 141 if (*aFile)
michael@0 142 return NS_OK;
michael@0 143
michael@0 144 #elif defined(XP_UNIX)
michael@0 145
michael@0 146 // In the absence of a good way to get the executable directory let
michael@0 147 // us try this for unix:
michael@0 148 // - if MOZILLA_FIVE_HOME is defined, that is it
michael@0 149 // - else give the current directory
michael@0 150 char buf[MAXPATHLEN];
michael@0 151
michael@0 152 // The MOZ_DEFAULT_MOZILLA_FIVE_HOME variable can be set at configure time with
michael@0 153 // a --with-default-mozilla-five-home=foo autoconf flag.
michael@0 154 //
michael@0 155 // The idea here is to allow for builds that have a default MOZILLA_FIVE_HOME
michael@0 156 // regardless of the environment. This makes it easier to write apps that
michael@0 157 // embed mozilla without having to worry about setting up the environment
michael@0 158 //
michael@0 159 // We do this by putenv()ing the default value into the environment. Note that
michael@0 160 // we only do this if it is not already set.
michael@0 161 #ifdef MOZ_DEFAULT_MOZILLA_FIVE_HOME
michael@0 162 const char *home = PR_GetEnv("MOZILLA_FIVE_HOME");
michael@0 163 if (!home || !*home)
michael@0 164 {
michael@0 165 putenv("MOZILLA_FIVE_HOME=" MOZ_DEFAULT_MOZILLA_FIVE_HOME);
michael@0 166 }
michael@0 167 #endif
michael@0 168
michael@0 169 char *moz5 = PR_GetEnv("MOZILLA_FIVE_HOME");
michael@0 170 if (moz5 && *moz5)
michael@0 171 {
michael@0 172 if (realpath(moz5, buf)) {
michael@0 173 localFile->InitWithNativePath(nsDependentCString(buf));
michael@0 174 *aFile = localFile;
michael@0 175 return NS_OK;
michael@0 176 }
michael@0 177 }
michael@0 178 #if defined(DEBUG)
michael@0 179 static bool firstWarning = true;
michael@0 180
michael@0 181 if((!moz5 || !*moz5) && firstWarning) {
michael@0 182 // Warn that MOZILLA_FIVE_HOME not set, once.
michael@0 183 printf("Warning: MOZILLA_FIVE_HOME not set.\n");
michael@0 184 firstWarning = false;
michael@0 185 }
michael@0 186 #endif /* DEBUG */
michael@0 187
michael@0 188 // Fall back to current directory.
michael@0 189 if (getcwd(buf, sizeof(buf)))
michael@0 190 {
michael@0 191 localFile->InitWithNativePath(nsDependentCString(buf));
michael@0 192 *aFile = localFile;
michael@0 193 return NS_OK;
michael@0 194 }
michael@0 195
michael@0 196 #endif
michael@0 197
michael@0 198 NS_RELEASE(localFile);
michael@0 199
michael@0 200 NS_ERROR("unable to get current process directory");
michael@0 201 return NS_ERROR_FAILURE;
michael@0 202 } // GetCurrentProcessDirectory()
michael@0 203
michael@0 204 nsDirectoryService* nsDirectoryService::gService = nullptr;
michael@0 205
michael@0 206 nsDirectoryService::nsDirectoryService()
michael@0 207 : mHashtable(256)
michael@0 208 {
michael@0 209 }
michael@0 210
michael@0 211 nsresult
michael@0 212 nsDirectoryService::Create(nsISupports *outer, REFNSIID aIID, void **aResult)
michael@0 213 {
michael@0 214 if (NS_WARN_IF(!aResult))
michael@0 215 return NS_ERROR_INVALID_ARG;
michael@0 216 if (NS_WARN_IF(outer))
michael@0 217 return NS_ERROR_NO_AGGREGATION;
michael@0 218
michael@0 219 if (!gService)
michael@0 220 {
michael@0 221 return NS_ERROR_NOT_INITIALIZED;
michael@0 222 }
michael@0 223
michael@0 224 return gService->QueryInterface(aIID, aResult);
michael@0 225 }
michael@0 226
michael@0 227 #define DIR_ATOM(name_, value_) nsIAtom* nsDirectoryService::name_ = nullptr;
michael@0 228 #include "nsDirectoryServiceAtomList.h"
michael@0 229 #undef DIR_ATOM
michael@0 230
michael@0 231 #define DIR_ATOM(name_, value_) NS_STATIC_ATOM_BUFFER(name_##_buffer, value_)
michael@0 232 #include "nsDirectoryServiceAtomList.h"
michael@0 233 #undef DIR_ATOM
michael@0 234
michael@0 235 static const nsStaticAtom directory_atoms[] = {
michael@0 236 #define DIR_ATOM(name_, value_) NS_STATIC_ATOM(name_##_buffer, &nsDirectoryService::name_),
michael@0 237 #include "nsDirectoryServiceAtomList.h"
michael@0 238 #undef DIR_ATOM
michael@0 239 };
michael@0 240
michael@0 241 NS_IMETHODIMP
michael@0 242 nsDirectoryService::Init()
michael@0 243 {
michael@0 244 NS_NOTREACHED("nsDirectoryService::Init() for internal use only!");
michael@0 245 return NS_OK;
michael@0 246 }
michael@0 247
michael@0 248 void
michael@0 249 nsDirectoryService::RealInit()
michael@0 250 {
michael@0 251 NS_ASSERTION(!gService,
michael@0 252 "nsDirectoryService::RealInit Mustn't initialize twice!");
michael@0 253
michael@0 254 nsRefPtr<nsDirectoryService> self = new nsDirectoryService();
michael@0 255
michael@0 256 NS_RegisterStaticAtoms(directory_atoms);
michael@0 257
michael@0 258 // Let the list hold the only reference to the provider.
michael@0 259 nsAppFileLocationProvider *defaultProvider = new nsAppFileLocationProvider;
michael@0 260 self->mProviders.AppendElement(defaultProvider);
michael@0 261
michael@0 262 self.swap(gService);
michael@0 263 }
michael@0 264
michael@0 265 nsDirectoryService::~nsDirectoryService()
michael@0 266 {
michael@0 267 }
michael@0 268
michael@0 269 NS_IMPL_ISUPPORTS(nsDirectoryService, nsIProperties, nsIDirectoryService, nsIDirectoryServiceProvider, nsIDirectoryServiceProvider2)
michael@0 270
michael@0 271
michael@0 272 NS_IMETHODIMP
michael@0 273 nsDirectoryService::Undefine(const char* prop)
michael@0 274 {
michael@0 275 if (NS_WARN_IF(!prop))
michael@0 276 return NS_ERROR_INVALID_ARG;
michael@0 277
michael@0 278 nsDependentCString key(prop);
michael@0 279 if (!mHashtable.Get(key, nullptr))
michael@0 280 return NS_ERROR_FAILURE;
michael@0 281
michael@0 282 mHashtable.Remove(key);
michael@0 283 return NS_OK;
michael@0 284 }
michael@0 285
michael@0 286 NS_IMETHODIMP
michael@0 287 nsDirectoryService::GetKeys(uint32_t *count, char ***keys)
michael@0 288 {
michael@0 289 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 290 }
michael@0 291
michael@0 292 struct FileData
michael@0 293 {
michael@0 294 FileData(const char* aProperty,
michael@0 295 const nsIID& aUUID) :
michael@0 296 property(aProperty),
michael@0 297 data(nullptr),
michael@0 298 persistent(true),
michael@0 299 uuid(aUUID) {}
michael@0 300
michael@0 301 const char* property;
michael@0 302 nsISupports* data;
michael@0 303 bool persistent;
michael@0 304 const nsIID& uuid;
michael@0 305 };
michael@0 306
michael@0 307 static bool FindProviderFile(nsIDirectoryServiceProvider* aElement,
michael@0 308 FileData* aData)
michael@0 309 {
michael@0 310 nsresult rv;
michael@0 311 if (aData->uuid.Equals(NS_GET_IID(nsISimpleEnumerator))) {
michael@0 312 // Not all providers implement this iface
michael@0 313 nsCOMPtr<nsIDirectoryServiceProvider2> prov2 = do_QueryInterface(aElement);
michael@0 314 if (prov2)
michael@0 315 {
michael@0 316 nsCOMPtr<nsISimpleEnumerator> newFiles;
michael@0 317 rv = prov2->GetFiles(aData->property, getter_AddRefs(newFiles));
michael@0 318 if (NS_SUCCEEDED(rv) && newFiles) {
michael@0 319 if (aData->data) {
michael@0 320 nsCOMPtr<nsISimpleEnumerator> unionFiles;
michael@0 321
michael@0 322 NS_NewUnionEnumerator(getter_AddRefs(unionFiles),
michael@0 323 (nsISimpleEnumerator*) aData->data, newFiles);
michael@0 324
michael@0 325 if (unionFiles)
michael@0 326 unionFiles.swap(* (nsISimpleEnumerator**) &aData->data);
michael@0 327 }
michael@0 328 else
michael@0 329 {
michael@0 330 NS_ADDREF(aData->data = newFiles);
michael@0 331 }
michael@0 332
michael@0 333 aData->persistent = false; // Enumerators can never be persistent
michael@0 334 return rv == NS_SUCCESS_AGGREGATE_RESULT;
michael@0 335 }
michael@0 336 }
michael@0 337 }
michael@0 338 else
michael@0 339 {
michael@0 340 rv = aElement->GetFile(aData->property, &aData->persistent,
michael@0 341 (nsIFile **)&aData->data);
michael@0 342 if (NS_SUCCEEDED(rv) && aData->data)
michael@0 343 return false;
michael@0 344 }
michael@0 345
michael@0 346 return true;
michael@0 347 }
michael@0 348
michael@0 349 NS_IMETHODIMP
michael@0 350 nsDirectoryService::Get(const char* prop, const nsIID & uuid, void* *result)
michael@0 351 {
michael@0 352 if (NS_WARN_IF(!prop))
michael@0 353 return NS_ERROR_INVALID_ARG;
michael@0 354
michael@0 355 nsDependentCString key(prop);
michael@0 356
michael@0 357 nsCOMPtr<nsIFile> cachedFile = mHashtable.Get(key);
michael@0 358
michael@0 359 if (cachedFile) {
michael@0 360 nsCOMPtr<nsIFile> cloneFile;
michael@0 361 cachedFile->Clone(getter_AddRefs(cloneFile));
michael@0 362 return cloneFile->QueryInterface(uuid, result);
michael@0 363 }
michael@0 364
michael@0 365 // it is not one of our defaults, lets check any providers
michael@0 366 FileData fileData(prop, uuid);
michael@0 367
michael@0 368 for (int32_t i = mProviders.Length() - 1; i >= 0; i--) {
michael@0 369 if (!FindProviderFile(mProviders[i], &fileData)) {
michael@0 370 break;
michael@0 371 }
michael@0 372 }
michael@0 373 if (fileData.data)
michael@0 374 {
michael@0 375 if (fileData.persistent)
michael@0 376 {
michael@0 377 Set(prop, static_cast<nsIFile*>(fileData.data));
michael@0 378 }
michael@0 379 nsresult rv = (fileData.data)->QueryInterface(uuid, result);
michael@0 380 NS_RELEASE(fileData.data); // addref occurs in FindProviderFile()
michael@0 381 return rv;
michael@0 382 }
michael@0 383
michael@0 384 FindProviderFile(static_cast<nsIDirectoryServiceProvider*>(this), &fileData);
michael@0 385 if (fileData.data)
michael@0 386 {
michael@0 387 if (fileData.persistent)
michael@0 388 {
michael@0 389 Set(prop, static_cast<nsIFile*>(fileData.data));
michael@0 390 }
michael@0 391 nsresult rv = (fileData.data)->QueryInterface(uuid, result);
michael@0 392 NS_RELEASE(fileData.data); // addref occurs in FindProviderFile()
michael@0 393 return rv;
michael@0 394 }
michael@0 395
michael@0 396 return NS_ERROR_FAILURE;
michael@0 397 }
michael@0 398
michael@0 399 NS_IMETHODIMP
michael@0 400 nsDirectoryService::Set(const char* prop, nsISupports* value)
michael@0 401 {
michael@0 402 if (NS_WARN_IF(!prop))
michael@0 403 return NS_ERROR_INVALID_ARG;
michael@0 404
michael@0 405 nsDependentCString key(prop);
michael@0 406 if (mHashtable.Get(key, nullptr) || !value) {
michael@0 407 return NS_ERROR_FAILURE;
michael@0 408 }
michael@0 409
michael@0 410 nsCOMPtr<nsIFile> ourFile = do_QueryInterface(value);
michael@0 411 if (ourFile) {
michael@0 412 nsCOMPtr<nsIFile> cloneFile;
michael@0 413 ourFile->Clone (getter_AddRefs (cloneFile));
michael@0 414 mHashtable.Put(key, cloneFile);
michael@0 415
michael@0 416 return NS_OK;
michael@0 417 }
michael@0 418
michael@0 419 return NS_ERROR_FAILURE;
michael@0 420 }
michael@0 421
michael@0 422 NS_IMETHODIMP
michael@0 423 nsDirectoryService::Has(const char *prop, bool *_retval)
michael@0 424 {
michael@0 425 if (NS_WARN_IF(!prop))
michael@0 426 return NS_ERROR_INVALID_ARG;
michael@0 427
michael@0 428 *_retval = false;
michael@0 429 nsCOMPtr<nsIFile> value;
michael@0 430 nsresult rv = Get(prop, NS_GET_IID(nsIFile), getter_AddRefs(value));
michael@0 431 if (NS_FAILED(rv))
michael@0 432 return NS_OK;
michael@0 433
michael@0 434 if (value)
michael@0 435 {
michael@0 436 *_retval = true;
michael@0 437 }
michael@0 438
michael@0 439 return rv;
michael@0 440 }
michael@0 441
michael@0 442 NS_IMETHODIMP
michael@0 443 nsDirectoryService::RegisterProvider(nsIDirectoryServiceProvider *prov)
michael@0 444 {
michael@0 445 if (!prov)
michael@0 446 return NS_ERROR_FAILURE;
michael@0 447
michael@0 448 mProviders.AppendElement(prov);
michael@0 449 return NS_OK;
michael@0 450 }
michael@0 451
michael@0 452 void
michael@0 453 nsDirectoryService::RegisterCategoryProviders()
michael@0 454 {
michael@0 455 nsCOMPtr<nsICategoryManager> catman
michael@0 456 (do_GetService(NS_CATEGORYMANAGER_CONTRACTID));
michael@0 457 if (!catman)
michael@0 458 return;
michael@0 459
michael@0 460 nsCOMPtr<nsISimpleEnumerator> entries;
michael@0 461 catman->EnumerateCategory(XPCOM_DIRECTORY_PROVIDER_CATEGORY,
michael@0 462 getter_AddRefs(entries));
michael@0 463
michael@0 464 nsCOMPtr<nsIUTF8StringEnumerator> strings(do_QueryInterface(entries));
michael@0 465 if (!strings)
michael@0 466 return;
michael@0 467
michael@0 468 bool more;
michael@0 469 while (NS_SUCCEEDED(strings->HasMore(&more)) && more) {
michael@0 470 nsAutoCString entry;
michael@0 471 strings->GetNext(entry);
michael@0 472
michael@0 473 nsXPIDLCString contractID;
michael@0 474 catman->GetCategoryEntry(XPCOM_DIRECTORY_PROVIDER_CATEGORY, entry.get(), getter_Copies(contractID));
michael@0 475
michael@0 476 if (contractID) {
michael@0 477 nsCOMPtr<nsIDirectoryServiceProvider> provider = do_GetService(contractID.get());
michael@0 478 if (provider)
michael@0 479 RegisterProvider(provider);
michael@0 480 }
michael@0 481 }
michael@0 482 }
michael@0 483
michael@0 484 NS_IMETHODIMP
michael@0 485 nsDirectoryService::UnregisterProvider(nsIDirectoryServiceProvider *prov)
michael@0 486 {
michael@0 487 if (!prov)
michael@0 488 return NS_ERROR_FAILURE;
michael@0 489
michael@0 490 mProviders.RemoveElement(prov);
michael@0 491 return NS_OK;
michael@0 492 }
michael@0 493
michael@0 494 // DO NOT ADD ANY LOCATIONS TO THIS FUNCTION UNTIL YOU TALK TO: dougt@netscape.com.
michael@0 495 // This is meant to be a place of xpcom or system specific file locations, not
michael@0 496 // application specific locations. If you need the later, register a callback for
michael@0 497 // your application.
michael@0 498
michael@0 499 NS_IMETHODIMP
michael@0 500 nsDirectoryService::GetFile(const char *prop, bool *persistent, nsIFile **_retval)
michael@0 501 {
michael@0 502 nsCOMPtr<nsIFile> localFile;
michael@0 503 nsresult rv = NS_ERROR_FAILURE;
michael@0 504
michael@0 505 *_retval = nullptr;
michael@0 506 *persistent = true;
michael@0 507
michael@0 508 nsCOMPtr<nsIAtom> inAtom = do_GetAtom(prop);
michael@0 509
michael@0 510 // check to see if it is one of our defaults
michael@0 511
michael@0 512 if (inAtom == nsDirectoryService::sCurrentProcess ||
michael@0 513 inAtom == nsDirectoryService::sOS_CurrentProcessDirectory )
michael@0 514 {
michael@0 515 rv = GetCurrentProcessDirectory(getter_AddRefs(localFile));
michael@0 516 }
michael@0 517
michael@0 518 // Unless otherwise set, the core pieces of the GRE exist
michael@0 519 // in the current process directory.
michael@0 520 else if (inAtom == nsDirectoryService::sGRE_Directory)
michael@0 521 {
michael@0 522 rv = GetCurrentProcessDirectory(getter_AddRefs(localFile));
michael@0 523 }
michael@0 524 else if (inAtom == nsDirectoryService::sOS_DriveDirectory)
michael@0 525 {
michael@0 526 rv = GetSpecialSystemDirectory(OS_DriveDirectory, getter_AddRefs(localFile));
michael@0 527 }
michael@0 528 else if (inAtom == nsDirectoryService::sOS_TemporaryDirectory)
michael@0 529 {
michael@0 530 rv = GetSpecialSystemDirectory(OS_TemporaryDirectory, getter_AddRefs(localFile));
michael@0 531 }
michael@0 532 else if (inAtom == nsDirectoryService::sOS_CurrentProcessDirectory)
michael@0 533 {
michael@0 534 rv = GetSpecialSystemDirectory(OS_CurrentProcessDirectory, getter_AddRefs(localFile));
michael@0 535 }
michael@0 536 else if (inAtom == nsDirectoryService::sOS_CurrentWorkingDirectory)
michael@0 537 {
michael@0 538 rv = GetSpecialSystemDirectory(OS_CurrentWorkingDirectory, getter_AddRefs(localFile));
michael@0 539 }
michael@0 540
michael@0 541 #if defined(MOZ_WIDGET_COCOA)
michael@0 542 else if (inAtom == nsDirectoryService::sDirectory)
michael@0 543 {
michael@0 544 rv = GetOSXFolderType(kClassicDomain, kSystemFolderType, getter_AddRefs(localFile));
michael@0 545 }
michael@0 546 else if (inAtom == nsDirectoryService::sTrashDirectory)
michael@0 547 {
michael@0 548 rv = GetOSXFolderType(kClassicDomain, kTrashFolderType, getter_AddRefs(localFile));
michael@0 549 }
michael@0 550 else if (inAtom == nsDirectoryService::sStartupDirectory)
michael@0 551 {
michael@0 552 rv = GetOSXFolderType(kClassicDomain, kStartupFolderType, getter_AddRefs(localFile));
michael@0 553 }
michael@0 554 else if (inAtom == nsDirectoryService::sShutdownDirectory)
michael@0 555 {
michael@0 556 rv = GetOSXFolderType(kClassicDomain, kShutdownFolderType, getter_AddRefs(localFile));
michael@0 557 }
michael@0 558 else if (inAtom == nsDirectoryService::sAppleMenuDirectory)
michael@0 559 {
michael@0 560 rv = GetOSXFolderType(kClassicDomain, kAppleMenuFolderType, getter_AddRefs(localFile));
michael@0 561 }
michael@0 562 else if (inAtom == nsDirectoryService::sControlPanelDirectory)
michael@0 563 {
michael@0 564 rv = GetOSXFolderType(kClassicDomain, kControlPanelFolderType, getter_AddRefs(localFile));
michael@0 565 }
michael@0 566 else if (inAtom == nsDirectoryService::sExtensionDirectory)
michael@0 567 {
michael@0 568 rv = GetOSXFolderType(kClassicDomain, kExtensionFolderType, getter_AddRefs(localFile));
michael@0 569 }
michael@0 570 else if (inAtom == nsDirectoryService::sFontsDirectory)
michael@0 571 {
michael@0 572 rv = GetOSXFolderType(kClassicDomain, kFontsFolderType, getter_AddRefs(localFile));
michael@0 573 }
michael@0 574 else if (inAtom == nsDirectoryService::sPreferencesDirectory)
michael@0 575 {
michael@0 576 rv = GetOSXFolderType(kClassicDomain, kPreferencesFolderType, getter_AddRefs(localFile));
michael@0 577 }
michael@0 578 else if (inAtom == nsDirectoryService::sDocumentsDirectory)
michael@0 579 {
michael@0 580 rv = GetOSXFolderType(kClassicDomain, kDocumentsFolderType, getter_AddRefs(localFile));
michael@0 581 }
michael@0 582 else if (inAtom == nsDirectoryService::sInternetSearchDirectory)
michael@0 583 {
michael@0 584 rv = GetOSXFolderType(kClassicDomain, kInternetSearchSitesFolderType, getter_AddRefs(localFile));
michael@0 585 }
michael@0 586 else if (inAtom == nsDirectoryService::sUserLibDirectory)
michael@0 587 {
michael@0 588 rv = GetOSXFolderType(kUserDomain, kDomainLibraryFolderType, getter_AddRefs(localFile));
michael@0 589 }
michael@0 590 else if (inAtom == nsDirectoryService::sOS_HomeDirectory)
michael@0 591 {
michael@0 592 rv = GetOSXFolderType(kUserDomain, kDomainTopLevelFolderType, getter_AddRefs(localFile));
michael@0 593 }
michael@0 594 else if (inAtom == nsDirectoryService::sDefaultDownloadDirectory)
michael@0 595 {
michael@0 596 // 10.5 and later, we can use kDownloadsFolderType which is defined in
michael@0 597 // Folders.h as "down". However, in order to support 10.4 still, we
michael@0 598 // cannot use the named constant. We'll use it's value, and if it
michael@0 599 // fails, fall back to the desktop.
michael@0 600 #ifndef kDownloadsFolderType
michael@0 601 #define kDownloadsFolderType 'down'
michael@0 602 #endif
michael@0 603
michael@0 604 rv = GetOSXFolderType(kUserDomain, kDownloadsFolderType,
michael@0 605 getter_AddRefs(localFile));
michael@0 606 if (NS_FAILED(rv)) {
michael@0 607 rv = GetOSXFolderType(kUserDomain, kDesktopFolderType,
michael@0 608 getter_AddRefs(localFile));
michael@0 609 }
michael@0 610 }
michael@0 611 else if (inAtom == nsDirectoryService::sUserDesktopDirectory ||
michael@0 612 inAtom == nsDirectoryService::sOS_DesktopDirectory)
michael@0 613 {
michael@0 614 rv = GetOSXFolderType(kUserDomain, kDesktopFolderType, getter_AddRefs(localFile));
michael@0 615 }
michael@0 616 else if (inAtom == nsDirectoryService::sLocalDesktopDirectory)
michael@0 617 {
michael@0 618 rv = GetOSXFolderType(kLocalDomain, kDesktopFolderType, getter_AddRefs(localFile));
michael@0 619 }
michael@0 620 else if (inAtom == nsDirectoryService::sUserApplicationsDirectory)
michael@0 621 {
michael@0 622 rv = GetOSXFolderType(kUserDomain, kApplicationsFolderType, getter_AddRefs(localFile));
michael@0 623 }
michael@0 624 else if (inAtom == nsDirectoryService::sLocalApplicationsDirectory)
michael@0 625 {
michael@0 626 rv = GetOSXFolderType(kLocalDomain, kApplicationsFolderType, getter_AddRefs(localFile));
michael@0 627 }
michael@0 628 else if (inAtom == nsDirectoryService::sUserDocumentsDirectory)
michael@0 629 {
michael@0 630 rv = GetOSXFolderType(kUserDomain, kDocumentsFolderType, getter_AddRefs(localFile));
michael@0 631 }
michael@0 632 else if (inAtom == nsDirectoryService::sLocalDocumentsDirectory)
michael@0 633 {
michael@0 634 rv = GetOSXFolderType(kLocalDomain, kDocumentsFolderType, getter_AddRefs(localFile));
michael@0 635 }
michael@0 636 else if (inAtom == nsDirectoryService::sUserInternetPlugInDirectory)
michael@0 637 {
michael@0 638 rv = GetOSXFolderType(kUserDomain, kInternetPlugInFolderType, getter_AddRefs(localFile));
michael@0 639 }
michael@0 640 else if (inAtom == nsDirectoryService::sLocalInternetPlugInDirectory)
michael@0 641 {
michael@0 642 rv = GetOSXFolderType(kLocalDomain, kInternetPlugInFolderType, getter_AddRefs(localFile));
michael@0 643 }
michael@0 644 else if (inAtom == nsDirectoryService::sUserFrameworksDirectory)
michael@0 645 {
michael@0 646 rv = GetOSXFolderType(kUserDomain, kFrameworksFolderType, getter_AddRefs(localFile));
michael@0 647 }
michael@0 648 else if (inAtom == nsDirectoryService::sLocalFrameworksDirectory)
michael@0 649 {
michael@0 650 rv = GetOSXFolderType(kLocalDomain, kFrameworksFolderType, getter_AddRefs(localFile));
michael@0 651 }
michael@0 652 else if (inAtom == nsDirectoryService::sUserPreferencesDirectory)
michael@0 653 {
michael@0 654 rv = GetOSXFolderType(kUserDomain, kPreferencesFolderType, getter_AddRefs(localFile));
michael@0 655 }
michael@0 656 else if (inAtom == nsDirectoryService::sLocalPreferencesDirectory)
michael@0 657 {
michael@0 658 rv = GetOSXFolderType(kLocalDomain, kPreferencesFolderType, getter_AddRefs(localFile));
michael@0 659 }
michael@0 660 else if (inAtom == nsDirectoryService::sPictureDocumentsDirectory)
michael@0 661 {
michael@0 662 rv = GetOSXFolderType(kUserDomain, kPictureDocumentsFolderType, getter_AddRefs(localFile));
michael@0 663 }
michael@0 664 else if (inAtom == nsDirectoryService::sMovieDocumentsDirectory)
michael@0 665 {
michael@0 666 rv = GetOSXFolderType(kUserDomain, kMovieDocumentsFolderType, getter_AddRefs(localFile));
michael@0 667 }
michael@0 668 else if (inAtom == nsDirectoryService::sMusicDocumentsDirectory)
michael@0 669 {
michael@0 670 rv = GetOSXFolderType(kUserDomain, kMusicDocumentsFolderType, getter_AddRefs(localFile));
michael@0 671 }
michael@0 672 else if (inAtom == nsDirectoryService::sInternetSitesDirectory)
michael@0 673 {
michael@0 674 rv = GetOSXFolderType(kUserDomain, kInternetSitesFolderType, getter_AddRefs(localFile));
michael@0 675 }
michael@0 676 #elif defined (XP_WIN)
michael@0 677 else if (inAtom == nsDirectoryService::sSystemDirectory)
michael@0 678 {
michael@0 679 rv = GetSpecialSystemDirectory(Win_SystemDirectory, getter_AddRefs(localFile));
michael@0 680 }
michael@0 681 else if (inAtom == nsDirectoryService::sWindowsDirectory)
michael@0 682 {
michael@0 683 rv = GetSpecialSystemDirectory(Win_WindowsDirectory, getter_AddRefs(localFile));
michael@0 684 }
michael@0 685 else if (inAtom == nsDirectoryService::sWindowsProgramFiles)
michael@0 686 {
michael@0 687 rv = GetSpecialSystemDirectory(Win_ProgramFiles, getter_AddRefs(localFile));
michael@0 688 }
michael@0 689 else if (inAtom == nsDirectoryService::sOS_HomeDirectory)
michael@0 690 {
michael@0 691 rv = GetSpecialSystemDirectory(Win_HomeDirectory, getter_AddRefs(localFile));
michael@0 692 }
michael@0 693 else if (inAtom == nsDirectoryService::sDesktop)
michael@0 694 {
michael@0 695 rv = GetSpecialSystemDirectory(Win_Desktop, getter_AddRefs(localFile));
michael@0 696 }
michael@0 697 else if (inAtom == nsDirectoryService::sPrograms)
michael@0 698 {
michael@0 699 rv = GetSpecialSystemDirectory(Win_Programs, getter_AddRefs(localFile));
michael@0 700 }
michael@0 701 else if (inAtom == nsDirectoryService::sControls)
michael@0 702 {
michael@0 703 rv = GetSpecialSystemDirectory(Win_Controls, getter_AddRefs(localFile));
michael@0 704 }
michael@0 705 else if (inAtom == nsDirectoryService::sPrinters)
michael@0 706 {
michael@0 707 rv = GetSpecialSystemDirectory(Win_Printers, getter_AddRefs(localFile));
michael@0 708 }
michael@0 709 else if (inAtom == nsDirectoryService::sPersonal)
michael@0 710 {
michael@0 711 rv = GetSpecialSystemDirectory(Win_Personal, getter_AddRefs(localFile));
michael@0 712 }
michael@0 713 else if (inAtom == nsDirectoryService::sFavorites)
michael@0 714 {
michael@0 715 rv = GetSpecialSystemDirectory(Win_Favorites, getter_AddRefs(localFile));
michael@0 716 }
michael@0 717 else if (inAtom == nsDirectoryService::sStartup)
michael@0 718 {
michael@0 719 rv = GetSpecialSystemDirectory(Win_Startup, getter_AddRefs(localFile));
michael@0 720 }
michael@0 721 else if (inAtom == nsDirectoryService::sRecent)
michael@0 722 {
michael@0 723 rv = GetSpecialSystemDirectory(Win_Recent, getter_AddRefs(localFile));
michael@0 724 }
michael@0 725 else if (inAtom == nsDirectoryService::sSendto)
michael@0 726 {
michael@0 727 rv = GetSpecialSystemDirectory(Win_Sendto, getter_AddRefs(localFile));
michael@0 728 }
michael@0 729 else if (inAtom == nsDirectoryService::sBitbucket)
michael@0 730 {
michael@0 731 rv = GetSpecialSystemDirectory(Win_Bitbucket, getter_AddRefs(localFile));
michael@0 732 }
michael@0 733 else if (inAtom == nsDirectoryService::sStartmenu)
michael@0 734 {
michael@0 735 rv = GetSpecialSystemDirectory(Win_Startmenu, getter_AddRefs(localFile));
michael@0 736 }
michael@0 737 else if (inAtom == nsDirectoryService::sDesktopdirectory ||
michael@0 738 inAtom == nsDirectoryService::sOS_DesktopDirectory)
michael@0 739 {
michael@0 740 rv = GetSpecialSystemDirectory(Win_Desktopdirectory, getter_AddRefs(localFile));
michael@0 741 }
michael@0 742 else if (inAtom == nsDirectoryService::sDrives)
michael@0 743 {
michael@0 744 rv = GetSpecialSystemDirectory(Win_Drives, getter_AddRefs(localFile));
michael@0 745 }
michael@0 746 else if (inAtom == nsDirectoryService::sNetwork)
michael@0 747 {
michael@0 748 rv = GetSpecialSystemDirectory(Win_Network, getter_AddRefs(localFile));
michael@0 749 }
michael@0 750 else if (inAtom == nsDirectoryService::sNethood)
michael@0 751 {
michael@0 752 rv = GetSpecialSystemDirectory(Win_Nethood, getter_AddRefs(localFile));
michael@0 753 }
michael@0 754 else if (inAtom == nsDirectoryService::sFonts)
michael@0 755 {
michael@0 756 rv = GetSpecialSystemDirectory(Win_Fonts, getter_AddRefs(localFile));
michael@0 757 }
michael@0 758 else if (inAtom == nsDirectoryService::sTemplates)
michael@0 759 {
michael@0 760 rv = GetSpecialSystemDirectory(Win_Templates, getter_AddRefs(localFile));
michael@0 761 }
michael@0 762 else if (inAtom == nsDirectoryService::sCommon_Startmenu)
michael@0 763 {
michael@0 764 rv = GetSpecialSystemDirectory(Win_Common_Startmenu, getter_AddRefs(localFile));
michael@0 765 }
michael@0 766 else if (inAtom == nsDirectoryService::sCommon_Programs)
michael@0 767 {
michael@0 768 rv = GetSpecialSystemDirectory(Win_Common_Programs, getter_AddRefs(localFile));
michael@0 769 }
michael@0 770 else if (inAtom == nsDirectoryService::sCommon_Startup)
michael@0 771 {
michael@0 772 rv = GetSpecialSystemDirectory(Win_Common_Startup, getter_AddRefs(localFile));
michael@0 773 }
michael@0 774 else if (inAtom == nsDirectoryService::sCommon_Desktopdirectory)
michael@0 775 {
michael@0 776 rv = GetSpecialSystemDirectory(Win_Common_Desktopdirectory, getter_AddRefs(localFile));
michael@0 777 }
michael@0 778 else if (inAtom == nsDirectoryService::sCommon_AppData)
michael@0 779 {
michael@0 780 rv = GetSpecialSystemDirectory(Win_Common_AppData, getter_AddRefs(localFile));
michael@0 781 }
michael@0 782 else if (inAtom == nsDirectoryService::sAppdata)
michael@0 783 {
michael@0 784 rv = GetSpecialSystemDirectory(Win_Appdata, getter_AddRefs(localFile));
michael@0 785 }
michael@0 786 else if (inAtom == nsDirectoryService::sLocalAppdata)
michael@0 787 {
michael@0 788 rv = GetSpecialSystemDirectory(Win_LocalAppdata, getter_AddRefs(localFile));
michael@0 789 }
michael@0 790 else if (inAtom == nsDirectoryService::sPrinthood)
michael@0 791 {
michael@0 792 rv = GetSpecialSystemDirectory(Win_Printhood, getter_AddRefs(localFile));
michael@0 793 }
michael@0 794 else if (inAtom == nsDirectoryService::sWinCookiesDirectory)
michael@0 795 {
michael@0 796 rv = GetSpecialSystemDirectory(Win_Cookies, getter_AddRefs(localFile));
michael@0 797 }
michael@0 798 else if (inAtom == nsDirectoryService::sDefaultDownloadDirectory)
michael@0 799 {
michael@0 800 rv = GetSpecialSystemDirectory(Win_Downloads, getter_AddRefs(localFile));
michael@0 801 }
michael@0 802 else if (inAtom == nsDirectoryService::sDocs)
michael@0 803 {
michael@0 804 rv = GetSpecialSystemDirectory(Win_Documents, getter_AddRefs(localFile));
michael@0 805 }
michael@0 806 else if (inAtom == nsDirectoryService::sPictures)
michael@0 807 {
michael@0 808 rv = GetSpecialSystemDirectory(Win_Pictures, getter_AddRefs(localFile));
michael@0 809 }
michael@0 810 else if (inAtom == nsDirectoryService::sMusic)
michael@0 811 {
michael@0 812 rv = GetSpecialSystemDirectory(Win_Music, getter_AddRefs(localFile));
michael@0 813 }
michael@0 814 else if (inAtom == nsDirectoryService::sVideos)
michael@0 815 {
michael@0 816 rv = GetSpecialSystemDirectory(Win_Videos, getter_AddRefs(localFile));
michael@0 817 }
michael@0 818 #elif defined (XP_UNIX)
michael@0 819
michael@0 820 else if (inAtom == nsDirectoryService::sLocalDirectory)
michael@0 821 {
michael@0 822 rv = GetSpecialSystemDirectory(Unix_LocalDirectory, getter_AddRefs(localFile));
michael@0 823 }
michael@0 824 else if (inAtom == nsDirectoryService::sLibDirectory)
michael@0 825 {
michael@0 826 rv = GetSpecialSystemDirectory(Unix_LibDirectory, getter_AddRefs(localFile));
michael@0 827 }
michael@0 828 else if (inAtom == nsDirectoryService::sOS_HomeDirectory)
michael@0 829 {
michael@0 830 rv = GetSpecialSystemDirectory(Unix_HomeDirectory, getter_AddRefs(localFile));
michael@0 831 }
michael@0 832 else if (inAtom == nsDirectoryService::sXDGDesktop ||
michael@0 833 inAtom == nsDirectoryService::sOS_DesktopDirectory)
michael@0 834 {
michael@0 835 rv = GetSpecialSystemDirectory(Unix_XDG_Desktop, getter_AddRefs(localFile));
michael@0 836 *persistent = false;
michael@0 837 }
michael@0 838 else if (inAtom == nsDirectoryService::sXDGDocuments)
michael@0 839 {
michael@0 840 rv = GetSpecialSystemDirectory(Unix_XDG_Documents, getter_AddRefs(localFile));
michael@0 841 *persistent = false;
michael@0 842 }
michael@0 843 else if (inAtom == nsDirectoryService::sXDGDownload ||
michael@0 844 inAtom == nsDirectoryService::sDefaultDownloadDirectory)
michael@0 845 {
michael@0 846 rv = GetSpecialSystemDirectory(Unix_XDG_Download, getter_AddRefs(localFile));
michael@0 847 *persistent = false;
michael@0 848 }
michael@0 849 else if (inAtom == nsDirectoryService::sXDGMusic)
michael@0 850 {
michael@0 851 rv = GetSpecialSystemDirectory(Unix_XDG_Music, getter_AddRefs(localFile));
michael@0 852 *persistent = false;
michael@0 853 }
michael@0 854 else if (inAtom == nsDirectoryService::sXDGPictures)
michael@0 855 {
michael@0 856 rv = GetSpecialSystemDirectory(Unix_XDG_Pictures, getter_AddRefs(localFile));
michael@0 857 *persistent = false;
michael@0 858 }
michael@0 859 else if (inAtom == nsDirectoryService::sXDGPublicShare)
michael@0 860 {
michael@0 861 rv = GetSpecialSystemDirectory(Unix_XDG_PublicShare, getter_AddRefs(localFile));
michael@0 862 *persistent = false;
michael@0 863 }
michael@0 864 else if (inAtom == nsDirectoryService::sXDGTemplates)
michael@0 865 {
michael@0 866 rv = GetSpecialSystemDirectory(Unix_XDG_Templates, getter_AddRefs(localFile));
michael@0 867 *persistent = false;
michael@0 868 }
michael@0 869 else if (inAtom == nsDirectoryService::sXDGVideos)
michael@0 870 {
michael@0 871 rv = GetSpecialSystemDirectory(Unix_XDG_Videos, getter_AddRefs(localFile));
michael@0 872 *persistent = false;
michael@0 873 }
michael@0 874 #endif
michael@0 875
michael@0 876 if (NS_FAILED(rv))
michael@0 877 return rv;
michael@0 878
michael@0 879 if (!localFile)
michael@0 880 return NS_ERROR_FAILURE;
michael@0 881
michael@0 882 localFile.forget(_retval);
michael@0 883 return NS_OK;
michael@0 884 }
michael@0 885
michael@0 886 NS_IMETHODIMP
michael@0 887 nsDirectoryService::GetFiles(const char *prop, nsISimpleEnumerator **_retval)
michael@0 888 {
michael@0 889 if (NS_WARN_IF(!_retval))
michael@0 890 return NS_ERROR_INVALID_ARG;
michael@0 891 *_retval = nullptr;
michael@0 892
michael@0 893 return NS_ERROR_FAILURE;
michael@0 894 }

mercurial