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_
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"
20 class ScopedClosureRunner
;
21 class SequencedTaskRunner
;
28 // Interface class used for getting the free disk space. Tests can inject an
29 // implementation that reports fake free disk space.
30 class FreeDiskSpaceGetterInterface
{
32 virtual ~FreeDiskSpaceGetterInterface() {}
33 virtual int64
AmountOfFreeDiskSpace() = 0;
36 // FileCache is used to maintain cache states of FileSystem.
38 // All non-static public member functions, unless mentioned otherwise (see
39 // GetCacheFilePath() for example), should be run with |blocking_task_runner|.
42 // Enum defining type of file operation e.g. copy or move, etc.
43 enum FileOperationType
{
44 FILE_OPERATION_MOVE
= 0,
48 // |cache_file_directory| stores cached files.
50 // |blocking_task_runner| indicates the blocking worker pool for cache
51 // operations. All operations on this FileCache must be run on this runner.
54 // |free_disk_space_getter| is used to inject a custom free disk space
55 // getter for testing. NULL must be passed for production code.
57 // Must be called on the UI thread.
58 FileCache(ResourceMetadataStorage
* storage
,
59 const base::FilePath
& cache_file_directory
,
60 base::SequencedTaskRunner
* blocking_task_runner
,
61 FreeDiskSpaceGetterInterface
* free_disk_space_getter
);
63 // Returns true if the given path is under drive cache directory, i.e.
64 // <user_profile_dir>/GCache/v1
66 // Can be called on any thread.
67 bool IsUnderFileCacheDirectory(const base::FilePath
& path
) const;
69 // Frees up disk space to store a file with |num_bytes| size content, while
70 // keeping cryptohome::kMinFreeSpaceInBytes bytes on the disk, if needed.
71 // Returns true if we successfully manage to have enough space, otherwise
73 bool FreeDiskSpaceIfNeededFor(int64 num_bytes
);
75 // Checks if file corresponding to |id| exists in cache, and returns
76 // FILE_ERROR_OK with |cache_file_path| storing the path to the file.
77 // |cache_file_path| must not be null.
78 FileError
GetFile(const std::string
& id
, base::FilePath
* cache_file_path
);
80 // Stores |source_path| as a cache of the remote content of the file
81 // with |id| and |md5|.
82 // Pass an empty string as MD5 to mark the entry as dirty.
83 FileError
Store(const std::string
& id
,
84 const std::string
& md5
,
85 const base::FilePath
& source_path
,
86 FileOperationType file_operation_type
);
88 // Pins the specified entry.
89 FileError
Pin(const std::string
& id
);
91 // Unpins the specified entry.
92 FileError
Unpin(const std::string
& id
);
94 // Sets the state of the cache entry corresponding to |id| as mounted.
95 FileError
MarkAsMounted(const std::string
& id
,
96 base::FilePath
* cache_file_path
);
98 // Sets the state of the cache entry corresponding to file_path as unmounted.
99 FileError
MarkAsUnmounted(const base::FilePath
& file_path
);
101 // Opens the cache file corresponding to |id| for write. |file_closer| should
102 // be kept alive until writing finishes.
103 // This method must be called before writing to cache files.
104 FileError
OpenForWrite(const std::string
& id
,
105 scoped_ptr
<base::ScopedClosureRunner
>* file_closer
);
107 // Returns true if the cache file corresponding to |id| is write-opened.
108 bool IsOpenedForWrite(const std::string
& id
);
110 // Calculates MD5 of the cache file and updates the stored value.
111 FileError
UpdateMd5(const std::string
& id
);
113 // Clears dirty state of the specified entry.
114 FileError
ClearDirty(const std::string
& id
);
116 // Removes the specified cache entry and delete cache files if available.
117 FileError
Remove(const std::string
& id
);
119 // Removes all the files in the cache directory.
122 // Initializes the cache. Returns true on success.
125 // Destroys this cache. This function posts a task to the blocking task
126 // runner to safely delete the object.
127 // Must be called on the UI thread.
130 // Moves files in the cache directory which are not managed by FileCache to
132 // |recovered_cache_info| should contain cache info recovered from the trashed
133 // metadata DB. It is used to ignore non-dirty files.
134 bool RecoverFilesFromCacheDirectory(
135 const base::FilePath
& dest_directory
,
136 const ResourceMetadataStorage::RecoveredCacheInfoMap
&
137 recovered_cache_info
);
140 friend class FileCacheTest
;
144 // Returns absolute path of the file if it were cached or to be cached.
146 // Can be called on any thread.
147 base::FilePath
GetCacheFilePath(const std::string
& id
) const;
149 // Checks whether the current thread is on the right sequenced worker pool
150 // with the right sequence ID. If not, DCHECK will fail.
151 void AssertOnSequencedWorkerPool();
153 // Destroys the cache on the blocking pool.
154 void DestroyOnBlockingPool();
156 // Returns true if we have sufficient space to store the given number of
157 // bytes, while keeping cryptohome::kMinFreeSpaceInBytes bytes on the disk.
158 bool HasEnoughSpaceFor(int64 num_bytes
, const base::FilePath
& path
);
160 // Renames cache files from old "prefix:id.md5" format to the new format.
161 // TODO(hashimoto): Remove this method at some point.
162 bool RenameCacheFilesToNewFormat();
164 // This method must be called after writing to a cache file.
165 // Used to implement OpenForWrite().
166 void CloseForWrite(const std::string
& id
);
168 const base::FilePath cache_file_directory_
;
170 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner_
;
172 base::CancellationFlag in_shutdown_
;
174 ResourceMetadataStorage
* storage_
;
176 FreeDiskSpaceGetterInterface
* free_disk_space_getter_
; // Not owned.
178 // IDs of files being write-opened.
179 std::map
<std::string
, int> write_opened_files_
;
181 // IDs of files marked mounted.
182 std::set
<std::string
> mounted_files_
;
184 base::ThreadChecker thread_checker_
;
186 // Note: This should remain the last member so it'll be destroyed and
187 // invalidate its weak pointers before any other members are destroyed.
188 // This object should be accessed only on |blocking_task_runner_|.
189 base::WeakPtrFactory
<FileCache
> weak_ptr_factory_
;
190 DISALLOW_COPY_AND_ASSIGN(FileCache
);
193 } // namespace internal
196 #endif // COMPONENTS_DRIVE_FILE_CACHE_H_