Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(57)

Side by Side Diff: chrome/browser/chromeos/drive/file_cache.cc

Issue 15690021: drive: Move temporary file removal responsiblity to FileCache (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 280 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 291
292 bool FileCache::FreeDiskSpaceIfNeededFor(int64 num_bytes) { 292 bool FileCache::FreeDiskSpaceIfNeededFor(int64 num_bytes) {
293 AssertOnSequencedWorkerPool(); 293 AssertOnSequencedWorkerPool();
294 294
295 // Do nothing and return if we have enough space. 295 // Do nothing and return if we have enough space.
296 if (HasEnoughSpaceFor(num_bytes, cache_root_path_)) 296 if (HasEnoughSpaceFor(num_bytes, cache_root_path_))
297 return true; 297 return true;
298 298
299 // Otherwise, try to free up the disk space. 299 // Otherwise, try to free up the disk space.
300 DVLOG(1) << "Freeing up disk space for " << num_bytes; 300 DVLOG(1) << "Freeing up disk space for " << num_bytes;
301
301 // First remove temporary files from the metadata. 302 // First remove temporary files from the metadata.
302 metadata_->RemoveTemporaryFiles(); 303 scoped_ptr<FileCacheMetadata::Iterator> it = metadata_->GetIterator();
304 for (; !it->IsAtEnd(); it->Advance()) {
305 if (!it->GetValue().is_persistent())
306 metadata_->RemoveCacheEntry(it->GetKey());
307 }
308 DCHECK(!it->HasError());
309
303 // Then remove all files under "tmp" directory. 310 // Then remove all files under "tmp" directory.
304 RemoveAllFiles(GetCacheDirectoryPath(CACHE_TYPE_TMP)); 311 RemoveAllFiles(GetCacheDirectoryPath(CACHE_TYPE_TMP));
305 312
306 // Check the disk space again. 313 // Check the disk space again.
307 return HasEnoughSpaceFor(num_bytes, cache_root_path_); 314 return HasEnoughSpaceFor(num_bytes, cache_root_path_);
308 } 315 }
309 316
310 void FileCache::GetFileOnUIThread(const std::string& resource_id, 317 void FileCache::GetFileOnUIThread(const std::string& resource_id,
311 const std::string& md5, 318 const std::string& md5,
312 const GetFileFromCacheCallback& callback) { 319 const GetFileFromCacheCallback& callback) {
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 411
405 base::PostTaskAndReplyWithResult( 412 base::PostTaskAndReplyWithResult(
406 blocking_task_runner_, 413 blocking_task_runner_,
407 FROM_HERE, 414 FROM_HERE,
408 base::Bind(&FileCache::Pin, 415 base::Bind(&FileCache::Pin,
409 base::Unretained(this), resource_id, md5), 416 base::Unretained(this), resource_id, md5),
410 base::Bind(&FileCache::OnPinned, 417 base::Bind(&FileCache::OnPinned,
411 weak_ptr_factory_.GetWeakPtr(), resource_id, md5, callback)); 418 weak_ptr_factory_.GetWeakPtr(), resource_id, md5, callback));
412 } 419 }
413 420
421 FileError FileCache::Pin(const std::string& resource_id,
422 const std::string& md5) {
423 AssertOnSequencedWorkerPool();
424
425 bool is_persistent = true;
426 FileCacheEntry cache_entry;
427 if (!GetCacheEntry(resource_id, md5, &cache_entry)) {
428 // The file will be first downloaded in 'tmp', then moved to 'persistent'.
429 is_persistent = false;
430 } else { // File exists in cache, determines destination path.
431 // Determine source and destination paths.
432
433 // If file is dirty or mounted, don't move it.
434 if (!cache_entry.is_dirty() && !cache_entry.is_mounted()) {
435 // If file was pinned before but actual file blob doesn't exist in cache:
436 // - don't need to move the file.
437 if (!cache_entry.is_present()) {
438 DCHECK(cache_entry.is_pinned());
439 return FILE_ERROR_OK;
440 }
441 // File exists, move it to persistent dir.
442 // Gets the current path of the file in cache.
443 base::FilePath source_path = GetCacheFilePath(
444 resource_id,
445 md5,
446 GetSubDirectoryType(cache_entry),
447 CACHED_FILE_FROM_SERVER);
448 base::FilePath dest_path = GetCacheFilePath(resource_id,
449 md5,
450 CACHE_TYPE_PERSISTENT,
451 CACHED_FILE_FROM_SERVER);
452 if (!MoveFile(source_path, dest_path))
453 return FILE_ERROR_FAILED;
454 }
455 }
456
457 // Now that file operations have completed, update metadata.
458 cache_entry.set_md5(md5);
459 cache_entry.set_is_pinned(true);
460 cache_entry.set_is_persistent(is_persistent);
461 metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry);
462 return FILE_ERROR_OK;
463 }
464
414 void FileCache::UnpinOnUIThread(const std::string& resource_id, 465 void FileCache::UnpinOnUIThread(const std::string& resource_id,
415 const std::string& md5, 466 const std::string& md5,
416 const FileOperationCallback& callback) { 467 const FileOperationCallback& callback) {
417 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 468 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
418 DCHECK(!callback.is_null()); 469 DCHECK(!callback.is_null());
419 470
420 base::PostTaskAndReplyWithResult( 471 base::PostTaskAndReplyWithResult(
421 blocking_task_runner_, 472 blocking_task_runner_,
422 FROM_HERE, 473 FROM_HERE,
423 base::Bind(&FileCache::Unpin, 474 base::Bind(&FileCache::Unpin,
(...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after
791 cache_entry.set_md5(md5); 842 cache_entry.set_md5(md5);
792 cache_entry.set_is_present(true); 843 cache_entry.set_is_present(true);
793 cache_entry.set_is_persistent(sub_dir_type == CACHE_TYPE_PERSISTENT); 844 cache_entry.set_is_persistent(sub_dir_type == CACHE_TYPE_PERSISTENT);
794 cache_entry.set_is_dirty(origin == CACHED_FILE_LOCALLY_MODIFIED); 845 cache_entry.set_is_dirty(origin == CACHED_FILE_LOCALLY_MODIFIED);
795 metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry); 846 metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry);
796 } 847 }
797 848
798 return success ? FILE_ERROR_OK : FILE_ERROR_FAILED; 849 return success ? FILE_ERROR_OK : FILE_ERROR_FAILED;
799 } 850 }
800 851
801 FileError FileCache::Pin(const std::string& resource_id,
802 const std::string& md5) {
803 AssertOnSequencedWorkerPool();
804
805 bool is_persistent = true;
806 FileCacheEntry cache_entry;
807 if (!GetCacheEntry(resource_id, md5, &cache_entry)) {
808 // The file will be first downloaded in 'tmp', then moved to 'persistent'.
809 is_persistent = false;
810 } else { // File exists in cache, determines destination path.
811 // Determine source and destination paths.
812
813 // If file is dirty or mounted, don't move it.
814 if (!cache_entry.is_dirty() && !cache_entry.is_mounted()) {
815 // If file was pinned before but actual file blob doesn't exist in cache:
816 // - don't need to move the file.
817 if (!cache_entry.is_present()) {
818 DCHECK(cache_entry.is_pinned());
819 return FILE_ERROR_OK;
820 }
821 // File exists, move it to persistent dir.
822 // Gets the current path of the file in cache.
823 base::FilePath source_path = GetCacheFilePath(
824 resource_id,
825 md5,
826 GetSubDirectoryType(cache_entry),
827 CACHED_FILE_FROM_SERVER);
828 base::FilePath dest_path = GetCacheFilePath(resource_id,
829 md5,
830 CACHE_TYPE_PERSISTENT,
831 CACHED_FILE_FROM_SERVER);
832 if (!MoveFile(source_path, dest_path))
833 return FILE_ERROR_FAILED;
834 }
835 }
836
837 // Now that file operations have completed, update metadata.
838 cache_entry.set_md5(md5);
839 cache_entry.set_is_pinned(true);
840 cache_entry.set_is_persistent(is_persistent);
841 metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry);
842 return FILE_ERROR_OK;
843 }
844
845
846 FileError FileCache::MarkAsMounted(const std::string& resource_id, 852 FileError FileCache::MarkAsMounted(const std::string& resource_id,
847 const std::string& md5, 853 const std::string& md5,
848 base::FilePath* cache_file_path) { 854 base::FilePath* cache_file_path) {
849 AssertOnSequencedWorkerPool(); 855 AssertOnSequencedWorkerPool();
850 DCHECK(cache_file_path); 856 DCHECK(cache_file_path);
851 857
852 // Get cache entry associated with the resource_id and md5 858 // Get cache entry associated with the resource_id and md5
853 FileCacheEntry cache_entry; 859 FileCacheEntry cache_entry;
854 if (!GetCacheEntry(resource_id, md5, &cache_entry)) 860 if (!GetCacheEntry(resource_id, md5, &cache_entry))
855 return FILE_ERROR_NOT_FOUND; 861 return FILE_ERROR_NOT_FOUND;
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after
1100 } 1106 }
1101 1107
1102 // static 1108 // static
1103 FileCache::CacheSubDirectoryType FileCache::GetSubDirectoryType( 1109 FileCache::CacheSubDirectoryType FileCache::GetSubDirectoryType(
1104 const FileCacheEntry& cache_entry) { 1110 const FileCacheEntry& cache_entry) {
1105 return cache_entry.is_persistent() ? CACHE_TYPE_PERSISTENT : CACHE_TYPE_TMP; 1111 return cache_entry.is_persistent() ? CACHE_TYPE_PERSISTENT : CACHE_TYPE_TMP;
1106 } 1112 }
1107 1113
1108 } // namespace internal 1114 } // namespace internal
1109 } // namespace drive 1115 } // namespace drive
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/drive/file_cache.h ('k') | chrome/browser/chromeos/drive/file_cache_metadata.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698