Chromium Code Reviews| Index: chrome/browser/chromeos/gdata/stale_cache_files_remover.cc |
| diff --git a/chrome/browser/chromeos/gdata/stale_cache_files_remover.cc b/chrome/browser/chromeos/gdata/stale_cache_files_remover.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..11281ecb7bfb396907b57fbdd991bc4073dfadc3 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/gdata/stale_cache_files_remover.cc |
| @@ -0,0 +1,118 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/chromeos/gdata/stale_cache_files_remover.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/message_loop.h" |
| +#include "chrome/browser/chromeos/gdata/gdata_cache.h" |
| +#include "chrome/browser/chromeos/gdata/gdata_file_system.h" |
| +#include "chrome/browser/chromeos/gdata/gdata.pb.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +using content::BrowserThread; |
| + |
| +namespace gdata { |
| + |
| +namespace { |
| + |
| +// Delay to scrub caches after the first feed fetch (in seconds). |
| +const int kDriveScrubDelayAfterFirstFeedFetch = 10; |
|
satorux1
2012/08/10 23:47:40
maybe just
kDelaySeconds = 10;
|
| + |
| +void RemoveCacheIfNecessary( |
|
satorux1
2012/08/10 23:47:40
function comment is missing. please fix other plac
|
| + const std::string& resource_id, |
| + gdata::GDataCache* cache, |
| + const std::string& cache_md5, |
| + GDataFileError error, |
| + const FilePath& gdata_file_path, |
| + scoped_ptr<gdata::GDataEntryProto> file_proto) { |
| + DCHECK(cache); |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + |
| + // Removes the cache file if the givin cache status is invalid or unavailable. |
| + if (error != gdata::GDATA_FILE_OK || |
| + !file_proto.get() || |
| + file_proto->file_info().is_directory() || |
| + cache_md5 != file_proto->file_specific_info().file_md5()) { |
| + cache->RemoveOnUIThread(resource_id, CacheOperationCallback()); |
| + return; |
| + } |
| + |
| + // Otherwise, does nothing with the cache. |
| +} |
| + |
| +void GetCacheEntryAndRemoveCacheIfNecessary( |
| + gdata::GDataFileSystemInterface* file_system, |
| + gdata::GDataCache* cache, |
| + const std::string& resource_id, |
| + bool success, |
| + const gdata::GDataCacheEntry& cache_entry) { |
| + DCHECK(file_system); |
| + DCHECK(cache); |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + |
| + // Removes the chehe if GetCacheEntryOnUIThread() is failed. |
| + if (!success) { |
| + cache->RemoveOnUIThread(resource_id, CacheOperationCallback()); |
| + return; |
| + } |
| + |
| + file_system->GetEntryInfoByResourceId(resource_id, |
| + base::Bind(&RemoveCacheIfNecessary, |
| + resource_id, |
| + cache, |
| + cache_entry.md5())); |
| +} |
| + |
| +} // namespace |
| + |
| +StaleCacheFilesRemover::StaleCacheFilesRemover( |
| + GDataFileSystemInterface* file_system, |
| + GDataCache* cache) |
| + : file_system_(file_system), |
| + cache_(cache), |
| + weak_ptr_factory_(this) { |
| +} |
| + |
| +StaleCacheFilesRemover::~StaleCacheFilesRemover() { |
| +} |
| + |
| +// Sets the timer to start scrubing. |
| +void StaleCacheFilesRemover::ScrubCacheAfterDelay() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + |
| + base::MessageLoopProxy::current()->PostDelayedTask( |
| + FROM_HERE, |
| + base::Bind(&StaleCacheFilesRemover::ScrubCache, |
| + weak_ptr_factory_.GetWeakPtr()), |
| + base::TimeDelta::FromSeconds(kDriveScrubDelayAfterFirstFeedFetch)); |
| +} |
| + |
| +void StaleCacheFilesRemover::ScrubCache() { |
|
satorux1
2012/08/10 23:47:40
Because it's delayed, it's not guaranteed that cac
|
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + |
| + const FilePath root_path = FilePath(gdata::kGDataRootDirectory); |
| + cache_->GetResourceIdsOfAllFilesOnUIThread( |
| + base::Bind(&StaleCacheFilesRemover::OnGetResourceIdsOfAllFiles, |
| + weak_ptr_factory_.GetWeakPtr())); |
| +} |
| + |
| +void StaleCacheFilesRemover::OnGetResourceIdsOfAllFiles( |
| + const std::vector<std::string>& resource_ids) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + |
| + for (size_t i = 0; i < resource_ids.size(); ++i) { |
| + const std::string& resource_id = resource_ids[i]; |
| + cache_->GetCacheEntryOnUIThread( |
| + resource_id, |
| + "", // Doesn't check MD5. |
| + base::Bind(&GetCacheEntryAndRemoveCacheIfNecessary, |
| + file_system_, |
| + cache_, |
| + resource_id)); |
| + } |
| + MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
| +} |
| + |
| +} // namespace gdata |