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