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 kDelaySeconds = 10; | |
| 22 | |
| 23 // Check the cache file and removes if it is unavailable or invalid (eg. md5 | |
| 24 // mismatch). This is called from GetCacheEntryAndRemoveCacheIfNecessary(); | |
| 25 void RemoveCacheIfNecessary( | |
|
satorux1
2012/08/14 17:19:37
Please make it a private function of StaleCacheFil
yoshiki
2012/08/16 07:39:56
Done.
| |
| 26 const std::string& resource_id, | |
| 27 gdata::GDataCache* cache, | |
| 28 const std::string& cache_md5, | |
| 29 GDataFileError error, | |
| 30 const FilePath& gdata_file_path, | |
| 31 scoped_ptr<gdata::GDataEntryProto> file_proto) { | |
|
satorux1
2012/08/14 17:19:37
entry_proto
yoshiki
2012/08/16 07:39:56
Done.
| |
| 32 DCHECK(cache); | |
| 33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 34 | |
| 35 if (error != gdata::GDATA_FILE_OK || | |
| 36 !file_proto.get() || | |
| 37 file_proto->file_info().is_directory() || | |
| 38 cache_md5 != file_proto->file_specific_info().file_md5()) { | |
|
satorux1
2012/08/14 17:19:37
The condition looks complicated. Would be nice to
yoshiki
2012/08/16 07:39:56
Done.
| |
| 39 cache->RemoveOnUIThread(resource_id, CacheOperationCallback()); | |
|
satorux1
2012/08/14 17:19:37
Instead of passing a null-callback, could you pass
yoshiki
2012/08/16 07:39:56
Done.
| |
| 40 return; | |
| 41 } | |
| 42 | |
| 43 // Otherwise, does nothing with the cache. | |
|
satorux1
2012/08/14 17:19:37
don't need this.
yoshiki
2012/08/16 07:39:56
Done.
| |
| 44 } | |
| 45 | |
| 46 // Gets the file entry and calls RemoveCacheIfNecessary() with the file entry. | |
| 47 // This is called from StaleCacheFilesRemover::OnGetResourceIdsOfAllFiles(). | |
| 48 void GetEntryInfoAndRemoveCacheIfNecessary( | |
|
satorux1
2012/08/14 17:19:37
Please make it a private function of StaleCacheFil
yoshiki
2012/08/16 07:39:56
Done.
| |
| 49 gdata::GDataFileSystemInterface* file_system, | |
| 50 gdata::GDataCache* cache, | |
| 51 const std::string& resource_id, | |
| 52 bool success, | |
| 53 const gdata::GDataCacheEntry& cache_entry) { | |
| 54 DCHECK(file_system); | |
| 55 DCHECK(cache); | |
| 56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 57 | |
| 58 // Removes the chehe if GetCacheEntryOnUIThread() is failed. | |
|
satorux1
2012/08/14 17:19:37
cache. If you use emacs, please use M-x ispell-com
satorux1
2012/08/14 17:19:37
is failed -> failed
yoshiki
2012/08/16 07:39:56
Done.
yoshiki
2012/08/16 07:39:56
Done.
| |
| 59 if (!success) { | |
| 60 cache->RemoveOnUIThread(resource_id, CacheOperationCallback()); | |
|
satorux1
2012/08/14 17:19:37
This probably won't work if GetCacheEntryOnUIThrea
yoshiki
2012/08/16 07:39:56
Done.
| |
| 61 return; | |
| 62 } | |
| 63 | |
| 64 file_system->GetEntryInfoByResourceId(resource_id, | |
| 65 base::Bind(&RemoveCacheIfNecessary, | |
| 66 resource_id, | |
| 67 cache, | |
| 68 cache_entry.md5())); | |
| 69 } | |
| 70 | |
| 71 } // namespace | |
| 72 | |
| 73 StaleCacheFilesRemover::StaleCacheFilesRemover( | |
| 74 GDataFileSystemInterface* file_system, | |
| 75 GDataCache* cache) | |
| 76 : file_system_(file_system), | |
| 77 cache_(cache), | |
| 78 weak_ptr_factory_(this) { | |
|
satorux1
2012/08/14 17:19:37
allow_this_in...
yoshiki
2012/08/16 07:39:56
Done.
| |
| 79 } | |
| 80 | |
| 81 StaleCacheFilesRemover::~StaleCacheFilesRemover() { | |
| 82 } | |
| 83 | |
| 84 // Sets the timer to start removing. | |
|
satorux1
2012/08/14 17:19:37
please remove comment here.
yoshiki
2012/08/16 07:39:56
Done.
| |
| 85 void StaleCacheFilesRemover::Start() { | |
| 86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 87 | |
| 88 base::MessageLoopProxy::current()->PostDelayedTask( | |
| 89 FROM_HERE, | |
| 90 base::Bind(&StaleCacheFilesRemover::RemoveSlateCacheFiles, | |
| 91 weak_ptr_factory_.GetWeakPtr()), | |
| 92 base::TimeDelta::FromSeconds(kDelaySeconds)); | |
| 93 } | |
| 94 | |
| 95 // Gets the list of all the resource id and calls OnGetResourceIdsOfAllFiles() | |
| 96 // with the list. | |
|
satorux1
2012/08/14 17:19:37
please move this to .h file.
yoshiki
2012/08/16 07:39:56
This method is removed
| |
| 97 void StaleCacheFilesRemover::RemoveSlateCacheFiles() { | |
| 98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 99 | |
| 100 const FilePath root_path = FilePath(gdata::kGDataRootDirectory); | |
| 101 cache_->GetResourceIdsOfAllFilesOnUIThread( | |
| 102 base::Bind(&StaleCacheFilesRemover::OnGetResourceIdsOfAllFiles, | |
| 103 weak_ptr_factory_.GetWeakPtr())); | |
| 104 } | |
| 105 | |
| 106 // Gets the cache entry of each resource id. And passes the cache entry to | |
| 107 // GetEntryInfoAndRemoveCacheIfNecessary() | |
|
satorux1
2012/08/14 17:19:37
please move this to .h file.
yoshiki
2012/08/16 07:39:56
Done.
| |
| 108 void StaleCacheFilesRemover::OnGetResourceIdsOfAllFiles( | |
| 109 const std::vector<std::string>& resource_ids) { | |
| 110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 111 | |
| 112 for (size_t i = 0; i < resource_ids.size(); ++i) { | |
| 113 const std::string& resource_id = resource_ids[i]; | |
| 114 cache_->GetCacheEntryOnUIThread( | |
| 115 resource_id, | |
| 116 "", // Doesn't check MD5. | |
|
satorux1
2012/08/14 17:19:37
// Don't check MD5.
yoshiki
2012/08/16 07:39:56
Done.
| |
| 117 base::Bind(&GetEntryInfoAndRemoveCacheIfNecessary, | |
| 118 file_system_, | |
| 119 cache_, | |
| 120 resource_id)); | |
| 121 } | |
| 122 } | |
| 123 | |
| 124 } // namespace gdata | |
| OLD | NEW |