| 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/get_updates_processor.h" | 5 #include "sync/engine/get_updates_processor.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/debug/trace_event.h" | 9 #include "base/debug/trace_event.h" |
| 10 #include "sync/engine/get_updates_delegate.h" | 10 #include "sync/engine/get_updates_delegate.h" |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 << update_response.get_updates().encryption_keys_size() | 53 << update_response.get_updates().encryption_keys_size() |
| 54 << "encryption keys. Nigori keystore key " | 54 << "encryption keys. Nigori keystore key " |
| 55 << (success ? "" : "not ") << "updated."; | 55 << (success ? "" : "not ") << "updated."; |
| 56 return (success ? SYNCER_OK : SERVER_RESPONSE_VALIDATION_FAILED); | 56 return (success ? SYNCER_OK : SERVER_RESPONSE_VALIDATION_FAILED); |
| 57 } | 57 } |
| 58 | 58 |
| 59 // Given a GetUpdates response, iterates over all the returned items and | 59 // Given a GetUpdates response, iterates over all the returned items and |
| 60 // divides them according to their type. Outputs a map from model types to | 60 // divides them according to their type. Outputs a map from model types to |
| 61 // received SyncEntities. The output map will have entries (possibly empty) | 61 // received SyncEntities. The output map will have entries (possibly empty) |
| 62 // for all types in |requested_types|. | 62 // for all types in |requested_types|. |
| 63 void PartitionUpdatesByType( | 63 void PartitionUpdatesByType(const sync_pb::GetUpdatesResponse& gu_response, |
| 64 const sync_pb::GetUpdatesResponse& updates, | 64 ModelTypeSet requested_types, |
| 65 ModelTypeSet requested_types, | 65 TypeSyncEntityMap* updates_by_type) { |
| 66 TypeSyncEntityMap* updates_by_type) { | 66 int update_count = gu_response.entries().size(); |
| 67 int update_count = updates.entries().size(); | |
| 68 for (ModelTypeSet::Iterator it = requested_types.First(); | 67 for (ModelTypeSet::Iterator it = requested_types.First(); |
| 69 it.Good(); it.Inc()) { | 68 it.Good(); it.Inc()) { |
| 70 updates_by_type->insert(std::make_pair(it.Get(), SyncEntityList())); | 69 updates_by_type->insert(std::make_pair(it.Get(), SyncEntityList())); |
| 71 } | 70 } |
| 72 for (int i = 0; i < update_count; ++i) { | 71 for (int i = 0; i < update_count; ++i) { |
| 73 const sync_pb::SyncEntity& update = updates.entries(i); | 72 const sync_pb::SyncEntity& update = gu_response.entries(i); |
| 74 ModelType type = GetModelType(update); | 73 ModelType type = GetModelType(update); |
| 75 if (!IsRealDataType(type)) { | 74 if (!IsRealDataType(type)) { |
| 76 NOTREACHED() << "Received update with invalid type."; | 75 NOTREACHED() << "Received update with invalid type."; |
| 77 continue; | 76 continue; |
| 78 } | 77 } |
| 79 | 78 |
| 80 TypeSyncEntityMap::iterator it = updates_by_type->find(type); | 79 TypeSyncEntityMap::iterator it = updates_by_type->find(type); |
| 81 if (it == updates_by_type->end()) { | 80 if (it == updates_by_type->end()) { |
| 82 NOTREACHED() << "Received update for unexpected type " | 81 NOTREACHED() << "Received update for unexpected type " |
| 83 << ModelTypeToString(type); | 82 << ModelTypeToString(type); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 104 if (!request_types.Has(model_type)) { | 103 if (!request_types.Has(model_type)) { |
| 105 DLOG(WARNING) | 104 DLOG(WARNING) |
| 106 << "Skipping unexpected progress marker for non-enabled type " | 105 << "Skipping unexpected progress marker for non-enabled type " |
| 107 << ModelTypeToString(model_type); | 106 << ModelTypeToString(model_type); |
| 108 continue; | 107 continue; |
| 109 } | 108 } |
| 110 index_map->insert(std::make_pair(model_type, i)); | 109 index_map->insert(std::make_pair(model_type, i)); |
| 111 } | 110 } |
| 112 } | 111 } |
| 113 | 112 |
| 113 void PartitionContextMutationsByType( |
| 114 const sync_pb::GetUpdatesResponse& gu_response, |
| 115 ModelTypeSet request_types, |
| 116 TypeToIndexMap* index_map) { |
| 117 for (int i = 0; i < gu_response.context_mutations_size(); ++i) { |
| 118 int field_number = gu_response.context_mutations(i).data_type_id(); |
| 119 ModelType model_type = GetModelTypeFromSpecificsFieldNumber(field_number); |
| 120 if (!IsRealDataType(model_type)) { |
| 121 DLOG(WARNING) << "Unknown field number " << field_number; |
| 122 continue; |
| 123 } |
| 124 if (!request_types.Has(model_type)) { |
| 125 DLOG(WARNING) |
| 126 << "Skipping unexpected context mutation for non-enabled type " |
| 127 << ModelTypeToString(model_type); |
| 128 continue; |
| 129 } |
| 130 index_map->insert(std::make_pair(model_type, i)); |
| 131 } |
| 132 } |
| 133 |
| 114 // Initializes the parts of the GetUpdatesMessage that depend on shared state, | 134 // Initializes the parts of the GetUpdatesMessage that depend on shared state, |
| 115 // like the ShouldRequestEncryptionKey() status. This is kept separate from the | 135 // like the ShouldRequestEncryptionKey() status. This is kept separate from the |
| 116 // other of the message-building functions to make the rest of the code easier | 136 // other of the message-building functions to make the rest of the code easier |
| 117 // to test. | 137 // to test. |
| 118 void InitDownloadUpdatesContext( | 138 void InitDownloadUpdatesContext( |
| 119 sessions::SyncSession* session, | 139 sessions::SyncSession* session, |
| 120 bool create_mobile_bookmarks_folder, | 140 bool create_mobile_bookmarks_folder, |
| 121 sync_pb::ClientToServerMessage* message) { | 141 sync_pb::ClientToServerMessage* message) { |
| 122 message->set_share(session->context()->account_name()); | 142 message->set_share(session->context()->account_name()); |
| 123 message->set_message_contents(sync_pb::ClientToServerMessage::GET_UPDATES); | 143 message->set_message_contents(sync_pb::ClientToServerMessage::GET_UPDATES); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 sync_pb::GetUpdatesMessage* get_updates = message->mutable_get_updates(); | 191 sync_pb::GetUpdatesMessage* get_updates = message->mutable_get_updates(); |
| 172 | 192 |
| 173 for (ModelTypeSet::Iterator it = gu_types.First(); it.Good(); it.Inc()) { | 193 for (ModelTypeSet::Iterator it = gu_types.First(); it.Good(); it.Inc()) { |
| 174 UpdateHandlerMap::iterator handler_it = update_handler_map_->find(it.Get()); | 194 UpdateHandlerMap::iterator handler_it = update_handler_map_->find(it.Get()); |
| 175 DCHECK(handler_it != update_handler_map_->end()) | 195 DCHECK(handler_it != update_handler_map_->end()) |
| 176 << "Failed to look up handler for " << ModelTypeToString(it.Get()); | 196 << "Failed to look up handler for " << ModelTypeToString(it.Get()); |
| 177 sync_pb::DataTypeProgressMarker* progress_marker = | 197 sync_pb::DataTypeProgressMarker* progress_marker = |
| 178 get_updates->add_from_progress_marker(); | 198 get_updates->add_from_progress_marker(); |
| 179 handler_it->second->GetDownloadProgress(progress_marker); | 199 handler_it->second->GetDownloadProgress(progress_marker); |
| 180 progress_marker->clear_gc_directive(); | 200 progress_marker->clear_gc_directive(); |
| 201 |
| 202 sync_pb::DataTypeContext context; |
| 203 handler_it->second->GetDataTypeContext(&context); |
| 204 if (!context.context().empty()) |
| 205 get_updates->add_client_contexts()->Swap(&context); |
| 181 } | 206 } |
| 207 |
| 182 delegate_.HelpPopulateGuMessage(get_updates); | 208 delegate_.HelpPopulateGuMessage(get_updates); |
| 183 } | 209 } |
| 184 | 210 |
| 185 SyncerError GetUpdatesProcessor::ExecuteDownloadUpdates( | 211 SyncerError GetUpdatesProcessor::ExecuteDownloadUpdates( |
| 186 ModelTypeSet request_types, | 212 ModelTypeSet request_types, |
| 187 sessions::SyncSession* session, | 213 sessions::SyncSession* session, |
| 188 sync_pb::ClientToServerMessage* msg) { | 214 sync_pb::ClientToServerMessage* msg) { |
| 189 sync_pb::ClientToServerResponse update_response; | 215 sync_pb::ClientToServerResponse update_response; |
| 190 sessions::StatusController* status = session->mutable_status_controller(); | 216 sessions::StatusController* status = session->mutable_status_controller(); |
| 191 bool need_encryption_key = ShouldRequestEncryptionKey(session->context()); | 217 bool need_encryption_key = ShouldRequestEncryptionKey(session->context()); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 | 305 |
| 280 TypeToIndexMap progress_index_by_type; | 306 TypeToIndexMap progress_index_by_type; |
| 281 PartitionProgressMarkersByType(gu_response, | 307 PartitionProgressMarkersByType(gu_response, |
| 282 gu_types, | 308 gu_types, |
| 283 &progress_index_by_type); | 309 &progress_index_by_type); |
| 284 if (gu_types.Size() != progress_index_by_type.size()) { | 310 if (gu_types.Size() != progress_index_by_type.size()) { |
| 285 NOTREACHED() << "Missing progress markers in GetUpdates response."; | 311 NOTREACHED() << "Missing progress markers in GetUpdates response."; |
| 286 return false; | 312 return false; |
| 287 } | 313 } |
| 288 | 314 |
| 315 TypeToIndexMap context_by_type; |
| 316 PartitionContextMutationsByType(gu_response, gu_types, &context_by_type); |
| 317 |
| 289 // Iterate over these maps in parallel, processing updates for each type. | 318 // Iterate over these maps in parallel, processing updates for each type. |
| 290 TypeToIndexMap::iterator progress_marker_iter = | 319 TypeToIndexMap::iterator progress_marker_iter = |
| 291 progress_index_by_type.begin(); | 320 progress_index_by_type.begin(); |
| 292 TypeSyncEntityMap::iterator updates_iter = updates_by_type.begin(); | 321 TypeSyncEntityMap::iterator updates_iter = updates_by_type.begin(); |
| 293 for (; (progress_marker_iter != progress_index_by_type.end() | 322 for (; (progress_marker_iter != progress_index_by_type.end() |
| 294 && updates_iter != updates_by_type.end()); | 323 && updates_iter != updates_by_type.end()); |
| 295 ++progress_marker_iter, ++updates_iter) { | 324 ++progress_marker_iter, ++updates_iter) { |
| 296 DCHECK_EQ(progress_marker_iter->first, updates_iter->first); | 325 DCHECK_EQ(progress_marker_iter->first, updates_iter->first); |
| 297 ModelType type = progress_marker_iter->first; | 326 ModelType type = progress_marker_iter->first; |
| 298 | 327 |
| 299 UpdateHandlerMap::iterator update_handler_iter = | 328 UpdateHandlerMap::iterator update_handler_iter = |
| 300 update_handler_map_->find(type); | 329 update_handler_map_->find(type); |
| 301 | 330 |
| 331 sync_pb::DataTypeContext context; |
| 332 TypeToIndexMap::iterator context_iter = context_by_type.find(type); |
| 333 if (context_iter != context_by_type.end()) |
| 334 context.CopyFrom(gu_response.context_mutations(context_iter->second)); |
| 335 |
| 302 if (update_handler_iter != update_handler_map_->end()) { | 336 if (update_handler_iter != update_handler_map_->end()) { |
| 303 update_handler_iter->second->ProcessGetUpdatesResponse( | 337 update_handler_iter->second->ProcessGetUpdatesResponse( |
| 304 gu_response.new_progress_marker(progress_marker_iter->second), | 338 gu_response.new_progress_marker(progress_marker_iter->second), |
| 339 context, |
| 305 updates_iter->second, | 340 updates_iter->second, |
| 306 status_controller); | 341 status_controller); |
| 307 } else { | 342 } else { |
| 308 DLOG(WARNING) | 343 DLOG(WARNING) |
| 309 << "Ignoring received updates of a type we can't handle. " | 344 << "Ignoring received updates of a type we can't handle. " |
| 310 << "Type is: " << ModelTypeToString(type); | 345 << "Type is: " << ModelTypeToString(type); |
| 311 continue; | 346 continue; |
| 312 } | 347 } |
| 313 } | 348 } |
| 314 DCHECK(progress_marker_iter == progress_index_by_type.end() && | 349 DCHECK(progress_marker_iter == progress_index_by_type.end() && |
| 315 updates_iter == updates_by_type.end()); | 350 updates_iter == updates_by_type.end()); |
| 316 | 351 |
| 317 return true; | 352 return true; |
| 318 } | 353 } |
| 319 | 354 |
| 320 void GetUpdatesProcessor::ApplyUpdates( | 355 void GetUpdatesProcessor::ApplyUpdates( |
| 321 ModelTypeSet gu_types, | 356 ModelTypeSet gu_types, |
| 322 sessions::StatusController* status_controller) { | 357 sessions::StatusController* status_controller) { |
| 323 delegate_.ApplyUpdates(gu_types, status_controller, update_handler_map_); | 358 delegate_.ApplyUpdates(gu_types, status_controller, update_handler_map_); |
| 324 } | 359 } |
| 325 | 360 |
| 326 void GetUpdatesProcessor::CopyClientDebugInfo( | 361 void GetUpdatesProcessor::CopyClientDebugInfo( |
| 327 sessions::DebugInfoGetter* debug_info_getter, | 362 sessions::DebugInfoGetter* debug_info_getter, |
| 328 sync_pb::DebugInfo* debug_info) { | 363 sync_pb::DebugInfo* debug_info) { |
| 329 DVLOG(1) << "Copying client debug info to send."; | 364 DVLOG(1) << "Copying client debug info to send."; |
| 330 debug_info_getter->GetDebugInfo(debug_info); | 365 debug_info_getter->GetDebugInfo(debug_info); |
| 331 } | 366 } |
| 332 | 367 |
| 333 } // namespace syncer | 368 } // namespace syncer |
| OLD | NEW |