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