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