| 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 "sync/engine/model_type_sync_worker_impl.h" | 5 #include "sync/engine/model_type_sync_worker_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/format_macros.h" | 8 #include "base/format_macros.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 const DataTypeState& initial_state, | 23 const DataTypeState& initial_state, |
| 24 const UpdateResponseDataList& saved_pending_updates, | 24 const UpdateResponseDataList& saved_pending_updates, |
| 25 scoped_ptr<Cryptographer> cryptographer, | 25 scoped_ptr<Cryptographer> cryptographer, |
| 26 NudgeHandler* nudge_handler, | 26 NudgeHandler* nudge_handler, |
| 27 scoped_ptr<ModelTypeSyncProxy> type_sync_proxy) | 27 scoped_ptr<ModelTypeSyncProxy> type_sync_proxy) |
| 28 : type_(type), | 28 : type_(type), |
| 29 data_type_state_(initial_state), | 29 data_type_state_(initial_state), |
| 30 type_sync_proxy_(type_sync_proxy.Pass()), | 30 type_sync_proxy_(type_sync_proxy.Pass()), |
| 31 cryptographer_(cryptographer.Pass()), | 31 cryptographer_(cryptographer.Pass()), |
| 32 nudge_handler_(nudge_handler), | 32 nudge_handler_(nudge_handler), |
| 33 entities_deleter_(&entities_), | |
| 34 weak_ptr_factory_(this) { | 33 weak_ptr_factory_(this) { |
| 35 // Request an initial sync if it hasn't been completed yet. | 34 // Request an initial sync if it hasn't been completed yet. |
| 36 if (!data_type_state_.initial_sync_done) { | 35 if (!data_type_state_.initial_sync_done) { |
| 37 nudge_handler_->NudgeForInitialDownload(type_); | 36 nudge_handler_->NudgeForInitialDownload(type_); |
| 38 } | 37 } |
| 39 | 38 |
| 40 for (UpdateResponseDataList::const_iterator it = | 39 for (UpdateResponseDataList::const_iterator it = |
| 41 saved_pending_updates.begin(); | 40 saved_pending_updates.begin(); |
| 42 it != saved_pending_updates.end(); | 41 it != saved_pending_updates.end(); |
| 43 ++it) { | 42 ++it) { |
| 44 EntityTracker* entity_tracker = EntityTracker::FromServerUpdate( | 43 scoped_ptr<EntityTracker> entity_tracker = EntityTracker::FromServerUpdate( |
| 45 it->id, it->client_tag_hash, it->response_version); | 44 it->id, it->client_tag_hash, it->response_version); |
| 46 entity_tracker->ReceivePendingUpdate(*it); | 45 entity_tracker->ReceivePendingUpdate(*it); |
| 47 entities_.insert(std::make_pair(it->client_tag_hash, entity_tracker)); | 46 entities_.insert(it->client_tag_hash, entity_tracker.Pass()); |
| 48 } | 47 } |
| 49 | 48 |
| 50 if (cryptographer_) { | 49 if (cryptographer_) { |
| 51 DVLOG(1) << ModelTypeToString(type_) << ": Starting with encryption key " | 50 DVLOG(1) << ModelTypeToString(type_) << ": Starting with encryption key " |
| 52 << cryptographer_->GetDefaultNigoriKeyName(); | 51 << cryptographer_->GetDefaultNigoriKeyName(); |
| 53 OnCryptographerUpdated(); | 52 OnCryptographerUpdated(); |
| 54 } | 53 } |
| 55 } | 54 } |
| 56 | 55 |
| 57 ModelTypeSyncWorkerImpl::~ModelTypeSyncWorkerImpl() { | 56 ModelTypeSyncWorkerImpl::~ModelTypeSyncWorkerImpl() { |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 data_type_state_.type_root_id = update_entity->id_string(); | 120 data_type_state_.type_root_id = update_entity->id_string(); |
| 122 } else { | 121 } else { |
| 123 // Normal updates are handled here. | 122 // Normal updates are handled here. |
| 124 const std::string& client_tag_hash = | 123 const std::string& client_tag_hash = |
| 125 update_entity->client_defined_unique_tag(); | 124 update_entity->client_defined_unique_tag(); |
| 126 DCHECK(!client_tag_hash.empty()); | 125 DCHECK(!client_tag_hash.empty()); |
| 127 | 126 |
| 128 EntityTracker* entity_tracker = NULL; | 127 EntityTracker* entity_tracker = NULL; |
| 129 EntityMap::const_iterator map_it = entities_.find(client_tag_hash); | 128 EntityMap::const_iterator map_it = entities_.find(client_tag_hash); |
| 130 if (map_it == entities_.end()) { | 129 if (map_it == entities_.end()) { |
| 131 entity_tracker = | 130 scoped_ptr<EntityTracker> scoped_entity_tracker = |
| 132 EntityTracker::FromServerUpdate(update_entity->id_string(), | 131 EntityTracker::FromServerUpdate(update_entity->id_string(), |
| 133 client_tag_hash, | 132 client_tag_hash, |
| 134 update_entity->version()); | 133 update_entity->version()); |
| 135 entities_.insert(std::make_pair(client_tag_hash, entity_tracker)); | 134 entity_tracker = scoped_entity_tracker.get(); |
| 135 entities_.insert(client_tag_hash, scoped_entity_tracker.Pass()); |
| 136 } else { | 136 } else { |
| 137 entity_tracker = map_it->second; | 137 entity_tracker = map_it->second; |
| 138 } | 138 } |
| 139 | 139 |
| 140 // Prepare the message for the model thread. | 140 // Prepare the message for the model thread. |
| 141 UpdateResponseData response_data; | 141 UpdateResponseData response_data; |
| 142 response_data.id = update_entity->id_string(); | 142 response_data.id = update_entity->id_string(); |
| 143 response_data.client_tag_hash = client_tag_hash; | 143 response_data.client_tag_hash = client_tag_hash; |
| 144 response_data.response_version = update_entity->version(); | 144 response_data.response_version = update_entity->version(); |
| 145 response_data.ctime = ProtoTimeToTime(update_entity->ctime()); | 145 response_data.ctime = ProtoTimeToTime(update_entity->ctime()); |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 return scoped_ptr<CommitContribution>(new NonBlockingTypeCommitContribution( | 266 return scoped_ptr<CommitContribution>(new NonBlockingTypeCommitContribution( |
| 267 data_type_state_.type_context, commit_entities, sequence_numbers, this)); | 267 data_type_state_.type_context, commit_entities, sequence_numbers, this)); |
| 268 } | 268 } |
| 269 | 269 |
| 270 void ModelTypeSyncWorkerImpl::StorePendingCommit( | 270 void ModelTypeSyncWorkerImpl::StorePendingCommit( |
| 271 const CommitRequestData& request) { | 271 const CommitRequestData& request) { |
| 272 if (!request.deleted) { | 272 if (!request.deleted) { |
| 273 DCHECK_EQ(type_, GetModelTypeFromSpecifics(request.specifics)); | 273 DCHECK_EQ(type_, GetModelTypeFromSpecifics(request.specifics)); |
| 274 } | 274 } |
| 275 | 275 |
| 276 EntityMap::iterator map_it = entities_.find(request.client_tag_hash); | 276 EntityMap::const_iterator map_it = entities_.find(request.client_tag_hash); |
| 277 if (map_it == entities_.end()) { | 277 if (map_it == entities_.end()) { |
| 278 EntityTracker* entity = | 278 scoped_ptr<EntityTracker> entity = EntityTracker::FromCommitRequest( |
| 279 EntityTracker::FromCommitRequest(request.id, | 279 request.id, request.client_tag_hash, request.sequence_number, |
| 280 request.client_tag_hash, | 280 request.base_version, request.ctime, request.mtime, |
| 281 request.sequence_number, | 281 request.non_unique_name, request.deleted, request.specifics); |
| 282 request.base_version, | 282 entities_.insert(request.client_tag_hash, entity.Pass()); |
| 283 request.ctime, | |
| 284 request.mtime, | |
| 285 request.non_unique_name, | |
| 286 request.deleted, | |
| 287 request.specifics); | |
| 288 entities_.insert(std::make_pair(request.client_tag_hash, entity)); | |
| 289 } else { | 283 } else { |
| 290 EntityTracker* entity = map_it->second; | 284 EntityTracker* entity = map_it->second; |
| 291 entity->RequestCommit(request.id, | 285 entity->RequestCommit(request.id, |
| 292 request.client_tag_hash, | 286 request.client_tag_hash, |
| 293 request.sequence_number, | 287 request.sequence_number, |
| 294 request.base_version, | 288 request.base_version, |
| 295 request.ctime, | 289 request.ctime, |
| 296 request.mtime, | 290 request.mtime, |
| 297 request.non_unique_name, | 291 request.non_unique_name, |
| 298 request.deleted, | 292 request.deleted, |
| 299 request.specifics); | 293 request.specifics); |
| 300 } | 294 } |
| 301 } | 295 } |
| 302 | 296 |
| 303 void ModelTypeSyncWorkerImpl::OnCommitResponse( | 297 void ModelTypeSyncWorkerImpl::OnCommitResponse( |
| 304 const CommitResponseDataList& response_list) { | 298 const CommitResponseDataList& response_list) { |
| 305 for (CommitResponseDataList::const_iterator response_it = | 299 for (CommitResponseDataList::const_iterator response_it = |
| 306 response_list.begin(); | 300 response_list.begin(); |
| 307 response_it != response_list.end(); | 301 response_it != response_list.end(); |
| 308 ++response_it) { | 302 ++response_it) { |
| 309 const std::string client_tag_hash = response_it->client_tag_hash; | 303 const std::string client_tag_hash = response_it->client_tag_hash; |
| 310 EntityMap::iterator map_it = entities_.find(client_tag_hash); | 304 EntityMap::const_iterator map_it = entities_.find(client_tag_hash); |
| 311 | 305 |
| 312 // There's no way we could have committed an entry we know nothing about. | 306 // There's no way we could have committed an entry we know nothing about. |
| 313 if (map_it == entities_.end()) { | 307 if (map_it == entities_.end()) { |
| 314 NOTREACHED() << "Received commit response for item unknown to us." | 308 NOTREACHED() << "Received commit response for item unknown to us." |
| 315 << " Model type: " << ModelTypeToString(type_) | 309 << " Model type: " << ModelTypeToString(type_) |
| 316 << " ID: " << response_it->id; | 310 << " ID: " << response_it->id; |
| 317 continue; | 311 continue; |
| 318 } | 312 } |
| 319 | 313 |
| 320 EntityTracker* entity = map_it->second; | 314 EntityTracker* entity = map_it->second; |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 449 return false; | 443 return false; |
| 450 } | 444 } |
| 451 if (!out->ParseFromString(plaintext)) { | 445 if (!out->ParseFromString(plaintext)) { |
| 452 LOG(ERROR) << "Failed to parse decrypted entity"; | 446 LOG(ERROR) << "Failed to parse decrypted entity"; |
| 453 return false; | 447 return false; |
| 454 } | 448 } |
| 455 return true; | 449 return true; |
| 456 } | 450 } |
| 457 | 451 |
| 458 } // namespace syncer | 452 } // namespace syncer |
| OLD | NEW |