blob: b205a77b700e300b0b3b475c0bb8675c44054da6 [file] [log] [blame]
[email protected]3653146a2012-05-29 13:41:471// 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
lukasza6364a022015-08-21 01:13:245#ifndef COMPONENTS_DRIVE_FILE_CACHE_H_
6#define COMPONENTS_DRIVE_FILE_CACHE_H_
[email protected]3653146a2012-05-29 13:41:477
[email protected]4b60a25f2013-06-17 09:43:118#include <set>
[email protected]3653146a2012-05-29 13:41:479#include <string>
10
[email protected]57999812013-02-24 05:40:5211#include "base/files/file_path.h"
[email protected]3653146a2012-05-29 13:41:4712#include "base/memory/scoped_ptr.h"
[email protected]73f9c742012-06-15 07:37:1313#include "base/memory/weak_ptr.h"
hashimoto246e4a82015-04-17 07:44:4914#include "base/synchronization/cancellation_flag.h"
lukasza037c10b12015-06-12 04:21:2515#include "base/threading/thread_checker.h"
lukasza76b4a982015-08-08 00:36:3916#include "components/drive/file_errors.h"
lukasza6364a022015-08-21 01:13:2417#include "components/drive/resource_metadata_storage.h"
lukasza3fb22622015-08-27 21:04:3418#if defined(OS_CHROMEOS)
19#include "third_party/cros_system_api/constants/cryptohome.h"
20#endif
[email protected]3653146a2012-05-29 13:41:4721
[email protected]ddbf2052012-07-13 15:07:0222namespace base {
[email protected]8b03ab3a2014-01-15 17:52:4523class ScopedClosureRunner;
[email protected]ddbf2052012-07-13 15:07:0224class SequencedTaskRunner;
[email protected]ddbf2052012-07-13 15:07:0225} // namespace base
26
[email protected]d9d04df2012-10-12 07:06:3527namespace drive {
[email protected]3653146a2012-05-29 13:41:4728
[email protected]59c7cdec2013-05-07 04:17:1329namespace internal {
30
lukasza3fb22622015-08-27 21:04:3431#if defined(OS_CHROMEOS)
32 const int64 kMinFreeSpaceInBytes = cryptohome::kMinFreeSpaceInBytes;
33#else
34 const int64 kMinFreeSpaceInBytes = 512ull * 1024ull * 1024ull; // 512MB
35#endif
36
[email protected]f6fd98a2012-12-14 00:04:0237// Interface class used for getting the free disk space. Tests can inject an
38// implementation that reports fake free disk space.
39class FreeDiskSpaceGetterInterface {
40 public:
41 virtual ~FreeDiskSpaceGetterInterface() {}
[email protected]cf64404b2012-12-14 07:12:5042 virtual int64 AmountOfFreeDiskSpace() = 0;
[email protected]f6fd98a2012-12-14 00:04:0243};
44
[email protected]0d52ed52013-05-01 08:21:2145// FileCache is used to maintain cache states of FileSystem.
[email protected]6b70c7b2012-06-14 03:10:4346//
47// All non-static public member functions, unless mentioned otherwise (see
[email protected]54ba37502013-05-09 08:43:4048// GetCacheFilePath() for example), should be run with |blocking_task_runner|.
[email protected]eca3fc92013-05-01 03:53:4049class FileCache {
[email protected]3653146a2012-05-29 13:41:4750 public:
[email protected]a321b9632012-06-14 03:29:1751 // Enum defining type of file operation e.g. copy or move, etc.
52 enum FileOperationType {
53 FILE_OPERATION_MOVE = 0,
54 FILE_OPERATION_COPY,
55 };
[email protected]32a7fc852012-06-08 17:25:5056
[email protected]2df61e12013-06-21 16:00:0957 // |cache_file_directory| stores cached files.
[email protected]17196ee2012-12-13 06:23:5158 //
[email protected]8e37b9b2013-12-11 09:06:0259 // |blocking_task_runner| indicates the blocking worker pool for cache
60 // operations. All operations on this FileCache must be run on this runner.
61 // Must not be null.
[email protected]f6fd98a2012-12-14 00:04:0262 //
63 // |free_disk_space_getter| is used to inject a custom free disk space
64 // getter for testing. NULL must be passed for production code.
[email protected]54ba37502013-05-09 08:43:4065 //
66 // Must be called on the UI thread.
[email protected]2df61e12013-06-21 16:00:0967 FileCache(ResourceMetadataStorage* storage,
[email protected]e07f7b7b2013-06-19 03:43:1268 const base::FilePath& cache_file_directory,
[email protected]54ba37502013-05-09 08:43:4069 base::SequencedTaskRunner* blocking_task_runner,
70 FreeDiskSpaceGetterInterface* free_disk_space_getter);
[email protected]17196ee2012-12-13 06:23:5171
[email protected]7bb73e3d2012-09-08 17:41:2972 // Returns true if the given path is under drive cache directory, i.e.
[email protected]01ba15f72012-06-09 00:41:0573 // <user_profile_dir>/GCache/v1
[email protected]6b70c7b2012-06-14 03:10:4374 //
75 // Can be called on any thread.
[email protected]eca3fc92013-05-01 03:53:4076 bool IsUnderFileCacheDirectory(const base::FilePath& path) const;
[email protected]01ba15f72012-06-09 00:41:0577
[email protected]ec514362013-05-27 17:52:2278 // Frees up disk space to store a file with |num_bytes| size content, while
lukasza3fb22622015-08-27 21:04:3479 // keeping drive::internal::kMinFreeSpaceInBytes bytes on the disk, if needed.
[email protected]ec514362013-05-27 17:52:2280 // Returns true if we successfully manage to have enough space, otherwise
81 // false.
82 bool FreeDiskSpaceIfNeededFor(int64 num_bytes);
83
yawano8578abf2015-08-26 09:15:5084 // Calculates and returns evictable cache size. In error case, this returns 0.
85 uint64_t CalculateEvictableCacheSize();
86
[email protected]c9e4738d2013-08-26 03:04:0787 // Checks if file corresponding to |id| exists in cache, and returns
[email protected]a61fd6882013-07-26 05:12:3988 // FILE_ERROR_OK with |cache_file_path| storing the path to the file.
[email protected]ec514362013-05-27 17:52:2289 // |cache_file_path| must not be null.
[email protected]c9e4738d2013-08-26 03:04:0790 FileError GetFile(const std::string& id, base::FilePath* cache_file_path);
[email protected]ec514362013-05-27 17:52:2291
[email protected]82c4eb92013-05-21 11:25:2392 // Stores |source_path| as a cache of the remote content of the file
[email protected]c9e4738d2013-08-26 03:04:0793 // with |id| and |md5|.
[email protected]bae99ae52014-01-29 01:13:1494 // Pass an empty string as MD5 to mark the entry as dirty.
[email protected]c9e4738d2013-08-26 03:04:0795 FileError Store(const std::string& id,
[email protected]82c4eb92013-05-21 11:25:2396 const std::string& md5,
97 const base::FilePath& source_path,
98 FileOperationType file_operation_type);
99
[email protected]f8b1a532013-06-06 08:35:08100 // Pins the specified entry.
[email protected]c9e4738d2013-08-26 03:04:07101 FileError Pin(const std::string& id);
[email protected]f8b1a532013-06-06 08:35:08102
[email protected]b53e8da2013-05-29 07:47:18103 // Unpins the specified entry.
[email protected]c9e4738d2013-08-26 03:04:07104 FileError Unpin(const std::string& id);
[email protected]ec514362013-05-27 17:52:22105
[email protected]c3f65642013-08-28 02:04:33106 // Sets the state of the cache entry corresponding to |id| as mounted.
107 FileError MarkAsMounted(const std::string& id,
108 base::FilePath* cache_file_path);
109
[email protected]8e37b9b2013-12-11 09:06:02110 // Sets the state of the cache entry corresponding to file_path as unmounted.
111 FileError MarkAsUnmounted(const base::FilePath& file_path);
[email protected]a321b9632012-06-14 03:29:17112
[email protected]8b03ab3a2014-01-15 17:52:45113 // Opens the cache file corresponding to |id| for write. |file_closer| should
114 // be kept alive until writing finishes.
115 // This method must be called before writing to cache files.
116 FileError OpenForWrite(const std::string& id,
117 scoped_ptr<base::ScopedClosureRunner>* file_closer);
118
119 // Returns true if the cache file corresponding to |id| is write-opened.
120 bool IsOpenedForWrite(const std::string& id);
[email protected]b568b882013-06-10 04:38:07121
[email protected]b1bf19a2014-01-21 04:45:19122 // Calculates MD5 of the cache file and updates the stored value.
123 FileError UpdateMd5(const std::string& id);
124
125 // Clears dirty state of the specified entry.
126 FileError ClearDirty(const std::string& id);
[email protected]a321b9632012-06-14 03:29:17127
[email protected]3361a542013-05-22 17:38:27128 // Removes the specified cache entry and delete cache files if available.
[email protected]c9e4738d2013-08-26 03:04:07129 FileError Remove(const std::string& id);
[email protected]3361a542013-05-22 17:38:27130
[email protected]cd8fd37f2014-05-20 15:45:21131 // Removes all the files in the cache directory.
[email protected]823ca9712013-09-13 10:09:09132 bool ClearAll();
[email protected]f861b392012-08-03 20:41:12133
[email protected]34a1bbf32013-06-17 07:24:02134 // Initializes the cache. Returns true on success.
135 bool Initialize();
[email protected]73f9c742012-06-15 07:37:13136
[email protected]17196ee2012-12-13 06:23:51137 // Destroys this cache. This function posts a task to the blocking task
138 // runner to safely delete the object.
[email protected]54ba37502013-05-09 08:43:40139 // Must be called on the UI thread.
[email protected]77fb1a62012-11-01 13:42:32140 void Destroy();
[email protected]73f9c742012-06-15 07:37:13141
[email protected]8e37b9b2013-12-11 09:06:02142 // Moves files in the cache directory which are not managed by FileCache to
[email protected]b7af4f12013-10-31 06:57:45143 // |dest_directory|.
[email protected]026d4a522013-11-05 14:22:18144 // |recovered_cache_info| should contain cache info recovered from the trashed
145 // metadata DB. It is used to ignore non-dirty files.
[email protected]760abc32013-11-01 05:13:01146 bool RecoverFilesFromCacheDirectory(
147 const base::FilePath& dest_directory,
[email protected]026d4a522013-11-05 14:22:18148 const ResourceMetadataStorage::RecoveredCacheInfoMap&
149 recovered_cache_info);
[email protected]b7af4f12013-10-31 06:57:45150
[email protected]ca5f6da2012-06-18 12:54:59151 private:
[email protected]eca3fc92013-05-01 03:53:40152 friend class FileCacheTest;
[email protected]e9663392013-04-12 09:55:15153
[email protected]54ba37502013-05-09 08:43:40154 ~FileCache();
[email protected]fcc92a52012-06-08 22:54:16155
[email protected]e9663392013-04-12 09:55:15156 // Returns absolute path of the file if it were cached or to be cached.
157 //
158 // Can be called on any thread.
[email protected]c9e4738d2013-08-26 03:04:07159 base::FilePath GetCacheFilePath(const std::string& id) const;
[email protected]e9663392013-04-12 09:55:15160
[email protected]fcc92a52012-06-08 22:54:16161 // Checks whether the current thread is on the right sequenced worker pool
162 // with the right sequence ID. If not, DCHECK will fail.
163 void AssertOnSequencedWorkerPool();
[email protected]3653146a2012-05-29 13:41:47164
[email protected]17196ee2012-12-13 06:23:51165 // Destroys the cache on the blocking pool.
[email protected]77fb1a62012-11-01 13:42:32166 void DestroyOnBlockingPool();
[email protected]73f9c742012-06-15 07:37:13167
[email protected]f6fd98a2012-12-14 00:04:02168 // Returns true if we have sufficient space to store the given number of
lukasza3fb22622015-08-27 21:04:34169 // bytes, while keeping drive::internal::kMinFreeSpaceInBytes bytes on the
170 // disk.
[email protected]650b2d52013-02-10 03:41:45171 bool HasEnoughSpaceFor(int64 num_bytes, const base::FilePath& path);
[email protected]f6fd98a2012-12-14 00:04:02172
[email protected]f2731d12013-10-22 03:23:15173 // Renames cache files from old "prefix:id.md5" format to the new format.
[email protected]91a464e62013-07-10 09:30:06174 // TODO(hashimoto): Remove this method at some point.
[email protected]f2731d12013-10-22 03:23:15175 bool RenameCacheFilesToNewFormat();
[email protected]91a464e62013-07-10 09:30:06176
[email protected]8b03ab3a2014-01-15 17:52:45177 // This method must be called after writing to a cache file.
178 // Used to implement OpenForWrite().
179 void CloseForWrite(const std::string& id);
180
yawano8578abf2015-08-26 09:15:50181 // Returns true if the cache entry can be evicted.
182 bool IsEvictable(const std::string& id, const ResourceEntry& entry);
183
[email protected]e07f7b7b2013-06-19 03:43:12184 const base::FilePath cache_file_directory_;
185
[email protected]ddbf2052012-07-13 15:07:02186 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
[email protected]32a7fc852012-06-08 17:25:50187
hashimoto246e4a82015-04-17 07:44:49188 base::CancellationFlag in_shutdown_;
189
[email protected]2df61e12013-06-21 16:00:09190 ResourceMetadataStorage* storage_;
[email protected]ca5f6da2012-06-18 12:54:59191
[email protected]f6fd98a2012-12-14 00:04:02192 FreeDiskSpaceGetterInterface* free_disk_space_getter_; // Not owned.
193
[email protected]8b03ab3a2014-01-15 17:52:45194 // IDs of files being write-opened.
195 std::map<std::string, int> write_opened_files_;
196
[email protected]c9e4738d2013-08-26 03:04:07197 // IDs of files marked mounted.
[email protected]4b60a25f2013-06-17 09:43:11198 std::set<std::string> mounted_files_;
199
lukasza037c10b12015-06-12 04:21:25200 base::ThreadChecker thread_checker_;
201
[email protected]e53ac8f2012-08-02 07:05:00202 // Note: This should remain the last member so it'll be destroyed and
203 // invalidate its weak pointers before any other members are destroyed.
[email protected]8b03ab3a2014-01-15 17:52:45204 // This object should be accessed only on |blocking_task_runner_|.
[email protected]eca3fc92013-05-01 03:53:40205 base::WeakPtrFactory<FileCache> weak_ptr_factory_;
206 DISALLOW_COPY_AND_ASSIGN(FileCache);
[email protected]3653146a2012-05-29 13:41:47207};
208
[email protected]59c7cdec2013-05-07 04:17:13209} // namespace internal
[email protected]d9d04df2012-10-12 07:06:35210} // namespace drive
[email protected]3653146a2012-05-29 13:41:47211
lukasza6364a022015-08-21 01:13:24212#endif // COMPONENTS_DRIVE_FILE_CACHE_H_