pastarmovj@chromium.org | 8f45209 | 2012-05-09 19:50:10 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
maruel@google.com | 15cfc7f | 2008-08-08 05:23:09 +0900 | [diff] [blame] | 5 | #include "base/path_service.h" |
avi@google.com | ac68ba2 | 2008-08-07 01:01:25 +0900 | [diff] [blame] | 6 | |
dbeam@chromium.org | e888849 | 2013-03-24 04:10:54 +0900 | [diff] [blame] | 7 | #if defined(OS_WIN) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 8 | #include <windows.h> |
| 9 | #include <shellapi.h> |
| 10 | #include <shlobj.h> |
avi@google.com | ac68ba2 | 2008-08-07 01:01:25 +0900 | [diff] [blame] | 11 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 12 | |
brettw@chromium.org | 30230a8 | 2013-06-12 02:52:44 +0900 | [diff] [blame] | 13 | #include "base/containers/hash_tables.h" |
brettw@chromium.org | 59eef1f | 2013-02-24 14:40:52 +0900 | [diff] [blame] | 14 | #include "base/files/file_path.h" |
brettw@chromium.org | 01f3da4 | 2014-08-14 05:22:14 +0900 | [diff] [blame] | 15 | #include "base/files/file_util.h" |
satish@chromium.org | 2940ad1 | 2010-12-14 16:48:49 +0900 | [diff] [blame] | 16 | #include "base/lazy_instance.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 17 | #include "base/logging.h" |
brettw@chromium.org | abe477a | 2011-01-21 13:55:52 +0900 | [diff] [blame] | 18 | #include "base/synchronization/lock.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 19 | |
brettw@chromium.org | c02b603 | 2013-02-16 13:12:26 +0900 | [diff] [blame] | 20 | using base::FilePath; |
brettw@chromium.org | 99b198e | 2013-04-12 14:17:15 +0900 | [diff] [blame] | 21 | using base::MakeAbsoluteFilePath; |
brettw@chromium.org | c02b603 | 2013-02-16 13:12:26 +0900 | [diff] [blame] | 22 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 23 | namespace base { |
evanm@google.com | 3e9d5d7 | 2008-11-20 01:50:03 +0900 | [diff] [blame] | 24 | bool PathProvider(int key, FilePath* result); |
erikkay@google.com | 9fc57d0 | 2008-08-09 05:16:08 +0900 | [diff] [blame] | 25 | #if defined(OS_WIN) |
evanm@google.com | 3e9d5d7 | 2008-11-20 01:50:03 +0900 | [diff] [blame] | 26 | bool PathProviderWin(int key, FilePath* result); |
evanm@google.com | e41d3b3 | 2008-08-15 10:04:11 +0900 | [diff] [blame] | 27 | #elif defined(OS_MACOSX) |
evanm@google.com | 3e9d5d7 | 2008-11-20 01:50:03 +0900 | [diff] [blame] | 28 | bool PathProviderMac(int key, FilePath* result); |
michaelbai@google.com | 2251c62 | 2011-06-22 07:34:50 +0900 | [diff] [blame] | 29 | #elif defined(OS_ANDROID) |
| 30 | bool PathProviderAndroid(int key, FilePath* result); |
evan@chromium.org | c136509 | 2009-11-21 10:29:00 +0900 | [diff] [blame] | 31 | #elif defined(OS_POSIX) |
gab@chromium.org | 97fc1e6 | 2012-09-21 01:24:52 +0900 | [diff] [blame] | 32 | // PathProviderPosix is the default path provider on POSIX OSes other than |
| 33 | // Mac and Android. |
evan@chromium.org | c136509 | 2009-11-21 10:29:00 +0900 | [diff] [blame] | 34 | bool PathProviderPosix(int key, FilePath* result); |
erikkay@google.com | c7980ee | 2008-08-06 04:46:31 +0900 | [diff] [blame] | 35 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | namespace { |
| 39 | |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 40 | typedef base::hash_map<int, FilePath> PathMap; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 41 | |
| 42 | // We keep a linked list of providers. In a debug build we ensure that no two |
| 43 | // providers claim overlapping keys. |
| 44 | struct Provider { |
| 45 | PathService::ProviderFunc func; |
| 46 | struct Provider* next; |
| 47 | #ifndef NDEBUG |
| 48 | int key_start; |
| 49 | int key_end; |
| 50 | #endif |
erikkay@google.com | 564ef6d | 2008-08-21 00:47:39 +0900 | [diff] [blame] | 51 | bool is_static; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 52 | }; |
| 53 | |
jhawkins@chromium.org | 88fbaf6 | 2012-01-28 09:34:40 +0900 | [diff] [blame] | 54 | Provider base_provider = { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 55 | base::PathProvider, |
| 56 | NULL, |
| 57 | #ifndef NDEBUG |
| 58 | base::PATH_START, |
erikkay@google.com | 564ef6d | 2008-08-21 00:47:39 +0900 | [diff] [blame] | 59 | base::PATH_END, |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 60 | #endif |
erikkay@google.com | 564ef6d | 2008-08-21 00:47:39 +0900 | [diff] [blame] | 61 | true |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 62 | }; |
| 63 | |
evan@chromium.org | c136509 | 2009-11-21 10:29:00 +0900 | [diff] [blame] | 64 | #if defined(OS_WIN) |
jhawkins@chromium.org | 88fbaf6 | 2012-01-28 09:34:40 +0900 | [diff] [blame] | 65 | Provider base_provider_win = { |
erikkay@google.com | c7980ee | 2008-08-06 04:46:31 +0900 | [diff] [blame] | 66 | base::PathProviderWin, |
| 67 | &base_provider, |
| 68 | #ifndef NDEBUG |
| 69 | base::PATH_WIN_START, |
erikkay@google.com | 564ef6d | 2008-08-21 00:47:39 +0900 | [diff] [blame] | 70 | base::PATH_WIN_END, |
erikkay@google.com | c7980ee | 2008-08-06 04:46:31 +0900 | [diff] [blame] | 71 | #endif |
erikkay@google.com | 564ef6d | 2008-08-21 00:47:39 +0900 | [diff] [blame] | 72 | true |
erikkay@google.com | c7980ee | 2008-08-06 04:46:31 +0900 | [diff] [blame] | 73 | }; |
| 74 | #endif |
| 75 | |
evan@chromium.org | c136509 | 2009-11-21 10:29:00 +0900 | [diff] [blame] | 76 | #if defined(OS_MACOSX) |
jhawkins@chromium.org | 88fbaf6 | 2012-01-28 09:34:40 +0900 | [diff] [blame] | 77 | Provider base_provider_mac = { |
erikkay@google.com | 9fc57d0 | 2008-08-09 05:16:08 +0900 | [diff] [blame] | 78 | base::PathProviderMac, |
| 79 | &base_provider, |
| 80 | #ifndef NDEBUG |
| 81 | base::PATH_MAC_START, |
erikkay@google.com | 564ef6d | 2008-08-21 00:47:39 +0900 | [diff] [blame] | 82 | base::PATH_MAC_END, |
erikkay@google.com | 9fc57d0 | 2008-08-09 05:16:08 +0900 | [diff] [blame] | 83 | #endif |
erikkay@google.com | 564ef6d | 2008-08-21 00:47:39 +0900 | [diff] [blame] | 84 | true |
| 85 | }; |
erikkay@google.com | 9fc57d0 | 2008-08-09 05:16:08 +0900 | [diff] [blame] | 86 | #endif |
evanm@google.com | e41d3b3 | 2008-08-15 10:04:11 +0900 | [diff] [blame] | 87 | |
michaelbai@google.com | 2251c62 | 2011-06-22 07:34:50 +0900 | [diff] [blame] | 88 | #if defined(OS_ANDROID) |
jhawkins@chromium.org | 88fbaf6 | 2012-01-28 09:34:40 +0900 | [diff] [blame] | 89 | Provider base_provider_android = { |
michaelbai@google.com | 2251c62 | 2011-06-22 07:34:50 +0900 | [diff] [blame] | 90 | base::PathProviderAndroid, |
| 91 | &base_provider, |
| 92 | #ifndef NDEBUG |
gab@chromium.org | 97fc1e6 | 2012-09-21 01:24:52 +0900 | [diff] [blame] | 93 | base::PATH_ANDROID_START, |
| 94 | base::PATH_ANDROID_END, |
michaelbai@google.com | 2251c62 | 2011-06-22 07:34:50 +0900 | [diff] [blame] | 95 | #endif |
| 96 | true |
| 97 | }; |
| 98 | #endif |
| 99 | |
| 100 | #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
jhawkins@chromium.org | 88fbaf6 | 2012-01-28 09:34:40 +0900 | [diff] [blame] | 101 | Provider base_provider_posix = { |
evan@chromium.org | c136509 | 2009-11-21 10:29:00 +0900 | [diff] [blame] | 102 | base::PathProviderPosix, |
evanm@google.com | e41d3b3 | 2008-08-15 10:04:11 +0900 | [diff] [blame] | 103 | &base_provider, |
| 104 | #ifndef NDEBUG |
gab@chromium.org | 97fc1e6 | 2012-09-21 01:24:52 +0900 | [diff] [blame] | 105 | base::PATH_POSIX_START, |
| 106 | base::PATH_POSIX_END, |
evanm@google.com | e41d3b3 | 2008-08-15 10:04:11 +0900 | [diff] [blame] | 107 | #endif |
erikkay@google.com | 564ef6d | 2008-08-21 00:47:39 +0900 | [diff] [blame] | 108 | true |
| 109 | }; |
evanm@google.com | e41d3b3 | 2008-08-15 10:04:11 +0900 | [diff] [blame] | 110 | #endif |
| 111 | |
| 112 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 113 | struct PathData { |
brettw@chromium.org | abe477a | 2011-01-21 13:55:52 +0900 | [diff] [blame] | 114 | base::Lock lock; |
| 115 | PathMap cache; // Cache mappings from path key to path value. |
| 116 | PathMap overrides; // Track path overrides. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 117 | Provider* providers; // Linked list of path service providers. |
vitalybuka@chromium.org | 5a75b7e | 2013-01-29 09:56:56 +0900 | [diff] [blame] | 118 | bool cache_disabled; // Don't use cache if true; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 119 | |
vitalybuka@chromium.org | 5a75b7e | 2013-01-29 09:56:56 +0900 | [diff] [blame] | 120 | PathData() : cache_disabled(false) { |
erikkay@google.com | c7980ee | 2008-08-06 04:46:31 +0900 | [diff] [blame] | 121 | #if defined(OS_WIN) |
| 122 | providers = &base_provider_win; |
erikkay@google.com | 9fc57d0 | 2008-08-09 05:16:08 +0900 | [diff] [blame] | 123 | #elif defined(OS_MACOSX) |
| 124 | providers = &base_provider_mac; |
michaelbai@google.com | 2251c62 | 2011-06-22 07:34:50 +0900 | [diff] [blame] | 125 | #elif defined(OS_ANDROID) |
| 126 | providers = &base_provider_android; |
evan@chromium.org | c136509 | 2009-11-21 10:29:00 +0900 | [diff] [blame] | 127 | #elif defined(OS_POSIX) |
| 128 | providers = &base_provider_posix; |
erikkay@google.com | c7980ee | 2008-08-06 04:46:31 +0900 | [diff] [blame] | 129 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 130 | } |
erikkay@google.com | 564ef6d | 2008-08-21 00:47:39 +0900 | [diff] [blame] | 131 | |
| 132 | ~PathData() { |
| 133 | Provider* p = providers; |
| 134 | while (p) { |
| 135 | Provider* next = p->next; |
| 136 | if (!p->is_static) |
| 137 | delete p; |
| 138 | p = next; |
| 139 | } |
| 140 | } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 141 | }; |
maruel@chromium.org | 8fe7adc | 2009-03-04 00:01:12 +0900 | [diff] [blame] | 142 | |
joth@chromium.org | b24883c | 2011-11-15 22:31:49 +0900 | [diff] [blame] | 143 | static base::LazyInstance<PathData> g_path_data = LAZY_INSTANCE_INITIALIZER; |
satish@chromium.org | 2940ad1 | 2010-12-14 16:48:49 +0900 | [diff] [blame] | 144 | |
erikkay@google.com | a3ff275 | 2008-08-13 02:33:52 +0900 | [diff] [blame] | 145 | static PathData* GetPathData() { |
satish@chromium.org | 2940ad1 | 2010-12-14 16:48:49 +0900 | [diff] [blame] | 146 | return g_path_data.Pointer(); |
erikkay@google.com | a3ff275 | 2008-08-13 02:33:52 +0900 | [diff] [blame] | 147 | } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 148 | |
pastarmovj@chromium.org | 5dce41a | 2012-09-27 04:05:12 +0900 | [diff] [blame] | 149 | // 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) { |
vitalybuka@chromium.org | 5a75b7e | 2013-01-29 09:56:56 +0900 | [diff] [blame] | 151 | if (path_data->cache_disabled) |
| 152 | return false; |
erikkay@google.com | 3620e4c | 2008-08-12 00:38:27 +0900 | [diff] [blame] | 153 | // check for a cached version |
| 154 | PathMap::const_iterator it = path_data->cache.find(key); |
| 155 | if (it != path_data->cache.end()) { |
| 156 | *result = it->second; |
| 157 | return true; |
| 158 | } |
| 159 | return false; |
| 160 | } |
| 161 | |
pastarmovj@chromium.org | 5dce41a | 2012-09-27 04:05:12 +0900 | [diff] [blame] | 162 | // Tries to find |key| in the overrides map. |path_data| should be locked by the |
| 163 | // caller! |
| 164 | bool LockedGetFromOverrides(int key, PathData* path_data, FilePath* result) { |
jhawkins@chromium.org | da5f34c | 2011-12-15 03:47:26 +0900 | [diff] [blame] | 165 | // check for an overridden version. |
phajdan.jr@chromium.org | 26faa10 | 2010-09-10 08:49:04 +0900 | [diff] [blame] | 166 | PathMap::const_iterator it = path_data->overrides.find(key); |
| 167 | if (it != path_data->overrides.end()) { |
vitalybuka@chromium.org | 5a75b7e | 2013-01-29 09:56:56 +0900 | [diff] [blame] | 168 | if (!path_data->cache_disabled) |
| 169 | path_data->cache[key] = it->second; |
phajdan.jr@chromium.org | 26faa10 | 2010-09-10 08:49:04 +0900 | [diff] [blame] | 170 | *result = it->second; |
| 171 | return true; |
| 172 | } |
| 173 | return false; |
| 174 | } |
| 175 | |
pastarmovj@chromium.org | 5dce41a | 2012-09-27 04:05:12 +0900 | [diff] [blame] | 176 | } // namespace |
erikkay@google.com | 3620e4c | 2008-08-12 00:38:27 +0900 | [diff] [blame] | 177 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 178 | // 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 |
| 180 | // moot, but we should keep this in mind for the future. |
| 181 | // static |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 182 | bool PathService::Get(int key, FilePath* result) { |
erikkay@google.com | a3ff275 | 2008-08-13 02:33:52 +0900 | [diff] [blame] | 183 | PathData* path_data = GetPathData(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 184 | DCHECK(path_data); |
| 185 | DCHECK(result); |
pkasting@chromium.org | d23fe1a | 2011-04-01 05:34:25 +0900 | [diff] [blame] | 186 | DCHECK_GE(key, base::DIR_CURRENT); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 187 | |
| 188 | // special case the current directory because it can never be cached |
erikkay@google.com | 9fc57d0 | 2008-08-09 05:16:08 +0900 | [diff] [blame] | 189 | if (key == base::DIR_CURRENT) |
brettw@chromium.org | 14b3aa2 | 2014-03-12 05:59:02 +0900 | [diff] [blame] | 190 | return base::GetCurrentDirectory(result); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 191 | |
pastarmovj@chromium.org | 5dce41a | 2012-09-27 04:05:12 +0900 | [diff] [blame] | 192 | Provider* provider = NULL; |
| 193 | { |
| 194 | base::AutoLock scoped_lock(path_data->lock); |
| 195 | if (LockedGetFromCache(key, path_data, result)) |
| 196 | return true; |
estade@chromium.org | 234a632 | 2009-02-28 10:49:55 +0900 | [diff] [blame] | 197 | |
pastarmovj@chromium.org | 5dce41a | 2012-09-27 04:05:12 +0900 | [diff] [blame] | 198 | if (LockedGetFromOverrides(key, path_data, result)) |
| 199 | return true; |
| 200 | |
| 201 | // Get the beginning of the list while it is still locked. |
| 202 | provider = path_data->providers; |
| 203 | } |
phajdan.jr@chromium.org | 26faa10 | 2010-09-10 08:49:04 +0900 | [diff] [blame] | 204 | |
evanm@google.com | 3e9d5d7 | 2008-11-20 01:50:03 +0900 | [diff] [blame] | 205 | FilePath path; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 206 | |
pastarmovj@chromium.org | 5dce41a | 2012-09-27 04:05:12 +0900 | [diff] [blame] | 207 | // Iterating does not need the lock because only the list head might be |
| 208 | // modified on another thread. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 209 | while (provider) { |
evanm@google.com | 3e9d5d7 | 2008-11-20 01:50:03 +0900 | [diff] [blame] | 210 | if (provider->func(key, &path)) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 211 | break; |
estade@chromium.org | 234a632 | 2009-02-28 10:49:55 +0900 | [diff] [blame] | 212 | DCHECK(path.empty()) << "provider should not have modified path"; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 213 | provider = provider->next; |
| 214 | } |
| 215 | |
estade@chromium.org | 234a632 | 2009-02-28 10:49:55 +0900 | [diff] [blame] | 216 | if (path.empty()) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 217 | return false; |
| 218 | |
maruel@chromium.org | 487eb9c | 2013-01-26 13:51:29 +0900 | [diff] [blame] | 219 | if (path.ReferencesParent()) { |
| 220 | // Make sure path service never returns a path with ".." in it. |
brettw@chromium.org | 99b198e | 2013-04-12 14:17:15 +0900 | [diff] [blame] | 221 | path = MakeAbsoluteFilePath(path); |
| 222 | if (path.empty()) |
maruel@chromium.org | 487eb9c | 2013-01-26 13:51:29 +0900 | [diff] [blame] | 223 | return false; |
maruel@chromium.org | 487eb9c | 2013-01-26 13:51:29 +0900 | [diff] [blame] | 224 | } |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 225 | *result = path; |
pastarmovj@chromium.org | 5dce41a | 2012-09-27 04:05:12 +0900 | [diff] [blame] | 226 | |
| 227 | base::AutoLock scoped_lock(path_data->lock); |
vitalybuka@chromium.org | 5a75b7e | 2013-01-29 09:56:56 +0900 | [diff] [blame] | 228 | if (!path_data->cache_disabled) |
| 229 | path_data->cache[key] = path; |
pastarmovj@chromium.org | 5dce41a | 2012-09-27 04:05:12 +0900 | [diff] [blame] | 230 | |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 231 | return true; |
| 232 | } |
| 233 | |
pastarmovj@chromium.org | 5dce41a | 2012-09-27 04:05:12 +0900 | [diff] [blame] | 234 | // static |
jcampan@chromium.org | cce10e7 | 2009-06-26 02:29:09 +0900 | [diff] [blame] | 235 | bool PathService::Override(int key, const FilePath& path) { |
joaodasilva@chromium.org | 41819c5 | 2014-05-03 02:59:42 +0900 | [diff] [blame] | 236 | // Just call the full function with true for the value of |create|, and |
| 237 | // assume that |path| may not be absolute yet. |
| 238 | return OverrideAndCreateIfNeeded(key, path, false, true); |
pastarmovj@chromium.org | 8f45209 | 2012-05-09 19:50:10 +0900 | [diff] [blame] | 239 | } |
| 240 | |
pastarmovj@chromium.org | 5dce41a | 2012-09-27 04:05:12 +0900 | [diff] [blame] | 241 | // static |
pastarmovj@chromium.org | 8f45209 | 2012-05-09 19:50:10 +0900 | [diff] [blame] | 242 | bool PathService::OverrideAndCreateIfNeeded(int key, |
| 243 | const FilePath& path, |
joaodasilva@chromium.org | 41819c5 | 2014-05-03 02:59:42 +0900 | [diff] [blame] | 244 | bool is_absolute, |
pastarmovj@chromium.org | 8f45209 | 2012-05-09 19:50:10 +0900 | [diff] [blame] | 245 | bool create) { |
erikkay@google.com | a3ff275 | 2008-08-13 02:33:52 +0900 | [diff] [blame] | 246 | PathData* path_data = GetPathData(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 247 | DCHECK(path_data); |
kushi.p@gmail.com | e18fdd3 | 2011-03-14 07:13:33 +0900 | [diff] [blame] | 248 | DCHECK_GT(key, base::DIR_CURRENT) << "invalid path key"; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 249 | |
jcampan@chromium.org | cce10e7 | 2009-06-26 02:29:09 +0900 | [diff] [blame] | 250 | FilePath file_path = path; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 251 | |
pastarmovj@chromium.org | 8f45209 | 2012-05-09 19:50:10 +0900 | [diff] [blame] | 252 | // For some locations this will fail if called from inside the sandbox there- |
| 253 | // fore we protect this call with a flag. |
| 254 | if (create) { |
| 255 | // Make sure the directory exists. We need to do this before we translate |
brettw@chromium.org | 99b198e | 2013-04-12 14:17:15 +0900 | [diff] [blame] | 256 | // this to the absolute path because on POSIX, MakeAbsoluteFilePath fails |
| 257 | // if called on a non-existent path. |
brettw@chromium.org | 10b6412 | 2013-07-12 02:36:07 +0900 | [diff] [blame] | 258 | if (!base::PathExists(file_path) && |
brettw@chromium.org | 738669a | 2013-12-04 05:08:54 +0900 | [diff] [blame] | 259 | !base::CreateDirectory(file_path)) |
pastarmovj@chromium.org | 8f45209 | 2012-05-09 19:50:10 +0900 | [diff] [blame] | 260 | return false; |
| 261 | } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 262 | |
brettw@chromium.org | 99b198e | 2013-04-12 14:17:15 +0900 | [diff] [blame] | 263 | // We need to have an absolute path. |
joaodasilva@chromium.org | 41819c5 | 2014-05-03 02:59:42 +0900 | [diff] [blame] | 264 | if (!is_absolute) { |
| 265 | file_path = MakeAbsoluteFilePath(file_path); |
| 266 | if (file_path.empty()) |
| 267 | return false; |
| 268 | } |
| 269 | DCHECK(file_path.IsAbsolute()); |
erg@google.com | 08945ec | 2009-10-28 08:31:36 +0900 | [diff] [blame] | 270 | |
brettw@chromium.org | abe477a | 2011-01-21 13:55:52 +0900 | [diff] [blame] | 271 | base::AutoLock scoped_lock(path_data->lock); |
phajdan.jr@chromium.org | 26faa10 | 2010-09-10 08:49:04 +0900 | [diff] [blame] | 272 | |
| 273 | // 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. |
| 275 | path_data->cache.clear(); |
| 276 | |
phajdan.jr@chromium.org | 26faa10 | 2010-09-10 08:49:04 +0900 | [diff] [blame] | 277 | path_data->overrides[key] = file_path; |
| 278 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 279 | return true; |
| 280 | } |
| 281 | |
pastarmovj@chromium.org | 5dce41a | 2012-09-27 04:05:12 +0900 | [diff] [blame] | 282 | // static |
| 283 | bool PathService::RemoveOverride(int key) { |
| 284 | PathData* path_data = GetPathData(); |
| 285 | DCHECK(path_data); |
| 286 | |
| 287 | base::AutoLock scoped_lock(path_data->lock); |
| 288 | |
| 289 | if (path_data->overrides.find(key) == path_data->overrides.end()) |
| 290 | return false; |
| 291 | |
| 292 | // 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. |
| 294 | path_data->cache.clear(); |
| 295 | |
| 296 | path_data->overrides.erase(key); |
| 297 | |
| 298 | return true; |
| 299 | } |
| 300 | |
| 301 | // static |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 302 | void PathService::RegisterProvider(ProviderFunc func, int key_start, |
| 303 | int key_end) { |
erikkay@google.com | a3ff275 | 2008-08-13 02:33:52 +0900 | [diff] [blame] | 304 | PathData* path_data = GetPathData(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 305 | DCHECK(path_data); |
kushi.p@gmail.com | e18fdd3 | 2011-03-14 07:13:33 +0900 | [diff] [blame] | 306 | DCHECK_GT(key_end, key_start); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 307 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 308 | Provider* p; |
| 309 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 310 | p = new Provider; |
erikkay@google.com | 564ef6d | 2008-08-21 00:47:39 +0900 | [diff] [blame] | 311 | p->is_static = false; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 312 | p->func = func; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 313 | #ifndef NDEBUG |
| 314 | p->key_start = key_start; |
| 315 | p->key_end = key_end; |
| 316 | #endif |
pastarmovj@chromium.org | 5dce41a | 2012-09-27 04:05:12 +0900 | [diff] [blame] | 317 | |
| 318 | base::AutoLock scoped_lock(path_data->lock); |
| 319 | |
| 320 | #ifndef NDEBUG |
| 321 | Provider *iter = path_data->providers; |
| 322 | while (iter) { |
| 323 | DCHECK(key_start >= iter->key_end || key_end <= iter->key_start) << |
| 324 | "path provider collision"; |
| 325 | iter = iter->next; |
| 326 | } |
| 327 | #endif |
| 328 | |
| 329 | p->next = path_data->providers; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 330 | path_data->providers = p; |
| 331 | } |
vitalybuka@chromium.org | 5a75b7e | 2013-01-29 09:56:56 +0900 | [diff] [blame] | 332 | |
| 333 | // static |
| 334 | void PathService::DisableCache() { |
| 335 | PathData* path_data = GetPathData(); |
| 336 | DCHECK(path_data); |
| 337 | |
| 338 | base::AutoLock scoped_lock(path_data->lock); |
| 339 | path_data->cache.clear(); |
| 340 | path_data->cache_disabled = true; |
| 341 | } |