Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(574)

Side by Side Diff: base/path_service.cc

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

Powered by Google App Engine
This is Rietveld 408576698