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 // Emits the log when the remove failed. | |
21 void EmitErrorLog(GDataFileError error, | |
22 const std::string& resource_id, | |
23 const std::string& md5) { | |
24 if (error != gdata::GDATA_FILE_OK) | |
satorux1
2012/08/16 11:03:53
add {}
yoshiki
2012/08/17 03:49:59
Done.
| |
25 LOG(WARNING) << | |
26 "Failed to remove a stale cache file. resource_id:" << resource_id; | |
satorux1
2012/08/16 11:03:53
formatting looks weird
LOG(WARNING) << "Failed to
yoshiki
2012/08/17 03:49:59
Done.
| |
27 } | |
28 | |
29 } // namespace | |
30 | |
31 StaleCacheFilesRemover::StaleCacheFilesRemover( | |
32 GDataFileSystemInterface* file_system, | |
33 GDataCache* cache) | |
34 : file_system_(file_system), | |
35 cache_(cache), | |
36 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | |
37 file_system_->AddObserver(this); | |
38 } | |
39 | |
40 StaleCacheFilesRemover::~StaleCacheFilesRemover() { | |
41 file_system_->RemoveObserver(this); | |
42 } | |
43 | |
44 void StaleCacheFilesRemover::OnInitialLoadFinished() { | |
45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
46 | |
47 const FilePath root_path = FilePath(gdata::kGDataRootDirectory); | |
48 cache_->GetResourceIdsOfAllFilesOnUIThread( | |
49 base::Bind(&StaleCacheFilesRemover::OnGetResourceIdsOfAllFiles, | |
50 weak_ptr_factory_.GetWeakPtr())); | |
51 } | |
52 | |
53 void StaleCacheFilesRemover::OnGetResourceIdsOfAllFiles( | |
54 const std::vector<std::string>& resource_ids) { | |
55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
56 | |
57 for (size_t i = 0; i < resource_ids.size(); ++i) { | |
58 const std::string& resource_id = resource_ids[i]; | |
59 cache_->GetCacheEntryOnUIThread( | |
60 resource_id, | |
61 "", // Don't check MD5. | |
62 base::Bind( | |
63 &StaleCacheFilesRemover::GetEntryInfoAndRemoveCacheIfNecessary, | |
64 weak_ptr_factory_.GetWeakPtr(), | |
65 resource_id)); | |
66 } | |
67 } | |
68 | |
69 void StaleCacheFilesRemover::GetEntryInfoAndRemoveCacheIfNecessary( | |
70 const std::string& resource_id, | |
71 bool success, | |
72 const gdata::GDataCacheEntry& cache_entry) { | |
73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
74 | |
75 // Removes the cache if GetCacheEntryOnUIThread() failed. | |
76 if (!success) { | |
77 LOG(WARNING) << "GetCacheEntryOnUIThread() failed"; | |
78 return; | |
79 } | |
80 | |
81 file_system_->GetEntryInfoByResourceId( | |
82 resource_id, | |
83 base::Bind(&StaleCacheFilesRemover::RemoveCacheIfNecessary, | |
84 weak_ptr_factory_.GetWeakPtr(), | |
85 resource_id, | |
86 cache_entry.md5())); | |
87 } | |
88 | |
89 void StaleCacheFilesRemover::RemoveCacheIfNecessary( | |
90 const std::string& resource_id, | |
91 const std::string& cache_md5, | |
92 GDataFileError error, | |
93 const FilePath& gdata_file_path, | |
94 scoped_ptr<gdata::GDataEntryProto> entry_proto) { | |
95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
96 | |
97 // The entry is not found in the file system. | |
98 if (error != gdata::GDATA_FILE_OK) { | |
99 cache_->RemoveOnUIThread(resource_id, base::Bind(&EmitErrorLog)); | |
100 return; | |
101 } | |
102 | |
103 // The entry is found but the MD5 does not match. | |
104 DCHECK(entry_proto.get()); | |
105 if (!entry_proto->has_file_specific_info() || | |
106 cache_md5 != entry_proto->file_specific_info().file_md5()) { | |
107 cache_->RemoveOnUIThread(resource_id, base::Bind(&EmitErrorLog)); | |
108 return; | |
109 } | |
110 } | |
111 | |
112 } // namespace gdata | |
OLD | NEW |