Roll src/third_party/WebKit 57aef96:a1089e6 (svn 201978:201979)
[chromium-blink-merge.git] / sync / engine / directory_update_handler.cc
blob5b2bac854a9b5795501cc096cf926dfd67bf1cde
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
3 // found in the LICENSE file.
5 #include "sync/engine/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/directory_type_debug_info_emitter.h"
11 #include "sync/syncable/directory.h"
12 #include "sync/syncable/model_neutral_mutable_entry.h"
13 #include "sync/syncable/syncable_model_neutral_write_transaction.h"
14 #include "sync/syncable/syncable_write_transaction.h"
15 #include "sync/util/data_type_histogram.h"
17 namespace syncer {
19 using syncable::SYNCER;
21 DirectoryUpdateHandler::DirectoryUpdateHandler(
22 syncable::Directory* dir,
23 ModelType type,
24 scoped_refptr<ModelSafeWorker> worker,
25 DirectoryTypeDebugInfoEmitter* debug_info_emitter)
26 : dir_(dir),
27 type_(type),
28 worker_(worker),
29 debug_info_emitter_(debug_info_emitter) {}
31 DirectoryUpdateHandler::~DirectoryUpdateHandler() {}
33 void DirectoryUpdateHandler::GetDownloadProgress(
34 sync_pb::DataTypeProgressMarker* progress_marker) const {
35 dir_->GetDownloadProgress(type_, progress_marker);
38 void DirectoryUpdateHandler::GetDataTypeContext(
39 sync_pb::DataTypeContext* context) const {
40 syncable::ModelNeutralWriteTransaction trans(FROM_HERE, SYNCER, dir_);
41 dir_->GetDataTypeContext(&trans, type_, context);
44 SyncerError DirectoryUpdateHandler::ProcessGetUpdatesResponse(
45 const sync_pb::DataTypeProgressMarker& progress_marker,
46 const sync_pb::DataTypeContext& mutated_context,
47 const SyncEntityList& applicable_updates,
48 sessions::StatusController* status) {
49 syncable::ModelNeutralWriteTransaction trans(FROM_HERE, SYNCER, dir_);
50 if (progress_marker.ByteSize() > 0) {
51 SyncRecordDatatypeBin("DataUse.Sync.ProgressMarker.Bytes",
52 ModelTypeToHistogramInt(type_),
53 progress_marker.ByteSize());
55 if (mutated_context.has_context()) {
56 sync_pb::DataTypeContext local_context;
57 dir_->GetDataTypeContext(&trans, type_, &local_context);
59 // Only update the local context if it is still relevant. If the local
60 // version is higher, it means a local change happened while the mutation
61 // was in flight, and the local context takes priority.
62 if (mutated_context.version() >= local_context.version() &&
63 local_context.context() != mutated_context.context()) {
64 dir_->SetDataTypeContext(&trans, type_, mutated_context);
65 // TODO(zea): trigger the datatype's UpdateDataTypeContext method.
66 } else if (mutated_context.version() < local_context.version()) {
67 // A GetUpdates using the old context was in progress when the context was
68 // set. Fail this get updates cycle, to force a retry.
69 DVLOG(1) << "GU Context conflict detected, forcing GU retry.";
70 debug_info_emitter_->EmitUpdateCountersUpdate();
71 return DATATYPE_TRIGGERED_RETRY;
75 // Auto-create permanent folder for the type if the progress marker
76 // changes from empty to non-empty.
77 if (IsTypeWithClientGeneratedRoot(type_) &&
78 dir_->HasEmptyDownloadProgress(type_) &&
79 IsValidProgressMarker(progress_marker)) {
80 CreateTypeRoot(&trans);
83 UpdateSyncEntities(&trans, applicable_updates, status);
85 if (IsValidProgressMarker(progress_marker)) {
86 ExpireEntriesIfNeeded(&trans, progress_marker);
87 UpdateProgressMarker(progress_marker);
90 debug_info_emitter_->EmitUpdateCountersUpdate();
91 return SYNCER_OK;
94 void DirectoryUpdateHandler::CreateTypeRoot(
95 syncable::ModelNeutralWriteTransaction* trans) {
96 syncable::ModelNeutralMutableEntry entry(
97 trans, syncable::CREATE_NEW_TYPE_ROOT, type_);
98 if (!entry.good()) {
99 // This will fail only if matching entry already exists, for example
100 // if the type gets disabled and its progress marker gets cleared,
101 // then the type gets re-enabled again.
102 DVLOG(1) << "Type root folder " << ModelTypeToRootTag(type_)
103 << " already exists.";
104 return;
107 entry.PutServerIsDir(true);
108 entry.PutUniqueServerTag(ModelTypeToRootTag(type_));
111 void DirectoryUpdateHandler::ApplyUpdates(sessions::StatusController* status) {
112 if (!IsApplyUpdatesRequired()) {
113 return;
116 // This will invoke handlers that belong to the model and its thread, so we
117 // switch to the appropriate thread before we start this work.
118 WorkCallback c = base::Bind(
119 &DirectoryUpdateHandler::ApplyUpdatesImpl,
120 // We wait until the callback is executed. We can safely use Unretained.
121 base::Unretained(this),
122 base::Unretained(status));
123 worker_->DoWorkAndWaitUntilDone(c);
125 debug_info_emitter_->EmitUpdateCountersUpdate();
126 debug_info_emitter_->EmitStatusCountersUpdate();
129 void DirectoryUpdateHandler::PassiveApplyUpdates(
130 sessions::StatusController* status) {
131 if (!IsApplyUpdatesRequired()) {
132 return;
135 // Just do the work here instead of deferring to another thread.
136 ApplyUpdatesImpl(status);
138 debug_info_emitter_->EmitUpdateCountersUpdate();
139 debug_info_emitter_->EmitStatusCountersUpdate();
142 SyncerError DirectoryUpdateHandler::ApplyUpdatesImpl(
143 sessions::StatusController* status) {
144 syncable::WriteTransaction trans(FROM_HERE, syncable::SYNCER, dir_);
146 std::vector<int64> handles;
147 dir_->GetUnappliedUpdateMetaHandles(
148 &trans,
149 FullModelTypeSet(type_),
150 &handles);
152 // First set of update application passes.
153 UpdateApplicator applicator(dir_->GetCryptographer(&trans));
154 applicator.AttemptApplications(&trans, handles);
156 // The old StatusController counters.
157 status->increment_num_updates_applied_by(applicator.updates_applied());
158 status->increment_num_hierarchy_conflicts_by(
159 applicator.hierarchy_conflicts());
160 status->increment_num_encryption_conflicts_by(
161 applicator.encryption_conflicts());
163 // The new UpdateCounter counters.
164 UpdateCounters* counters = debug_info_emitter_->GetMutableUpdateCounters();
165 counters->num_updates_applied += applicator.updates_applied();
166 counters->num_hierarchy_conflict_application_failures =
167 applicator.hierarchy_conflicts();
168 counters->num_encryption_conflict_application_failures +=
169 applicator.encryption_conflicts();
171 if (applicator.simple_conflict_ids().size() != 0) {
172 // Resolve the simple conflicts we just detected.
173 ConflictResolver resolver;
174 resolver.ResolveConflicts(&trans,
175 dir_->GetCryptographer(&trans),
176 applicator.simple_conflict_ids(),
177 status,
178 counters);
180 // Conflict resolution sometimes results in more updates to apply.
181 handles.clear();
182 dir_->GetUnappliedUpdateMetaHandles(
183 &trans,
184 FullModelTypeSet(type_),
185 &handles);
187 UpdateApplicator conflict_applicator(dir_->GetCryptographer(&trans));
188 conflict_applicator.AttemptApplications(&trans, handles);
190 // We count the number of updates from both applicator passes.
191 status->increment_num_updates_applied_by(
192 conflict_applicator.updates_applied());
193 counters->num_updates_applied += conflict_applicator.updates_applied();
195 // Encryption conflicts should remain unchanged by the resolution of simple
196 // conflicts. Those can only be solved by updating our nigori key bag.
197 DCHECK_EQ(conflict_applicator.encryption_conflicts(),
198 applicator.encryption_conflicts());
200 // Hierarchy conflicts should also remain unchanged, for reasons that are
201 // more subtle. Hierarchy conflicts exist when the application of a pending
202 // update from the server would make the local folder hierarchy
203 // inconsistent. The resolution of simple conflicts could never affect the
204 // hierarchy conflicting item directly, because hierarchy conflicts are not
205 // processed by the conflict resolver. It could, in theory, modify the
206 // local hierarchy on which hierarchy conflict detection depends. However,
207 // the conflict resolution algorithm currently in use does not allow this.
208 DCHECK_EQ(conflict_applicator.hierarchy_conflicts(),
209 applicator.hierarchy_conflicts());
211 // There should be no simple conflicts remaining. We know this because the
212 // resolver should have resolved all the conflicts we detected last time
213 // and, by the two previous assertions, that no conflicts have been
214 // downgraded from encryption or hierarchy down to simple.
215 DCHECK(conflict_applicator.simple_conflict_ids().empty());
218 return SYNCER_OK;
221 bool DirectoryUpdateHandler::IsApplyUpdatesRequired() {
222 if (IsControlType(type_)) {
223 return false; // We don't process control types here.
226 return dir_->TypeHasUnappliedUpdates(type_);
229 void DirectoryUpdateHandler::UpdateSyncEntities(
230 syncable::ModelNeutralWriteTransaction* trans,
231 const SyncEntityList& applicable_updates,
232 sessions::StatusController* status) {
233 UpdateCounters* counters = debug_info_emitter_->GetMutableUpdateCounters();
234 counters->num_updates_received += applicable_updates.size();
235 ProcessDownloadedUpdates(dir_, trans, type_,
236 applicable_updates, status, counters);
239 bool DirectoryUpdateHandler::IsValidProgressMarker(
240 const sync_pb::DataTypeProgressMarker& progress_marker) const {
241 if (progress_marker.token().empty()) {
242 return false;
244 int field_number = progress_marker.data_type_id();
245 ModelType model_type = GetModelTypeFromSpecificsFieldNumber(field_number);
246 if (!IsRealDataType(model_type) || type_ != model_type) {
247 NOTREACHED()
248 << "Update handler of type " << ModelTypeToString(type_)
249 << " asked to process progress marker with invalid type "
250 << field_number;
251 return false;
253 return true;
256 void DirectoryUpdateHandler::UpdateProgressMarker(
257 const sync_pb::DataTypeProgressMarker& progress_marker) {
258 if (progress_marker.has_gc_directive() || !cached_gc_directive_) {
259 dir_->SetDownloadProgress(type_, progress_marker);
260 } else {
261 sync_pb::DataTypeProgressMarker merged_marker = progress_marker;
262 merged_marker.mutable_gc_directive()->CopyFrom(*cached_gc_directive_);
263 dir_->SetDownloadProgress(type_, merged_marker);
267 void DirectoryUpdateHandler::ExpireEntriesIfNeeded(
268 syncable::ModelNeutralWriteTransaction* trans,
269 const sync_pb::DataTypeProgressMarker& progress_marker) {
270 if (!cached_gc_directive_) {
271 sync_pb::DataTypeProgressMarker current_marker;
272 GetDownloadProgress(&current_marker);
273 if (current_marker.has_gc_directive()) {
274 cached_gc_directive_.reset(new sync_pb::GarbageCollectionDirective(
275 current_marker.gc_directive()));
279 if (!progress_marker.has_gc_directive())
280 return;
282 const sync_pb::GarbageCollectionDirective& new_gc_directive =
283 progress_marker.gc_directive();
285 if (new_gc_directive.has_version_watermark() &&
286 (!cached_gc_directive_ ||
287 cached_gc_directive_->version_watermark() <
288 new_gc_directive.version_watermark())) {
289 ExpireEntriesByVersion(dir_, trans, type_,
290 new_gc_directive.version_watermark());
293 cached_gc_directive_.reset(
294 new sync_pb::GarbageCollectionDirective(new_gc_directive));
297 } // namespace syncer