Chromium Code Reviews| 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/gdata_cache.h" | |
| 10 #include "chrome/browser/chromeos/gdata/gdata_file_system.h" | |
| 11 #include "chrome/browser/chromeos/gdata/gdata.pb.h" | |
| 12 #include "content/public/browser/browser_thread.h" | |
| 13 | |
| 14 using content::BrowserThread; | |
| 15 | |
| 16 namespace gdata { | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 // Delay to scrub caches after the first feed fetch (in seconds). | |
| 21 const int kDriveScrubDelayAfterFirstFeedFetch = 10; | |
|
satorux1
2012/08/10 23:47:40
maybe just
kDelaySeconds = 10;
| |
| 22 | |
| 23 void RemoveCacheIfNecessary( | |
|
satorux1
2012/08/10 23:47:40
function comment is missing. please fix other plac
| |
| 24 const std::string& resource_id, | |
| 25 gdata::GDataCache* cache, | |
| 26 const std::string& cache_md5, | |
| 27 GDataFileError error, | |
| 28 const FilePath& gdata_file_path, | |
| 29 scoped_ptr<gdata::GDataEntryProto> file_proto) { | |
| 30 DCHECK(cache); | |
| 31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 32 | |
| 33 // Removes the cache file if the givin cache status is invalid or unavailable. | |
| 34 if (error != gdata::GDATA_FILE_OK || | |
| 35 !file_proto.get() || | |
| 36 file_proto->file_info().is_directory() || | |
| 37 cache_md5 != file_proto->file_specific_info().file_md5()) { | |
| 38 cache->RemoveOnUIThread(resource_id, CacheOperationCallback()); | |
| 39 return; | |
| 40 } | |
| 41 | |
| 42 // Otherwise, does nothing with the cache. | |
| 43 } | |
| 44 | |
| 45 void GetCacheEntryAndRemoveCacheIfNecessary( | |
| 46 gdata::GDataFileSystemInterface* file_system, | |
| 47 gdata::GDataCache* cache, | |
| 48 const std::string& resource_id, | |
| 49 bool success, | |
| 50 const gdata::GDataCacheEntry& cache_entry) { | |
| 51 DCHECK(file_system); | |
| 52 DCHECK(cache); | |
| 53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 54 | |
| 55 // Removes the chehe if GetCacheEntryOnUIThread() is failed. | |
| 56 if (!success) { | |
| 57 cache->RemoveOnUIThread(resource_id, CacheOperationCallback()); | |
| 58 return; | |
| 59 } | |
| 60 | |
| 61 file_system->GetEntryInfoByResourceId(resource_id, | |
| 62 base::Bind(&RemoveCacheIfNecessary, | |
| 63 resource_id, | |
| 64 cache, | |
| 65 cache_entry.md5())); | |
| 66 } | |
| 67 | |
| 68 } // namespace | |
| 69 | |
| 70 StaleCacheFilesRemover::StaleCacheFilesRemover( | |
| 71 GDataFileSystemInterface* file_system, | |
| 72 GDataCache* cache) | |
| 73 : file_system_(file_system), | |
| 74 cache_(cache), | |
| 75 weak_ptr_factory_(this) { | |
| 76 } | |
| 77 | |
| 78 StaleCacheFilesRemover::~StaleCacheFilesRemover() { | |
| 79 } | |
| 80 | |
| 81 // Sets the timer to start scrubing. | |
| 82 void StaleCacheFilesRemover::ScrubCacheAfterDelay() { | |
| 83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 84 | |
| 85 base::MessageLoopProxy::current()->PostDelayedTask( | |
| 86 FROM_HERE, | |
| 87 base::Bind(&StaleCacheFilesRemover::ScrubCache, | |
| 88 weak_ptr_factory_.GetWeakPtr()), | |
| 89 base::TimeDelta::FromSeconds(kDriveScrubDelayAfterFirstFeedFetch)); | |
| 90 } | |
| 91 | |
| 92 void StaleCacheFilesRemover::ScrubCache() { | |
|
satorux1
2012/08/10 23:47:40
Because it's delayed, it's not guaranteed that cac
| |
| 93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 94 | |
| 95 const FilePath root_path = FilePath(gdata::kGDataRootDirectory); | |
| 96 cache_->GetResourceIdsOfAllFilesOnUIThread( | |
| 97 base::Bind(&StaleCacheFilesRemover::OnGetResourceIdsOfAllFiles, | |
| 98 weak_ptr_factory_.GetWeakPtr())); | |
| 99 } | |
| 100 | |
| 101 void StaleCacheFilesRemover::OnGetResourceIdsOfAllFiles( | |
| 102 const std::vector<std::string>& resource_ids) { | |
| 103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 104 | |
| 105 for (size_t i = 0; i < resource_ids.size(); ++i) { | |
| 106 const std::string& resource_id = resource_ids[i]; | |
| 107 cache_->GetCacheEntryOnUIThread( | |
| 108 resource_id, | |
| 109 "", // Doesn't check MD5. | |
| 110 base::Bind(&GetCacheEntryAndRemoveCacheIfNecessary, | |
| 111 file_system_, | |
| 112 cache_, | |
| 113 resource_id)); | |
| 114 } | |
| 115 MessageLoop::current()->DeleteSoon(FROM_HERE, this); | |
| 116 } | |
| 117 | |
| 118 } // namespace gdata | |
| OLD | NEW |