1 // Copyright 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 "sync/engine/sync_directory_update_handler.h" 6 7 #include "sync/engine/conflict_resolver.h" 8 #include "sync/engine/process_updates_util.h" 9 #include "sync/engine/update_applicator.h" 10 #include "sync/sessions/status_controller.h" 11 #include "sync/syncable/directory.h" 12 #include "sync/syncable/syncable_model_neutral_write_transaction.h" 13 #include "sync/syncable/syncable_write_transaction.h" 14 15 namespace syncer { 16 17 using syncable::SYNCER; 18 19 SyncDirectoryUpdateHandler::SyncDirectoryUpdateHandler( 20 syncable::Directory* dir, 21 ModelType type, 22 scoped_refptr<ModelSafeWorker> worker) 23 : dir_(dir), 24 type_(type), 25 worker_(worker) {} 26 27 SyncDirectoryUpdateHandler::~SyncDirectoryUpdateHandler() {} 28 29 void SyncDirectoryUpdateHandler::GetDownloadProgress( 30 sync_pb::DataTypeProgressMarker* progress_marker) const { 31 dir_->GetDownloadProgress(type_, progress_marker); 32 } 33 34 void SyncDirectoryUpdateHandler::ProcessGetUpdatesResponse( 35 const sync_pb::DataTypeProgressMarker& progress_marker, 36 const SyncEntityList& applicable_updates, 37 sessions::StatusController* status) { 38 syncable::ModelNeutralWriteTransaction trans(FROM_HERE, SYNCER, dir_); 39 UpdateSyncEntities(&trans, applicable_updates, status); 40 UpdateProgressMarker(progress_marker); 41 } 42 43 void SyncDirectoryUpdateHandler::ApplyUpdates( 44 sessions::StatusController* status) { 45 if (IsControlType(type_)) { 46 return; // We don't process control types here. 47 } 48 49 if (!dir_->TypeHasUnappliedUpdates(type_)) { 50 return; // No work to do. Skip this type. 51 } 52 53 WorkCallback c = base::Bind( 54 &SyncDirectoryUpdateHandler::ApplyUpdatesImpl, 55 // We wait until the callback is executed. We can safely use Unretained. 56 base::Unretained(this), 57 base::Unretained(status)); 58 worker_->DoWorkAndWaitUntilDone(c); 59 } 60 61 SyncerError SyncDirectoryUpdateHandler::ApplyUpdatesImpl( 62 sessions::StatusController* status) { 63 syncable::WriteTransaction trans(FROM_HERE, syncable::SYNCER, dir_); 64 65 std::vector<int64> handles; 66 dir_->GetUnappliedUpdateMetaHandles( 67 &trans, 68 FullModelTypeSet(type_), 69 &handles); 70 71 // First set of update application passes. 72 UpdateApplicator applicator(dir_->GetCryptographer(&trans)); 73 applicator.AttemptApplications(&trans, handles); 74 status->increment_num_updates_applied_by(applicator.updates_applied()); 75 status->increment_num_hierarchy_conflicts_by( 76 applicator.hierarchy_conflicts()); 77 status->increment_num_encryption_conflicts_by( 78 applicator.encryption_conflicts()); 79 80 if (applicator.simple_conflict_ids().size() != 0) { 81 // Resolve the simple conflicts we just detected. 82 ConflictResolver resolver; 83 resolver.ResolveConflicts(&trans, 84 dir_->GetCryptographer(&trans), 85 applicator.simple_conflict_ids(), 86 status); 87 88 // Conflict resolution sometimes results in more updates to apply. 89 handles.clear(); 90 dir_->GetUnappliedUpdateMetaHandles( 91 &trans, 92 FullModelTypeSet(type_), 93 &handles); 94 95 UpdateApplicator conflict_applicator(dir_->GetCryptographer(&trans)); 96 conflict_applicator.AttemptApplications(&trans, handles); 97 98 // We count the number of updates from both applicator passes. 99 status->increment_num_updates_applied_by( 100 conflict_applicator.updates_applied()); 101 102 // Encryption conflicts should remain unchanged by the resolution of simple 103 // conflicts. Those can only be solved by updating our nigori key bag. 104 DCHECK_EQ(conflict_applicator.encryption_conflicts(), 105 applicator.encryption_conflicts()); 106 107 // Hierarchy conflicts should also remain unchanged, for reasons that are 108 // more subtle. Hierarchy conflicts exist when the application of a pending 109 // update from the server would make the local folder hierarchy 110 // inconsistent. The resolution of simple conflicts could never affect the 111 // hierarchy conflicting item directly, because hierarchy conflicts are not 112 // processed by the conflict resolver. It could, in theory, modify the 113 // local hierarchy on which hierarchy conflict detection depends. However, 114 // the conflict resolution algorithm currently in use does not allow this. 115 DCHECK_EQ(conflict_applicator.hierarchy_conflicts(), 116 applicator.hierarchy_conflicts()); 117 118 // There should be no simple conflicts remaining. We know this because the 119 // resolver should have resolved all the conflicts we detected last time 120 // and, by the two previous assertions, that no conflicts have been 121 // downgraded from encryption or hierarchy down to simple. 122 DCHECK(conflict_applicator.simple_conflict_ids().empty()); 123 } 124 125 return SYNCER_OK; 126 } 127 128 void SyncDirectoryUpdateHandler::UpdateSyncEntities( 129 syncable::ModelNeutralWriteTransaction* trans, 130 const SyncEntityList& applicable_updates, 131 sessions::StatusController* status) { 132 ProcessDownloadedUpdates(dir_, trans, type_, applicable_updates, status); 133 } 134 135 void SyncDirectoryUpdateHandler::UpdateProgressMarker( 136 const sync_pb::DataTypeProgressMarker& progress_marker) { 137 int field_number = progress_marker.data_type_id(); 138 ModelType model_type = GetModelTypeFromSpecificsFieldNumber(field_number); 139 if (!IsRealDataType(model_type) || type_ != model_type) { 140 NOTREACHED() 141 << "Update handler of type " << ModelTypeToString(type_) 142 << " asked to process progress marker with invalid type " 143 << field_number; 144 } 145 dir_->SetDownloadProgress(type_, progress_marker); 146 } 147 148 } // namespace syncer 149