path_service.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_PATH_SERVICE_H_
  5. #define BASE_PATH_SERVICE_H_
  6. #include "base/base_export.h"
  7. #include "base/base_paths.h"
  8. #include "base/gtest_prod_util.h"
  9. #include "build/build_config.h"
  10. namespace base {
  11. class FilePath;
  12. class ScopedPathOverride;
  13. // The path service is a global table mapping keys to file system paths. It is
  14. // OK to use this service from multiple threads.
  15. //
  16. class BASE_EXPORT PathService {
  17. public:
  18. // Populates |path| with a special directory or file. Returns true on success,
  19. // in which case |path| is guaranteed to have a non-empty value. On failure,
  20. // |path| will not be changed.
  21. static bool Get(int key, FilePath* path);
  22. // Returns the corresponding path; CHECKs that the operation succeeds.
  23. static FilePath CheckedGet(int key);
  24. // Overrides the path to a special directory or file. This cannot be used to
  25. // change the value of DIR_CURRENT, but that should be obvious. Also, if the
  26. // path specifies a directory that does not exist, the directory will be
  27. // created by this method. This method returns true if successful.
  28. //
  29. // If the given path is relative, then it will be resolved against
  30. // DIR_CURRENT.
  31. //
  32. // WARNING: Consumers of PathService::Get may expect paths to be constant
  33. // over the lifetime of the app, so this method should be used with caution.
  34. //
  35. // Unit tests generally should use ScopedPathOverride instead. Overrides from
  36. // one test should not carry over to another.
  37. static bool Override(int key, const FilePath& path);
  38. // This function does the same as PathService::Override but it takes extra
  39. // parameters:
  40. // - |is_absolute| indicates that |path| has already been expanded into an
  41. // absolute path, otherwise MakeAbsoluteFilePath() will be used. This is
  42. // useful to override paths that may not exist yet, since MakeAbsoluteFilePath
  43. // fails for those. Note that MakeAbsoluteFilePath also expands symbolic
  44. // links, even if path.IsAbsolute() is already true.
  45. // - |create| guides whether the directory to be overriden must
  46. // be created in case it doesn't exist already.
  47. static bool OverrideAndCreateIfNeeded(int key,
  48. const FilePath& path,
  49. bool is_absolute,
  50. bool create);
  51. // To extend the set of supported keys, you can register a path provider,
  52. // which is just a function mirroring PathService::Get. The ProviderFunc
  53. // returns false if it cannot provide a non-empty path for the given key.
  54. // Otherwise, true is returned.
  55. //
  56. // WARNING: This function could be called on any thread from which the
  57. // PathService is used, so a the ProviderFunc MUST BE THREADSAFE.
  58. //
  59. typedef bool (*ProviderFunc)(int, FilePath*);
  60. // Call to register a path provider. You must specify the range "[key_start,
  61. // key_end)" of supported path keys.
  62. static void RegisterProvider(ProviderFunc provider,
  63. int key_start,
  64. int key_end);
  65. // Disable internal cache.
  66. static void DisableCache();
  67. private:
  68. friend class ScopedPathOverride;
  69. FRIEND_TEST_ALL_PREFIXES(PathServiceTest, RemoveOverride);
  70. // Removes an override for a special directory or file. Returns true if there
  71. // was an override to remove or false if none was present.
  72. static bool RemoveOverrideForTests(int key);
  73. // Returns whether an override is present for a special directory or file.
  74. static bool IsOverriddenForTests(int key);
  75. };
  76. } // namespace base
  77. #endif // BASE_PATH_SERVICE_H_