| 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 #include "chrome/browser/chromeos/drive/fake_file_system.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/bind_helpers.h" | |
| 9 #include "base/callback.h" | |
| 10 #include "base/files/file_path.h" | |
| 11 #include "base/files/file_util.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "components/drive/drive.pb.h" | |
| 14 #include "components/drive/file_errors.h" | |
| 15 #include "components/drive/file_system_core_util.h" | |
| 16 #include "components/drive/resource_entry_conversion.h" | |
| 17 #include "components/drive/service/drive_service_interface.h" | |
| 18 #include "content/public/browser/browser_thread.h" | |
| 19 #include "google_apis/drive/drive_api_parser.h" | |
| 20 | |
| 21 namespace drive { | |
| 22 namespace test_util { | |
| 23 | |
| 24 using content::BrowserThread; | |
| 25 | |
| 26 FakeFileSystem::FakeFileSystem(DriveServiceInterface* drive_service) | |
| 27 : drive_service_(drive_service), | |
| 28 weak_ptr_factory_(this) { | |
| 29 CHECK(cache_dir_.CreateUniqueTempDir()); | |
| 30 } | |
| 31 | |
| 32 FakeFileSystem::~FakeFileSystem() { | |
| 33 } | |
| 34 | |
| 35 void FakeFileSystem::AddObserver(FileSystemObserver* observer) { | |
| 36 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 37 } | |
| 38 | |
| 39 void FakeFileSystem::RemoveObserver(FileSystemObserver* observer) { | |
| 40 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 41 } | |
| 42 | |
| 43 void FakeFileSystem::CheckForUpdates() { | |
| 44 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 45 } | |
| 46 | |
| 47 void FakeFileSystem::TransferFileFromLocalToRemote( | |
| 48 const base::FilePath& local_src_file_path, | |
| 49 const base::FilePath& remote_dest_file_path, | |
| 50 const FileOperationCallback& callback) { | |
| 51 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 52 } | |
| 53 | |
| 54 void FakeFileSystem::OpenFile(const base::FilePath& file_path, | |
| 55 OpenMode open_mode, | |
| 56 const std::string& mime_type, | |
| 57 const OpenFileCallback& callback) { | |
| 58 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 59 } | |
| 60 | |
| 61 void FakeFileSystem::Copy(const base::FilePath& src_file_path, | |
| 62 const base::FilePath& dest_file_path, | |
| 63 bool preserve_last_modified, | |
| 64 const FileOperationCallback& callback) { | |
| 65 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 66 } | |
| 67 | |
| 68 void FakeFileSystem::Move(const base::FilePath& src_file_path, | |
| 69 const base::FilePath& dest_file_path, | |
| 70 const FileOperationCallback& callback) { | |
| 71 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 72 } | |
| 73 | |
| 74 void FakeFileSystem::Remove(const base::FilePath& file_path, | |
| 75 bool is_recursive, | |
| 76 const FileOperationCallback& callback) { | |
| 77 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 78 } | |
| 79 | |
| 80 void FakeFileSystem::CreateDirectory( | |
| 81 const base::FilePath& directory_path, | |
| 82 bool is_exclusive, | |
| 83 bool is_recursive, | |
| 84 const FileOperationCallback& callback) { | |
| 85 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 86 } | |
| 87 | |
| 88 void FakeFileSystem::CreateFile(const base::FilePath& file_path, | |
| 89 bool is_exclusive, | |
| 90 const std::string& mime_type, | |
| 91 const FileOperationCallback& callback) { | |
| 92 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 93 } | |
| 94 | |
| 95 void FakeFileSystem::TouchFile(const base::FilePath& file_path, | |
| 96 const base::Time& last_access_time, | |
| 97 const base::Time& last_modified_time, | |
| 98 const FileOperationCallback& callback) { | |
| 99 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 100 } | |
| 101 | |
| 102 void FakeFileSystem::TruncateFile(const base::FilePath& file_path, | |
| 103 int64 length, | |
| 104 const FileOperationCallback& callback) { | |
| 105 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 106 } | |
| 107 | |
| 108 void FakeFileSystem::Pin(const base::FilePath& file_path, | |
| 109 const FileOperationCallback& callback) { | |
| 110 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 111 } | |
| 112 | |
| 113 void FakeFileSystem::Unpin(const base::FilePath& file_path, | |
| 114 const FileOperationCallback& callback) { | |
| 115 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 116 } | |
| 117 | |
| 118 void FakeFileSystem::GetFile(const base::FilePath& file_path, | |
| 119 const GetFileCallback& callback) { | |
| 120 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 121 } | |
| 122 | |
| 123 void FakeFileSystem::GetFileForSaving(const base::FilePath& file_path, | |
| 124 const GetFileCallback& callback) { | |
| 125 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 126 } | |
| 127 | |
| 128 base::Closure FakeFileSystem::GetFileContent( | |
| 129 const base::FilePath& file_path, | |
| 130 const GetFileContentInitializedCallback& initialized_callback, | |
| 131 const google_apis::GetContentCallback& get_content_callback, | |
| 132 const FileOperationCallback& completion_callback) { | |
| 133 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 134 | |
| 135 GetResourceEntry( | |
| 136 file_path, | |
| 137 base::Bind(&FakeFileSystem::GetFileContentAfterGetResourceEntry, | |
| 138 weak_ptr_factory_.GetWeakPtr(), | |
| 139 initialized_callback, get_content_callback, | |
| 140 completion_callback)); | |
| 141 return base::Bind(&base::DoNothing); | |
| 142 } | |
| 143 | |
| 144 void FakeFileSystem::GetResourceEntry( | |
| 145 const base::FilePath& file_path, | |
| 146 const GetResourceEntryCallback& callback) { | |
| 147 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 148 | |
| 149 if (file_path == util::GetDriveMyDriveRootPath()) { | |
| 150 // Specialized for the root entry. | |
| 151 drive_service_->GetAboutResource( | |
| 152 base::Bind( | |
| 153 &FakeFileSystem::GetResourceEntryAfterGetAboutResource, | |
| 154 weak_ptr_factory_.GetWeakPtr(), callback)); | |
| 155 return; | |
| 156 } | |
| 157 | |
| 158 // Now, we only support files under my drive. | |
| 159 DCHECK(util::GetDriveMyDriveRootPath().IsParent(file_path)); | |
| 160 GetResourceEntry( | |
| 161 file_path.DirName(), | |
| 162 base::Bind( | |
| 163 &FakeFileSystem::GetResourceEntryAfterGetParentEntryInfo, | |
| 164 weak_ptr_factory_.GetWeakPtr(), file_path.BaseName(), callback)); | |
| 165 } | |
| 166 | |
| 167 void FakeFileSystem::ReadDirectory( | |
| 168 const base::FilePath& file_path, | |
| 169 const ReadDirectoryEntriesCallback& entries_callback, | |
| 170 const FileOperationCallback& completion_callback) { | |
| 171 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 172 } | |
| 173 | |
| 174 void FakeFileSystem::Search(const std::string& search_query, | |
| 175 const GURL& next_link, | |
| 176 const SearchCallback& callback) { | |
| 177 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 178 } | |
| 179 | |
| 180 void FakeFileSystem::SearchMetadata( | |
| 181 const std::string& query, | |
| 182 int options, | |
| 183 int at_most_num_matches, | |
| 184 const SearchMetadataCallback& callback) { | |
| 185 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 186 } | |
| 187 | |
| 188 void FakeFileSystem::SearchByHashes(const std::set<std::string>& hashes, | |
| 189 const SearchByHashesCallback& callback) { | |
| 190 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 191 } | |
| 192 | |
| 193 void FakeFileSystem::GetAvailableSpace( | |
| 194 const GetAvailableSpaceCallback& callback) { | |
| 195 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 196 } | |
| 197 | |
| 198 void FakeFileSystem::GetShareUrl( | |
| 199 const base::FilePath& file_path, | |
| 200 const GURL& embed_origin, | |
| 201 const GetShareUrlCallback& callback) { | |
| 202 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 203 } | |
| 204 | |
| 205 void FakeFileSystem::GetMetadata( | |
| 206 const GetFilesystemMetadataCallback& callback) { | |
| 207 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 208 } | |
| 209 | |
| 210 void FakeFileSystem::MarkCacheFileAsMounted( | |
| 211 const base::FilePath& drive_file_path, | |
| 212 const MarkMountedCallback& callback) { | |
| 213 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 214 } | |
| 215 | |
| 216 void FakeFileSystem::MarkCacheFileAsUnmounted( | |
| 217 const base::FilePath& cache_file_path, | |
| 218 const FileOperationCallback& callback) { | |
| 219 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 220 } | |
| 221 | |
| 222 void FakeFileSystem::AddPermission(const base::FilePath& drive_file_path, | |
| 223 const std::string& email, | |
| 224 google_apis::drive::PermissionRole role, | |
| 225 const FileOperationCallback& callback) { | |
| 226 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 227 } | |
| 228 | |
| 229 void FakeFileSystem::SetProperty( | |
| 230 const base::FilePath& drive_file_path, | |
| 231 google_apis::drive::Property::Visibility visibility, | |
| 232 const std::string& key, | |
| 233 const std::string& value, | |
| 234 const FileOperationCallback& callback) { | |
| 235 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 236 } | |
| 237 | |
| 238 void FakeFileSystem::Reset(const FileOperationCallback& callback) { | |
| 239 } | |
| 240 | |
| 241 void FakeFileSystem::GetPathFromResourceId( | |
| 242 const std::string& resource_id, | |
| 243 const GetFilePathCallback& callback) { | |
| 244 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 245 } | |
| 246 | |
| 247 void FakeFileSystem::FreeDiskSpaceIfNeededFor( | |
| 248 int64 num_bytes, | |
| 249 const FreeDiskSpaceCallback& callback) { | |
| 250 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 251 } | |
| 252 | |
| 253 void FakeFileSystem::CalculateEvictableCacheSize( | |
| 254 const EvictableCacheSizeCallback& callback) { | |
| 255 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 256 } | |
| 257 | |
| 258 // Implementation of GetFileContent. | |
| 259 void FakeFileSystem::GetFileContentAfterGetResourceEntry( | |
| 260 const GetFileContentInitializedCallback& initialized_callback, | |
| 261 const google_apis::GetContentCallback& get_content_callback, | |
| 262 const FileOperationCallback& completion_callback, | |
| 263 FileError error, | |
| 264 scoped_ptr<ResourceEntry> entry) { | |
| 265 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 266 | |
| 267 if (error != FILE_ERROR_OK) { | |
| 268 completion_callback.Run(error); | |
| 269 return; | |
| 270 } | |
| 271 DCHECK(entry); | |
| 272 | |
| 273 // We're only interested in a file. | |
| 274 if (entry->file_info().is_directory()) { | |
| 275 completion_callback.Run(FILE_ERROR_NOT_A_FILE); | |
| 276 return; | |
| 277 } | |
| 278 | |
| 279 // Fetch google_apis::FileResource for its |download_url|. | |
| 280 drive_service_->GetFileResource( | |
| 281 entry->resource_id(), | |
| 282 base::Bind( | |
| 283 &FakeFileSystem::GetFileContentAfterGetFileResource, | |
| 284 weak_ptr_factory_.GetWeakPtr(), | |
| 285 initialized_callback, | |
| 286 get_content_callback, | |
| 287 completion_callback)); | |
| 288 } | |
| 289 | |
| 290 void FakeFileSystem::GetFileContentAfterGetFileResource( | |
| 291 const GetFileContentInitializedCallback& initialized_callback, | |
| 292 const google_apis::GetContentCallback& get_content_callback, | |
| 293 const FileOperationCallback& completion_callback, | |
| 294 google_apis::DriveApiErrorCode gdata_error, | |
| 295 scoped_ptr<google_apis::FileResource> gdata_entry) { | |
| 296 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 297 | |
| 298 FileError error = GDataToFileError(gdata_error); | |
| 299 if (error != FILE_ERROR_OK) { | |
| 300 completion_callback.Run(error); | |
| 301 return; | |
| 302 } | |
| 303 DCHECK(gdata_entry); | |
| 304 | |
| 305 scoped_ptr<ResourceEntry> entry(new ResourceEntry); | |
| 306 std::string parent_resource_id; | |
| 307 bool converted = ConvertFileResourceToResourceEntry( | |
| 308 *gdata_entry, entry.get(), &parent_resource_id); | |
| 309 DCHECK(converted); | |
| 310 entry->set_parent_local_id(parent_resource_id); | |
| 311 | |
| 312 base::FilePath cache_path = | |
| 313 cache_dir_.path().AppendASCII(entry->resource_id()); | |
| 314 if (entry->file_specific_info().is_hosted_document()) { | |
| 315 // For hosted documents return a dummy cache without server request. | |
| 316 int result = base::WriteFile(cache_path, "", 0); | |
| 317 DCHECK_EQ(0, result); | |
| 318 } | |
| 319 if (base::PathExists(cache_path)) { | |
| 320 // Cache file is found. | |
| 321 initialized_callback.Run(FILE_ERROR_OK, cache_path, entry.Pass()); | |
| 322 completion_callback.Run(FILE_ERROR_OK); | |
| 323 return; | |
| 324 } | |
| 325 | |
| 326 initialized_callback.Run(FILE_ERROR_OK, base::FilePath(), entry.Pass()); | |
| 327 drive_service_->DownloadFile( | |
| 328 cache_path, | |
| 329 gdata_entry->file_id(), | |
| 330 base::Bind(&FakeFileSystem::GetFileContentAfterDownloadFile, | |
| 331 weak_ptr_factory_.GetWeakPtr(), | |
| 332 completion_callback), | |
| 333 get_content_callback, | |
| 334 google_apis::ProgressCallback()); | |
| 335 } | |
| 336 | |
| 337 void FakeFileSystem::GetFileContentAfterDownloadFile( | |
| 338 const FileOperationCallback& completion_callback, | |
| 339 google_apis::DriveApiErrorCode gdata_error, | |
| 340 const base::FilePath& temp_file) { | |
| 341 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 342 completion_callback.Run(GDataToFileError(gdata_error)); | |
| 343 } | |
| 344 | |
| 345 // Implementation of GetResourceEntry. | |
| 346 void FakeFileSystem::GetResourceEntryAfterGetAboutResource( | |
| 347 const GetResourceEntryCallback& callback, | |
| 348 google_apis::DriveApiErrorCode gdata_error, | |
| 349 scoped_ptr<google_apis::AboutResource> about_resource) { | |
| 350 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 351 | |
| 352 FileError error = GDataToFileError(gdata_error); | |
| 353 if (error != FILE_ERROR_OK) { | |
| 354 callback.Run(error, scoped_ptr<ResourceEntry>()); | |
| 355 return; | |
| 356 } | |
| 357 | |
| 358 DCHECK(about_resource); | |
| 359 scoped_ptr<ResourceEntry> root(new ResourceEntry); | |
| 360 root->mutable_file_info()->set_is_directory(true); | |
| 361 root->set_resource_id(about_resource->root_folder_id()); | |
| 362 root->set_title(util::kDriveMyDriveRootDirName); | |
| 363 callback.Run(error, root.Pass()); | |
| 364 } | |
| 365 | |
| 366 void FakeFileSystem::GetResourceEntryAfterGetParentEntryInfo( | |
| 367 const base::FilePath& base_name, | |
| 368 const GetResourceEntryCallback& callback, | |
| 369 FileError error, | |
| 370 scoped_ptr<ResourceEntry> parent_entry) { | |
| 371 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 372 | |
| 373 if (error != FILE_ERROR_OK) { | |
| 374 callback.Run(error, scoped_ptr<ResourceEntry>()); | |
| 375 return; | |
| 376 } | |
| 377 | |
| 378 DCHECK(parent_entry); | |
| 379 drive_service_->GetFileListInDirectory( | |
| 380 parent_entry->resource_id(), | |
| 381 base::Bind( | |
| 382 &FakeFileSystem::GetResourceEntryAfterGetFileList, | |
| 383 weak_ptr_factory_.GetWeakPtr(), base_name, callback)); | |
| 384 } | |
| 385 | |
| 386 void FakeFileSystem::GetResourceEntryAfterGetFileList( | |
| 387 const base::FilePath& base_name, | |
| 388 const GetResourceEntryCallback& callback, | |
| 389 google_apis::DriveApiErrorCode gdata_error, | |
| 390 scoped_ptr<google_apis::FileList> file_list) { | |
| 391 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 392 | |
| 393 FileError error = GDataToFileError(gdata_error); | |
| 394 if (error != FILE_ERROR_OK) { | |
| 395 callback.Run(error, scoped_ptr<ResourceEntry>()); | |
| 396 return; | |
| 397 } | |
| 398 | |
| 399 DCHECK(file_list); | |
| 400 const ScopedVector<google_apis::FileResource>& entries = file_list->items(); | |
| 401 for (size_t i = 0; i < entries.size(); ++i) { | |
| 402 scoped_ptr<ResourceEntry> entry(new ResourceEntry); | |
| 403 std::string parent_resource_id; | |
| 404 bool converted = ConvertFileResourceToResourceEntry( | |
| 405 *entries[i], entry.get(), &parent_resource_id); | |
| 406 DCHECK(converted); | |
| 407 entry->set_parent_local_id(parent_resource_id); | |
| 408 | |
| 409 if (entry->base_name() == base_name.AsUTF8Unsafe()) { | |
| 410 // Found the target entry. | |
| 411 callback.Run(FILE_ERROR_OK, entry.Pass()); | |
| 412 return; | |
| 413 } | |
| 414 } | |
| 415 | |
| 416 callback.Run(FILE_ERROR_NOT_FOUND, scoped_ptr<ResourceEntry>()); | |
| 417 } | |
| 418 | |
| 419 } // namespace test_util | |
| 420 } // namespace drive | |
| OLD | NEW |