[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 | |
[email protected] | ec83778 | 2013-05-01 07:20:24 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_FAKE_FILE_SYSTEM_H_ |
| 6 | #define CHROME_BROWSER_CHROMEOS_DRIVE_FAKE_FILE_SYSTEM_H_ |
[email protected] | 88e15d2e | 2013-02-15 10:53:47 | [diff] [blame] | 7 | |
| 8 | #include <string> |
| 9 | |
| 10 | #include "base/basictypes.h" |
[email protected] | d775226 | 2013-03-26 12:56:59 | [diff] [blame] | 11 | #include "base/callback_forward.h" |
[email protected] | 7e8cd6e3 | 2013-04-22 08:01:01 | [diff] [blame] | 12 | #include "base/files/scoped_temp_dir.h" |
[email protected] | 88e15d2e | 2013-02-15 10:53:47 | [diff] [blame] | 13 | #include "base/memory/scoped_ptr.h" |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 14 | #include "chrome/browser/chromeos/drive/file_errors.h" |
[email protected] | 976309f1 | 2013-05-02 01:19:15 | [diff] [blame] | 15 | #include "chrome/browser/chromeos/drive/file_system_interface.h" |
[email protected] | d775226 | 2013-03-26 12:56:59 | [diff] [blame] | 16 | #include "chrome/browser/google_apis/gdata_errorcode.h" |
[email protected] | 88e15d2e | 2013-02-15 10:53:47 | [diff] [blame] | 17 | |
| 18 | namespace google_apis { |
| 19 | |
[email protected] | d775226 | 2013-03-26 12:56:59 | [diff] [blame] | 20 | class AboutResource; |
| 21 | class DriveServiceInterface; |
[email protected] | 88e15d2e | 2013-02-15 10:53:47 | [diff] [blame] | 22 | class ResourceEntry; |
| 23 | |
| 24 | } // namespace google_apis |
| 25 | |
| 26 | namespace drive { |
| 27 | |
[email protected] | 43309dd9 | 2013-04-25 07:28:03 | [diff] [blame] | 28 | class FileSystemObserver; |
[email protected] | 67241b83 | 2013-05-02 04:55:52 | [diff] [blame] | 29 | class ResourceEntry; |
[email protected] | 88e15d2e | 2013-02-15 10:53:47 | [diff] [blame] | 30 | |
| 31 | namespace test_util { |
| 32 | |
[email protected] | 0d52ed5 | 2013-05-01 08:21:21 | [diff] [blame] | 33 | // This class implements a fake FileSystem which acts like a real Drive |
[email protected] | 88e15d2e | 2013-02-15 10:53:47 | [diff] [blame] | 34 | // file system with FakeDriveService, for testing purpose. |
| 35 | // Note that this class doesn't support "caching" at the moment, so the number |
| 36 | // of interactions to the FakeDriveService may be bigger than the real |
| 37 | // implementation. |
| 38 | // Currently most methods are empty (not implemented). |
[email protected] | 976309f1 | 2013-05-02 01:19:15 | [diff] [blame] | 39 | class FakeFileSystem : public FileSystemInterface { |
[email protected] | 88e15d2e | 2013-02-15 10:53:47 | [diff] [blame] | 40 | public: |
[email protected] | ec83778 | 2013-05-01 07:20:24 | [diff] [blame] | 41 | explicit FakeFileSystem( |
[email protected] | d775226 | 2013-03-26 12:56:59 | [diff] [blame] | 42 | google_apis::DriveServiceInterface* drive_service); |
[email protected] | ec83778 | 2013-05-01 07:20:24 | [diff] [blame] | 43 | virtual ~FakeFileSystem(); |
[email protected] | 88e15d2e | 2013-02-15 10:53:47 | [diff] [blame] | 44 | |
[email protected] | 7e8cd6e3 | 2013-04-22 08:01:01 | [diff] [blame] | 45 | // Initialization for testing. This can be called instead of Initialize |
| 46 | // for testing purpose. Returns true for success. |
| 47 | bool InitializeForTesting(); |
| 48 | |
[email protected] | 976309f1 | 2013-05-02 01:19:15 | [diff] [blame] | 49 | // FileSystemInterface Overrides. |
[email protected] | 88e15d2e | 2013-02-15 10:53:47 | [diff] [blame] | 50 | virtual void Initialize() OVERRIDE; |
[email protected] | 43309dd9 | 2013-04-25 07:28:03 | [diff] [blame] | 51 | virtual void AddObserver(FileSystemObserver* observer) OVERRIDE; |
| 52 | virtual void RemoveObserver(FileSystemObserver* observer) OVERRIDE; |
[email protected] | 88e15d2e | 2013-02-15 10:53:47 | [diff] [blame] | 53 | virtual void CheckForUpdates() OVERRIDE; |
| 54 | virtual void GetEntryInfoByResourceId( |
| 55 | const std::string& resource_id, |
| 56 | const GetEntryInfoWithFilePathCallback& callback) OVERRIDE; |
| 57 | virtual void TransferFileFromRemoteToLocal( |
| 58 | const base::FilePath& remote_src_file_path, |
| 59 | const base::FilePath& local_dest_file_path, |
| 60 | const FileOperationCallback& callback) OVERRIDE; |
| 61 | virtual void TransferFileFromLocalToRemote( |
| 62 | const base::FilePath& local_src_file_path, |
| 63 | const base::FilePath& remote_dest_file_path, |
| 64 | const FileOperationCallback& callback) OVERRIDE; |
| 65 | virtual void OpenFile(const base::FilePath& file_path, |
| 66 | const OpenFileCallback& callback) OVERRIDE; |
| 67 | virtual void CloseFile(const base::FilePath& file_path, |
| 68 | const FileOperationCallback& callback) OVERRIDE; |
| 69 | virtual void Copy(const base::FilePath& src_file_path, |
| 70 | const base::FilePath& dest_file_path, |
| 71 | const FileOperationCallback& callback) OVERRIDE; |
| 72 | virtual void Move(const base::FilePath& src_file_path, |
| 73 | const base::FilePath& dest_file_path, |
| 74 | const FileOperationCallback& callback) OVERRIDE; |
| 75 | virtual void Remove(const base::FilePath& file_path, |
| 76 | bool is_recursive, |
| 77 | const FileOperationCallback& callback) OVERRIDE; |
| 78 | virtual void CreateDirectory(const base::FilePath& directory_path, |
| 79 | bool is_exclusive, |
| 80 | bool is_recursive, |
| 81 | const FileOperationCallback& callback) OVERRIDE; |
| 82 | virtual void CreateFile(const base::FilePath& file_path, |
| 83 | bool is_exclusive, |
| 84 | const FileOperationCallback& callback) OVERRIDE; |
[email protected] | 378b613 | 2013-04-19 18:05:18 | [diff] [blame] | 85 | virtual void Pin(const base::FilePath& file_path, |
| 86 | const FileOperationCallback& callback) OVERRIDE; |
| 87 | virtual void Unpin(const base::FilePath& file_path, |
| 88 | const FileOperationCallback& callback) OVERRIDE; |
[email protected] | 88e15d2e | 2013-02-15 10:53:47 | [diff] [blame] | 89 | virtual void GetFileByPath(const base::FilePath& file_path, |
| 90 | const GetFileCallback& callback) OVERRIDE; |
| 91 | virtual void GetFileByResourceId( |
| 92 | const std::string& resource_id, |
| 93 | const DriveClientContext& context, |
| 94 | const GetFileCallback& get_file_callback, |
| 95 | const google_apis::GetContentCallback& get_content_callback) OVERRIDE; |
[email protected] | 8e6e51f8 | 2013-04-16 05:04:00 | [diff] [blame] | 96 | virtual void GetFileContentByPath( |
| 97 | const base::FilePath& file_path, |
| 98 | const GetFileContentInitializedCallback& initialized_callback, |
| 99 | const google_apis::GetContentCallback& get_content_callback, |
| 100 | const FileOperationCallback& completion_callback) OVERRIDE; |
[email protected] | 88e15d2e | 2013-02-15 10:53:47 | [diff] [blame] | 101 | virtual void UpdateFileByResourceId( |
| 102 | const std::string& resource_id, |
| 103 | const DriveClientContext& context, |
| 104 | const FileOperationCallback& callback) OVERRIDE; |
| 105 | virtual void GetEntryInfoByPath( |
| 106 | const base::FilePath& file_path, |
| 107 | const GetEntryInfoCallback& callback) OVERRIDE; |
| 108 | virtual void ReadDirectoryByPath( |
| 109 | const base::FilePath& file_path, |
| 110 | const ReadDirectoryWithSettingCallback& callback) OVERRIDE; |
[email protected] | 6d7779888 | 2013-03-06 01:25:24 | [diff] [blame] | 111 | virtual void RefreshDirectory( |
| 112 | const base::FilePath& file_path, |
| 113 | const FileOperationCallback& callback) OVERRIDE; |
[email protected] | 88e15d2e | 2013-02-15 10:53:47 | [diff] [blame] | 114 | virtual void Search(const std::string& search_query, |
[email protected] | 88e15d2e | 2013-02-15 10:53:47 | [diff] [blame] | 115 | const GURL& next_feed, |
| 116 | const SearchCallback& callback) OVERRIDE; |
[email protected] | 14b69c5 | 2013-03-27 09:48:47 | [diff] [blame] | 117 | virtual void SearchMetadata(const std::string& query, |
[email protected] | 48c884ca | 2013-04-09 12:40:01 | [diff] [blame] | 118 | int options, |
[email protected] | 14b69c5 | 2013-03-27 09:48:47 | [diff] [blame] | 119 | int at_most_num_matches, |
| 120 | const SearchMetadataCallback& callback) OVERRIDE; |
[email protected] | 88e15d2e | 2013-02-15 10:53:47 | [diff] [blame] | 121 | virtual void GetAvailableSpace( |
| 122 | const GetAvailableSpaceCallback& callback) OVERRIDE; |
[email protected] | ec529fa | 2013-03-07 10:22:17 | [diff] [blame] | 123 | virtual void AddUploadedFile(scoped_ptr<google_apis::ResourceEntry> doc_entry, |
[email protected] | 88e15d2e | 2013-02-15 10:53:47 | [diff] [blame] | 124 | const base::FilePath& file_content_path, |
| 125 | const FileOperationCallback& callback) OVERRIDE; |
| 126 | virtual void GetMetadata( |
| 127 | const GetFilesystemMetadataCallback& callback) OVERRIDE; |
[email protected] | 48348d5c | 2013-04-22 17:44:22 | [diff] [blame] | 128 | virtual void MarkCacheFileAsMounted( |
| 129 | const base::FilePath& drive_file_path, |
| 130 | const OpenFileCallback& callback) OVERRIDE; |
| 131 | virtual void MarkCacheFileAsUnmounted( |
| 132 | const base::FilePath& cache_file_path, |
| 133 | const FileOperationCallback& callback) OVERRIDE; |
[email protected] | 45dff6d | 2013-04-23 17:47:31 | [diff] [blame] | 134 | virtual void GetCacheEntryByResourceId( |
| 135 | const std::string& resource_id, |
| 136 | const std::string& md5, |
| 137 | const GetCacheEntryCallback& callback) OVERRIDE; |
[email protected] | 88e15d2e | 2013-02-15 10:53:47 | [diff] [blame] | 138 | virtual void Reload() OVERRIDE; |
| 139 | |
| 140 | private: |
[email protected] | d775226 | 2013-03-26 12:56:59 | [diff] [blame] | 141 | // Callback to return the result of GetFilePath. |
| 142 | typedef base::Callback<void(const base::FilePath& file_path)> |
| 143 | GetFilePathCallback; |
| 144 | |
| 145 | // Returns the path for the |resource_id| via |callback|. |
| 146 | // How the method works: |
| 147 | // 1) Gets AboutResource from the drive service to obtain root resource id. |
| 148 | // 2) Gets ResourceEntry from the drive service to get the base name, |
| 149 | // prepends it to the |file_path|. Unless it is root, also tries for |
| 150 | // the parent recursively. |
| 151 | void GetFilePath(const std::string& resource_id, |
| 152 | const GetFilePathCallback& callback); |
| 153 | void GetFilePathAfterGetAboutResource( |
| 154 | const std::string& resource_id, |
| 155 | const GetFilePathCallback& callback, |
| 156 | google_apis::GDataErrorCode error, |
| 157 | scoped_ptr<google_apis::AboutResource> about_resource); |
| 158 | void GetFilePathInternal( |
| 159 | const std::string& root_resource_id, |
| 160 | const std::string& resource_id, |
| 161 | const base::FilePath& file_path, |
| 162 | const GetFilePathCallback& callback); |
| 163 | void GetFilePathAfterGetResourceEntry( |
| 164 | const std::string& root_resource_id, |
| 165 | const base::FilePath& remaining_file_path, |
| 166 | const GetFilePathCallback& callback, |
| 167 | google_apis::GDataErrorCode error_in, |
| 168 | scoped_ptr<google_apis::ResourceEntry> resource_entry); |
| 169 | |
| 170 | // Helpers of GetEntryInfoByResourceId. |
| 171 | // How the method works: |
| 172 | // 1) Gets ResourceEntry from the drive service. |
| 173 | // 2) Gets the file path of the resource. |
| 174 | // 3) Runs the |callback|. |
| 175 | void GetEntryInfoByResourceIdAfterGetResourceEntry( |
| 176 | const GetEntryInfoWithFilePathCallback& callback, |
| 177 | google_apis::GDataErrorCode error_in, |
| 178 | scoped_ptr<google_apis::ResourceEntry> resource_entry); |
| 179 | void GetEntryInfoByResourceIdAfterGetFilePath( |
| 180 | const GetEntryInfoWithFilePathCallback& callback, |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 181 | FileError error, |
[email protected] | 67241b83 | 2013-05-02 04:55:52 | [diff] [blame] | 182 | scoped_ptr<ResourceEntry> entry, |
[email protected] | d775226 | 2013-03-26 12:56:59 | [diff] [blame] | 183 | const base::FilePath& parent_file_path); |
| 184 | |
[email protected] | 7e8cd6e3 | 2013-04-22 08:01:01 | [diff] [blame] | 185 | // Helpers of GetFileContentByPath. |
| 186 | // How the method works: |
[email protected] | 67241b83 | 2013-05-02 04:55:52 | [diff] [blame] | 187 | // 1) Gets ResourceEntry of the path. |
[email protected] | 7e8cd6e3 | 2013-04-22 08:01:01 | [diff] [blame] | 188 | // 2) Look at if there is a cache file or not. If found return it. |
| 189 | // 3) Otherwise start DownloadFile. |
| 190 | // 4) Runs the |completion_callback| upon the download completion. |
| 191 | void GetFileContentByPathAfterGetEntryInfo( |
| 192 | const base::FilePath& file_path, |
| 193 | const GetFileContentInitializedCallback& initialized_callback, |
| 194 | const google_apis::GetContentCallback& get_content_callback, |
| 195 | const FileOperationCallback& completion_callback, |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 196 | FileError error, |
[email protected] | 67241b83 | 2013-05-02 04:55:52 | [diff] [blame] | 197 | scoped_ptr<ResourceEntry> entry); |
[email protected] | 5b82c9d | 2013-05-10 03:06:27 | [diff] [blame^] | 198 | void GetFileContentByPathAfterGetResourceEntry( |
| 199 | const base::FilePath& file_path, |
| 200 | const GetFileContentInitializedCallback& initialized_callback, |
| 201 | const google_apis::GetContentCallback& get_content_callback, |
| 202 | const FileOperationCallback& completion_callback, |
| 203 | google_apis::GDataErrorCode gdata_error, |
| 204 | scoped_ptr<google_apis::ResourceEntry> gdata_entry); |
[email protected] | 7e8cd6e3 | 2013-04-22 08:01:01 | [diff] [blame] | 205 | void GetFileContentByPathAfterDownloadFile( |
| 206 | const FileOperationCallback& completion_callback, |
| 207 | google_apis::GDataErrorCode gdata_error, |
| 208 | const base::FilePath& temp_file); |
| 209 | |
[email protected] | d14c29b | 2013-04-01 09:44:18 | [diff] [blame] | 210 | // Helpers of GetEntryInfoByPath. |
| 211 | // How the method works: |
| 212 | // 1) If the path is root, gets AboutResrouce from the drive service |
[email protected] | 67241b83 | 2013-05-02 04:55:52 | [diff] [blame] | 213 | // and create ResourceEntry. |
| 214 | // 2-1) Otherwise, gets the parent's ResourceEntry by recursive call. |
[email protected] | d14c29b | 2013-04-01 09:44:18 | [diff] [blame] | 215 | // 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] | 216 | // 2-3) Search the results based on title, and return the ResourceEntry. |
[email protected] | d14c29b | 2013-04-01 09:44:18 | [diff] [blame] | 217 | // 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] | 218 | // not supported in FakeFileSystem. Thus, even if the server has |
[email protected] | d14c29b | 2013-04-01 09:44:18 | [diff] [blame] | 219 | // files sharing the same name under a directory, the second (or later) |
| 220 | // file cannot be taken with the suffixed name. |
| 221 | void GetEntryInfoByPathAfterGetAboutResource( |
| 222 | const GetEntryInfoCallback& callback, |
| 223 | google_apis::GDataErrorCode gdata_error, |
| 224 | scoped_ptr<google_apis::AboutResource> about_resource); |
| 225 | void GetEntryInfoByPathAfterGetParentEntryInfo( |
| 226 | const base::FilePath& base_name, |
| 227 | const GetEntryInfoCallback& callback, |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 228 | FileError error, |
[email protected] | 67241b83 | 2013-05-02 04:55:52 | [diff] [blame] | 229 | scoped_ptr<ResourceEntry> parent_entry); |
[email protected] | d14c29b | 2013-04-01 09:44:18 | [diff] [blame] | 230 | void GetEntryInfoByPathAfterGetResourceList( |
| 231 | const base::FilePath& base_name, |
| 232 | const GetEntryInfoCallback& callback, |
| 233 | google_apis::GDataErrorCode gdata_error, |
| 234 | scoped_ptr<google_apis::ResourceList> resource_list); |
| 235 | |
[email protected] | d775226 | 2013-03-26 12:56:59 | [diff] [blame] | 236 | google_apis::DriveServiceInterface* drive_service_; // Not owned. |
[email protected] | 7e8cd6e3 | 2013-04-22 08:01:01 | [diff] [blame] | 237 | base::ScopedTempDir cache_dir_; |
[email protected] | d775226 | 2013-03-26 12:56:59 | [diff] [blame] | 238 | |
| 239 | // Note: This should remain the last member so it'll be destroyed and |
| 240 | // invalidate the weak pointers before any other members are destroyed. |
[email protected] | ec83778 | 2013-05-01 07:20:24 | [diff] [blame] | 241 | base::WeakPtrFactory<FakeFileSystem> weak_ptr_factory_; |
[email protected] | d775226 | 2013-03-26 12:56:59 | [diff] [blame] | 242 | |
[email protected] | ec83778 | 2013-05-01 07:20:24 | [diff] [blame] | 243 | DISALLOW_COPY_AND_ASSIGN(FakeFileSystem); |
[email protected] | 88e15d2e | 2013-02-15 10:53:47 | [diff] [blame] | 244 | }; |
| 245 | |
| 246 | } // namespace test_util |
| 247 | } // namespace drive |
| 248 | |
[email protected] | ec83778 | 2013-05-01 07:20:24 | [diff] [blame] | 249 | #endif // CHROME_BROWSER_CHROMEOS_DRIVE_FAKE_FILE_SYSTEM_H_ |