[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 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 | #ifndef CHROME_BROWSER_CHROMEOS_GDATA_GDATA_CACHE_H_ |
| 6 | #define CHROME_BROWSER_CHROMEOS_GDATA_GDATA_CACHE_H_ |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 7 | |
| 8 | #include <map> |
| 9 | #include <string> |
[email protected] | 79c3752d | 2012-07-17 12:10:08 | [diff] [blame] | 10 | #include <vector> |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 11 | |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 12 | #include "base/callback.h" |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 13 | #include "base/file_path.h" |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 14 | #include "base/memory/scoped_ptr.h" |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 15 | #include "base/memory/weak_ptr.h" |
| 16 | #include "base/observer_list.h" |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 17 | #include "base/platform_file.h" |
[email protected] | 7f90e77c | 2012-07-17 09:35:31 | [diff] [blame] | 18 | #include "chrome/browser/chromeos/gdata/gdata_errorcode.h" |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 19 | |
[email protected] | 01ba15f7 | 2012-06-09 00:41:05 | [diff] [blame] | 20 | class Profile; |
| 21 | |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 22 | namespace base { |
| 23 | |
| 24 | class SequencedTaskRunner; |
| 25 | |
| 26 | } // namespace base |
| 27 | |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 28 | namespace gdata { |
| 29 | |
[email protected] | 12e4c18 | 2012-07-12 21:30:04 | [diff] [blame] | 30 | class GDataCacheEntry; |
[email protected] | ca5f6da | 2012-06-18 12:54:59 | [diff] [blame] | 31 | class GDataCacheMetadata; |
| 32 | |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 33 | // Callback for SetMountedStateOnUIThread and ClearAllOnUIThread. |
[email protected] | 7f90e77c | 2012-07-17 09:35:31 | [diff] [blame] | 34 | typedef base::Callback<void(GDataFileError error, |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 35 | const FilePath& file_path)> |
| 36 | ChangeCacheStateCallback; |
[email protected] | 7986b8d | 2012-06-14 15:05:14 | [diff] [blame] | 37 | |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 38 | // Callback for completion of cache operation. |
[email protected] | 7f90e77c | 2012-07-17 09:35:31 | [diff] [blame] | 39 | typedef base::Callback<void(GDataFileError error, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 40 | const std::string& resource_id, |
| 41 | const std::string& md5)> CacheOperationCallback; |
| 42 | |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 43 | // Callback for GetFileFromCache. |
[email protected] | 7f90e77c | 2012-07-17 09:35:31 | [diff] [blame] | 44 | typedef base::Callback<void(GDataFileError error, |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 45 | const std::string& resource_id, |
| 46 | const std::string& md5, |
| 47 | const FilePath& cache_file_path)> |
| 48 | GetFileFromCacheCallback; |
| 49 | |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 50 | // Callback for GetResourceIdsOfBacklogOnUIThread. |
| 51 | // |to_fetch| is for resource IDs of pinned-but-not-fetched files. |
| 52 | // |to_upload| is for resource IDs of dirty-but-not-uploaded files. |
| 53 | typedef base::Callback<void(const std::vector<std::string>& to_fetch, |
| 54 | const std::vector<std::string>& to_upload)> |
[email protected] | 85b6219 | 2012-06-29 19:56:38 | [diff] [blame] | 55 | GetResourceIdsOfBacklogCallback; |
| 56 | |
| 57 | // Callback for GetResourceIdsOfExistingPinnedFilesOnUIThread. |
| 58 | typedef base::Callback<void(const std::vector<std::string>& resource_ids)> |
[email protected] | 8764a39 | 2012-06-20 06:43:08 | [diff] [blame] | 59 | GetResourceIdsCallback; |
| 60 | |
[email protected] | fae353a | 2012-07-11 23:30:27 | [diff] [blame] | 61 | // Callback for GetCacheEntryOnUIThread. |
| 62 | // |success| indicates if the operation was successful. |
| 63 | // |cache_entry| is the obtained cache entry. On failure, |cache_state| is |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 64 | // set to TEST_CACHE_STATE_NONE. |
[email protected] | fae353a | 2012-07-11 23:30:27 | [diff] [blame] | 65 | typedef base::Callback<void(bool success, const GDataCacheEntry& cache_entry)> |
| 66 | GetCacheEntryCallback; |
| 67 | |
[email protected] | 6b70c7b | 2012-06-14 03:10:43 | [diff] [blame] | 68 | // GDataCache is used to maintain cache states of GDataFileSystem. |
| 69 | // |
| 70 | // All non-static public member functions, unless mentioned otherwise (see |
| 71 | // GetCacheFilePath() for example), should be called from the sequenced |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 72 | // worker pool with the sequence token set by CreateGDataCacheOnUIThread(). This |
[email protected] | 6b70c7b | 2012-06-14 03:10:43 | [diff] [blame] | 73 | // threading model is enforced by AssertOnSequencedWorkerPool(). |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 74 | // |
| 75 | // TODO(hashimoto): Change threading model of this class to make public methods |
| 76 | // being called on UI thread unless mentioned otherwise. crbug.com/132926 |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 77 | class GDataCache { |
| 78 | public: |
| 79 | // Enum defining GCache subdirectory location. |
[email protected] | 6b70c7b | 2012-06-14 03:10:43 | [diff] [blame] | 80 | // This indexes into |GDataCache::cache_paths_| vector. |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 81 | enum CacheSubDirectoryType { |
| 82 | CACHE_TYPE_META = 0, // Downloaded feeds. |
| 83 | CACHE_TYPE_PINNED, // Symlinks to files in persistent dir that are |
| 84 | // pinned, or to /dev/null for non-existent |
| 85 | // files. |
| 86 | CACHE_TYPE_OUTGOING, // Symlinks to files in persistent or tmp dir to |
| 87 | // be uploaded. |
| 88 | CACHE_TYPE_PERSISTENT, // Files that are pinned or modified locally, |
| 89 | // not evictable, hopefully. |
| 90 | CACHE_TYPE_TMP, // Files that don't meet criteria to be in |
| 91 | // persistent dir, and hence evictable. |
| 92 | CACHE_TYPE_TMP_DOWNLOADS, // Downloaded files. |
| 93 | CACHE_TYPE_TMP_DOCUMENTS, // Temporary JSON files for hosted documents. |
| 94 | NUM_CACHE_TYPES, // This must be at the end. |
| 95 | }; |
| 96 | |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 97 | // Enum defining origin of a cached file. |
| 98 | enum CachedFileOrigin { |
| 99 | CACHED_FILE_FROM_SERVER = 0, |
| 100 | CACHED_FILE_LOCALLY_MODIFIED, |
| 101 | CACHED_FILE_MOUNTED, |
| 102 | }; |
| 103 | |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 104 | // Enum defining type of file operation e.g. copy or move, etc. |
| 105 | enum FileOperationType { |
| 106 | FILE_OPERATION_MOVE = 0, |
| 107 | FILE_OPERATION_COPY, |
| 108 | }; |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 109 | |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 110 | // Used to notify events. All events are notified on UI thread. |
| 111 | class Observer { |
| 112 | public: |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 113 | // Triggered when a file has been pinned successfully. |
| 114 | virtual void OnCachePinned(const std::string& resource_id, |
| 115 | const std::string& md5) {} |
| 116 | |
| 117 | // Triggered when a file has been unpinned successfully. |
| 118 | virtual void OnCacheUnpinned(const std::string& resource_id, |
| 119 | const std::string& md5) {} |
| 120 | |
[email protected] | d7664c2 | 2012-06-18 19:35:49 | [diff] [blame] | 121 | // Triggered when a dirty file has been committed (saved) successfully. |
| 122 | virtual void OnCacheCommitted(const std::string& resource_id) {} |
| 123 | |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 124 | protected: |
| 125 | virtual ~Observer() {} |
| 126 | }; |
| 127 | |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 128 | // Returns the sub-directory under gdata cache directory for the given sub |
| 129 | // directory type. Example: <user_profile_dir>/GCache/v1/tmp |
[email protected] | 6b70c7b | 2012-06-14 03:10:43 | [diff] [blame] | 130 | // |
| 131 | // Can be called on any thread. |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 132 | FilePath GetCacheDirectoryPath(CacheSubDirectoryType sub_dir_type) const; |
| 133 | |
| 134 | // Returns absolute path of the file if it were cached or to be cached. |
[email protected] | 6b70c7b | 2012-06-14 03:10:43 | [diff] [blame] | 135 | // |
| 136 | // Can be called on any thread. |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 137 | FilePath GetCacheFilePath(const std::string& resource_id, |
| 138 | const std::string& md5, |
| 139 | CacheSubDirectoryType sub_dir_type, |
| 140 | CachedFileOrigin file_orign) const; |
| 141 | |
[email protected] | 01ba15f7 | 2012-06-09 00:41:05 | [diff] [blame] | 142 | // Returns true if the given path is under gdata cache directory, i.e. |
| 143 | // <user_profile_dir>/GCache/v1 |
[email protected] | 6b70c7b | 2012-06-14 03:10:43 | [diff] [blame] | 144 | // |
| 145 | // Can be called on any thread. |
[email protected] | 01ba15f7 | 2012-06-09 00:41:05 | [diff] [blame] | 146 | bool IsUnderGDataCacheDirectory(const FilePath& path) const; |
| 147 | |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 148 | // Adds observer. |
| 149 | // Must be called on UI thread. |
| 150 | void AddObserver(Observer* observer); |
| 151 | |
| 152 | // Removes observer. |
| 153 | // Must be called on UI thread. |
| 154 | void RemoveObserver(Observer* observer); |
| 155 | |
[email protected] | 4324fdc | 2012-06-29 05:32:48 | [diff] [blame] | 156 | // Gets the cache entry by the given resource ID and MD5. |
| 157 | // See also GetCacheEntry(). |
| 158 | // |
| 159 | // Must be called on UI thread. |callback| is run on UI thread. |
| 160 | void GetCacheEntryOnUIThread( |
| 161 | const std::string& resource_id, |
| 162 | const std::string& md5, |
| 163 | const GetCacheEntryCallback& callback); |
| 164 | |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 165 | // Gets the resource IDs of pinned-but-not-fetched files and |
| 166 | // dirty-but-not-uploaded files. |
| 167 | // |
[email protected] | 8764a39 | 2012-06-20 06:43:08 | [diff] [blame] | 168 | // Must be called on UI thread. |callback| is run on UI thread. |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 169 | void GetResourceIdsOfBacklogOnUIThread( |
[email protected] | 85b6219 | 2012-06-29 19:56:38 | [diff] [blame] | 170 | const GetResourceIdsOfBacklogCallback& callback); |
| 171 | |
[email protected] | d7754f5 | 2012-08-01 08:45:33 | [diff] [blame] | 172 | // Gets the resource IDs of all existing (i.e. cached locally) pinned |
[email protected] | cd23643 | 2012-07-27 18:03:30 | [diff] [blame] | 173 | // files, including pinned dirty files. |
[email protected] | 85b6219 | 2012-06-29 19:56:38 | [diff] [blame] | 174 | // |
| 175 | // Must be called on UI thread. |callback| is run on UI thread. |
| 176 | void GetResourceIdsOfExistingPinnedFilesOnUIThread( |
[email protected] | 8764a39 | 2012-06-20 06:43:08 | [diff] [blame] | 177 | const GetResourceIdsCallback& callback); |
| 178 | |
[email protected] | cd23643 | 2012-07-27 18:03:30 | [diff] [blame] | 179 | // Gets the resource IDs of all files in the cache. |
| 180 | // |
| 181 | // Must be called on UI thread. |callback| is run on UI thread. |
| 182 | void GetResourceIdsOfAllFilesOnUIThread( |
| 183 | const GetResourceIdsCallback& callback); |
| 184 | |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 185 | // Frees up disk space to store the given number of bytes, while keeping |
| 186 | // kMinFreSpace bytes on the disk, if needed. |has_enough_space| is |
| 187 | // updated to indicate if we have enough space. |
| 188 | void FreeDiskSpaceIfNeededFor(int64 num_bytes, |
| 189 | bool* has_enough_space); |
| 190 | |
| 191 | // Checks if file corresponding to |resource_id| and |md5| exists in cache. |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 192 | void GetFileOnUIThread(const std::string& resource_id, |
| 193 | const std::string& md5, |
| 194 | const GetFileFromCacheCallback& callback); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 195 | |
| 196 | // Modifies cache state, which involves the following: |
| 197 | // - moves or copies (per |file_operation_type|) |source_path| |
| 198 | // to |dest_path| in the cache dir |
| 199 | // - if necessary, creates symlink |
| 200 | // - deletes stale cached versions of |resource_id| in |
| 201 | // |dest_path|'s directory. |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 202 | void StoreOnUIThread(const std::string& resource_id, |
| 203 | const std::string& md5, |
| 204 | const FilePath& source_path, |
| 205 | FileOperationType file_operation_type, |
| 206 | const CacheOperationCallback& callback); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 207 | |
| 208 | // Modifies cache state, which involves the following: |
| 209 | // - moves |source_path| to |dest_path| in persistent dir if |
| 210 | // file is not dirty |
| 211 | // - creates symlink in pinned dir that references downloaded or locally |
| 212 | // modified file |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 213 | void PinOnUIThread(const std::string& resource_id, |
| 214 | const std::string& md5, |
| 215 | const CacheOperationCallback& callback); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 216 | |
| 217 | // Modifies cache state, which involves the following: |
| 218 | // - moves |source_path| to |dest_path| in tmp dir if file is not dirty |
| 219 | // - deletes symlink from pinned dir |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 220 | void UnpinOnUIThread(const std::string& resource_id, |
| 221 | const std::string& md5, |
| 222 | const CacheOperationCallback& callback); |
[email protected] | 7986b8d | 2012-06-14 15:05:14 | [diff] [blame] | 223 | |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 224 | // Modifies cache state, which involves the following: |
| 225 | // - moves |source_path| to |dest_path|, where |
| 226 | // if we're mounting: |source_path| is the unmounted path and has .<md5> |
| 227 | // extension, and |dest_path| is the mounted path in persistent dir |
| 228 | // and has .<md5>.mounted extension; |
| 229 | // if we're unmounting: the opposite is true for the two paths, i.e. |
| 230 | // |dest_path| is the mounted path and |source_path| the unmounted path. |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 231 | void SetMountedStateOnUIThread(const FilePath& file_path, |
| 232 | bool to_mount, |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 233 | const ChangeCacheStateCallback& callback); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 234 | |
| 235 | // Modifies cache state, which involves the following: |
| 236 | // - moves |source_path| to |dest_path| in persistent dir, where |
| 237 | // |source_path| has .<md5> extension and |dest_path| has .local extension |
| 238 | // - if file is pinned, updates symlink in pinned dir to reference dirty file |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 239 | void MarkDirtyOnUIThread(const std::string& resource_id, |
| 240 | const std::string& md5, |
| 241 | const GetFileFromCacheCallback& callback); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 242 | |
| 243 | // Modifies cache state, i.e. creates symlink in outgoing |
| 244 | // dir to reference dirty file in persistent dir. |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 245 | void CommitDirtyOnUIThread(const std::string& resource_id, |
| 246 | const std::string& md5, |
| 247 | const CacheOperationCallback& callback); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 248 | |
| 249 | // Modifies cache state, which involves the following: |
| 250 | // - moves |source_path| to |dest_path| in persistent dir if |
| 251 | // file is pinned or tmp dir otherwise, where |source_path| has .local |
| 252 | // extension and |dest_path| has .<md5> extension |
| 253 | // - deletes symlink in outgoing dir |
| 254 | // - if file is pinned, updates symlink in pinned dir to reference |
| 255 | // |dest_path| |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 256 | void ClearDirtyOnUIThread(const std::string& resource_id, |
| 257 | const std::string& md5, |
| 258 | const CacheOperationCallback& callback); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 259 | |
| 260 | // Does the following: |
| 261 | // - remove all delete stale cache versions corresponding to |resource_id| in |
| 262 | // persistent, tmp and pinned directories |
| 263 | // - remove entry corresponding to |resource_id| from cache map. |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 264 | void RemoveOnUIThread(const std::string& resource_id, |
| 265 | const CacheOperationCallback& callback); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 266 | |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 267 | // Does the following: |
| 268 | // - remove all the files in the cache directory. |
| 269 | // - re-create the |metadata_| instance. |
| 270 | void ClearAllOnUIThread(const ChangeCacheStateCallback& callback); |
| 271 | |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 272 | // Utility method to call Initialize on UI thread. |
| 273 | void RequestInitializeOnUIThread(); |
| 274 | |
[email protected] | d310bfc | 2012-08-10 09:41:28 | [diff] [blame^] | 275 | // Utility method to call InitializeForTesting on UI thread. |
| 276 | void RequestInitializeOnUIThreadForTesting(); |
| 277 | |
[email protected] | 79c3752d | 2012-07-17 12:10:08 | [diff] [blame] | 278 | // Force a rescan of cache files, for testing. |
| 279 | void ForceRescanOnUIThreadForTesting(); |
| 280 | |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 281 | // Gets the cache entry for file corresponding to |resource_id| and |md5| |
| 282 | // and returns true if entry exists in cache map. Otherwise, returns false. |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 283 | // |md5| can be empty if only matching |resource_id| is desired, which may |
| 284 | // happen when looking for pinned entries where symlinks' filenames have no |
| 285 | // extension and hence no md5. |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 286 | bool GetCacheEntry(const std::string& resource_id, |
| 287 | const std::string& md5, |
| 288 | GDataCacheEntry* entry); |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 289 | |
| 290 | // Factory methods for GDataCache. |
[email protected] | fcc92a5 | 2012-06-08 22:54:16 | [diff] [blame] | 291 | // |pool| and |sequence_token| are used to assert that the functions are |
| 292 | // called on the right sequenced worker pool with the right sequence token. |
| 293 | // |
| 294 | // For testing, the thread assertion can be disabled by passing NULL and |
| 295 | // the default value of SequenceToken. |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 296 | static GDataCache* CreateGDataCacheOnUIThread( |
[email protected] | fcc92a5 | 2012-06-08 22:54:16 | [diff] [blame] | 297 | const FilePath& cache_root_path, |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 298 | base::SequencedTaskRunner* blocking_task_runner); |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 299 | |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 300 | // Deletes the cache. |
| 301 | void DestroyOnUIThread(); |
| 302 | |
[email protected] | 01ba15f7 | 2012-06-09 00:41:05 | [diff] [blame] | 303 | // Gets the cache root path (i.e. <user_profile_dir>/GCache/v1) from the |
| 304 | // profile. |
| 305 | // TODO(satorux): Write a unit test for this. |
| 306 | static FilePath GetCacheRootPath(Profile* profile); |
| 307 | |
[email protected] | 30d9dda | 2012-06-30 05:56:28 | [diff] [blame] | 308 | // Returns file paths for all the cache sub directories under |
| 309 | // |cache_root_path|. |
| 310 | static std::vector<FilePath> GetCachePaths(const FilePath& cache_root_path); |
| 311 | |
| 312 | // Creates cache directory and its sub-directories if they don't exist. |
| 313 | // TODO(glotov): take care of this when the setup and cleanup part is |
| 314 | // landed, noting that these directories need to be created for development |
| 315 | // in linux box and unittest. (http://crosbug.com/27577) |
| 316 | static bool CreateCacheDirectories( |
| 317 | const std::vector<FilePath>& paths_to_create); |
| 318 | |
[email protected] | fae353a | 2012-07-11 23:30:27 | [diff] [blame] | 319 | // Returns the type of the sub directory where the cache file is stored. |
| 320 | static CacheSubDirectoryType GetSubDirectoryType( |
| 321 | const GDataCacheEntry& cache_entry); |
| 322 | |
[email protected] | ca5f6da | 2012-06-18 12:54:59 | [diff] [blame] | 323 | private: |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 324 | GDataCache(const FilePath& cache_root_path, |
| 325 | base::SequencedTaskRunner* blocking_task_runner); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 326 | virtual ~GDataCache(); |
[email protected] | fcc92a5 | 2012-06-08 22:54:16 | [diff] [blame] | 327 | |
| 328 | // Checks whether the current thread is on the right sequenced worker pool |
| 329 | // with the right sequence ID. If not, DCHECK will fail. |
| 330 | void AssertOnSequencedWorkerPool(); |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 331 | |
[email protected] | ca5f6da | 2012-06-18 12:54:59 | [diff] [blame] | 332 | // Initializes the cache. |
| 333 | void Initialize(); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 334 | |
[email protected] | d310bfc | 2012-08-10 09:41:28 | [diff] [blame^] | 335 | // Initializes the cache with in-memory cache for testing. |
| 336 | // The in-memory cache is used since it's faster than the db. |
| 337 | void InitializeForTesting(); |
| 338 | |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 339 | // Deletes the cache. |
| 340 | void Destroy(); |
| 341 | |
[email protected] | 79c3752d | 2012-07-17 12:10:08 | [diff] [blame] | 342 | // Force a rescan of cache directories. |
| 343 | void ForceRescanForTesting(); |
| 344 | |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 345 | // Used to implement GetResourceIdsOfBacklogOnUIThread. |
| 346 | void GetResourceIdsOfBacklog( |
| 347 | std::vector<std::string>* to_fetch, |
| 348 | std::vector<std::string>* to_upload); |
[email protected] | 8764a39 | 2012-06-20 06:43:08 | [diff] [blame] | 349 | |
[email protected] | 85b6219 | 2012-06-29 19:56:38 | [diff] [blame] | 350 | // Used to implement GetResourceIdsOfExistingPinnedFilesOnUIThread. |
| 351 | void GetResourceIdsOfExistingPinnedFiles( |
| 352 | std::vector<std::string>* resource_ids); |
| 353 | |
[email protected] | cd23643 | 2012-07-27 18:03:30 | [diff] [blame] | 354 | // Used to implement GetResourceIdsOfAllFilesOnUIThread. |
| 355 | void GetResourceIdsOfAllFiles( |
| 356 | std::vector<std::string>* resource_ids); |
| 357 | |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 358 | // Used to implement GetFileOnUIThread. |
| 359 | void GetFile(const std::string& resource_id, |
| 360 | const std::string& md5, |
[email protected] | 7f90e77c | 2012-07-17 09:35:31 | [diff] [blame] | 361 | GDataFileError* error, |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 362 | FilePath* cache_file_path); |
| 363 | |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 364 | // Used to implement StoreOnUIThread. |
| 365 | void Store(const std::string& resource_id, |
| 366 | const std::string& md5, |
| 367 | const FilePath& source_path, |
| 368 | FileOperationType file_operation_type, |
[email protected] | 7f90e77c | 2012-07-17 09:35:31 | [diff] [blame] | 369 | GDataFileError* error); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 370 | |
| 371 | // Used to implement PinOnUIThread. |
| 372 | void Pin(const std::string& resource_id, |
| 373 | const std::string& md5, |
| 374 | FileOperationType file_operation_type, |
[email protected] | 7f90e77c | 2012-07-17 09:35:31 | [diff] [blame] | 375 | GDataFileError* error); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 376 | |
| 377 | // Used to implement UnpinOnUIThread. |
| 378 | void Unpin(const std::string& resource_id, |
| 379 | const std::string& md5, |
| 380 | FileOperationType file_operation_type, |
[email protected] | 7f90e77c | 2012-07-17 09:35:31 | [diff] [blame] | 381 | GDataFileError* error); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 382 | |
| 383 | // Used to implement SetMountedStateOnUIThread. |
| 384 | void SetMountedState(const FilePath& file_path, |
| 385 | bool to_mount, |
[email protected] | 7f90e77c | 2012-07-17 09:35:31 | [diff] [blame] | 386 | GDataFileError* error, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 387 | FilePath* cache_file_path); |
| 388 | |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 389 | // Used to implement MarkDirtyOnUIThread. |
| 390 | void MarkDirty(const std::string& resource_id, |
| 391 | const std::string& md5, |
| 392 | FileOperationType file_operation_type, |
[email protected] | 7f90e77c | 2012-07-17 09:35:31 | [diff] [blame] | 393 | GDataFileError* error, |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 394 | FilePath* cache_file_path); |
| 395 | |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 396 | // Used to implement CommitDirtyOnUIThread. |
| 397 | void CommitDirty(const std::string& resource_id, |
| 398 | const std::string& md5, |
| 399 | FileOperationType file_operation_type, |
[email protected] | 7f90e77c | 2012-07-17 09:35:31 | [diff] [blame] | 400 | GDataFileError* error); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 401 | |
| 402 | // Used to implement ClearDirtyOnUIThread. |
| 403 | void ClearDirty(const std::string& resource_id, |
| 404 | const std::string& md5, |
| 405 | FileOperationType file_operation_type, |
[email protected] | 7f90e77c | 2012-07-17 09:35:31 | [diff] [blame] | 406 | GDataFileError* error); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 407 | |
| 408 | // Used to implement RemoveOnUIThread. |
| 409 | void Remove(const std::string& resource_id, |
[email protected] | 7f90e77c | 2012-07-17 09:35:31 | [diff] [blame] | 410 | GDataFileError* error); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 411 | |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 412 | // Used to implement ClearAllUIThread. |
| 413 | void ClearAll(GDataFileError* error); |
| 414 | |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 415 | // Runs callback and notifies the observers when file is pinned. |
[email protected] | 7f90e77c | 2012-07-17 09:35:31 | [diff] [blame] | 416 | void OnPinned(GDataFileError* error, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 417 | const std::string& resource_id, |
| 418 | const std::string& md5, |
| 419 | const CacheOperationCallback& callback); |
| 420 | |
| 421 | // Runs callback and notifies the observers when file is unpinned. |
[email protected] | 7f90e77c | 2012-07-17 09:35:31 | [diff] [blame] | 422 | void OnUnpinned(GDataFileError* error, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 423 | const std::string& resource_id, |
| 424 | const std::string& md5, |
| 425 | const CacheOperationCallback& callback); |
| 426 | |
[email protected] | d7664c2 | 2012-06-18 19:35:49 | [diff] [blame] | 427 | // Runs callback and notifies the observers when file is committed. |
[email protected] | 7f90e77c | 2012-07-17 09:35:31 | [diff] [blame] | 428 | void OnCommitDirty(GDataFileError* error, |
[email protected] | d7664c2 | 2012-06-18 19:35:49 | [diff] [blame] | 429 | const std::string& resource_id, |
| 430 | const std::string& md5, |
| 431 | const CacheOperationCallback& callback); |
| 432 | |
[email protected] | 4324fdc | 2012-06-29 05:32:48 | [diff] [blame] | 433 | // Helper function to implement GetCacheEntryOnUIThread(). |
| 434 | void GetCacheEntryHelper(const std::string& resource_id, |
| 435 | const std::string& md5, |
| 436 | bool* success, |
[email protected] | fae353a | 2012-07-11 23:30:27 | [diff] [blame] | 437 | GDataCacheEntry* cache_entry); |
[email protected] | 4324fdc | 2012-06-29 05:32:48 | [diff] [blame] | 438 | |
[email protected] | 01ba15f7 | 2012-06-09 00:41:05 | [diff] [blame] | 439 | // The root directory of the cache (i.e. <user_profile_dir>/GCache/v1). |
| 440 | const FilePath cache_root_path_; |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 441 | // Paths for all subdirectories of GCache, one for each |
| 442 | // GDataCache::CacheSubDirectoryType enum. |
[email protected] | 6b70c7b | 2012-06-14 03:10:43 | [diff] [blame] | 443 | const std::vector<FilePath> cache_paths_; |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 444 | scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 445 | |
[email protected] | ca5f6da | 2012-06-18 12:54:59 | [diff] [blame] | 446 | // The cache state data. This member must be access only on the blocking pool. |
| 447 | scoped_ptr<GDataCacheMetadata> metadata_; |
| 448 | |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 449 | // List of observers, this member must be accessed on UI thread. |
| 450 | ObserverList<Observer> observers_; |
| 451 | |
[email protected] | e53ac8f | 2012-08-02 07:05:00 | [diff] [blame] | 452 | // Note: This should remain the last member so it'll be destroyed and |
| 453 | // invalidate its weak pointers before any other members are destroyed. |
| 454 | base::WeakPtrFactory<GDataCache> weak_ptr_factory_; |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 455 | DISALLOW_COPY_AND_ASSIGN(GDataCache); |
| 456 | }; |
| 457 | |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 458 | |
| 459 | // The minimum free space to keep. GDataFileSystem::GetFileByPath() returns |
[email protected] | 7f90e77c | 2012-07-17 09:35:31 | [diff] [blame] | 460 | // GDATA_FILE_ERROR_NO_SPACE if the available space is smaller than |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 461 | // this value. |
| 462 | // |
| 463 | // Copied from cryptohome/homedirs.h. |
| 464 | // TODO(satorux): Share the constant. |
| 465 | const int64 kMinFreeSpace = 512 * 1LL << 20; |
| 466 | |
| 467 | // Interface class used for getting the free disk space. Only for testing. |
| 468 | class FreeDiskSpaceGetterInterface { |
| 469 | public: |
| 470 | virtual ~FreeDiskSpaceGetterInterface() {} |
| 471 | virtual int64 AmountOfFreeDiskSpace() const = 0; |
| 472 | }; |
| 473 | |
| 474 | // Sets the free disk space getter for testing. |
| 475 | // The existing getter is deleted. |
| 476 | void SetFreeDiskSpaceGetterForTesting( |
| 477 | FreeDiskSpaceGetterInterface* getter); |
| 478 | |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 479 | } // namespace gdata |
| 480 | |
| 481 | #endif // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_CACHE_H_ |