OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/chromeos/gdata/stale_cache_files_remover.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/message_loop.h" |
| 9 #include "chrome/browser/chromeos/gdata/drive_cache.h" |
| 10 #include "chrome/browser/chromeos/gdata/drive.pb.h" |
| 11 #include "chrome/browser/chromeos/gdata/drive_file_system.h" |
| 12 #include "content/public/browser/browser_thread.h" |
| 13 |
| 14 using content::BrowserThread; |
| 15 |
| 16 namespace gdata { |
| 17 |
| 18 namespace { |
| 19 |
| 20 // Emits the log when the remove failed. |
| 21 void EmitErrorLog(DriveFileError error, |
| 22 const std::string& resource_id, |
| 23 const std::string& md5) { |
| 24 if (error != gdata::DRIVE_FILE_OK) { |
| 25 LOG(WARNING) << "Failed to remove a stale cache file. resource_id:" |
| 26 << resource_id; |
| 27 } |
| 28 } |
| 29 |
| 30 } // namespace |
| 31 |
| 32 StaleCacheFilesRemover::StaleCacheFilesRemover( |
| 33 DriveFileSystemInterface* file_system, |
| 34 DriveCache* cache) |
| 35 : cache_(cache), |
| 36 file_system_(file_system), |
| 37 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
| 38 file_system_->AddObserver(this); |
| 39 } |
| 40 |
| 41 StaleCacheFilesRemover::~StaleCacheFilesRemover() { |
| 42 file_system_->RemoveObserver(this); |
| 43 } |
| 44 |
| 45 void StaleCacheFilesRemover::OnInitialLoadFinished() { |
| 46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 47 |
| 48 const FilePath root_path = FilePath(gdata::kDriveRootDirectory); |
| 49 cache_->GetResourceIdsOfAllFilesOnUIThread( |
| 50 base::Bind(&StaleCacheFilesRemover::OnGetResourceIdsOfAllFiles, |
| 51 weak_ptr_factory_.GetWeakPtr())); |
| 52 } |
| 53 |
| 54 void StaleCacheFilesRemover::OnGetResourceIdsOfAllFiles( |
| 55 const std::vector<std::string>& resource_ids) { |
| 56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 57 |
| 58 for (size_t i = 0; i < resource_ids.size(); ++i) { |
| 59 const std::string& resource_id = resource_ids[i]; |
| 60 cache_->GetCacheEntryOnUIThread( |
| 61 resource_id, |
| 62 "", // Don't check MD5. |
| 63 base::Bind( |
| 64 &StaleCacheFilesRemover::GetEntryInfoAndRemoveCacheIfNecessary, |
| 65 weak_ptr_factory_.GetWeakPtr(), |
| 66 resource_id)); |
| 67 } |
| 68 } |
| 69 |
| 70 void StaleCacheFilesRemover::GetEntryInfoAndRemoveCacheIfNecessary( |
| 71 const std::string& resource_id, |
| 72 bool success, |
| 73 const gdata::DriveCacheEntry& cache_entry) { |
| 74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 75 |
| 76 // Removes the cache if GetCacheEntryOnUIThread() failed. |
| 77 if (!success) { |
| 78 LOG(WARNING) << "GetCacheEntryOnUIThread() failed"; |
| 79 return; |
| 80 } |
| 81 |
| 82 file_system_->GetEntryInfoByResourceId( |
| 83 resource_id, |
| 84 base::Bind(&StaleCacheFilesRemover::RemoveCacheIfNecessary, |
| 85 weak_ptr_factory_.GetWeakPtr(), |
| 86 resource_id, |
| 87 cache_entry.md5())); |
| 88 } |
| 89 |
| 90 void StaleCacheFilesRemover::RemoveCacheIfNecessary( |
| 91 const std::string& resource_id, |
| 92 const std::string& cache_md5, |
| 93 DriveFileError error, |
| 94 const FilePath& gdata_file_path, |
| 95 scoped_ptr<gdata::DriveEntryProto> entry_proto) { |
| 96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 97 |
| 98 // The entry is not found in the file system. |
| 99 if (error != gdata::DRIVE_FILE_OK) { |
| 100 cache_->RemoveOnUIThread(resource_id, base::Bind(&EmitErrorLog)); |
| 101 return; |
| 102 } |
| 103 |
| 104 // The entry is found but the MD5 does not match. |
| 105 DCHECK(entry_proto.get()); |
| 106 if (!entry_proto->has_file_specific_info() || |
| 107 cache_md5 != entry_proto->file_specific_info().file_md5()) { |
| 108 cache_->RemoveOnUIThread(resource_id, base::Bind(&EmitErrorLog)); |
| 109 return; |
| 110 } |
| 111 } |
| 112 |
| 113 } // namespace gdata |
OLD | NEW |