| 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/obfuscated_file_util.h" | 5 #include "storage/browser/fileapi/obfuscated_file_util.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <queue> | 10 #include <queue> |
| 11 #include <tuple> |
| 11 | 12 |
| 12 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 13 #include "base/format_macros.h" | 14 #include "base/format_macros.h" |
| 14 #include "base/logging.h" | 15 #include "base/logging.h" |
| 15 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
| 16 #include "base/metrics/histogram.h" | 17 #include "base/metrics/histogram.h" |
| 17 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
| 18 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
| 19 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
| 20 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 if (growth > 0 && new_quota < 0) | 85 if (growth > 0 && new_quota < 0) |
| 85 return false; | 86 return false; |
| 86 context->set_allowed_bytes_growth(new_quota); | 87 context->set_allowed_bytes_growth(new_quota); |
| 87 return true; | 88 return true; |
| 88 } | 89 } |
| 89 | 90 |
| 90 void UpdateUsage(FileSystemOperationContext* context, | 91 void UpdateUsage(FileSystemOperationContext* context, |
| 91 const FileSystemURL& url, | 92 const FileSystemURL& url, |
| 92 int64_t growth) { | 93 int64_t growth) { |
| 93 context->update_observers()->Notify( | 94 context->update_observers()->Notify( |
| 94 &FileUpdateObserver::OnUpdate, base::MakeTuple(url, growth)); | 95 &FileUpdateObserver::OnUpdate, std::make_tuple(url, growth)); |
| 95 } | 96 } |
| 96 | 97 |
| 97 void TouchDirectory(SandboxDirectoryDatabase* db, FileId dir_id) { | 98 void TouchDirectory(SandboxDirectoryDatabase* db, FileId dir_id) { |
| 98 DCHECK(db); | 99 DCHECK(db); |
| 99 if (!db->UpdateModificationTime(dir_id, base::Time::Now())) | 100 if (!db->UpdateModificationTime(dir_id, base::Time::Now())) |
| 100 NOTREACHED(); | 101 NOTREACHED(); |
| 101 } | 102 } |
| 102 | 103 |
| 103 enum IsolatedOriginStatus { | 104 enum IsolatedOriginStatus { |
| 104 kIsolatedOriginMatch, | 105 kIsolatedOriginMatch, |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 315 | 316 |
| 316 int64_t growth = UsageForPath(file_info.name.size()); | 317 int64_t growth = UsageForPath(file_info.name.size()); |
| 317 if (!AllocateQuota(context, growth)) | 318 if (!AllocateQuota(context, growth)) |
| 318 return base::File::FILE_ERROR_NO_SPACE; | 319 return base::File::FILE_ERROR_NO_SPACE; |
| 319 base::File::Error error = CreateFile(context, base::FilePath(), url, | 320 base::File::Error error = CreateFile(context, base::FilePath(), url, |
| 320 &file_info); | 321 &file_info); |
| 321 if (created && base::File::FILE_OK == error) { | 322 if (created && base::File::FILE_OK == error) { |
| 322 *created = true; | 323 *created = true; |
| 323 UpdateUsage(context, url, growth); | 324 UpdateUsage(context, url, growth); |
| 324 context->change_observers()->Notify( | 325 context->change_observers()->Notify( |
| 325 &FileChangeObserver::OnCreateFile, base::MakeTuple(url)); | 326 &FileChangeObserver::OnCreateFile, std::make_tuple(url)); |
| 326 } | 327 } |
| 327 return error; | 328 return error; |
| 328 } | 329 } |
| 329 | 330 |
| 330 base::File::Error ObfuscatedFileUtil::CreateDirectory( | 331 base::File::Error ObfuscatedFileUtil::CreateDirectory( |
| 331 FileSystemOperationContext* context, | 332 FileSystemOperationContext* context, |
| 332 const FileSystemURL& url, | 333 const FileSystemURL& url, |
| 333 bool exclusive, | 334 bool exclusive, |
| 334 bool recursive) { | 335 bool recursive) { |
| 335 SandboxDirectoryDatabase* db = GetDirectoryDatabase(url, true); | 336 SandboxDirectoryDatabase* db = GetDirectoryDatabase(url, true); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 file_info.modification_time = base::Time::Now(); | 375 file_info.modification_time = base::Time::Now(); |
| 375 file_info.parent_id = parent_id; | 376 file_info.parent_id = parent_id; |
| 376 int64_t growth = UsageForPath(file_info.name.size()); | 377 int64_t growth = UsageForPath(file_info.name.size()); |
| 377 if (!AllocateQuota(context, growth)) | 378 if (!AllocateQuota(context, growth)) |
| 378 return base::File::FILE_ERROR_NO_SPACE; | 379 return base::File::FILE_ERROR_NO_SPACE; |
| 379 base::File::Error error = db->AddFileInfo(file_info, &parent_id); | 380 base::File::Error error = db->AddFileInfo(file_info, &parent_id); |
| 380 if (error != base::File::FILE_OK) | 381 if (error != base::File::FILE_OK) |
| 381 return error; | 382 return error; |
| 382 UpdateUsage(context, url, growth); | 383 UpdateUsage(context, url, growth); |
| 383 context->change_observers()->Notify( | 384 context->change_observers()->Notify( |
| 384 &FileChangeObserver::OnCreateDirectory, base::MakeTuple(url)); | 385 &FileChangeObserver::OnCreateDirectory, std::make_tuple(url)); |
| 385 if (first) { | 386 if (first) { |
| 386 first = false; | 387 first = false; |
| 387 TouchDirectory(db, file_info.parent_id); | 388 TouchDirectory(db, file_info.parent_id); |
| 388 } | 389 } |
| 389 } | 390 } |
| 390 return base::File::FILE_OK; | 391 return base::File::FILE_OK; |
| 391 } | 392 } |
| 392 | 393 |
| 393 base::File::Error ObfuscatedFileUtil::GetFileInfo( | 394 base::File::Error ObfuscatedFileUtil::GetFileInfo( |
| 394 FileSystemOperationContext* context, | 395 FileSystemOperationContext* context, |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 475 if (error != base::File::FILE_OK) | 476 if (error != base::File::FILE_OK) |
| 476 return error; | 477 return error; |
| 477 | 478 |
| 478 int64_t growth = length - file_info.size; | 479 int64_t growth = length - file_info.size; |
| 479 if (!AllocateQuota(context, growth)) | 480 if (!AllocateQuota(context, growth)) |
| 480 return base::File::FILE_ERROR_NO_SPACE; | 481 return base::File::FILE_ERROR_NO_SPACE; |
| 481 error = NativeFileUtil::Truncate(local_path, length); | 482 error = NativeFileUtil::Truncate(local_path, length); |
| 482 if (error == base::File::FILE_OK) { | 483 if (error == base::File::FILE_OK) { |
| 483 UpdateUsage(context, url, growth); | 484 UpdateUsage(context, url, growth); |
| 484 context->change_observers()->Notify( | 485 context->change_observers()->Notify( |
| 485 &FileChangeObserver::OnModifyFile, base::MakeTuple(url)); | 486 &FileChangeObserver::OnModifyFile, std::make_tuple(url)); |
| 486 } | 487 } |
| 487 return error; | 488 return error; |
| 488 } | 489 } |
| 489 | 490 |
| 490 base::File::Error ObfuscatedFileUtil::CopyOrMoveFile( | 491 base::File::Error ObfuscatedFileUtil::CopyOrMoveFile( |
| 491 FileSystemOperationContext* context, | 492 FileSystemOperationContext* context, |
| 492 const FileSystemURL& src_url, | 493 const FileSystemURL& src_url, |
| 493 const FileSystemURL& dest_url, | 494 const FileSystemURL& dest_url, |
| 494 CopyOrMoveOption option, | 495 CopyOrMoveOption option, |
| 495 bool copy) { | 496 bool copy) { |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 602 error = base::File::FILE_ERROR_FAILED; | 603 error = base::File::FILE_ERROR_FAILED; |
| 603 } | 604 } |
| 604 } | 605 } |
| 605 | 606 |
| 606 if (error != base::File::FILE_OK) | 607 if (error != base::File::FILE_OK) |
| 607 return error; | 608 return error; |
| 608 | 609 |
| 609 if (overwrite) { | 610 if (overwrite) { |
| 610 context->change_observers()->Notify( | 611 context->change_observers()->Notify( |
| 611 &FileChangeObserver::OnModifyFile, | 612 &FileChangeObserver::OnModifyFile, |
| 612 base::MakeTuple(dest_url)); | 613 std::make_tuple(dest_url)); |
| 613 } else { | 614 } else { |
| 614 context->change_observers()->Notify( | 615 context->change_observers()->Notify( |
| 615 &FileChangeObserver::OnCreateFileFrom, | 616 &FileChangeObserver::OnCreateFileFrom, |
| 616 base::MakeTuple(dest_url, src_url)); | 617 std::make_tuple(dest_url, src_url)); |
| 617 } | 618 } |
| 618 | 619 |
| 619 if (!copy) { | 620 if (!copy) { |
| 620 context->change_observers()->Notify( | 621 context->change_observers()->Notify( |
| 621 &FileChangeObserver::OnRemoveFile, base::MakeTuple(src_url)); | 622 &FileChangeObserver::OnRemoveFile, std::make_tuple(src_url)); |
| 622 TouchDirectory(db, src_file_info.parent_id); | 623 TouchDirectory(db, src_file_info.parent_id); |
| 623 } | 624 } |
| 624 | 625 |
| 625 TouchDirectory(db, dest_file_info.parent_id); | 626 TouchDirectory(db, dest_file_info.parent_id); |
| 626 | 627 |
| 627 UpdateUsage(context, dest_url, growth); | 628 UpdateUsage(context, dest_url, growth); |
| 628 return error; | 629 return error; |
| 629 } | 630 } |
| 630 | 631 |
| 631 base::File::Error ObfuscatedFileUtil::CopyInForeignFile( | 632 base::File::Error ObfuscatedFileUtil::CopyInForeignFile( |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 690 true /* copy */)); | 691 true /* copy */)); |
| 691 } else { | 692 } else { |
| 692 error = CreateFile(context, src_file_path, dest_url, &dest_file_info); | 693 error = CreateFile(context, src_file_path, dest_url, &dest_file_info); |
| 693 } | 694 } |
| 694 | 695 |
| 695 if (error != base::File::FILE_OK) | 696 if (error != base::File::FILE_OK) |
| 696 return error; | 697 return error; |
| 697 | 698 |
| 698 if (overwrite) { | 699 if (overwrite) { |
| 699 context->change_observers()->Notify( | 700 context->change_observers()->Notify( |
| 700 &FileChangeObserver::OnModifyFile, base::MakeTuple(dest_url)); | 701 &FileChangeObserver::OnModifyFile, std::make_tuple(dest_url)); |
| 701 } else { | 702 } else { |
| 702 context->change_observers()->Notify( | 703 context->change_observers()->Notify( |
| 703 &FileChangeObserver::OnCreateFile, base::MakeTuple(dest_url)); | 704 &FileChangeObserver::OnCreateFile, std::make_tuple(dest_url)); |
| 704 } | 705 } |
| 705 | 706 |
| 706 UpdateUsage(context, dest_url, growth); | 707 UpdateUsage(context, dest_url, growth); |
| 707 TouchDirectory(db, dest_file_info.parent_id); | 708 TouchDirectory(db, dest_file_info.parent_id); |
| 708 return base::File::FILE_OK; | 709 return base::File::FILE_OK; |
| 709 } | 710 } |
| 710 | 711 |
| 711 base::File::Error ObfuscatedFileUtil::DeleteFile( | 712 base::File::Error ObfuscatedFileUtil::DeleteFile( |
| 712 FileSystemOperationContext* context, | 713 FileSystemOperationContext* context, |
| 713 const FileSystemURL& url) { | 714 const FileSystemURL& url) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 734 -UsageForPath(file_info.name.size()) - platform_file_info.size; | 735 -UsageForPath(file_info.name.size()) - platform_file_info.size; |
| 735 AllocateQuota(context, growth); | 736 AllocateQuota(context, growth); |
| 736 if (!db->RemoveFileInfo(file_id)) { | 737 if (!db->RemoveFileInfo(file_id)) { |
| 737 NOTREACHED(); | 738 NOTREACHED(); |
| 738 return base::File::FILE_ERROR_FAILED; | 739 return base::File::FILE_ERROR_FAILED; |
| 739 } | 740 } |
| 740 UpdateUsage(context, url, growth); | 741 UpdateUsage(context, url, growth); |
| 741 TouchDirectory(db, file_info.parent_id); | 742 TouchDirectory(db, file_info.parent_id); |
| 742 | 743 |
| 743 context->change_observers()->Notify( | 744 context->change_observers()->Notify( |
| 744 &FileChangeObserver::OnRemoveFile, base::MakeTuple(url)); | 745 &FileChangeObserver::OnRemoveFile, std::make_tuple(url)); |
| 745 | 746 |
| 746 if (error == base::File::FILE_ERROR_NOT_FOUND) | 747 if (error == base::File::FILE_ERROR_NOT_FOUND) |
| 747 return base::File::FILE_OK; | 748 return base::File::FILE_OK; |
| 748 | 749 |
| 749 error = NativeFileUtil::DeleteFile(local_path); | 750 error = NativeFileUtil::DeleteFile(local_path); |
| 750 if (base::File::FILE_OK != error) | 751 if (base::File::FILE_OK != error) |
| 751 LOG(WARNING) << "Leaked a backing file."; | 752 LOG(WARNING) << "Leaked a backing file."; |
| 752 return base::File::FILE_OK; | 753 return base::File::FILE_OK; |
| 753 } | 754 } |
| 754 | 755 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 769 } | 770 } |
| 770 if (!file_info.is_directory()) | 771 if (!file_info.is_directory()) |
| 771 return base::File::FILE_ERROR_NOT_A_DIRECTORY; | 772 return base::File::FILE_ERROR_NOT_A_DIRECTORY; |
| 772 if (!db->RemoveFileInfo(file_id)) | 773 if (!db->RemoveFileInfo(file_id)) |
| 773 return base::File::FILE_ERROR_NOT_EMPTY; | 774 return base::File::FILE_ERROR_NOT_EMPTY; |
| 774 int64_t growth = -UsageForPath(file_info.name.size()); | 775 int64_t growth = -UsageForPath(file_info.name.size()); |
| 775 AllocateQuota(context, growth); | 776 AllocateQuota(context, growth); |
| 776 UpdateUsage(context, url, growth); | 777 UpdateUsage(context, url, growth); |
| 777 TouchDirectory(db, file_info.parent_id); | 778 TouchDirectory(db, file_info.parent_id); |
| 778 context->change_observers()->Notify( | 779 context->change_observers()->Notify( |
| 779 &FileChangeObserver::OnRemoveDirectory, base::MakeTuple(url)); | 780 &FileChangeObserver::OnRemoveDirectory, std::make_tuple(url)); |
| 780 return base::File::FILE_OK; | 781 return base::File::FILE_OK; |
| 781 } | 782 } |
| 782 | 783 |
| 783 storage::ScopedFile ObfuscatedFileUtil::CreateSnapshotFile( | 784 storage::ScopedFile ObfuscatedFileUtil::CreateSnapshotFile( |
| 784 FileSystemOperationContext* context, | 785 FileSystemOperationContext* context, |
| 785 const FileSystemURL& url, | 786 const FileSystemURL& url, |
| 786 base::File::Error* error, | 787 base::File::Error* error, |
| 787 base::File::Info* file_info, | 788 base::File::Info* file_info, |
| 788 base::FilePath* platform_path) { | 789 base::FilePath* platform_path) { |
| 789 // We're just returning the local file information. | 790 // We're just returning the local file information. |
| (...skipping 575 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1365 InitFileInfo(&file_info, parent_id, | 1366 InitFileInfo(&file_info, parent_id, |
| 1366 VirtualPath::BaseName(url.path()).value()); | 1367 VirtualPath::BaseName(url.path()).value()); |
| 1367 | 1368 |
| 1368 int64_t growth = UsageForPath(file_info.name.size()); | 1369 int64_t growth = UsageForPath(file_info.name.size()); |
| 1369 if (!AllocateQuota(context, growth)) | 1370 if (!AllocateQuota(context, growth)) |
| 1370 return base::File(base::File::FILE_ERROR_NO_SPACE); | 1371 return base::File(base::File::FILE_ERROR_NO_SPACE); |
| 1371 base::File file = CreateAndOpenFile(context, url, &file_info, file_flags); | 1372 base::File file = CreateAndOpenFile(context, url, &file_info, file_flags); |
| 1372 if (file.IsValid()) { | 1373 if (file.IsValid()) { |
| 1373 UpdateUsage(context, url, growth); | 1374 UpdateUsage(context, url, growth); |
| 1374 context->change_observers()->Notify( | 1375 context->change_observers()->Notify( |
| 1375 &FileChangeObserver::OnCreateFile, base::MakeTuple(url)); | 1376 &FileChangeObserver::OnCreateFile, std::make_tuple(url)); |
| 1376 } | 1377 } |
| 1377 return file; | 1378 return file; |
| 1378 } | 1379 } |
| 1379 | 1380 |
| 1380 if (file_flags & base::File::FLAG_CREATE) | 1381 if (file_flags & base::File::FLAG_CREATE) |
| 1381 return base::File(base::File::FILE_ERROR_EXISTS); | 1382 return base::File(base::File::FILE_ERROR_EXISTS); |
| 1382 | 1383 |
| 1383 base::File::Info platform_file_info; | 1384 base::File::Info platform_file_info; |
| 1384 base::FilePath local_path; | 1385 base::FilePath local_path; |
| 1385 FileInfo file_info; | 1386 FileInfo file_info; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 1408 LOG(WARNING) << "Lost a backing file."; | 1409 LOG(WARNING) << "Lost a backing file."; |
| 1409 return base::File(base::File::FILE_ERROR_FAILED); | 1410 return base::File(base::File::FILE_ERROR_FAILED); |
| 1410 } | 1411 } |
| 1411 return file; | 1412 return file; |
| 1412 } | 1413 } |
| 1413 | 1414 |
| 1414 // If truncating we need to update the usage. | 1415 // If truncating we need to update the usage. |
| 1415 if (delta) { | 1416 if (delta) { |
| 1416 UpdateUsage(context, url, delta); | 1417 UpdateUsage(context, url, delta); |
| 1417 context->change_observers()->Notify( | 1418 context->change_observers()->Notify( |
| 1418 &FileChangeObserver::OnModifyFile, base::MakeTuple(url)); | 1419 &FileChangeObserver::OnModifyFile, std::make_tuple(url)); |
| 1419 } | 1420 } |
| 1420 return file; | 1421 return file; |
| 1421 } | 1422 } |
| 1422 | 1423 |
| 1423 bool ObfuscatedFileUtil::HasIsolatedStorage(const GURL& origin) { | 1424 bool ObfuscatedFileUtil::HasIsolatedStorage(const GURL& origin) { |
| 1424 return special_storage_policy_.get() && | 1425 return special_storage_policy_.get() && |
| 1425 special_storage_policy_->HasIsolatedStorage(origin); | 1426 special_storage_policy_->HasIsolatedStorage(origin); |
| 1426 } | 1427 } |
| 1427 | 1428 |
| 1428 } // namespace storage | 1429 } // namespace storage |
| OLD | NEW |