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.
5 #include "sync/engine/sync_directory_update_handler.h"
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"
17 using syncable::SYNCER;
19 SyncDirectoryUpdateHandler::SyncDirectoryUpdateHandler(
20 syncable::Directory* dir,
22 scoped_refptr<ModelSafeWorker> worker)
27 SyncDirectoryUpdateHandler::~SyncDirectoryUpdateHandler() {}
29 void SyncDirectoryUpdateHandler::GetDownloadProgress(
30 sync_pb::DataTypeProgressMarker* progress_marker) const {
31 dir_->GetDownloadProgress(type_, progress_marker);
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);
43 void SyncDirectoryUpdateHandler::ApplyUpdates(
44 sessions::StatusController* status) {
45 if (!IsApplyUpdatesRequired()) {
49 // This will invoke handlers that belong to the model and its thread, so we
50 // switch to the appropriate thread before we start this work.
51 WorkCallback c = base::Bind(
52 &SyncDirectoryUpdateHandler::ApplyUpdatesImpl,
53 // We wait until the callback is executed. We can safely use Unretained.
54 base::Unretained(this),
55 base::Unretained(status));
56 worker_->DoWorkAndWaitUntilDone(c);
59 void SyncDirectoryUpdateHandler::PassiveApplyUpdates(
60 sessions::StatusController* status) {
61 if (!IsApplyUpdatesRequired()) {
65 // Just do the work here instead of deferring to another thread.
66 ApplyUpdatesImpl(status);
69 SyncerError SyncDirectoryUpdateHandler::ApplyUpdatesImpl(
70 sessions::StatusController* status) {
71 syncable::WriteTransaction trans(FROM_HERE, syncable::SYNCER, dir_);
73 std::vector<int64> handles;
74 dir_->GetUnappliedUpdateMetaHandles(
76 FullModelTypeSet(type_),
79 // First set of update application passes.
80 UpdateApplicator applicator(dir_->GetCryptographer(&trans));
81 applicator.AttemptApplications(&trans, handles);
82 status->increment_num_updates_applied_by(applicator.updates_applied());
83 status->increment_num_hierarchy_conflicts_by(
84 applicator.hierarchy_conflicts());
85 status->increment_num_encryption_conflicts_by(
86 applicator.encryption_conflicts());
88 if (applicator.simple_conflict_ids().size() != 0) {
89 // Resolve the simple conflicts we just detected.
90 ConflictResolver resolver;
91 resolver.ResolveConflicts(&trans,
92 dir_->GetCryptographer(&trans),
93 applicator.simple_conflict_ids(),
96 // Conflict resolution sometimes results in more updates to apply.
98 dir_->GetUnappliedUpdateMetaHandles(
100 FullModelTypeSet(type_),
103 UpdateApplicator conflict_applicator(dir_->GetCryptographer(&trans));
104 conflict_applicator.AttemptApplications(&trans, handles);
106 // We count the number of updates from both applicator passes.
107 status->increment_num_updates_applied_by(
108 conflict_applicator.updates_applied());
110 // Encryption conflicts should remain unchanged by the resolution of simple
111 // conflicts. Those can only be solved by updating our nigori key bag.
112 DCHECK_EQ(conflict_applicator.encryption_conflicts(),
113 applicator.encryption_conflicts());
115 // Hierarchy conflicts should also remain unchanged, for reasons that are
116 // more subtle. Hierarchy conflicts exist when the application of a pending
117 // update from the server would make the local folder hierarchy
118 // inconsistent. The resolution of simple conflicts could never affect the
119 // hierarchy conflicting item directly, because hierarchy conflicts are not
120 // processed by the conflict resolver. It could, in theory, modify the
121 // local hierarchy on which hierarchy conflict detection depends. However,
122 // the conflict resolution algorithm currently in use does not allow this.
123 DCHECK_EQ(conflict_applicator.hierarchy_conflicts(),
124 applicator.hierarchy_conflicts());
126 // There should be no simple conflicts remaining. We know this because the
127 // resolver should have resolved all the conflicts we detected last time
128 // and, by the two previous assertions, that no conflicts have been
129 // downgraded from encryption or hierarchy down to simple.
130 DCHECK(conflict_applicator.simple_conflict_ids().empty());
136 bool SyncDirectoryUpdateHandler::IsApplyUpdatesRequired() {
137 if (IsControlType(type_)) {
138 return false; // We don't process control types here.
141 return dir_->TypeHasUnappliedUpdates(type_);
144 void SyncDirectoryUpdateHandler::UpdateSyncEntities(
145 syncable::ModelNeutralWriteTransaction* trans,
146 const SyncEntityList& applicable_updates,
147 sessions::StatusController* status) {
148 ProcessDownloadedUpdates(dir_, trans, type_, applicable_updates, status);
151 void SyncDirectoryUpdateHandler::UpdateProgressMarker(
152 const sync_pb::DataTypeProgressMarker& progress_marker) {
153 int field_number = progress_marker.data_type_id();
154 ModelType model_type = GetModelTypeFromSpecificsFieldNumber(field_number);
155 if (!IsRealDataType(model_type) || type_ != model_type) {
157 << "Update handler of type " << ModelTypeToString(type_)
158 << " asked to process progress marker with invalid type "
161 dir_->SetDownloadProgress(type_, progress_marker);
164 } // namespace syncer