| OLD | NEW |
| (Empty) |
| 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 | |
| 5 #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_FAKE_DRIVE_FILE_SYSTEM_H_ | |
| 6 #define CHROME_BROWSER_CHROMEOS_DRIVE_FAKE_DRIVE_FILE_SYSTEM_H_ | |
| 7 | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/basictypes.h" | |
| 11 #include "base/callback_forward.h" | |
| 12 #include "base/files/scoped_temp_dir.h" | |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "chrome/browser/chromeos/drive/drive_file_system_interface.h" | |
| 15 #include "chrome/browser/chromeos/drive/file_errors.h" | |
| 16 #include "chrome/browser/google_apis/gdata_errorcode.h" | |
| 17 | |
| 18 namespace google_apis { | |
| 19 | |
| 20 class AboutResource; | |
| 21 class DriveServiceInterface; | |
| 22 class ResourceEntry; | |
| 23 | |
| 24 } // namespace google_apis | |
| 25 | |
| 26 namespace drive { | |
| 27 | |
| 28 class DriveEntryProto; | |
| 29 class FileSystemObserver; | |
| 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). | |
| 39 class FakeDriveFileSystem : public DriveFileSystemInterface { | |
| 40 public: | |
| 41 explicit FakeDriveFileSystem( | |
| 42 google_apis::DriveServiceInterface* drive_service); | |
| 43 virtual ~FakeDriveFileSystem(); | |
| 44 | |
| 45 // Initialization for testing. This can be called instead of Initialize | |
| 46 // for testing purpose. Returns true for success. | |
| 47 bool InitializeForTesting(); | |
| 48 | |
| 49 // DriveFileSystemInterface Overrides. | |
| 50 virtual void Initialize() OVERRIDE; | |
| 51 virtual void AddObserver(FileSystemObserver* observer) OVERRIDE; | |
| 52 virtual void RemoveObserver(FileSystemObserver* observer) OVERRIDE; | |
| 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; | |
| 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; | |
| 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; | |
| 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; | |
| 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; | |
| 111 virtual void RefreshDirectory( | |
| 112 const base::FilePath& file_path, | |
| 113 const FileOperationCallback& callback) OVERRIDE; | |
| 114 virtual void Search(const std::string& search_query, | |
| 115 const GURL& next_feed, | |
| 116 const SearchCallback& callback) OVERRIDE; | |
| 117 virtual void SearchMetadata(const std::string& query, | |
| 118 int options, | |
| 119 int at_most_num_matches, | |
| 120 const SearchMetadataCallback& callback) OVERRIDE; | |
| 121 virtual void GetAvailableSpace( | |
| 122 const GetAvailableSpaceCallback& callback) OVERRIDE; | |
| 123 virtual void AddUploadedFile(scoped_ptr<google_apis::ResourceEntry> doc_entry, | |
| 124 const base::FilePath& file_content_path, | |
| 125 const FileOperationCallback& callback) OVERRIDE; | |
| 126 virtual void GetMetadata( | |
| 127 const GetFilesystemMetadataCallback& callback) OVERRIDE; | |
| 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; | |
| 134 virtual void GetCacheEntryByResourceId( | |
| 135 const std::string& resource_id, | |
| 136 const std::string& md5, | |
| 137 const GetCacheEntryCallback& callback) OVERRIDE; | |
| 138 virtual void IterateCache(const CacheIterateCallback& iteration_callback, | |
| 139 const base::Closure& completion_callback) OVERRIDE; | |
| 140 virtual void Reload() OVERRIDE; | |
| 141 | |
| 142 private: | |
| 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, | |
| 183 FileError error, | |
| 184 scoped_ptr<DriveEntryProto> entry_proto, | |
| 185 const base::FilePath& parent_file_path); | |
| 186 | |
| 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, | |
| 198 FileError error, | |
| 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 | |
| 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 | |
| 213 // not supported in FakeDriveFileSystem. Thus, even if the server has | |
| 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, | |
| 223 FileError error, | |
| 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 | |
| 231 google_apis::DriveServiceInterface* drive_service_; // Not owned. | |
| 232 base::ScopedTempDir cache_dir_; | |
| 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. | |
| 236 base::WeakPtrFactory<FakeDriveFileSystem> weak_ptr_factory_; | |
| 237 | |
| 238 DISALLOW_COPY_AND_ASSIGN(FakeDriveFileSystem); | |
| 239 }; | |
| 240 | |
| 241 } // namespace test_util | |
| 242 } // namespace drive | |
| 243 | |
| 244 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_FAKE_DRIVE_FILE_SYSTEM_H_ | |
| OLD | NEW |