OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "components/sync/engine_impl/model_type_worker.h" | 5 #include "components/sync/engine_impl/model_type_worker.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/format_macros.h" | 13 #include "base/format_macros.h" |
14 #include "base/guid.h" | 14 #include "base/guid.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
17 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
18 #include "components/sync/base/time.h" | 18 #include "components/sync/base/time.h" |
19 #include "components/sync/core/model_type_processor.h" | 19 #include "components/sync/core/model_type_processor.h" |
20 #include "components/sync/engine_impl/commit_contribution.h" | 20 #include "components/sync/engine_impl/commit_contribution.h" |
21 #include "components/sync/engine_impl/non_blocking_type_commit_contribution.h" | 21 #include "components/sync/engine_impl/non_blocking_type_commit_contribution.h" |
22 #include "components/sync/engine_impl/worker_entity_tracker.h" | 22 #include "components/sync/engine_impl/worker_entity_tracker.h" |
23 #include "components/sync/syncable/syncable_util.h" | 23 #include "components/sync/syncable/syncable_util.h" |
24 | 24 |
25 namespace syncer_v2 { | 25 namespace syncer { |
26 | |
27 using syncer::CommitContribution; | |
28 using syncer::Cryptographer; | |
29 using syncer::ModelType; | |
30 using syncer::NudgeHandler; | |
31 using syncer::SyncerError; | |
32 | 26 |
33 ModelTypeWorker::ModelTypeWorker( | 27 ModelTypeWorker::ModelTypeWorker( |
34 ModelType type, | 28 ModelType type, |
35 const sync_pb::DataTypeState& initial_state, | 29 const sync_pb::DataTypeState& initial_state, |
36 std::unique_ptr<Cryptographer> cryptographer, | 30 std::unique_ptr<Cryptographer> cryptographer, |
37 NudgeHandler* nudge_handler, | 31 NudgeHandler* nudge_handler, |
38 std::unique_ptr<ModelTypeProcessor> model_type_processor) | 32 std::unique_ptr<ModelTypeProcessor> model_type_processor) |
39 : type_(type), | 33 : type_(type), |
40 data_type_state_(initial_state), | 34 data_type_state_(initial_state), |
41 model_type_processor_(std::move(model_type_processor)), | 35 model_type_processor_(std::move(model_type_processor)), |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 void ModelTypeWorker::GetDataTypeContext( | 82 void ModelTypeWorker::GetDataTypeContext( |
89 sync_pb::DataTypeContext* context) const { | 83 sync_pb::DataTypeContext* context) const { |
90 DCHECK(thread_checker_.CalledOnValidThread()); | 84 DCHECK(thread_checker_.CalledOnValidThread()); |
91 context->CopyFrom(data_type_state_.type_context()); | 85 context->CopyFrom(data_type_state_.type_context()); |
92 } | 86 } |
93 | 87 |
94 SyncerError ModelTypeWorker::ProcessGetUpdatesResponse( | 88 SyncerError ModelTypeWorker::ProcessGetUpdatesResponse( |
95 const sync_pb::DataTypeProgressMarker& progress_marker, | 89 const sync_pb::DataTypeProgressMarker& progress_marker, |
96 const sync_pb::DataTypeContext& mutated_context, | 90 const sync_pb::DataTypeContext& mutated_context, |
97 const SyncEntityList& applicable_updates, | 91 const SyncEntityList& applicable_updates, |
98 syncer::StatusController* status) { | 92 StatusController* status) { |
99 DCHECK(thread_checker_.CalledOnValidThread()); | 93 DCHECK(thread_checker_.CalledOnValidThread()); |
100 | 94 |
101 // TODO(rlarocque): Handle data type context conflicts. | 95 // TODO(rlarocque): Handle data type context conflicts. |
102 *data_type_state_.mutable_type_context() = mutated_context; | 96 *data_type_state_.mutable_type_context() = mutated_context; |
103 *data_type_state_.mutable_progress_marker() = progress_marker; | 97 *data_type_state_.mutable_progress_marker() = progress_marker; |
104 | 98 |
105 for (const sync_pb::SyncEntity* update_entity : applicable_updates) { | 99 for (const sync_pb::SyncEntity* update_entity : applicable_updates) { |
106 // Skip updates for permanent folders. | 100 // Skip updates for permanent folders. |
107 // TODO(crbug.com/516866): might need to handle this for hierarchical types. | 101 // TODO(crbug.com/516866): might need to handle this for hierarchical types. |
108 if (!update_entity->server_defined_unique_tag().empty()) | 102 if (!update_entity->server_defined_unique_tag().empty()) |
109 continue; | 103 continue; |
110 | 104 |
111 // Normal updates are handled here. | 105 // Normal updates are handled here. |
112 const std::string& client_tag_hash = | 106 const std::string& client_tag_hash = |
113 update_entity->client_defined_unique_tag(); | 107 update_entity->client_defined_unique_tag(); |
114 | 108 |
115 // TODO(crbug.com/516866): this wouldn't be true for bookmarks. | 109 // TODO(crbug.com/516866): this wouldn't be true for bookmarks. |
116 DCHECK(!client_tag_hash.empty()); | 110 DCHECK(!client_tag_hash.empty()); |
117 | 111 |
118 // Prepare the message for the model thread. | 112 // Prepare the message for the model thread. |
119 EntityData data; | 113 EntityData data; |
120 data.id = update_entity->id_string(); | 114 data.id = update_entity->id_string(); |
121 data.client_tag_hash = client_tag_hash; | 115 data.client_tag_hash = client_tag_hash; |
122 data.creation_time = syncer::ProtoTimeToTime(update_entity->ctime()); | 116 data.creation_time = ProtoTimeToTime(update_entity->ctime()); |
123 data.modification_time = syncer::ProtoTimeToTime(update_entity->mtime()); | 117 data.modification_time = ProtoTimeToTime(update_entity->mtime()); |
124 data.non_unique_name = update_entity->name(); | 118 data.non_unique_name = update_entity->name(); |
125 | 119 |
126 UpdateResponseData response_data; | 120 UpdateResponseData response_data; |
127 response_data.response_version = update_entity->version(); | 121 response_data.response_version = update_entity->version(); |
128 | 122 |
129 WorkerEntityTracker* entity = GetOrCreateEntityTracker(data); | 123 WorkerEntityTracker* entity = GetOrCreateEntityTracker(data); |
130 | 124 |
131 // Deleted entities must use the default instance of EntitySpecifics in | 125 // Deleted entities must use the default instance of EntitySpecifics in |
132 // order for EntityData to correctly reflect that they are deleted. | 126 // order for EntityData to correctly reflect that they are deleted. |
133 const sync_pb::EntitySpecifics& specifics = | 127 const sync_pb::EntitySpecifics& specifics = |
(...skipping 17 matching lines...) Expand all Loading... |
151 pending_updates_.push_back(response_data); | 145 pending_updates_.push_back(response_data); |
152 } | 146 } |
153 } else { | 147 } else { |
154 // Can't decrypt right now. Ask the entity tracker to handle it. | 148 // Can't decrypt right now. Ask the entity tracker to handle it. |
155 data.specifics = specifics; | 149 data.specifics = specifics; |
156 response_data.entity = data.PassToPtr(); | 150 response_data.entity = data.PassToPtr(); |
157 entity->ReceiveEncryptedUpdate(response_data); | 151 entity->ReceiveEncryptedUpdate(response_data); |
158 } | 152 } |
159 } | 153 } |
160 | 154 |
161 return syncer::SYNCER_OK; | 155 return SYNCER_OK; |
162 } | 156 } |
163 | 157 |
164 void ModelTypeWorker::ApplyUpdates(syncer::StatusController* status) { | 158 void ModelTypeWorker::ApplyUpdates(StatusController* status) { |
165 DCHECK(thread_checker_.CalledOnValidThread()); | 159 DCHECK(thread_checker_.CalledOnValidThread()); |
166 // This should only ever be called after one PassiveApplyUpdates. | 160 // This should only ever be called after one PassiveApplyUpdates. |
167 DCHECK(data_type_state_.initial_sync_done()); | 161 DCHECK(data_type_state_.initial_sync_done()); |
168 // Download cycle is done, pass all updates to the processor. | 162 // Download cycle is done, pass all updates to the processor. |
169 ApplyPendingUpdates(); | 163 ApplyPendingUpdates(); |
170 } | 164 } |
171 | 165 |
172 void ModelTypeWorker::PassiveApplyUpdates(syncer::StatusController* status) { | 166 void ModelTypeWorker::PassiveApplyUpdates(StatusController* status) { |
173 DCHECK(thread_checker_.CalledOnValidThread()); | 167 DCHECK(thread_checker_.CalledOnValidThread()); |
174 // This should only be called at the end of the very first download cycle. | 168 // This should only be called at the end of the very first download cycle. |
175 DCHECK(!data_type_state_.initial_sync_done()); | 169 DCHECK(!data_type_state_.initial_sync_done()); |
176 // Indicate to the processor that the initial download is done. The initial | 170 // Indicate to the processor that the initial download is done. The initial |
177 // sync technically isn't done yet but by the time this value is persisted to | 171 // sync technically isn't done yet but by the time this value is persisted to |
178 // disk on the model thread it will be. | 172 // disk on the model thread it will be. |
179 data_type_state_.set_initial_sync_done(true); | 173 data_type_state_.set_initial_sync_done(true); |
180 ApplyPendingUpdates(); | 174 ApplyPendingUpdates(); |
181 } | 175 } |
182 | 176 |
183 void ModelTypeWorker::ApplyPendingUpdates() { | 177 void ModelTypeWorker::ApplyPendingUpdates() { |
184 DVLOG(1) << ModelTypeToString(type_) << ": " | 178 DVLOG(1) << ModelTypeToString(type_) << ": " |
185 << base::StringPrintf("Delivering %" PRIuS " applicable updates.", | 179 << base::StringPrintf("Delivering %" PRIuS " applicable updates.", |
186 pending_updates_.size()); | 180 pending_updates_.size()); |
187 model_type_processor_->OnUpdateReceived(data_type_state_, pending_updates_); | 181 model_type_processor_->OnUpdateReceived(data_type_state_, pending_updates_); |
188 pending_updates_.clear(); | 182 pending_updates_.clear(); |
189 } | 183 } |
190 | 184 |
191 void ModelTypeWorker::EnqueueForCommit(const CommitRequestDataList& list) { | 185 void ModelTypeWorker::EnqueueForCommit(const CommitRequestDataList& list) { |
192 DCHECK(thread_checker_.CalledOnValidThread()); | 186 DCHECK(thread_checker_.CalledOnValidThread()); |
193 DCHECK(IsTypeInitialized()) | 187 DCHECK(IsTypeInitialized()) |
194 << "Asked to commit items before type was initialized. " | 188 << "Asked to commit items before type was initialized. " |
195 << "ModelType is: " << ModelTypeToString(type_); | 189 << "ModelType is: " << ModelTypeToString(type_); |
196 | 190 |
197 for (const CommitRequestData& commit : list) { | 191 for (const CommitRequestData& commit : list) { |
198 const EntityData& data = commit.entity.value(); | 192 const EntityData& data = commit.entity.value(); |
199 if (!data.is_deleted()) { | 193 if (!data.is_deleted()) { |
200 DCHECK_EQ(type_, syncer::GetModelTypeFromSpecifics(data.specifics)); | 194 DCHECK_EQ(type_, GetModelTypeFromSpecifics(data.specifics)); |
201 } | 195 } |
202 GetOrCreateEntityTracker(data)->RequestCommit(commit); | 196 GetOrCreateEntityTracker(data)->RequestCommit(commit); |
203 } | 197 } |
204 | 198 |
205 if (CanCommitItems()) | 199 if (CanCommitItems()) |
206 nudge_handler_->NudgeForCommit(type_); | 200 nudge_handler_->NudgeForCommit(type_); |
207 } | 201 } |
208 | 202 |
209 // CommitContributor implementation. | 203 // CommitContributor implementation. |
210 std::unique_ptr<CommitContribution> ModelTypeWorker::GetContribution( | 204 std::unique_ptr<CommitContribution> ModelTypeWorker::GetContribution( |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
411 entities_[data.client_tag_hash] = std::move(entity); | 405 entities_[data.client_tag_hash] = std::move(entity); |
412 return entity_ptr; | 406 return entity_ptr; |
413 } | 407 } |
414 | 408 |
415 WorkerEntityTracker* ModelTypeWorker::GetOrCreateEntityTracker( | 409 WorkerEntityTracker* ModelTypeWorker::GetOrCreateEntityTracker( |
416 const EntityData& data) { | 410 const EntityData& data) { |
417 WorkerEntityTracker* entity = GetEntityTracker(data.client_tag_hash); | 411 WorkerEntityTracker* entity = GetEntityTracker(data.client_tag_hash); |
418 return entity ? entity : CreateEntityTracker(data); | 412 return entity ? entity : CreateEntityTracker(data); |
419 } | 413 } |
420 | 414 |
421 } // namespace syncer_v2 | 415 } // namespace syncer |
OLD | NEW |