OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/browser/fileapi/isolated_context.h" | 5 #include "webkit/browser/fileapi/isolated_context.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/rand_util.h" | 10 #include "base/rand_util.h" |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 public: | 99 public: |
100 enum PathType { | 100 enum PathType { |
101 PLATFORM_PATH, | 101 PLATFORM_PATH, |
102 VIRTUAL_PATH | 102 VIRTUAL_PATH |
103 }; | 103 }; |
104 | 104 |
105 // For a single-path isolated file system, which could be registered by | 105 // For a single-path isolated file system, which could be registered by |
106 // IsolatedContext::RegisterFileSystemForPath() or | 106 // IsolatedContext::RegisterFileSystemForPath() or |
107 // IsolatedContext::RegisterFileSystemForVirtualPath(). | 107 // IsolatedContext::RegisterFileSystemForVirtualPath(). |
108 // Most of isolated file system contexts should be of this type. | 108 // Most of isolated file system contexts should be of this type. |
109 Instance(FileSystemType type, const MountPointInfo& file_info, | 109 Instance(FileSystemType type, |
| 110 const std::string& filesystem_id, |
| 111 const MountPointInfo& file_info, |
110 PathType path_type); | 112 PathType path_type); |
111 | 113 |
112 // For a multi-paths isolated file system. As of writing only file system | 114 // For a multi-paths isolated file system. As of writing only file system |
113 // type which could have multi-paths is Dragged file system, and | 115 // type which could have multi-paths is Dragged file system, and |
114 // could be registered by IsolatedContext::RegisterDraggedFileSystem(). | 116 // could be registered by IsolatedContext::RegisterDraggedFileSystem(). |
115 Instance(FileSystemType type, const std::set<MountPointInfo>& files); | 117 Instance(FileSystemType type, const std::set<MountPointInfo>& files); |
116 | 118 |
117 ~Instance(); | 119 ~Instance(); |
118 | 120 |
119 FileSystemType type() const { return type_; } | 121 FileSystemType type() const { return type_; } |
| 122 const std::string& filesystem_id() const { return filesystem_id_; } |
120 const MountPointInfo& file_info() const { return file_info_; } | 123 const MountPointInfo& file_info() const { return file_info_; } |
121 const std::set<MountPointInfo>& files() const { return files_; } | 124 const std::set<MountPointInfo>& files() const { return files_; } |
122 int ref_counts() const { return ref_counts_; } | 125 int ref_counts() const { return ref_counts_; } |
123 | 126 |
124 void AddRef() { ++ref_counts_; } | 127 void AddRef() { ++ref_counts_; } |
125 void RemoveRef() { --ref_counts_; } | 128 void RemoveRef() { --ref_counts_; } |
126 | 129 |
127 bool ResolvePathForName(const std::string& name, base::FilePath* path) const; | 130 bool ResolvePathForName(const std::string& name, base::FilePath* path) const; |
128 | 131 |
129 // Returns true if the instance is a single-path instance. | 132 // Returns true if the instance is a single-path instance. |
130 bool IsSinglePathInstance() const; | 133 bool IsSinglePathInstance() const; |
131 | 134 |
132 private: | 135 private: |
133 const FileSystemType type_; | 136 const FileSystemType type_; |
| 137 const std::string filesystem_id_; |
134 | 138 |
135 // For single-path instance. | 139 // For single-path instance. |
136 const MountPointInfo file_info_; | 140 const MountPointInfo file_info_; |
137 const PathType path_type_; | 141 const PathType path_type_; |
138 | 142 |
139 // For multiple-path instance (e.g. dragged file system). | 143 // For multiple-path instance (e.g. dragged file system). |
140 const std::set<MountPointInfo> files_; | 144 const std::set<MountPointInfo> files_; |
141 | 145 |
142 // Reference counts. Note that an isolated filesystem is created with ref==0 | 146 // Reference counts. Note that an isolated filesystem is created with ref==0 |
143 // and will get deleted when the ref count reaches <=0. | 147 // and will get deleted when the ref count reaches <=0. |
144 int ref_counts_; | 148 int ref_counts_; |
145 | 149 |
146 DISALLOW_COPY_AND_ASSIGN(Instance); | 150 DISALLOW_COPY_AND_ASSIGN(Instance); |
147 }; | 151 }; |
148 | 152 |
149 IsolatedContext::Instance::Instance(FileSystemType type, | 153 IsolatedContext::Instance::Instance(FileSystemType type, |
| 154 const std::string& filesystem_id, |
150 const MountPointInfo& file_info, | 155 const MountPointInfo& file_info, |
151 Instance::PathType path_type) | 156 Instance::PathType path_type) |
152 : type_(type), | 157 : type_(type), |
| 158 filesystem_id_(filesystem_id), |
153 file_info_(file_info), | 159 file_info_(file_info), |
154 path_type_(path_type), | 160 path_type_(path_type), |
155 ref_counts_(0) { | 161 ref_counts_(0) { |
156 DCHECK(IsSinglePathIsolatedFileSystem(type_)); | 162 DCHECK(IsSinglePathIsolatedFileSystem(type_)); |
157 } | 163 } |
158 | 164 |
159 IsolatedContext::Instance::Instance(FileSystemType type, | 165 IsolatedContext::Instance::Instance(FileSystemType type, |
160 const std::set<MountPointInfo>& files) | 166 const std::set<MountPointInfo>& files) |
161 : type_(type), | 167 : type_(type), |
162 path_type_(PLATFORM_PATH), | 168 path_type_(PLATFORM_PATH), |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 const FileInfoSet& files) { | 217 const FileInfoSet& files) { |
212 base::AutoLock locker(lock_); | 218 base::AutoLock locker(lock_); |
213 std::string filesystem_id = GetNewFileSystemId(); | 219 std::string filesystem_id = GetNewFileSystemId(); |
214 instance_map_[filesystem_id] = new Instance( | 220 instance_map_[filesystem_id] = new Instance( |
215 kFileSystemTypeDragged, files.fileset()); | 221 kFileSystemTypeDragged, files.fileset()); |
216 return filesystem_id; | 222 return filesystem_id; |
217 } | 223 } |
218 | 224 |
219 std::string IsolatedContext::RegisterFileSystemForPath( | 225 std::string IsolatedContext::RegisterFileSystemForPath( |
220 FileSystemType type, | 226 FileSystemType type, |
| 227 const std::string& filesystem_id, |
221 const base::FilePath& path_in, | 228 const base::FilePath& path_in, |
222 std::string* register_name) { | 229 std::string* register_name) { |
223 base::FilePath path(path_in.NormalizePathSeparators()); | 230 base::FilePath path(path_in.NormalizePathSeparators()); |
224 if (path.ReferencesParent() || !path.IsAbsolute()) | 231 if (path.ReferencesParent() || !path.IsAbsolute()) |
225 return std::string(); | 232 return std::string(); |
226 std::string name; | 233 std::string name; |
227 if (register_name && !register_name->empty()) { | 234 if (register_name && !register_name->empty()) { |
228 name = *register_name; | 235 name = *register_name; |
229 } else { | 236 } else { |
230 name = base::FilePath(GetRegisterNameForPath(path)).AsUTF8Unsafe(); | 237 name = base::FilePath(GetRegisterNameForPath(path)).AsUTF8Unsafe(); |
231 if (register_name) | 238 if (register_name) |
232 register_name->assign(name); | 239 register_name->assign(name); |
233 } | 240 } |
234 | 241 |
235 base::AutoLock locker(lock_); | 242 base::AutoLock locker(lock_); |
236 std::string filesystem_id = GetNewFileSystemId(); | 243 std::string new_id = GetNewFileSystemId(); |
237 instance_map_[filesystem_id] = new Instance(type, MountPointInfo(name, path), | 244 instance_map_[new_id] = new Instance(type, filesystem_id, |
238 Instance::PLATFORM_PATH); | 245 MountPointInfo(name, path), |
239 path_to_id_map_[path].insert(filesystem_id); | 246 Instance::PLATFORM_PATH); |
240 return filesystem_id; | 247 path_to_id_map_[path].insert(new_id); |
| 248 return new_id; |
241 } | 249 } |
242 | 250 |
243 std::string IsolatedContext::RegisterFileSystemForVirtualPath( | 251 std::string IsolatedContext::RegisterFileSystemForVirtualPath( |
244 FileSystemType type, | 252 FileSystemType type, |
245 const std::string& register_name, | 253 const std::string& register_name, |
246 const base::FilePath& cracked_path_prefix) { | 254 const base::FilePath& cracked_path_prefix) { |
247 base::AutoLock locker(lock_); | 255 base::AutoLock locker(lock_); |
248 base::FilePath path(cracked_path_prefix.NormalizePathSeparators()); | 256 base::FilePath path(cracked_path_prefix.NormalizePathSeparators()); |
249 if (path.ReferencesParent()) | 257 if (path.ReferencesParent()) |
250 return std::string(); | 258 return std::string(); |
251 std::string filesystem_id = GetNewFileSystemId(); | 259 std::string filesystem_id = GetNewFileSystemId(); |
252 instance_map_[filesystem_id] = new Instance( | 260 instance_map_[filesystem_id] = new Instance( |
253 type, | 261 type, |
| 262 std::string(), // filesystem_id |
254 MountPointInfo(register_name, cracked_path_prefix), | 263 MountPointInfo(register_name, cracked_path_prefix), |
255 Instance::VIRTUAL_PATH); | 264 Instance::VIRTUAL_PATH); |
256 path_to_id_map_[path].insert(filesystem_id); | 265 path_to_id_map_[path].insert(filesystem_id); |
257 return filesystem_id; | 266 return filesystem_id; |
258 } | 267 } |
259 | 268 |
260 bool IsolatedContext::HandlesFileSystemMountType(FileSystemType type) const { | 269 bool IsolatedContext::HandlesFileSystemMountType(FileSystemType type) const { |
261 return type == kFileSystemTypeIsolated; | 270 return type == kFileSystemTypeIsolated; |
262 } | 271 } |
263 | 272 |
(...skipping 10 matching lines...) Expand all Loading... |
274 if (found == instance_map_.end() || !found->second->IsSinglePathInstance()) | 283 if (found == instance_map_.end() || !found->second->IsSinglePathInstance()) |
275 return false; | 284 return false; |
276 *path = found->second->file_info().path; | 285 *path = found->second->file_info().path; |
277 return true; | 286 return true; |
278 } | 287 } |
279 | 288 |
280 bool IsolatedContext::CrackVirtualPath( | 289 bool IsolatedContext::CrackVirtualPath( |
281 const base::FilePath& virtual_path, | 290 const base::FilePath& virtual_path, |
282 std::string* id_or_name, | 291 std::string* id_or_name, |
283 FileSystemType* type, | 292 FileSystemType* type, |
| 293 std::string* cracked_id, |
284 base::FilePath* path, | 294 base::FilePath* path, |
285 FileSystemMountOption* mount_option) const { | 295 FileSystemMountOption* mount_option) const { |
286 DCHECK(id_or_name); | 296 DCHECK(id_or_name); |
287 DCHECK(path); | 297 DCHECK(path); |
288 | 298 |
289 // This should not contain any '..' references. | 299 // This should not contain any '..' references. |
290 if (virtual_path.ReferencesParent()) | 300 if (virtual_path.ReferencesParent()) |
291 return false; | 301 return false; |
292 | 302 |
293 // Set the default mount option. | 303 // Set the default mount option. |
(...skipping 13 matching lines...) Expand all Loading... |
307 base::FilePath cracked_path; | 317 base::FilePath cracked_path; |
308 { | 318 { |
309 base::AutoLock locker(lock_); | 319 base::AutoLock locker(lock_); |
310 IDToInstance::const_iterator found_instance = instance_map_.find(fsid); | 320 IDToInstance::const_iterator found_instance = instance_map_.find(fsid); |
311 if (found_instance == instance_map_.end()) | 321 if (found_instance == instance_map_.end()) |
312 return false; | 322 return false; |
313 *id_or_name = fsid; | 323 *id_or_name = fsid; |
314 const Instance* instance = found_instance->second; | 324 const Instance* instance = found_instance->second; |
315 if (type) | 325 if (type) |
316 *type = instance->type(); | 326 *type = instance->type(); |
| 327 if (cracked_id) |
| 328 *cracked_id = instance->filesystem_id(); |
317 | 329 |
318 if (component_iter == components.end()) { | 330 if (component_iter == components.end()) { |
319 // The virtual root case. | 331 // The virtual root case. |
320 path->clear(); | 332 path->clear(); |
321 return true; | 333 return true; |
322 } | 334 } |
323 | 335 |
324 // *component_iter should be a name of the registered path. | 336 // *component_iter should be a name of the registered path. |
325 std::string name = base::FilePath(*component_iter++).AsUTF8Unsafe(); | 337 std::string name = base::FilePath(*component_iter++).AsUTF8Unsafe(); |
326 if (!instance->ResolvePathForName(name, &cracked_path)) | 338 if (!instance->ResolvePathForName(name, &cracked_path)) |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
413 STLDeleteContainerPairSecondPointers(instance_map_.begin(), | 425 STLDeleteContainerPairSecondPointers(instance_map_.begin(), |
414 instance_map_.end()); | 426 instance_map_.end()); |
415 } | 427 } |
416 | 428 |
417 FileSystemURL IsolatedContext::CrackFileSystemURL( | 429 FileSystemURL IsolatedContext::CrackFileSystemURL( |
418 const FileSystemURL& url) const { | 430 const FileSystemURL& url) const { |
419 if (!HandlesFileSystemMountType(url.type())) | 431 if (!HandlesFileSystemMountType(url.type())) |
420 return FileSystemURL(); | 432 return FileSystemURL(); |
421 | 433 |
422 std::string mount_name; | 434 std::string mount_name; |
| 435 std::string cracked_mount_name; |
423 FileSystemType cracked_type; | 436 FileSystemType cracked_type; |
424 base::FilePath cracked_path; | 437 base::FilePath cracked_path; |
425 FileSystemMountOption cracked_mount_option; | 438 FileSystemMountOption cracked_mount_option; |
426 if (!CrackVirtualPath(url.path(), &mount_name, &cracked_type, | 439 if (!CrackVirtualPath(url.path(), &mount_name, &cracked_type, |
427 &cracked_path, &cracked_mount_option)) { | 440 &cracked_mount_name, &cracked_path, |
| 441 &cracked_mount_option)) { |
428 return FileSystemURL(); | 442 return FileSystemURL(); |
429 } | 443 } |
430 | 444 |
431 return FileSystemURL( | 445 return FileSystemURL( |
432 url.origin(), url.mount_type(), url.virtual_path(), | 446 url.origin(), url.mount_type(), url.virtual_path(), |
433 !url.filesystem_id().empty() ? url.filesystem_id() : mount_name, | 447 !url.filesystem_id().empty() ? url.filesystem_id() : mount_name, |
434 cracked_type, cracked_path, mount_name, cracked_mount_option); | 448 cracked_type, cracked_path, |
| 449 cracked_mount_name.empty() ? mount_name : cracked_mount_name, |
| 450 cracked_mount_option); |
435 } | 451 } |
436 | 452 |
437 bool IsolatedContext::UnregisterFileSystem(const std::string& filesystem_id) { | 453 bool IsolatedContext::UnregisterFileSystem(const std::string& filesystem_id) { |
438 lock_.AssertAcquired(); | 454 lock_.AssertAcquired(); |
439 IDToInstance::iterator found = instance_map_.find(filesystem_id); | 455 IDToInstance::iterator found = instance_map_.find(filesystem_id); |
440 if (found == instance_map_.end()) | 456 if (found == instance_map_.end()) |
441 return false; | 457 return false; |
442 Instance* instance = found->second; | 458 Instance* instance = found->second; |
443 if (instance->IsSinglePathInstance()) { | 459 if (instance->IsSinglePathInstance()) { |
444 PathToID::iterator ids_iter = path_to_id_map_.find( | 460 PathToID::iterator ids_iter = path_to_id_map_.find( |
(...skipping 14 matching lines...) Expand all Loading... |
459 uint32 random_data[4]; | 475 uint32 random_data[4]; |
460 std::string id; | 476 std::string id; |
461 do { | 477 do { |
462 base::RandBytes(random_data, sizeof(random_data)); | 478 base::RandBytes(random_data, sizeof(random_data)); |
463 id = base::HexEncode(random_data, sizeof(random_data)); | 479 id = base::HexEncode(random_data, sizeof(random_data)); |
464 } while (instance_map_.find(id) != instance_map_.end()); | 480 } while (instance_map_.find(id) != instance_map_.end()); |
465 return id; | 481 return id; |
466 } | 482 } |
467 | 483 |
468 } // namespace fileapi | 484 } // namespace fileapi |
OLD | NEW |