| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/path_service.h" | 5 #include "base/path_service.h" |
| 6 | 6 |
| 7 #ifdef OS_WIN | 7 #ifdef OS_WIN |
| 8 #include <windows.h> | 8 #include <windows.h> |
| 9 #include <shellapi.h> | 9 #include <shellapi.h> |
| 10 #include <shlobj.h> | 10 #include <shlobj.h> |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 #elif defined(OS_MACOSX) | 24 #elif defined(OS_MACOSX) |
| 25 bool PathProviderMac(int key, FilePath* result); | 25 bool PathProviderMac(int key, FilePath* result); |
| 26 #elif defined(OS_POSIX) | 26 #elif defined(OS_POSIX) |
| 27 bool PathProviderPosix(int key, FilePath* result); | 27 bool PathProviderPosix(int key, FilePath* result); |
| 28 #endif | 28 #endif |
| 29 } | 29 } |
| 30 | 30 |
| 31 namespace { | 31 namespace { |
| 32 | 32 |
| 33 typedef base::hash_map<int, FilePath> PathMap; | 33 typedef base::hash_map<int, FilePath> PathMap; |
| 34 typedef base::hash_set<int> PathSet; |
| 34 | 35 |
| 35 // We keep a linked list of providers. In a debug build we ensure that no two | 36 // We keep a linked list of providers. In a debug build we ensure that no two |
| 36 // providers claim overlapping keys. | 37 // providers claim overlapping keys. |
| 37 struct Provider { | 38 struct Provider { |
| 38 PathService::ProviderFunc func; | 39 PathService::ProviderFunc func; |
| 39 struct Provider* next; | 40 struct Provider* next; |
| 40 #ifndef NDEBUG | 41 #ifndef NDEBUG |
| 41 int key_start; | 42 int key_start; |
| 42 int key_end; | 43 int key_end; |
| 43 #endif | 44 #endif |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 0, | 87 0, |
| 87 0, | 88 0, |
| 88 #endif | 89 #endif |
| 89 true | 90 true |
| 90 }; | 91 }; |
| 91 #endif | 92 #endif |
| 92 | 93 |
| 93 | 94 |
| 94 struct PathData { | 95 struct PathData { |
| 95 Lock lock; | 96 Lock lock; |
| 96 PathMap cache; // Cache mappings from path key to path value. | 97 PathMap cache; // Track mappings from path key to path value. |
| 97 PathMap overrides; // Track path overrides. | 98 PathSet overrides; // Track whether a path has been overridden. |
| 98 Provider* providers; // Linked list of path service providers. | 99 Provider* providers; // Linked list of path service providers. |
| 99 | 100 |
| 100 PathData() { | 101 PathData() { |
| 101 #if defined(OS_WIN) | 102 #if defined(OS_WIN) |
| 102 providers = &base_provider_win; | 103 providers = &base_provider_win; |
| 103 #elif defined(OS_MACOSX) | 104 #elif defined(OS_MACOSX) |
| 104 providers = &base_provider_mac; | 105 providers = &base_provider_mac; |
| 105 #elif defined(OS_POSIX) | 106 #elif defined(OS_POSIX) |
| 106 providers = &base_provider_posix; | 107 providers = &base_provider_posix; |
| 107 #endif | 108 #endif |
| (...skipping 25 matching lines...) Expand all Loading... |
| 133 // check for a cached version | 134 // check for a cached version |
| 134 PathMap::const_iterator it = path_data->cache.find(key); | 135 PathMap::const_iterator it = path_data->cache.find(key); |
| 135 if (it != path_data->cache.end()) { | 136 if (it != path_data->cache.end()) { |
| 136 *result = it->second; | 137 *result = it->second; |
| 137 return true; | 138 return true; |
| 138 } | 139 } |
| 139 return false; | 140 return false; |
| 140 } | 141 } |
| 141 | 142 |
| 142 // static | 143 // static |
| 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 | |
| 157 void PathService::AddToCache(int key, const FilePath& path) { | 144 void PathService::AddToCache(int key, const FilePath& path) { |
| 158 PathData* path_data = GetPathData(); | 145 PathData* path_data = GetPathData(); |
| 159 AutoLock scoped_lock(path_data->lock); | 146 AutoLock scoped_lock(path_data->lock); |
| 160 // Save the computed path in our cache. | 147 // Save the computed path in our cache. |
| 161 path_data->cache[key] = path; | 148 path_data->cache[key] = path; |
| 162 } | 149 } |
| 163 | 150 |
| 164 // TODO(brettw): this function does not handle long paths (filename > MAX_PATH) | 151 // 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 | 152 // 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. | 153 // moot, but we should keep this in mind for the future. |
| 167 // static | 154 // static |
| 168 bool PathService::Get(int key, FilePath* result) { | 155 bool PathService::Get(int key, FilePath* result) { |
| 169 PathData* path_data = GetPathData(); | 156 PathData* path_data = GetPathData(); |
| 170 DCHECK(path_data); | 157 DCHECK(path_data); |
| 171 DCHECK(result); | 158 DCHECK(result); |
| 172 DCHECK(key >= base::DIR_CURRENT); | 159 DCHECK(key >= base::DIR_CURRENT); |
| 173 | 160 |
| 174 // special case the current directory because it can never be cached | 161 // special case the current directory because it can never be cached |
| 175 if (key == base::DIR_CURRENT) | 162 if (key == base::DIR_CURRENT) |
| 176 return file_util::GetCurrentDirectory(result); | 163 return file_util::GetCurrentDirectory(result); |
| 177 | 164 |
| 178 if (GetFromCache(key, result)) { | 165 if (GetFromCache(key, result)) |
| 179 LOG(ERROR) << "Key: " << key << " returning " << result->value() << " from c
ache."; | |
| 180 return true; | 166 return true; |
| 181 } | |
| 182 | |
| 183 if (GetFromOverrides(key, result)) { | |
| 184 LOG(ERROR) << "Key: " << key << " returning " << result->value() << " from o
verrides."; | |
| 185 return true; | |
| 186 } | |
| 187 | 167 |
| 188 FilePath path; | 168 FilePath path; |
| 189 | 169 |
| 190 // search providers for the requested path | 170 // search providers for the requested path |
| 191 // NOTE: it should be safe to iterate here without the lock | 171 // NOTE: it should be safe to iterate here without the lock |
| 192 // since RegisterProvider always prepends. | 172 // since RegisterProvider always prepends. |
| 193 Provider* provider = path_data->providers; | 173 Provider* provider = path_data->providers; |
| 194 while (provider) { | 174 while (provider) { |
| 195 if (provider->func(key, &path)) | 175 if (provider->func(key, &path)) |
| 196 break; | 176 break; |
| 197 DCHECK(path.empty()) << "provider should not have modified path"; | 177 DCHECK(path.empty()) << "provider should not have modified path"; |
| 198 provider = provider->next; | 178 provider = provider->next; |
| 199 } | 179 } |
| 200 | 180 |
| 201 if (path.empty()) | 181 if (path.empty()) |
| 202 return false; | 182 return false; |
| 203 | 183 |
| 204 AddToCache(key, path); | 184 AddToCache(key, path); |
| 205 | 185 |
| 206 *result = path; | 186 *result = path; |
| 207 LOG(ERROR) << "Key: " << key << " returning " << result->value() << "."; | |
| 208 return true; | 187 return true; |
| 209 } | 188 } |
| 210 | 189 |
| 211 #if defined(OS_WIN) | 190 #if defined(OS_WIN) |
| 212 // static | 191 // static |
| 213 bool PathService::Get(int key, std::wstring* result) { | 192 bool PathService::Get(int key, std::wstring* result) { |
| 214 // Deprecated compatibility function. | 193 // Deprecated compatibility function. |
| 215 FilePath path; | 194 FilePath path; |
| 216 if (!Get(key, &path)) | 195 if (!Get(key, &path)) |
| 217 return false; | 196 return false; |
| 218 *result = path.ToWStringHack(); | 197 *result = path.ToWStringHack(); |
| 219 return true; | 198 return true; |
| 220 } | 199 } |
| 221 #endif | 200 #endif |
| 222 | 201 |
| 202 bool PathService::IsOverridden(int key) { |
| 203 PathData* path_data = GetPathData(); |
| 204 DCHECK(path_data); |
| 205 |
| 206 AutoLock scoped_lock(path_data->lock); |
| 207 return path_data->overrides.find(key) != path_data->overrides.end(); |
| 208 } |
| 209 |
| 223 bool PathService::Override(int key, const FilePath& path) { | 210 bool PathService::Override(int key, const FilePath& path) { |
| 224 PathData* path_data = GetPathData(); | 211 PathData* path_data = GetPathData(); |
| 225 DCHECK(path_data); | 212 DCHECK(path_data); |
| 226 DCHECK(key > base::DIR_CURRENT) << "invalid path key"; | 213 DCHECK(key > base::DIR_CURRENT) << "invalid path key"; |
| 227 | 214 |
| 228 FilePath file_path = path; | 215 FilePath file_path = path; |
| 229 | 216 |
| 230 // Make sure the directory exists. We need to do this before we translate | 217 // Make sure the directory exists. We need to do this before we translate |
| 231 // this to the absolute path because on POSIX, AbsolutePath fails if called | 218 // this to the absolute path because on POSIX, AbsolutePath fails if called |
| 232 // on a non-existant path. | 219 // on a non-existant path. |
| 233 if (!file_util::PathExists(file_path) && | 220 if (!file_util::PathExists(file_path) && |
| 234 !file_util::CreateDirectory(file_path)) | 221 !file_util::CreateDirectory(file_path)) |
| 235 return false; | 222 return false; |
| 236 | 223 |
| 237 // We need to have an absolute path, as extensions and plugins don't like | 224 // We need to have an absolute path, as extensions and plugins don't like |
| 238 // relative paths, and will glady crash the browser in CHECK()s if they get a | 225 // relative paths, and will glady crash the browser in CHECK()s if they get a |
| 239 // relative path. | 226 // relative path. |
| 240 if (!file_util::AbsolutePath(&file_path)) | 227 if (!file_util::AbsolutePath(&file_path)) |
| 241 return false; | 228 return false; |
| 242 | 229 |
| 243 AutoLock scoped_lock(path_data->lock); | 230 AutoLock scoped_lock(path_data->lock); |
| 244 | |
| 245 // Clear the cache now. Some of its entries could have depended | |
| 246 // on the value we are overriding, and are now out of sync with reality. | |
| 247 path_data->cache.clear(); | |
| 248 | |
| 249 path_data->cache[key] = file_path; | 231 path_data->cache[key] = file_path; |
| 250 path_data->overrides[key] = file_path; | 232 path_data->overrides.insert(key); |
| 251 | |
| 252 return true; | 233 return true; |
| 253 } | 234 } |
| 254 | 235 |
| 255 void PathService::RegisterProvider(ProviderFunc func, int key_start, | 236 void PathService::RegisterProvider(ProviderFunc func, int key_start, |
| 256 int key_end) { | 237 int key_end) { |
| 257 PathData* path_data = GetPathData(); | 238 PathData* path_data = GetPathData(); |
| 258 DCHECK(path_data); | 239 DCHECK(path_data); |
| 259 DCHECK(key_end > key_start); | 240 DCHECK(key_end > key_start); |
| 260 | 241 |
| 261 AutoLock scoped_lock(path_data->lock); | 242 AutoLock scoped_lock(path_data->lock); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 274 p = new Provider; | 255 p = new Provider; |
| 275 p->is_static = false; | 256 p->is_static = false; |
| 276 p->func = func; | 257 p->func = func; |
| 277 p->next = path_data->providers; | 258 p->next = path_data->providers; |
| 278 #ifndef NDEBUG | 259 #ifndef NDEBUG |
| 279 p->key_start = key_start; | 260 p->key_start = key_start; |
| 280 p->key_end = key_end; | 261 p->key_end = key_end; |
| 281 #endif | 262 #endif |
| 282 path_data->providers = p; | 263 path_data->providers = p; |
| 283 } | 264 } |
| OLD | NEW |