1 // Copyright (c) 2011 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.
5 #include "base/path_service.h"
13 #include "base/file_path.h"
14 #include "base/file_util.h"
15 #include "base/hash_tables.h"
16 #include "base/lazy_instance.h"
17 #include "base/logging.h"
18 #include "base/synchronization/lock.h"
21 bool PathProvider(int key
, FilePath
* result
);
23 bool PathProviderWin(int key
, FilePath
* result
);
24 #elif defined(OS_MACOSX)
25 bool PathProviderMac(int key
, FilePath
* result
);
26 #elif defined(OS_ANDROID)
27 bool PathProviderAndroid(int key
, FilePath
* result
);
28 #elif defined(OS_POSIX)
29 bool PathProviderPosix(int key
, FilePath
* result
);
35 typedef base::hash_map
<int, FilePath
> PathMap
;
37 // We keep a linked list of providers. In a debug build we ensure that no two
38 // providers claim overlapping keys.
40 PathService::ProviderFunc func
;
41 struct Provider
* next
;
49 static Provider base_provider
= {
60 static Provider base_provider_win
= {
61 base::PathProviderWin
,
71 #if defined(OS_MACOSX)
72 static Provider base_provider_mac
= {
73 base::PathProviderMac
,
83 #if defined(OS_ANDROID)
84 static Provider base_provider_android
= {
85 base::PathProviderAndroid
,
95 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
96 static Provider base_provider_posix
= {
97 base::PathProviderPosix
,
110 PathMap cache
; // Cache mappings from path key to path value.
111 PathMap overrides
; // Track path overrides.
112 Provider
* providers
; // Linked list of path service providers.
116 providers
= &base_provider_win
;
117 #elif defined(OS_MACOSX)
118 providers
= &base_provider_mac
;
119 #elif defined(OS_ANDROID)
120 providers
= &base_provider_android
;
121 #elif defined(OS_POSIX)
122 providers
= &base_provider_posix
;
127 Provider
* p
= providers
;
129 Provider
* next
= p
->next
;
137 static base::LazyInstance
<PathData
> g_path_data(base::LINKER_INITIALIZED
);
139 static PathData
* GetPathData() {
140 return g_path_data
.Pointer();
147 bool PathService::GetFromCache(int key
, FilePath
* result
) {
148 PathData
* path_data
= GetPathData();
149 base::AutoLock
scoped_lock(path_data
->lock
);
151 // check for a cached version
152 PathMap::const_iterator it
= path_data
->cache
.find(key
);
153 if (it
!= path_data
->cache
.end()) {
154 *result
= it
->second
;
161 bool PathService::GetFromOverrides(int key
, FilePath
* result
) {
162 PathData
* path_data
= GetPathData();
163 base::AutoLock
scoped_lock(path_data
->lock
);
165 // check for an overriden version.
166 PathMap::const_iterator it
= path_data
->overrides
.find(key
);
167 if (it
!= path_data
->overrides
.end()) {
168 *result
= it
->second
;
175 void PathService::AddToCache(int key
, const FilePath
& path
) {
176 PathData
* path_data
= GetPathData();
177 base::AutoLock
scoped_lock(path_data
->lock
);
178 // Save the computed path in our cache.
179 path_data
->cache
[key
] = path
;
182 // TODO(brettw): this function does not handle long paths (filename > MAX_PATH)
183 // characters). This isn't supported very well by Windows right now, so it is
184 // moot, but we should keep this in mind for the future.
186 bool PathService::Get(int key
, FilePath
* result
) {
187 PathData
* path_data
= GetPathData();
190 DCHECK_GE(key
, base::DIR_CURRENT
);
192 // special case the current directory because it can never be cached
193 if (key
== base::DIR_CURRENT
)
194 return file_util::GetCurrentDirectory(result
);
196 if (GetFromCache(key
, result
))
199 if (GetFromOverrides(key
, result
))
204 // search providers for the requested path
205 // NOTE: it should be safe to iterate here without the lock
206 // since RegisterProvider always prepends.
207 Provider
* provider
= path_data
->providers
;
209 if (provider
->func(key
, &path
))
211 DCHECK(path
.empty()) << "provider should not have modified path";
212 provider
= provider
->next
;
218 AddToCache(key
, path
);
224 bool PathService::Override(int key
, const FilePath
& path
) {
225 PathData
* path_data
= GetPathData();
227 DCHECK_GT(key
, base::DIR_CURRENT
) << "invalid path key";
229 FilePath file_path
= path
;
231 // Make sure the directory exists. We need to do this before we translate
232 // this to the absolute path because on POSIX, AbsolutePath fails if called
233 // on a non-existant path.
234 if (!file_util::PathExists(file_path
) &&
235 !file_util::CreateDirectory(file_path
))
238 // We need to have an absolute path, as extensions and plugins don't like
239 // relative paths, and will glady crash the browser in CHECK()s if they get a
241 if (!file_util::AbsolutePath(&file_path
))
244 base::AutoLock
scoped_lock(path_data
->lock
);
246 // Clear the cache now. Some of its entries could have depended
247 // on the value we are overriding, and are now out of sync with reality.
248 path_data
->cache
.clear();
250 path_data
->cache
[key
] = file_path
;
251 path_data
->overrides
[key
] = file_path
;
256 void PathService::RegisterProvider(ProviderFunc func
, int key_start
,
258 PathData
* path_data
= GetPathData();
260 DCHECK_GT(key_end
, key_start
);
262 base::AutoLock
scoped_lock(path_data
->lock
);
267 p
= path_data
->providers
;
269 DCHECK(key_start
>= p
->key_end
|| key_end
<= p
->key_start
) <<
270 "path provider collision";
276 p
->is_static
= false;
278 p
->next
= path_data
->providers
;
280 p
->key_start
= key_start
;
281 p
->key_end
= key_end
;
283 path_data
->providers
= p
;