Setting a layer property immediately should cancel any current animations.
[chromium-blink-merge.git] / base / path_service.cc
blob21eea9c54c91edc3811dc502071a6f1104caf90a
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"
7 #ifdef OS_WIN
8 #include <windows.h>
9 #include <shellapi.h>
10 #include <shlobj.h>
11 #endif
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"
20 namespace base {
21 bool PathProvider(int key, FilePath* result);
22 #if defined(OS_WIN)
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);
30 #endif
33 namespace {
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.
39 struct Provider {
40 PathService::ProviderFunc func;
41 struct Provider* next;
42 #ifndef NDEBUG
43 int key_start;
44 int key_end;
45 #endif
46 bool is_static;
49 static Provider base_provider = {
50 base::PathProvider,
51 NULL,
52 #ifndef NDEBUG
53 base::PATH_START,
54 base::PATH_END,
55 #endif
56 true
59 #if defined(OS_WIN)
60 static Provider base_provider_win = {
61 base::PathProviderWin,
62 &base_provider,
63 #ifndef NDEBUG
64 base::PATH_WIN_START,
65 base::PATH_WIN_END,
66 #endif
67 true
69 #endif
71 #if defined(OS_MACOSX)
72 static Provider base_provider_mac = {
73 base::PathProviderMac,
74 &base_provider,
75 #ifndef NDEBUG
76 base::PATH_MAC_START,
77 base::PATH_MAC_END,
78 #endif
79 true
81 #endif
83 #if defined(OS_ANDROID)
84 static Provider base_provider_android = {
85 base::PathProviderAndroid,
86 &base_provider,
87 #ifndef NDEBUG
90 #endif
91 true
93 #endif
95 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
96 static Provider base_provider_posix = {
97 base::PathProviderPosix,
98 &base_provider,
99 #ifndef NDEBUG
102 #endif
103 true
105 #endif
108 struct PathData {
109 base::Lock lock;
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.
114 PathData() {
115 #if defined(OS_WIN)
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;
123 #endif
126 ~PathData() {
127 Provider* p = providers;
128 while (p) {
129 Provider* next = p->next;
130 if (!p->is_static)
131 delete p;
132 p = next;
137 static base::LazyInstance<PathData> g_path_data(base::LINKER_INITIALIZED);
139 static PathData* GetPathData() {
140 return g_path_data.Pointer();
143 } // namespace
146 // static
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;
155 return true;
157 return false;
160 // static
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;
169 return true;
171 return false;
174 // static
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.
185 // static
186 bool PathService::Get(int key, FilePath* result) {
187 PathData* path_data = GetPathData();
188 DCHECK(path_data);
189 DCHECK(result);
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))
197 return true;
199 if (GetFromOverrides(key, result))
200 return true;
202 FilePath path;
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;
208 while (provider) {
209 if (provider->func(key, &path))
210 break;
211 DCHECK(path.empty()) << "provider should not have modified path";
212 provider = provider->next;
215 if (path.empty())
216 return false;
218 AddToCache(key, path);
220 *result = path;
221 return true;
224 bool PathService::Override(int key, const FilePath& path) {
225 PathData* path_data = GetPathData();
226 DCHECK(path_data);
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))
236 return false;
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
240 // relative path.
241 if (!file_util::AbsolutePath(&file_path))
242 return false;
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;
253 return true;
256 void PathService::RegisterProvider(ProviderFunc func, int key_start,
257 int key_end) {
258 PathData* path_data = GetPathData();
259 DCHECK(path_data);
260 DCHECK_GT(key_end, key_start);
262 base::AutoLock scoped_lock(path_data->lock);
264 Provider* p;
266 #ifndef NDEBUG
267 p = path_data->providers;
268 while (p) {
269 DCHECK(key_start >= p->key_end || key_end <= p->key_start) <<
270 "path provider collision";
271 p = p->next;
273 #endif
275 p = new Provider;
276 p->is_static = false;
277 p->func = func;
278 p->next = path_data->providers;
279 #ifndef NDEBUG
280 p->key_start = key_start;
281 p->key_end = key_end;
282 #endif
283 path_data->providers = p;