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 #ifndef CHROME_BROWSER_CHROMEOS_GDATA_GDATA_CACHE_METADATA_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_GDATA_GDATA_CACHE_METADATA_H_ |
6 #define CHROME_BROWSER_CHROMEOS_GDATA_GDATA_CACHE_METADATA_H_ | 6 #define CHROME_BROWSER_CHROMEOS_GDATA_GDATA_CACHE_METADATA_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/file_path.h" | 11 #include "base/file_path.h" |
12 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
13 #include "chrome/browser/chromeos/gdata/gdata_cache.h" | 13 #include "chrome/browser/chromeos/gdata/gdata_cache.h" |
14 | 14 |
15 namespace gdata { | 15 namespace gdata { |
16 | 16 |
17 // GDataCacheMetadata is interface to maintain metadata of GDataCache's cached | 17 // GDataCacheMetadata is interface to maintain metadata of GDataCache's cached |
18 // files. This class only manages metadata. File operations are done by | 18 // files. This class only manages metadata. File operations are done by |
19 // GDataCache. | 19 // GDataCache. |
20 // All member access including ctor and dtor must be made on the blocking pool. | 20 // All member access including ctor and dtor must be made on the blocking pool. |
21 class GDataCacheMetadata { | 21 class GDataCacheMetadata { |
22 public: | 22 public: |
23 // Callback for Iterate(). | 23 // Callback for Iterate(). |
24 typedef base::Callback<void(const std::string& resource_id, | 24 typedef base::Callback<void(const std::string& resource_id, |
25 const GDataCache::CacheEntry& cache_entry)> | 25 const GDataCacheEntry& cache_entry)> |
26 IterateCallback; | 26 IterateCallback; |
27 | 27 |
28 // |pool| and |sequence_token| are used to assert that the functions are | 28 // |pool| and |sequence_token| are used to assert that the functions are |
29 // called on the right sequenced worker pool with the right sequence token. | 29 // called on the right sequenced worker pool with the right sequence token. |
30 // | 30 // |
31 // For testing, the thread assertion can be disabled by passing NULL and | 31 // For testing, the thread assertion can be disabled by passing NULL and |
32 // the default value of SequenceToken. | 32 // the default value of SequenceToken. |
33 GDataCacheMetadata( | 33 GDataCacheMetadata( |
34 base::SequencedWorkerPool* pool, | 34 base::SequencedWorkerPool* pool, |
35 const base::SequencedWorkerPool::SequenceToken& sequence_token); | 35 const base::SequencedWorkerPool::SequenceToken& sequence_token); |
36 virtual ~GDataCacheMetadata(); | 36 virtual ~GDataCacheMetadata(); |
37 | 37 |
38 // Updates cache map with entry corresponding to |resource_id|. | 38 // Updates cache map with entry corresponding to |resource_id|. |
39 // Creates new entry if it doesn't exist, otherwise update the entry. | 39 // Creates new entry if it doesn't exist, otherwise update the entry. |
40 virtual void UpdateCache(const std::string& resource_id, | 40 virtual void UpdateCache(const std::string& resource_id, |
41 const GDataCache::CacheEntry& cache_entry) = 0; | 41 const GDataCacheEntry& cache_entry) = 0; |
42 | 42 |
43 // Removes entry corresponding to |resource_id| from cache map. | 43 // Removes entry corresponding to |resource_id| from cache map. |
44 virtual void RemoveFromCache(const std::string& resource_id) = 0; | 44 virtual void RemoveFromCache(const std::string& resource_id) = 0; |
45 | 45 |
46 // Returns the cache entry for file corresponding to |resource_id| and |md5| | 46 // Returns the cache entry for file corresponding to |resource_id| and |md5| |
47 // if entry exists in cache map. Otherwise, returns NULL. | 47 // if entry exists in cache map. Otherwise, returns NULL. |
48 // |md5| can be empty if only matching |resource_id| is desired, which may | 48 // |md5| can be empty if only matching |resource_id| is desired, which may |
49 // happen when looking for pinned entries where symlinks' filenames have no | 49 // happen when looking for pinned entries where symlinks' filenames have no |
50 // extension and hence no md5. | 50 // extension and hence no md5. |
51 virtual scoped_ptr<GDataCache::CacheEntry> GetCacheEntry( | 51 virtual scoped_ptr<GDataCacheEntry> GetCacheEntry( |
52 const std::string& resource_id, | 52 const std::string& resource_id, |
53 const std::string& md5) = 0; | 53 const std::string& md5) = 0; |
54 | 54 |
55 // Removes temporary files (files in CACHE_TYPE_TMP) from the cache map. | 55 // Removes temporary files (files in CACHE_TYPE_TMP) from the cache map. |
56 virtual void RemoveTemporaryFiles() = 0; | 56 virtual void RemoveTemporaryFiles() = 0; |
57 | 57 |
58 // Iterates over all the cache entries synchronously. |callback| is called | 58 // Iterates over all the cache entries synchronously. |callback| is called |
59 // on each cache entry. | 59 // on each cache entry. |
60 virtual void Iterate(const IterateCallback& callback) = 0; | 60 virtual void Iterate(const IterateCallback& callback) = 0; |
61 | 61 |
(...skipping 16 matching lines...) Expand all Loading... |
78 base::SequencedWorkerPool* pool, | 78 base::SequencedWorkerPool* pool, |
79 const base::SequencedWorkerPool::SequenceToken& sequence_token); | 79 const base::SequencedWorkerPool::SequenceToken& sequence_token); |
80 virtual ~GDataCacheMetadataMap(); | 80 virtual ~GDataCacheMetadataMap(); |
81 | 81 |
82 // Initializes the data. | 82 // Initializes the data. |
83 void Initialize(const std::vector<FilePath>& cache_paths); | 83 void Initialize(const std::vector<FilePath>& cache_paths); |
84 | 84 |
85 // GDataCacheMetadata overrides: | 85 // GDataCacheMetadata overrides: |
86 virtual void UpdateCache( | 86 virtual void UpdateCache( |
87 const std::string& resource_id, | 87 const std::string& resource_id, |
88 const GDataCache::CacheEntry& cache_entry) OVERRIDE; | 88 const GDataCacheEntry& cache_entry) OVERRIDE; |
89 virtual void RemoveFromCache(const std::string& resource_id) OVERRIDE; | 89 virtual void RemoveFromCache(const std::string& resource_id) OVERRIDE; |
90 virtual scoped_ptr<GDataCache::CacheEntry> GetCacheEntry( | 90 virtual scoped_ptr<GDataCacheEntry> GetCacheEntry( |
91 const std::string& resource_id, | 91 const std::string& resource_id, |
92 const std::string& md5) OVERRIDE; | 92 const std::string& md5) OVERRIDE; |
93 virtual void RemoveTemporaryFiles() OVERRIDE; | 93 virtual void RemoveTemporaryFiles() OVERRIDE; |
94 virtual void Iterate(const IterateCallback& callback) OVERRIDE; | 94 virtual void Iterate(const IterateCallback& callback) OVERRIDE; |
95 | 95 |
96 // A map table of cache file's resource id to its CacheEntry* entry. | 96 // A map table of cache file's resource id to its CacheEntry* entry. |
97 typedef std::map<std::string, GDataCache::CacheEntry> CacheMap; | 97 typedef std::map<std::string, GDataCacheEntry> CacheMap; |
98 | 98 |
99 // A map table of resource ID to file path. | 99 // A map table of resource ID to file path. |
100 typedef std::map<std::string, FilePath> ResourceIdToFilePathMap; | 100 typedef std::map<std::string, FilePath> ResourceIdToFilePathMap; |
101 | 101 |
102 private: | 102 private: |
103 friend class GDataCacheMetadataMapTest; | 103 friend class GDataCacheMetadataMapTest; |
104 FRIEND_TEST_ALL_PREFIXES(GDataCacheMetadataMapTest, RemoveTemporaryFilesTest); | 104 FRIEND_TEST_ALL_PREFIXES(GDataCacheMetadataMapTest, RemoveTemporaryFilesTest); |
105 | 105 |
106 // Scans cache subdirectory and build or update |cache_map| | 106 // Scans cache subdirectory and build or update |cache_map| |
107 // with found file blobs or symlinks. | 107 // with found file blobs or symlinks. |
108 // | 108 // |
109 // The resource IDs and file paths of discovered files are collected as a | 109 // The resource IDs and file paths of discovered files are collected as a |
110 // ResourceIdToFilePathMap, if these are processed properly. | 110 // ResourceIdToFilePathMap, if these are processed properly. |
111 static void ScanCacheDirectory( | 111 static void ScanCacheDirectory( |
112 const std::vector<FilePath>& cache_paths, | 112 const std::vector<FilePath>& cache_paths, |
113 GDataCache::CacheSubDirectoryType sub_dir_type, | 113 GDataCache::CacheSubDirectoryType sub_dir_type, |
114 CacheMap* cache_map, | 114 CacheMap* cache_map, |
115 ResourceIdToFilePathMap* processed_file_map); | 115 ResourceIdToFilePathMap* processed_file_map); |
116 | 116 |
117 // Returns true if |md5| matches the one in |cache_entry| with some | 117 // Returns true if |md5| matches the one in |cache_entry| with some |
118 // exceptions. See the function definition for details. | 118 // exceptions. See the function definition for details. |
119 static bool CheckIfMd5Matches(const std::string& md5, | 119 static bool CheckIfMd5Matches(const std::string& md5, |
120 const GDataCache::CacheEntry& cache_entry); | 120 const GDataCacheEntry& cache_entry); |
121 | 121 |
122 CacheMap cache_map_; | 122 CacheMap cache_map_; |
123 | 123 |
124 DISALLOW_COPY_AND_ASSIGN(GDataCacheMetadataMap); | 124 DISALLOW_COPY_AND_ASSIGN(GDataCacheMetadataMap); |
125 }; | 125 }; |
126 | 126 |
127 } // namespace gdata | 127 } // namespace gdata |
128 | 128 |
129 #endif // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_CACHE_METADATA_H_ | 129 #endif // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_CACHE_METADATA_H_ |
OLD | NEW |