path_service.h 3.5 KB

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