| 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 "chrome/browser/chromeos/drive/file_cache.h" | 5 #include "chrome/browser/chromeos/drive/file_cache.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 519 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 530 | 530 |
| 531 // TODO(hashimoto): Move logic around OnCommitDirty to FileSystem and remove | 531 // TODO(hashimoto): Move logic around OnCommitDirty to FileSystem and remove |
| 532 // this method. | 532 // this method. |
| 533 base::MessageLoopProxy::current()->PostTask( | 533 base::MessageLoopProxy::current()->PostTask( |
| 534 FROM_HERE, | 534 FROM_HERE, |
| 535 base::Bind(&FileCache::OnCommitDirty, | 535 base::Bind(&FileCache::OnCommitDirty, |
| 536 weak_ptr_factory_.GetWeakPtr(), resource_id, callback, | 536 weak_ptr_factory_.GetWeakPtr(), resource_id, callback, |
| 537 FILE_ERROR_OK)); | 537 FILE_ERROR_OK)); |
| 538 } | 538 } |
| 539 | 539 |
| 540 void FileCache::ClearDirtyOnUIThread(const std::string& resource_id, | 540 FileError FileCache::ClearDirty(const std::string& resource_id, |
| 541 const std::string& md5, | 541 const std::string& md5) { |
| 542 const FileOperationCallback& callback) { | 542 AssertOnSequencedWorkerPool(); |
| 543 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 544 DCHECK(!callback.is_null()); | |
| 545 | 543 |
| 546 base::PostTaskAndReplyWithResult( | 544 // |md5| is the new .<md5> extension to rename the file to. |
| 547 blocking_task_runner_, | 545 // So, search for entry in cache without comparing md5. |
| 548 FROM_HERE, | 546 FileCacheEntry cache_entry; |
| 549 base::Bind(&FileCache::ClearDirty, | 547 |
| 550 base::Unretained(this), resource_id, md5), | 548 // Clearing a dirty file means its entry and actual file blob must exist in |
| 551 callback); | 549 // cache. |
| 550 if (!GetCacheEntry(resource_id, std::string(), &cache_entry) || |
| 551 !cache_entry.is_present()) { |
| 552 LOG(WARNING) << "Can't clear dirty state of a file that wasn't cached: " |
| 553 << "res_id=" << resource_id |
| 554 << ", md5=" << md5; |
| 555 return FILE_ERROR_NOT_FOUND; |
| 556 } |
| 557 |
| 558 // If a file is not dirty (it should have been marked dirty via |
| 559 // MarkDirtyInCache), clearing its dirty state is an invalid operation. |
| 560 if (!cache_entry.is_dirty()) { |
| 561 LOG(WARNING) << "Can't clear dirty state of a non-dirty file: res_id=" |
| 562 << resource_id |
| 563 << ", md5=" << md5; |
| 564 return FILE_ERROR_INVALID_OPERATION; |
| 565 } |
| 566 |
| 567 // File must be dirty and hence in persistent dir. |
| 568 DCHECK(cache_entry.is_persistent()); |
| 569 |
| 570 // Get the current path of the file in cache. |
| 571 base::FilePath source_path = |
| 572 GetCacheFilePath(resource_id, |
| 573 md5, |
| 574 GetSubDirectoryType(cache_entry), |
| 575 CACHED_FILE_LOCALLY_MODIFIED); |
| 576 |
| 577 // Determine destination path. |
| 578 // If file is pinned, move it to persistent dir with .md5 extension; |
| 579 // otherwise, move it to tmp dir with .md5 extension. |
| 580 const CacheSubDirectoryType sub_dir_type = |
| 581 cache_entry.is_pinned() ? CACHE_TYPE_PERSISTENT : CACHE_TYPE_TMP; |
| 582 base::FilePath dest_path = GetCacheFilePath(resource_id, |
| 583 md5, |
| 584 sub_dir_type, |
| 585 CACHED_FILE_FROM_SERVER); |
| 586 |
| 587 if (!MoveFile(source_path, dest_path)) |
| 588 return FILE_ERROR_FAILED; |
| 589 |
| 590 // Now that file operations have completed, update metadata. |
| 591 cache_entry.set_md5(md5); |
| 592 cache_entry.set_is_dirty(false); |
| 593 cache_entry.set_is_persistent(sub_dir_type == CACHE_TYPE_PERSISTENT); |
| 594 metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry); |
| 595 return FILE_ERROR_OK; |
| 552 } | 596 } |
| 553 | 597 |
| 554 void FileCache::RemoveOnUIThread(const std::string& resource_id, | 598 void FileCache::RemoveOnUIThread(const std::string& resource_id, |
| 555 const FileOperationCallback& callback) { | 599 const FileOperationCallback& callback) { |
| 556 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 600 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 557 DCHECK(!callback.is_null()); | 601 DCHECK(!callback.is_null()); |
| 558 | 602 |
| 559 base::PostTaskAndReplyWithResult( | 603 base::PostTaskAndReplyWithResult( |
| 560 blocking_task_runner_, | 604 blocking_task_runner_, |
| 561 FROM_HERE, | 605 FROM_HERE, |
| (...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 952 return FILE_ERROR_FAILED; | 996 return FILE_ERROR_FAILED; |
| 953 | 997 |
| 954 // Now that file operations have completed, update metadata. | 998 // Now that file operations have completed, update metadata. |
| 955 cache_entry.set_md5(md5); | 999 cache_entry.set_md5(md5); |
| 956 cache_entry.set_is_dirty(true); | 1000 cache_entry.set_is_dirty(true); |
| 957 cache_entry.set_is_persistent(sub_dir_type == CACHE_TYPE_PERSISTENT); | 1001 cache_entry.set_is_persistent(sub_dir_type == CACHE_TYPE_PERSISTENT); |
| 958 metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry); | 1002 metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry); |
| 959 return FILE_ERROR_OK; | 1003 return FILE_ERROR_OK; |
| 960 } | 1004 } |
| 961 | 1005 |
| 962 FileError FileCache::ClearDirty(const std::string& resource_id, | |
| 963 const std::string& md5) { | |
| 964 AssertOnSequencedWorkerPool(); | |
| 965 | |
| 966 // |md5| is the new .<md5> extension to rename the file to. | |
| 967 // So, search for entry in cache without comparing md5. | |
| 968 FileCacheEntry cache_entry; | |
| 969 | |
| 970 // Clearing a dirty file means its entry and actual file blob must exist in | |
| 971 // cache. | |
| 972 if (!GetCacheEntry(resource_id, std::string(), &cache_entry) || | |
| 973 !cache_entry.is_present()) { | |
| 974 LOG(WARNING) << "Can't clear dirty state of a file that wasn't cached: " | |
| 975 << "res_id=" << resource_id | |
| 976 << ", md5=" << md5; | |
| 977 return FILE_ERROR_NOT_FOUND; | |
| 978 } | |
| 979 | |
| 980 // If a file is not dirty (it should have been marked dirty via | |
| 981 // MarkDirtyInCache), clearing its dirty state is an invalid operation. | |
| 982 if (!cache_entry.is_dirty()) { | |
| 983 LOG(WARNING) << "Can't clear dirty state of a non-dirty file: res_id=" | |
| 984 << resource_id | |
| 985 << ", md5=" << md5; | |
| 986 return FILE_ERROR_INVALID_OPERATION; | |
| 987 } | |
| 988 | |
| 989 // File must be dirty and hence in persistent dir. | |
| 990 DCHECK(cache_entry.is_persistent()); | |
| 991 | |
| 992 // Get the current path of the file in cache. | |
| 993 base::FilePath source_path = | |
| 994 GetCacheFilePath(resource_id, | |
| 995 md5, | |
| 996 GetSubDirectoryType(cache_entry), | |
| 997 CACHED_FILE_LOCALLY_MODIFIED); | |
| 998 | |
| 999 // Determine destination path. | |
| 1000 // If file is pinned, move it to persistent dir with .md5 extension; | |
| 1001 // otherwise, move it to tmp dir with .md5 extension. | |
| 1002 const CacheSubDirectoryType sub_dir_type = | |
| 1003 cache_entry.is_pinned() ? CACHE_TYPE_PERSISTENT : CACHE_TYPE_TMP; | |
| 1004 base::FilePath dest_path = GetCacheFilePath(resource_id, | |
| 1005 md5, | |
| 1006 sub_dir_type, | |
| 1007 CACHED_FILE_FROM_SERVER); | |
| 1008 | |
| 1009 if (!MoveFile(source_path, dest_path)) | |
| 1010 return FILE_ERROR_FAILED; | |
| 1011 | |
| 1012 // Now that file operations have completed, update metadata. | |
| 1013 cache_entry.set_md5(md5); | |
| 1014 cache_entry.set_is_dirty(false); | |
| 1015 cache_entry.set_is_persistent(sub_dir_type == CACHE_TYPE_PERSISTENT); | |
| 1016 metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry); | |
| 1017 return FILE_ERROR_OK; | |
| 1018 } | |
| 1019 | |
| 1020 bool FileCache::ClearAll() { | 1006 bool FileCache::ClearAll() { |
| 1021 AssertOnSequencedWorkerPool(); | 1007 AssertOnSequencedWorkerPool(); |
| 1022 | 1008 |
| 1023 if (!file_util::Delete(cache_root_path_, true)) { | 1009 if (!file_util::Delete(cache_root_path_, true)) { |
| 1024 LOG(WARNING) << "Failed to delete the cache directory"; | 1010 LOG(WARNING) << "Failed to delete the cache directory"; |
| 1025 return false; | 1011 return false; |
| 1026 } | 1012 } |
| 1027 | 1013 |
| 1028 if (!InitializeOnBlockingPool()) { | 1014 if (!InitializeOnBlockingPool()) { |
| 1029 LOG(WARNING) << "Failed to initialize the cache"; | 1015 LOG(WARNING) << "Failed to initialize the cache"; |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1131 } | 1117 } |
| 1132 | 1118 |
| 1133 // static | 1119 // static |
| 1134 FileCache::CacheSubDirectoryType FileCache::GetSubDirectoryType( | 1120 FileCache::CacheSubDirectoryType FileCache::GetSubDirectoryType( |
| 1135 const FileCacheEntry& cache_entry) { | 1121 const FileCacheEntry& cache_entry) { |
| 1136 return cache_entry.is_persistent() ? CACHE_TYPE_PERSISTENT : CACHE_TYPE_TMP; | 1122 return cache_entry.is_persistent() ? CACHE_TYPE_PERSISTENT : CACHE_TYPE_TMP; |
| 1137 } | 1123 } |
| 1138 | 1124 |
| 1139 } // namespace internal | 1125 } // namespace internal |
| 1140 } // namespace drive | 1126 } // namespace drive |
| OLD | NEW |