[email protected] | 88e15d2e | 2013-02-15 10:53:47 | [diff] [blame] | 1 | // Copyright (c) 2013 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 | |
yawano | 3513e14 | 2016-04-20 00:42:42 | [diff] [blame] | 5 | #ifndef COMPONENTS_DRIVE_CHROMEOS_FAKE_FILE_SYSTEM_H_ |
| 6 | #define COMPONENTS_DRIVE_CHROMEOS_FAKE_FILE_SYSTEM_H_ |
[email protected] | 88e15d2e | 2013-02-15 10:53:47 | [diff] [blame] | 7 | |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
dcheng | f4275023 | 2016-04-12 04:12:27 | [diff] [blame] | 10 | #include <memory> |
Stuart Langley | 664e229 | 2018-08-31 04:58:28 | [diff] [blame] | 11 | #include <set> |
[email protected] | 88e15d2e | 2013-02-15 10:53:47 | [diff] [blame] | 12 | #include <string> |
| 13 | |
[email protected] | d775226 | 2013-03-26 12:56:59 | [diff] [blame] | 14 | #include "base/callback_forward.h" |
[email protected] | 7e8cd6e3 | 2013-04-22 08:01:01 | [diff] [blame] | 15 | #include "base/files/scoped_temp_dir.h" |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 16 | #include "base/macros.h" |
yawano | 3513e14 | 2016-04-20 00:42:42 | [diff] [blame] | 17 | #include "components/drive/chromeos/file_system_interface.h" |
lukasza | 76b4a98 | 2015-08-08 00:36:39 | [diff] [blame] | 18 | #include "components/drive/file_errors.h" |
satorux | 1e04b42 | 2015-01-29 07:50:53 | [diff] [blame] | 19 | #include "google_apis/drive/drive_api_error_codes.h" |
[email protected] | 88e15d2e | 2013-02-15 10:53:47 | [diff] [blame] | 20 | |
| 21 | namespace google_apis { |
| 22 | |
[email protected] | d775226 | 2013-03-26 12:56:59 | [diff] [blame] | 23 | class AboutResource; |
[email protected] | 3125844 | 2014-06-05 03:37:41 | [diff] [blame] | 24 | class FileResource; |
[email protected] | 88e15d2e | 2013-02-15 10:53:47 | [diff] [blame] | 25 | |
| 26 | } // namespace google_apis |
| 27 | |
| 28 | namespace drive { |
| 29 | |
[email protected] | e50af765 | 2013-06-20 06:39:31 | [diff] [blame] | 30 | class DriveServiceInterface; |
[email protected] | 43309dd9 | 2013-04-25 07:28:03 | [diff] [blame] | 31 | class FileSystemObserver; |
[email protected] | 67241b83 | 2013-05-02 04:55:52 | [diff] [blame] | 32 | class ResourceEntry; |
[email protected] | 88e15d2e | 2013-02-15 10:53:47 | [diff] [blame] | 33 | |
| 34 | namespace test_util { |
| 35 | |
[email protected] | 0d52ed5 | 2013-05-01 08:21:21 | [diff] [blame] | 36 | // This class implements a fake FileSystem which acts like a real Drive |
[email protected] | 88e15d2e | 2013-02-15 10:53:47 | [diff] [blame] | 37 | // file system with FakeDriveService, for testing purpose. |
| 38 | // Note that this class doesn't support "caching" at the moment, so the number |
| 39 | // of interactions to the FakeDriveService may be bigger than the real |
| 40 | // implementation. |
| 41 | // Currently most methods are empty (not implemented). |
[email protected] | 976309f1 | 2013-05-02 01:19:15 | [diff] [blame] | 42 | class FakeFileSystem : public FileSystemInterface { |
[email protected] | 88e15d2e | 2013-02-15 10:53:47 | [diff] [blame] | 43 | public: |
[email protected] | e50af765 | 2013-06-20 06:39:31 | [diff] [blame] | 44 | explicit FakeFileSystem(DriveServiceInterface* drive_service); |
dcheng | fe77330 | 2015-01-22 19:45:52 | [diff] [blame] | 45 | ~FakeFileSystem() override; |
[email protected] | 88e15d2e | 2013-02-15 10:53:47 | [diff] [blame] | 46 | |
[email protected] | 976309f1 | 2013-05-02 01:19:15 | [diff] [blame] | 47 | // FileSystemInterface Overrides. |
dcheng | fe77330 | 2015-01-22 19:45:52 | [diff] [blame] | 48 | void AddObserver(FileSystemObserver* observer) override; |
| 49 | void RemoveObserver(FileSystemObserver* observer) override; |
| 50 | void CheckForUpdates() override; |
Stuart Langley | 664e229 | 2018-08-31 04:58:28 | [diff] [blame] | 51 | void CheckForUpdates(const std::set<std::string>& ids) override; |
dcheng | fe77330 | 2015-01-22 19:45:52 | [diff] [blame] | 52 | void TransferFileFromLocalToRemote( |
[email protected] | 88e15d2e | 2013-02-15 10:53:47 | [diff] [blame] | 53 | const base::FilePath& local_src_file_path, |
| 54 | const base::FilePath& remote_dest_file_path, |
mostynb | 9108efb9 | 2014-10-03 23:46:15 | [diff] [blame] | 55 | const FileOperationCallback& callback) override; |
dcheng | fe77330 | 2015-01-22 19:45:52 | [diff] [blame] | 56 | void OpenFile(const base::FilePath& file_path, |
| 57 | OpenMode open_mode, |
| 58 | const std::string& mime_type, |
Stuart Langley | 5887106 | 2018-08-28 05:10:58 | [diff] [blame] | 59 | OpenFileCallback callback) override; |
dcheng | fe77330 | 2015-01-22 19:45:52 | [diff] [blame] | 60 | void Copy(const base::FilePath& src_file_path, |
| 61 | const base::FilePath& dest_file_path, |
| 62 | bool preserve_last_modified, |
| 63 | const FileOperationCallback& callback) override; |
| 64 | void Move(const base::FilePath& src_file_path, |
| 65 | const base::FilePath& dest_file_path, |
| 66 | const FileOperationCallback& callback) override; |
| 67 | void Remove(const base::FilePath& file_path, |
| 68 | bool is_recursive, |
| 69 | const FileOperationCallback& callback) override; |
| 70 | void CreateDirectory(const base::FilePath& directory_path, |
| 71 | bool is_exclusive, |
| 72 | bool is_recursive, |
| 73 | const FileOperationCallback& callback) override; |
| 74 | void CreateFile(const base::FilePath& file_path, |
| 75 | bool is_exclusive, |
| 76 | const std::string& mime_type, |
| 77 | const FileOperationCallback& callback) override; |
| 78 | void TouchFile(const base::FilePath& file_path, |
| 79 | const base::Time& last_access_time, |
| 80 | const base::Time& last_modified_time, |
| 81 | const FileOperationCallback& callback) override; |
| 82 | void TruncateFile(const base::FilePath& file_path, |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 83 | int64_t length, |
mostynb | 9108efb9 | 2014-10-03 23:46:15 | [diff] [blame] | 84 | const FileOperationCallback& callback) override; |
dcheng | fe77330 | 2015-01-22 19:45:52 | [diff] [blame] | 85 | void Pin(const base::FilePath& file_path, |
| 86 | const FileOperationCallback& callback) override; |
| 87 | void Unpin(const base::FilePath& file_path, |
| 88 | const FileOperationCallback& callback) override; |
| 89 | void GetFile(const base::FilePath& file_path, |
Stuart Langley | bd8b7ee1 | 2018-08-27 01:24:44 | [diff] [blame] | 90 | GetFileCallback callback) override; |
dcheng | fe77330 | 2015-01-22 19:45:52 | [diff] [blame] | 91 | void GetFileForSaving(const base::FilePath& file_path, |
Stuart Langley | bd8b7ee1 | 2018-08-27 01:24:44 | [diff] [blame] | 92 | GetFileCallback callback) override; |
dcheng | fe77330 | 2015-01-22 19:45:52 | [diff] [blame] | 93 | base::Closure GetFileContent( |
[email protected] | 8e6e51f8 | 2013-04-16 05:04:00 | [diff] [blame] | 94 | const base::FilePath& file_path, |
Stuart Langley | 48b5145 | 2018-08-28 01:17:10 | [diff] [blame] | 95 | GetFileContentInitializedCallback initialized_callback, |
[email protected] | 8e6e51f8 | 2013-04-16 05:04:00 | [diff] [blame] | 96 | const google_apis::GetContentCallback& get_content_callback, |
mostynb | 9108efb9 | 2014-10-03 23:46:15 | [diff] [blame] | 97 | const FileOperationCallback& completion_callback) override; |
dcheng | fe77330 | 2015-01-22 19:45:52 | [diff] [blame] | 98 | void GetResourceEntry(const base::FilePath& file_path, |
Stuart Langley | d3b60202 | 2018-08-27 04:24:59 | [diff] [blame] | 99 | GetResourceEntryCallback callback) override; |
dcheng | fe77330 | 2015-01-22 19:45:52 | [diff] [blame] | 100 | void ReadDirectory(const base::FilePath& file_path, |
Stuart Langley | 4819fdb | 2018-08-27 00:12:28 | [diff] [blame] | 101 | ReadDirectoryEntriesCallback entries_callback, |
dcheng | fe77330 | 2015-01-22 19:45:52 | [diff] [blame] | 102 | const FileOperationCallback& completion_callback) override; |
| 103 | void Search(const std::string& search_query, |
| 104 | const GURL& next_link, |
Stuart Langley | 6a4013c9 | 2018-08-28 01:27:14 | [diff] [blame] | 105 | SearchCallback callback) override; |
dcheng | fe77330 | 2015-01-22 19:45:52 | [diff] [blame] | 106 | void SearchMetadata(const std::string& query, |
| 107 | int options, |
| 108 | int at_most_num_matches, |
Shuhei Takahashi | 5459d1be | 2017-08-16 08:47:54 | [diff] [blame] | 109 | MetadataSearchOrder order, |
Stuart Langley | 6a4013c9 | 2018-08-28 01:27:14 | [diff] [blame] | 110 | SearchMetadataCallback callback) override; |
hirono | 87acdc6 | 2015-01-23 03:18:21 | [diff] [blame] | 111 | void SearchByHashes(const std::set<std::string>& hashes, |
Stuart Langley | 6a4013c9 | 2018-08-28 01:27:14 | [diff] [blame] | 112 | SearchByHashesCallback callback) override; |
Stuart Langley | 5f46218 | 2018-09-11 01:04:16 | [diff] [blame] | 113 | void GetAvailableSpace(GetAvailableSpaceCallback callback) override; |
Stuart Langley | 4819fdb | 2018-08-27 00:12:28 | [diff] [blame] | 114 | void GetMetadata(GetFilesystemMetadataCallback callback) override; |
dcheng | fe77330 | 2015-01-22 19:45:52 | [diff] [blame] | 115 | void MarkCacheFileAsMounted(const base::FilePath& drive_file_path, |
Stuart Langley | 503c3a2c | 2018-09-14 01:15:46 | [diff] [blame] | 116 | MarkMountedCallback callback) override; |
dcheng | fe77330 | 2015-01-22 19:45:52 | [diff] [blame] | 117 | void MarkCacheFileAsUnmounted(const base::FilePath& cache_file_path, |
| 118 | const FileOperationCallback& callback) override; |
Tatsuhisa Yamaguchi | 4810128 | 2018-03-20 02:18:39 | [diff] [blame] | 119 | void IsCacheFileMarkedAsMounted(const base::FilePath& drive_file_path, |
Stuart Langley | 503c3a2c | 2018-09-14 01:15:46 | [diff] [blame] | 120 | IsMountedCallback callback) override; |
dcheng | fe77330 | 2015-01-22 19:45:52 | [diff] [blame] | 121 | void AddPermission(const base::FilePath& drive_file_path, |
| 122 | const std::string& email, |
| 123 | google_apis::drive::PermissionRole role, |
| 124 | const FileOperationCallback& callback) override; |
mtomasz | 163271c | 2015-02-24 10:50:19 | [diff] [blame] | 125 | void SetProperty(const base::FilePath& drive_file_path, |
| 126 | google_apis::drive::Property::Visibility visibility, |
| 127 | const std::string& key, |
| 128 | const std::string& value, |
| 129 | const FileOperationCallback& callback) override; |
dcheng | fe77330 | 2015-01-22 19:45:52 | [diff] [blame] | 130 | void Reset(const FileOperationCallback& callback) override; |
| 131 | void GetPathFromResourceId(const std::string& resource_id, |
| 132 | const GetFilePathCallback& callback) override; |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 133 | void FreeDiskSpaceIfNeededFor(int64_t num_bytes, |
hirono | d727d87 | 2015-02-17 09:32:51 | [diff] [blame] | 134 | const FreeDiskSpaceCallback& callback) override; |
fukino | 6380c07c | 2016-06-09 07:28:29 | [diff] [blame] | 135 | void CalculateCacheSize(const CacheSizeCallback& callback) override; |
| 136 | void CalculateEvictableCacheSize(const CacheSizeCallback& callback) override; |
[email protected] | 88e15d2e | 2013-02-15 10:53:47 | [diff] [blame] | 137 | |
| 138 | private: |
[email protected] | 6297d0a | 2013-11-07 14:06:14 | [diff] [blame] | 139 | // Helpers of GetFileContent. |
[email protected] | 7e8cd6e3 | 2013-04-22 08:01:01 | [diff] [blame] | 140 | // How the method works: |
[email protected] | 67241b83 | 2013-05-02 04:55:52 | [diff] [blame] | 141 | // 1) Gets ResourceEntry of the path. |
[email protected] | 7e8cd6e3 | 2013-04-22 08:01:01 | [diff] [blame] | 142 | // 2) Look at if there is a cache file or not. If found return it. |
| 143 | // 3) Otherwise start DownloadFile. |
| 144 | // 4) Runs the |completion_callback| upon the download completion. |
[email protected] | 6297d0a | 2013-11-07 14:06:14 | [diff] [blame] | 145 | void GetFileContentAfterGetResourceEntry( |
Stuart Langley | 48b5145 | 2018-08-28 01:17:10 | [diff] [blame] | 146 | GetFileContentInitializedCallback initialized_callback, |
[email protected] | 7e8cd6e3 | 2013-04-22 08:01:01 | [diff] [blame] | 147 | const google_apis::GetContentCallback& get_content_callback, |
| 148 | const FileOperationCallback& completion_callback, |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 149 | FileError error, |
dcheng | f4275023 | 2016-04-12 04:12:27 | [diff] [blame] | 150 | std::unique_ptr<ResourceEntry> entry); |
[email protected] | 3125844 | 2014-06-05 03:37:41 | [diff] [blame] | 151 | void GetFileContentAfterGetFileResource( |
Stuart Langley | 48b5145 | 2018-08-28 01:17:10 | [diff] [blame] | 152 | GetFileContentInitializedCallback initialized_callback, |
[email protected] | 5b82c9d | 2013-05-10 03:06:27 | [diff] [blame] | 153 | const google_apis::GetContentCallback& get_content_callback, |
| 154 | const FileOperationCallback& completion_callback, |
satorux | 1e04b42 | 2015-01-29 07:50:53 | [diff] [blame] | 155 | google_apis::DriveApiErrorCode gdata_error, |
dcheng | f4275023 | 2016-04-12 04:12:27 | [diff] [blame] | 156 | std::unique_ptr<google_apis::FileResource> gdata_entry); |
[email protected] | 6297d0a | 2013-11-07 14:06:14 | [diff] [blame] | 157 | void GetFileContentAfterDownloadFile( |
[email protected] | 7e8cd6e3 | 2013-04-22 08:01:01 | [diff] [blame] | 158 | const FileOperationCallback& completion_callback, |
satorux | 1e04b42 | 2015-01-29 07:50:53 | [diff] [blame] | 159 | google_apis::DriveApiErrorCode gdata_error, |
[email protected] | 7e8cd6e3 | 2013-04-22 08:01:01 | [diff] [blame] | 160 | const base::FilePath& temp_file); |
| 161 | |
[email protected] | 6297d0a | 2013-11-07 14:06:14 | [diff] [blame] | 162 | // Helpers of GetResourceEntry. |
[email protected] | d14c29b | 2013-04-01 09:44:18 | [diff] [blame] | 163 | // How the method works: |
| 164 | // 1) If the path is root, gets AboutResrouce from the drive service |
[email protected] | 67241b83 | 2013-05-02 04:55:52 | [diff] [blame] | 165 | // and create ResourceEntry. |
| 166 | // 2-1) Otherwise, gets the parent's ResourceEntry by recursive call. |
[email protected] | d14c29b | 2013-04-01 09:44:18 | [diff] [blame] | 167 | // 2-2) Then, gets the resource list by restricting the parent with its id. |
[email protected] | 67241b83 | 2013-05-02 04:55:52 | [diff] [blame] | 168 | // 2-3) Search the results based on title, and return the ResourceEntry. |
[email protected] | d14c29b | 2013-04-01 09:44:18 | [diff] [blame] | 169 | // Note that adding suffix (e.g. " (2)") for files sharing a same name is |
[email protected] | ec83778 | 2013-05-01 07:20:24 | [diff] [blame] | 170 | // not supported in FakeFileSystem. Thus, even if the server has |
[email protected] | d14c29b | 2013-04-01 09:44:18 | [diff] [blame] | 171 | // files sharing the same name under a directory, the second (or later) |
| 172 | // file cannot be taken with the suffixed name. |
[email protected] | 6297d0a | 2013-11-07 14:06:14 | [diff] [blame] | 173 | void GetResourceEntryAfterGetAboutResource( |
Stuart Langley | d3b60202 | 2018-08-27 04:24:59 | [diff] [blame] | 174 | GetResourceEntryCallback callback, |
satorux | 1e04b42 | 2015-01-29 07:50:53 | [diff] [blame] | 175 | google_apis::DriveApiErrorCode gdata_error, |
dcheng | f4275023 | 2016-04-12 04:12:27 | [diff] [blame] | 176 | std::unique_ptr<google_apis::AboutResource> about_resource); |
[email protected] | 6297d0a | 2013-11-07 14:06:14 | [diff] [blame] | 177 | void GetResourceEntryAfterGetParentEntryInfo( |
[email protected] | d14c29b | 2013-04-01 09:44:18 | [diff] [blame] | 178 | const base::FilePath& base_name, |
Stuart Langley | d3b60202 | 2018-08-27 04:24:59 | [diff] [blame] | 179 | GetResourceEntryCallback callback, |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 180 | FileError error, |
dcheng | f4275023 | 2016-04-12 04:12:27 | [diff] [blame] | 181 | std::unique_ptr<ResourceEntry> parent_entry); |
[email protected] | 2d3f735 | 2014-06-02 05:51:54 | [diff] [blame] | 182 | void GetResourceEntryAfterGetFileList( |
[email protected] | d14c29b | 2013-04-01 09:44:18 | [diff] [blame] | 183 | const base::FilePath& base_name, |
Stuart Langley | d3b60202 | 2018-08-27 04:24:59 | [diff] [blame] | 184 | GetResourceEntryCallback callback, |
satorux | 1e04b42 | 2015-01-29 07:50:53 | [diff] [blame] | 185 | google_apis::DriveApiErrorCode gdata_error, |
dcheng | f4275023 | 2016-04-12 04:12:27 | [diff] [blame] | 186 | std::unique_ptr<google_apis::FileList> file_list); |
[email protected] | d14c29b | 2013-04-01 09:44:18 | [diff] [blame] | 187 | |
[email protected] | e50af765 | 2013-06-20 06:39:31 | [diff] [blame] | 188 | DriveServiceInterface* drive_service_; // Not owned. |
[email protected] | 7e8cd6e3 | 2013-04-22 08:01:01 | [diff] [blame] | 189 | base::ScopedTempDir cache_dir_; |
[email protected] | d775226 | 2013-03-26 12:56:59 | [diff] [blame] | 190 | |
| 191 | // Note: This should remain the last member so it'll be destroyed and |
| 192 | // invalidate the weak pointers before any other members are destroyed. |
Jeremy Roman | 47d432e | 2019-08-20 14:24:00 | [diff] [blame] | 193 | base::WeakPtrFactory<FakeFileSystem> weak_ptr_factory_{this}; |
[email protected] | d775226 | 2013-03-26 12:56:59 | [diff] [blame] | 194 | |
[email protected] | ec83778 | 2013-05-01 07:20:24 | [diff] [blame] | 195 | DISALLOW_COPY_AND_ASSIGN(FakeFileSystem); |
[email protected] | 88e15d2e | 2013-02-15 10:53:47 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | } // namespace test_util |
| 199 | } // namespace drive |
| 200 | |
yawano | 3513e14 | 2016-04-20 00:42:42 | [diff] [blame] | 201 | #endif // COMPONENTS_DRIVE_CHROMEOS_FAKE_FILE_SYSTEM_H_ |