nsXREDirProvider.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef _nsXREDirProvider_h__
  6. #define _nsXREDirProvider_h__
  7. #include "nsIDirectoryService.h"
  8. #include "nsIProfileMigrator.h"
  9. #include "nsIFile.h"
  10. #include "nsCOMPtr.h"
  11. #include "nsCOMArray.h"
  12. #include "mozilla/Attributes.h"
  13. class nsXREDirProvider final : public nsIDirectoryServiceProvider2,
  14. public nsIProfileStartup
  15. {
  16. public:
  17. // we use a custom isupports implementation (no refcount)
  18. NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override;
  19. NS_IMETHOD_(MozExternalRefCountType) AddRef(void) override;
  20. NS_IMETHOD_(MozExternalRefCountType) Release(void) override;
  21. NS_DECL_NSIDIRECTORYSERVICEPROVIDER
  22. NS_DECL_NSIDIRECTORYSERVICEPROVIDER2
  23. NS_DECL_NSIPROFILESTARTUP
  24. nsXREDirProvider();
  25. // if aXULAppDir is null, use gArgv[0]
  26. nsresult Initialize(nsIFile *aXULAppDir,
  27. nsIFile *aGREDir,
  28. nsIDirectoryServiceProvider* aAppProvider = nullptr);
  29. ~nsXREDirProvider();
  30. static nsXREDirProvider* GetSingleton();
  31. nsresult GetUserProfilesRootDir(nsIFile** aResult,
  32. const nsACString* aProfileName,
  33. const nsACString* aAppName,
  34. const nsACString* aVendorName);
  35. nsresult GetUserProfilesLocalDir(nsIFile** aResult,
  36. const nsACString* aProfileName,
  37. const nsACString* aAppName,
  38. const nsACString* aVendorName);
  39. // We only set the profile dir, we don't ensure that it exists;
  40. // that is the responsibility of the toolkit profile service.
  41. // We also don't fire profile-changed notifications... that is
  42. // the responsibility of the apprunner.
  43. nsresult SetProfile(nsIFile* aProfileDir, nsIFile* aProfileLocalDir);
  44. void DoShutdown();
  45. static nsresult GetUserAppDataDirectory(nsIFile* *aFile) {
  46. return GetUserDataDirectory(aFile, false, nullptr, nullptr, nullptr);
  47. }
  48. static nsresult GetUserLocalDataDirectory(nsIFile* *aFile) {
  49. return GetUserDataDirectory(aFile, true, nullptr, nullptr, nullptr);
  50. }
  51. // By default GetUserDataDirectory gets profile path from gAppData,
  52. // but that can be overridden by using aProfileName/aAppName/aVendorName.
  53. static nsresult GetUserDataDirectory(nsIFile** aFile, bool aLocal,
  54. const nsACString* aProfileName,
  55. const nsACString* aAppName,
  56. const nsACString* aVendorName);
  57. /* make sure you clone it, if you need to do stuff to it */
  58. nsIFile* GetGREDir() { return mGREDir; }
  59. nsIFile* GetGREBinDir() { return mGREBinDir; }
  60. nsIFile* GetAppDir() {
  61. if (mXULAppDir)
  62. return mXULAppDir;
  63. return mGREDir;
  64. }
  65. /**
  66. * Get the directory under which update directory is created.
  67. * This method may be called before XPCOM is started. aResult
  68. * is a clone, it may be modified.
  69. */
  70. nsresult GetUpdateRootDir(nsIFile* *aResult);
  71. /**
  72. * Get the profile startup directory as determined by this class or by
  73. * mAppProvider. This method may be called before XPCOM is started. aResult
  74. * is a clone, it may be modified.
  75. */
  76. nsresult GetProfileStartupDir(nsIFile* *aResult);
  77. /**
  78. * Get the profile directory as determined by this class or by an
  79. * embedder-provided XPCOM directory provider. Only call this method
  80. * when XPCOM is initialized! aResult is a clone, it may be modified.
  81. */
  82. nsresult GetProfileDir(nsIFile* *aResult);
  83. protected:
  84. nsresult GetFilesInternal(const char* aProperty, nsISimpleEnumerator** aResult);
  85. static nsresult GetUserDataDirectoryHome(nsIFile* *aFile, bool aLocal);
  86. static nsresult GetSysUserExtensionsDirectory(nsIFile* *aFile);
  87. #if defined(XP_UNIX)
  88. static nsresult GetSystemExtensionsDirectory(nsIFile** aFile);
  89. #endif
  90. static nsresult EnsureDirectoryExists(nsIFile* aDirectory);
  91. // Determine the profile path within the UAppData directory. This is different
  92. // on every major platform.
  93. static nsresult AppendProfilePath(nsIFile* aFile,
  94. const nsACString* aProfileName,
  95. const nsACString* aAppName,
  96. const nsACString* aVendorName,
  97. bool aLocal);
  98. static nsresult AppendSysUserExtensionPath(nsIFile* aFile);
  99. // Internal helper that splits a path into components using the '/' and '\\'
  100. // delimiters.
  101. static inline nsresult AppendProfileString(nsIFile* aFile, const char* aPath);
  102. // Calculate and register extension and theme bundle directories.
  103. void LoadExtensionBundleDirectories();
  104. void Append(nsIFile* aDirectory);
  105. nsCOMPtr<nsIDirectoryServiceProvider> mAppProvider;
  106. // On OSX, mGREDir points to .app/Contents/Resources
  107. nsCOMPtr<nsIFile> mGREDir;
  108. // On OSX, mGREBinDir points to .app/Contents/MacOS
  109. nsCOMPtr<nsIFile> mGREBinDir;
  110. // On OSX, mXULAppDir points to .app/Contents/Resources/browser
  111. nsCOMPtr<nsIFile> mXULAppDir;
  112. nsCOMPtr<nsIFile> mProfileDir;
  113. nsCOMPtr<nsIFile> mProfileLocalDir;
  114. bool mProfileNotified;
  115. nsCOMArray<nsIFile> mAppBundleDirectories;
  116. nsCOMArray<nsIFile> mExtensionDirectories;
  117. nsCOMArray<nsIFile> mThemeDirectories;
  118. };
  119. #endif