[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] | 09ad1e62 | 2008-08-07 20:23:09 | [diff] [blame] | 13 | #include "base/file_util.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 14 | #include "base/files/file_path.h" |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 15 | #include "base/hash_tables.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" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 19 | |
[email protected] | 04af979a | 2013-02-16 04:12:26 | [diff] [blame] | 20 | using base::FilePath; |
| 21 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 22 | namespace base { |
[email protected] | 4792a26 | 2008-11-19 16:50:03 | [diff] [blame] | 23 | bool PathProvider(int key, FilePath* result); |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 24 | #if defined(OS_WIN) |
[email protected] | 4792a26 | 2008-11-19 16:50:03 | [diff] [blame] | 25 | bool PathProviderWin(int key, FilePath* result); |
[email protected] | 4c0040c | 2008-08-15 01:04:11 | [diff] [blame] | 26 | #elif defined(OS_MACOSX) |
[email protected] | 4792a26 | 2008-11-19 16:50:03 | [diff] [blame] | 27 | bool PathProviderMac(int key, FilePath* result); |
[email protected] | f7d6997 | 2011-06-21 22:34:50 | [diff] [blame] | 28 | #elif defined(OS_ANDROID) |
| 29 | bool PathProviderAndroid(int key, FilePath* result); |
[email protected] | 5d1937bb | 2009-11-21 01:29:00 | [diff] [blame] | 30 | #elif defined(OS_POSIX) |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 31 | // PathProviderPosix is the default path provider on POSIX OSes other than |
| 32 | // Mac and Android. |
[email protected] | 5d1937bb | 2009-11-21 01:29:00 | [diff] [blame] | 33 | bool PathProviderPosix(int key, FilePath* result); |
[email protected] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 34 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | namespace { |
| 38 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 39 | typedef base::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 = { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 54 | base::PathProvider, |
| 55 | NULL, |
| 56 | #ifndef NDEBUG |
| 57 | base::PATH_START, |
[email protected] | 173cb8a0 | 2008-08-20 15:47:39 | [diff] [blame] | 58 | base::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 = { |
[email protected] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 65 | base::PathProviderWin, |
| 66 | &base_provider, |
| 67 | #ifndef NDEBUG |
| 68 | base::PATH_WIN_START, |
[email protected] | 173cb8a0 | 2008-08-20 15:47:39 | [diff] [blame] | 69 | base::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 = { |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 77 | base::PathProviderMac, |
| 78 | &base_provider, |
| 79 | #ifndef NDEBUG |
| 80 | base::PATH_MAC_START, |
[email protected] | 173cb8a0 | 2008-08-20 15:47:39 | [diff] [blame] | 81 | base::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 = { |
[email protected] | f7d6997 | 2011-06-21 22:34:50 | [diff] [blame] | 89 | base::PathProviderAndroid, |
| 90 | &base_provider, |
| 91 | #ifndef NDEBUG |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 92 | base::PATH_ANDROID_START, |
| 93 | base::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 = { |
[email protected] | 5d1937bb | 2009-11-21 01:29:00 | [diff] [blame] | 101 | base::PathProviderPosix, |
[email protected] | 4c0040c | 2008-08-15 01:04:11 | [diff] [blame] | 102 | &base_provider, |
| 103 | #ifndef NDEBUG |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 104 | base::PATH_POSIX_START, |
| 105 | base::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 { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 113 | base::Lock lock; |
| 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 | } |
[email protected] | 173cb8a0 | 2008-08-20 15:47:39 | [diff] [blame] | 130 | |
| 131 | ~PathData() { |
| 132 | Provider* p = providers; |
| 133 | while (p) { |
| 134 | Provider* next = p->next; |
| 135 | if (!p->is_static) |
| 136 | delete p; |
| 137 | p = next; |
| 138 | } |
| 139 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 140 | }; |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 141 | |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 142 | static base::LazyInstance<PathData> g_path_data = LAZY_INSTANCE_INITIALIZER; |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 143 | |
[email protected] | 1265917f | 2008-08-12 17:33:52 | [diff] [blame] | 144 | static PathData* GetPathData() { |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 145 | return g_path_data.Pointer(); |
[email protected] | 1265917f | 2008-08-12 17:33:52 | [diff] [blame] | 146 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 147 | |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 148 | // Tries to find |key| in the cache. |path_data| should be locked by the caller! |
| 149 | bool LockedGetFromCache(int key, const PathData* path_data, FilePath* result) { |
[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 |
| 153 | PathMap::const_iterator it = path_data->cache.find(key); |
| 154 | if (it != path_data->cache.end()) { |
| 155 | *result = it->second; |
| 156 | return true; |
| 157 | } |
| 158 | return false; |
| 159 | } |
| 160 | |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 161 | // Tries to find |key| in the overrides map. |path_data| should be locked by the |
| 162 | // caller! |
| 163 | bool LockedGetFromOverrides(int key, PathData* path_data, FilePath* result) { |
[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); |
[email protected] | 8e03fec | 2011-03-31 20:34:25 | [diff] [blame] | 185 | DCHECK_GE(key, base::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 |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 188 | if (key == base::DIR_CURRENT) |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 189 | return file_util::GetCurrentDirectory(result); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 190 | |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 191 | Provider* provider = NULL; |
| 192 | { |
| 193 | base::AutoLock scoped_lock(path_data->lock); |
| 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. |
| 220 | if (!file_util::AbsolutePath(&path)) { |
| 221 | return false; |
| 222 | } |
| 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 | |
| 226 | base::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 | |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 233 | // static |
[email protected] | eca6a4f | 2009-06-25 17:29:09 | [diff] [blame] | 234 | bool PathService::Override(int key, const FilePath& path) { |
[email protected] | cb571e75 | 2012-05-09 10:50:10 | [diff] [blame] | 235 | // Just call the full function with true for the value of |create|. |
| 236 | return OverrideAndCreateIfNeeded(key, path, true); |
| 237 | } |
| 238 | |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 239 | // static |
[email protected] | cb571e75 | 2012-05-09 10:50:10 | [diff] [blame] | 240 | bool PathService::OverrideAndCreateIfNeeded(int key, |
| 241 | const FilePath& path, |
| 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); |
[email protected] | 88563f6 | 2011-03-13 22:13:33 | [diff] [blame] | 245 | DCHECK_GT(key, base::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 |
| 253 | // this to the absolute path because on POSIX, AbsolutePath fails if called |
| 254 | // on a non-existent path. |
| 255 | if (!file_util::PathExists(file_path) && |
| 256 | !file_util::CreateDirectory(file_path)) |
| 257 | return false; |
| 258 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 259 | |
[email protected] | dabdb68 | 2009-10-27 23:31:36 | [diff] [blame] | 260 | // We need to have an absolute path, as extensions and plugins don't like |
[email protected] | 846c3ecea | 2011-12-14 18:47:26 | [diff] [blame] | 261 | // relative paths, and will gladly crash the browser in CHECK()s if they get a |
[email protected] | dabdb68 | 2009-10-27 23:31:36 | [diff] [blame] | 262 | // relative path. |
| 263 | if (!file_util::AbsolutePath(&file_path)) |
| 264 | return false; |
| 265 | |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 266 | base::AutoLock scoped_lock(path_data->lock); |
[email protected] | 34e043b | 2010-09-09 23:49:04 | [diff] [blame] | 267 | |
| 268 | // Clear the cache now. Some of its entries could have depended |
| 269 | // on the value we are overriding, and are now out of sync with reality. |
| 270 | path_data->cache.clear(); |
| 271 | |
[email protected] | 34e043b | 2010-09-09 23:49:04 | [diff] [blame] | 272 | path_data->overrides[key] = file_path; |
| 273 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 274 | return true; |
| 275 | } |
| 276 | |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 277 | // static |
| 278 | bool PathService::RemoveOverride(int key) { |
| 279 | PathData* path_data = GetPathData(); |
| 280 | DCHECK(path_data); |
| 281 | |
| 282 | base::AutoLock scoped_lock(path_data->lock); |
| 283 | |
| 284 | if (path_data->overrides.find(key) == path_data->overrides.end()) |
| 285 | return false; |
| 286 | |
| 287 | // Clear the cache now. Some of its entries could have depended on the value |
| 288 | // we are going to remove, and are now out of sync. |
| 289 | path_data->cache.clear(); |
| 290 | |
| 291 | path_data->overrides.erase(key); |
| 292 | |
| 293 | return true; |
| 294 | } |
| 295 | |
| 296 | // static |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 297 | void PathService::RegisterProvider(ProviderFunc func, int key_start, |
| 298 | int key_end) { |
[email protected] | 1265917f | 2008-08-12 17:33:52 | [diff] [blame] | 299 | PathData* path_data = GetPathData(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 300 | DCHECK(path_data); |
[email protected] | 88563f6 | 2011-03-13 22:13:33 | [diff] [blame] | 301 | DCHECK_GT(key_end, key_start); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 302 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 303 | Provider* p; |
| 304 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 305 | p = new Provider; |
[email protected] | 173cb8a0 | 2008-08-20 15:47:39 | [diff] [blame] | 306 | p->is_static = false; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 307 | p->func = func; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 308 | #ifndef NDEBUG |
| 309 | p->key_start = key_start; |
| 310 | p->key_end = key_end; |
| 311 | #endif |
[email protected] | d6b3af9 | 2012-09-26 19:05:12 | [diff] [blame] | 312 | |
| 313 | base::AutoLock scoped_lock(path_data->lock); |
| 314 | |
| 315 | #ifndef NDEBUG |
| 316 | Provider *iter = path_data->providers; |
| 317 | while (iter) { |
| 318 | DCHECK(key_start >= iter->key_end || key_end <= iter->key_start) << |
| 319 | "path provider collision"; |
| 320 | iter = iter->next; |
| 321 | } |
| 322 | #endif |
| 323 | |
| 324 | p->next = path_data->providers; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 325 | path_data->providers = p; |
| 326 | } |
[email protected] | c5a726b3 | 2013-01-29 00:56:56 | [diff] [blame] | 327 | |
| 328 | // static |
| 329 | void PathService::DisableCache() { |
| 330 | PathData* path_data = GetPathData(); |
| 331 | DCHECK(path_data); |
| 332 | |
| 333 | base::AutoLock scoped_lock(path_data->lock); |
| 334 | path_data->cache.clear(); |
| 335 | path_data->cache_disabled = true; |
| 336 | } |