blob: 66a34a281929afa82c982f4892a79aae4bc02c8c [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
[email protected]eca3fc92013-05-01 03:53:405#ifndef CHROME_BROWSER_CHROMEOS_DRIVE_FILE_CACHE_H_
6#define CHROME_BROWSER_CHROMEOS_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>
[email protected]79c3752d2012-07-17 12:10:0810#include <vector>
[email protected]3653146a2012-05-29 13:41:4711
[email protected]dd91da82012-09-07 23:49:4912#include "base/callback_forward.h"
[email protected]57999812013-02-24 05:40:5213#include "base/files/file_path.h"
[email protected]3653146a2012-05-29 13:41:4714#include "base/memory/scoped_ptr.h"
[email protected]73f9c742012-06-15 07:37:1315#include "base/memory/weak_ptr.h"
[email protected]78a158b2013-04-23 06:57:4916#include "chrome/browser/chromeos/drive/file_errors.h"
[email protected]eefc02e2013-06-29 13:34:5417#include "chrome/browser/chromeos/drive/resource_metadata_storage.h"
[email protected]3653146a2012-05-29 13:41:4718
[email protected]ddbf2052012-07-13 15:07:0219namespace base {
[email protected]ddbf2052012-07-13 15:07:0220class SequencedTaskRunner;
[email protected]ddbf2052012-07-13 15:07:0221} // namespace base
22
[email protected]d9d04df2012-10-12 07:06:3523namespace drive {
[email protected]3653146a2012-05-29 13:41:4724
[email protected]aa7365c2013-05-01 05:50:4725class FileCacheEntry;
[email protected]c960d222012-06-15 10:03:5026
[email protected]59c7cdec2013-05-07 04:17:1327namespace internal {
28
[email protected]59c7cdec2013-05-07 04:17:1329// Callback for GetFileFromCache.
30typedef base::Callback<void(FileError error,
31 const base::FilePath& cache_file_path)>
32 GetFileFromCacheCallback;
33
[email protected]f6fd98a2012-12-14 00:04:0234// Interface class used for getting the free disk space. Tests can inject an
35// implementation that reports fake free disk space.
36class FreeDiskSpaceGetterInterface {
37 public:
38 virtual ~FreeDiskSpaceGetterInterface() {}
[email protected]cf64404b2012-12-14 07:12:5039 virtual int64 AmountOfFreeDiskSpace() = 0;
[email protected]f6fd98a2012-12-14 00:04:0240};
41
[email protected]0d52ed52013-05-01 08:21:2142// FileCache is used to maintain cache states of FileSystem.
[email protected]6b70c7b2012-06-14 03:10:4343//
44// All non-static public member functions, unless mentioned otherwise (see
[email protected]54ba37502013-05-09 08:43:4045// GetCacheFilePath() for example), should be run with |blocking_task_runner|.
[email protected]eca3fc92013-05-01 03:53:4046class FileCache {
[email protected]3653146a2012-05-29 13:41:4747 public:
[email protected]a321b9632012-06-14 03:29:1748 // Enum defining type of file operation e.g. copy or move, etc.
49 enum FileOperationType {
50 FILE_OPERATION_MOVE = 0,
51 FILE_OPERATION_COPY,
52 };
[email protected]32a7fc852012-06-08 17:25:5053
[email protected]eefc02e2013-06-29 13:34:5454 typedef ResourceMetadataStorage::CacheEntryIterator Iterator;
55
[email protected]2df61e12013-06-21 16:00:0956 // |cache_file_directory| stores cached files.
[email protected]17196ee2012-12-13 06:23:5157 //
58 // |blocking_task_runner| is used to post a task to the blocking worker
59 // pool for file operations. Must not be null.
[email protected]f6fd98a2012-12-14 00:04:0260 //
61 // |free_disk_space_getter| is used to inject a custom free disk space
62 // getter for testing. NULL must be passed for production code.
[email protected]54ba37502013-05-09 08:43:4063 //
64 // Must be called on the UI thread.
[email protected]2df61e12013-06-21 16:00:0965 FileCache(ResourceMetadataStorage* storage,
[email protected]e07f7b7b2013-06-19 03:43:1266 const base::FilePath& cache_file_directory,
[email protected]54ba37502013-05-09 08:43:4067 base::SequencedTaskRunner* blocking_task_runner,
68 FreeDiskSpaceGetterInterface* free_disk_space_getter);
[email protected]17196ee2012-12-13 06:23:5169
[email protected]7bb73e3d2012-09-08 17:41:2970 // Returns true if the given path is under drive cache directory, i.e.
[email protected]01ba15f72012-06-09 00:41:0571 // <user_profile_dir>/GCache/v1
[email protected]6b70c7b2012-06-14 03:10:4372 //
73 // Can be called on any thread.
[email protected]eca3fc92013-05-01 03:53:4074 bool IsUnderFileCacheDirectory(const base::FilePath& path) const;
[email protected]01ba15f72012-06-09 00:41:0575
[email protected]69877602013-10-17 15:53:3276 // Gets the cache entry for file corresponding to |id| and returns true if
77 // entry exists in cache map.
[email protected]c9e4738d2013-08-26 03:04:0778 bool GetCacheEntry(const std::string& id, FileCacheEntry* entry);
[email protected]8e38c9db2013-05-10 02:52:2979
[email protected]eefc02e2013-06-29 13:34:5480 // Returns an object to iterate over entries.
81 scoped_ptr<Iterator> GetIterator();
[email protected]3361a542013-05-22 17:38:2782
[email protected]ec514362013-05-27 17:52:2283 // Frees up disk space to store a file with |num_bytes| size content, while
84 // keeping kMinFreeSpace bytes on the disk, if needed.
85 // Returns true if we successfully manage to have enough space, otherwise
86 // false.
87 bool FreeDiskSpaceIfNeededFor(int64 num_bytes);
88
[email protected]c9e4738d2013-08-26 03:04:0789 // Checks if file corresponding to |id| exists in cache, and returns
[email protected]a61fd6882013-07-26 05:12:3990 // FILE_ERROR_OK with |cache_file_path| storing the path to the file.
[email protected]ec514362013-05-27 17:52:2291 // |cache_file_path| must not be null.
[email protected]c9e4738d2013-08-26 03:04:0792 FileError GetFile(const std::string& id, base::FilePath* cache_file_path);
[email protected]ec514362013-05-27 17:52:2293
[email protected]82c4eb92013-05-21 11:25:2394 // Stores |source_path| as a cache of the remote content of the file
[email protected]c9e4738d2013-08-26 03:04:0795 // with |id| and |md5|.
96 FileError Store(const std::string& id,
[email protected]82c4eb92013-05-21 11:25:2397 const std::string& md5,
98 const base::FilePath& source_path,
99 FileOperationType file_operation_type);
100
[email protected]f8b1a532013-06-06 08:35:08101 // Runs Pin() on |blocking_task_runner_|, and calls |callback| with the result
102 // asynchronously.
[email protected]2a2c4152012-11-26 11:34:50103 // |callback| must not be null.
[email protected]54ba37502013-05-09 08:43:40104 // Must be called on the UI thread.
[email protected]c9e4738d2013-08-26 03:04:07105 void PinOnUIThread(const std::string& id,
[email protected]54ba37502013-05-09 08:43:40106 const FileOperationCallback& callback);
[email protected]a321b9632012-06-14 03:29:17107
[email protected]f8b1a532013-06-06 08:35:08108 // Pins the specified entry.
[email protected]c9e4738d2013-08-26 03:04:07109 FileError Pin(const std::string& id);
[email protected]f8b1a532013-06-06 08:35:08110
[email protected]ec514362013-05-27 17:52:22111 // Runs Unpin() on |blocking_task_runner_|, and calls |callback| with the
112 // result asynchronously.
[email protected]2a2c4152012-11-26 11:34:50113 // |callback| must not be null.
[email protected]54ba37502013-05-09 08:43:40114 // Must be called on the UI thread.
[email protected]c9e4738d2013-08-26 03:04:07115 void UnpinOnUIThread(const std::string& id,
[email protected]54ba37502013-05-09 08:43:40116 const FileOperationCallback& callback);
[email protected]7986b8d2012-06-14 15:05:14117
[email protected]b53e8da2013-05-29 07:47:18118 // Unpins the specified entry.
[email protected]c9e4738d2013-08-26 03:04:07119 FileError Unpin(const std::string& id);
[email protected]ec514362013-05-27 17:52:22120
[email protected]c3f65642013-08-28 02:04:33121 // Runs MarkAsMounted() on |blocking_task_runner_|, and calls |callback| with
122 // the result asynchronously.
[email protected]9564c1502012-11-28 12:12:16123 // |callback| must not be null.
[email protected]54ba37502013-05-09 08:43:40124 // Must be called on the UI thread.
[email protected]c9e4738d2013-08-26 03:04:07125 void MarkAsMountedOnUIThread(const std::string& id,
[email protected]54ba37502013-05-09 08:43:40126 const GetFileFromCacheCallback& callback);
[email protected]9564c1502012-11-28 12:12:16127
[email protected]c3f65642013-08-28 02:04:33128 // Sets the state of the cache entry corresponding to |id| as mounted.
129 FileError MarkAsMounted(const std::string& id,
130 base::FilePath* cache_file_path);
131
[email protected]9564c1502012-11-28 12:12:16132 // Set the state of the cache entry corresponding to file_path as unmounted.
133 // |callback| must not be null.
[email protected]54ba37502013-05-09 08:43:40134 // Must be called on the UI thread.
135 void MarkAsUnmountedOnUIThread(const base::FilePath& file_path,
136 const FileOperationCallback& callback);
[email protected]a321b9632012-06-14 03:29:17137
[email protected]b568b882013-06-10 04:38:07138 // Marks the specified entry dirty.
[email protected]c9e4738d2013-08-26 03:04:07139 FileError MarkDirty(const std::string& id);
[email protected]b568b882013-06-10 04:38:07140
[email protected]c6524942013-07-26 02:24:05141 // Clears dirty state of the specified entry and updates its MD5.
[email protected]c9e4738d2013-08-26 03:04:07142 FileError ClearDirty(const std::string& id, const std::string& md5);
[email protected]a321b9632012-06-14 03:29:17143
[email protected]3361a542013-05-22 17:38:27144 // Removes the specified cache entry and delete cache files if available.
[email protected]c9e4738d2013-08-26 03:04:07145 FileError Remove(const std::string& id);
[email protected]3361a542013-05-22 17:38:27146
[email protected]823ca9712013-09-13 10:09:09147 // Removes all the files in the cache directory and cache entries in DB.
148 bool ClearAll();
[email protected]f861b392012-08-03 20:41:12149
[email protected]34a1bbf32013-06-17 07:24:02150 // Initializes the cache. Returns true on success.
151 bool Initialize();
[email protected]73f9c742012-06-15 07:37:13152
[email protected]17196ee2012-12-13 06:23:51153 // Destroys this cache. This function posts a task to the blocking task
154 // runner to safely delete the object.
[email protected]54ba37502013-05-09 08:43:40155 // Must be called on the UI thread.
[email protected]77fb1a62012-11-01 13:42:32156 void Destroy();
[email protected]73f9c742012-06-15 07:37:13157
[email protected]b7af4f12013-10-31 06:57:45158 // Moves files in the cache directory which are not manged by FileCache to
159 // |dest_directory|.
160 bool RecoverFilesFromCacheDirectory(const base::FilePath& dest_directory);
161
[email protected]ca5f6da2012-06-18 12:54:59162 private:
[email protected]eca3fc92013-05-01 03:53:40163 friend class FileCacheTest;
[email protected]f8b1a532013-06-06 08:35:08164 friend class FileCacheTestOnUIThread;
[email protected]e9663392013-04-12 09:55:15165
[email protected]54ba37502013-05-09 08:43:40166 ~FileCache();
[email protected]fcc92a52012-06-08 22:54:16167
[email protected]e9663392013-04-12 09:55:15168 // Returns absolute path of the file if it were cached or to be cached.
169 //
170 // Can be called on any thread.
[email protected]c9e4738d2013-08-26 03:04:07171 base::FilePath GetCacheFilePath(const std::string& id) const;
[email protected]e9663392013-04-12 09:55:15172
[email protected]fcc92a52012-06-08 22:54:16173 // Checks whether the current thread is on the right sequenced worker pool
174 // with the right sequence ID. If not, DCHECK will fail.
175 void AssertOnSequencedWorkerPool();
[email protected]3653146a2012-05-29 13:41:47176
[email protected]17196ee2012-12-13 06:23:51177 // Destroys the cache on the blocking pool.
[email protected]77fb1a62012-11-01 13:42:32178 void DestroyOnBlockingPool();
[email protected]73f9c742012-06-15 07:37:13179
[email protected]82c4eb92013-05-21 11:25:23180 // Used to implement Store and StoreLocallyModifiedOnUIThread.
181 // TODO(hidehiko): Merge this method with Store(), after
182 // StoreLocallyModifiedOnUIThread is removed.
[email protected]c9e4738d2013-08-26 03:04:07183 FileError StoreInternal(const std::string& id,
[email protected]82c4eb92013-05-21 11:25:23184 const std::string& md5,
185 const base::FilePath& source_path,
[email protected]6bf581142013-06-11 17:13:06186 FileOperationType file_operation_type);
[email protected]73f9c742012-06-15 07:37:13187
[email protected]54ba37502013-05-09 08:43:40188 // Used to implement MarkAsUnmountedOnUIThread.
189 FileError MarkAsUnmounted(const base::FilePath& file_path);
190
[email protected]f6fd98a2012-12-14 00:04:02191 // Returns true if we have sufficient space to store the given number of
192 // bytes, while keeping kMinFreeSpace bytes on the disk.
[email protected]650b2d52013-02-10 03:41:45193 bool HasEnoughSpaceFor(int64 num_bytes, const base::FilePath& path);
[email protected]f6fd98a2012-12-14 00:04:02194
[email protected]f2731d12013-10-22 03:23:15195 // Renames cache files from old "prefix:id.md5" format to the new format.
[email protected]91a464e62013-07-10 09:30:06196 // TODO(hashimoto): Remove this method at some point.
[email protected]f2731d12013-10-22 03:23:15197 bool RenameCacheFilesToNewFormat();
[email protected]91a464e62013-07-10 09:30:06198
[email protected]e07f7b7b2013-06-19 03:43:12199 const base::FilePath cache_file_directory_;
200
[email protected]ddbf2052012-07-13 15:07:02201 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
[email protected]32a7fc852012-06-08 17:25:50202
[email protected]2df61e12013-06-21 16:00:09203 ResourceMetadataStorage* storage_;
[email protected]ca5f6da2012-06-18 12:54:59204
[email protected]f6fd98a2012-12-14 00:04:02205 FreeDiskSpaceGetterInterface* free_disk_space_getter_; // Not owned.
206
[email protected]c9e4738d2013-08-26 03:04:07207 // IDs of files marked mounted.
[email protected]4b60a25f2013-06-17 09:43:11208 std::set<std::string> mounted_files_;
209
[email protected]e53ac8f2012-08-02 07:05:00210 // Note: This should remain the last member so it'll be destroyed and
211 // invalidate its weak pointers before any other members are destroyed.
[email protected]eca3fc92013-05-01 03:53:40212 base::WeakPtrFactory<FileCache> weak_ptr_factory_;
213 DISALLOW_COPY_AND_ASSIGN(FileCache);
[email protected]3653146a2012-05-29 13:41:47214};
215
[email protected]f18102152013-08-01 11:58:29216// The minimum free space to keep. Operations that add cache files return
217// FILE_ERROR_NO_LOCAL_SPACE if the available space is smaller than
[email protected]a321b9632012-06-14 03:29:17218// this value.
219//
220// Copied from cryptohome/homedirs.h.
221// TODO(satorux): Share the constant.
222const int64 kMinFreeSpace = 512 * 1LL << 20;
223
[email protected]59c7cdec2013-05-07 04:17:13224} // namespace internal
[email protected]d9d04df2012-10-12 07:06:35225} // namespace drive
[email protected]3653146a2012-05-29 13:41:47226
[email protected]eca3fc92013-05-01 03:53:40227#endif // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_CACHE_H_