[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] | 4b60a25f | 2013-06-17 09:43:11 | [diff] [blame] | 8 | #include <set> |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 9 | #include <string> |
[email protected] | 79c3752d | 2012-07-17 12:10:08 | [diff] [blame] | 10 | #include <vector> |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 11 | |
[email protected] | dd91da8 | 2012-09-07 23:49:49 | [diff] [blame] | 12 | #include "base/callback_forward.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 13 | #include "base/files/file_path.h" |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 14 | #include "base/memory/scoped_ptr.h" |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 15 | #include "base/memory/weak_ptr.h" |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 16 | #include "chrome/browser/chromeos/drive/file_errors.h" |
[email protected] | eefc02e | 2013-06-29 13:34:54 | [diff] [blame] | 17 | #include "chrome/browser/chromeos/drive/resource_metadata_storage.h" |
[email protected] | 3e8d64d | 2013-09-19 10:09:05 | [diff] [blame] | 18 | #include "chrome/browser/drive/drive_service_interface.h" |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 19 | |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 20 | namespace base { |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 21 | class SequencedTaskRunner; |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 22 | } // namespace base |
| 23 | |
[email protected] | d9d04df | 2012-10-12 07:06:35 | [diff] [blame] | 24 | namespace drive { |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 25 | |
[email protected] | aa7365c | 2013-05-01 05:50:47 | [diff] [blame] | 26 | class FileCacheEntry; |
[email protected] | c960d22 | 2012-06-15 10:03:50 | [diff] [blame] | 27 | |
[email protected] | 59c7cdec | 2013-05-07 04:17:13 | [diff] [blame] | 28 | namespace internal { |
| 29 | |
[email protected] | 59c7cdec | 2013-05-07 04:17:13 | [diff] [blame] | 30 | // Callback for GetFileFromCache. |
| 31 | typedef base::Callback<void(FileError error, |
| 32 | const base::FilePath& cache_file_path)> |
| 33 | GetFileFromCacheCallback; |
| 34 | |
[email protected] | f6fd98a | 2012-12-14 00:04:02 | [diff] [blame] | 35 | // Interface class used for getting the free disk space. Tests can inject an |
| 36 | // implementation that reports fake free disk space. |
| 37 | class FreeDiskSpaceGetterInterface { |
| 38 | public: |
| 39 | virtual ~FreeDiskSpaceGetterInterface() {} |
[email protected] | cf64404b | 2012-12-14 07:12:50 | [diff] [blame] | 40 | virtual int64 AmountOfFreeDiskSpace() = 0; |
[email protected] | f6fd98a | 2012-12-14 00:04:02 | [diff] [blame] | 41 | }; |
| 42 | |
[email protected] | 0d52ed5 | 2013-05-01 08:21:21 | [diff] [blame] | 43 | // FileCache is used to maintain cache states of FileSystem. |
[email protected] | 6b70c7b | 2012-06-14 03:10:43 | [diff] [blame] | 44 | // |
| 45 | // All non-static public member functions, unless mentioned otherwise (see |
[email protected] | 54ba3750 | 2013-05-09 08:43:40 | [diff] [blame] | 46 | // GetCacheFilePath() for example), should be run with |blocking_task_runner|. |
[email protected] | eca3fc9 | 2013-05-01 03:53:40 | [diff] [blame] | 47 | class FileCache { |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 48 | public: |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 49 | // Enum defining type of file operation e.g. copy or move, etc. |
| 50 | enum FileOperationType { |
| 51 | FILE_OPERATION_MOVE = 0, |
| 52 | FILE_OPERATION_COPY, |
| 53 | }; |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 54 | |
[email protected] | eefc02e | 2013-06-29 13:34:54 | [diff] [blame] | 55 | typedef ResourceMetadataStorage::CacheEntryIterator Iterator; |
| 56 | |
[email protected] | 2df61e1 | 2013-06-21 16:00:09 | [diff] [blame] | 57 | // |cache_file_directory| stores cached files. |
[email protected] | 17196ee | 2012-12-13 06:23:51 | [diff] [blame] | 58 | // |
| 59 | // |blocking_task_runner| is used to post a task to the blocking worker |
| 60 | // pool for file operations. Must not be null. |
[email protected] | f6fd98a | 2012-12-14 00:04:02 | [diff] [blame] | 61 | // |
| 62 | // |free_disk_space_getter| is used to inject a custom free disk space |
| 63 | // getter for testing. NULL must be passed for production code. |
[email protected] | 54ba3750 | 2013-05-09 08:43:40 | [diff] [blame] | 64 | // |
| 65 | // Must be called on the UI thread. |
[email protected] | 2df61e1 | 2013-06-21 16:00:09 | [diff] [blame] | 66 | FileCache(ResourceMetadataStorage* storage, |
[email protected] | e07f7b7b | 2013-06-19 03:43:12 | [diff] [blame] | 67 | const base::FilePath& cache_file_directory, |
[email protected] | 54ba3750 | 2013-05-09 08:43:40 | [diff] [blame] | 68 | base::SequencedTaskRunner* blocking_task_runner, |
| 69 | FreeDiskSpaceGetterInterface* free_disk_space_getter); |
[email protected] | 17196ee | 2012-12-13 06:23:51 | [diff] [blame] | 70 | |
[email protected] | 7bb73e3d | 2012-09-08 17:41:29 | [diff] [blame] | 71 | // Returns true if the given path is under drive cache directory, i.e. |
[email protected] | 01ba15f7 | 2012-06-09 00:41:05 | [diff] [blame] | 72 | // <user_profile_dir>/GCache/v1 |
[email protected] | 6b70c7b | 2012-06-14 03:10:43 | [diff] [blame] | 73 | // |
| 74 | // Can be called on any thread. |
[email protected] | eca3fc9 | 2013-05-01 03:53:40 | [diff] [blame] | 75 | bool IsUnderFileCacheDirectory(const base::FilePath& path) const; |
[email protected] | 01ba15f7 | 2012-06-09 00:41:05 | [diff] [blame] | 76 | |
[email protected] | 6987760 | 2013-10-17 15:53:32 | [diff] [blame^] | 77 | // Gets the cache entry for file corresponding to |id| and returns true if |
| 78 | // entry exists in cache map. |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 79 | bool GetCacheEntry(const std::string& id, FileCacheEntry* entry); |
[email protected] | 8e38c9db | 2013-05-10 02:52:29 | [diff] [blame] | 80 | |
[email protected] | eefc02e | 2013-06-29 13:34:54 | [diff] [blame] | 81 | // Returns an object to iterate over entries. |
| 82 | scoped_ptr<Iterator> GetIterator(); |
[email protected] | 3361a54 | 2013-05-22 17:38:27 | [diff] [blame] | 83 | |
[email protected] | ec51436 | 2013-05-27 17:52:22 | [diff] [blame] | 84 | // Frees up disk space to store a file with |num_bytes| size content, while |
| 85 | // keeping kMinFreeSpace bytes on the disk, if needed. |
| 86 | // Returns true if we successfully manage to have enough space, otherwise |
| 87 | // false. |
| 88 | bool FreeDiskSpaceIfNeededFor(int64 num_bytes); |
| 89 | |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 90 | // Checks if file corresponding to |id| exists in cache, and returns |
[email protected] | a61fd688 | 2013-07-26 05:12:39 | [diff] [blame] | 91 | // FILE_ERROR_OK with |cache_file_path| storing the path to the file. |
[email protected] | ec51436 | 2013-05-27 17:52:22 | [diff] [blame] | 92 | // |cache_file_path| must not be null. |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 93 | FileError GetFile(const std::string& id, base::FilePath* cache_file_path); |
[email protected] | ec51436 | 2013-05-27 17:52:22 | [diff] [blame] | 94 | |
[email protected] | 82c4eb9 | 2013-05-21 11:25:23 | [diff] [blame] | 95 | // Stores |source_path| as a cache of the remote content of the file |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 96 | // with |id| and |md5|. |
| 97 | FileError Store(const std::string& id, |
[email protected] | 82c4eb9 | 2013-05-21 11:25:23 | [diff] [blame] | 98 | const std::string& md5, |
| 99 | const base::FilePath& source_path, |
| 100 | FileOperationType file_operation_type); |
| 101 | |
[email protected] | f8b1a53 | 2013-06-06 08:35:08 | [diff] [blame] | 102 | // Runs Pin() on |blocking_task_runner_|, and calls |callback| with the result |
| 103 | // asynchronously. |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 104 | // |callback| must not be null. |
[email protected] | 54ba3750 | 2013-05-09 08:43:40 | [diff] [blame] | 105 | // Must be called on the UI thread. |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 106 | void PinOnUIThread(const std::string& id, |
[email protected] | 54ba3750 | 2013-05-09 08:43:40 | [diff] [blame] | 107 | const FileOperationCallback& callback); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 108 | |
[email protected] | f8b1a53 | 2013-06-06 08:35:08 | [diff] [blame] | 109 | // Pins the specified entry. |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 110 | FileError Pin(const std::string& id); |
[email protected] | f8b1a53 | 2013-06-06 08:35:08 | [diff] [blame] | 111 | |
[email protected] | ec51436 | 2013-05-27 17:52:22 | [diff] [blame] | 112 | // Runs Unpin() on |blocking_task_runner_|, and calls |callback| with the |
| 113 | // result asynchronously. |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 114 | // |callback| must not be null. |
[email protected] | 54ba3750 | 2013-05-09 08:43:40 | [diff] [blame] | 115 | // Must be called on the UI thread. |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 116 | void UnpinOnUIThread(const std::string& id, |
[email protected] | 54ba3750 | 2013-05-09 08:43:40 | [diff] [blame] | 117 | const FileOperationCallback& callback); |
[email protected] | 7986b8d | 2012-06-14 15:05:14 | [diff] [blame] | 118 | |
[email protected] | b53e8da | 2013-05-29 07:47:18 | [diff] [blame] | 119 | // Unpins the specified entry. |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 120 | FileError Unpin(const std::string& id); |
[email protected] | ec51436 | 2013-05-27 17:52:22 | [diff] [blame] | 121 | |
[email protected] | c3f6564 | 2013-08-28 02:04:33 | [diff] [blame] | 122 | // Runs MarkAsMounted() on |blocking_task_runner_|, and calls |callback| with |
| 123 | // the result asynchronously. |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 124 | // |callback| must not be null. |
[email protected] | 54ba3750 | 2013-05-09 08:43:40 | [diff] [blame] | 125 | // Must be called on the UI thread. |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 126 | void MarkAsMountedOnUIThread(const std::string& id, |
[email protected] | 54ba3750 | 2013-05-09 08:43:40 | [diff] [blame] | 127 | const GetFileFromCacheCallback& callback); |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 128 | |
[email protected] | c3f6564 | 2013-08-28 02:04:33 | [diff] [blame] | 129 | // Sets the state of the cache entry corresponding to |id| as mounted. |
| 130 | FileError MarkAsMounted(const std::string& id, |
| 131 | base::FilePath* cache_file_path); |
| 132 | |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 133 | // Set the state of the cache entry corresponding to file_path as unmounted. |
| 134 | // |callback| must not be null. |
[email protected] | 54ba3750 | 2013-05-09 08:43:40 | [diff] [blame] | 135 | // Must be called on the UI thread. |
| 136 | void MarkAsUnmountedOnUIThread(const base::FilePath& file_path, |
| 137 | const FileOperationCallback& callback); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 138 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 139 | // Marks the specified entry dirty. |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 140 | FileError MarkDirty(const std::string& id); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 141 | |
[email protected] | c652494 | 2013-07-26 02:24:05 | [diff] [blame] | 142 | // Clears dirty state of the specified entry and updates its MD5. |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 143 | FileError ClearDirty(const std::string& id, const std::string& md5); |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 144 | |
[email protected] | 3361a54 | 2013-05-22 17:38:27 | [diff] [blame] | 145 | // Removes the specified cache entry and delete cache files if available. |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 146 | FileError Remove(const std::string& id); |
[email protected] | 3361a54 | 2013-05-22 17:38:27 | [diff] [blame] | 147 | |
[email protected] | 823ca971 | 2013-09-13 10:09:09 | [diff] [blame] | 148 | // Removes all the files in the cache directory and cache entries in DB. |
| 149 | bool ClearAll(); |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 150 | |
[email protected] | 34a1bbf3 | 2013-06-17 07:24:02 | [diff] [blame] | 151 | // Initializes the cache. Returns true on success. |
| 152 | bool Initialize(); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 153 | |
[email protected] | 17196ee | 2012-12-13 06:23:51 | [diff] [blame] | 154 | // Destroys this cache. This function posts a task to the blocking task |
| 155 | // runner to safely delete the object. |
[email protected] | 54ba3750 | 2013-05-09 08:43:40 | [diff] [blame] | 156 | // Must be called on the UI thread. |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 157 | void Destroy(); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 158 | |
[email protected] | 3e8d64d | 2013-09-19 10:09:05 | [diff] [blame] | 159 | // Converts entry IDs and cache file names to the desired format. |
| 160 | // TODO(hashimoto): Remove this method at some point. |
| 161 | bool CanonicalizeIDs(const ResourceIdCanonicalizer& id_canonicalizer); |
| 162 | |
[email protected] | ca5f6da | 2012-06-18 12:54:59 | [diff] [blame] | 163 | private: |
[email protected] | eca3fc9 | 2013-05-01 03:53:40 | [diff] [blame] | 164 | friend class FileCacheTest; |
[email protected] | f8b1a53 | 2013-06-06 08:35:08 | [diff] [blame] | 165 | friend class FileCacheTestOnUIThread; |
[email protected] | e966339 | 2013-04-12 09:55:15 | [diff] [blame] | 166 | |
[email protected] | 54ba3750 | 2013-05-09 08:43:40 | [diff] [blame] | 167 | ~FileCache(); |
[email protected] | fcc92a5 | 2012-06-08 22:54:16 | [diff] [blame] | 168 | |
[email protected] | e966339 | 2013-04-12 09:55:15 | [diff] [blame] | 169 | // Returns absolute path of the file if it were cached or to be cached. |
| 170 | // |
| 171 | // Can be called on any thread. |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 172 | base::FilePath GetCacheFilePath(const std::string& id) const; |
[email protected] | e966339 | 2013-04-12 09:55:15 | [diff] [blame] | 173 | |
[email protected] | fcc92a5 | 2012-06-08 22:54:16 | [diff] [blame] | 174 | // Checks whether the current thread is on the right sequenced worker pool |
| 175 | // with the right sequence ID. If not, DCHECK will fail. |
| 176 | void AssertOnSequencedWorkerPool(); |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 177 | |
[email protected] | 17196ee | 2012-12-13 06:23:51 | [diff] [blame] | 178 | // Destroys the cache on the blocking pool. |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 179 | void DestroyOnBlockingPool(); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 180 | |
[email protected] | 82c4eb9 | 2013-05-21 11:25:23 | [diff] [blame] | 181 | // Used to implement Store and StoreLocallyModifiedOnUIThread. |
| 182 | // TODO(hidehiko): Merge this method with Store(), after |
| 183 | // StoreLocallyModifiedOnUIThread is removed. |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 184 | FileError StoreInternal(const std::string& id, |
[email protected] | 82c4eb9 | 2013-05-21 11:25:23 | [diff] [blame] | 185 | const std::string& md5, |
| 186 | const base::FilePath& source_path, |
[email protected] | 6bf58114 | 2013-06-11 17:13:06 | [diff] [blame] | 187 | FileOperationType file_operation_type); |
[email protected] | 73f9c74 | 2012-06-15 07:37:13 | [diff] [blame] | 188 | |
[email protected] | 54ba3750 | 2013-05-09 08:43:40 | [diff] [blame] | 189 | // Used to implement MarkAsUnmountedOnUIThread. |
| 190 | FileError MarkAsUnmounted(const base::FilePath& file_path); |
| 191 | |
[email protected] | f6fd98a | 2012-12-14 00:04:02 | [diff] [blame] | 192 | // Returns true if we have sufficient space to store the given number of |
| 193 | // bytes, while keeping kMinFreeSpace bytes on the disk. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 194 | bool HasEnoughSpaceFor(int64 num_bytes, const base::FilePath& path); |
[email protected] | f6fd98a | 2012-12-14 00:04:02 | [diff] [blame] | 195 | |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 196 | // Renames cache files from old "id.md5" format to the new format. |
[email protected] | 91a464e6 | 2013-07-10 09:30:06 | [diff] [blame] | 197 | // TODO(hashimoto): Remove this method at some point. |
| 198 | void RenameCacheFilesToNewFormat(); |
| 199 | |
[email protected] | e07f7b7b | 2013-06-19 03:43:12 | [diff] [blame] | 200 | const base::FilePath cache_file_directory_; |
| 201 | |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 202 | scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; |
[email protected] | 32a7fc85 | 2012-06-08 17:25:50 | [diff] [blame] | 203 | |
[email protected] | 2df61e1 | 2013-06-21 16:00:09 | [diff] [blame] | 204 | ResourceMetadataStorage* storage_; |
[email protected] | ca5f6da | 2012-06-18 12:54:59 | [diff] [blame] | 205 | |
[email protected] | f6fd98a | 2012-12-14 00:04:02 | [diff] [blame] | 206 | FreeDiskSpaceGetterInterface* free_disk_space_getter_; // Not owned. |
| 207 | |
[email protected] | c9e4738d | 2013-08-26 03:04:07 | [diff] [blame] | 208 | // IDs of files marked mounted. |
[email protected] | 4b60a25f | 2013-06-17 09:43:11 | [diff] [blame] | 209 | std::set<std::string> mounted_files_; |
| 210 | |
[email protected] | e53ac8f | 2012-08-02 07:05:00 | [diff] [blame] | 211 | // Note: This should remain the last member so it'll be destroyed and |
| 212 | // invalidate its weak pointers before any other members are destroyed. |
[email protected] | eca3fc9 | 2013-05-01 03:53:40 | [diff] [blame] | 213 | base::WeakPtrFactory<FileCache> weak_ptr_factory_; |
| 214 | DISALLOW_COPY_AND_ASSIGN(FileCache); |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 215 | }; |
| 216 | |
[email protected] | f1810215 | 2013-08-01 11:58:29 | [diff] [blame] | 217 | // The minimum free space to keep. Operations that add cache files return |
| 218 | // FILE_ERROR_NO_LOCAL_SPACE if the available space is smaller than |
[email protected] | a321b963 | 2012-06-14 03:29:17 | [diff] [blame] | 219 | // this value. |
| 220 | // |
| 221 | // Copied from cryptohome/homedirs.h. |
| 222 | // TODO(satorux): Share the constant. |
| 223 | const int64 kMinFreeSpace = 512 * 1LL << 20; |
| 224 | |
[email protected] | 59c7cdec | 2013-05-07 04:17:13 | [diff] [blame] | 225 | } // namespace internal |
[email protected] | d9d04df | 2012-10-12 07:06:35 | [diff] [blame] | 226 | } // namespace drive |
[email protected] | 3653146a | 2012-05-29 13:41:47 | [diff] [blame] | 227 | |
[email protected] | eca3fc9 | 2013-05-01 03:53:40 | [diff] [blame] | 228 | #endif // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_CACHE_H_ |