OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 #ifdef OS_WIN | 7 #ifdef 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/file_path.h" |
| 14 #include "base/file_util.h" |
13 #include "base/hash_tables.h" | 15 #include "base/hash_tables.h" |
14 #include "base/file_util.h" | |
15 #include "base/lock.h" | 16 #include "base/lock.h" |
16 #include "base/logging.h" | 17 #include "base/logging.h" |
17 #include "base/singleton.h" | 18 #include "base/singleton.h" |
18 #include "base/string_util.h" | 19 #include "base/string_util.h" |
19 | 20 |
20 namespace base { | 21 namespace base { |
21 bool PathProvider(int key, std::wstring* result); | 22 bool PathProvider(int key, std::wstring* result); |
22 #if defined(OS_WIN) | 23 #if defined(OS_WIN) |
23 bool PathProviderWin(int key, std::wstring* result); | 24 bool PathProviderWin(int key, std::wstring* result); |
24 #elif defined(OS_MACOSX) | 25 #elif defined(OS_MACOSX) |
25 bool PathProviderMac(int key, std::wstring* result); | 26 bool PathProviderMac(int key, std::wstring* result); |
26 #elif defined(OS_LINUX) | 27 #elif defined(OS_LINUX) |
27 bool PathProviderLinux(int key, std::wstring* result); | 28 bool PathProviderLinux(int key, std::wstring* result); |
28 #endif | 29 #endif |
29 } | 30 } |
30 | 31 |
31 namespace { | 32 namespace { |
32 | 33 |
33 typedef base::hash_map<int, std::wstring> PathMap; | 34 typedef base::hash_map<int, FilePath> PathMap; |
34 typedef base::hash_set<int> PathSet; | 35 typedef base::hash_set<int> PathSet; |
35 | 36 |
36 // We keep a linked list of providers. In a debug build we ensure that no two | 37 // We keep a linked list of providers. In a debug build we ensure that no two |
37 // providers claim overlapping keys. | 38 // providers claim overlapping keys. |
38 struct Provider { | 39 struct Provider { |
39 PathService::ProviderFunc func; | 40 PathService::ProviderFunc func; |
40 struct Provider* next; | 41 struct Provider* next; |
41 #ifndef NDEBUG | 42 #ifndef NDEBUG |
42 int key_start; | 43 int key_start; |
43 int key_end; | 44 int key_end; |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 }; | 121 }; |
121 | 122 |
122 static PathData* GetPathData() { | 123 static PathData* GetPathData() { |
123 return Singleton<PathData>::get(); | 124 return Singleton<PathData>::get(); |
124 } | 125 } |
125 | 126 |
126 } // namespace | 127 } // namespace |
127 | 128 |
128 | 129 |
129 // static | 130 // static |
130 bool PathService::GetFromCache(int key, std::wstring* result) { | 131 bool PathService::GetFromCache(int key, FilePath* result) { |
131 PathData* path_data = GetPathData(); | 132 PathData* path_data = GetPathData(); |
132 AutoLock scoped_lock(path_data->lock); | 133 AutoLock scoped_lock(path_data->lock); |
133 | 134 |
134 // check for a cached version | 135 // check for a cached version |
135 PathMap::const_iterator it = path_data->cache.find(key); | 136 PathMap::const_iterator it = path_data->cache.find(key); |
136 if (it != path_data->cache.end()) { | 137 if (it != path_data->cache.end()) { |
137 *result = it->second; | 138 *result = it->second; |
138 return true; | 139 return true; |
139 } | 140 } |
140 return false; | 141 return false; |
141 } | 142 } |
142 | 143 |
143 // static | 144 // static |
144 void PathService::AddToCache(int key, const std::wstring& path) { | 145 void PathService::AddToCache(int key, const FilePath& path) { |
145 PathData* path_data = GetPathData(); | 146 PathData* path_data = GetPathData(); |
146 AutoLock scoped_lock(path_data->lock); | 147 AutoLock scoped_lock(path_data->lock); |
147 // Save the computed path in our cache. | 148 // Save the computed path in our cache. |
148 path_data->cache[key] = path; | 149 path_data->cache[key] = path; |
149 } | 150 } |
150 | 151 |
151 // TODO(brettw): this function does not handle long paths (filename > MAX_PATH) | 152 // TODO(brettw): this function does not handle long paths (filename > MAX_PATH) |
152 // characters). This isn't supported very well by Windows right now, so it is | 153 // characters). This isn't supported very well by Windows right now, so it is |
153 // moot, but we should keep this in mind for the future. | 154 // moot, but we should keep this in mind for the future. |
154 // static | 155 // static |
155 bool PathService::Get(int key, std::wstring* result) { | 156 bool PathService::Get(int key, FilePath* result) { |
156 PathData* path_data = GetPathData(); | 157 PathData* path_data = GetPathData(); |
157 DCHECK(path_data); | 158 DCHECK(path_data); |
158 DCHECK(result); | 159 DCHECK(result); |
159 DCHECK(key >= base::DIR_CURRENT); | 160 DCHECK(key >= base::DIR_CURRENT); |
160 | 161 |
161 // special case the current directory because it can never be cached | 162 // special case the current directory because it can never be cached |
162 if (key == base::DIR_CURRENT) | 163 if (key == base::DIR_CURRENT) |
163 return file_util::GetCurrentDirectory(result); | 164 return file_util::GetCurrentDirectory(result); |
164 | 165 |
165 if (GetFromCache(key, result)) | 166 if (GetFromCache(key, result)) |
166 return true; | 167 return true; |
167 | 168 |
168 std::wstring path; | 169 std::wstring path_string; |
169 | 170 |
170 // search providers for the requested path | 171 // search providers for the requested path |
171 // NOTE: it should be safe to iterate here without the lock | 172 // NOTE: it should be safe to iterate here without the lock |
172 // since RegisterProvider always prepends. | 173 // since RegisterProvider always prepends. |
173 Provider* provider = path_data->providers; | 174 Provider* provider = path_data->providers; |
174 while (provider) { | 175 while (provider) { |
175 if (provider->func(key, &path)) | 176 if (provider->func(key, &path_string)) |
176 break; | 177 break; |
177 DCHECK(path.empty()) << "provider should not have modified path"; | 178 DCHECK(path_string.empty()) << "provider should not have modified path"; |
178 provider = provider->next; | 179 provider = provider->next; |
179 } | 180 } |
180 | 181 |
181 if (path.empty()) | 182 if (path_string.empty()) |
182 return false; | 183 return false; |
183 | 184 |
| 185 FilePath path = FilePath::FromWStringHack(path_string); |
184 AddToCache(key, path); | 186 AddToCache(key, path); |
185 | 187 |
186 result->swap(path); | 188 *result = path; |
187 return true; | 189 return true; |
188 } | 190 } |
189 | 191 |
| 192 // static |
| 193 bool PathService::Get(int key, std::wstring* result) { |
| 194 // Deprecated compatibility function. |
| 195 FilePath path; |
| 196 if (!Get(key, &path)) |
| 197 return false; |
| 198 *result = path.ToWStringHack(); |
| 199 return true; |
| 200 } |
| 201 |
190 bool PathService::IsOverridden(int key) { | 202 bool PathService::IsOverridden(int key) { |
191 PathData* path_data = GetPathData(); | 203 PathData* path_data = GetPathData(); |
192 DCHECK(path_data); | 204 DCHECK(path_data); |
193 | 205 |
194 AutoLock scoped_lock(path_data->lock); | 206 AutoLock scoped_lock(path_data->lock); |
195 return path_data->overrides.find(key) != path_data->overrides.end(); | 207 return path_data->overrides.find(key) != path_data->overrides.end(); |
196 } | 208 } |
197 | 209 |
198 bool PathService::Override(int key, const std::wstring& path) { | 210 bool PathService::Override(int key, const std::wstring& path) { |
199 PathData* path_data = GetPathData(); | 211 PathData* path_data = GetPathData(); |
200 DCHECK(path_data); | 212 DCHECK(path_data); |
201 DCHECK(key > base::DIR_CURRENT) << "invalid path key"; | 213 DCHECK(key > base::DIR_CURRENT) << "invalid path key"; |
202 | 214 |
203 std::wstring file_path = path; | 215 std::wstring file_path = path; |
204 if (!file_util::AbsolutePath(&file_path)) | 216 if (!file_util::AbsolutePath(&file_path)) |
205 return false; | 217 return false; |
206 | 218 |
207 // make sure the directory exists: | 219 // make sure the directory exists: |
208 if (!file_util::CreateDirectory(file_path)) | 220 if (!file_util::CreateDirectory(file_path)) |
209 return false; | 221 return false; |
210 | 222 |
211 file_util::TrimTrailingSeparator(&file_path); | 223 file_util::TrimTrailingSeparator(&file_path); |
212 | 224 |
213 AutoLock scoped_lock(path_data->lock); | 225 AutoLock scoped_lock(path_data->lock); |
214 path_data->cache[key] = file_path; | 226 path_data->cache[key] = FilePath::FromWStringHack(file_path); |
215 path_data->overrides.insert(key); | 227 path_data->overrides.insert(key); |
216 return true; | 228 return true; |
217 } | 229 } |
218 | 230 |
219 bool PathService::SetCurrentDirectory(const std::wstring& current_directory) { | 231 bool PathService::SetCurrentDirectory(const std::wstring& current_directory) { |
220 return file_util::SetCurrentDirectory(current_directory); | 232 return file_util::SetCurrentDirectory(current_directory); |
221 } | 233 } |
222 | 234 |
223 void PathService::RegisterProvider(ProviderFunc func, int key_start, | 235 void PathService::RegisterProvider(ProviderFunc func, int key_start, |
224 int key_end) { | 236 int key_end) { |
(...skipping 17 matching lines...) Expand all Loading... |
242 p = new Provider; | 254 p = new Provider; |
243 p->is_static = false; | 255 p->is_static = false; |
244 p->func = func; | 256 p->func = func; |
245 p->next = path_data->providers; | 257 p->next = path_data->providers; |
246 #ifndef NDEBUG | 258 #ifndef NDEBUG |
247 p->key_start = key_start; | 259 p->key_start = key_start; |
248 p->key_end = key_end; | 260 p->key_end = key_end; |
249 #endif | 261 #endif |
250 path_data->providers = p; | 262 path_data->providers = p; |
251 } | 263 } |
OLD | NEW |