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