[email protected] | 8e03fec | 2011-03-31 20:34:25 | [diff] [blame] | 1 | // Copyright (c) 2011 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 | |
| 7 | #ifdef 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] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 13 | #include "base/file_path.h" |
[email protected] | 09ad1e62 | 2008-08-07 20:23:09 | [diff] [blame] | 14 | #include "base/file_util.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 | |
| 20 | namespace base { |
[email protected] | 4792a26 | 2008-11-19 16:50:03 | [diff] [blame] | 21 | bool PathProvider(int key, FilePath* result); |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 22 | #if defined(OS_WIN) |
[email protected] | 4792a26 | 2008-11-19 16:50:03 | [diff] [blame] | 23 | bool PathProviderWin(int key, FilePath* result); |
[email protected] | 4c0040c | 2008-08-15 01:04:11 | [diff] [blame] | 24 | #elif defined(OS_MACOSX) |
[email protected] | 4792a26 | 2008-11-19 16:50:03 | [diff] [blame] | 25 | bool PathProviderMac(int key, FilePath* result); |
[email protected] | f7d6997 | 2011-06-21 22:34:50 | [diff] [blame] | 26 | #elif defined(OS_ANDROID) |
| 27 | bool PathProviderAndroid(int key, FilePath* result); |
[email protected] | 5d1937bb | 2009-11-21 01:29:00 | [diff] [blame] | 28 | #elif defined(OS_POSIX) |
| 29 | bool PathProviderPosix(int key, FilePath* result); |
[email protected] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 30 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | namespace { |
| 34 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 35 | typedef base::hash_map<int, FilePath> PathMap; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 36 | |
| 37 | // We keep a linked list of providers. In a debug build we ensure that no two |
| 38 | // providers claim overlapping keys. |
| 39 | struct Provider { |
| 40 | PathService::ProviderFunc func; |
| 41 | struct Provider* next; |
| 42 | #ifndef NDEBUG |
| 43 | int key_start; |
| 44 | int key_end; |
| 45 | #endif |
[email protected] | 173cb8a0 | 2008-08-20 15:47:39 | [diff] [blame] | 46 | bool is_static; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | static Provider base_provider = { |
| 50 | base::PathProvider, |
| 51 | NULL, |
| 52 | #ifndef NDEBUG |
| 53 | base::PATH_START, |
[email protected] | 173cb8a0 | 2008-08-20 15:47:39 | [diff] [blame] | 54 | base::PATH_END, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 55 | #endif |
[email protected] | 173cb8a0 | 2008-08-20 15:47:39 | [diff] [blame] | 56 | true |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 57 | }; |
| 58 | |
[email protected] | 5d1937bb | 2009-11-21 01:29:00 | [diff] [blame] | 59 | #if defined(OS_WIN) |
[email protected] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 60 | static Provider base_provider_win = { |
| 61 | base::PathProviderWin, |
| 62 | &base_provider, |
| 63 | #ifndef NDEBUG |
| 64 | base::PATH_WIN_START, |
[email protected] | 173cb8a0 | 2008-08-20 15:47:39 | [diff] [blame] | 65 | base::PATH_WIN_END, |
[email protected] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 66 | #endif |
[email protected] | 173cb8a0 | 2008-08-20 15:47:39 | [diff] [blame] | 67 | true |
[email protected] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 68 | }; |
| 69 | #endif |
| 70 | |
[email protected] | 5d1937bb | 2009-11-21 01:29:00 | [diff] [blame] | 71 | #if defined(OS_MACOSX) |
[email protected] | 173cb8a0 | 2008-08-20 15:47:39 | [diff] [blame] | 72 | static Provider base_provider_mac = { |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 73 | base::PathProviderMac, |
| 74 | &base_provider, |
| 75 | #ifndef NDEBUG |
| 76 | base::PATH_MAC_START, |
[email protected] | 173cb8a0 | 2008-08-20 15:47:39 | [diff] [blame] | 77 | base::PATH_MAC_END, |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 78 | #endif |
[email protected] | 173cb8a0 | 2008-08-20 15:47:39 | [diff] [blame] | 79 | true |
| 80 | }; |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 81 | #endif |
[email protected] | 4c0040c | 2008-08-15 01:04:11 | [diff] [blame] | 82 | |
[email protected] | f7d6997 | 2011-06-21 22:34:50 | [diff] [blame] | 83 | #if defined(OS_ANDROID) |
| 84 | static Provider base_provider_android = { |
| 85 | base::PathProviderAndroid, |
| 86 | &base_provider, |
| 87 | #ifndef NDEBUG |
| 88 | 0, |
| 89 | 0, |
| 90 | #endif |
| 91 | true |
| 92 | }; |
| 93 | #endif |
| 94 | |
| 95 | #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
[email protected] | 5d1937bb | 2009-11-21 01:29:00 | [diff] [blame] | 96 | static Provider base_provider_posix = { |
| 97 | base::PathProviderPosix, |
[email protected] | 4c0040c | 2008-08-15 01:04:11 | [diff] [blame] | 98 | &base_provider, |
| 99 | #ifndef NDEBUG |
[email protected] | 5d1937bb | 2009-11-21 01:29:00 | [diff] [blame] | 100 | 0, |
| 101 | 0, |
[email protected] | 4c0040c | 2008-08-15 01:04:11 | [diff] [blame] | 102 | #endif |
[email protected] | 173cb8a0 | 2008-08-20 15:47:39 | [diff] [blame] | 103 | true |
| 104 | }; |
[email protected] | 4c0040c | 2008-08-15 01:04:11 | [diff] [blame] | 105 | #endif |
| 106 | |
| 107 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 108 | struct PathData { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 109 | base::Lock lock; |
| 110 | PathMap cache; // Cache mappings from path key to path value. |
| 111 | PathMap overrides; // Track path overrides. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 112 | Provider* providers; // Linked list of path service providers. |
| 113 | |
[email protected] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 114 | PathData() { |
| 115 | #if defined(OS_WIN) |
| 116 | providers = &base_provider_win; |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 117 | #elif defined(OS_MACOSX) |
| 118 | providers = &base_provider_mac; |
[email protected] | f7d6997 | 2011-06-21 22:34:50 | [diff] [blame] | 119 | #elif defined(OS_ANDROID) |
| 120 | providers = &base_provider_android; |
[email protected] | 5d1937bb | 2009-11-21 01:29:00 | [diff] [blame] | 121 | #elif defined(OS_POSIX) |
| 122 | providers = &base_provider_posix; |
[email protected] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 123 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 124 | } |
[email protected] | 173cb8a0 | 2008-08-20 15:47:39 | [diff] [blame] | 125 | |
| 126 | ~PathData() { |
| 127 | Provider* p = providers; |
| 128 | while (p) { |
| 129 | Provider* next = p->next; |
| 130 | if (!p->is_static) |
| 131 | delete p; |
| 132 | p = next; |
| 133 | } |
| 134 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 135 | }; |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 136 | |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 137 | static base::LazyInstance<PathData> g_path_data = LAZY_INSTANCE_INITIALIZER; |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 138 | |
[email protected] | 1265917f | 2008-08-12 17:33:52 | [diff] [blame] | 139 | static PathData* GetPathData() { |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 140 | return g_path_data.Pointer(); |
[email protected] | 1265917f | 2008-08-12 17:33:52 | [diff] [blame] | 141 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 142 | |
| 143 | } // namespace |
| 144 | |
[email protected] | 6723f83 | 2008-08-11 15:38:27 | [diff] [blame] | 145 | |
[email protected] | 1265917f | 2008-08-12 17:33:52 | [diff] [blame] | 146 | // static |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 147 | bool PathService::GetFromCache(int key, FilePath* result) { |
[email protected] | 1265917f | 2008-08-12 17:33:52 | [diff] [blame] | 148 | PathData* path_data = GetPathData(); |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 149 | base::AutoLock scoped_lock(path_data->lock); |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 150 | |
[email protected] | 6723f83 | 2008-08-11 15:38:27 | [diff] [blame] | 151 | // check for a cached version |
| 152 | PathMap::const_iterator it = path_data->cache.find(key); |
| 153 | if (it != path_data->cache.end()) { |
| 154 | *result = it->second; |
| 155 | return true; |
| 156 | } |
| 157 | return false; |
| 158 | } |
| 159 | |
[email protected] | 1265917f | 2008-08-12 17:33:52 | [diff] [blame] | 160 | // static |
[email protected] | 34e043b | 2010-09-09 23:49:04 | [diff] [blame] | 161 | bool PathService::GetFromOverrides(int key, FilePath* result) { |
| 162 | PathData* path_data = GetPathData(); |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 163 | base::AutoLock scoped_lock(path_data->lock); |
[email protected] | 34e043b | 2010-09-09 23:49:04 | [diff] [blame] | 164 | |
[email protected] | 846c3ecea | 2011-12-14 18:47:26 | [diff] [blame^] | 165 | // check for an overridden version. |
[email protected] | 34e043b | 2010-09-09 23:49:04 | [diff] [blame] | 166 | PathMap::const_iterator it = path_data->overrides.find(key); |
| 167 | if (it != path_data->overrides.end()) { |
| 168 | *result = it->second; |
| 169 | return true; |
| 170 | } |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | // static |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 175 | void PathService::AddToCache(int key, const FilePath& path) { |
[email protected] | 1265917f | 2008-08-12 17:33:52 | [diff] [blame] | 176 | PathData* path_data = GetPathData(); |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 177 | base::AutoLock scoped_lock(path_data->lock); |
[email protected] | 6723f83 | 2008-08-11 15:38:27 | [diff] [blame] | 178 | // Save the computed path in our cache. |
| 179 | path_data->cache[key] = path; |
| 180 | } |
| 181 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 182 | // TODO(brettw): this function does not handle long paths (filename > MAX_PATH) |
| 183 | // characters). This isn't supported very well by Windows right now, so it is |
| 184 | // moot, but we should keep this in mind for the future. |
| 185 | // static |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 186 | bool PathService::Get(int key, FilePath* result) { |
[email protected] | 1265917f | 2008-08-12 17:33:52 | [diff] [blame] | 187 | PathData* path_data = GetPathData(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 188 | DCHECK(path_data); |
| 189 | DCHECK(result); |
[email protected] | 8e03fec | 2011-03-31 20:34:25 | [diff] [blame] | 190 | DCHECK_GE(key, base::DIR_CURRENT); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 191 | |
| 192 | // special case the current directory because it can never be cached |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 193 | if (key == base::DIR_CURRENT) |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 194 | return file_util::GetCurrentDirectory(result); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 195 | |
[email protected] | aa443bbd | 2010-08-19 23:20:09 | [diff] [blame] | 196 | if (GetFromCache(key, result)) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 197 | return true; |
[email protected] | c1a9f8d4 | 2009-02-28 01:49:55 | [diff] [blame] | 198 | |
[email protected] | 34e043b | 2010-09-09 23:49:04 | [diff] [blame] | 199 | if (GetFromOverrides(key, result)) |
| 200 | return true; |
| 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 | |
| 204 | // search providers for the requested path |
[email protected] | 6723f83 | 2008-08-11 15:38:27 | [diff] [blame] | 205 | // NOTE: it should be safe to iterate here without the lock |
| 206 | // since RegisterProvider always prepends. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 207 | Provider* provider = path_data->providers; |
| 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] | 6723f83 | 2008-08-11 15:38:27 | [diff] [blame] | 218 | AddToCache(key, path); |
[email protected] | c1a9f8d4 | 2009-02-28 01:49:55 | [diff] [blame] | 219 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 220 | *result = path; |
| 221 | return true; |
| 222 | } |
| 223 | |
[email protected] | eca6a4f | 2009-06-25 17:29:09 | [diff] [blame] | 224 | bool PathService::Override(int key, const FilePath& path) { |
[email protected] | 1265917f | 2008-08-12 17:33:52 | [diff] [blame] | 225 | PathData* path_data = GetPathData(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 226 | DCHECK(path_data); |
[email protected] | 88563f6 | 2011-03-13 22:13:33 | [diff] [blame] | 227 | DCHECK_GT(key, base::DIR_CURRENT) << "invalid path key"; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 228 | |
[email protected] | eca6a4f | 2009-06-25 17:29:09 | [diff] [blame] | 229 | FilePath file_path = path; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 230 | |
[email protected] | dabdb68 | 2009-10-27 23:31:36 | [diff] [blame] | 231 | // Make sure the directory exists. We need to do this before we translate |
| 232 | // this to the absolute path because on POSIX, AbsolutePath fails if called |
[email protected] | 846c3ecea | 2011-12-14 18:47:26 | [diff] [blame^] | 233 | // on a non-existent path. |
[email protected] | eca6a4f | 2009-06-25 17:29:09 | [diff] [blame] | 234 | if (!file_util::PathExists(file_path) && |
| 235 | !file_util::CreateDirectory(file_path)) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 236 | return false; |
| 237 | |
[email protected] | dabdb68 | 2009-10-27 23:31:36 | [diff] [blame] | 238 | // 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^] | 239 | // 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] | 240 | // relative path. |
| 241 | if (!file_util::AbsolutePath(&file_path)) |
| 242 | return false; |
| 243 | |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 244 | base::AutoLock scoped_lock(path_data->lock); |
[email protected] | 34e043b | 2010-09-09 23:49:04 | [diff] [blame] | 245 | |
| 246 | // Clear the cache now. Some of its entries could have depended |
| 247 | // on the value we are overriding, and are now out of sync with reality. |
| 248 | path_data->cache.clear(); |
| 249 | |
[email protected] | eca6a4f | 2009-06-25 17:29:09 | [diff] [blame] | 250 | path_data->cache[key] = file_path; |
[email protected] | 34e043b | 2010-09-09 23:49:04 | [diff] [blame] | 251 | path_data->overrides[key] = file_path; |
| 252 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 253 | return true; |
| 254 | } |
| 255 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 256 | void PathService::RegisterProvider(ProviderFunc func, int key_start, |
| 257 | int key_end) { |
[email protected] | 1265917f | 2008-08-12 17:33:52 | [diff] [blame] | 258 | PathData* path_data = GetPathData(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 259 | DCHECK(path_data); |
[email protected] | 88563f6 | 2011-03-13 22:13:33 | [diff] [blame] | 260 | DCHECK_GT(key_end, key_start); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 261 | |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 262 | base::AutoLock scoped_lock(path_data->lock); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 263 | |
| 264 | Provider* p; |
| 265 | |
| 266 | #ifndef NDEBUG |
| 267 | p = path_data->providers; |
| 268 | while (p) { |
| 269 | DCHECK(key_start >= p->key_end || key_end <= p->key_start) << |
| 270 | "path provider collision"; |
| 271 | p = p->next; |
| 272 | } |
| 273 | #endif |
| 274 | |
| 275 | p = new Provider; |
[email protected] | 173cb8a0 | 2008-08-20 15:47:39 | [diff] [blame] | 276 | p->is_static = false; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 277 | p->func = func; |
| 278 | p->next = path_data->providers; |
| 279 | #ifndef NDEBUG |
| 280 | p->key_start = key_start; |
| 281 | p->key_end = key_end; |
| 282 | #endif |
| 283 | path_data->providers = p; |
| 284 | } |