Get rid of a couple references to IDR_WARNING
[chromium-blink-merge.git] / sync / engine / directory_update_handler.cc
blobadcece7baf8294db03d533eb79d2c11bc13594ab
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"
16 namespace syncer {
18 using syncable::SYNCER;
20 DirectoryUpdateHandler::DirectoryUpdateHandler(
21 syncable::Directory* dir,
22 ModelType type,
23 scoped_refptr<ModelSafeWorker> worker,
24 DirectoryTypeDebugInfoEmitter* debug_info_emitter)
25 : dir_(dir),
26 type_(type),
27 worker_(worker),
28 debug_info_emitter_(debug_info_emitter) {}
30 DirectoryUpdateHandler::~DirectoryUpdateHandler() {}
32 void DirectoryUpdateHandler::GetDownloadProgress(
33 sync_pb::DataTypeProgressMarker* progress_marker) const {
34 dir_->GetDownloadProgress(type_, progress_marker);
37 void DirectoryUpdateHandler::GetDataTypeContext(
38 sync_pb::DataTypeContext* context) const {
39 syncable::ModelNeutralWriteTransaction trans(FROM_HERE, SYNCER, dir_);
40 dir_->GetDataTypeContext(&trans, type_, context);
43 SyncerError DirectoryUpdateHandler::ProcessGetUpdatesResponse(
44 const sync_pb::DataTypeProgressMarker& progress_marker,
45 const sync_pb::DataTypeContext& mutated_context,
46 const SyncEntityList& applicable_updates,
47 sessions::StatusController* status) {
48 syncable::ModelNeutralWriteTransaction trans(FROM_HERE, SYNCER, dir_);
49 if (mutated_context.has_context()) {
50 sync_pb::DataTypeContext local_context;
51 dir_->GetDataTypeContext(&trans, type_, &local_context);
53 // Only update the local context if it is still relevant. If the local
54 // version is higher, it means a local change happened while the mutation
55 // was in flight, and the local context takes priority.
56 if (mutated_context.version() >= local_context.version() &&
57 local_context.context() != mutated_context.context()) {
58 dir_->SetDataTypeContext(&trans, type_, mutated_context);
59 // TODO(zea): trigger the datatype's UpdateDataTypeContext method.
60 } else if (mutated_context.version() < local_context.version()) {
61 // A GetUpdates using the old context was in progress when the context was
62 // set. Fail this get updates cycle, to force a retry.
63 DVLOG(1) << "GU Context conflict detected, forcing GU retry.";
64 debug_info_emitter_->EmitUpdateCountersUpdate();
65 return DATATYPE_TRIGGERED_RETRY;
69 // Auto-create permanent folder for the type if the progress marker
70 // changes from empty to non-empty.
71 if (IsTypeWithClientGeneratedRoot(type_) &&
72 dir_->HasEmptyDownloadProgress(type_) &&
73 IsValidProgressMarker(progress_marker)) {
74 CreateTypeRoot(&trans);
77 UpdateSyncEntities(&trans, applicable_updates, status);
79 if (IsValidProgressMarker(progress_marker)) {
80 ExpireEntriesIfNeeded(&trans, progress_marker);
81 UpdateProgressMarker(progress_marker);
84 debug_info_emitter_->EmitUpdateCountersUpdate();
85 return SYNCER_OK;
88 void DirectoryUpdateHandler::CreateTypeRoot(
89 syncable::ModelNeutralWriteTransaction* trans) {
90 syncable::ModelNeutralMutableEntry entry(
91 trans, syncable::CREATE_NEW_TYPE_ROOT, type_);
92 if (!entry.good()) {
93 // This will fail only if matching entry already exists, for example
94 // if the type gets disabled and its progress marker gets cleared,
95 // then the type gets re-enabled again.
96 DVLOG(1) << "Type root folder " << ModelTypeToRootTag(type_)
97 << " already exists.";
98 return;
101 entry.PutServerIsDir(true);
102 entry.PutUniqueServerTag(ModelTypeToRootTag(type_));
105 void DirectoryUpdateHandler::ApplyUpdates(sessions::StatusController* status) {
106 if (!IsApplyUpdatesRequired()) {
107 return;
110 // This will invoke handlers that belong to the model and its thread, so we
111 // switch to the appropriate thread before we start this work.
112 WorkCallback c = base::Bind(
113 &DirectoryUpdateHandler::ApplyUpdatesImpl,
114 // We wait until the callback is executed. We can safely use Unretained.
115 base::Unretained(this),
116 base::Unretained(status));
117 worker_->DoWorkAndWaitUntilDone(c);
119 debug_info_emitter_->EmitUpdateCountersUpdate();
120 debug_info_emitter_->EmitStatusCountersUpdate();
123 void DirectoryUpdateHandler::PassiveApplyUpdates(
124 sessions::StatusController* status) {
125 if (!IsApplyUpdatesRequired()) {
126 return;
129 // Just do the work here instead of deferring to another thread.
130 ApplyUpdatesImpl(status);
132 debug_info_emitter_->EmitUpdateCountersUpdate();
133 debug_info_emitter_->EmitStatusCountersUpdate();
136 SyncerError DirectoryUpdateHandler::ApplyUpdatesImpl(
137 sessions::StatusController* status) {
138 syncable::WriteTransaction trans(FROM_HERE, syncable::SYNCER, dir_);
140 std::vector<int64> handles;
141 dir_->GetUnappliedUpdateMetaHandles(
142 &trans,
143 FullModelTypeSet(type_),
144 &handles);
146 // First set of update application passes.
147 UpdateApplicator applicator(dir_->GetCryptographer(&trans));
148 applicator.AttemptApplications(&trans, handles);
150 // The old StatusController counters.
151 status->increment_num_updates_applied_by(applicator.updates_applied());
152 status->increment_num_hierarchy_conflicts_by(
153 applicator.hierarchy_conflicts());
154 status->increment_num_encryption_conflicts_by(
155 applicator.encryption_conflicts());
157 // The new UpdateCounter counters.
158 UpdateCounters* counters = debug_info_emitter_->GetMutableUpdateCounters();
159 counters->num_updates_applied += applicator.updates_applied();
160 counters->num_hierarchy_conflict_application_failures =
161 applicator.hierarchy_conflicts();
162 counters->num_encryption_conflict_application_failures +=
163 applicator.encryption_conflicts();
165 if (applicator.simple_conflict_ids().size() != 0) {
166 // Resolve the simple conflicts we just detected.
167 ConflictResolver resolver;
168 resolver.ResolveConflicts(&trans,
169 dir_->GetCryptographer(&trans),
170 applicator.simple_conflict_ids(),
171 status,
172 counters);
174 // Conflict resolution sometimes results in more updates to apply.
175 handles.clear();
176 dir_->GetUnappliedUpdateMetaHandles(
177 &trans,
178 FullModelTypeSet(type_),
179 &handles);
181 UpdateApplicator conflict_applicator(dir_->GetCryptographer(&trans));
182 conflict_applicator.AttemptApplications(&trans, handles);
184 // We count the number of updates from both applicator passes.
185 status->increment_num_updates_applied_by(
186 conflict_applicator.updates_applied());
187 counters->num_updates_applied += conflict_applicator.updates_applied();
189 // Encryption conflicts should remain unchanged by the resolution of simple
190 // conflicts. Those can only be solved by updating our nigori key bag.
191 DCHECK_EQ(conflict_applicator.encryption_conflicts(),
192 applicator.encryption_conflicts());
194 // Hierarchy conflicts should also remain unchanged, for reasons that are
195 // more subtle. Hierarchy conflicts exist when the application of a pending
196 // update from the server would make the local folder hierarchy
197 // inconsistent. The resolution of simple conflicts could never affect the
198 // hierarchy conflicting item directly, because hierarchy conflicts are not
199 // processed by the conflict resolver. It could, in theory, modify the
200 // local hierarchy on which hierarchy conflict detection depends. However,
201 // the conflict resolution algorithm currently in use does not allow this.
202 DCHECK_EQ(conflict_applicator.hierarchy_conflicts(),
203 applicator.hierarchy_conflicts());
205 // There should be no simple conflicts remaining. We know this because the
206 // resolver should have resolved all the conflicts we detected last time
207 // and, by the two previous assertions, that no conflicts have been
208 // downgraded from encryption or hierarchy down to simple.
209 DCHECK(conflict_applicator.simple_conflict_ids().empty());
212 return SYNCER_OK;
215 bool DirectoryUpdateHandler::IsApplyUpdatesRequired() {
216 if (IsControlType(type_)) {
217 return false; // We don't process control types here.
220 return dir_->TypeHasUnappliedUpdates(type_);
223 void DirectoryUpdateHandler::UpdateSyncEntities(
224 syncable::ModelNeutralWriteTransaction* trans,
225 const SyncEntityList& applicable_updates,
226 sessions::StatusController* status) {
227 UpdateCounters* counters = debug_info_emitter_->GetMutableUpdateCounters();
228 counters->num_updates_received += applicable_updates.size();
229 ProcessDownloadedUpdates(dir_, trans, type_,
230 applicable_updates, status, counters);
233 bool DirectoryUpdateHandler::IsValidProgressMarker(
234 const sync_pb::DataTypeProgressMarker& progress_marker) const {
235 if (progress_marker.token().empty()) {
236 return false;
238 int field_number = progress_marker.data_type_id();
239 ModelType model_type = GetModelTypeFromSpecificsFieldNumber(field_number);
240 if (!IsRealDataType(model_type) || type_ != model_type) {
241 NOTREACHED()
242 << "Update handler of type " << ModelTypeToString(type_)
243 << " asked to process progress marker with invalid type "
244 << field_number;
245 return false;
247 return true;
250 void DirectoryUpdateHandler::UpdateProgressMarker(
251 const sync_pb::DataTypeProgressMarker& progress_marker) {
252 if (progress_marker.has_gc_directive() || !cached_gc_directive_) {
253 dir_->SetDownloadProgress(type_, progress_marker);
254 } else {
255 sync_pb::DataTypeProgressMarker merged_marker = progress_marker;
256 merged_marker.mutable_gc_directive()->CopyFrom(*cached_gc_directive_);
257 dir_->SetDownloadProgress(type_, merged_marker);
261 void DirectoryUpdateHandler::ExpireEntriesIfNeeded(
262 syncable::ModelNeutralWriteTransaction* trans,
263 const sync_pb::DataTypeProgressMarker& progress_marker) {
264 if (!cached_gc_directive_) {
265 sync_pb::DataTypeProgressMarker current_marker;
266 GetDownloadProgress(&current_marker);
267 if (current_marker.has_gc_directive()) {
268 cached_gc_directive_.reset(new sync_pb::GarbageCollectionDirective(
269 current_marker.gc_directive()));
273 if (!progress_marker.has_gc_directive())
274 return;
276 const sync_pb::GarbageCollectionDirective& new_gc_directive =
277 progress_marker.gc_directive();
279 if (new_gc_directive.has_version_watermark() &&
280 (!cached_gc_directive_ ||
281 cached_gc_directive_->version_watermark() <
282 new_gc_directive.version_watermark())) {
283 ExpireEntriesByVersion(dir_, trans, type_,
284 new_gc_directive.version_watermark());
287 cached_gc_directive_.reset(
288 new sync_pb::GarbageCollectionDirective(new_gc_directive));
291 } // namespace syncer