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