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