Cancel MD5 update on shutdown
[chromium-blink-merge.git] / chrome / browser / chromeos / drive / file_cache.h
blobf93c3e1e943d6523bc57f2ea8190753c58255962
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.
5 #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_FILE_CACHE_H_
6 #define CHROME_BROWSER_CHROMEOS_DRIVE_FILE_CACHE_H_
8 #include <set>
9 #include <string>
11 #include "base/files/file_path.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/synchronization/cancellation_flag.h"
15 #include "chrome/browser/chromeos/drive/file_errors.h"
16 #include "chrome/browser/chromeos/drive/resource_metadata_storage.h"
18 namespace base {
19 class ScopedClosureRunner;
20 class SequencedTaskRunner;
21 } // namespace base
23 namespace drive {
25 namespace internal {
27 // Interface class used for getting the free disk space. Tests can inject an
28 // implementation that reports fake free disk space.
29 class FreeDiskSpaceGetterInterface {
30 public:
31 virtual ~FreeDiskSpaceGetterInterface() {}
32 virtual int64 AmountOfFreeDiskSpace() = 0;
35 // FileCache is used to maintain cache states of FileSystem.
37 // All non-static public member functions, unless mentioned otherwise (see
38 // GetCacheFilePath() for example), should be run with |blocking_task_runner|.
39 class FileCache {
40 public:
41 // Enum defining type of file operation e.g. copy or move, etc.
42 enum FileOperationType {
43 FILE_OPERATION_MOVE = 0,
44 FILE_OPERATION_COPY,
47 // |cache_file_directory| stores cached files.
49 // |blocking_task_runner| indicates the blocking worker pool for cache
50 // operations. All operations on this FileCache must be run on this runner.
51 // Must not be null.
53 // |free_disk_space_getter| is used to inject a custom free disk space
54 // getter for testing. NULL must be passed for production code.
56 // Must be called on the UI thread.
57 FileCache(ResourceMetadataStorage* storage,
58 const base::FilePath& cache_file_directory,
59 base::SequencedTaskRunner* blocking_task_runner,
60 FreeDiskSpaceGetterInterface* free_disk_space_getter);
62 // Returns true if the given path is under drive cache directory, i.e.
63 // <user_profile_dir>/GCache/v1
65 // Can be called on any thread.
66 bool IsUnderFileCacheDirectory(const base::FilePath& path) const;
68 // Frees up disk space to store a file with |num_bytes| size content, while
69 // keeping cryptohome::kMinFreeSpaceInBytes bytes on the disk, if needed.
70 // Returns true if we successfully manage to have enough space, otherwise
71 // false.
72 bool FreeDiskSpaceIfNeededFor(int64 num_bytes);
74 // Checks if file corresponding to |id| exists in cache, and returns
75 // FILE_ERROR_OK with |cache_file_path| storing the path to the file.
76 // |cache_file_path| must not be null.
77 FileError GetFile(const std::string& id, base::FilePath* cache_file_path);
79 // Stores |source_path| as a cache of the remote content of the file
80 // with |id| and |md5|.
81 // Pass an empty string as MD5 to mark the entry as dirty.
82 FileError Store(const std::string& id,
83 const std::string& md5,
84 const base::FilePath& source_path,
85 FileOperationType file_operation_type);
87 // Pins the specified entry.
88 FileError Pin(const std::string& id);
90 // Unpins the specified entry.
91 FileError Unpin(const std::string& id);
93 // Sets the state of the cache entry corresponding to |id| as mounted.
94 FileError MarkAsMounted(const std::string& id,
95 base::FilePath* cache_file_path);
97 // Sets the state of the cache entry corresponding to file_path as unmounted.
98 FileError MarkAsUnmounted(const base::FilePath& file_path);
100 // Opens the cache file corresponding to |id| for write. |file_closer| should
101 // be kept alive until writing finishes.
102 // This method must be called before writing to cache files.
103 FileError OpenForWrite(const std::string& id,
104 scoped_ptr<base::ScopedClosureRunner>* file_closer);
106 // Returns true if the cache file corresponding to |id| is write-opened.
107 bool IsOpenedForWrite(const std::string& id);
109 // Calculates MD5 of the cache file and updates the stored value.
110 FileError UpdateMd5(const std::string& id);
112 // Clears dirty state of the specified entry.
113 FileError ClearDirty(const std::string& id);
115 // Removes the specified cache entry and delete cache files if available.
116 FileError Remove(const std::string& id);
118 // Removes all the files in the cache directory.
119 bool ClearAll();
121 // Initializes the cache. Returns true on success.
122 bool Initialize();
124 // Destroys this cache. This function posts a task to the blocking task
125 // runner to safely delete the object.
126 // Must be called on the UI thread.
127 void Destroy();
129 // Moves files in the cache directory which are not managed by FileCache to
130 // |dest_directory|.
131 // |recovered_cache_info| should contain cache info recovered from the trashed
132 // metadata DB. It is used to ignore non-dirty files.
133 bool RecoverFilesFromCacheDirectory(
134 const base::FilePath& dest_directory,
135 const ResourceMetadataStorage::RecoveredCacheInfoMap&
136 recovered_cache_info);
138 private:
139 friend class FileCacheTest;
141 ~FileCache();
143 // Returns absolute path of the file if it were cached or to be cached.
145 // Can be called on any thread.
146 base::FilePath GetCacheFilePath(const std::string& id) const;
148 // Checks whether the current thread is on the right sequenced worker pool
149 // with the right sequence ID. If not, DCHECK will fail.
150 void AssertOnSequencedWorkerPool();
152 // Destroys the cache on the blocking pool.
153 void DestroyOnBlockingPool();
155 // Returns true if we have sufficient space to store the given number of
156 // bytes, while keeping cryptohome::kMinFreeSpaceInBytes bytes on the disk.
157 bool HasEnoughSpaceFor(int64 num_bytes, const base::FilePath& path);
159 // Renames cache files from old "prefix:id.md5" format to the new format.
160 // TODO(hashimoto): Remove this method at some point.
161 bool RenameCacheFilesToNewFormat();
163 // This method must be called after writing to a cache file.
164 // Used to implement OpenForWrite().
165 void CloseForWrite(const std::string& id);
167 const base::FilePath cache_file_directory_;
169 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
171 base::CancellationFlag in_shutdown_;
173 ResourceMetadataStorage* storage_;
175 FreeDiskSpaceGetterInterface* free_disk_space_getter_; // Not owned.
177 // IDs of files being write-opened.
178 std::map<std::string, int> write_opened_files_;
180 // IDs of files marked mounted.
181 std::set<std::string> mounted_files_;
183 // Note: This should remain the last member so it'll be destroyed and
184 // invalidate its weak pointers before any other members are destroyed.
185 // This object should be accessed only on |blocking_task_runner_|.
186 base::WeakPtrFactory<FileCache> weak_ptr_factory_;
187 DISALLOW_COPY_AND_ASSIGN(FileCache);
190 } // namespace internal
191 } // namespace drive
193 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_CACHE_H_