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 #include "chrome/browser/chromeos/gdata/gdata_cache.h" | 5 #include "chrome/browser/chromeos/gdata/gdata_cache.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
12 #include "base/threading/thread_restrictions.h" | 12 #include "base/threading/thread_restrictions.h" |
| 13 #include "chrome/browser/chromeos/gdata/gdata_util.h" |
13 | 14 |
14 namespace gdata { | 15 namespace gdata { |
15 namespace { | 16 namespace { |
16 | 17 |
| 18 const char kLocallyModifiedFileExtension[] = "local"; |
| 19 |
| 20 const FilePath::CharType kGDataCacheMetaDir[] = FILE_PATH_LITERAL("meta"); |
| 21 const FilePath::CharType kGDataCachePinnedDir[] = FILE_PATH_LITERAL("pinned"); |
| 22 const FilePath::CharType kGDataCacheOutgoingDir[] = |
| 23 FILE_PATH_LITERAL("outgoing"); |
| 24 const FilePath::CharType kGDataCachePersistentDir[] = |
| 25 FILE_PATH_LITERAL("persistent"); |
| 26 const FilePath::CharType kGDataCacheTmpDir[] = FILE_PATH_LITERAL("tmp"); |
| 27 const FilePath::CharType kGDataCacheTmpDownloadsDir[] = |
| 28 FILE_PATH_LITERAL("tmp/downloads"); |
| 29 const FilePath::CharType kGDataCacheTmpDocumentsDir[] = |
| 30 FILE_PATH_LITERAL("tmp/documents"); |
| 31 |
17 std::string CacheSubDirectoryTypeToString( | 32 std::string CacheSubDirectoryTypeToString( |
18 GDataCache::CacheSubDirectoryType subdir) { | 33 GDataCache::CacheSubDirectoryType subdir) { |
19 switch (subdir) { | 34 switch (subdir) { |
20 case GDataCache::CACHE_TYPE_META: | 35 case GDataCache::CACHE_TYPE_META: |
21 return "meta"; | 36 return "meta"; |
22 case GDataCache::CACHE_TYPE_PINNED: | 37 case GDataCache::CACHE_TYPE_PINNED: |
23 return "pinned"; | 38 return "pinned"; |
24 case GDataCache::CACHE_TYPE_OUTGOING: | 39 case GDataCache::CACHE_TYPE_OUTGOING: |
25 return "outgoing"; | 40 return "outgoing"; |
26 case GDataCache::CACHE_TYPE_PERSISTENT: | 41 case GDataCache::CACHE_TYPE_PERSISTENT: |
27 return "persistent"; | 42 return "persistent"; |
28 case GDataCache::CACHE_TYPE_TMP: | 43 case GDataCache::CACHE_TYPE_TMP: |
29 return "tmp"; | 44 return "tmp"; |
30 case GDataCache::CACHE_TYPE_TMP_DOWNLOADS: | 45 case GDataCache::CACHE_TYPE_TMP_DOWNLOADS: |
31 return "tmp_downloads"; | 46 return "tmp_downloads"; |
32 case GDataCache::CACHE_TYPE_TMP_DOCUMENTS: | 47 case GDataCache::CACHE_TYPE_TMP_DOCUMENTS: |
33 return "tmp_documents"; | 48 return "tmp_documents"; |
34 case GDataCache::NUM_CACHE_TYPES: | 49 case GDataCache::NUM_CACHE_TYPES: |
35 NOTREACHED(); | 50 NOTREACHED(); |
36 } | 51 } |
37 NOTREACHED(); | 52 NOTREACHED(); |
38 return "unknown subdir"; | 53 return "unknown subdir"; |
39 } | 54 } |
40 | 55 |
41 } // namespace | 56 } // namespace |
42 | 57 |
| 58 const char GDataCache::kMountedArchiveFileExtension[] = "mounted"; |
| 59 |
43 std::string GDataCache::CacheEntry::ToString() const { | 60 std::string GDataCache::CacheEntry::ToString() const { |
44 std::vector<std::string> cache_states; | 61 std::vector<std::string> cache_states; |
45 if (GDataCache::IsCachePresent(cache_state)) | 62 if (GDataCache::IsCachePresent(cache_state)) |
46 cache_states.push_back("present"); | 63 cache_states.push_back("present"); |
47 if (GDataCache::IsCachePinned(cache_state)) | 64 if (GDataCache::IsCachePinned(cache_state)) |
48 cache_states.push_back("pinned"); | 65 cache_states.push_back("pinned"); |
49 if (GDataCache::IsCacheDirty(cache_state)) | 66 if (GDataCache::IsCacheDirty(cache_state)) |
50 cache_states.push_back("dirty"); | 67 cache_states.push_back("dirty"); |
51 | 68 |
52 return base::StringPrintf("md5=%s, subdir=%s, cache_state=%s", | 69 return base::StringPrintf("md5=%s, subdir=%s, cache_state=%s", |
53 md5.c_str(), | 70 md5.c_str(), |
54 CacheSubDirectoryTypeToString(sub_dir_type).c_str(), | 71 CacheSubDirectoryTypeToString(sub_dir_type).c_str(), |
55 JoinString(cache_states, ',').c_str()); | 72 JoinString(cache_states, ',').c_str()); |
56 } | 73 } |
57 | 74 |
58 GDataCache::GDataCache() { | 75 GDataCache::GDataCache(const FilePath& cache_root_path) { |
| 76 // Insert into |cache_paths_| in order defined in enum CacheSubDirectoryType. |
| 77 cache_paths_.push_back(cache_root_path.Append(kGDataCacheMetaDir)); |
| 78 cache_paths_.push_back(cache_root_path.Append(kGDataCachePinnedDir)); |
| 79 cache_paths_.push_back(cache_root_path.Append(kGDataCacheOutgoingDir)); |
| 80 cache_paths_.push_back(cache_root_path.Append(kGDataCachePersistentDir)); |
| 81 cache_paths_.push_back(cache_root_path.Append(kGDataCacheTmpDir)); |
| 82 cache_paths_.push_back(cache_root_path.Append(kGDataCacheTmpDownloadsDir)); |
| 83 cache_paths_.push_back(cache_root_path.Append(kGDataCacheTmpDocumentsDir)); |
59 } | 84 } |
60 | 85 |
61 GDataCache::~GDataCache() { | 86 GDataCache::~GDataCache() { |
62 } | 87 } |
63 | 88 |
| 89 FilePath GDataCache::GetCacheDirectoryPath( |
| 90 CacheSubDirectoryType sub_dir_type) const { |
| 91 DCHECK_LE(0, sub_dir_type); |
| 92 DCHECK_GT(NUM_CACHE_TYPES, sub_dir_type); |
| 93 return cache_paths_[sub_dir_type]; |
| 94 } |
| 95 |
| 96 FilePath GDataCache::GetCacheFilePath(const std::string& resource_id, |
| 97 const std::string& md5, |
| 98 CacheSubDirectoryType sub_dir_type, |
| 99 CachedFileOrigin file_origin) const { |
| 100 DCHECK(sub_dir_type != CACHE_TYPE_META); |
| 101 |
| 102 // Runs on any thread. |
| 103 // Filename is formatted as resource_id.md5, i.e. resource_id is the base |
| 104 // name and md5 is the extension. |
| 105 std::string base_name = util::EscapeCacheFileName(resource_id); |
| 106 if (file_origin == CACHED_FILE_LOCALLY_MODIFIED) { |
| 107 DCHECK(sub_dir_type == CACHE_TYPE_PERSISTENT); |
| 108 base_name += FilePath::kExtensionSeparator; |
| 109 base_name += kLocallyModifiedFileExtension; |
| 110 } else if (!md5.empty()) { |
| 111 base_name += FilePath::kExtensionSeparator; |
| 112 base_name += util::EscapeCacheFileName(md5); |
| 113 } |
| 114 // For mounted archives the filename is formatted as resource_id.md5.mounted, |
| 115 // i.e. resource_id.md5 is the base name and ".mounted" is the extension |
| 116 if (file_origin == CACHED_FILE_MOUNTED) { |
| 117 DCHECK(sub_dir_type == CACHE_TYPE_PERSISTENT); |
| 118 base_name += FilePath::kExtensionSeparator; |
| 119 base_name += kMountedArchiveFileExtension; |
| 120 } |
| 121 return GetCacheDirectoryPath(sub_dir_type).Append(base_name); |
| 122 } |
| 123 |
64 class GDataCacheMap : public GDataCache { | 124 class GDataCacheMap : public GDataCache { |
65 public: | 125 public: |
66 GDataCacheMap(); | 126 explicit GDataCacheMap(const FilePath& cache_root_path); |
67 | 127 |
68 protected: | 128 protected: |
69 virtual ~GDataCacheMap(); | 129 virtual ~GDataCacheMap(); |
70 | 130 |
71 private: | 131 private: |
72 // GDataCache implementation. | 132 // GDataCache implementation. |
73 virtual void SetCacheMap(const CacheMap& new_cache_map) OVERRIDE; | 133 virtual void SetCacheMap(const CacheMap& new_cache_map) OVERRIDE; |
74 virtual void UpdateCache(const std::string& resource_id, | 134 virtual void UpdateCache(const std::string& resource_id, |
75 const std::string& md5, | 135 const std::string& md5, |
76 CacheSubDirectoryType subdir, | 136 CacheSubDirectoryType subdir, |
77 int cache_state) OVERRIDE; | 137 int cache_state) OVERRIDE; |
78 virtual void RemoveFromCache(const std::string& resource_id) OVERRIDE; | 138 virtual void RemoveFromCache(const std::string& resource_id) OVERRIDE; |
79 virtual scoped_ptr<CacheEntry> GetCacheEntry(const std::string& resource_id, | 139 virtual scoped_ptr<CacheEntry> GetCacheEntry(const std::string& resource_id, |
80 const std::string& md5) OVERRIDE; | 140 const std::string& md5) OVERRIDE; |
81 virtual void RemoveTemporaryFiles() OVERRIDE; | 141 virtual void RemoveTemporaryFiles() OVERRIDE; |
82 | 142 |
83 CacheMap cache_map_; | 143 CacheMap cache_map_; |
84 }; | 144 }; |
85 | 145 |
86 GDataCacheMap::GDataCacheMap() { | 146 GDataCacheMap::GDataCacheMap(const FilePath& cache_root_path) |
| 147 : GDataCache(cache_root_path) { |
87 } | 148 } |
88 | 149 |
89 GDataCacheMap::~GDataCacheMap() { | 150 GDataCacheMap::~GDataCacheMap() { |
90 base::ThreadRestrictions::AssertIOAllowed(); | 151 base::ThreadRestrictions::AssertIOAllowed(); |
91 cache_map_.clear(); | 152 cache_map_.clear(); |
92 } | 153 } |
93 | 154 |
94 void GDataCacheMap::SetCacheMap(const CacheMap& new_cache_map) { | 155 void GDataCacheMap::SetCacheMap(const CacheMap& new_cache_map) { |
95 base::ThreadRestrictions::AssertIOAllowed(); | 156 base::ThreadRestrictions::AssertIOAllowed(); |
96 cache_map_ = new_cache_map; | 157 cache_map_ = new_cache_map; |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 if (iter->second.sub_dir_type == CACHE_TYPE_TMP) { | 237 if (iter->second.sub_dir_type == CACHE_TYPE_TMP) { |
177 // Post-increment the iterator to avoid iterator invalidation. | 238 // Post-increment the iterator to avoid iterator invalidation. |
178 cache_map_.erase(iter++); | 239 cache_map_.erase(iter++); |
179 } else { | 240 } else { |
180 ++iter; | 241 ++iter; |
181 } | 242 } |
182 } | 243 } |
183 } | 244 } |
184 | 245 |
185 // static | 246 // static |
186 scoped_ptr<GDataCache> GDataCache::CreateGDataCache() { | 247 scoped_ptr<GDataCache> GDataCache::CreateGDataCache( |
187 return scoped_ptr<GDataCache>(new GDataCacheMap()); | 248 const FilePath& cache_root_path) { |
| 249 return scoped_ptr<GDataCache>(new GDataCacheMap(cache_root_path)); |
188 } | 250 } |
189 | 251 |
190 } // namespace gdata | 252 } // namespace gdata |
OLD | NEW |