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