| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/sync_driver/generic_change_processor.h" | 5 #include "components/sync_driver/generic_change_processor.h" |
| 6 | 6 |
| 7 #include "base/location.h" | 7 #include "base/location.h" |
| 8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "components/sync_driver/sync_api_component_factory.h" | 10 #include "components/sync_driver/sync_api_component_factory.h" |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 DCHECK_NE(type_, syncer::UNSPECIFIED); | 105 DCHECK_NE(type_, syncer::UNSPECIFIED); |
| 106 if (attachment_store.get()) { | 106 if (attachment_store.get()) { |
| 107 attachment_service_ = sync_factory->CreateAttachmentService( | 107 attachment_service_ = sync_factory->CreateAttachmentService( |
| 108 attachment_store, *user_share, this); | 108 attachment_store, *user_share, this); |
| 109 attachment_service_weak_ptr_factory_.reset( | 109 attachment_service_weak_ptr_factory_.reset( |
| 110 new base::WeakPtrFactory<syncer::AttachmentService>( | 110 new base::WeakPtrFactory<syncer::AttachmentService>( |
| 111 attachment_service_.get())); | 111 attachment_service_.get())); |
| 112 attachment_service_proxy_.reset(new syncer::AttachmentServiceProxy( | 112 attachment_service_proxy_.reset(new syncer::AttachmentServiceProxy( |
| 113 base::MessageLoopProxy::current(), | 113 base::MessageLoopProxy::current(), |
| 114 attachment_service_weak_ptr_factory_->GetWeakPtr())); | 114 attachment_service_weak_ptr_factory_->GetWeakPtr())); |
| 115 UploadAllAttachmentsNotOnServer(); |
| 115 } else { | 116 } else { |
| 116 attachment_service_proxy_.reset(new syncer::AttachmentServiceProxy( | 117 attachment_service_proxy_.reset(new syncer::AttachmentServiceProxy( |
| 117 base::MessageLoopProxy::current(), | 118 base::MessageLoopProxy::current(), |
| 118 base::WeakPtr<syncer::AttachmentService>())); | 119 base::WeakPtr<syncer::AttachmentService>())); |
| 119 } | 120 } |
| 120 } | 121 } |
| 121 | 122 |
| 122 GenericChangeProcessor::~GenericChangeProcessor() { | 123 GenericChangeProcessor::~GenericChangeProcessor() { |
| 123 DCHECK(CalledOnValidThread()); | 124 DCHECK(CalledOnValidThread()); |
| 124 } | 125 } |
| (...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 401 | 402 |
| 402 } // namespace | 403 } // namespace |
| 403 | 404 |
| 404 syncer::SyncError GenericChangeProcessor::ProcessSyncChanges( | 405 syncer::SyncError GenericChangeProcessor::ProcessSyncChanges( |
| 405 const tracked_objects::Location& from_here, | 406 const tracked_objects::Location& from_here, |
| 406 const syncer::SyncChangeList& list_of_changes) { | 407 const syncer::SyncChangeList& list_of_changes) { |
| 407 DCHECK(CalledOnValidThread()); | 408 DCHECK(CalledOnValidThread()); |
| 408 | 409 |
| 409 // Keep track of brand new attachments so we can persist them on this device | 410 // Keep track of brand new attachments so we can persist them on this device |
| 410 // and upload them to the server. | 411 // and upload them to the server. |
| 411 syncer::AttachmentIdList new_attachments; | 412 syncer::AttachmentIdSet new_attachments; |
| 412 | 413 |
| 413 syncer::WriteTransaction trans(from_here, share_handle()); | 414 syncer::WriteTransaction trans(from_here, share_handle()); |
| 414 | 415 |
| 415 for (syncer::SyncChangeList::const_iterator iter = list_of_changes.begin(); | 416 for (syncer::SyncChangeList::const_iterator iter = list_of_changes.begin(); |
| 416 iter != list_of_changes.end(); | 417 iter != list_of_changes.end(); |
| 417 ++iter) { | 418 ++iter) { |
| 418 const syncer::SyncChange& change = *iter; | 419 const syncer::SyncChange& change = *iter; |
| 419 DCHECK_EQ(change.sync_data().GetDataType(), type_); | 420 DCHECK_EQ(change.sync_data().GetDataType(), type_); |
| 420 std::string type_str = syncer::ModelTypeToString(type_); | 421 std::string type_str = syncer::ModelTypeToString(type_); |
| 421 syncer::WriteNode sync_node(&trans); | 422 syncer::WriteNode sync_node(&trans); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 463 syncer::SyncError error( | 464 syncer::SyncError error( |
| 464 FROM_HERE, | 465 FROM_HERE, |
| 465 syncer::SyncError::DATATYPE_ERROR, | 466 syncer::SyncError::DATATYPE_ERROR, |
| 466 "Datatype performs attachment operation without initializing " | 467 "Datatype performs attachment operation without initializing " |
| 467 "attachment store", | 468 "attachment store", |
| 468 type_); | 469 type_); |
| 469 error_handler()->OnSingleDataTypeUnrecoverableError(error); | 470 error_handler()->OnSingleDataTypeUnrecoverableError(error); |
| 470 NOTREACHED(); | 471 NOTREACHED(); |
| 471 return error; | 472 return error; |
| 472 } | 473 } |
| 473 UploadAttachments(new_attachments); | 474 attachment_service_->UploadAttachments(new_attachments); |
| 474 } | 475 } |
| 475 | 476 |
| 476 return syncer::SyncError(); | 477 return syncer::SyncError(); |
| 477 } | 478 } |
| 478 | 479 |
| 479 // WARNING: this code is sensitive to compiler optimizations. Be careful | 480 // WARNING: this code is sensitive to compiler optimizations. Be careful |
| 480 // modifying any code around an OnSingleDataTypeUnrecoverableError call, else | 481 // modifying any code around an OnSingleDataTypeUnrecoverableError call, else |
| 481 // the compiler attempts to merge it with other calls, losing useful information | 482 // the compiler attempts to merge it with other calls, losing useful information |
| 482 // in breakpad uploads. | 483 // in breakpad uploads. |
| 483 syncer::SyncError GenericChangeProcessor::HandleActionAdd( | 484 syncer::SyncError GenericChangeProcessor::HandleActionAdd( |
| 484 const syncer::SyncChange& change, | 485 const syncer::SyncChange& change, |
| 485 const std::string& type_str, | 486 const std::string& type_str, |
| 486 const syncer::WriteTransaction& trans, | 487 const syncer::WriteTransaction& trans, |
| 487 syncer::WriteNode* sync_node, | 488 syncer::WriteNode* sync_node, |
| 488 syncer::AttachmentIdList* new_attachments) { | 489 syncer::AttachmentIdSet* new_attachments) { |
| 489 // TODO(sync): Handle other types of creation (custom parents, folders, | 490 // TODO(sync): Handle other types of creation (custom parents, folders, |
| 490 // etc.). | 491 // etc.). |
| 491 syncer::ReadNode root_node(&trans); | 492 syncer::ReadNode root_node(&trans); |
| 492 const syncer::SyncDataLocal sync_data_local(change.sync_data()); | 493 const syncer::SyncDataLocal sync_data_local(change.sync_data()); |
| 493 if (root_node.InitTypeRoot(sync_data_local.GetDataType()) != | 494 if (root_node.InitTypeRoot(sync_data_local.GetDataType()) != |
| 494 syncer::BaseNode::INIT_OK) { | 495 syncer::BaseNode::INIT_OK) { |
| 495 syncer::SyncError error(FROM_HERE, | 496 syncer::SyncError error(FROM_HERE, |
| 496 syncer::SyncError::DATATYPE_ERROR, | 497 syncer::SyncError::DATATYPE_ERROR, |
| 497 "Failed to look up root node for type " + type_str, | 498 "Failed to look up root node for type " + type_str, |
| 498 type_); | 499 type_); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 546 } | 547 } |
| 547 } | 548 } |
| 548 } | 549 } |
| 549 sync_node->SetTitle(change.sync_data().GetTitle()); | 550 sync_node->SetTitle(change.sync_data().GetTitle()); |
| 550 SetNodeSpecifics(sync_data_local.GetSpecifics(), sync_node); | 551 SetNodeSpecifics(sync_data_local.GetSpecifics(), sync_node); |
| 551 | 552 |
| 552 syncer::AttachmentIdList attachment_ids = sync_data_local.GetAttachmentIds(); | 553 syncer::AttachmentIdList attachment_ids = sync_data_local.GetAttachmentIds(); |
| 553 SetAttachmentMetadata(attachment_ids, sync_node); | 554 SetAttachmentMetadata(attachment_ids, sync_node); |
| 554 | 555 |
| 555 // Return any newly added attachments. | 556 // Return any newly added attachments. |
| 556 new_attachments->insert( | 557 new_attachments->insert(attachment_ids.begin(), attachment_ids.end()); |
| 557 new_attachments->end(), attachment_ids.begin(), attachment_ids.end()); | |
| 558 if (merge_result_.get()) { | 558 if (merge_result_.get()) { |
| 559 merge_result_->set_num_items_added(merge_result_->num_items_added() + 1); | 559 merge_result_->set_num_items_added(merge_result_->num_items_added() + 1); |
| 560 } | 560 } |
| 561 return syncer::SyncError(); | 561 return syncer::SyncError(); |
| 562 } | 562 } |
| 563 // WARNING: this code is sensitive to compiler optimizations. Be careful | 563 // WARNING: this code is sensitive to compiler optimizations. Be careful |
| 564 // modifying any code around an OnSingleDataTypeUnrecoverableError call, else | 564 // modifying any code around an OnSingleDataTypeUnrecoverableError call, else |
| 565 // the compiler attempts to merge it with other calls, losing useful information | 565 // the compiler attempts to merge it with other calls, losing useful information |
| 566 // in breakpad uploads. | 566 // in breakpad uploads. |
| 567 syncer::SyncError GenericChangeProcessor::HandleActionUpdate( | 567 syncer::SyncError GenericChangeProcessor::HandleActionUpdate( |
| 568 const syncer::SyncChange& change, | 568 const syncer::SyncChange& change, |
| 569 const std::string& type_str, | 569 const std::string& type_str, |
| 570 const syncer::WriteTransaction& trans, | 570 const syncer::WriteTransaction& trans, |
| 571 syncer::WriteNode* sync_node, | 571 syncer::WriteNode* sync_node, |
| 572 syncer::AttachmentIdList* new_attachments) { | 572 syncer::AttachmentIdSet* new_attachments) { |
| 573 // TODO(zea): consider having this logic for all possible changes? | 573 // TODO(zea): consider having this logic for all possible changes? |
| 574 | 574 |
| 575 const syncer::SyncDataLocal sync_data_local(change.sync_data()); | 575 const syncer::SyncDataLocal sync_data_local(change.sync_data()); |
| 576 syncer::BaseNode::InitByLookupResult result = | 576 syncer::BaseNode::InitByLookupResult result = |
| 577 sync_node->InitByClientTagLookup(sync_data_local.GetDataType(), | 577 sync_node->InitByClientTagLookup(sync_data_local.GetDataType(), |
| 578 sync_data_local.GetTag()); | 578 sync_data_local.GetTag()); |
| 579 if (result != syncer::BaseNode::INIT_OK) { | 579 if (result != syncer::BaseNode::INIT_OK) { |
| 580 std::string error_prefix = "Failed to load " + type_str + " node. " + | 580 std::string error_prefix = "Failed to load " + type_str + " node. " + |
| 581 change.location().ToString() + ", "; | 581 change.location().ToString() + ", "; |
| 582 if (result == syncer::BaseNode::INIT_FAILED_PRECONDITION) { | 582 if (result == syncer::BaseNode::INIT_FAILED_PRECONDITION) { |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 648 } | 648 } |
| 649 } | 649 } |
| 650 } | 650 } |
| 651 | 651 |
| 652 sync_node->SetTitle(change.sync_data().GetTitle()); | 652 sync_node->SetTitle(change.sync_data().GetTitle()); |
| 653 SetNodeSpecifics(sync_data_local.GetSpecifics(), sync_node); | 653 SetNodeSpecifics(sync_data_local.GetSpecifics(), sync_node); |
| 654 syncer::AttachmentIdList attachment_ids = sync_data_local.GetAttachmentIds(); | 654 syncer::AttachmentIdList attachment_ids = sync_data_local.GetAttachmentIds(); |
| 655 SetAttachmentMetadata(attachment_ids, sync_node); | 655 SetAttachmentMetadata(attachment_ids, sync_node); |
| 656 | 656 |
| 657 // Return any newly added attachments. | 657 // Return any newly added attachments. |
| 658 new_attachments->insert( | 658 new_attachments->insert(attachment_ids.begin(), attachment_ids.end()); |
| 659 new_attachments->end(), attachment_ids.begin(), attachment_ids.end()); | |
| 660 | 659 |
| 661 if (merge_result_.get()) { | 660 if (merge_result_.get()) { |
| 662 merge_result_->set_num_items_modified(merge_result_->num_items_modified() + | 661 merge_result_->set_num_items_modified(merge_result_->num_items_modified() + |
| 663 1); | 662 1); |
| 664 } | 663 } |
| 665 // TODO(sync): Support updating other parts of the sync node (title, | 664 // TODO(sync): Support updating other parts of the sync node (title, |
| 666 // successor, parent, etc.). | 665 // successor, parent, etc.). |
| 667 return syncer::SyncError(); | 666 return syncer::SyncError(); |
| 668 } | 667 } |
| 669 | 668 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 696 } | 695 } |
| 697 | 696 |
| 698 void GenericChangeProcessor::StartImpl() { | 697 void GenericChangeProcessor::StartImpl() { |
| 699 } | 698 } |
| 700 | 699 |
| 701 syncer::UserShare* GenericChangeProcessor::share_handle() const { | 700 syncer::UserShare* GenericChangeProcessor::share_handle() const { |
| 702 DCHECK(CalledOnValidThread()); | 701 DCHECK(CalledOnValidThread()); |
| 703 return share_handle_; | 702 return share_handle_; |
| 704 } | 703 } |
| 705 | 704 |
| 706 void GenericChangeProcessor::UploadAttachments( | 705 void GenericChangeProcessor::UploadAllAttachmentsNotOnServer() { |
| 707 const syncer::AttachmentIdList& attachment_ids) { | |
| 708 DCHECK(CalledOnValidThread()); | 706 DCHECK(CalledOnValidThread()); |
| 709 DCHECK(attachment_service_.get() != NULL); | 707 DCHECK(attachment_service_.get()); |
| 710 | 708 syncer::AttachmentIdSet id_set; |
| 711 syncer::AttachmentIdSet attachment_id_set; | 709 { |
| 712 attachment_id_set.insert(attachment_ids.begin(), attachment_ids.end()); | 710 syncer::ReadTransaction trans(FROM_HERE, share_handle()); |
| 713 attachment_service_->UploadAttachments(attachment_id_set); | 711 trans.GetAttachmentIdsToUpload(type_, &id_set); |
| 712 } |
| 713 if (!id_set.empty()) { |
| 714 attachment_service_->UploadAttachments(id_set); |
| 715 } |
| 714 } | 716 } |
| 715 | 717 |
| 716 } // namespace sync_driver | 718 } // namespace sync_driver |
| OLD | NEW |