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