blob: 2c3266e2212921a13cbfa5ab894dc4144184c406 [file] [log] [blame]
[email protected]3653146a2012-05-29 13:41:471// 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#ifndef CHROME_BROWSER_CHROMEOS_GDATA_GDATA_CACHE_H_
6#define CHROME_BROWSER_CHROMEOS_GDATA_GDATA_CACHE_H_
7#pragma once
8
9#include <map>
10#include <string>
11
[email protected]32a7fc852012-06-08 17:25:5012#include "base/file_path.h"
[email protected]3653146a2012-05-29 13:41:4713#include "base/memory/scoped_ptr.h"
[email protected]fcc92a52012-06-08 22:54:1614#include "base/threading/sequenced_worker_pool.h"
[email protected]3653146a2012-05-29 13:41:4715
[email protected]3653146a2012-05-29 13:41:4716namespace gdata {
17
18class GDataCache {
19 public:
20 // Enum defining GCache subdirectory location.
21 // This indexes into |GDataFileSystem::cache_paths_| vector.
22 enum CacheSubDirectoryType {
23 CACHE_TYPE_META = 0, // Downloaded feeds.
24 CACHE_TYPE_PINNED, // Symlinks to files in persistent dir that are
25 // pinned, or to /dev/null for non-existent
26 // files.
27 CACHE_TYPE_OUTGOING, // Symlinks to files in persistent or tmp dir to
28 // be uploaded.
29 CACHE_TYPE_PERSISTENT, // Files that are pinned or modified locally,
30 // not evictable, hopefully.
31 CACHE_TYPE_TMP, // Files that don't meet criteria to be in
32 // persistent dir, and hence evictable.
33 CACHE_TYPE_TMP_DOWNLOADS, // Downloaded files.
34 CACHE_TYPE_TMP_DOCUMENTS, // Temporary JSON files for hosted documents.
35 NUM_CACHE_TYPES, // This must be at the end.
36 };
37
38 // This is used as a bitmask for the cache state.
39 enum CacheState {
40 CACHE_STATE_NONE = 0x0,
41 CACHE_STATE_PINNED = 0x1 << 0,
42 CACHE_STATE_PRESENT = 0x1 << 1,
43 CACHE_STATE_DIRTY = 0x1 << 2,
44 CACHE_STATE_MOUNTED = 0x1 << 3,
45 };
46
[email protected]32a7fc852012-06-08 17:25:5047 // Enum defining origin of a cached file.
48 enum CachedFileOrigin {
49 CACHED_FILE_FROM_SERVER = 0,
50 CACHED_FILE_LOCALLY_MODIFIED,
51 CACHED_FILE_MOUNTED,
52 };
53
54 static const char kMountedArchiveFileExtension[];
55
[email protected]3653146a2012-05-29 13:41:4756 // Structure to store information of an existing cache file.
57 struct CacheEntry {
58 CacheEntry(const std::string& md5,
59 CacheSubDirectoryType sub_dir_type,
60 int cache_state)
61 : md5(md5),
62 sub_dir_type(sub_dir_type),
63 cache_state(cache_state) {
64 }
65
66 bool IsPresent() const { return IsCachePresent(cache_state); }
67 bool IsPinned() const { return IsCachePinned(cache_state); }
68 bool IsDirty() const { return IsCacheDirty(cache_state); }
69 bool IsMounted() const { return IsCacheMounted(cache_state); }
70
71 // For debugging purposes.
72 std::string ToString() const;
73
74 std::string md5;
75 CacheSubDirectoryType sub_dir_type;
76 int cache_state;
77 };
78
79 static bool IsCachePresent(int cache_state) {
80 return cache_state & CACHE_STATE_PRESENT;
81 }
82 static bool IsCachePinned(int cache_state) {
83 return cache_state & CACHE_STATE_PINNED;
84 }
85 static bool IsCacheDirty(int cache_state) {
86 return cache_state & CACHE_STATE_DIRTY;
87 }
88 static bool IsCacheMounted(int cache_state) {
89 return cache_state & CACHE_STATE_MOUNTED;
90 }
91 static int SetCachePresent(int cache_state) {
92 return cache_state |= CACHE_STATE_PRESENT;
93 }
94 static int SetCachePinned(int cache_state) {
95 return cache_state |= CACHE_STATE_PINNED;
96 }
97 static int SetCacheDirty(int cache_state) {
98 return cache_state |= CACHE_STATE_DIRTY;
99 }
100 static int SetCacheMounted(int cache_state) {
101 return cache_state |= CACHE_STATE_MOUNTED;
102 }
103 static int ClearCachePresent(int cache_state) {
104 return cache_state &= ~CACHE_STATE_PRESENT;
105 }
106 static int ClearCachePinned(int cache_state) {
107 return cache_state &= ~CACHE_STATE_PINNED;
108 }
109 static int ClearCacheDirty(int cache_state) {
110 return cache_state &= ~CACHE_STATE_DIRTY;
111 }
112 static int ClearCacheMounted(int cache_state) {
113 return cache_state &= ~CACHE_STATE_MOUNTED;
114 }
115
116 // A map table of cache file's resource id to its CacheEntry* entry.
117 typedef std::map<std::string, CacheEntry> CacheMap;
118
119 virtual ~GDataCache();
120
[email protected]32a7fc852012-06-08 17:25:50121 // Returns the sub-directory under gdata cache directory for the given sub
122 // directory type. Example: <user_profile_dir>/GCache/v1/tmp
123 FilePath GetCacheDirectoryPath(CacheSubDirectoryType sub_dir_type) const;
124
125 // Returns absolute path of the file if it were cached or to be cached.
126 FilePath GetCacheFilePath(const std::string& resource_id,
127 const std::string& md5,
128 CacheSubDirectoryType sub_dir_type,
129 CachedFileOrigin file_orign) const;
130
131 // TODO(hashimoto): Remove this method when crbug.com/131756 is fixed.
132 const std::vector<FilePath>& cache_paths() const { return cache_paths_; }
133
[email protected]3653146a2012-05-29 13:41:47134 // Sets |cache_map_| data member to formal parameter |new_cache_map|.
135 virtual void SetCacheMap(const CacheMap& new_cache_map) = 0;
136
137 // Updates cache map with entry corresponding to |resource_id|.
138 // Creates new entry if it doesn't exist, otherwise update the entry.
139 virtual void UpdateCache(const std::string& resource_id,
[email protected]32a7fc852012-06-08 17:25:50140 const std::string& md5,
141 CacheSubDirectoryType subdir,
142 int cache_state) = 0;
[email protected]3653146a2012-05-29 13:41:47143
144 // Removes entry corresponding to |resource_id| from cache map.
145 virtual void RemoveFromCache(const std::string& resource_id) = 0;
146
147 // Returns the cache entry for file corresponding to |resource_id| and |md5|
148 // if entry exists in cache map. Otherwise, returns NULL.
149 // |md5| can be empty if only matching |resource_id| is desired, which may
150 // happen when looking for pinned entries where symlinks' filenames have no
151 // extension and hence no md5.
152 virtual scoped_ptr<CacheEntry> GetCacheEntry(const std::string& resource_id,
153 const std::string& md5) = 0;
154
155 // Removes temporary files (files in CACHE_TYPE_TMP) from the cache map.
156 virtual void RemoveTemporaryFiles() = 0;
157
158 // Factory methods for GDataCache.
[email protected]fcc92a52012-06-08 22:54:16159 // |pool| and |sequence_token| are used to assert that the functions are
160 // called on the right sequenced worker pool with the right sequence token.
161 //
162 // For testing, the thread assertion can be disabled by passing NULL and
163 // the default value of SequenceToken.
[email protected]32a7fc852012-06-08 17:25:50164 static scoped_ptr<GDataCache> CreateGDataCache(
[email protected]fcc92a52012-06-08 22:54:16165 const FilePath& cache_root_path,
166 base::SequencedWorkerPool* pool,
167 const base::SequencedWorkerPool::SequenceToken& sequence_token);
[email protected]3653146a2012-05-29 13:41:47168
169 protected:
[email protected]fcc92a52012-06-08 22:54:16170 GDataCache(
171 const FilePath& cache_root_path,
172 base::SequencedWorkerPool* pool_,
173 const base::SequencedWorkerPool::SequenceToken& sequence_token);
174
175 // Checks whether the current thread is on the right sequenced worker pool
176 // with the right sequence ID. If not, DCHECK will fail.
177 void AssertOnSequencedWorkerPool();
[email protected]3653146a2012-05-29 13:41:47178
179 private:
[email protected]32a7fc852012-06-08 17:25:50180 // Paths for all subdirectories of GCache, one for each
181 // GDataCache::CacheSubDirectoryType enum.
182 std::vector<FilePath> cache_paths_;
[email protected]fcc92a52012-06-08 22:54:16183 base::SequencedWorkerPool* pool_;
184 const base::SequencedWorkerPool::SequenceToken sequence_token_;
[email protected]32a7fc852012-06-08 17:25:50185
[email protected]3653146a2012-05-29 13:41:47186 DISALLOW_COPY_AND_ASSIGN(GDataCache);
187};
188
189} // namespace gdata
190
191#endif // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_CACHE_H_