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/file_util.h" | 13 #include "base/file_util.h" |
14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
15 #include "base/hash_tables.h" | 15 #include "base/hash_tables.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 using base::FilePath; |
| 21 using base::MakeAbsoluteFilePath; |
21 | 22 |
22 namespace base { | 23 namespace base { |
23 bool PathProvider(int key, FilePath* result); | 24 bool PathProvider(int key, FilePath* result); |
24 #if defined(OS_WIN) | 25 #if defined(OS_WIN) |
25 bool PathProviderWin(int key, FilePath* result); | 26 bool PathProviderWin(int key, FilePath* result); |
26 #elif defined(OS_MACOSX) | 27 #elif defined(OS_MACOSX) |
27 bool PathProviderMac(int key, FilePath* result); | 28 bool PathProviderMac(int key, FilePath* result); |
28 #elif defined(OS_ANDROID) | 29 #elif defined(OS_ANDROID) |
29 bool PathProviderAndroid(int key, FilePath* result); | 30 bool PathProviderAndroid(int key, FilePath* result); |
30 #elif defined(OS_POSIX) | 31 #elif defined(OS_POSIX) |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 break; | 211 break; |
211 DCHECK(path.empty()) << "provider should not have modified path"; | 212 DCHECK(path.empty()) << "provider should not have modified path"; |
212 provider = provider->next; | 213 provider = provider->next; |
213 } | 214 } |
214 | 215 |
215 if (path.empty()) | 216 if (path.empty()) |
216 return false; | 217 return false; |
217 | 218 |
218 if (path.ReferencesParent()) { | 219 if (path.ReferencesParent()) { |
219 // Make sure path service never returns a path with ".." in it. | 220 // Make sure path service never returns a path with ".." in it. |
220 if (!file_util::AbsolutePath(&path)) { | 221 path = MakeAbsoluteFilePath(path); |
| 222 if (path.empty()) |
221 return false; | 223 return false; |
222 } | |
223 } | 224 } |
224 *result = path; | 225 *result = path; |
225 | 226 |
226 base::AutoLock scoped_lock(path_data->lock); | 227 base::AutoLock scoped_lock(path_data->lock); |
227 if (!path_data->cache_disabled) | 228 if (!path_data->cache_disabled) |
228 path_data->cache[key] = path; | 229 path_data->cache[key] = path; |
229 | 230 |
230 return true; | 231 return true; |
231 } | 232 } |
232 | 233 |
(...skipping 10 matching lines...) Expand all Loading... |
243 PathData* path_data = GetPathData(); | 244 PathData* path_data = GetPathData(); |
244 DCHECK(path_data); | 245 DCHECK(path_data); |
245 DCHECK_GT(key, base::DIR_CURRENT) << "invalid path key"; | 246 DCHECK_GT(key, base::DIR_CURRENT) << "invalid path key"; |
246 | 247 |
247 FilePath file_path = path; | 248 FilePath file_path = path; |
248 | 249 |
249 // 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- |
250 // fore we protect this call with a flag. | 251 // fore we protect this call with a flag. |
251 if (create) { | 252 if (create) { |
252 // 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 |
253 // this to the absolute path because on POSIX, AbsolutePath fails if called | 254 // this to the absolute path because on POSIX, MakeAbsoluteFilePath fails |
254 // on a non-existent path. | 255 // if called on a non-existent path. |
255 if (!file_util::PathExists(file_path) && | 256 if (!file_util::PathExists(file_path) && |
256 !file_util::CreateDirectory(file_path)) | 257 !file_util::CreateDirectory(file_path)) |
257 return false; | 258 return false; |
258 } | 259 } |
259 | 260 |
260 // We need to have an absolute path, as extensions and plugins don't like | 261 // We need to have an absolute path. |
261 // relative paths, and will gladly crash the browser in CHECK()s if they get a | 262 file_path = MakeAbsoluteFilePath(file_path); |
262 // relative path. | 263 if (file_path.empty()) |
263 if (!file_util::AbsolutePath(&file_path)) | |
264 return false; | 264 return false; |
265 | 265 |
266 base::AutoLock scoped_lock(path_data->lock); | 266 base::AutoLock scoped_lock(path_data->lock); |
267 | 267 |
268 // Clear the cache now. Some of its entries could have depended | 268 // Clear the cache now. Some of its entries could have depended |
269 // on the value we are overriding, and are now out of sync with reality. | 269 // on the value we are overriding, and are now out of sync with reality. |
270 path_data->cache.clear(); | 270 path_data->cache.clear(); |
271 | 271 |
272 path_data->overrides[key] = file_path; | 272 path_data->overrides[key] = file_path; |
273 | 273 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 | 327 |
328 // static | 328 // static |
329 void PathService::DisableCache() { | 329 void PathService::DisableCache() { |
330 PathData* path_data = GetPathData(); | 330 PathData* path_data = GetPathData(); |
331 DCHECK(path_data); | 331 DCHECK(path_data); |
332 | 332 |
333 base::AutoLock scoped_lock(path_data->lock); | 333 base::AutoLock scoped_lock(path_data->lock); |
334 path_data->cache.clear(); | 334 path_data->cache.clear(); |
335 path_data->cache_disabled = true; | 335 path_data->cache_disabled = true; |
336 } | 336 } |
OLD | NEW |