blob: 9b4db3620c8ee1274f321eea493d55a615793a2b [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
12#include "base/memory/scoped_ptr.h"
13
14class FilePath;
15
16namespace 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
47 // Structure to store information of an existing cache file.
48 struct CacheEntry {
49 CacheEntry(const std::string& md5,
50 CacheSubDirectoryType sub_dir_type,
51 int cache_state)
52 : md5(md5),
53 sub_dir_type(sub_dir_type),
54 cache_state(cache_state) {
55 }
56
57 bool IsPresent() const { return IsCachePresent(cache_state); }
58 bool IsPinned() const { return IsCachePinned(cache_state); }
59 bool IsDirty() const { return IsCacheDirty(cache_state); }
60 bool IsMounted() const { return IsCacheMounted(cache_state); }
61
62 // For debugging purposes.
63 std::string ToString() const;
64
65 std::string md5;
66 CacheSubDirectoryType sub_dir_type;
67 int cache_state;
68 };
69
70 static bool IsCachePresent(int cache_state) {
71 return cache_state & CACHE_STATE_PRESENT;
72 }
73 static bool IsCachePinned(int cache_state) {
74 return cache_state & CACHE_STATE_PINNED;
75 }
76 static bool IsCacheDirty(int cache_state) {
77 return cache_state & CACHE_STATE_DIRTY;
78 }
79 static bool IsCacheMounted(int cache_state) {
80 return cache_state & CACHE_STATE_MOUNTED;
81 }
82 static int SetCachePresent(int cache_state) {
83 return cache_state |= CACHE_STATE_PRESENT;
84 }
85 static int SetCachePinned(int cache_state) {
86 return cache_state |= CACHE_STATE_PINNED;
87 }
88 static int SetCacheDirty(int cache_state) {
89 return cache_state |= CACHE_STATE_DIRTY;
90 }
91 static int SetCacheMounted(int cache_state) {
92 return cache_state |= CACHE_STATE_MOUNTED;
93 }
94 static int ClearCachePresent(int cache_state) {
95 return cache_state &= ~CACHE_STATE_PRESENT;
96 }
97 static int ClearCachePinned(int cache_state) {
98 return cache_state &= ~CACHE_STATE_PINNED;
99 }
100 static int ClearCacheDirty(int cache_state) {
101 return cache_state &= ~CACHE_STATE_DIRTY;
102 }
103 static int ClearCacheMounted(int cache_state) {
104 return cache_state &= ~CACHE_STATE_MOUNTED;
105 }
106
107 // A map table of cache file's resource id to its CacheEntry* entry.
108 typedef std::map<std::string, CacheEntry> CacheMap;
109
110 virtual ~GDataCache();
111
112 // Sets |cache_map_| data member to formal parameter |new_cache_map|.
113 virtual void SetCacheMap(const CacheMap& new_cache_map) = 0;
114
115 // Updates cache map with entry corresponding to |resource_id|.
116 // Creates new entry if it doesn't exist, otherwise update the entry.
117 virtual void UpdateCache(const std::string& resource_id,
118 const std::string& md5,
119 CacheSubDirectoryType subdir,
120 int cache_state) = 0;
121
122 // Removes entry corresponding to |resource_id| from cache map.
123 virtual void RemoveFromCache(const std::string& resource_id) = 0;
124
125 // Returns the cache entry for file corresponding to |resource_id| and |md5|
126 // if entry exists in cache map. Otherwise, returns NULL.
127 // |md5| can be empty if only matching |resource_id| is desired, which may
128 // happen when looking for pinned entries where symlinks' filenames have no
129 // extension and hence no md5.
130 virtual scoped_ptr<CacheEntry> GetCacheEntry(const std::string& resource_id,
131 const std::string& md5) = 0;
132
133 // Removes temporary files (files in CACHE_TYPE_TMP) from the cache map.
134 virtual void RemoveTemporaryFiles() = 0;
135
136 // Factory methods for GDataCache.
137 static scoped_ptr<GDataCache> CreateGDataCache();
138
139 protected:
140 GDataCache();
141
142 private:
143 DISALLOW_COPY_AND_ASSIGN(GDataCache);
144};
145
146} // namespace gdata
147
148#endif // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_CACHE_H_