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