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