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