Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/drive/file_system/copy_operation.h" | 5 #include "chrome/browser/chromeos/drive/file_system/copy_operation.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/task_runner_util.h" | 10 #include "base/task_runner_util.h" |
| 11 #include "chrome/browser/chromeos/drive/drive.pb.h" | 11 #include "chrome/browser/chromeos/drive/drive.pb.h" |
| 12 #include "chrome/browser/chromeos/drive/file_cache.h" | 12 #include "chrome/browser/chromeos/drive/file_cache.h" |
| 13 #include "chrome/browser/chromeos/drive/file_change.h" | |
| 13 #include "chrome/browser/chromeos/drive/file_system/create_file_operation.h" | 14 #include "chrome/browser/chromeos/drive/file_system/create_file_operation.h" |
| 14 #include "chrome/browser/chromeos/drive/file_system/operation_observer.h" | 15 #include "chrome/browser/chromeos/drive/file_system/operation_observer.h" |
| 15 #include "chrome/browser/chromeos/drive/file_system_util.h" | 16 #include "chrome/browser/chromeos/drive/file_system_util.h" |
| 16 #include "chrome/browser/chromeos/drive/job_scheduler.h" | 17 #include "chrome/browser/chromeos/drive/job_scheduler.h" |
| 17 #include "chrome/browser/chromeos/drive/resource_entry_conversion.h" | 18 #include "chrome/browser/chromeos/drive/resource_entry_conversion.h" |
| 18 #include "chrome/browser/chromeos/drive/resource_metadata.h" | 19 #include "chrome/browser/chromeos/drive/resource_metadata.h" |
| 19 #include "chrome/browser/drive/drive_api_util.h" | 20 #include "chrome/browser/drive/drive_api_util.h" |
| 20 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 21 #include "google_apis/drive/drive_api_parser.h" | 22 #include "google_apis/drive/drive_api_parser.h" |
| 22 | 23 |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 156 return error; | 157 return error; |
| 157 | 158 |
| 158 return cache->Store(local_id, std::string(), cache_file_path, | 159 return cache->Store(local_id, std::string(), cache_file_path, |
| 159 internal::FileCache::FILE_OPERATION_COPY); | 160 internal::FileCache::FILE_OPERATION_COPY); |
| 160 } | 161 } |
| 161 | 162 |
| 162 // Stores the entry returned from the server and returns its path. | 163 // Stores the entry returned from the server and returns its path. |
| 163 FileError UpdateLocalStateForServerSideOperation( | 164 FileError UpdateLocalStateForServerSideOperation( |
| 164 internal::ResourceMetadata* metadata, | 165 internal::ResourceMetadata* metadata, |
| 165 scoped_ptr<google_apis::FileResource> file_resource, | 166 scoped_ptr<google_apis::FileResource> file_resource, |
| 167 ResourceEntry* resource_entry, | |
|
kinaba
2014/06/23 05:37:43
ResourceEntry* entry
should be ok.
yoshiki
2014/06/24 02:02:22
Done.
| |
| 166 base::FilePath* file_path) { | 168 base::FilePath* file_path) { |
| 167 DCHECK(file_resource); | 169 DCHECK(file_resource); |
| 168 | 170 |
| 169 ResourceEntry entry; | |
| 170 std::string parent_resource_id; | 171 std::string parent_resource_id; |
| 171 if (!ConvertFileResourceToResourceEntry(*file_resource, &entry, | 172 if (!ConvertFileResourceToResourceEntry( |
| 172 &parent_resource_id) || | 173 *file_resource, resource_entry, &parent_resource_id) || |
| 173 parent_resource_id.empty()) | 174 parent_resource_id.empty()) |
| 174 return FILE_ERROR_NOT_A_FILE; | 175 return FILE_ERROR_NOT_A_FILE; |
| 175 | 176 |
| 176 std::string parent_local_id; | 177 std::string parent_local_id; |
| 177 FileError error = metadata->GetIdByResourceId(parent_resource_id, | 178 FileError error = metadata->GetIdByResourceId(parent_resource_id, |
| 178 &parent_local_id); | 179 &parent_local_id); |
| 179 if (error != FILE_ERROR_OK) | 180 if (error != FILE_ERROR_OK) |
| 180 return error; | 181 return error; |
| 181 entry.set_parent_local_id(parent_local_id); | 182 resource_entry->set_parent_local_id(parent_local_id); |
| 182 | 183 |
| 183 std::string local_id; | 184 std::string local_id; |
| 184 error = metadata->AddEntry(entry, &local_id); | 185 error = metadata->AddEntry(*resource_entry, &local_id); |
| 185 // Depending on timing, the metadata may have inserted via change list | 186 // Depending on timing, the metadata may have inserted via change list |
| 186 // already. So, FILE_ERROR_EXISTS is not an error. | 187 // already. So, FILE_ERROR_EXISTS is not an error. |
| 187 if (error == FILE_ERROR_EXISTS) | 188 if (error == FILE_ERROR_EXISTS) |
| 188 error = metadata->GetIdByResourceId(entry.resource_id(), &local_id); | 189 error = |
| 190 metadata->GetIdByResourceId(resource_entry->resource_id(), &local_id); | |
| 189 | 191 |
| 190 if (error != FILE_ERROR_OK) | 192 if (error != FILE_ERROR_OK) |
| 191 return error; | 193 return error; |
| 192 | 194 |
| 193 return metadata->GetFilePath(local_id, file_path); | 195 return metadata->GetFilePath(local_id, file_path); |
| 194 } | 196 } |
| 195 | 197 |
| 196 // Stores the file at |local_file_path| to the cache as a content of entry at | 198 // Stores the file at |local_file_path| to the cache as a content of entry at |
| 197 // |remote_dest_path|, and marks it dirty. | 199 // |remote_dest_path|, and marks it dirty. |
| 198 FileError UpdateLocalStateForScheduleTransfer( | 200 FileError UpdateLocalStateForScheduleTransfer( |
| 199 internal::ResourceMetadata* metadata, | 201 internal::ResourceMetadata* metadata, |
| 200 internal::FileCache* cache, | 202 internal::FileCache* cache, |
| 201 const base::FilePath& local_src_path, | 203 const base::FilePath& local_src_path, |
| 202 const base::FilePath& remote_dest_path, | 204 const base::FilePath& remote_dest_path, |
| 205 ResourceEntry* entry, | |
| 203 std::string* local_id) { | 206 std::string* local_id) { |
| 204 FileError error = metadata->GetIdByPath(remote_dest_path, local_id); | 207 FileError error = metadata->GetIdByPath(remote_dest_path, local_id); |
| 205 if (error != FILE_ERROR_OK) | 208 if (error != FILE_ERROR_OK) |
| 206 return error; | 209 return error; |
| 207 | 210 |
| 208 ResourceEntry entry; | 211 error = metadata->GetResourceEntryById(*local_id, entry); |
| 209 error = metadata->GetResourceEntryById(*local_id, &entry); | |
| 210 if (error != FILE_ERROR_OK) | 212 if (error != FILE_ERROR_OK) |
| 211 return error; | 213 return error; |
| 212 | 214 |
| 213 return cache->Store(*local_id, std::string(), local_src_path, | 215 return cache->Store(*local_id, std::string(), local_src_path, |
| 214 internal::FileCache::FILE_OPERATION_COPY); | 216 internal::FileCache::FILE_OPERATION_COPY); |
| 215 } | 217 } |
| 216 | 218 |
| 217 // Gets the file size of the |local_path|, and the ResourceEntry for the parent | 219 // Gets the file size of the |local_path|, and the ResourceEntry for the parent |
| 218 // of |remote_path| to prepare the necessary information for transfer. | 220 // of |remote_path| to prepare the necessary information for transfer. |
| 219 FileError PrepareTransferFileFromLocalToRemote( | 221 FileError PrepareTransferFileFromLocalToRemote( |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 329 const std::vector<std::string>* updated_local_ids, | 331 const std::vector<std::string>* updated_local_ids, |
| 330 const bool* directory_changed, | 332 const bool* directory_changed, |
| 331 const bool* should_copy_on_server, | 333 const bool* should_copy_on_server, |
| 332 FileError error) { | 334 FileError error) { |
| 333 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 335 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 334 DCHECK(!params->callback.is_null()); | 336 DCHECK(!params->callback.is_null()); |
| 335 | 337 |
| 336 for (size_t i = 0; i < updated_local_ids->size(); ++i) | 338 for (size_t i = 0; i < updated_local_ids->size(); ++i) |
| 337 observer_->OnEntryUpdatedByOperation((*updated_local_ids)[i]); | 339 observer_->OnEntryUpdatedByOperation((*updated_local_ids)[i]); |
| 338 | 340 |
| 339 if (*directory_changed) | 341 if (*directory_changed) { |
| 340 observer_->OnDirectoryChangedByOperation(params->dest_file_path.DirName()); | 342 FileChange changed_file; |
| 343 DCHECK(params->src_entry.file_info().is_directory()); | |
| 344 changed_file.Update(params->dest_file_path, | |
| 345 FileChange::FILE_TYPE_FILE, | |
| 346 FileChange::ADD_OR_UPDATE); | |
| 347 observer_->OnDirectoryChangedByOperation(changed_file); | |
| 348 } | |
| 341 | 349 |
| 342 if (error != FILE_ERROR_OK || !*should_copy_on_server) { | 350 if (error != FILE_ERROR_OK || !*should_copy_on_server) { |
| 343 params->callback.Run(error); | 351 params->callback.Run(error); |
| 344 return; | 352 return; |
| 345 } | 353 } |
| 346 | 354 |
| 347 base::FilePath new_title = params->dest_file_path.BaseName(); | 355 base::FilePath new_title = params->dest_file_path.BaseName(); |
| 348 if (params->src_entry.file_specific_info().is_hosted_document()) { | 356 if (params->src_entry.file_specific_info().is_hosted_document()) { |
| 349 // Drop the document extension, which should not be in the title. | 357 // Drop the document extension, which should not be in the title. |
| 350 // TODO(yoshiki): Remove this code with crbug.com/223304. | 358 // TODO(yoshiki): Remove this code with crbug.com/223304. |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 442 case HAS_PARENT: | 450 case HAS_PARENT: |
| 443 CopyResourceOnServer(params->resource_id, | 451 CopyResourceOnServer(params->resource_id, |
| 444 params->parent_resource_id, | 452 params->parent_resource_id, |
| 445 params->new_title, | 453 params->new_title, |
| 446 base::Time(), | 454 base::Time(), |
| 447 params->callback); | 455 params->callback); |
| 448 break; | 456 break; |
| 449 // When |resource_id| has no parent, we just set the new destination folder | 457 // When |resource_id| has no parent, we just set the new destination folder |
| 450 // as the parent, for sharing the document between the original source. | 458 // as the parent, for sharing the document between the original source. |
| 451 // This reparenting is already done in LocalWorkForTransferJsonGdocFile(). | 459 // This reparenting is already done in LocalWorkForTransferJsonGdocFile(). |
| 452 case IS_ORPHAN: | 460 case IS_ORPHAN: { |
| 453 DCHECK(!params->changed_path.empty()); | 461 DCHECK(!params->changed_path.empty()); |
| 454 observer_->OnEntryUpdatedByOperation(params->local_id); | 462 observer_->OnEntryUpdatedByOperation(params->local_id); |
| 455 observer_->OnDirectoryChangedByOperation(params->changed_path.DirName()); | 463 |
| 464 FileChange changed_file; | |
| 465 changed_file.Update( | |
| 466 params->changed_path, | |
| 467 FileChange::FILE_TYPE_FILE, // This must be a hosted document. | |
| 468 FileChange::ADD_OR_UPDATE); | |
| 469 observer_->OnDirectoryChangedByOperation(changed_file); | |
| 456 params->callback.Run(error); | 470 params->callback.Run(error); |
| 457 break; | 471 break; |
| 472 } | |
| 458 // When the |resource_id| is not in the local metadata, assume it to be a | 473 // When the |resource_id| is not in the local metadata, assume it to be a |
| 459 // document just now shared on the server but not synced locally. | 474 // document just now shared on the server but not synced locally. |
| 460 // Same as the IS_ORPHAN case, we want to deal the case by setting parent, | 475 // Same as the IS_ORPHAN case, we want to deal the case by setting parent, |
| 461 // but this time we need to resort to server side operation. | 476 // but this time we need to resort to server side operation. |
| 462 case NOT_IN_METADATA: | 477 case NOT_IN_METADATA: |
| 463 scheduler_->UpdateResource( | 478 scheduler_->UpdateResource( |
| 464 params->resource_id, | 479 params->resource_id, |
| 465 params->parent_resource_id, | 480 params->parent_resource_id, |
| 466 params->new_title, | 481 params->new_title, |
| 467 base::Time(), | 482 base::Time(), |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 496 scoped_ptr<google_apis::FileResource> entry) { | 511 scoped_ptr<google_apis::FileResource> entry) { |
| 497 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 512 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 498 DCHECK(!callback.is_null()); | 513 DCHECK(!callback.is_null()); |
| 499 | 514 |
| 500 FileError error = GDataToFileError(status); | 515 FileError error = GDataToFileError(status); |
| 501 if (error != FILE_ERROR_OK) { | 516 if (error != FILE_ERROR_OK) { |
| 502 callback.Run(error); | 517 callback.Run(error); |
| 503 return; | 518 return; |
| 504 } | 519 } |
| 505 | 520 |
| 521 ResourceEntry* resource_entry = new ResourceEntry; | |
| 522 | |
| 506 // The copy on the server side is completed successfully. Update the local | 523 // The copy on the server side is completed successfully. Update the local |
| 507 // metadata. | 524 // metadata. |
| 508 base::FilePath* file_path = new base::FilePath; | 525 base::FilePath* file_path = new base::FilePath; |
| 509 base::PostTaskAndReplyWithResult( | 526 base::PostTaskAndReplyWithResult( |
| 510 blocking_task_runner_.get(), | 527 blocking_task_runner_.get(), |
| 511 FROM_HERE, | 528 FROM_HERE, |
| 512 base::Bind(&UpdateLocalStateForServerSideOperation, | 529 base::Bind(&UpdateLocalStateForServerSideOperation, |
| 513 metadata_, base::Passed(&entry), file_path), | 530 metadata_, |
| 531 base::Passed(&entry), | |
| 532 resource_entry, | |
| 533 file_path), | |
| 514 base::Bind(&CopyOperation::UpdateAfterLocalStateUpdate, | 534 base::Bind(&CopyOperation::UpdateAfterLocalStateUpdate, |
| 515 weak_ptr_factory_.GetWeakPtr(), | 535 weak_ptr_factory_.GetWeakPtr(), |
| 516 callback, base::Owned(file_path))); | 536 callback, |
| 537 base::Owned(file_path), | |
| 538 base::Owned(resource_entry))); | |
| 517 } | 539 } |
| 518 | 540 |
| 519 void CopyOperation::UpdateAfterLocalStateUpdate( | 541 void CopyOperation::UpdateAfterLocalStateUpdate( |
| 520 const FileOperationCallback& callback, | 542 const FileOperationCallback& callback, |
| 521 base::FilePath* file_path, | 543 base::FilePath* file_path, |
| 544 const ResourceEntry* entry, | |
| 522 FileError error) { | 545 FileError error) { |
| 523 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 546 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 524 DCHECK(!callback.is_null()); | 547 DCHECK(!callback.is_null()); |
| 525 | 548 |
| 526 if (error == FILE_ERROR_OK) | 549 if (error == FILE_ERROR_OK) { |
| 527 observer_->OnDirectoryChangedByOperation(file_path->DirName()); | 550 FileChange changed_file; |
| 551 changed_file.Update(*file_path, *entry, FileChange::ADD_OR_UPDATE); | |
| 552 observer_->OnDirectoryChangedByOperation(changed_file); | |
| 553 } | |
| 528 callback.Run(error); | 554 callback.Run(error); |
| 529 } | 555 } |
| 530 | 556 |
| 531 void CopyOperation::ScheduleTransferRegularFile( | 557 void CopyOperation::ScheduleTransferRegularFile( |
| 532 const base::FilePath& local_src_path, | 558 const base::FilePath& local_src_path, |
| 533 const base::FilePath& remote_dest_path, | 559 const base::FilePath& remote_dest_path, |
| 534 const FileOperationCallback& callback) { | 560 const FileOperationCallback& callback) { |
| 535 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 561 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 536 DCHECK(!callback.is_null()); | 562 DCHECK(!callback.is_null()); |
| 537 | 563 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 551 FileError error) { | 577 FileError error) { |
| 552 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 578 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 553 DCHECK(!callback.is_null()); | 579 DCHECK(!callback.is_null()); |
| 554 | 580 |
| 555 if (error != FILE_ERROR_OK) { | 581 if (error != FILE_ERROR_OK) { |
| 556 callback.Run(error); | 582 callback.Run(error); |
| 557 return; | 583 return; |
| 558 } | 584 } |
| 559 | 585 |
| 560 std::string* local_id = new std::string; | 586 std::string* local_id = new std::string; |
| 587 ResourceEntry* entry = new ResourceEntry; | |
| 561 base::PostTaskAndReplyWithResult( | 588 base::PostTaskAndReplyWithResult( |
| 562 blocking_task_runner_.get(), | 589 blocking_task_runner_.get(), |
| 563 FROM_HERE, | 590 FROM_HERE, |
| 564 base::Bind( | 591 base::Bind(&UpdateLocalStateForScheduleTransfer, |
| 565 &UpdateLocalStateForScheduleTransfer, | 592 metadata_, |
| 566 metadata_, cache_, local_src_path, remote_dest_path, local_id), | 593 cache_, |
| 594 local_src_path, | |
| 595 remote_dest_path, | |
| 596 entry, | |
| 597 local_id), | |
| 567 base::Bind( | 598 base::Bind( |
| 568 &CopyOperation::ScheduleTransferRegularFileAfterUpdateLocalState, | 599 &CopyOperation::ScheduleTransferRegularFileAfterUpdateLocalState, |
| 569 weak_ptr_factory_.GetWeakPtr(), callback, remote_dest_path, | 600 weak_ptr_factory_.GetWeakPtr(), |
| 601 callback, | |
| 602 remote_dest_path, | |
| 603 base::Owned(entry), | |
| 570 base::Owned(local_id))); | 604 base::Owned(local_id))); |
| 571 } | 605 } |
| 572 | 606 |
| 573 void CopyOperation::ScheduleTransferRegularFileAfterUpdateLocalState( | 607 void CopyOperation::ScheduleTransferRegularFileAfterUpdateLocalState( |
| 574 const FileOperationCallback& callback, | 608 const FileOperationCallback& callback, |
| 575 const base::FilePath& remote_dest_path, | 609 const base::FilePath& remote_dest_path, |
| 610 const ResourceEntry* entry, | |
| 576 std::string* local_id, | 611 std::string* local_id, |
| 577 FileError error) { | 612 FileError error) { |
| 578 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 613 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 579 DCHECK(!callback.is_null()); | 614 DCHECK(!callback.is_null()); |
| 580 | 615 |
| 581 if (error == FILE_ERROR_OK) { | 616 if (error == FILE_ERROR_OK) { |
| 582 observer_->OnDirectoryChangedByOperation(remote_dest_path.DirName()); | 617 FileChange changed_file; |
| 618 changed_file.Update(remote_dest_path, *entry, FileChange::ADD_OR_UPDATE); | |
| 619 observer_->OnDirectoryChangedByOperation(changed_file); | |
| 583 observer_->OnEntryUpdatedByOperation(*local_id); | 620 observer_->OnEntryUpdatedByOperation(*local_id); |
| 584 } | 621 } |
| 585 callback.Run(error); | 622 callback.Run(error); |
| 586 } | 623 } |
| 587 | 624 |
| 588 } // namespace file_system | 625 } // namespace file_system |
| 589 } // namespace drive | 626 } // namespace drive |
| OLD | NEW |