[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 | |
[email protected] | 15de814 | 2012-10-11 06:00:54 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_CACHE_H_ |
| 6 | #define CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_CACHE_H_ |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 7 | |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 8 | #include <string> |
[email protected] | 79c3752d | 2012-07-17 12:10:08 | [diff] [blame] | 9 | #include <vector> |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 10 | |
[email protected] | dd91da8 | 2012-09-07 23:49:49 | [diff] [blame] | 11 | #include "base/callback_forward.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame^] | 12 | #include "base/files/file_path.h" |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 13 | #include "base/memory/scoped_ptr.h" |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 14 | #include "base/memory/weak_ptr.h" |
| 15 | #include "base/observer_list.h" |
[email protected] | d68ede0 | 2012-11-07 14:30:53 | [diff] [blame] | 16 | #include "chrome/browser/chromeos/drive/drive_cache_metadata.h" |
[email protected] | ae7c98f9 | 2012-10-11 09:51:24 | [diff] [blame] | 17 | #include "chrome/browser/chromeos/drive/drive_file_error.h" |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 18 | |
[email protected] | 01ba15f7 | 2012-06-09 00:41:05 | [diff] [blame] | 19 | class Profile; |
| 20 | |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 21 | namespace base { |
| 22 | |
| 23 | class SequencedTaskRunner; |
| 24 | |
| 25 | } // namespace base |
| 26 | |
[email protected] | d9d04df | 2012-10-12 07:06:35 | [diff] [blame] | 27 | namespace drive { |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 28 | |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 29 | class DriveCacheEntry; |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 30 | class DriveCacheMetadata; |
[email protected] | a0927550 | 2012-10-10 04:48:01 | [diff] [blame] | 31 | class DriveCacheObserver; |
[email protected] | ca5f6da | 2012-06-18 12:54:59 | [diff] [blame] | 32 | |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 33 | // Callback for GetFileFromCache. |
[email protected] | 11f60db | 2012-08-23 16:28:15 | [diff] [blame] | 34 | typedef base::Callback<void(DriveFileError error, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 35 | const base::FilePath& cache_file_path)> |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 36 | GetFileFromCacheCallback; |
| 37 | |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 38 | // Callback for GetCacheEntry. |
[email protected] | fae353a | 2012-07-11 23:30:27 | [diff] [blame] | 39 | // |success| indicates if the operation was successful. |
| 40 | // |cache_entry| is the obtained cache entry. On failure, |cache_state| is |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 41 | // set to TEST_CACHE_STATE_NONE. |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 42 | typedef base::Callback<void(bool success, const DriveCacheEntry& cache_entry)> |
[email protected] | fae353a | 2012-07-11 23:30:27 | [diff] [blame] | 43 | GetCacheEntryCallback; |
| 44 | |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 45 | // Callback for RequestInitialize. |
[email protected] | 322e003 | 2012-10-07 01:55:53 | [diff] [blame] | 46 | // |success| indicates if the operation was successful. |
| 47 | // TODO(satorux): Change this to DriveFileError when it becomes necessary. |
| 48 | typedef base::Callback<void(bool success)> |
| 49 | InitializeCacheCallback; |
| 50 | |
[email protected] | f6fd98a | 2012-12-14 00:04:02 | [diff] [blame] | 51 | // Interface class used for getting the free disk space. Tests can inject an |
| 52 | // implementation that reports fake free disk space. |
| 53 | class FreeDiskSpaceGetterInterface { |
| 54 | public: |
| 55 | virtual ~FreeDiskSpaceGetterInterface() {} |
[email protected] | cf64404b | 2012-12-14 07:12:50 | [diff] [blame] | 56 | virtual int64 AmountOfFreeDiskSpace() = 0; |
[email protected] | f6fd98a | 2012-12-14 00:04:02 | [diff] [blame] | 57 | }; |
| 58 | |
[email protected] | 7f53dc7 | 2012-08-23 19:06:55 | [diff] [blame] | 59 | // DriveCache is used to maintain cache states of DriveFileSystem. |
[email protected] | 6b70c7b | 2012-06-14 03:10:43 | [diff] [blame] | 60 | // |
| 61 | // All non-static public member functions, unless mentioned otherwise (see |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 62 | // GetCacheFilePath() for example), should be called from the UI thread. |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 63 | class DriveCache { |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 64 | public: |
| 65 | // Enum defining GCache subdirectory location. |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 66 | // This indexes into |DriveCache::cache_paths_| vector. |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 67 | enum CacheSubDirectoryType { |
| 68 | CACHE_TYPE_META = 0, // Downloaded feeds. |
| 69 | CACHE_TYPE_PINNED, // Symlinks to files in persistent dir that are |
| 70 | // pinned, or to /dev/null for non-existent |
| 71 | // files. |
| 72 | CACHE_TYPE_OUTGOING, // Symlinks to files in persistent or tmp dir to |
| 73 | // be uploaded. |
| 74 | CACHE_TYPE_PERSISTENT, // Files that are pinned or modified locally, |
| 75 | // not evictable, hopefully. |
| 76 | CACHE_TYPE_TMP, // Files that don't meet criteria to be in |
| 77 | // persistent dir, and hence evictable. |
| 78 | CACHE_TYPE_TMP_DOWNLOADS, // Downloaded files. |
| 79 | CACHE_TYPE_TMP_DOCUMENTS, // Temporary JSON files for hosted documents. |
| 80 | NUM_CACHE_TYPES, // This must be at the end. |
| 81 | }; |
| 82 | |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 83 | // Enum defining origin of a cached file. |
| 84 | enum CachedFileOrigin { |
| 85 | CACHED_FILE_FROM_SERVER = 0, |
| 86 | CACHED_FILE_LOCALLY_MODIFIED, |
| 87 | CACHED_FILE_MOUNTED, |
| 88 | }; |
| 89 | |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 90 | // Enum defining type of file operation e.g. copy or move, etc. |
| 91 | enum FileOperationType { |
| 92 | FILE_OPERATION_MOVE = 0, |
| 93 | FILE_OPERATION_COPY, |
| 94 | }; |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 95 | |
[email protected] | 17196ee | 2012-12-13 06:23:51 | [diff] [blame] | 96 | // |cache_root_path| specifies the root directory for the cache. Sub |
| 97 | // directories will be created under the root directory. |
| 98 | // |
| 99 | // |blocking_task_runner| is used to post a task to the blocking worker |
| 100 | // pool for file operations. Must not be null. |
[email protected] | f6fd98a | 2012-12-14 00:04:02 | [diff] [blame] | 101 | // |
| 102 | // |free_disk_space_getter| is used to inject a custom free disk space |
| 103 | // getter for testing. NULL must be passed for production code. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 104 | DriveCache(const base::FilePath& cache_root_path, |
[email protected] | f6fd98a | 2012-12-14 00:04:02 | [diff] [blame] | 105 | base::SequencedTaskRunner* blocking_task_runner, |
| 106 | FreeDiskSpaceGetterInterface* free_disk_space_getter); |
[email protected] | 17196ee | 2012-12-13 06:23:51 | [diff] [blame] | 107 | |
[email protected] | 7bb73e3d | 2012-09-08 17:41:29 | [diff] [blame] | 108 | // Returns the sub-directory under drive cache directory for the given sub |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 109 | // directory type. Example: <user_profile_dir>/GCache/v1/tmp |
[email protected] | 6b70c7b | 2012-06-14 03:10:43 | [diff] [blame] | 110 | // |
| 111 | // Can be called on any thread. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 112 | base::FilePath GetCacheDirectoryPath( |
| 113 | CacheSubDirectoryType sub_dir_type) const; |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 114 | |
| 115 | // 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] | 116 | // |
| 117 | // Can be called on any thread. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 118 | base::FilePath GetCacheFilePath(const std::string& resource_id, |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 119 | const std::string& md5, |
| 120 | CacheSubDirectoryType sub_dir_type, |
[email protected] | 2fdc0aeb | 2012-10-16 12:13:44 | [diff] [blame] | 121 | CachedFileOrigin file_origin) const; |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 122 | |
[email protected] | 7bb73e3d | 2012-09-08 17:41:29 | [diff] [blame] | 123 | // Returns true if the given path is under drive cache directory, i.e. |
[email protected] | 01ba15f7 | 2012-06-09 00:41:05 | [diff] [blame] | 124 | // <user_profile_dir>/GCache/v1 |
[email protected] | 6b70c7b | 2012-06-14 03:10:43 | [diff] [blame] | 125 | // |
| 126 | // Can be called on any thread. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 127 | bool IsUnderDriveCacheDirectory(const base::FilePath& path) const; |
[email protected] | 01ba15f7 | 2012-06-09 00:41:05 | [diff] [blame] | 128 | |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 129 | // Adds observer. |
[email protected] | a0927550 | 2012-10-10 04:48:01 | [diff] [blame] | 130 | void AddObserver(DriveCacheObserver* observer); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 131 | |
| 132 | // Removes observer. |
[email protected] | a0927550 | 2012-10-10 04:48:01 | [diff] [blame] | 133 | void RemoveObserver(DriveCacheObserver* observer); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 134 | |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 135 | // Gets the cache entry for file corresponding to |resource_id| and |md5| |
[email protected] | fba5954 | 2012-12-18 06:05:38 | [diff] [blame] | 136 | // and runs |callback| with true and the entry found if entry exists in cache |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 137 | // map. Otherwise, runs |callback| with false. |
| 138 | // |md5| can be empty if only matching |resource_id| is desired, which may |
| 139 | // happen when looking for pinned entries where symlinks' filenames have no |
| 140 | // extension and hence no md5. |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 141 | // |callback| must not be null. |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 142 | void GetCacheEntry(const std::string& resource_id, |
| 143 | const std::string& md5, |
| 144 | const GetCacheEntryCallback& callback); |
[email protected] | 4324fdc | 2012-06-29 05:32:48 | [diff] [blame] | 145 | |
[email protected] | d68ede0 | 2012-11-07 14:30:53 | [diff] [blame] | 146 | // Iterates all files in the cache and calls |iteration_callback| for each |
| 147 | // file. |completion_callback| is run upon completion. |
| 148 | void Iterate(const CacheIterateCallback& iteration_callback, |
| 149 | const base::Closure& completion_callback); |
[email protected] | cd23643 | 2012-07-27 18:03:30 | [diff] [blame] | 150 | |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 151 | // Frees up disk space to store the given number of bytes, while keeping |
[email protected] | 71314de | 2012-10-30 13:07:51 | [diff] [blame] | 152 | // kMinFreeSpace bytes on the disk, if needed. |
[email protected] | f423c0b | 2012-11-22 09:51:10 | [diff] [blame] | 153 | // Runs |callback| with true when we successfully manage to have enough space. |
| 154 | void FreeDiskSpaceIfNeededFor(int64 num_bytes, |
| 155 | const InitializeCacheCallback& callback); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 156 | |
| 157 | // Checks if file corresponding to |resource_id| and |md5| exists in cache. |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 158 | // |callback| must not be null. |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 159 | void GetFile(const std::string& resource_id, |
| 160 | const std::string& md5, |
| 161 | const GetFileFromCacheCallback& callback); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 162 | |
| 163 | // Modifies cache state, which involves the following: |
| 164 | // - moves or copies (per |file_operation_type|) |source_path| |
| 165 | // to |dest_path| in the cache dir |
| 166 | // - if necessary, creates symlink |
| 167 | // - deletes stale cached versions of |resource_id| in |
| 168 | // |dest_path|'s directory. |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 169 | // |callback| must not be null. |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 170 | void Store(const std::string& resource_id, |
| 171 | const std::string& md5, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 172 | const base::FilePath& source_path, |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 173 | FileOperationType file_operation_type, |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 174 | const FileOperationCallback& callback); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 175 | |
| 176 | // Modifies cache state, which involves the following: |
| 177 | // - moves |source_path| to |dest_path| in persistent dir if |
| 178 | // file is not dirty |
| 179 | // - creates symlink in pinned dir that references downloaded or locally |
| 180 | // modified file |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 181 | // |callback| must not be null. |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 182 | void Pin(const std::string& resource_id, |
| 183 | const std::string& md5, |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 184 | const FileOperationCallback& callback); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 185 | |
| 186 | // Modifies cache state, which involves the following: |
| 187 | // - moves |source_path| to |dest_path| in tmp dir if file is not dirty |
| 188 | // - deletes symlink from pinned dir |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 189 | // |callback| must not be null. |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 190 | void Unpin(const std::string& resource_id, |
| 191 | const std::string& md5, |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 192 | const FileOperationCallback& callback); |
[email protected] | 7986b8d | 2012-06-14 15:05:14 | [diff] [blame] | 193 | |
[email protected] | 35c1f9b | 2013-02-07 07:39:42 | [diff] [blame] | 194 | // Sets the state of the cache entry corresponding to |resource_id| and |md5| |
| 195 | // as mounted. |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 196 | // |callback| must not be null. |
[email protected] | 35c1f9b | 2013-02-07 07:39:42 | [diff] [blame] | 197 | void MarkAsMounted(const std::string& resource_id, |
| 198 | const std::string& md5, |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 199 | const GetFileFromCacheCallback& callback); |
| 200 | |
| 201 | // Set the state of the cache entry corresponding to file_path as unmounted. |
| 202 | // |callback| must not be null. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 203 | void MarkAsUnmounted(const base::FilePath& file_path, |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 204 | const FileOperationCallback& callback); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 205 | |
| 206 | // Modifies cache state, which involves the following: |
| 207 | // - moves |source_path| to |dest_path| in persistent dir, where |
| 208 | // |source_path| has .<md5> extension and |dest_path| has .local extension |
| 209 | // - if file is pinned, updates symlink in pinned dir to reference dirty file |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 210 | // |callback| must not be null. |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 211 | void MarkDirty(const std::string& resource_id, |
| 212 | const std::string& md5, |
[email protected] | bc809e4 | 2012-11-28 04:46:29 | [diff] [blame] | 213 | const FileOperationCallback& callback); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 214 | |
| 215 | // Modifies cache state, i.e. creates symlink in outgoing |
| 216 | // dir to reference dirty file in persistent dir. |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 217 | // |callback| must not be null. |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 218 | void CommitDirty(const std::string& resource_id, |
| 219 | const std::string& md5, |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 220 | const FileOperationCallback& callback); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 221 | |
| 222 | // Modifies cache state, which involves the following: |
| 223 | // - moves |source_path| to |dest_path| in persistent dir if |
| 224 | // file is pinned or tmp dir otherwise, where |source_path| has .local |
| 225 | // extension and |dest_path| has .<md5> extension |
| 226 | // - deletes symlink in outgoing dir |
| 227 | // - if file is pinned, updates symlink in pinned dir to reference |
| 228 | // |dest_path| |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 229 | // |callback| must not be null. |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 230 | void ClearDirty(const std::string& resource_id, |
| 231 | const std::string& md5, |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 232 | const FileOperationCallback& callback); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 233 | |
| 234 | // Does the following: |
| 235 | // - remove all delete stale cache versions corresponding to |resource_id| in |
| 236 | // persistent, tmp and pinned directories |
| 237 | // - remove entry corresponding to |resource_id| from cache map. |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 238 | // |callback| must not be null. |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 239 | void Remove(const std::string& resource_id, |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 240 | const FileOperationCallback& callback); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 241 | |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 242 | // Does the following: |
| 243 | // - remove all the files in the cache directory. |
| 244 | // - re-create the |metadata_| instance. |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 245 | // |callback| must not be null. |
| 246 | void ClearAll(const InitializeCacheCallback& callback); |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 247 | |
[email protected] | 322e003 | 2012-10-07 01:55:53 | [diff] [blame] | 248 | // Utility method to call Initialize on UI thread. |callback| is called on |
| 249 | // UI thread when the initialization is complete. |
| 250 | // |callback| must not be null. |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 251 | void RequestInitialize(const InitializeCacheCallback& callback); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 252 | |
[email protected] | d310bfc | 2012-08-10 09:41:28 | [diff] [blame] | 253 | // Utility method to call InitializeForTesting on UI thread. |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 254 | void RequestInitializeForTesting(); |
[email protected] | d310bfc | 2012-08-10 09:41:28 | [diff] [blame] | 255 | |
[email protected] | 17196ee | 2012-12-13 06:23:51 | [diff] [blame] | 256 | // Destroys this cache. This function posts a task to the blocking task |
| 257 | // runner to safely delete the object. |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 258 | void Destroy(); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 259 | |
[email protected] | 01ba15f7 | 2012-06-09 00:41:05 | [diff] [blame] | 260 | // Gets the cache root path (i.e. <user_profile_dir>/GCache/v1) from the |
| 261 | // profile. |
| 262 | // TODO(satorux): Write a unit test for this. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 263 | static base::FilePath GetCacheRootPath(Profile* profile); |
[email protected] | 01ba15f7 | 2012-06-09 00:41:05 | [diff] [blame] | 264 | |
[email protected] | 30d9dda | 2012-06-30 05:56:28 | [diff] [blame] | 265 | // Returns file paths for all the cache sub directories under |
| 266 | // |cache_root_path|. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 267 | static std::vector<base::FilePath> GetCachePaths( |
| 268 | const base::FilePath& cache_root_path); |
[email protected] | 30d9dda | 2012-06-30 05:56:28 | [diff] [blame] | 269 | |
| 270 | // Creates cache directory and its sub-directories if they don't exist. |
| 271 | // TODO(glotov): take care of this when the setup and cleanup part is |
| 272 | // landed, noting that these directories need to be created for development |
| 273 | // in linux box and unittest. (http://crosbug.com/27577) |
| 274 | static bool CreateCacheDirectories( |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 275 | const std::vector<base::FilePath>& paths_to_create); |
[email protected] | 30d9dda | 2012-06-30 05:56:28 | [diff] [blame] | 276 | |
[email protected] | fae353a | 2012-07-11 23:30:27 | [diff] [blame] | 277 | // Returns the type of the sub directory where the cache file is stored. |
| 278 | static CacheSubDirectoryType GetSubDirectoryType( |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 279 | const DriveCacheEntry& cache_entry); |
[email protected] | fae353a | 2012-07-11 23:30:27 | [diff] [blame] | 280 | |
[email protected] | ca5f6da | 2012-06-18 12:54:59 | [diff] [blame] | 281 | private: |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 282 | typedef std::pair<DriveFileError, base::FilePath> GetFileResult; |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 283 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 284 | virtual ~DriveCache(); |
[email protected] | fcc92a5 | 2012-06-08 22:54:16 | [diff] [blame] | 285 | |
| 286 | // Checks whether the current thread is on the right sequenced worker pool |
| 287 | // with the right sequence ID. If not, DCHECK will fail. |
| 288 | void AssertOnSequencedWorkerPool(); |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 289 | |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 290 | // Initializes the cache. Returns true on success. |
| 291 | bool InitializeOnBlockingPool(); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 292 | |
[email protected] | d310bfc | 2012-08-10 09:41:28 | [diff] [blame] | 293 | // Initializes the cache with in-memory cache for testing. |
| 294 | // The in-memory cache is used since it's faster than the db. |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 295 | void InitializeOnBlockingPoolForTesting(); |
[email protected] | d310bfc | 2012-08-10 09:41:28 | [diff] [blame] | 296 | |
[email protected] | 17196ee | 2012-12-13 06:23:51 | [diff] [blame] | 297 | // Destroys the cache on the blocking pool. |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 298 | void DestroyOnBlockingPool(); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 299 | |
[email protected] | 717e43c | 2012-11-22 07:47:39 | [diff] [blame] | 300 | // Gets the cache entry by the given resource ID and MD5. |
| 301 | // See also GetCacheEntry(). |
| 302 | bool GetCacheEntryOnBlockingPool(const std::string& resource_id, |
| 303 | const std::string& md5, |
| 304 | DriveCacheEntry* entry); |
| 305 | |
[email protected] | d68ede0 | 2012-11-07 14:30:53 | [diff] [blame] | 306 | // Used to implement Iterate(). |
| 307 | void IterateOnBlockingPool(const CacheIterateCallback& iteration_callback); |
[email protected] | cd23643 | 2012-07-27 18:03:30 | [diff] [blame] | 308 | |
[email protected] | f423c0b | 2012-11-22 09:51:10 | [diff] [blame] | 309 | // Used to implement FreeDiskSpaceIfNeededFor(). |
| 310 | bool FreeDiskSpaceOnBlockingPoolIfNeededFor(int64 num_bytes); |
| 311 | |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 312 | // Used to implement GetFile. |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 313 | scoped_ptr<GetFileResult> GetFileOnBlockingPool( |
| 314 | const std::string& resource_id, |
| 315 | const std::string& md5); |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 316 | |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 317 | // Used to implement Store. |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 318 | DriveFileError StoreOnBlockingPool(const std::string& resource_id, |
| 319 | const std::string& md5, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 320 | const base::FilePath& source_path, |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 321 | FileOperationType file_operation_type); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 322 | |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 323 | // Used to implement Pin. |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 324 | DriveFileError PinOnBlockingPool(const std::string& resource_id, |
[email protected] | 3f5e290 | 2012-11-06 08:21:15 | [diff] [blame] | 325 | const std::string& md5); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 326 | |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 327 | // Used to implement Unpin. |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 328 | DriveFileError UnpinOnBlockingPool(const std::string& resource_id, |
[email protected] | 3f5e290 | 2012-11-06 08:21:15 | [diff] [blame] | 329 | const std::string& md5); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 330 | |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 331 | // Used to implement MarkAsMounted. |
| 332 | scoped_ptr<GetFileResult> MarkAsMountedOnBlockingPool( |
[email protected] | 35c1f9b | 2013-02-07 07:39:42 | [diff] [blame] | 333 | const std::string& resource_id, |
| 334 | const std::string& md5); |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 335 | |
| 336 | // Used to implement MarkAsUnmounted. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 337 | DriveFileError MarkAsUnmountedOnBlockingPool(const base::FilePath& file_path); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 338 | |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 339 | // Used to implement MarkDirty. |
[email protected] | bc809e4 | 2012-11-28 04:46:29 | [diff] [blame] | 340 | DriveFileError MarkDirtyOnBlockingPool(const std::string& resource_id, |
| 341 | const std::string& md5); |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 342 | |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 343 | // Used to implement CommitDirty. |
[email protected] | 3f5e290 | 2012-11-06 08:21:15 | [diff] [blame] | 344 | DriveFileError CommitDirtyOnBlockingPool(const std::string& resource_id, |
| 345 | const std::string& md5); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 346 | |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 347 | // Used to implement ClearDirty. |
[email protected] | 3f5e290 | 2012-11-06 08:21:15 | [diff] [blame] | 348 | DriveFileError ClearDirtyOnBlockingPool(const std::string& resource_id, |
| 349 | const std::string& md5); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 350 | |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 351 | // Used to implement Remove. |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 352 | DriveFileError RemoveOnBlockingPool(const std::string& resource_id); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 353 | |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 354 | // Used to implement ClearAll. |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 355 | bool ClearAllOnBlockingPool(); |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 356 | |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 357 | // Runs callback and notifies the observers when file is pinned. |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 358 | void OnPinned(const std::string& resource_id, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 359 | const std::string& md5, |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 360 | const FileOperationCallback& callback, |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 361 | DriveFileError error); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 362 | |
| 363 | // Runs callback and notifies the observers when file is unpinned. |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 364 | void OnUnpinned(const std::string& resource_id, |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 365 | const std::string& md5, |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 366 | const FileOperationCallback& callback, |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 367 | DriveFileError error); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 368 | |
[email protected] | d7664c2 | 2012-06-18 19:35:49 | [diff] [blame] | 369 | // Runs callback and notifies the observers when file is committed. |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 370 | void OnCommitDirty(const std::string& resource_id, |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 371 | const FileOperationCallback& callback, |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 372 | DriveFileError error); |
[email protected] | 4324fdc | 2012-06-29 05:32:48 | [diff] [blame] | 373 | |
[email protected] | f6fd98a | 2012-12-14 00:04:02 | [diff] [blame] | 374 | // Returns true if we have sufficient space to store the given number of |
| 375 | // bytes, while keeping kMinFreeSpace bytes on the disk. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 376 | bool HasEnoughSpaceFor(int64 num_bytes, const base::FilePath& path); |
[email protected] | f6fd98a | 2012-12-14 00:04:02 | [diff] [blame] | 377 | |
[email protected] | 01ba15f7 | 2012-06-09 00:41:05 | [diff] [blame] | 378 | // The root directory of the cache (i.e. <user_profile_dir>/GCache/v1). |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 379 | const base::FilePath cache_root_path_; |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 380 | // Paths for all subdirectories of GCache, one for each |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 381 | // DriveCache::CacheSubDirectoryType enum. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 382 | const std::vector<base::FilePath> cache_paths_; |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 383 | scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 384 | |
[email protected] | ca5f6da | 2012-06-18 12:54:59 | [diff] [blame] | 385 | // The cache state data. This member must be access only on the blocking pool. |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 386 | scoped_ptr<DriveCacheMetadata> metadata_; |
[email protected] | ca5f6da | 2012-06-18 12:54:59 | [diff] [blame] | 387 | |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 388 | // List of observers, this member must be accessed on UI thread. |
[email protected] | a0927550 | 2012-10-10 04:48:01 | [diff] [blame] | 389 | ObserverList<DriveCacheObserver> observers_; |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 390 | |
[email protected] | f6fd98a | 2012-12-14 00:04:02 | [diff] [blame] | 391 | FreeDiskSpaceGetterInterface* free_disk_space_getter_; // Not owned. |
| 392 | |
[email protected] | e53ac8f | 2012-08-02 07:05:00 | [diff] [blame] | 393 | // Note: This should remain the last member so it'll be destroyed and |
| 394 | // invalidate its weak pointers before any other members are destroyed. |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 395 | base::WeakPtrFactory<DriveCache> weak_ptr_factory_; |
| 396 | DISALLOW_COPY_AND_ASSIGN(DriveCache); |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 397 | }; |
| 398 | |
[email protected] | 7f53dc7 | 2012-08-23 19:06:55 | [diff] [blame] | 399 | // The minimum free space to keep. DriveFileSystem::GetFileByPath() returns |
[email protected] | 7f90e77c | 2012-07-17 09:35:31 | [diff] [blame] | 400 | // GDATA_FILE_ERROR_NO_SPACE if the available space is smaller than |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 401 | // this value. |
| 402 | // |
| 403 | // Copied from cryptohome/homedirs.h. |
| 404 | // TODO(satorux): Share the constant. |
| 405 | const int64 kMinFreeSpace = 512 * 1LL << 20; |
| 406 | |
[email protected] | d9d04df | 2012-10-12 07:06:35 | [diff] [blame] | 407 | } // namespace drive |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 408 | |
[email protected] | 15de814 | 2012-10-11 06:00:54 | [diff] [blame] | 409 | #endif // CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_CACHE_H_ |