[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] | 88e15d2e | 2013-02-15 10:53:47 | [diff] [blame] | 14 | #include "chrome/browser/chromeos/drive/drive_file_system_interface.h" |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 15 | #include "chrome/browser/chromeos/drive/file_errors.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] | d775226 | 2013-03-26 12:56:59 | [diff] [blame] | 28 | class DriveEntryProto; |
[email protected] | 43309dd9 | 2013-04-25 07:28:03 | [diff] [blame] | 29 | class FileSystemObserver; |
[email protected] | 88e15d2e | 2013-02-15 10:53:47 | [diff] [blame] | 30 | |
| 31 | namespace test_util { |
| 32 | |
| 33 | // This class implements a fake DriveFileSystem which acts like a real Drive |
| 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] | ec83778 | 2013-05-01 07:20:24 | [diff] [blame^] | 39 | class FakeFileSystem : public DriveFileSystemInterface { |
[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] | 88e15d2e | 2013-02-15 10:53:47 | [diff] [blame] | 49 | // DriveFileSystemInterface Overrides. |
| 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] | cc45a5f | 2013-04-24 04:49:00 | [diff] [blame] | 138 | virtual void IterateCache(const CacheIterateCallback& iteration_callback, |
| 139 | const base::Closure& completion_callback) OVERRIDE; |
[email protected] | 88e15d2e | 2013-02-15 10:53:47 | [diff] [blame] | 140 | virtual void Reload() OVERRIDE; |
| 141 | |
| 142 | private: |
[email protected] | d775226 | 2013-03-26 12:56:59 | [diff] [blame] | 143 | // Callback to return the result of GetFilePath. |
| 144 | typedef base::Callback<void(const base::FilePath& file_path)> |
| 145 | GetFilePathCallback; |
| 146 | |
| 147 | // Returns the path for the |resource_id| via |callback|. |
| 148 | // How the method works: |
| 149 | // 1) Gets AboutResource from the drive service to obtain root resource id. |
| 150 | // 2) Gets ResourceEntry from the drive service to get the base name, |
| 151 | // prepends it to the |file_path|. Unless it is root, also tries for |
| 152 | // the parent recursively. |
| 153 | void GetFilePath(const std::string& resource_id, |
| 154 | const GetFilePathCallback& callback); |
| 155 | void GetFilePathAfterGetAboutResource( |
| 156 | const std::string& resource_id, |
| 157 | const GetFilePathCallback& callback, |
| 158 | google_apis::GDataErrorCode error, |
| 159 | scoped_ptr<google_apis::AboutResource> about_resource); |
| 160 | void GetFilePathInternal( |
| 161 | const std::string& root_resource_id, |
| 162 | const std::string& resource_id, |
| 163 | const base::FilePath& file_path, |
| 164 | const GetFilePathCallback& callback); |
| 165 | void GetFilePathAfterGetResourceEntry( |
| 166 | const std::string& root_resource_id, |
| 167 | const base::FilePath& remaining_file_path, |
| 168 | const GetFilePathCallback& callback, |
| 169 | google_apis::GDataErrorCode error_in, |
| 170 | scoped_ptr<google_apis::ResourceEntry> resource_entry); |
| 171 | |
| 172 | // Helpers of GetEntryInfoByResourceId. |
| 173 | // How the method works: |
| 174 | // 1) Gets ResourceEntry from the drive service. |
| 175 | // 2) Gets the file path of the resource. |
| 176 | // 3) Runs the |callback|. |
| 177 | void GetEntryInfoByResourceIdAfterGetResourceEntry( |
| 178 | const GetEntryInfoWithFilePathCallback& callback, |
| 179 | google_apis::GDataErrorCode error_in, |
| 180 | scoped_ptr<google_apis::ResourceEntry> resource_entry); |
| 181 | void GetEntryInfoByResourceIdAfterGetFilePath( |
| 182 | const GetEntryInfoWithFilePathCallback& callback, |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 183 | FileError error, |
[email protected] | d775226 | 2013-03-26 12:56:59 | [diff] [blame] | 184 | scoped_ptr<DriveEntryProto> entry_proto, |
| 185 | const base::FilePath& parent_file_path); |
| 186 | |
[email protected] | 7e8cd6e3 | 2013-04-22 08:01:01 | [diff] [blame] | 187 | // Helpers of GetFileContentByPath. |
| 188 | // How the method works: |
| 189 | // 1) Gets DriveEntryProto of the path. |
| 190 | // 2) Look at if there is a cache file or not. If found return it. |
| 191 | // 3) Otherwise start DownloadFile. |
| 192 | // 4) Runs the |completion_callback| upon the download completion. |
| 193 | void GetFileContentByPathAfterGetEntryInfo( |
| 194 | const base::FilePath& file_path, |
| 195 | const GetFileContentInitializedCallback& initialized_callback, |
| 196 | const google_apis::GetContentCallback& get_content_callback, |
| 197 | const FileOperationCallback& completion_callback, |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 198 | FileError error, |
[email protected] | 7e8cd6e3 | 2013-04-22 08:01:01 | [diff] [blame] | 199 | scoped_ptr<DriveEntryProto> entry_proto); |
| 200 | void GetFileContentByPathAfterDownloadFile( |
| 201 | const FileOperationCallback& completion_callback, |
| 202 | google_apis::GDataErrorCode gdata_error, |
| 203 | const base::FilePath& temp_file); |
| 204 | |
[email protected] | d14c29b | 2013-04-01 09:44:18 | [diff] [blame] | 205 | // Helpers of GetEntryInfoByPath. |
| 206 | // How the method works: |
| 207 | // 1) If the path is root, gets AboutResrouce from the drive service |
| 208 | // and create DriveEntryProto. |
| 209 | // 2-1) Otherwise, gets the parent's DriveEntryProto by recursive call. |
| 210 | // 2-2) Then, gets the resource list by restricting the parent with its id. |
| 211 | // 2-3) Search the results based on title, and return the DriveEntryProto. |
| 212 | // 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^] | 213 | // not supported in FakeFileSystem. Thus, even if the server has |
[email protected] | d14c29b | 2013-04-01 09:44:18 | [diff] [blame] | 214 | // files sharing the same name under a directory, the second (or later) |
| 215 | // file cannot be taken with the suffixed name. |
| 216 | void GetEntryInfoByPathAfterGetAboutResource( |
| 217 | const GetEntryInfoCallback& callback, |
| 218 | google_apis::GDataErrorCode gdata_error, |
| 219 | scoped_ptr<google_apis::AboutResource> about_resource); |
| 220 | void GetEntryInfoByPathAfterGetParentEntryInfo( |
| 221 | const base::FilePath& base_name, |
| 222 | const GetEntryInfoCallback& callback, |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 223 | FileError error, |
[email protected] | d14c29b | 2013-04-01 09:44:18 | [diff] [blame] | 224 | scoped_ptr<DriveEntryProto> parent_entry_proto); |
| 225 | void GetEntryInfoByPathAfterGetResourceList( |
| 226 | const base::FilePath& base_name, |
| 227 | const GetEntryInfoCallback& callback, |
| 228 | google_apis::GDataErrorCode gdata_error, |
| 229 | scoped_ptr<google_apis::ResourceList> resource_list); |
| 230 | |
[email protected] | d775226 | 2013-03-26 12:56:59 | [diff] [blame] | 231 | google_apis::DriveServiceInterface* drive_service_; // Not owned. |
[email protected] | 7e8cd6e3 | 2013-04-22 08:01:01 | [diff] [blame] | 232 | base::ScopedTempDir cache_dir_; |
[email protected] | d775226 | 2013-03-26 12:56:59 | [diff] [blame] | 233 | |
| 234 | // Note: This should remain the last member so it'll be destroyed and |
| 235 | // invalidate the weak pointers before any other members are destroyed. |
[email protected] | ec83778 | 2013-05-01 07:20:24 | [diff] [blame^] | 236 | base::WeakPtrFactory<FakeFileSystem> weak_ptr_factory_; |
[email protected] | d775226 | 2013-03-26 12:56:59 | [diff] [blame] | 237 | |
[email protected] | ec83778 | 2013-05-01 07:20:24 | [diff] [blame^] | 238 | DISALLOW_COPY_AND_ASSIGN(FakeFileSystem); |
[email protected] | 88e15d2e | 2013-02-15 10:53:47 | [diff] [blame] | 239 | }; |
| 240 | |
| 241 | } // namespace test_util |
| 242 | } // namespace drive |
| 243 | |
[email protected] | ec83778 | 2013-05-01 07:20:24 | [diff] [blame^] | 244 | #endif // CHROME_BROWSER_CHROMEOS_DRIVE_FAKE_FILE_SYSTEM_H_ |