| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #if defined(OS_WIN) | 7 #if defined(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> |
| 11 #endif | 11 #endif |
| 12 | 12 |
| 13 #include "base/containers/hash_tables.h" | 13 #include "base/containers/hash_tables.h" |
| 14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 15 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
| 16 #include "base/lazy_instance.h" | 16 #include "base/lazy_instance.h" |
| 17 #include "base/logging.h" | 17 #include "base/logging.h" |
| 18 #include "base/synchronization/lock.h" | 18 #include "base/synchronization/lock.h" |
| 19 | 19 |
| 20 using base::FilePath; | 20 namespace base { |
| 21 using base::MakeAbsoluteFilePath; | |
| 22 | 21 |
| 23 namespace base { | 22 bool PathProvider(int key, FilePath* result); |
| 24 bool PathProvider(int key, FilePath* result); | 23 |
| 25 #if defined(OS_WIN) | 24 #if defined(OS_WIN) |
| 26 bool PathProviderWin(int key, FilePath* result); | 25 bool PathProviderWin(int key, FilePath* result); |
| 27 #elif defined(OS_MACOSX) | 26 #elif defined(OS_MACOSX) |
| 28 bool PathProviderMac(int key, FilePath* result); | 27 bool PathProviderMac(int key, FilePath* result); |
| 29 #elif defined(OS_ANDROID) | 28 #elif defined(OS_ANDROID) |
| 30 bool PathProviderAndroid(int key, FilePath* result); | 29 bool PathProviderAndroid(int key, FilePath* result); |
| 31 #elif defined(OS_POSIX) | 30 #elif defined(OS_POSIX) |
| 32 // PathProviderPosix is the default path provider on POSIX OSes other than | 31 // PathProviderPosix is the default path provider on POSIX OSes other than |
| 33 // Mac and Android. | 32 // Mac and Android. |
| 34 bool PathProviderPosix(int key, FilePath* result); | 33 bool PathProviderPosix(int key, FilePath* result); |
| 35 #endif | 34 #endif |
| 36 } // namespace base | |
| 37 | 35 |
| 38 namespace { | 36 namespace { |
| 39 | 37 |
| 40 typedef base::hash_map<int, FilePath> PathMap; | 38 typedef hash_map<int, FilePath> PathMap; |
| 41 | 39 |
| 42 // We keep a linked list of providers. In a debug build we ensure that no two | 40 // We keep a linked list of providers. In a debug build we ensure that no two |
| 43 // providers claim overlapping keys. | 41 // providers claim overlapping keys. |
| 44 struct Provider { | 42 struct Provider { |
| 45 PathService::ProviderFunc func; | 43 PathService::ProviderFunc func; |
| 46 struct Provider* next; | 44 struct Provider* next; |
| 47 #ifndef NDEBUG | 45 #ifndef NDEBUG |
| 48 int key_start; | 46 int key_start; |
| 49 int key_end; | 47 int key_end; |
| 50 #endif | 48 #endif |
| 51 bool is_static; | 49 bool is_static; |
| 52 }; | 50 }; |
| 53 | 51 |
| 54 Provider base_provider = { | 52 Provider base_provider = { |
| 55 base::PathProvider, | 53 PathProvider, |
| 56 NULL, | 54 NULL, |
| 57 #ifndef NDEBUG | 55 #ifndef NDEBUG |
| 58 base::PATH_START, | 56 PATH_START, |
| 59 base::PATH_END, | 57 PATH_END, |
| 60 #endif | 58 #endif |
| 61 true | 59 true |
| 62 }; | 60 }; |
| 63 | 61 |
| 64 #if defined(OS_WIN) | 62 #if defined(OS_WIN) |
| 65 Provider base_provider_win = { | 63 Provider base_provider_win = { |
| 66 base::PathProviderWin, | 64 PathProviderWin, |
| 67 &base_provider, | 65 &base_provider, |
| 68 #ifndef NDEBUG | 66 #ifndef NDEBUG |
| 69 base::PATH_WIN_START, | 67 PATH_WIN_START, |
| 70 base::PATH_WIN_END, | 68 PATH_WIN_END, |
| 71 #endif | 69 #endif |
| 72 true | 70 true |
| 73 }; | 71 }; |
| 74 #endif | 72 #endif |
| 75 | 73 |
| 76 #if defined(OS_MACOSX) | 74 #if defined(OS_MACOSX) |
| 77 Provider base_provider_mac = { | 75 Provider base_provider_mac = { |
| 78 base::PathProviderMac, | 76 PathProviderMac, |
| 79 &base_provider, | 77 &base_provider, |
| 80 #ifndef NDEBUG | 78 #ifndef NDEBUG |
| 81 base::PATH_MAC_START, | 79 PATH_MAC_START, |
| 82 base::PATH_MAC_END, | 80 PATH_MAC_END, |
| 83 #endif | 81 #endif |
| 84 true | 82 true |
| 85 }; | 83 }; |
| 86 #endif | 84 #endif |
| 87 | 85 |
| 88 #if defined(OS_ANDROID) | 86 #if defined(OS_ANDROID) |
| 89 Provider base_provider_android = { | 87 Provider base_provider_android = { |
| 90 base::PathProviderAndroid, | 88 PathProviderAndroid, |
| 91 &base_provider, | 89 &base_provider, |
| 92 #ifndef NDEBUG | 90 #ifndef NDEBUG |
| 93 base::PATH_ANDROID_START, | 91 PATH_ANDROID_START, |
| 94 base::PATH_ANDROID_END, | 92 PATH_ANDROID_END, |
| 95 #endif | 93 #endif |
| 96 true | 94 true |
| 97 }; | 95 }; |
| 98 #endif | 96 #endif |
| 99 | 97 |
| 100 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) | 98 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
| 101 Provider base_provider_posix = { | 99 Provider base_provider_posix = { |
| 102 base::PathProviderPosix, | 100 PathProviderPosix, |
| 103 &base_provider, | 101 &base_provider, |
| 104 #ifndef NDEBUG | 102 #ifndef NDEBUG |
| 105 base::PATH_POSIX_START, | 103 PATH_POSIX_START, |
| 106 base::PATH_POSIX_END, | 104 PATH_POSIX_END, |
| 107 #endif | 105 #endif |
| 108 true | 106 true |
| 109 }; | 107 }; |
| 110 #endif | 108 #endif |
| 111 | 109 |
| 112 | 110 |
| 113 struct PathData { | 111 struct PathData { |
| 114 base::Lock lock; | 112 Lock lock; |
| 115 PathMap cache; // Cache mappings from path key to path value. | 113 PathMap cache; // Cache mappings from path key to path value. |
| 116 PathMap overrides; // Track path overrides. | 114 PathMap overrides; // Track path overrides. |
| 117 Provider* providers; // Linked list of path service providers. | 115 Provider* providers; // Linked list of path service providers. |
| 118 bool cache_disabled; // Don't use cache if true; | 116 bool cache_disabled; // Don't use cache if true; |
| 119 | 117 |
| 120 PathData() : cache_disabled(false) { | 118 PathData() : cache_disabled(false) { |
| 121 #if defined(OS_WIN) | 119 #if defined(OS_WIN) |
| 122 providers = &base_provider_win; | 120 providers = &base_provider_win; |
| 123 #elif defined(OS_MACOSX) | 121 #elif defined(OS_MACOSX) |
| 124 providers = &base_provider_mac; | 122 providers = &base_provider_mac; |
| 125 #elif defined(OS_ANDROID) | 123 #elif defined(OS_ANDROID) |
| 126 providers = &base_provider_android; | 124 providers = &base_provider_android; |
| 127 #elif defined(OS_POSIX) | 125 #elif defined(OS_POSIX) |
| 128 providers = &base_provider_posix; | 126 providers = &base_provider_posix; |
| 129 #endif | 127 #endif |
| 130 } | 128 } |
| 131 | 129 |
| 132 ~PathData() { | 130 ~PathData() { |
| 133 Provider* p = providers; | 131 Provider* p = providers; |
| 134 while (p) { | 132 while (p) { |
| 135 Provider* next = p->next; | 133 Provider* next = p->next; |
| 136 if (!p->is_static) | 134 if (!p->is_static) |
| 137 delete p; | 135 delete p; |
| 138 p = next; | 136 p = next; |
| 139 } | 137 } |
| 140 } | 138 } |
| 141 }; | 139 }; |
| 142 | 140 |
| 143 static base::LazyInstance<PathData> g_path_data = LAZY_INSTANCE_INITIALIZER; | 141 static LazyInstance<PathData> g_path_data = LAZY_INSTANCE_INITIALIZER; |
| 144 | 142 |
| 145 static PathData* GetPathData() { | 143 static PathData* GetPathData() { |
| 146 return g_path_data.Pointer(); | 144 return g_path_data.Pointer(); |
| 147 } | 145 } |
| 148 | 146 |
| 149 // Tries to find |key| in the cache. |path_data| should be locked by the caller! | 147 // Tries to find |key| in the cache. |path_data| should be locked by the caller! |
| 150 bool LockedGetFromCache(int key, const PathData* path_data, FilePath* result) { | 148 bool LockedGetFromCache(int key, const PathData* path_data, FilePath* result) { |
| 151 if (path_data->cache_disabled) | 149 if (path_data->cache_disabled) |
| 152 return false; | 150 return false; |
| 153 // check for a cached version | 151 // check for a cached version |
| (...skipping 22 matching lines...) Expand all Loading... |
| 176 } // namespace | 174 } // namespace |
| 177 | 175 |
| 178 // TODO(brettw): this function does not handle long paths (filename > MAX_PATH) | 176 // TODO(brettw): this function does not handle long paths (filename > MAX_PATH) |
| 179 // characters). This isn't supported very well by Windows right now, so it is | 177 // characters). This isn't supported very well by Windows right now, so it is |
| 180 // moot, but we should keep this in mind for the future. | 178 // moot, but we should keep this in mind for the future. |
| 181 // static | 179 // static |
| 182 bool PathService::Get(int key, FilePath* result) { | 180 bool PathService::Get(int key, FilePath* result) { |
| 183 PathData* path_data = GetPathData(); | 181 PathData* path_data = GetPathData(); |
| 184 DCHECK(path_data); | 182 DCHECK(path_data); |
| 185 DCHECK(result); | 183 DCHECK(result); |
| 186 DCHECK_GE(key, base::DIR_CURRENT); | 184 DCHECK_GE(key, DIR_CURRENT); |
| 187 | 185 |
| 188 // special case the current directory because it can never be cached | 186 // special case the current directory because it can never be cached |
| 189 if (key == base::DIR_CURRENT) | 187 if (key == DIR_CURRENT) |
| 190 return base::GetCurrentDirectory(result); | 188 return GetCurrentDirectory(result); |
| 191 | 189 |
| 192 Provider* provider = NULL; | 190 Provider* provider = NULL; |
| 193 { | 191 { |
| 194 base::AutoLock scoped_lock(path_data->lock); | 192 AutoLock scoped_lock(path_data->lock); |
| 195 if (LockedGetFromCache(key, path_data, result)) | 193 if (LockedGetFromCache(key, path_data, result)) |
| 196 return true; | 194 return true; |
| 197 | 195 |
| 198 if (LockedGetFromOverrides(key, path_data, result)) | 196 if (LockedGetFromOverrides(key, path_data, result)) |
| 199 return true; | 197 return true; |
| 200 | 198 |
| 201 // Get the beginning of the list while it is still locked. | 199 // Get the beginning of the list while it is still locked. |
| 202 provider = path_data->providers; | 200 provider = path_data->providers; |
| 203 } | 201 } |
| 204 | 202 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 217 return false; | 215 return false; |
| 218 | 216 |
| 219 if (path.ReferencesParent()) { | 217 if (path.ReferencesParent()) { |
| 220 // Make sure path service never returns a path with ".." in it. | 218 // Make sure path service never returns a path with ".." in it. |
| 221 path = MakeAbsoluteFilePath(path); | 219 path = MakeAbsoluteFilePath(path); |
| 222 if (path.empty()) | 220 if (path.empty()) |
| 223 return false; | 221 return false; |
| 224 } | 222 } |
| 225 *result = path; | 223 *result = path; |
| 226 | 224 |
| 227 base::AutoLock scoped_lock(path_data->lock); | 225 AutoLock scoped_lock(path_data->lock); |
| 228 if (!path_data->cache_disabled) | 226 if (!path_data->cache_disabled) |
| 229 path_data->cache[key] = path; | 227 path_data->cache[key] = path; |
| 230 | 228 |
| 231 return true; | 229 return true; |
| 232 } | 230 } |
| 233 | 231 |
| 234 // static | 232 // static |
| 235 bool PathService::Override(int key, const FilePath& path) { | 233 bool PathService::Override(int key, const FilePath& path) { |
| 236 // Just call the full function with true for the value of |create|, and | 234 // Just call the full function with true for the value of |create|, and |
| 237 // assume that |path| may not be absolute yet. | 235 // assume that |path| may not be absolute yet. |
| 238 return OverrideAndCreateIfNeeded(key, path, false, true); | 236 return OverrideAndCreateIfNeeded(key, path, false, true); |
| 239 } | 237 } |
| 240 | 238 |
| 241 // static | 239 // static |
| 242 bool PathService::OverrideAndCreateIfNeeded(int key, | 240 bool PathService::OverrideAndCreateIfNeeded(int key, |
| 243 const FilePath& path, | 241 const FilePath& path, |
| 244 bool is_absolute, | 242 bool is_absolute, |
| 245 bool create) { | 243 bool create) { |
| 246 PathData* path_data = GetPathData(); | 244 PathData* path_data = GetPathData(); |
| 247 DCHECK(path_data); | 245 DCHECK(path_data); |
| 248 DCHECK_GT(key, base::DIR_CURRENT) << "invalid path key"; | 246 DCHECK_GT(key, DIR_CURRENT) << "invalid path key"; |
| 249 | 247 |
| 250 FilePath file_path = path; | 248 FilePath file_path = path; |
| 251 | 249 |
| 252 // For some locations this will fail if called from inside the sandbox there- | 250 // For some locations this will fail if called from inside the sandbox there- |
| 253 // fore we protect this call with a flag. | 251 // fore we protect this call with a flag. |
| 254 if (create) { | 252 if (create) { |
| 255 // Make sure the directory exists. We need to do this before we translate | 253 // Make sure the directory exists. We need to do this before we translate |
| 256 // this to the absolute path because on POSIX, MakeAbsoluteFilePath fails | 254 // this to the absolute path because on POSIX, MakeAbsoluteFilePath fails |
| 257 // if called on a non-existent path. | 255 // if called on a non-existent path. |
| 258 if (!base::PathExists(file_path) && | 256 if (!PathExists(file_path) && !CreateDirectory(file_path)) |
| 259 !base::CreateDirectory(file_path)) | |
| 260 return false; | 257 return false; |
| 261 } | 258 } |
| 262 | 259 |
| 263 // We need to have an absolute path. | 260 // We need to have an absolute path. |
| 264 if (!is_absolute) { | 261 if (!is_absolute) { |
| 265 file_path = MakeAbsoluteFilePath(file_path); | 262 file_path = MakeAbsoluteFilePath(file_path); |
| 266 if (file_path.empty()) | 263 if (file_path.empty()) |
| 267 return false; | 264 return false; |
| 268 } | 265 } |
| 269 DCHECK(file_path.IsAbsolute()); | 266 DCHECK(file_path.IsAbsolute()); |
| 270 | 267 |
| 271 base::AutoLock scoped_lock(path_data->lock); | 268 AutoLock scoped_lock(path_data->lock); |
| 272 | 269 |
| 273 // Clear the cache now. Some of its entries could have depended | 270 // Clear the cache now. Some of its entries could have depended |
| 274 // on the value we are overriding, and are now out of sync with reality. | 271 // on the value we are overriding, and are now out of sync with reality. |
| 275 path_data->cache.clear(); | 272 path_data->cache.clear(); |
| 276 | 273 |
| 277 path_data->overrides[key] = file_path; | 274 path_data->overrides[key] = file_path; |
| 278 | 275 |
| 279 return true; | 276 return true; |
| 280 } | 277 } |
| 281 | 278 |
| 282 // static | 279 // static |
| 283 bool PathService::RemoveOverride(int key) { | 280 bool PathService::RemoveOverride(int key) { |
| 284 PathData* path_data = GetPathData(); | 281 PathData* path_data = GetPathData(); |
| 285 DCHECK(path_data); | 282 DCHECK(path_data); |
| 286 | 283 |
| 287 base::AutoLock scoped_lock(path_data->lock); | 284 AutoLock scoped_lock(path_data->lock); |
| 288 | 285 |
| 289 if (path_data->overrides.find(key) == path_data->overrides.end()) | 286 if (path_data->overrides.find(key) == path_data->overrides.end()) |
| 290 return false; | 287 return false; |
| 291 | 288 |
| 292 // Clear the cache now. Some of its entries could have depended on the value | 289 // Clear the cache now. Some of its entries could have depended on the value |
| 293 // we are going to remove, and are now out of sync. | 290 // we are going to remove, and are now out of sync. |
| 294 path_data->cache.clear(); | 291 path_data->cache.clear(); |
| 295 | 292 |
| 296 path_data->overrides.erase(key); | 293 path_data->overrides.erase(key); |
| 297 | 294 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 308 Provider* p; | 305 Provider* p; |
| 309 | 306 |
| 310 p = new Provider; | 307 p = new Provider; |
| 311 p->is_static = false; | 308 p->is_static = false; |
| 312 p->func = func; | 309 p->func = func; |
| 313 #ifndef NDEBUG | 310 #ifndef NDEBUG |
| 314 p->key_start = key_start; | 311 p->key_start = key_start; |
| 315 p->key_end = key_end; | 312 p->key_end = key_end; |
| 316 #endif | 313 #endif |
| 317 | 314 |
| 318 base::AutoLock scoped_lock(path_data->lock); | 315 AutoLock scoped_lock(path_data->lock); |
| 319 | 316 |
| 320 #ifndef NDEBUG | 317 #ifndef NDEBUG |
| 321 Provider *iter = path_data->providers; | 318 Provider *iter = path_data->providers; |
| 322 while (iter) { | 319 while (iter) { |
| 323 DCHECK(key_start >= iter->key_end || key_end <= iter->key_start) << | 320 DCHECK(key_start >= iter->key_end || key_end <= iter->key_start) << |
| 324 "path provider collision"; | 321 "path provider collision"; |
| 325 iter = iter->next; | 322 iter = iter->next; |
| 326 } | 323 } |
| 327 #endif | 324 #endif |
| 328 | 325 |
| 329 p->next = path_data->providers; | 326 p->next = path_data->providers; |
| 330 path_data->providers = p; | 327 path_data->providers = p; |
| 331 } | 328 } |
| 332 | 329 |
| 333 // static | 330 // static |
| 334 void PathService::DisableCache() { | 331 void PathService::DisableCache() { |
| 335 PathData* path_data = GetPathData(); | 332 PathData* path_data = GetPathData(); |
| 336 DCHECK(path_data); | 333 DCHECK(path_data); |
| 337 | 334 |
| 338 base::AutoLock scoped_lock(path_data->lock); | 335 AutoLock scoped_lock(path_data->lock); |
| 339 path_data->cache.clear(); | 336 path_data->cache.clear(); |
| 340 path_data->cache_disabled = true; | 337 path_data->cache_disabled = true; |
| 341 } | 338 } |
| 339 |
| 340 } // namespace base |
| OLD | NEW |