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_POSIX)
27 bool PathProviderPosix(int key
, FilePath
* result
);
33 typedef base::hash_map
<int, FilePath
> PathMap
;
35 // We keep a linked list of providers. In a debug build we ensure that no two
36 // providers claim overlapping keys.
38 PathService::ProviderFunc func
;
39 struct Provider
* next
;
47 static Provider base_provider
= {
58 static Provider base_provider_win
= {
59 base::PathProviderWin
,
69 #if defined(OS_MACOSX)
70 static Provider base_provider_mac
= {
71 base::PathProviderMac
,
81 #if defined(OS_POSIX) && !defined(OS_MACOSX)
82 static Provider base_provider_posix
= {
83 base::PathProviderPosix
,
96 PathMap cache
; // Cache mappings from path key to path value.
97 PathMap overrides
; // Track path overrides.
98 Provider
* providers
; // Linked list of path service providers.
102 providers
= &base_provider_win
;
103 #elif defined(OS_MACOSX)
104 providers
= &base_provider_mac
;
105 #elif defined(OS_POSIX)
106 providers
= &base_provider_posix
;
111 Provider
* p
= providers
;
113 Provider
* next
= p
->next
;
121 static base::LazyInstance
<PathData
> g_path_data(base::LINKER_INITIALIZED
);
123 static PathData
* GetPathData() {
124 return g_path_data
.Pointer();
131 bool PathService::GetFromCache(int key
, FilePath
* result
) {
132 PathData
* path_data
= GetPathData();
133 base::AutoLock
scoped_lock(path_data
->lock
);
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
;
145 bool PathService::GetFromOverrides(int key
, FilePath
* result
) {
146 PathData
* path_data
= GetPathData();
147 base::AutoLock
scoped_lock(path_data
->lock
);
149 // check for an overriden version.
150 PathMap::const_iterator it
= path_data
->overrides
.find(key
);
151 if (it
!= path_data
->overrides
.end()) {
152 *result
= it
->second
;
159 void PathService::AddToCache(int key
, const FilePath
& path
) {
160 PathData
* path_data
= GetPathData();
161 base::AutoLock
scoped_lock(path_data
->lock
);
162 // Save the computed path in our cache.
163 path_data
->cache
[key
] = path
;
166 // TODO(brettw): this function does not handle long paths (filename > MAX_PATH)
167 // characters). This isn't supported very well by Windows right now, so it is
168 // moot, but we should keep this in mind for the future.
170 bool PathService::Get(int key
, FilePath
* result
) {
171 PathData
* path_data
= GetPathData();
174 DCHECK_GE(key
, base::DIR_CURRENT
);
176 // special case the current directory because it can never be cached
177 if (key
== base::DIR_CURRENT
)
178 return file_util::GetCurrentDirectory(result
);
180 if (GetFromCache(key
, result
))
183 if (GetFromOverrides(key
, result
))
188 // search providers for the requested path
189 // NOTE: it should be safe to iterate here without the lock
190 // since RegisterProvider always prepends.
191 Provider
* provider
= path_data
->providers
;
193 if (provider
->func(key
, &path
))
195 DCHECK(path
.empty()) << "provider should not have modified path";
196 provider
= provider
->next
;
202 AddToCache(key
, path
);
208 bool PathService::Override(int key
, const FilePath
& path
) {
209 PathData
* path_data
= GetPathData();
211 DCHECK_GT(key
, base::DIR_CURRENT
) << "invalid path key";
213 FilePath file_path
= path
;
215 // Make sure the directory exists. We need to do this before we translate
216 // this to the absolute path because on POSIX, AbsolutePath fails if called
217 // on a non-existant path.
218 if (!file_util::PathExists(file_path
) &&
219 !file_util::CreateDirectory(file_path
))
222 // We need to have an absolute path, as extensions and plugins don't like
223 // relative paths, and will glady crash the browser in CHECK()s if they get a
225 if (!file_util::AbsolutePath(&file_path
))
228 base::AutoLock
scoped_lock(path_data
->lock
);
230 // Clear the cache now. Some of its entries could have depended
231 // on the value we are overriding, and are now out of sync with reality.
232 path_data
->cache
.clear();
234 path_data
->cache
[key
] = file_path
;
235 path_data
->overrides
[key
] = file_path
;
240 void PathService::RegisterProvider(ProviderFunc func
, int key_start
,
242 PathData
* path_data
= GetPathData();
244 DCHECK_GT(key_end
, key_start
);
246 base::AutoLock
scoped_lock(path_data
->lock
);
251 p
= path_data
->providers
;
253 DCHECK(key_start
>= p
->key_end
|| key_end
<= p
->key_start
) <<
254 "path provider collision";
260 p
->is_static
= false;
262 p
->next
= path_data
->providers
;
264 p
->key_start
= key_start
;
265 p
->key_end
= key_end
;
267 path_data
->providers
= p
;