[M108 Migration][VD] Support set time and time zone offset
[platform/framework/web/chromium-efl.git] / base / path_service.cc
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/path_service.h"
6
7 #include <unordered_map>
8
9 #include "base/check_op.h"
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/logging.h"
13 #include "base/memory/raw_ptr.h"
14 #include "base/synchronization/lock.h"
15 #include "build/build_config.h"
16
17 #if BUILDFLAG(IS_WIN)
18 #include <windows.h>
19 #include <shellapi.h>
20 #include <shlobj.h>
21 #endif
22
23 namespace base {
24
25 bool PathProvider(int key, FilePath* result);
26
27 #if BUILDFLAG(IS_WIN)
28 bool PathProviderWin(int key, FilePath* result);
29 #elif BUILDFLAG(IS_APPLE)
30 bool PathProviderMac(int key, FilePath* result);
31 #elif BUILDFLAG(IS_ANDROID)
32 bool PathProviderAndroid(int key, FilePath* result);
33 #elif BUILDFLAG(IS_FUCHSIA)
34 bool PathProviderFuchsia(int key, FilePath* result);
35 #elif BUILDFLAG(IS_POSIX)
36 // PathProviderPosix is the default path provider on POSIX OSes other than
37 // Mac and Android.
38 bool PathProviderPosix(int key, FilePath* result);
39 #endif
40
41 namespace {
42
43 typedef std::unordered_map<int, FilePath> PathMap;
44
45 // We keep a linked list of providers.  In a debug build we ensure that no two
46 // providers claim overlapping keys.
47 struct Provider {
48   PathService::ProviderFunc func;
49   struct Provider* next;
50 #ifndef NDEBUG
51   int key_start;
52   int key_end;
53 #endif
54   bool is_static;
55 };
56
57 Provider base_provider = {PathProvider, nullptr,
58 #ifndef NDEBUG
59                           PATH_START, PATH_END,
60 #endif
61                           true};
62
63 #if BUILDFLAG(IS_WIN)
64 Provider base_provider_win = {
65   PathProviderWin,
66   &base_provider,
67 #ifndef NDEBUG
68   PATH_WIN_START,
69   PATH_WIN_END,
70 #endif
71   true
72 };
73 #endif
74
75 #if BUILDFLAG(IS_APPLE)
76 Provider base_provider_mac = {
77   PathProviderMac,
78   &base_provider,
79 #ifndef NDEBUG
80   PATH_MAC_START,
81   PATH_MAC_END,
82 #endif
83   true
84 };
85 #endif
86
87 #if BUILDFLAG(IS_ANDROID)
88 Provider base_provider_android = {
89   PathProviderAndroid,
90   &base_provider,
91 #ifndef NDEBUG
92   PATH_ANDROID_START,
93   PATH_ANDROID_END,
94 #endif
95   true
96 };
97 #endif
98
99 #if BUILDFLAG(IS_FUCHSIA)
100 Provider base_provider_fuchsia = {PathProviderFuchsia, &base_provider,
101 #ifndef NDEBUG
102                                   0, 0,
103 #endif
104                                   true};
105 #endif
106
107 #if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_APPLE) && !BUILDFLAG(IS_ANDROID)
108 Provider base_provider_posix = {
109   PathProviderPosix,
110   &base_provider,
111 #ifndef NDEBUG
112   PATH_POSIX_START,
113   PATH_POSIX_END,
114 #endif
115   true
116 };
117 #endif
118
119
120 struct PathData {
121   Lock lock;
122   PathMap cache;        // Cache mappings from path key to path value.
123   PathMap overrides;    // Track path overrides.
124   raw_ptr<Provider> providers;  // Linked list of path service providers.
125   bool cache_disabled;  // Don't use cache if true;
126
127   PathData() : cache_disabled(false) {
128 #if BUILDFLAG(IS_WIN)
129     providers = &base_provider_win;
130 #elif BUILDFLAG(IS_APPLE)
131     providers = &base_provider_mac;
132 #elif BUILDFLAG(IS_ANDROID)
133     providers = &base_provider_android;
134 #elif BUILDFLAG(IS_FUCHSIA)
135     providers = &base_provider_fuchsia;
136 #elif BUILDFLAG(IS_POSIX)
137     providers = &base_provider_posix;
138 #endif
139   }
140 };
141
142 static PathData* GetPathData() {
143   static auto* path_data = new PathData();
144   return path_data;
145 }
146
147 // Tries to find |key| in the cache.
148 bool LockedGetFromCache(int key, const PathData* path_data, FilePath* result)
149     EXCLUSIVE_LOCKS_REQUIRED(path_data->lock) {
150   if (path_data->cache_disabled)
151     return false;
152   // check for a cached version
153   auto it = path_data->cache.find(key);
154   if (it != path_data->cache.end()) {
155     *result = it->second;
156     return true;
157   }
158   return false;
159 }
160
161 // Tries to find |key| in the overrides map.
162 bool LockedGetFromOverrides(int key, PathData* path_data, FilePath* result)
163     EXCLUSIVE_LOCKS_REQUIRED(path_data->lock) {
164   // check for an overridden version.
165   PathMap::const_iterator it = path_data->overrides.find(key);
166   if (it != path_data->overrides.end()) {
167     if (!path_data->cache_disabled)
168       path_data->cache[key] = it->second;
169     *result = it->second;
170     return true;
171   }
172   return false;
173 }
174
175 }  // namespace
176
177 // TODO(brettw): this function does not handle long paths (filename > MAX_PATH)
178 // characters). This isn't supported very well by Windows right now, so it is
179 // moot, but we should keep this in mind for the future.
180 // static
181 bool PathService::Get(int key, FilePath* result) {
182   PathData* path_data = GetPathData();
183   DCHECK(path_data);
184   DCHECK(result);
185   DCHECK_GT(key, PATH_START);
186
187   // Special case the current directory because it can never be cached.
188   if (key == DIR_CURRENT)
189     return GetCurrentDirectory(result);
190
191   Provider* provider = nullptr;
192   {
193     AutoLock scoped_lock(path_data->lock);
194     if (LockedGetFromCache(key, path_data, result))
195       return true;
196
197     if (LockedGetFromOverrides(key, path_data, result))
198       return true;
199
200     // Get the beginning of the list while it is still locked.
201     provider = path_data->providers;
202   }
203
204   FilePath path;
205
206   // Iterating does not need the lock because only the list head might be
207   // modified on another thread.
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;
213   }
214
215   if (path.empty())
216     return false;
217
218   if (path.ReferencesParent()) {
219     // Make sure path service never returns a path with ".." in it.
220     path = MakeAbsoluteFilePath(path);
221     if (path.empty())
222       return false;
223   }
224   *result = path;
225
226   AutoLock scoped_lock(path_data->lock);
227   if (!path_data->cache_disabled)
228     path_data->cache[key] = path;
229
230   return true;
231 }
232
233 FilePath PathService::CheckedGet(int key) {
234   FilePath path;
235   LOG_IF(FATAL, !Get(key, &path)) << "Failed to get the path for " << key;
236   return path;
237 }
238
239 // static
240 bool PathService::Override(int key, const FilePath& path) {
241   // Just call the full function with true for the value of |create|, and
242   // assume that |path| may not be absolute yet.
243   return OverrideAndCreateIfNeeded(key, path, false, true);
244 }
245
246 // static
247 bool PathService::OverrideAndCreateIfNeeded(int key,
248                                             const FilePath& path,
249                                             bool is_absolute,
250                                             bool create) {
251   PathData* path_data = GetPathData();
252   DCHECK(path_data);
253   DCHECK_GT(key, PATH_START) << "invalid path key";
254
255   FilePath file_path = path;
256
257   // For some locations this will fail if called from inside the sandbox there-
258   // fore we protect this call with a flag.
259   if (create) {
260     // Make sure the directory exists. We need to do this before we translate
261     // this to the absolute path because on POSIX, MakeAbsoluteFilePath fails
262     // if called on a non-existent path.
263     if (!PathExists(file_path) && !CreateDirectory(file_path))
264       return false;
265   }
266
267   // We need to have an absolute path.
268   if (!is_absolute) {
269     file_path = MakeAbsoluteFilePath(file_path);
270     if (file_path.empty())
271       return false;
272   }
273   DCHECK(file_path.IsAbsolute());
274
275   AutoLock scoped_lock(path_data->lock);
276
277   // Clear the cache now. Some of its entries could have depended
278   // on the value we are overriding, and are now out of sync with reality.
279   path_data->cache.clear();
280
281   path_data->overrides[key] = file_path;
282
283   return true;
284 }
285
286 // static
287 bool PathService::RemoveOverrideForTests(int key) {
288   PathData* path_data = GetPathData();
289   DCHECK(path_data);
290
291   AutoLock scoped_lock(path_data->lock);
292
293   if (path_data->overrides.find(key) == path_data->overrides.end())
294     return false;
295
296   // Clear the cache now. Some of its entries could have depended on the value
297   // we are going to remove, and are now out of sync.
298   path_data->cache.clear();
299
300   path_data->overrides.erase(key);
301
302   return true;
303 }
304
305 // static
306 bool PathService::IsOverriddenForTests(int key) {
307   PathData* path_data = GetPathData();
308   DCHECK(path_data);
309
310   AutoLock scoped_lock(path_data->lock);
311
312   return path_data->overrides.find(key) != path_data->overrides.end();
313 }
314
315 // static
316 void PathService::RegisterProvider(ProviderFunc func, int key_start,
317                                    int key_end) {
318   PathData* path_data = GetPathData();
319   DCHECK(path_data);
320   DCHECK_GT(key_end, key_start);
321
322   Provider* p;
323
324   p = new Provider;
325   p->is_static = false;
326   p->func = func;
327 #ifndef NDEBUG
328   p->key_start = key_start;
329   p->key_end = key_end;
330 #endif
331
332   AutoLock scoped_lock(path_data->lock);
333
334 #ifndef NDEBUG
335   Provider *iter = path_data->providers;
336   while (iter) {
337     DCHECK(key_start >= iter->key_end || key_end <= iter->key_start) <<
338       "path provider collision";
339     iter = iter->next;
340   }
341 #endif
342
343   p->next = path_data->providers;
344   path_data->providers = p;
345 }
346
347 // static
348 void PathService::DisableCache() {
349   PathData* path_data = GetPathData();
350   DCHECK(path_data);
351
352   AutoLock scoped_lock(path_data->lock);
353   path_data->cache.clear();
354   path_data->cache_disabled = true;
355 }
356
357 }  // namespace base