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" |
[email protected] | 355cc274 | 2008-08-06 16:01:25 | [diff] [blame] | 19 | #include "base/string_util.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 20 | |
| 21 | namespace base { |
[email protected] | 4792a26 | 2008-11-19 16:50:03 | [diff] [blame^] | 22 | bool PathProvider(int key, FilePath* result); |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 23 | #if defined(OS_WIN) |
[email protected] | 4792a26 | 2008-11-19 16:50:03 | [diff] [blame^] | 24 | bool PathProviderWin(int key, FilePath* result); |
[email protected] | 4c0040c | 2008-08-15 01:04:11 | [diff] [blame] | 25 | #elif defined(OS_MACOSX) |
[email protected] | 4792a26 | 2008-11-19 16:50:03 | [diff] [blame^] | 26 | bool PathProviderMac(int key, FilePath* result); |
[email protected] | 4c0040c | 2008-08-15 01:04:11 | [diff] [blame] | 27 | #elif defined(OS_LINUX) |
[email protected] | 4792a26 | 2008-11-19 16:50:03 | [diff] [blame^] | 28 | bool PathProviderLinux(int key, FilePath* result); |
[email protected] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 29 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | namespace { |
| 33 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 34 | typedef base::hash_map<int, FilePath> PathMap; |
[email protected] | 355cc274 | 2008-08-06 16:01:25 | [diff] [blame] | 35 | typedef base::hash_set<int> PathSet; |
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] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 59 | #ifdef OS_WIN |
| 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] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 71 | #ifdef 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 | |
| 83 | #if defined(OS_LINUX) |
| 84 | static Provider base_provider_linux = { |
| 85 | base::PathProviderLinux, |
| 86 | &base_provider, |
| 87 | #ifndef NDEBUG |
| 88 | base::PATH_LINUX_START, |
[email protected] | 173cb8a0 | 2008-08-20 15:47:39 | [diff] [blame] | 89 | base::PATH_LINUX_END, |
[email protected] | 4c0040c | 2008-08-15 01:04:11 | [diff] [blame] | 90 | #endif |
[email protected] | 173cb8a0 | 2008-08-20 15:47:39 | [diff] [blame] | 91 | true |
| 92 | }; |
[email protected] | 4c0040c | 2008-08-15 01:04:11 | [diff] [blame] | 93 | #endif |
| 94 | |
| 95 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 96 | struct PathData { |
| 97 | Lock lock; |
| 98 | PathMap cache; // Track mappings from path key to path value. |
| 99 | PathSet overrides; // Track whether a path has been overridden. |
| 100 | Provider* providers; // Linked list of path service providers. |
| 101 | |
[email protected] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 102 | PathData() { |
| 103 | #if defined(OS_WIN) |
| 104 | providers = &base_provider_win; |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 105 | #elif defined(OS_MACOSX) |
| 106 | providers = &base_provider_mac; |
| 107 | #elif defined(OS_LINUX) |
[email protected] | 4c0040c | 2008-08-15 01:04:11 | [diff] [blame] | 108 | providers = &base_provider_linux; |
[email protected] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 109 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 110 | } |
[email protected] | 173cb8a0 | 2008-08-20 15:47:39 | [diff] [blame] | 111 | |
| 112 | ~PathData() { |
| 113 | Provider* p = providers; |
| 114 | while (p) { |
| 115 | Provider* next = p->next; |
| 116 | if (!p->is_static) |
| 117 | delete p; |
| 118 | p = next; |
| 119 | } |
| 120 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 121 | }; |
[email protected] | 1265917f | 2008-08-12 17:33:52 | [diff] [blame] | 122 | |
| 123 | static PathData* GetPathData() { |
| 124 | return Singleton<PathData>::get(); |
| 125 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 126 | |
| 127 | } // namespace |
| 128 | |
[email protected] | 6723f83 | 2008-08-11 15:38:27 | [diff] [blame] | 129 | |
[email protected] | 1265917f | 2008-08-12 17:33:52 | [diff] [blame] | 130 | // static |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 131 | bool PathService::GetFromCache(int key, FilePath* result) { |
[email protected] | 1265917f | 2008-08-12 17:33:52 | [diff] [blame] | 132 | PathData* path_data = GetPathData(); |
[email protected] | 6723f83 | 2008-08-11 15:38:27 | [diff] [blame] | 133 | AutoLock scoped_lock(path_data->lock); |
| 134 | |
| 135 | // check for a cached version |
| 136 | PathMap::const_iterator it = path_data->cache.find(key); |
| 137 | if (it != path_data->cache.end()) { |
| 138 | *result = it->second; |
| 139 | return true; |
| 140 | } |
| 141 | return false; |
| 142 | } |
| 143 | |
[email protected] | 1265917f | 2008-08-12 17:33:52 | [diff] [blame] | 144 | // static |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 145 | void PathService::AddToCache(int key, const FilePath& path) { |
[email protected] | 1265917f | 2008-08-12 17:33:52 | [diff] [blame] | 146 | PathData* path_data = GetPathData(); |
[email protected] | 6723f83 | 2008-08-11 15:38:27 | [diff] [blame] | 147 | AutoLock scoped_lock(path_data->lock); |
| 148 | // Save the computed path in our cache. |
| 149 | path_data->cache[key] = path; |
| 150 | } |
| 151 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 152 | // TODO(brettw): this function does not handle long paths (filename > MAX_PATH) |
| 153 | // characters). This isn't supported very well by Windows right now, so it is |
| 154 | // moot, but we should keep this in mind for the future. |
| 155 | // static |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 156 | bool PathService::Get(int key, FilePath* result) { |
[email protected] | 1265917f | 2008-08-12 17:33:52 | [diff] [blame] | 157 | PathData* path_data = GetPathData(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 158 | DCHECK(path_data); |
| 159 | DCHECK(result); |
| 160 | DCHECK(key >= base::DIR_CURRENT); |
| 161 | |
| 162 | // special case the current directory because it can never be cached |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 163 | if (key == base::DIR_CURRENT) |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 164 | return file_util::GetCurrentDirectory(result); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 165 | |
[email protected] | 6723f83 | 2008-08-11 15:38:27 | [diff] [blame] | 166 | if (GetFromCache(key, result)) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 167 | return true; |
[email protected] | 6723f83 | 2008-08-11 15:38:27 | [diff] [blame] | 168 | |
[email protected] | 4792a26 | 2008-11-19 16:50:03 | [diff] [blame^] | 169 | FilePath path; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 170 | |
| 171 | // search providers for the requested path |
[email protected] | 6723f83 | 2008-08-11 15:38:27 | [diff] [blame] | 172 | // NOTE: it should be safe to iterate here without the lock |
| 173 | // since RegisterProvider always prepends. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 174 | Provider* provider = path_data->providers; |
| 175 | while (provider) { |
[email protected] | 4792a26 | 2008-11-19 16:50:03 | [diff] [blame^] | 176 | if (provider->func(key, &path)) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 177 | break; |
[email protected] | 4792a26 | 2008-11-19 16:50:03 | [diff] [blame^] | 178 | DCHECK(path.value().empty()) << "provider should not have modified path"; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 179 | provider = provider->next; |
| 180 | } |
| 181 | |
[email protected] | 4792a26 | 2008-11-19 16:50:03 | [diff] [blame^] | 182 | if (path.value().empty()) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 183 | return false; |
| 184 | |
[email protected] | 6723f83 | 2008-08-11 15:38:27 | [diff] [blame] | 185 | AddToCache(key, path); |
| 186 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 187 | *result = path; |
| 188 | return true; |
| 189 | } |
| 190 | |
| 191 | // static |
| 192 | bool PathService::Get(int key, std::wstring* result) { |
| 193 | // Deprecated compatibility function. |
| 194 | FilePath path; |
| 195 | if (!Get(key, &path)) |
| 196 | return false; |
| 197 | *result = path.ToWStringHack(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 198 | return true; |
| 199 | } |
| 200 | |
| 201 | bool PathService::IsOverridden(int key) { |
[email protected] | 1265917f | 2008-08-12 17:33:52 | [diff] [blame] | 202 | PathData* path_data = GetPathData(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 203 | DCHECK(path_data); |
| 204 | |
| 205 | AutoLock scoped_lock(path_data->lock); |
| 206 | return path_data->overrides.find(key) != path_data->overrides.end(); |
| 207 | } |
| 208 | |
| 209 | bool PathService::Override(int key, const std::wstring& path) { |
[email protected] | 1265917f | 2008-08-12 17:33:52 | [diff] [blame] | 210 | PathData* path_data = GetPathData(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 211 | DCHECK(path_data); |
| 212 | DCHECK(key > base::DIR_CURRENT) << "invalid path key"; |
| 213 | |
[email protected] | 1265917f | 2008-08-12 17:33:52 | [diff] [blame] | 214 | std::wstring file_path = path; |
| 215 | if (!file_util::AbsolutePath(&file_path)) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 216 | return false; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 217 | |
| 218 | // make sure the directory exists: |
[email protected] | 806b9c6 | 2008-09-11 16:09:11 | [diff] [blame] | 219 | if (!file_util::CreateDirectory(file_path)) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 220 | return false; |
| 221 | |
| 222 | file_util::TrimTrailingSeparator(&file_path); |
| 223 | |
| 224 | AutoLock scoped_lock(path_data->lock); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 225 | path_data->cache[key] = FilePath::FromWStringHack(file_path); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 226 | path_data->overrides.insert(key); |
| 227 | return true; |
| 228 | } |
| 229 | |
| 230 | bool PathService::SetCurrentDirectory(const std::wstring& current_directory) { |
[email protected] | 1010f7d | 2008-08-06 16:29:44 | [diff] [blame] | 231 | return file_util::SetCurrentDirectory(current_directory); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | void PathService::RegisterProvider(ProviderFunc func, int key_start, |
| 235 | int key_end) { |
[email protected] | 1265917f | 2008-08-12 17:33:52 | [diff] [blame] | 236 | PathData* path_data = GetPathData(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 237 | DCHECK(path_data); |
| 238 | DCHECK(key_end > key_start); |
| 239 | |
| 240 | AutoLock scoped_lock(path_data->lock); |
| 241 | |
| 242 | Provider* p; |
| 243 | |
| 244 | #ifndef NDEBUG |
| 245 | p = path_data->providers; |
| 246 | while (p) { |
| 247 | DCHECK(key_start >= p->key_end || key_end <= p->key_start) << |
| 248 | "path provider collision"; |
| 249 | p = p->next; |
| 250 | } |
| 251 | #endif |
| 252 | |
| 253 | p = new Provider; |
[email protected] | 173cb8a0 | 2008-08-20 15:47:39 | [diff] [blame] | 254 | p->is_static = false; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 255 | p->func = func; |
| 256 | p->next = path_data->providers; |
| 257 | #ifndef NDEBUG |
| 258 | p->key_start = key_start; |
| 259 | p->key_end = key_end; |
| 260 | #endif |
| 261 | path_data->providers = p; |
| 262 | } |