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