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. |
| 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. We can safely use Unretained. |
| 56 base::Unretained(this), |
| 57 base::Unretained(status)); |
| 58 worker_->DoWorkAndWaitUntilDone(c); |
| 59 } |
| 60 |
| 61 SyncerError SyncDirectoryUpdateHandler::ApplyUpdatesImpl( |
| 62 sessions::StatusController* status) { |
| 63 syncable::WriteTransaction trans(FROM_HERE, syncable::SYNCER, dir_); |
| 64 |
| 65 std::vector<int64> handles; |
| 66 dir_->GetUnappliedUpdateMetaHandles( |
| 67 &trans, |
| 68 FullModelTypeSet(type_), |
| 69 &handles); |
| 70 |
| 71 // First set of update application passes. |
| 72 UpdateApplicator applicator(dir_->GetCryptographer(&trans)); |
| 73 applicator.AttemptApplications(&trans, handles); |
| 74 status->increment_num_updates_applied_by(applicator.updates_applied()); |
| 75 status->increment_num_hierarchy_conflicts_by( |
| 76 applicator.hierarchy_conflicts()); |
| 77 status->increment_num_encryption_conflicts_by( |
| 78 applicator.encryption_conflicts()); |
| 79 |
| 80 if (applicator.simple_conflict_ids().size() != 0) { |
| 81 // Resolve the simple conflicts we just detected. |
| 82 ConflictResolver resolver; |
| 83 resolver.ResolveConflicts(&trans, |
| 84 dir_->GetCryptographer(&trans), |
| 85 applicator.simple_conflict_ids(), |
| 86 status); |
| 87 |
| 88 // Conflict resolution sometimes results in more updates to apply. |
| 89 handles.clear(); |
| 90 dir_->GetUnappliedUpdateMetaHandles( |
| 91 &trans, |
| 92 FullModelTypeSet(type_), |
| 93 &handles); |
| 94 |
| 95 UpdateApplicator conflict_applicator(dir_->GetCryptographer(&trans)); |
| 96 conflict_applicator.AttemptApplications(&trans, handles); |
| 97 |
| 98 // We count the number of updates from both applicator passes. |
| 99 status->increment_num_updates_applied_by( |
| 100 conflict_applicator.updates_applied()); |
| 101 |
| 102 // Encryption conflicts should remain unchanged by the resolution of simple |
| 103 // conflicts. Those can only be solved by updating our nigori key bag. |
| 104 DCHECK_EQ(conflict_applicator.encryption_conflicts(), |
| 105 applicator.encryption_conflicts()); |
| 106 |
| 107 // Hierarchy conflicts should also remain unchanged, for reasons that are |
| 108 // more subtle. Hierarchy conflicts exist when the application of a pending |
| 109 // update from the server would make the local folder hierarchy |
| 110 // inconsistent. The resolution of simple conflicts could never affect the |
| 111 // hierarchy conflicting item directly, because hierarchy conflicts are not |
| 112 // processed by the conflict resolver. It could, in theory, modify the |
| 113 // local hierarchy on which hierarchy conflict detection depends. However, |
| 114 // the conflict resolution algorithm currently in use does not allow this. |
| 115 DCHECK_EQ(conflict_applicator.hierarchy_conflicts(), |
| 116 applicator.hierarchy_conflicts()); |
| 117 |
| 118 // There should be no simple conflicts remaining. We know this because the |
| 119 // resolver should have resolved all the conflicts we detected last time |
| 120 // and, by the two previous assertions, that no conflicts have been |
| 121 // downgraded from encryption or hierarchy down to simple. |
| 122 DCHECK(conflict_applicator.simple_conflict_ids().empty()); |
| 123 } |
| 124 |
| 125 return SYNCER_OK; |
| 126 } |
| 127 |
37 void SyncDirectoryUpdateHandler::UpdateSyncEntities( | 128 void SyncDirectoryUpdateHandler::UpdateSyncEntities( |
38 syncable::ModelNeutralWriteTransaction* trans, | 129 syncable::ModelNeutralWriteTransaction* trans, |
39 const SyncEntityList& applicable_updates, | 130 const SyncEntityList& applicable_updates, |
40 sessions::StatusController* status) { | 131 sessions::StatusController* status) { |
41 ProcessDownloadedUpdates(dir_, trans, type_, applicable_updates, status); | 132 ProcessDownloadedUpdates(dir_, trans, type_, applicable_updates, status); |
42 } | 133 } |
43 | 134 |
44 void SyncDirectoryUpdateHandler::UpdateProgressMarker( | 135 void SyncDirectoryUpdateHandler::UpdateProgressMarker( |
45 const sync_pb::DataTypeProgressMarker& progress_marker) { | 136 const sync_pb::DataTypeProgressMarker& progress_marker) { |
46 int field_number = progress_marker.data_type_id(); | 137 int field_number = progress_marker.data_type_id(); |
47 ModelType model_type = GetModelTypeFromSpecificsFieldNumber(field_number); | 138 ModelType model_type = GetModelTypeFromSpecificsFieldNumber(field_number); |
48 if (!IsRealDataType(model_type) || type_ != model_type) { | 139 if (!IsRealDataType(model_type) || type_ != model_type) { |
49 NOTREACHED() | 140 NOTREACHED() |
50 << "Update handler of type " << ModelTypeToString(type_) | 141 << "Update handler of type " << ModelTypeToString(type_) |
51 << " asked to process progress marker with invalid type " | 142 << " asked to process progress marker with invalid type " |
52 << field_number; | 143 << field_number; |
53 } | 144 } |
54 dir_->SetDownloadProgress(type_, progress_marker); | 145 dir_->SetDownloadProgress(type_, progress_marker); |
55 } | 146 } |
56 | 147 |
57 } // namespace syncer | 148 } // namespace syncer |
OLD | NEW |