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