| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_FILE_CACHE_H_ | |
| 6 #define CHROME_BROWSER_CHROMEOS_DRIVE_FILE_CACHE_H_ | |
| 7 | |
| 8 #include <set> | |
| 9 #include <string> | |
| 10 | |
| 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 "chrome/browser/chromeos/drive/resource_metadata_storage.h" | |
| 17 #include "components/drive/file_errors.h" | |
| 18 | |
| 19 namespace base { | |
| 20 class ScopedClosureRunner; | |
| 21 class SequencedTaskRunner; | |
| 22 } // namespace base | |
| 23 | |
| 24 namespace drive { | |
| 25 | |
| 26 namespace internal { | |
| 27 | |
| 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 { | |
| 31 public: | |
| 32 virtual ~FreeDiskSpaceGetterInterface() {} | |
| 33 virtual int64 AmountOfFreeDiskSpace() = 0; | |
| 34 }; | |
| 35 | |
| 36 // FileCache is used to maintain cache states of FileSystem. | |
| 37 // | |
| 38 // All non-static public member functions, unless mentioned otherwise (see | |
| 39 // GetCacheFilePath() for example), should be run with |blocking_task_runner|. | |
| 40 class FileCache { | |
| 41 public: | |
| 42 // Enum defining type of file operation e.g. copy or move, etc. | |
| 43 enum FileOperationType { | |
| 44 FILE_OPERATION_MOVE = 0, | |
| 45 FILE_OPERATION_COPY, | |
| 46 }; | |
| 47 | |
| 48 // |cache_file_directory| stores cached files. | |
| 49 // | |
| 50 // |blocking_task_runner| indicates the blocking worker pool for cache | |
| 51 // operations. All operations on this FileCache must be run on this runner. | |
| 52 // Must not be null. | |
| 53 // | |
| 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. | |
| 56 // | |
| 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); | |
| 62 | |
| 63 // Returns true if the given path is under drive cache directory, i.e. | |
| 64 // <user_profile_dir>/GCache/v1 | |
| 65 // | |
| 66 // Can be called on any thread. | |
| 67 bool IsUnderFileCacheDirectory(const base::FilePath& path) const; | |
| 68 | |
| 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 | |
| 72 // false. | |
| 73 bool FreeDiskSpaceIfNeededFor(int64 num_bytes); | |
| 74 | |
| 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); | |
| 79 | |
| 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); | |
| 87 | |
| 88 // Pins the specified entry. | |
| 89 FileError Pin(const std::string& id); | |
| 90 | |
| 91 // Unpins the specified entry. | |
| 92 FileError Unpin(const std::string& id); | |
| 93 | |
| 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); | |
| 97 | |
| 98 // Sets the state of the cache entry corresponding to file_path as unmounted. | |
| 99 FileError MarkAsUnmounted(const base::FilePath& file_path); | |
| 100 | |
| 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); | |
| 106 | |
| 107 // Returns true if the cache file corresponding to |id| is write-opened. | |
| 108 bool IsOpenedForWrite(const std::string& id); | |
| 109 | |
| 110 // Calculates MD5 of the cache file and updates the stored value. | |
| 111 FileError UpdateMd5(const std::string& id); | |
| 112 | |
| 113 // Clears dirty state of the specified entry. | |
| 114 FileError ClearDirty(const std::string& id); | |
| 115 | |
| 116 // Removes the specified cache entry and delete cache files if available. | |
| 117 FileError Remove(const std::string& id); | |
| 118 | |
| 119 // Removes all the files in the cache directory. | |
| 120 bool ClearAll(); | |
| 121 | |
| 122 // Initializes the cache. Returns true on success. | |
| 123 bool Initialize(); | |
| 124 | |
| 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. | |
| 128 void Destroy(); | |
| 129 | |
| 130 // Moves files in the cache directory which are not managed by FileCache to | |
| 131 // |dest_directory|. | |
| 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); | |
| 138 | |
| 139 private: | |
| 140 friend class FileCacheTest; | |
| 141 | |
| 142 ~FileCache(); | |
| 143 | |
| 144 // Returns absolute path of the file if it were cached or to be cached. | |
| 145 // | |
| 146 // Can be called on any thread. | |
| 147 base::FilePath GetCacheFilePath(const std::string& id) const; | |
| 148 | |
| 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(); | |
| 152 | |
| 153 // Destroys the cache on the blocking pool. | |
| 154 void DestroyOnBlockingPool(); | |
| 155 | |
| 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); | |
| 159 | |
| 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(); | |
| 163 | |
| 164 // This method must be called after writing to a cache file. | |
| 165 // Used to implement OpenForWrite(). | |
| 166 void CloseForWrite(const std::string& id); | |
| 167 | |
| 168 const base::FilePath cache_file_directory_; | |
| 169 | |
| 170 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; | |
| 171 | |
| 172 base::CancellationFlag in_shutdown_; | |
| 173 | |
| 174 ResourceMetadataStorage* storage_; | |
| 175 | |
| 176 FreeDiskSpaceGetterInterface* free_disk_space_getter_; // Not owned. | |
| 177 | |
| 178 // IDs of files being write-opened. | |
| 179 std::map<std::string, int> write_opened_files_; | |
| 180 | |
| 181 // IDs of files marked mounted. | |
| 182 std::set<std::string> mounted_files_; | |
| 183 | |
| 184 base::ThreadChecker thread_checker_; | |
| 185 | |
| 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); | |
| 191 }; | |
| 192 | |
| 193 } // namespace internal | |
| 194 } // namespace drive | |
| 195 | |
| 196 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_CACHE_H_ | |
| OLD | NEW |