1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/path_service.h"
#ifdef OS_WIN
#include <windows.h>
#include <shellapi.h>
#include <shlobj.h>
#endif
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/hash_tables.h"
#include "base/lock.h"
#include "base/logging.h"
#include "base/singleton.h"
namespace base {
bool PathProvider(int key, FilePath* result);
#if defined(OS_WIN)
bool PathProviderWin(int key, FilePath* result);
#elif defined(OS_MACOSX)
bool PathProviderMac(int key, FilePath* result);
#elif defined(OS_POSIX)
bool PathProviderPosix(int key, FilePath* result);
#endif
}
namespace {
typedef base::hash_map<int, FilePath> PathMap;
// We keep a linked list of providers. In a debug build we ensure that no two
// providers claim overlapping keys.
struct Provider {
PathService::ProviderFunc func;
struct Provider* next;
#ifndef NDEBUG
int key_start;
int key_end;
#endif
bool is_static;
};
static Provider base_provider = {
base::PathProvider,
NULL,
#ifndef NDEBUG
base::PATH_START,
base::PATH_END,
#endif
true
};
#if defined(OS_WIN)
static Provider base_provider_win = {
base::PathProviderWin,
&base_provider,
#ifndef NDEBUG
base::PATH_WIN_START,
base::PATH_WIN_END,
#endif
true
};
#endif
#if defined(OS_MACOSX)
static Provider base_provider_mac = {
base::PathProviderMac,
&base_provider,
#ifndef NDEBUG
base::PATH_MAC_START,
base::PATH_MAC_END,
#endif
true
};
#endif
#if defined(OS_POSIX) && !defined(OS_MACOSX)
static Provider base_provider_posix = {
base::PathProviderPosix,
&base_provider,
#ifndef NDEBUG
0,
0,
#endif
true
};
#endif
struct PathData {
Lock lock;
PathMap cache; // Cache mappings from path key to path value.
PathMap overrides; // Track path overrides.
Provider* providers; // Linked list of path service providers.
PathData() {
#if defined(OS_WIN)
providers = &base_provider_win;
#elif defined(OS_MACOSX)
providers = &base_provider_mac;
#elif defined(OS_POSIX)
providers = &base_provider_posix;
#endif
}
~PathData() {
Provider* p = providers;
while (p) {
Provider* next = p->next;
if (!p->is_static)
delete p;
p = next;
}
}
};
static PathData* GetPathData() {
return Singleton<PathData>::get();
}
} // namespace
// static
bool PathService::GetFromCache(int key, FilePath* result) {
PathData* path_data = GetPathData();
AutoLock scoped_lock(path_data->lock);
// check for a cached version
PathMap::const_iterator it = path_data->cache.find(key);
if (it != path_data->cache.end()) {
*result = it->second;
return true;
}
return false;
}
// static
bool PathService::GetFromOverrides(int key, FilePath* result) {
PathData* path_data = GetPathData();
AutoLock scoped_lock(path_data->lock);
// check for an overriden version.
PathMap::const_iterator it = path_data->overrides.find(key);
if (it != path_data->overrides.end()) {
*result = it->second;
return true;
}
return false;
}
// static
void PathService::AddToCache(int key, const FilePath& path) {
PathData* path_data = GetPathData();
AutoLock scoped_lock(path_data->lock);
// Save the computed path in our cache.
path_data->cache[key] = path;
}
// TODO(brettw): this function does not handle long paths (filename > MAX_PATH)
// characters). This isn't supported very well by Windows right now, so it is
// moot, but we should keep this in mind for the future.
// static
bool PathService::Get(int key, FilePath* result) {
PathData* path_data = GetPathData();
DCHECK(path_data);
DCHECK(result);
DCHECK(key >= base::DIR_CURRENT);
// special case the current directory because it can never be cached
if (key == base::DIR_CURRENT)
return file_util::GetCurrentDirectory(result);
if (GetFromCache(key, result))
return true;
if (GetFromOverrides(key, result))
return true;
FilePath path;
// search providers for the requested path
// NOTE: it should be safe to iterate here without the lock
// since RegisterProvider always prepends.
Provider* provider = path_data->providers;
while (provider) {
if (provider->func(key, &path))
break;
DCHECK(path.empty()) << "provider should not have modified path";
provider = provider->next;
}
if (path.empty())
return false;
AddToCache(key, path);
*result = path;
return true;
}
#if defined(OS_WIN)
// static
bool PathService::Get(int key, std::wstring* result) {
// Deprecated compatibility function.
FilePath path;
if (!Get(key, &path))
return false;
*result = path.ToWStringHack();
return true;
}
#endif
bool PathService::Override(int key, const FilePath& path) {
PathData* path_data = GetPathData();
DCHECK(path_data);
DCHECK(key > base::DIR_CURRENT) << "invalid path key";
FilePath file_path = path;
// Make sure the directory exists. We need to do this before we translate
// this to the absolute path because on POSIX, AbsolutePath fails if called
// on a non-existant path.
if (!file_util::PathExists(file_path) &&
!file_util::CreateDirectory(file_path))
return false;
// We need to have an absolute path, as extensions and plugins don't like
// relative paths, and will glady crash the browser in CHECK()s if they get a
// relative path.
if (!file_util::AbsolutePath(&file_path))
return false;
AutoLock scoped_lock(path_data->lock);
// Clear the cache now. Some of its entries could have depended
// on the value we are overriding, and are now out of sync with reality.
path_data->cache.clear();
path_data->cache[key] = file_path;
path_data->overrides[key] = file_path;
return true;
}
void PathService::RegisterProvider(ProviderFunc func, int key_start,
int key_end) {
PathData* path_data = GetPathData();
DCHECK(path_data);
DCHECK(key_end > key_start);
AutoLock scoped_lock(path_data->lock);
Provider* p;
#ifndef NDEBUG
p = path_data->providers;
while (p) {
DCHECK(key_start >= p->key_end || key_end <= p->key_start) <<
"path provider collision";
p = p->next;
}
#endif
p = new Provider;
p->is_static = false;
p->func = func;
p->next = path_data->providers;
#ifndef NDEBUG
p->key_start = key_start;
p->key_end = key_end;
#endif
path_data->providers = p;
}
|