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