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