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