OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "webkit/fileapi/external_mount_points.h" | 5 #include "webkit/fileapi/external_mount_points.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 return false; | 122 return false; |
123 | 123 |
124 instance_map_[mount_name] = new Instance(type, path, remote_proxy); | 124 instance_map_[mount_name] = new Instance(type, path, remote_proxy); |
125 if (!path.empty()) | 125 if (!path.empty()) |
126 path_to_name_map_.insert(std::make_pair(path, mount_name)); | 126 path_to_name_map_.insert(std::make_pair(path, mount_name)); |
127 return true; | 127 return true; |
128 } | 128 } |
129 | 129 |
130 bool ExternalMountPoints::HandlesFileSystemMountType( | 130 bool ExternalMountPoints::HandlesFileSystemMountType( |
131 FileSystemType type) const { | 131 FileSystemType type) const { |
132 return type == kFileSystemTypeExternal; | 132 return type == kFileSystemTypeExternal || |
| 133 type == kFileSystemTypeNativeForPlatformApp; |
133 } | 134 } |
134 | 135 |
135 bool ExternalMountPoints::RevokeFileSystem(const std::string& mount_name) { | 136 bool ExternalMountPoints::RevokeFileSystem(const std::string& mount_name) { |
136 base::AutoLock locker(lock_); | 137 base::AutoLock locker(lock_); |
137 NameToInstance::iterator found = instance_map_.find(mount_name); | 138 NameToInstance::iterator found = instance_map_.find(mount_name); |
138 if (found == instance_map_.end()) | 139 if (found == instance_map_.end()) |
139 return false; | 140 return false; |
140 Instance* instance = found->second; | 141 Instance* instance = found->second; |
141 path_to_name_map_.erase(NormalizeFilePath(instance->path())); | 142 path_to_name_map_.erase(NormalizeFilePath(instance->path())); |
142 delete found->second; | 143 delete found->second; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 for (; component_iter != components.end(); ++component_iter) | 197 for (; component_iter != components.end(); ++component_iter) |
197 cracked_path = cracked_path.Append(*component_iter); | 198 cracked_path = cracked_path.Append(*component_iter); |
198 *path = cracked_path; | 199 *path = cracked_path; |
199 return true; | 200 return true; |
200 } | 201 } |
201 | 202 |
202 FileSystemURL ExternalMountPoints::CrackURL(const GURL& url) const { | 203 FileSystemURL ExternalMountPoints::CrackURL(const GURL& url) const { |
203 FileSystemURL filesystem_url = FileSystemURL(url); | 204 FileSystemURL filesystem_url = FileSystemURL(url); |
204 if (!filesystem_url.is_valid()) | 205 if (!filesystem_url.is_valid()) |
205 return FileSystemURL(); | 206 return FileSystemURL(); |
206 return CreateCrackedFileSystemURL(filesystem_url.origin(), | 207 return CrackFileSystemURL(filesystem_url); |
207 filesystem_url.mount_type(), | |
208 filesystem_url.path()); | |
209 } | 208 } |
210 | 209 |
211 FileSystemURL ExternalMountPoints::CreateCrackedFileSystemURL( | 210 FileSystemURL ExternalMountPoints::CreateCrackedFileSystemURL( |
212 const GURL& origin, | 211 const GURL& origin, |
213 FileSystemType type, | 212 FileSystemType type, |
214 const base::FilePath& path) const { | 213 const base::FilePath& path) const { |
215 if (!HandlesFileSystemMountType(type)) | 214 return CrackFileSystemURL(FileSystemURL(origin, type, path)); |
216 return FileSystemURL(); | |
217 | |
218 std::string mount_name; | |
219 FileSystemType cracked_type; | |
220 base::FilePath cracked_path; | |
221 if (!CrackVirtualPath(path, &mount_name, &cracked_type, &cracked_path)) | |
222 return FileSystemURL(); | |
223 | |
224 return FileSystemURL(origin, type, path, | |
225 mount_name, cracked_type, cracked_path); | |
226 } | 215 } |
227 | 216 |
228 RemoteFileSystemProxyInterface* ExternalMountPoints::GetRemoteFileSystemProxy( | 217 RemoteFileSystemProxyInterface* ExternalMountPoints::GetRemoteFileSystemProxy( |
229 const std::string& mount_name) const { | 218 const std::string& mount_name) const { |
230 base::AutoLock locker(lock_); | 219 base::AutoLock locker(lock_); |
231 NameToInstance::const_iterator found = instance_map_.find(mount_name); | 220 NameToInstance::const_iterator found = instance_map_.find(mount_name); |
232 if (found == instance_map_.end()) | 221 if (found == instance_map_.end()) |
233 return NULL; | 222 return NULL; |
234 return found->second->remote_proxy(); | 223 return found->second->remote_proxy(); |
235 } | 224 } |
236 | 225 |
237 void ExternalMountPoints::AddMountPointInfosTo( | 226 void ExternalMountPoints::AddMountPointInfosTo( |
238 std::vector<MountPointInfo>* mount_points) const { | 227 std::vector<MountPointInfo>* mount_points) const { |
239 base::AutoLock locker(lock_); | 228 base::AutoLock locker(lock_); |
240 DCHECK(mount_points); | 229 DCHECK(mount_points); |
241 for (NameToInstance::const_iterator iter = instance_map_.begin(); | 230 for (NameToInstance::const_iterator iter = instance_map_.begin(); |
242 iter != instance_map_.end(); ++iter) { | 231 iter != instance_map_.end(); ++iter) { |
243 mount_points->push_back(MountPointInfo(iter->first, iter->second->path())); | 232 mount_points->push_back(MountPointInfo(iter->first, iter->second->path())); |
244 } | 233 } |
245 } | 234 } |
246 | 235 |
247 bool ExternalMountPoints::GetVirtualPath(const base::FilePath& path_in, | 236 bool ExternalMountPoints::GetVirtualPath(const base::FilePath& path_in, |
248 base::FilePath* virtual_path) { | 237 base::FilePath* virtual_path) const { |
249 DCHECK(virtual_path); | 238 DCHECK(virtual_path); |
250 | 239 |
251 base::AutoLock locker(lock_); | 240 base::AutoLock locker(lock_); |
252 | 241 |
253 base::FilePath path = NormalizeFilePath(path_in); | 242 base::FilePath path = NormalizeFilePath(path_in); |
254 std::map<base::FilePath, std::string>::reverse_iterator iter( | 243 std::map<base::FilePath, std::string>::const_reverse_iterator iter( |
255 path_to_name_map_.upper_bound(path)); | 244 path_to_name_map_.upper_bound(path)); |
256 if (iter == path_to_name_map_.rend()) | 245 if (iter == path_to_name_map_.rend()) |
257 return false; | 246 return false; |
258 | 247 |
259 *virtual_path = CreateVirtualRootPath(iter->second); | 248 *virtual_path = CreateVirtualRootPath(iter->second); |
260 if (iter->first == path) | 249 if (iter->first == path) |
261 return true; | 250 return true; |
262 return iter->first.AppendRelativePath(path, virtual_path); | 251 return iter->first.AppendRelativePath(path, virtual_path); |
263 } | 252 } |
264 | 253 |
265 base::FilePath ExternalMountPoints::CreateVirtualRootPath( | 254 base::FilePath ExternalMountPoints::CreateVirtualRootPath( |
266 const std::string& mount_name) const { | 255 const std::string& mount_name) const { |
267 return base::FilePath().AppendASCII(mount_name); | 256 return base::FilePath().AppendASCII(mount_name); |
268 } | 257 } |
269 | 258 |
270 ExternalMountPoints::ExternalMountPoints() {} | 259 ExternalMountPoints::ExternalMountPoints() {} |
271 | 260 |
272 ExternalMountPoints::~ExternalMountPoints() { | 261 ExternalMountPoints::~ExternalMountPoints() { |
273 STLDeleteContainerPairSecondPointers(instance_map_.begin(), | 262 STLDeleteContainerPairSecondPointers(instance_map_.begin(), |
274 instance_map_.end()); | 263 instance_map_.end()); |
275 } | 264 } |
276 | 265 |
| 266 FileSystemURL ExternalMountPoints::CrackFileSystemURL( |
| 267 const FileSystemURL& url) const { |
| 268 if (!HandlesFileSystemMountType(url.type())) |
| 269 return FileSystemURL(); |
| 270 |
| 271 base::FilePath virtual_path = url.path(); |
| 272 if (url.type() == kFileSystemTypeNativeForPlatformApp) { |
| 273 #if defined(OS_CHROMEOS) |
| 274 // On Chrome OS, find a mount point and virtual path for the external fs. |
| 275 if (!GetVirtualPath(url.path(), &virtual_path)) |
| 276 return FileSystemURL(); |
| 277 #else |
| 278 // On other OS, it is simply a native local path. |
| 279 return FileSystemURL( |
| 280 url.origin(), url.mount_type(), url.virtual_path(), |
| 281 url.mount_filesystem_id(), kFileSystemTypeNativeLocal, |
| 282 url.path(), url.filesystem_id()); |
| 283 #endif |
| 284 } |
| 285 |
| 286 std::string mount_name; |
| 287 FileSystemType cracked_type; |
| 288 base::FilePath cracked_path; |
| 289 |
| 290 if (!CrackVirtualPath(virtual_path, &mount_name, &cracked_type, |
| 291 &cracked_path)) { |
| 292 return FileSystemURL(); |
| 293 } |
| 294 |
| 295 return FileSystemURL( |
| 296 url.origin(), url.mount_type(), url.virtual_path(), |
| 297 !url.filesystem_id().empty() ? url.filesystem_id() : mount_name, |
| 298 cracked_type, cracked_path, mount_name); |
| 299 } |
| 300 |
277 bool ExternalMountPoints::ValidateNewMountPoint(const std::string& mount_name, | 301 bool ExternalMountPoints::ValidateNewMountPoint(const std::string& mount_name, |
278 const base::FilePath& path) { | 302 const base::FilePath& path) { |
279 lock_.AssertAcquired(); | 303 lock_.AssertAcquired(); |
280 | 304 |
281 // Mount name must not be empty. | 305 // Mount name must not be empty. |
282 if (mount_name.empty()) | 306 if (mount_name.empty()) |
283 return false; | 307 return false; |
284 | 308 |
285 // Verify there is no registered mount point with the same name. | 309 // Verify there is no registered mount point with the same name. |
286 NameToInstance::iterator found = instance_map_.find(mount_name); | 310 NameToInstance::iterator found = instance_map_.find(mount_name); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 base::FilePath ScopedExternalFileSystem::GetVirtualRootPath() const { | 349 base::FilePath ScopedExternalFileSystem::GetVirtualRootPath() const { |
326 return ExternalMountPoints::GetSystemInstance()-> | 350 return ExternalMountPoints::GetSystemInstance()-> |
327 CreateVirtualRootPath(mount_name_); | 351 CreateVirtualRootPath(mount_name_); |
328 } | 352 } |
329 | 353 |
330 ScopedExternalFileSystem::~ScopedExternalFileSystem() { | 354 ScopedExternalFileSystem::~ScopedExternalFileSystem() { |
331 ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(mount_name_); | 355 ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(mount_name_); |
332 } | 356 } |
333 | 357 |
334 } // namespace fileapi | 358 } // namespace fileapi |
OLD | NEW |