blob: 28f01124e57afd8bb3530a149d3665ad435cb120 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2012 The Chromium Authors
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
[email protected]057a85532009-09-02 17:18:225#ifndef BASE_PATH_SERVICE_H_
6#define BASE_PATH_SERVICE_H_
initial.commitd7cae122008-07-26 21:49:387
[email protected]0bea7252011-08-05 15:34:008#include "base/base_export.h"
initial.commitd7cae122008-07-26 21:49:389#include "base/base_paths.h"
[email protected]d6b3af92012-09-26 19:05:1210#include "base/gtest_prod_util.h"
[email protected]90509cb2011-03-25 18:46:3811#include "build/build_config.h"
initial.commitd7cae122008-07-26 21:49:3812
[email protected]d3e6d252012-09-27 00:45:3613namespace base {
brettwf0dea132015-09-25 20:08:5414
[email protected]a3ef4832013-02-02 05:12:3315class FilePath;
[email protected]d3e6d252012-09-27 00:45:3616class ScopedPathOverride;
[email protected]d3e6d252012-09-27 00:45:3617
initial.commitd7cae122008-07-26 21:49:3818// 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//
[email protected]0bea7252011-08-05 15:34:0021class BASE_EXPORT PathService {
initial.commitd7cae122008-07-26 21:49:3822 public:
Greg Thompson92af8b92020-03-13 23:13:5423 // 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.
brettwf0dea132015-09-25 20:08:5426 static bool Get(int key, FilePath* path);
initial.commitd7cae122008-07-26 21:49:3827
Maksim Ivanov9a39401c2020-07-08 19:30:3728 // Returns the corresponding path; CHECKs that the operation succeeds.
29 static FilePath CheckedGet(int key);
30
initial.commitd7cae122008-07-26 21:49:3831 // 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 //
[email protected]52a261f2009-03-03 15:01:1236 // If the given path is relative, then it will be resolved against
37 // DIR_CURRENT.
initial.commitd7cae122008-07-26 21:49:3838 //
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.
[email protected]826ab452014-02-26 13:20:3641 //
42 // Unit tests generally should use ScopedPathOverride instead. Overrides from
43 // one test should not carry over to another.
brettwf0dea132015-09-25 20:08:5444 static bool Override(int key, const FilePath& path);
initial.commitd7cae122008-07-26 21:49:3845
[email protected]ff9ed9f2014-05-02 17:59:4246 // 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
[email protected]cb571e752012-05-09 10:50:1054 // be created in case it doesn't exist already.
55 static bool OverrideAndCreateIfNeeded(int key,
brettwf0dea132015-09-25 20:08:5456 const FilePath& path,
[email protected]ff9ed9f2014-05-02 17:59:4257 bool is_absolute,
[email protected]cb571e752012-05-09 10:50:1058 bool create);
59
initial.commitd7cae122008-07-26 21:49:3860 // To extend the set of supported keys, you can register a path provider,
61 // which is just a function mirroring PathService::Get. The ProviderFunc
62 // returns false if it cannot provide a non-empty path for the given key.
63 // Otherwise, true is returned.
64 //
65 // WARNING: This function could be called on any thread from which the
66 // PathService is used, so a the ProviderFunc MUST BE THREADSAFE.
67 //
brettwf0dea132015-09-25 20:08:5468 typedef bool (*ProviderFunc)(int, FilePath*);
initial.commitd7cae122008-07-26 21:49:3869
70 // Call to register a path provider. You must specify the range "[key_start,
71 // key_end)" of supported path keys.
72 static void RegisterProvider(ProviderFunc provider,
73 int key_start,
74 int key_end);
[email protected]d6b3af92012-09-26 19:05:1275
[email protected]c5a726b32013-01-29 00:56:5676 // Disable internal cache.
77 static void DisableCache();
78
[email protected]1265917f2008-08-12 17:33:5279 private:
brettwf0dea132015-09-25 20:08:5480 friend class ScopedPathOverride;
[email protected]d6b3af92012-09-26 19:05:1281 FRIEND_TEST_ALL_PREFIXES(PathServiceTest, RemoveOverride);
82
83 // Removes an override for a special directory or file. Returns true if there
84 // was an override to remove or false if none was present.
mlcui3f0b3892020-12-10 00:11:1385 static bool RemoveOverrideForTests(int key);
mlcui8a86199b2020-12-03 06:41:2786
87 // Returns whether an override is present for a special directory or file.
88 static bool IsOverriddenForTests(int key);
initial.commitd7cae122008-07-26 21:49:3889};
90
brettwf0dea132015-09-25 20:08:5491} // namespace base
92
[email protected]057a85532009-09-02 17:18:2293#endif // BASE_PATH_SERVICE_H_