| 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/get_updates_processor.h" | 5 #include "components/sync/engine_impl/get_updates_processor.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <utility> |
| 10 | 11 |
| 11 #include "base/trace_event/trace_event.h" | 12 #include "base/trace_event/trace_event.h" |
| 12 #include "components/sync/engine/events/get_updates_response_event.h" | 13 #include "components/sync/engine/events/get_updates_response_event.h" |
| 13 #include "components/sync/engine_impl/cycle/status_controller.h" | 14 #include "components/sync/engine_impl/cycle/status_controller.h" |
| 14 #include "components/sync/engine_impl/cycle/sync_cycle.h" | 15 #include "components/sync/engine_impl/cycle/sync_cycle.h" |
| 15 #include "components/sync/engine_impl/get_updates_delegate.h" | 16 #include "components/sync/engine_impl/get_updates_delegate.h" |
| 16 #include "components/sync/engine_impl/syncer_proto_util.h" | 17 #include "components/sync/engine_impl/syncer_proto_util.h" |
| 17 #include "components/sync/engine_impl/update_handler.h" | 18 #include "components/sync/engine_impl/update_handler.h" |
| 18 #include "components/sync/protocol/sync.pb.h" | 19 #include "components/sync/protocol/sync.pb.h" |
| 19 #include "components/sync/syncable/directory.h" | 20 #include "components/sync/syncable/directory.h" |
| 20 #include "components/sync/syncable/nigori_handler.h" | 21 #include "components/sync/syncable/nigori_handler.h" |
| 21 #include "components/sync/syncable/syncable_read_transaction.h" | 22 #include "components/sync/syncable/syncable_read_transaction.h" |
| 22 | 23 |
| 23 typedef std::vector<const sync_pb::SyncEntity*> SyncEntityList; | |
| 24 typedef std::map<syncer::ModelType, SyncEntityList> TypeSyncEntityMap; | |
| 25 | |
| 26 namespace syncer { | 24 namespace syncer { |
| 27 | 25 |
| 26 namespace { |
| 27 |
| 28 typedef std::vector<const sync_pb::SyncEntity*> SyncEntityList; |
| 29 typedef std::map<ModelType, SyncEntityList> TypeSyncEntityMap; |
| 28 typedef std::map<ModelType, size_t> TypeToIndexMap; | 30 typedef std::map<ModelType, size_t> TypeToIndexMap; |
| 29 | 31 |
| 30 namespace { | |
| 31 | |
| 32 bool ShouldRequestEncryptionKey(SyncCycleContext* context) { | 32 bool ShouldRequestEncryptionKey(SyncCycleContext* context) { |
| 33 syncable::Directory* dir = context->directory(); | 33 syncable::Directory* dir = context->directory(); |
| 34 syncable::ReadTransaction trans(FROM_HERE, dir); | 34 syncable::ReadTransaction trans(FROM_HERE, dir); |
| 35 syncable::NigoriHandler* nigori_handler = dir->GetNigoriHandler(); | 35 syncable::NigoriHandler* nigori_handler = dir->GetNigoriHandler(); |
| 36 return nigori_handler->NeedKeystoreKey(&trans); | 36 return nigori_handler->NeedKeystoreKey(&trans); |
| 37 } | 37 } |
| 38 | 38 |
| 39 SyncerError HandleGetEncryptionKeyResponse( | 39 SyncerError HandleGetEncryptionKeyResponse( |
| 40 const sync_pb::ClientToServerResponse& update_response, | 40 const sync_pb::ClientToServerResponse& update_response, |
| 41 syncable::Directory* dir) { | 41 syncable::Directory* dir) { |
| (...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 ModelTypeSet request_types, | 280 ModelTypeSet request_types, |
| 281 StatusController* status) { | 281 StatusController* status) { |
| 282 status->increment_num_updates_downloaded_by(gu_response.entries_size()); | 282 status->increment_num_updates_downloaded_by(gu_response.entries_size()); |
| 283 | 283 |
| 284 // The changes remaining field is used to prevent the client from looping. If | 284 // The changes remaining field is used to prevent the client from looping. If |
| 285 // that field is being set incorrectly, we're in big trouble. | 285 // that field is being set incorrectly, we're in big trouble. |
| 286 if (!gu_response.has_changes_remaining()) { | 286 if (!gu_response.has_changes_remaining()) { |
| 287 return SERVER_RESPONSE_VALIDATION_FAILED; | 287 return SERVER_RESPONSE_VALIDATION_FAILED; |
| 288 } | 288 } |
| 289 | 289 |
| 290 syncer::SyncerError result = | 290 SyncerError result = |
| 291 ProcessGetUpdatesResponse(request_types, gu_response, status); | 291 ProcessGetUpdatesResponse(request_types, gu_response, status); |
| 292 if (result != syncer::SYNCER_OK) | 292 if (result != SYNCER_OK) |
| 293 return result; | 293 return result; |
| 294 | 294 |
| 295 if (gu_response.changes_remaining() == 0) { | 295 if (gu_response.changes_remaining() == 0) { |
| 296 return SYNCER_OK; | 296 return SYNCER_OK; |
| 297 } else { | 297 } else { |
| 298 return SERVER_MORE_TO_DOWNLOAD; | 298 return SERVER_MORE_TO_DOWNLOAD; |
| 299 } | 299 } |
| 300 } | 300 } |
| 301 | 301 |
| 302 syncer::SyncerError GetUpdatesProcessor::ProcessGetUpdatesResponse( | 302 SyncerError GetUpdatesProcessor::ProcessGetUpdatesResponse( |
| 303 ModelTypeSet gu_types, | 303 ModelTypeSet gu_types, |
| 304 const sync_pb::GetUpdatesResponse& gu_response, | 304 const sync_pb::GetUpdatesResponse& gu_response, |
| 305 StatusController* status_controller) { | 305 StatusController* status_controller) { |
| 306 TypeSyncEntityMap updates_by_type; | 306 TypeSyncEntityMap updates_by_type; |
| 307 PartitionUpdatesByType(gu_response, gu_types, &updates_by_type); | 307 PartitionUpdatesByType(gu_response, gu_types, &updates_by_type); |
| 308 DCHECK_EQ(gu_types.Size(), updates_by_type.size()); | 308 DCHECK_EQ(gu_types.Size(), updates_by_type.size()); |
| 309 | 309 |
| 310 TypeToIndexMap progress_index_by_type; | 310 TypeToIndexMap progress_index_by_type; |
| 311 PartitionProgressMarkersByType(gu_response, gu_types, | 311 PartitionProgressMarkersByType(gu_response, gu_types, |
| 312 &progress_index_by_type); | 312 &progress_index_by_type); |
| 313 if (gu_types.Size() != progress_index_by_type.size()) { | 313 if (gu_types.Size() != progress_index_by_type.size()) { |
| 314 NOTREACHED() << "Missing progress markers in GetUpdates response."; | 314 NOTREACHED() << "Missing progress markers in GetUpdates response."; |
| 315 return syncer::SERVER_RESPONSE_VALIDATION_FAILED; | 315 return SERVER_RESPONSE_VALIDATION_FAILED; |
| 316 } | 316 } |
| 317 | 317 |
| 318 TypeToIndexMap context_by_type; | 318 TypeToIndexMap context_by_type; |
| 319 PartitionContextMutationsByType(gu_response, gu_types, &context_by_type); | 319 PartitionContextMutationsByType(gu_response, gu_types, &context_by_type); |
| 320 | 320 |
| 321 // Iterate over these maps in parallel, processing updates for each type. | 321 // Iterate over these maps in parallel, processing updates for each type. |
| 322 TypeToIndexMap::iterator progress_marker_iter = | 322 TypeToIndexMap::iterator progress_marker_iter = |
| 323 progress_index_by_type.begin(); | 323 progress_index_by_type.begin(); |
| 324 TypeSyncEntityMap::iterator updates_iter = updates_by_type.begin(); | 324 TypeSyncEntityMap::iterator updates_iter = updates_by_type.begin(); |
| 325 for (; (progress_marker_iter != progress_index_by_type.end() && | 325 for (; (progress_marker_iter != progress_index_by_type.end() && |
| 326 updates_iter != updates_by_type.end()); | 326 updates_iter != updates_by_type.end()); |
| 327 ++progress_marker_iter, ++updates_iter) { | 327 ++progress_marker_iter, ++updates_iter) { |
| 328 DCHECK_EQ(progress_marker_iter->first, updates_iter->first); | 328 DCHECK_EQ(progress_marker_iter->first, updates_iter->first); |
| 329 ModelType type = progress_marker_iter->first; | 329 ModelType type = progress_marker_iter->first; |
| 330 | 330 |
| 331 UpdateHandlerMap::iterator update_handler_iter = | 331 UpdateHandlerMap::iterator update_handler_iter = |
| 332 update_handler_map_->find(type); | 332 update_handler_map_->find(type); |
| 333 | 333 |
| 334 sync_pb::DataTypeContext context; | 334 sync_pb::DataTypeContext context; |
| 335 TypeToIndexMap::iterator context_iter = context_by_type.find(type); | 335 TypeToIndexMap::iterator context_iter = context_by_type.find(type); |
| 336 if (context_iter != context_by_type.end()) | 336 if (context_iter != context_by_type.end()) |
| 337 context.CopyFrom(gu_response.context_mutations(context_iter->second)); | 337 context.CopyFrom(gu_response.context_mutations(context_iter->second)); |
| 338 | 338 |
| 339 if (update_handler_iter != update_handler_map_->end()) { | 339 if (update_handler_iter != update_handler_map_->end()) { |
| 340 syncer::SyncerError result = | 340 SyncerError result = |
| 341 update_handler_iter->second->ProcessGetUpdatesResponse( | 341 update_handler_iter->second->ProcessGetUpdatesResponse( |
| 342 gu_response.new_progress_marker(progress_marker_iter->second), | 342 gu_response.new_progress_marker(progress_marker_iter->second), |
| 343 context, updates_iter->second, status_controller); | 343 context, updates_iter->second, status_controller); |
| 344 if (result != syncer::SYNCER_OK) | 344 if (result != SYNCER_OK) |
| 345 return result; | 345 return result; |
| 346 } else { | 346 } else { |
| 347 DLOG(WARNING) << "Ignoring received updates of a type we can't handle. " | 347 DLOG(WARNING) << "Ignoring received updates of a type we can't handle. " |
| 348 << "Type is: " << ModelTypeToString(type); | 348 << "Type is: " << ModelTypeToString(type); |
| 349 continue; | 349 continue; |
| 350 } | 350 } |
| 351 } | 351 } |
| 352 DCHECK(progress_marker_iter == progress_index_by_type.end() && | 352 DCHECK(progress_marker_iter == progress_index_by_type.end() && |
| 353 updates_iter == updates_by_type.end()); | 353 updates_iter == updates_by_type.end()); |
| 354 | 354 |
| 355 return syncer::SYNCER_OK; | 355 return SYNCER_OK; |
| 356 } | 356 } |
| 357 | 357 |
| 358 void GetUpdatesProcessor::ApplyUpdates(ModelTypeSet gu_types, | 358 void GetUpdatesProcessor::ApplyUpdates(ModelTypeSet gu_types, |
| 359 StatusController* status_controller) { | 359 StatusController* status_controller) { |
| 360 status_controller->set_get_updates_request_types(gu_types); | 360 status_controller->set_get_updates_request_types(gu_types); |
| 361 delegate_.ApplyUpdates(gu_types, status_controller, update_handler_map_); | 361 delegate_.ApplyUpdates(gu_types, status_controller, update_handler_map_); |
| 362 } | 362 } |
| 363 | 363 |
| 364 void GetUpdatesProcessor::CopyClientDebugInfo( | 364 void GetUpdatesProcessor::CopyClientDebugInfo( |
| 365 DebugInfoGetter* debug_info_getter, | 365 DebugInfoGetter* debug_info_getter, |
| 366 sync_pb::DebugInfo* debug_info) { | 366 sync_pb::DebugInfo* debug_info) { |
| 367 DVLOG(1) << "Copying client debug info to send."; | 367 DVLOG(1) << "Copying client debug info to send."; |
| 368 debug_info_getter->GetDebugInfo(debug_info); | 368 debug_info_getter->GetDebugInfo(debug_info); |
| 369 } | 369 } |
| 370 | 370 |
| 371 } // namespace syncer | 371 } // namespace syncer |
| OLD | NEW |