pastarmovj@chromium.org | 8f45209 | 2012-05-09 19:50:10 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
jcampan@chromium.org | 36fe221 | 2009-09-03 02:18:22 +0900 | [diff] [blame] | 5 | #ifndef BASE_PATH_SERVICE_H_ |
| 6 | #define BASE_PATH_SERVICE_H_ |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 7 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 8 | #include <string> |
| 9 | |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 10 | #include "base/base_export.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 11 | #include "base/base_paths.h" |
pastarmovj@chromium.org | 5dce41a | 2012-09-27 04:05:12 +0900 | [diff] [blame] | 12 | #include "base/gtest_prod_util.h" |
rvargas@google.com | 4891e7d | 2011-03-26 03:46:38 +0900 | [diff] [blame] | 13 | #include "build/build_config.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 14 | |
pastarmovj@chromium.org | e61bf33 | 2012-09-27 09:45:36 +0900 | [diff] [blame] | 15 | namespace base { |
brettw | 8f52b65 | 2015-09-26 05:08:54 +0900 | [diff] [blame] | 16 | |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 17 | class FilePath; |
pastarmovj@chromium.org | e61bf33 | 2012-09-27 09:45:36 +0900 | [diff] [blame] | 18 | class ScopedPathOverride; |
pastarmovj@chromium.org | e61bf33 | 2012-09-27 09:45:36 +0900 | [diff] [blame] | 19 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 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. |
| 22 | // |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 23 | class BASE_EXPORT PathService { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 24 | public: |
| 25 | // Retrieves a path to a special directory or file and places it into the |
| 26 | // string pointed to by 'path'. If you ask for a directory it is guaranteed |
| 27 | // to NOT have a path separator at the end. For example, "c:\windows\temp" |
| 28 | // Directories are also guaranteed to exist when this function succeeds. |
| 29 | // |
| 30 | // Returns true if the directory or file was successfully retrieved. On |
| 31 | // failure, 'path' will not be changed. |
brettw | 8f52b65 | 2015-09-26 05:08:54 +0900 | [diff] [blame] | 32 | static bool Get(int key, FilePath* path); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 33 | |
| 34 | // Overrides the path to a special directory or file. This cannot be used to |
| 35 | // change the value of DIR_CURRENT, but that should be obvious. Also, if the |
| 36 | // path specifies a directory that does not exist, the directory will be |
| 37 | // created by this method. This method returns true if successful. |
| 38 | // |
maruel@chromium.org | 8fe7adc | 2009-03-04 00:01:12 +0900 | [diff] [blame] | 39 | // If the given path is relative, then it will be resolved against |
| 40 | // DIR_CURRENT. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 41 | // |
| 42 | // WARNING: Consumers of PathService::Get may expect paths to be constant |
| 43 | // over the lifetime of the app, so this method should be used with caution. |
thestig@chromium.org | 54debc4 | 2014-02-26 22:20:36 +0900 | [diff] [blame] | 44 | // |
| 45 | // Unit tests generally should use ScopedPathOverride instead. Overrides from |
| 46 | // one test should not carry over to another. |
brettw | 8f52b65 | 2015-09-26 05:08:54 +0900 | [diff] [blame] | 47 | static bool Override(int key, const FilePath& path); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 48 | |
joaodasilva@chromium.org | 41819c5 | 2014-05-03 02:59:42 +0900 | [diff] [blame] | 49 | // This function does the same as PathService::Override but it takes extra |
| 50 | // parameters: |
| 51 | // - |is_absolute| indicates that |path| has already been expanded into an |
| 52 | // absolute path, otherwise MakeAbsoluteFilePath() will be used. This is |
| 53 | // useful to override paths that may not exist yet, since MakeAbsoluteFilePath |
| 54 | // fails for those. Note that MakeAbsoluteFilePath also expands symbolic |
| 55 | // links, even if path.IsAbsolute() is already true. |
| 56 | // - |create| guides whether the directory to be overriden must |
pastarmovj@chromium.org | 8f45209 | 2012-05-09 19:50:10 +0900 | [diff] [blame] | 57 | // be created in case it doesn't exist already. |
| 58 | static bool OverrideAndCreateIfNeeded(int key, |
brettw | 8f52b65 | 2015-09-26 05:08:54 +0900 | [diff] [blame] | 59 | const FilePath& path, |
joaodasilva@chromium.org | 41819c5 | 2014-05-03 02:59:42 +0900 | [diff] [blame] | 60 | bool is_absolute, |
pastarmovj@chromium.org | 8f45209 | 2012-05-09 19:50:10 +0900 | [diff] [blame] | 61 | bool create); |
| 62 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 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 | // |
brettw | 8f52b65 | 2015-09-26 05:08:54 +0900 | [diff] [blame] | 71 | typedef bool (*ProviderFunc)(int, FilePath*); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 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); |
pastarmovj@chromium.org | 5dce41a | 2012-09-27 04:05:12 +0900 | [diff] [blame] | 78 | |
vitalybuka@chromium.org | 5a75b7e | 2013-01-29 09:56:56 +0900 | [diff] [blame] | 79 | // Disable internal cache. |
| 80 | static void DisableCache(); |
| 81 | |
erikkay@google.com | a3ff275 | 2008-08-13 02:33:52 +0900 | [diff] [blame] | 82 | private: |
brettw | 8f52b65 | 2015-09-26 05:08:54 +0900 | [diff] [blame] | 83 | friend class ScopedPathOverride; |
pastarmovj@chromium.org | 5dce41a | 2012-09-27 04:05:12 +0900 | [diff] [blame] | 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 | // NOTE: This function is intended to be used by tests only! |
| 89 | static bool RemoveOverride(int key); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 90 | }; |
| 91 | |
brettw | 8f52b65 | 2015-09-26 05:08:54 +0900 | [diff] [blame] | 92 | } // namespace base |
| 93 | |
| 94 | // TODO(brettw) Convert all callers to using the base namespace and remove this. |
| 95 | using base::PathService; |
| 96 | |
jcampan@chromium.org | 36fe221 | 2009-09-03 02:18:22 +0900 | [diff] [blame] | 97 | #endif // BASE_PATH_SERVICE_H_ |