[email protected] | cb571e75 | 2012-05-09 10:50:10 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [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 | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
[email protected] | 09ad1e62 | 2008-08-07 20:23:09 | [diff] [blame] | 5 | #include "base/path_service.h" |
[email protected] | 355cc274 | 2008-08-06 16:01:25 | [diff] [blame] | 6 | |
brettw | 1ce49f6 | 2017-04-27 19:42:32 | [diff] [blame] | 7 | #include <unordered_map> |
| 8 | |
[email protected] | 3166220 | 2013-03-23 19:10:54 | [diff] [blame] | 9 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 10 | #include <windows.h> |
| 11 | #include <shellapi.h> |
| 12 | #include <shlobj.h> |
[email protected] | 355cc274 | 2008-08-06 16:01:25 | [diff] [blame] | 13 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 14 | |
Hans Wennborg | c3cffa6 | 2020-04-27 10:09:12 | [diff] [blame] | 15 | #include "base/check_op.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 16 | #include "base/files/file_path.h" |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 17 | #include "base/files/file_util.h" |
Maksim Ivanov | eeb2d080 | 2020-07-17 21:44:00 | [diff] [blame] | 18 | #include "base/logging.h" |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 19 | #include "base/synchronization/lock.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 20 | #include "build/build_config.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 21 | |
| 22 | namespace base { |
brettw | f0dea13 | 2015-09-25 20:08:54 | [diff] [blame] | 23 | |
| 24 | bool PathProvider(int key, FilePath* result); |
| 25 | |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 26 | #if defined(OS_WIN) |
brettw | f0dea13 | 2015-09-25 20:08:54 | [diff] [blame] | 27 | bool PathProviderWin(int key, FilePath* result); |
Avi Drissman | 5b28637 | 2020-07-28 21:59:38 | [diff] [blame] | 28 | #elif defined(OS_APPLE) |
brettw | f0dea13 | 2015-09-25 20:08:54 | [diff] [blame] | 29 | bool PathProviderMac(int key, FilePath* result); |
[email protected] | f7d6997 | 2011-06-21 22:34:50 | [diff] [blame] | 30 | #elif defined(OS_ANDROID) |
brettw | f0dea13 | 2015-09-25 20:08:54 | [diff] [blame] | 31 | bool PathProviderAndroid(int key, FilePath* result); |
scottmg | 1ab7aa8 | 2017-05-25 05:22:49 | [diff] [blame] | 32 | #elif defined(OS_FUCHSIA) |
| 33 | bool PathProviderFuchsia(int key, FilePath* result); |
[email protected] | 5d1937bb | 2009-11-21 01:29:00 | [diff] [blame] | 34 | #elif defined(OS_POSIX) |
brettw | f0dea13 | 2015-09-25 20:08:54 | [diff] [blame] | 35 | // PathProviderPosix is the default path provider on POSIX OSes other than |
| 36 | // Mac and Android. |
| 37 | bool PathProviderPosix(int key, FilePath* result); |
[email protected] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 38 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 39 | |
| 40 | namespace { |
| 41 | |
brettw | 1ce49f6 | 2017-04-27 19:42:32 | [diff] [blame] | 42 | typedef std::unordered_map<int, FilePath> PathMap; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 43 | |
| 44 | // We keep a linked list of providers. In a debug build we ensure that no two |
| 45 | // providers claim overlapping keys. |
| 46 | struct Provider { |
| 47 | PathService::ProviderFunc func; |
| 48 | struct Provider* next; |
| 49 | #ifndef NDEBUG |
| 50 | int key_start; |
| 51 | int key_end; |
| 52 | #endif |
[email protected] | 173cb8a0 | 2008-08-20 15:47:39 | [diff] [blame] | 53 | bool is_static; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 54 | }; |
| 55 | |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 56 | Provider base_provider = {PathProvider, nullptr, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 57 | #ifndef NDEBUG |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 58 | PATH_START, PATH_END, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 59 | #endif |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 60 | true}; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 61 | |
[email protected] | 5d1937bb | 2009-11-21 01:29:00 | [diff] [blame] | 62 | #if defined(OS_WIN) |
[email protected] | da9ccfb | 2012-01-28 00:34:40 | [diff] [blame] | 63 | Provider base_provider_win = { |
brettw | f0dea13 | 2015-09-25 20:08:54 | [diff] [blame] | 64 | PathProviderWin, |
[email protected] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 65 | &base_provider, |
| 66 | #ifndef NDEBUG |
brettw | f0dea13 | 2015-09-25 20:08:54 | [diff] [blame] | 67 | PATH_WIN_START, |
| 68 | PATH_WIN_END, |
[email protected] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 69 | #endif |
[email protected] | 173cb8a0 | 2008-08-20 15:47:39 | [diff] [blame] | 70 | true |
[email protected] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 71 | }; |
| 72 | #endif |
| 73 | |
Avi Drissman | 5b28637 | 2020-07-28 21:59:38 | [diff] [blame] | 74 | #if defined(OS_APPLE) |
[email protected] | da9ccfb | 2012-01-28 00:34:40 | [diff] [blame] | 75 | Provider base_provider_mac = { |
brettw | f0dea13 | 2015-09-25 20:08:54 | [diff] [blame] | 76 | PathProviderMac, |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 77 | &base_provider, |
| 78 | #ifndef NDEBUG |
brettw | f0dea13 | 2015-09-25 20:08:54 | [diff] [blame] | 79 | PATH_MAC_START, |
| 80 | PATH_MAC_END, |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 81 | #endif |
[email protected] | 173cb8a0 | 2008-08-20 15:47:39 | [diff] [blame] | 82 | true |
| 83 | }; |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 84 | #endif |
[email protected] | 4c0040c | 2008-08-15 01:04:11 | [diff] [blame] | 85 | |
[email protected] | f7d6997 | 2011-06-21 22:34:50 | [diff] [blame] | 86 | #if defined(OS_ANDROID) |
[email protected] | da9ccfb | 2012-01-28 00:34:40 | [diff] [blame] | 87 | Provider base_provider_android = { |
brettw | f0dea13 | 2015-09-25 20:08:54 | [diff] [blame] | 88 | PathProviderAndroid, |
[email protected] | f7d6997 | 2011-06-21 22:34:50 | [diff] [blame] | 89 | &base_provider, |
| 90 | #ifndef NDEBUG |
brettw | f0dea13 | 2015-09-25 20:08:54 | [diff] [blame] | 91 | PATH_ANDROID_START, |
| 92 | PATH_ANDROID_END, |
[email protected] | f7d6997 | 2011-06-21 22:34:50 | [diff] [blame] | 93 | #endif |
| 94 | true |
| 95 | }; |
| 96 | #endif |
| 97 | |
scottmg | 1ab7aa8 | 2017-05-25 05:22:49 | [diff] [blame] | 98 | #if defined(OS_FUCHSIA) |
| 99 | Provider base_provider_fuchsia = {PathProviderFuchsia, &base_provider, |
| 100 | #ifndef NDEBUG |
| 101 | 0, 0, |
| 102 | #endif |
| 103 | true}; |
| 104 | #endif |
| 105 | |
Avi Drissman | 5b28637 | 2020-07-28 21:59:38 | [diff] [blame] | 106 | #if defined(OS_POSIX) && !defined(OS_APPLE) && !defined(OS_ANDROID) && \ |
scottmg | 1ab7aa8 | 2017-05-25 05:22:49 | [diff] [blame] | 107 | !defined(OS_FUCHSIA) |
[email protected] | da9ccfb | 2012-01-28 00:34:40 | [diff] [blame] | 108 | Provider base_provider_posix = { |
brettw | f0dea13 | 2015-09-25 20:08:54 | [diff] [blame] | 109 | PathProviderPosix, |
[email protected] | 4c0040c | 2008-08-15 01:04:11 | [diff] [blame] | 110 | &base_provider, |
| 111 | #ifndef NDEBUG |
brettw | f0dea13 | 2015-09-25 20:08:54 | [diff] [blame] | 112 | PATH_POSIX_START, |
| 113 | PATH_POSIX_END, |
[email protected] | 4c0040c | 2008-08-15 01:04:11 | [diff] [blame] | 114 | #endif |
[email protected] | 173cb8a0 | 2008-08-20 15:47:39 | [diff] [blame] | 115 | true |
| 116 | }; |
[email protected] | 4c0040c | 2008-08-15 01:04:11 | [diff] [blame] | 117 | #endif |
| 118 | |
| 119 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 120 | struct PathData { |
brettw | f0dea13 | 2015-09-25 20:08:54 | [diff] [blame] | 121 | Lock lock; |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 122 | PathMap cache; // Cache mappings from path key to path value. |
| 123 | PathMap overrides; // Track path overrides. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 124 | Provider* providers; // Linked list of path service providers. |
[email protected] | c5a726b3 | 2013-01-29 00:56:56 | [diff] [blame] | 125 | bool cache_disabled; // Don't use cache if true; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 126 | |
[email protected] | c5a726b3 | 2013-01-29 00:56:56 | [diff] [blame] | 127 | PathData() : cache_disabled(false) { |
[email protected] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 128 | #if defined(OS_WIN) |
| 129 | providers = &base_provider_win; |
Avi Drissman | 5b28637 | 2020-07-28 21:59:38 | [diff] [blame] | 130 | #elif defined(OS_APPLE) |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 131 | providers = &base_provider_mac; |
[email protected] | f7d6997 | 2011-06-21 22:34:50 | [diff] [blame] | 132 | #elif defined(OS_ANDROID) |
| 133 | providers = &base_provider_android; |
scottmg | 1ab7aa8 | 2017-05-25 05:22:49 | [diff] [blame] | 134 | #elif defined(OS_FUCHSIA) |
| 135 | providers = &base_provider_fuchsia; |
[email protected] | 5d1937bb | 2009-11-21 01:29:00 | [diff] [blame] | 136 | #elif defined(OS_POSIX) |
| 137 | providers = &base_provider_posix; |
[email protected] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 138 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 139 | } |
| 140 | }; |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 141 | |
[email protected] | 1265917f | 2008-08-12 17:33:52 | [diff] [blame] | 142 | static PathData* GetPathData() { |
vmpstr | 5f02eae | 2017-02-13 21:11:41 | [diff] [blame] | 143 | static auto* path_data = new PathData(); |
scottmg | 6ece5ae | 2017-02-01 18:25:19 | [diff] [blame] | 144 | return path_data; |
[email protected] | 1265917f | 2008-08-12 17:33:52 | [diff] [blame] | 145 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 146 | |
Benoit Lize | 2585915 | 2020-07-09 11:52:09 | [diff] [blame] | 147 | // Tries to find |key| in the cache. |
| 148 | bool LockedGetFromCache(int key, const PathData* path_data, FilePath* result) |
| 149 | EXCLUSIVE_LOCKS_REQUIRED(path_data->lock) { |
[email protected] | c5a726b3 | 2013-01-29 00:56:56 | [diff] [blame] | 150 | if (path_data->cache_disabled) |
| 151 | return false; |
[email protected] | 6723f83 | 2008-08-11 15:38:27 | [diff] [blame] | 152 | // check for a cached version |
jdoerrie | 1c4b8ff | 2018-10-03 00:10:57 | [diff] [blame] | 153 | auto it = path_data->cache.find(key); |
[email protected] | 6723f83 | 2008-08-11 15:38:27 | [diff] [blame] | 154 | if (it != path_data->cache.end()) { |
| 155 | *result = it->second; |
| 156 | return true; |
| 157 | } |
| 158 | return false; |
| 159 | } |
| 160 | |
Benoit Lize | 2585915 | 2020-07-09 11:52:09 | [diff] [blame] | 161 | // Tries to find |key| in the overrides map. |
| 162 | bool LockedGetFromOverrides(int key, PathData* path_data, FilePath* result) |
| 163 | EXCLUSIVE_LOCKS_REQUIRED(path_data->lock) { |
[email protected] | 846c3ecea | 2011-12-14 18:47:26 | [diff] [blame] | 164 | // check for an overridden version. |
[email protected] | 34e043b | 2010-09-09 23:49:04 | [diff] [blame] | 165 | PathMap::const_iterator it = path_data->overrides.find(key); |
| 166 | if (it != path_data->overrides.end()) { |
[email protected] | c5a726b3 | 2013-01-29 00:56:56 | [diff] [blame] | 167 | if (!path_data->cache_disabled) |
| 168 | path_data->cache[key] = it->second; |
[email protected] | 34e043b | 2010-09-09 23:49:04 | [diff] [blame] | 169 | *result = it->second; |
| 170 | return true; |
| 171 | } |
| 172 | return false; |
| 173 | } |
| 174 | |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 175 | } // namespace |
[email protected] | 6723f83 | 2008-08-11 15:38:27 | [diff] [blame] | 176 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 177 | // TODO(brettw): this function does not handle long paths (filename > MAX_PATH) |
| 178 | // characters). This isn't supported very well by Windows right now, so it is |
| 179 | // moot, but we should keep this in mind for the future. |
| 180 | // static |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 181 | bool PathService::Get(int key, FilePath* result) { |
[email protected] | 1265917f | 2008-08-12 17:33:52 | [diff] [blame] | 182 | PathData* path_data = GetPathData(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 183 | DCHECK(path_data); |
| 184 | DCHECK(result); |
brettw | f0dea13 | 2015-09-25 20:08:54 | [diff] [blame] | 185 | DCHECK_GE(key, DIR_CURRENT); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 186 | |
| 187 | // special case the current directory because it can never be cached |
brettw | f0dea13 | 2015-09-25 20:08:54 | [diff] [blame] | 188 | if (key == DIR_CURRENT) |
| 189 | return GetCurrentDirectory(result); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 190 | |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 191 | Provider* provider = nullptr; |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 192 | { |
brettw | f0dea13 | 2015-09-25 20:08:54 | [diff] [blame] | 193 | AutoLock scoped_lock(path_data->lock); |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 194 | if (LockedGetFromCache(key, path_data, result)) |
| 195 | return true; |
[email protected] | c1a9f8d4 | 2009-02-28 01:49:55 | [diff] [blame] | 196 | |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 197 | if (LockedGetFromOverrides(key, path_data, result)) |
| 198 | return true; |
| 199 | |
| 200 | // Get the beginning of the list while it is still locked. |
| 201 | provider = path_data->providers; |
| 202 | } |
[email protected] | 34e043b | 2010-09-09 23:49:04 | [diff] [blame] | 203 | |
[email protected] | 4792a26 | 2008-11-19 16:50:03 | [diff] [blame] | 204 | FilePath path; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 205 | |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 206 | // Iterating does not need the lock because only the list head might be |
| 207 | // modified on another thread. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 208 | while (provider) { |
[email protected] | 4792a26 | 2008-11-19 16:50:03 | [diff] [blame] | 209 | if (provider->func(key, &path)) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 210 | break; |
[email protected] | c1a9f8d4 | 2009-02-28 01:49:55 | [diff] [blame] | 211 | DCHECK(path.empty()) << "provider should not have modified path"; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 212 | provider = provider->next; |
| 213 | } |
| 214 | |
[email protected] | c1a9f8d4 | 2009-02-28 01:49:55 | [diff] [blame] | 215 | if (path.empty()) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 216 | return false; |
| 217 | |
[email protected] | 082f820 | 2013-01-26 04:51:29 | [diff] [blame] | 218 | if (path.ReferencesParent()) { |
| 219 | // Make sure path service never returns a path with ".." in it. |
[email protected] | 1547693 | 2013-04-12 05:17:15 | [diff] [blame] | 220 | path = MakeAbsoluteFilePath(path); |
| 221 | if (path.empty()) |
[email protected] | 082f820 | 2013-01-26 04:51:29 | [diff] [blame] | 222 | return false; |
[email protected] | 082f820 | 2013-01-26 04:51:29 | [diff] [blame] | 223 | } |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 224 | *result = path; |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 225 | |
brettw | f0dea13 | 2015-09-25 20:08:54 | [diff] [blame] | 226 | AutoLock scoped_lock(path_data->lock); |
[email protected] | c5a726b3 | 2013-01-29 00:56:56 | [diff] [blame] | 227 | if (!path_data->cache_disabled) |
| 228 | path_data->cache[key] = path; |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 229 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 230 | return true; |
| 231 | } |
| 232 | |
Maksim Ivanov | 9a39401c | 2020-07-08 19:30:37 | [diff] [blame] | 233 | FilePath PathService::CheckedGet(int key) { |
| 234 | FilePath path; |
Maksim Ivanov | eeb2d080 | 2020-07-17 21:44:00 | [diff] [blame] | 235 | LOG_IF(FATAL, !Get(key, &path)) << "Failed to get the path for " << key; |
Maksim Ivanov | 9a39401c | 2020-07-08 19:30:37 | [diff] [blame] | 236 | return path; |
| 237 | } |
| 238 | |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 239 | // static |
[email protected] | eca6a4f | 2009-06-25 17:29:09 | [diff] [blame] | 240 | bool PathService::Override(int key, const FilePath& path) { |
[email protected] | ff9ed9f | 2014-05-02 17:59:42 | [diff] [blame] | 241 | // Just call the full function with true for the value of |create|, and |
| 242 | // assume that |path| may not be absolute yet. |
| 243 | return OverrideAndCreateIfNeeded(key, path, false, true); |
[email protected] | cb571e75 | 2012-05-09 10:50:10 | [diff] [blame] | 244 | } |
| 245 | |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 246 | // static |
[email protected] | cb571e75 | 2012-05-09 10:50:10 | [diff] [blame] | 247 | bool PathService::OverrideAndCreateIfNeeded(int key, |
| 248 | const FilePath& path, |
[email protected] | ff9ed9f | 2014-05-02 17:59:42 | [diff] [blame] | 249 | bool is_absolute, |
[email protected] | cb571e75 | 2012-05-09 10:50:10 | [diff] [blame] | 250 | bool create) { |
[email protected] | 1265917f | 2008-08-12 17:33:52 | [diff] [blame] | 251 | PathData* path_data = GetPathData(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 252 | DCHECK(path_data); |
brettw | f0dea13 | 2015-09-25 20:08:54 | [diff] [blame] | 253 | DCHECK_GT(key, DIR_CURRENT) << "invalid path key"; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 254 | |
[email protected] | eca6a4f | 2009-06-25 17:29:09 | [diff] [blame] | 255 | FilePath file_path = path; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 256 | |
[email protected] | cb571e75 | 2012-05-09 10:50:10 | [diff] [blame] | 257 | // For some locations this will fail if called from inside the sandbox there- |
| 258 | // fore we protect this call with a flag. |
| 259 | if (create) { |
| 260 | // Make sure the directory exists. We need to do this before we translate |
[email protected] | 1547693 | 2013-04-12 05:17:15 | [diff] [blame] | 261 | // this to the absolute path because on POSIX, MakeAbsoluteFilePath fails |
| 262 | // if called on a non-existent path. |
brettw | f0dea13 | 2015-09-25 20:08:54 | [diff] [blame] | 263 | if (!PathExists(file_path) && !CreateDirectory(file_path)) |
[email protected] | cb571e75 | 2012-05-09 10:50:10 | [diff] [blame] | 264 | return false; |
| 265 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 266 | |
[email protected] | 1547693 | 2013-04-12 05:17:15 | [diff] [blame] | 267 | // We need to have an absolute path. |
[email protected] | ff9ed9f | 2014-05-02 17:59:42 | [diff] [blame] | 268 | if (!is_absolute) { |
| 269 | file_path = MakeAbsoluteFilePath(file_path); |
| 270 | if (file_path.empty()) |
| 271 | return false; |
| 272 | } |
| 273 | DCHECK(file_path.IsAbsolute()); |
[email protected] | dabdb68 | 2009-10-27 23:31:36 | [diff] [blame] | 274 | |
brettw | f0dea13 | 2015-09-25 20:08:54 | [diff] [blame] | 275 | AutoLock scoped_lock(path_data->lock); |
[email protected] | 34e043b | 2010-09-09 23:49:04 | [diff] [blame] | 276 | |
| 277 | // Clear the cache now. Some of its entries could have depended |
| 278 | // on the value we are overriding, and are now out of sync with reality. |
| 279 | path_data->cache.clear(); |
| 280 | |
[email protected] | 34e043b | 2010-09-09 23:49:04 | [diff] [blame] | 281 | path_data->overrides[key] = file_path; |
| 282 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 283 | return true; |
| 284 | } |
| 285 | |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 286 | // static |
mlcui | 3f0b389 | 2020-12-10 00:11:13 | [diff] [blame] | 287 | bool PathService::RemoveOverrideForTests(int key) { |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 288 | PathData* path_data = GetPathData(); |
| 289 | DCHECK(path_data); |
| 290 | |
brettw | f0dea13 | 2015-09-25 20:08:54 | [diff] [blame] | 291 | AutoLock scoped_lock(path_data->lock); |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 292 | |
| 293 | if (path_data->overrides.find(key) == path_data->overrides.end()) |
| 294 | return false; |
| 295 | |
| 296 | // Clear the cache now. Some of its entries could have depended on the value |
| 297 | // we are going to remove, and are now out of sync. |
| 298 | path_data->cache.clear(); |
| 299 | |
| 300 | path_data->overrides.erase(key); |
| 301 | |
| 302 | return true; |
| 303 | } |
| 304 | |
| 305 | // static |
mlcui | 8a86199b | 2020-12-03 06:41:27 | [diff] [blame] | 306 | bool PathService::IsOverriddenForTests(int key) { |
| 307 | PathData* path_data = GetPathData(); |
| 308 | DCHECK(path_data); |
| 309 | |
| 310 | AutoLock scoped_lock(path_data->lock); |
| 311 | |
| 312 | return path_data->overrides.find(key) != path_data->overrides.end(); |
| 313 | } |
| 314 | |
| 315 | // static |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 316 | void PathService::RegisterProvider(ProviderFunc func, int key_start, |
| 317 | int key_end) { |
[email protected] | 1265917f | 2008-08-12 17:33:52 | [diff] [blame] | 318 | PathData* path_data = GetPathData(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 319 | DCHECK(path_data); |
[email protected] | 88563f6 | 2011-03-13 22:13:33 | [diff] [blame] | 320 | DCHECK_GT(key_end, key_start); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 321 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 322 | Provider* p; |
| 323 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 324 | p = new Provider; |
[email protected] | 173cb8a0 | 2008-08-20 15:47:39 | [diff] [blame] | 325 | p->is_static = false; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 326 | p->func = func; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 327 | #ifndef NDEBUG |
| 328 | p->key_start = key_start; |
| 329 | p->key_end = key_end; |
| 330 | #endif |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 331 | |
brettw | f0dea13 | 2015-09-25 20:08:54 | [diff] [blame] | 332 | AutoLock scoped_lock(path_data->lock); |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 333 | |
| 334 | #ifndef NDEBUG |
| 335 | Provider *iter = path_data->providers; |
| 336 | while (iter) { |
| 337 | DCHECK(key_start >= iter->key_end || key_end <= iter->key_start) << |
| 338 | "path provider collision"; |
| 339 | iter = iter->next; |
| 340 | } |
| 341 | #endif |
| 342 | |
| 343 | p->next = path_data->providers; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 344 | path_data->providers = p; |
| 345 | } |
[email protected] | c5a726b3 | 2013-01-29 00:56:56 | [diff] [blame] | 346 | |
| 347 | // static |
| 348 | void PathService::DisableCache() { |
| 349 | PathData* path_data = GetPathData(); |
| 350 | DCHECK(path_data); |
| 351 | |
brettw | f0dea13 | 2015-09-25 20:08:54 | [diff] [blame] | 352 | AutoLock scoped_lock(path_data->lock); |
[email protected] | c5a726b3 | 2013-01-29 00:56:56 | [diff] [blame] | 353 | path_data->cache.clear(); |
| 354 | path_data->cache_disabled = true; |
| 355 | } |
brettw | f0dea13 | 2015-09-25 20:08:54 | [diff] [blame] | 356 | |
| 357 | } // namespace base |