Add 'did_proceed' and 'repeat_visit' to ClientMalwareReportRequest to track CTR.
[chromium-blink-merge.git] / components / drive / file_cache.h
blobb205a77b700e300b0b3b475c0bb8675c44054da6
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 COMPONENTS_DRIVE_FILE_CACHE_H_
6 #define COMPONENTS_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 "base/threading/thread_checker.h"
16 #include "components/drive/file_errors.h"
17 #include "components/drive/resource_metadata_storage.h"
18 #if defined(OS_CHROMEOS)
19 #include "third_party/cros_system_api/constants/cryptohome.h"
20 #endif
22 namespace base {
23 class ScopedClosureRunner;
24 class SequencedTaskRunner;
25 } // namespace base
27 namespace drive {
29 namespace internal {
31 #if defined(OS_CHROMEOS)
32 const int64 kMinFreeSpaceInBytes = cryptohome::kMinFreeSpaceInBytes;
33 #else
34 const int64 kMinFreeSpaceInBytes = 512ull * 1024ull * 1024ull; // 512MB
35 #endif
37 // Interface class used for getting the free disk space. Tests can inject an
38 // implementation that reports fake free disk space.
39 class FreeDiskSpaceGetterInterface {
40 public:
41 virtual ~FreeDiskSpaceGetterInterface() {}
42 virtual int64 AmountOfFreeDiskSpace() = 0;
45 // FileCache is used to maintain cache states of FileSystem.
47 // All non-static public member functions, unless mentioned otherwise (see
48 // GetCacheFilePath() for example), should be run with |blocking_task_runner|.
49 class FileCache {
50 public:
51 // Enum defining type of file operation e.g. copy or move, etc.
52 enum FileOperationType {
53 FILE_OPERATION_MOVE = 0,
54 FILE_OPERATION_COPY,
57 // |cache_file_directory| stores cached files.
59 // |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.
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.
66 // Must be called on the UI thread.
67 FileCache(ResourceMetadataStorage* storage,
68 const base::FilePath& cache_file_directory,
69 base::SequencedTaskRunner* blocking_task_runner,
70 FreeDiskSpaceGetterInterface* free_disk_space_getter);
72 // Returns true if the given path is under drive cache directory, i.e.
73 // <user_profile_dir>/GCache/v1
75 // Can be called on any thread.
76 bool IsUnderFileCacheDirectory(const base::FilePath& path) const;
78 // Frees up disk space to store a file with |num_bytes| size content, while
79 // keeping drive::internal::kMinFreeSpaceInBytes bytes on the disk, if needed.
80 // Returns true if we successfully manage to have enough space, otherwise
81 // false.
82 bool FreeDiskSpaceIfNeededFor(int64 num_bytes);
84 // Calculates and returns evictable cache size. In error case, this returns 0.
85 uint64_t CalculateEvictableCacheSize();
87 // Checks if file corresponding to |id| exists in cache, and returns
88 // FILE_ERROR_OK with |cache_file_path| storing the path to the file.
89 // |cache_file_path| must not be null.
90 FileError GetFile(const std::string& id, base::FilePath* cache_file_path);
92 // Stores |source_path| as a cache of the remote content of the file
93 // with |id| and |md5|.
94 // Pass an empty string as MD5 to mark the entry as dirty.
95 FileError Store(const std::string& id,
96 const std::string& md5,
97 const base::FilePath& source_path,
98 FileOperationType file_operation_type);
100 // Pins the specified entry.
101 FileError Pin(const std::string& id);
103 // Unpins the specified entry.
104 FileError Unpin(const std::string& id);
106 // 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);
110 // Sets the state of the cache entry corresponding to file_path as unmounted.
111 FileError MarkAsUnmounted(const base::FilePath& file_path);
113 // 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);
119 // Returns true if the cache file corresponding to |id| is write-opened.
120 bool IsOpenedForWrite(const std::string& id);
122 // Calculates MD5 of the cache file and updates the stored value.
123 FileError UpdateMd5(const std::string& id);
125 // Clears dirty state of the specified entry.
126 FileError ClearDirty(const std::string& id);
128 // Removes the specified cache entry and delete cache files if available.
129 FileError Remove(const std::string& id);
131 // Removes all the files in the cache directory.
132 bool ClearAll();
134 // Initializes the cache. Returns true on success.
135 bool Initialize();
137 // Destroys this cache. This function posts a task to the blocking task
138 // runner to safely delete the object.
139 // Must be called on the UI thread.
140 void Destroy();
142 // Moves files in the cache directory which are not managed by FileCache to
143 // |dest_directory|.
144 // |recovered_cache_info| should contain cache info recovered from the trashed
145 // metadata DB. It is used to ignore non-dirty files.
146 bool RecoverFilesFromCacheDirectory(
147 const base::FilePath& dest_directory,
148 const ResourceMetadataStorage::RecoveredCacheInfoMap&
149 recovered_cache_info);
151 private:
152 friend class FileCacheTest;
154 ~FileCache();
156 // Returns absolute path of the file if it were cached or to be cached.
158 // Can be called on any thread.
159 base::FilePath GetCacheFilePath(const std::string& id) const;
161 // 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();
165 // Destroys the cache on the blocking pool.
166 void DestroyOnBlockingPool();
168 // Returns true if we have sufficient space to store the given number of
169 // bytes, while keeping drive::internal::kMinFreeSpaceInBytes bytes on the
170 // disk.
171 bool HasEnoughSpaceFor(int64 num_bytes, const base::FilePath& path);
173 // Renames cache files from old "prefix:id.md5" format to the new format.
174 // TODO(hashimoto): Remove this method at some point.
175 bool RenameCacheFilesToNewFormat();
177 // This method must be called after writing to a cache file.
178 // Used to implement OpenForWrite().
179 void CloseForWrite(const std::string& id);
181 // Returns true if the cache entry can be evicted.
182 bool IsEvictable(const std::string& id, const ResourceEntry& entry);
184 const base::FilePath cache_file_directory_;
186 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
188 base::CancellationFlag in_shutdown_;
190 ResourceMetadataStorage* storage_;
192 FreeDiskSpaceGetterInterface* free_disk_space_getter_; // Not owned.
194 // IDs of files being write-opened.
195 std::map<std::string, int> write_opened_files_;
197 // IDs of files marked mounted.
198 std::set<std::string> mounted_files_;
200 base::ThreadChecker thread_checker_;
202 // Note: This should remain the last member so it'll be destroyed and
203 // invalidate its weak pointers before any other members are destroyed.
204 // This object should be accessed only on |blocking_task_runner_|.
205 base::WeakPtrFactory<FileCache> weak_ptr_factory_;
206 DISALLOW_COPY_AND_ASSIGN(FileCache);
209 } // namespace internal
210 } // namespace drive
212 #endif // COMPONENTS_DRIVE_FILE_CACHE_H_