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 "storage/browser/fileapi/isolated_context.h" | 5 #include "storage/browser/fileapi/isolated_context.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/ptr_util.h" |
12 #include "base/rand_util.h" | 13 #include "base/rand_util.h" |
13 #include "base/stl_util.h" | |
14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
16 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
17 #include "storage/browser/fileapi/file_system_url.h" | 17 #include "storage/browser/fileapi/file_system_url.h" |
18 | 18 |
19 namespace storage { | 19 namespace storage { |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 base::FilePath::StringType GetRegisterNameForPath(const base::FilePath& path) { | 23 base::FilePath::StringType GetRegisterNameForPath(const base::FilePath& path) { |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 | 212 |
213 // static | 213 // static |
214 bool IsolatedContext::IsIsolatedType(FileSystemType type) { | 214 bool IsolatedContext::IsIsolatedType(FileSystemType type) { |
215 return type == kFileSystemTypeIsolated || type == kFileSystemTypeExternal; | 215 return type == kFileSystemTypeIsolated || type == kFileSystemTypeExternal; |
216 } | 216 } |
217 | 217 |
218 std::string IsolatedContext::RegisterDraggedFileSystem( | 218 std::string IsolatedContext::RegisterDraggedFileSystem( |
219 const FileInfoSet& files) { | 219 const FileInfoSet& files) { |
220 base::AutoLock locker(lock_); | 220 base::AutoLock locker(lock_); |
221 std::string filesystem_id = GetNewFileSystemId(); | 221 std::string filesystem_id = GetNewFileSystemId(); |
222 instance_map_[filesystem_id] = new Instance( | 222 instance_map_[filesystem_id] = |
223 kFileSystemTypeDragged, files.fileset()); | 223 base::MakeUnique<Instance>(kFileSystemTypeDragged, files.fileset()); |
224 return filesystem_id; | 224 return filesystem_id; |
225 } | 225 } |
226 | 226 |
227 std::string IsolatedContext::RegisterFileSystemForPath( | 227 std::string IsolatedContext::RegisterFileSystemForPath( |
228 FileSystemType type, | 228 FileSystemType type, |
229 const std::string& filesystem_id, | 229 const std::string& filesystem_id, |
230 const base::FilePath& path_in, | 230 const base::FilePath& path_in, |
231 std::string* register_name) { | 231 std::string* register_name) { |
232 base::FilePath path(path_in.NormalizePathSeparators()); | 232 base::FilePath path(path_in.NormalizePathSeparators()); |
233 if (path.ReferencesParent() || !path.IsAbsolute()) | 233 if (path.ReferencesParent() || !path.IsAbsolute()) |
234 return std::string(); | 234 return std::string(); |
235 std::string name; | 235 std::string name; |
236 if (register_name && !register_name->empty()) { | 236 if (register_name && !register_name->empty()) { |
237 name = *register_name; | 237 name = *register_name; |
238 } else { | 238 } else { |
239 name = base::FilePath(GetRegisterNameForPath(path)).AsUTF8Unsafe(); | 239 name = base::FilePath(GetRegisterNameForPath(path)).AsUTF8Unsafe(); |
240 if (register_name) | 240 if (register_name) |
241 register_name->assign(name); | 241 register_name->assign(name); |
242 } | 242 } |
243 | 243 |
244 base::AutoLock locker(lock_); | 244 base::AutoLock locker(lock_); |
245 std::string new_id = GetNewFileSystemId(); | 245 std::string new_id = GetNewFileSystemId(); |
246 instance_map_[new_id] = new Instance(type, filesystem_id, | 246 instance_map_[new_id] = base::MakeUnique<Instance>( |
247 MountPointInfo(name, path), | 247 type, filesystem_id, MountPointInfo(name, path), Instance::PLATFORM_PATH); |
248 Instance::PLATFORM_PATH); | |
249 path_to_id_map_[path].insert(new_id); | 248 path_to_id_map_[path].insert(new_id); |
250 return new_id; | 249 return new_id; |
251 } | 250 } |
252 | 251 |
253 std::string IsolatedContext::RegisterFileSystemForVirtualPath( | 252 std::string IsolatedContext::RegisterFileSystemForVirtualPath( |
254 FileSystemType type, | 253 FileSystemType type, |
255 const std::string& register_name, | 254 const std::string& register_name, |
256 const base::FilePath& cracked_path_prefix) { | 255 const base::FilePath& cracked_path_prefix) { |
257 base::AutoLock locker(lock_); | 256 base::AutoLock locker(lock_); |
258 base::FilePath path(cracked_path_prefix.NormalizePathSeparators()); | 257 base::FilePath path(cracked_path_prefix.NormalizePathSeparators()); |
259 if (path.ReferencesParent()) | 258 if (path.ReferencesParent()) |
260 return std::string(); | 259 return std::string(); |
261 std::string filesystem_id = GetNewFileSystemId(); | 260 std::string filesystem_id = GetNewFileSystemId(); |
262 instance_map_[filesystem_id] = new Instance( | 261 instance_map_[filesystem_id] = base::MakeUnique<Instance>( |
263 type, | 262 type, |
264 std::string(), // filesystem_id | 263 std::string(), // filesystem_id |
265 MountPointInfo(register_name, cracked_path_prefix), | 264 MountPointInfo(register_name, cracked_path_prefix), |
266 Instance::VIRTUAL_PATH); | 265 Instance::VIRTUAL_PATH); |
267 path_to_id_map_[path].insert(filesystem_id); | 266 path_to_id_map_[path].insert(filesystem_id); |
268 return filesystem_id; | 267 return filesystem_id; |
269 } | 268 } |
270 | 269 |
271 bool IsolatedContext::HandlesFileSystemMountType(FileSystemType type) const { | 270 bool IsolatedContext::HandlesFileSystemMountType(FileSystemType type) const { |
272 return type == kFileSystemTypeIsolated; | 271 return type == kFileSystemTypeIsolated; |
273 } | 272 } |
274 | 273 |
275 bool IsolatedContext::RevokeFileSystem(const std::string& filesystem_id) { | 274 bool IsolatedContext::RevokeFileSystem(const std::string& filesystem_id) { |
276 base::AutoLock locker(lock_); | 275 base::AutoLock locker(lock_); |
277 return UnregisterFileSystem(filesystem_id); | 276 return UnregisterFileSystem(filesystem_id); |
278 } | 277 } |
279 | 278 |
280 bool IsolatedContext::GetRegisteredPath( | 279 bool IsolatedContext::GetRegisteredPath( |
281 const std::string& filesystem_id, base::FilePath* path) const { | 280 const std::string& filesystem_id, base::FilePath* path) const { |
282 DCHECK(path); | 281 DCHECK(path); |
283 base::AutoLock locker(lock_); | 282 base::AutoLock locker(lock_); |
284 IDToInstance::const_iterator found = instance_map_.find(filesystem_id); | 283 auto found = instance_map_.find(filesystem_id); |
285 if (found == instance_map_.end() || !found->second->IsSinglePathInstance()) | 284 if (found == instance_map_.end() || !found->second->IsSinglePathInstance()) |
286 return false; | 285 return false; |
287 *path = found->second->file_info().path; | 286 *path = found->second->file_info().path; |
288 return true; | 287 return true; |
289 } | 288 } |
290 | 289 |
291 bool IsolatedContext::CrackVirtualPath( | 290 bool IsolatedContext::CrackVirtualPath( |
292 const base::FilePath& virtual_path, | 291 const base::FilePath& virtual_path, |
293 std::string* id_or_name, | 292 std::string* id_or_name, |
294 FileSystemType* type, | 293 FileSystemType* type, |
(...skipping 17 matching lines...) Expand all Loading... |
312 return false; | 311 return false; |
313 std::vector<base::FilePath::StringType>::iterator component_iter = | 312 std::vector<base::FilePath::StringType>::iterator component_iter = |
314 components.begin(); | 313 components.begin(); |
315 std::string fsid = base::FilePath(*component_iter++).MaybeAsASCII(); | 314 std::string fsid = base::FilePath(*component_iter++).MaybeAsASCII(); |
316 if (fsid.empty()) | 315 if (fsid.empty()) |
317 return false; | 316 return false; |
318 | 317 |
319 base::FilePath cracked_path; | 318 base::FilePath cracked_path; |
320 { | 319 { |
321 base::AutoLock locker(lock_); | 320 base::AutoLock locker(lock_); |
322 IDToInstance::const_iterator found_instance = instance_map_.find(fsid); | 321 auto found_instance = instance_map_.find(fsid); |
323 if (found_instance == instance_map_.end()) | 322 if (found_instance == instance_map_.end()) |
324 return false; | 323 return false; |
325 *id_or_name = fsid; | 324 *id_or_name = fsid; |
326 const Instance* instance = found_instance->second; | 325 const Instance* instance = found_instance->second.get(); |
327 if (type) | 326 if (type) |
328 *type = instance->type(); | 327 *type = instance->type(); |
329 if (cracked_id) | 328 if (cracked_id) |
330 *cracked_id = instance->filesystem_id(); | 329 *cracked_id = instance->filesystem_id(); |
331 | 330 |
332 if (component_iter == components.end()) { | 331 if (component_iter == components.end()) { |
333 // The virtual root case. | 332 // The virtual root case. |
334 path->clear(); | 333 path->clear(); |
335 return true; | 334 return true; |
336 } | 335 } |
(...skipping 20 matching lines...) Expand all Loading... |
357 FileSystemURL IsolatedContext::CreateCrackedFileSystemURL( | 356 FileSystemURL IsolatedContext::CreateCrackedFileSystemURL( |
358 const GURL& origin, | 357 const GURL& origin, |
359 FileSystemType type, | 358 FileSystemType type, |
360 const base::FilePath& path) const { | 359 const base::FilePath& path) const { |
361 return CrackFileSystemURL(FileSystemURL(origin, type, path)); | 360 return CrackFileSystemURL(FileSystemURL(origin, type, path)); |
362 } | 361 } |
363 | 362 |
364 void IsolatedContext::RevokeFileSystemByPath(const base::FilePath& path_in) { | 363 void IsolatedContext::RevokeFileSystemByPath(const base::FilePath& path_in) { |
365 base::AutoLock locker(lock_); | 364 base::AutoLock locker(lock_); |
366 base::FilePath path(path_in.NormalizePathSeparators()); | 365 base::FilePath path(path_in.NormalizePathSeparators()); |
367 PathToID::iterator ids_iter = path_to_id_map_.find(path); | 366 auto ids_iter = path_to_id_map_.find(path); |
368 if (ids_iter == path_to_id_map_.end()) | 367 if (ids_iter == path_to_id_map_.end()) |
369 return; | 368 return; |
370 std::set<std::string>& ids = ids_iter->second; | 369 std::set<std::string>& ids = ids_iter->second; |
371 for (std::set<std::string>::iterator iter = ids.begin(); | 370 for (auto iter = ids.begin(); iter != ids.end(); ++iter) |
372 iter != ids.end(); ++iter) { | 371 instance_map_.erase(*iter); |
373 IDToInstance::iterator found = instance_map_.find(*iter); | |
374 if (found != instance_map_.end()) { | |
375 delete found->second; | |
376 instance_map_.erase(found); | |
377 } | |
378 } | |
379 path_to_id_map_.erase(ids_iter); | 372 path_to_id_map_.erase(ids_iter); |
380 } | 373 } |
381 | 374 |
382 void IsolatedContext::AddReference(const std::string& filesystem_id) { | 375 void IsolatedContext::AddReference(const std::string& filesystem_id) { |
383 base::AutoLock locker(lock_); | 376 base::AutoLock locker(lock_); |
384 DCHECK(instance_map_.find(filesystem_id) != instance_map_.end()); | 377 DCHECK(instance_map_.find(filesystem_id) != instance_map_.end()); |
385 instance_map_[filesystem_id]->AddRef(); | 378 instance_map_[filesystem_id]->AddRef(); |
386 } | 379 } |
387 | 380 |
388 void IsolatedContext::RemoveReference(const std::string& filesystem_id) { | 381 void IsolatedContext::RemoveReference(const std::string& filesystem_id) { |
389 base::AutoLock locker(lock_); | 382 base::AutoLock locker(lock_); |
390 // This could get called for non-existent filesystem if it has been | 383 // This could get called for non-existent filesystem if it has been |
391 // already deleted by RevokeFileSystemByPath. | 384 // already deleted by RevokeFileSystemByPath. |
392 IDToInstance::iterator found = instance_map_.find(filesystem_id); | 385 auto found = instance_map_.find(filesystem_id); |
393 if (found == instance_map_.end()) | 386 if (found == instance_map_.end()) |
394 return; | 387 return; |
395 Instance* instance = found->second; | 388 Instance* instance = found->second.get(); |
396 DCHECK_GT(instance->ref_counts(), 0); | 389 DCHECK_GT(instance->ref_counts(), 0); |
397 instance->RemoveRef(); | 390 instance->RemoveRef(); |
398 if (instance->ref_counts() == 0) { | 391 if (instance->ref_counts() == 0) { |
399 bool deleted = UnregisterFileSystem(filesystem_id); | 392 bool deleted = UnregisterFileSystem(filesystem_id); |
400 DCHECK(deleted); | 393 DCHECK(deleted); |
401 } | 394 } |
402 } | 395 } |
403 | 396 |
404 bool IsolatedContext::GetDraggedFileInfo( | 397 bool IsolatedContext::GetDraggedFileInfo( |
405 const std::string& filesystem_id, | 398 const std::string& filesystem_id, |
406 std::vector<MountPointInfo>* files) const { | 399 std::vector<MountPointInfo>* files) const { |
407 DCHECK(files); | 400 DCHECK(files); |
408 base::AutoLock locker(lock_); | 401 base::AutoLock locker(lock_); |
409 IDToInstance::const_iterator found = instance_map_.find(filesystem_id); | 402 auto found = instance_map_.find(filesystem_id); |
410 if (found == instance_map_.end() || | 403 if (found == instance_map_.end() || |
411 found->second->type() != kFileSystemTypeDragged) | 404 found->second->type() != kFileSystemTypeDragged) |
412 return false; | 405 return false; |
413 files->assign(found->second->files().begin(), | 406 files->assign(found->second->files().begin(), |
414 found->second->files().end()); | 407 found->second->files().end()); |
415 return true; | 408 return true; |
416 } | 409 } |
417 | 410 |
418 base::FilePath IsolatedContext::CreateVirtualRootPath( | 411 base::FilePath IsolatedContext::CreateVirtualRootPath( |
419 const std::string& filesystem_id) const { | 412 const std::string& filesystem_id) const { |
420 return base::FilePath().AppendASCII(filesystem_id); | 413 return base::FilePath().AppendASCII(filesystem_id); |
421 } | 414 } |
422 | 415 |
423 IsolatedContext::IsolatedContext() { | 416 IsolatedContext::IsolatedContext() = default; |
424 } | |
425 | 417 |
426 IsolatedContext::~IsolatedContext() { | 418 IsolatedContext::~IsolatedContext() = default; |
427 base::STLDeleteContainerPairSecondPointers(instance_map_.begin(), | |
428 instance_map_.end()); | |
429 } | |
430 | 419 |
431 FileSystemURL IsolatedContext::CrackFileSystemURL( | 420 FileSystemURL IsolatedContext::CrackFileSystemURL( |
432 const FileSystemURL& url) const { | 421 const FileSystemURL& url) const { |
433 if (!HandlesFileSystemMountType(url.type())) | 422 if (!HandlesFileSystemMountType(url.type())) |
434 return FileSystemURL(); | 423 return FileSystemURL(); |
435 | 424 |
436 std::string mount_name; | 425 std::string mount_name; |
437 std::string cracked_mount_name; | 426 std::string cracked_mount_name; |
438 FileSystemType cracked_type; | 427 FileSystemType cracked_type; |
439 base::FilePath cracked_path; | 428 base::FilePath cracked_path; |
440 FileSystemMountOption cracked_mount_option; | 429 FileSystemMountOption cracked_mount_option; |
441 if (!CrackVirtualPath(url.path(), &mount_name, &cracked_type, | 430 if (!CrackVirtualPath(url.path(), &mount_name, &cracked_type, |
442 &cracked_mount_name, &cracked_path, | 431 &cracked_mount_name, &cracked_path, |
443 &cracked_mount_option)) { | 432 &cracked_mount_option)) { |
444 return FileSystemURL(); | 433 return FileSystemURL(); |
445 } | 434 } |
446 | 435 |
447 return FileSystemURL( | 436 return FileSystemURL( |
448 url.origin(), url.mount_type(), url.virtual_path(), | 437 url.origin(), url.mount_type(), url.virtual_path(), |
449 !url.filesystem_id().empty() ? url.filesystem_id() : mount_name, | 438 !url.filesystem_id().empty() ? url.filesystem_id() : mount_name, |
450 cracked_type, cracked_path, | 439 cracked_type, cracked_path, |
451 cracked_mount_name.empty() ? mount_name : cracked_mount_name, | 440 cracked_mount_name.empty() ? mount_name : cracked_mount_name, |
452 cracked_mount_option); | 441 cracked_mount_option); |
453 } | 442 } |
454 | 443 |
455 bool IsolatedContext::UnregisterFileSystem(const std::string& filesystem_id) { | 444 bool IsolatedContext::UnregisterFileSystem(const std::string& filesystem_id) { |
456 lock_.AssertAcquired(); | 445 lock_.AssertAcquired(); |
457 IDToInstance::iterator found = instance_map_.find(filesystem_id); | 446 auto found = instance_map_.find(filesystem_id); |
458 if (found == instance_map_.end()) | 447 if (found == instance_map_.end()) |
459 return false; | 448 return false; |
460 Instance* instance = found->second; | 449 Instance* instance = found->second.get(); |
461 if (instance->IsSinglePathInstance()) { | 450 if (instance->IsSinglePathInstance()) { |
462 PathToID::iterator ids_iter = path_to_id_map_.find( | 451 auto ids_iter = path_to_id_map_.find(instance->file_info().path); |
463 instance->file_info().path); | |
464 DCHECK(ids_iter != path_to_id_map_.end()); | 452 DCHECK(ids_iter != path_to_id_map_.end()); |
465 ids_iter->second.erase(filesystem_id); | 453 ids_iter->second.erase(filesystem_id); |
466 if (ids_iter->second.empty()) | 454 if (ids_iter->second.empty()) |
467 path_to_id_map_.erase(ids_iter); | 455 path_to_id_map_.erase(ids_iter); |
468 } | 456 } |
469 delete found->second; | |
470 instance_map_.erase(found); | 457 instance_map_.erase(found); |
471 return true; | 458 return true; |
472 } | 459 } |
473 | 460 |
474 std::string IsolatedContext::GetNewFileSystemId() const { | 461 std::string IsolatedContext::GetNewFileSystemId() const { |
475 // Returns an arbitrary random string which must be unique in the map. | 462 // Returns an arbitrary random string which must be unique in the map. |
476 lock_.AssertAcquired(); | 463 lock_.AssertAcquired(); |
477 uint32_t random_data[4]; | 464 uint32_t random_data[4]; |
478 std::string id; | 465 std::string id; |
479 do { | 466 do { |
480 base::RandBytes(random_data, sizeof(random_data)); | 467 base::RandBytes(random_data, sizeof(random_data)); |
481 id = base::HexEncode(random_data, sizeof(random_data)); | 468 id = base::HexEncode(random_data, sizeof(random_data)); |
482 } while (instance_map_.find(id) != instance_map_.end()); | 469 } while (instance_map_.find(id) != instance_map_.end()); |
483 return id; | 470 return id; |
484 } | 471 } |
485 | 472 |
486 } // namespace storage | 473 } // namespace storage |
OLD | NEW |