| 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 "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 | 10 |
| 11 #include "base/trace_event/trace_event.h" | 11 #include "base/trace_event/trace_event.h" |
| 12 #include "sync/engine/get_updates_delegate.h" | 12 #include "components/sync/engine/events/get_updates_response_event.h" |
| 13 #include "sync/engine/syncer_proto_util.h" | 13 #include "components/sync/engine_impl/get_updates_delegate.h" |
| 14 #include "sync/engine/update_handler.h" | 14 #include "components/sync/engine_impl/syncer_proto_util.h" |
| 15 #include "sync/internal_api/public/events/get_updates_response_event.h" | 15 #include "components/sync/engine_impl/update_handler.h" |
| 16 #include "sync/protocol/sync.pb.h" | 16 #include "components/sync/protocol/sync.pb.h" |
| 17 #include "sync/sessions/status_controller.h" | 17 #include "components/sync/sessions_impl/status_controller.h" |
| 18 #include "sync/sessions/sync_session.h" | 18 #include "components/sync/sessions_impl/sync_session.h" |
| 19 #include "sync/syncable/directory.h" | 19 #include "components/sync/syncable/directory.h" |
| 20 #include "sync/syncable/nigori_handler.h" | 20 #include "components/sync/syncable/nigori_handler.h" |
| 21 #include "sync/syncable/syncable_read_transaction.h" | 21 #include "components/sync/syncable/syncable_read_transaction.h" |
| 22 | 22 |
| 23 typedef std::vector<const sync_pb::SyncEntity*> SyncEntityList; | 23 typedef std::vector<const sync_pb::SyncEntity*> SyncEntityList; |
| 24 typedef std::map<syncer::ModelType, SyncEntityList> TypeSyncEntityMap; | 24 typedef std::map<syncer::ModelType, SyncEntityList> TypeSyncEntityMap; |
| 25 | 25 |
| 26 namespace syncer { | 26 namespace syncer { |
| 27 | 27 |
| 28 typedef std::map<ModelType, size_t> TypeToIndexMap; | 28 typedef std::map<ModelType, size_t> TypeToIndexMap; |
| 29 | 29 |
| 30 namespace { | 30 namespace { |
| 31 | 31 |
| 32 bool ShouldRequestEncryptionKey(sessions::SyncSessionContext* context) { | 32 bool ShouldRequestEncryptionKey(sessions::SyncSessionContext* 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 | |
| 40 SyncerError HandleGetEncryptionKeyResponse( | 39 SyncerError HandleGetEncryptionKeyResponse( |
| 41 const sync_pb::ClientToServerResponse& update_response, | 40 const sync_pb::ClientToServerResponse& update_response, |
| 42 syncable::Directory* dir) { | 41 syncable::Directory* dir) { |
| 43 bool success = false; | 42 bool success = false; |
| 44 if (update_response.get_updates().encryption_keys_size() == 0) { | 43 if (update_response.get_updates().encryption_keys_size() == 0) { |
| 45 LOG(ERROR) << "Failed to receive encryption key from server."; | 44 LOG(ERROR) << "Failed to receive encryption key from server."; |
| 46 return SERVER_RESPONSE_VALIDATION_FAILED; | 45 return SERVER_RESPONSE_VALIDATION_FAILED; |
| 47 } | 46 } |
| 48 syncable::ReadTransaction trans(FROM_HERE, dir); | 47 syncable::ReadTransaction trans(FROM_HERE, dir); |
| 49 syncable::NigoriHandler* nigori_handler = dir->GetNigoriHandler(); | 48 syncable::NigoriHandler* nigori_handler = dir->GetNigoriHandler(); |
| 50 success = nigori_handler->SetKeystoreKeys( | 49 success = nigori_handler->SetKeystoreKeys( |
| 51 update_response.get_updates().encryption_keys(), | 50 update_response.get_updates().encryption_keys(), &trans); |
| 52 &trans); | |
| 53 | 51 |
| 54 DVLOG(1) << "GetUpdates returned " | 52 DVLOG(1) << "GetUpdates returned " |
| 55 << update_response.get_updates().encryption_keys_size() | 53 << update_response.get_updates().encryption_keys_size() |
| 56 << "encryption keys. Nigori keystore key " | 54 << "encryption keys. Nigori keystore key " << (success ? "" : "not ") |
| 57 << (success ? "" : "not ") << "updated."; | 55 << "updated."; |
| 58 return (success ? SYNCER_OK : SERVER_RESPONSE_VALIDATION_FAILED); | 56 return (success ? SYNCER_OK : SERVER_RESPONSE_VALIDATION_FAILED); |
| 59 } | 57 } |
| 60 | 58 |
| 61 // Given a GetUpdates response, iterates over all the returned items and | 59 // Given a GetUpdates response, iterates over all the returned items and |
| 62 // 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 |
| 63 // received SyncEntities. The output map will have entries (possibly empty) | 61 // received SyncEntities. The output map will have entries (possibly empty) |
| 64 // for all types in |requested_types|. | 62 // for all types in |requested_types|. |
| 65 void PartitionUpdatesByType(const sync_pb::GetUpdatesResponse& gu_response, | 63 void PartitionUpdatesByType(const sync_pb::GetUpdatesResponse& gu_response, |
| 66 ModelTypeSet requested_types, | 64 ModelTypeSet requested_types, |
| 67 TypeSyncEntityMap* updates_by_type) { | 65 TypeSyncEntityMap* updates_by_type) { |
| 68 int update_count = gu_response.entries().size(); | 66 int update_count = gu_response.entries().size(); |
| 69 for (ModelTypeSet::Iterator it = requested_types.First(); | 67 for (ModelTypeSet::Iterator it = requested_types.First(); it.Good(); |
| 70 it.Good(); it.Inc()) { | 68 it.Inc()) { |
| 71 updates_by_type->insert(std::make_pair(it.Get(), SyncEntityList())); | 69 updates_by_type->insert(std::make_pair(it.Get(), SyncEntityList())); |
| 72 } | 70 } |
| 73 for (int i = 0; i < update_count; ++i) { | 71 for (int i = 0; i < update_count; ++i) { |
| 74 const sync_pb::SyncEntity& update = gu_response.entries(i); | 72 const sync_pb::SyncEntity& update = gu_response.entries(i); |
| 75 ModelType type = GetModelType(update); | 73 ModelType type = GetModelType(update); |
| 76 if (!IsRealDataType(type)) { | 74 if (!IsRealDataType(type)) { |
| 77 NOTREACHED() << "Received update with invalid type."; | 75 NOTREACHED() << "Received update with invalid type."; |
| 78 continue; | 76 continue; |
| 79 } | 77 } |
| 80 | 78 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 continue; | 129 continue; |
| 132 } | 130 } |
| 133 index_map->insert(std::make_pair(model_type, i)); | 131 index_map->insert(std::make_pair(model_type, i)); |
| 134 } | 132 } |
| 135 } | 133 } |
| 136 | 134 |
| 137 // Initializes the parts of the GetUpdatesMessage that depend on shared state, | 135 // Initializes the parts of the GetUpdatesMessage that depend on shared state, |
| 138 // like the ShouldRequestEncryptionKey() status. This is kept separate from the | 136 // like the ShouldRequestEncryptionKey() status. This is kept separate from the |
| 139 // other of the message-building functions to make the rest of the code easier | 137 // other of the message-building functions to make the rest of the code easier |
| 140 // to test. | 138 // to test. |
| 141 void InitDownloadUpdatesContext( | 139 void InitDownloadUpdatesContext(sessions::SyncSession* session, |
| 142 sessions::SyncSession* session, | 140 bool create_mobile_bookmarks_folder, |
| 143 bool create_mobile_bookmarks_folder, | 141 sync_pb::ClientToServerMessage* message) { |
| 144 sync_pb::ClientToServerMessage* message) { | |
| 145 message->set_share(session->context()->account_name()); | 142 message->set_share(session->context()->account_name()); |
| 146 message->set_message_contents(sync_pb::ClientToServerMessage::GET_UPDATES); | 143 message->set_message_contents(sync_pb::ClientToServerMessage::GET_UPDATES); |
| 147 | 144 |
| 148 sync_pb::GetUpdatesMessage* get_updates = message->mutable_get_updates(); | 145 sync_pb::GetUpdatesMessage* get_updates = message->mutable_get_updates(); |
| 149 | 146 |
| 150 // We want folders for our associated types, always. If we were to set | 147 // We want folders for our associated types, always. If we were to set |
| 151 // this to false, the server would send just the non-container items | 148 // this to false, the server would send just the non-container items |
| 152 // (e.g. Bookmark URLs but not their containing folders). | 149 // (e.g. Bookmark URLs but not their containing folders). |
| 153 get_updates->set_fetch_folders(true); | 150 get_updates->set_fetch_folders(true); |
| 154 | 151 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 170 | 167 |
| 171 GetUpdatesProcessor::~GetUpdatesProcessor() {} | 168 GetUpdatesProcessor::~GetUpdatesProcessor() {} |
| 172 | 169 |
| 173 SyncerError GetUpdatesProcessor::DownloadUpdates( | 170 SyncerError GetUpdatesProcessor::DownloadUpdates( |
| 174 ModelTypeSet* request_types, | 171 ModelTypeSet* request_types, |
| 175 sessions::SyncSession* session, | 172 sessions::SyncSession* session, |
| 176 bool create_mobile_bookmarks_folder) { | 173 bool create_mobile_bookmarks_folder) { |
| 177 TRACE_EVENT0("sync", "DownloadUpdates"); | 174 TRACE_EVENT0("sync", "DownloadUpdates"); |
| 178 | 175 |
| 179 sync_pb::ClientToServerMessage message; | 176 sync_pb::ClientToServerMessage message; |
| 180 InitDownloadUpdatesContext(session, | 177 InitDownloadUpdatesContext(session, create_mobile_bookmarks_folder, &message); |
| 181 create_mobile_bookmarks_folder, | |
| 182 &message); | |
| 183 PrepareGetUpdates(*request_types, &message); | 178 PrepareGetUpdates(*request_types, &message); |
| 184 | 179 |
| 185 SyncerError result = ExecuteDownloadUpdates(request_types, session, &message); | 180 SyncerError result = ExecuteDownloadUpdates(request_types, session, &message); |
| 186 session->mutable_status_controller()->set_last_download_updates_result( | 181 session->mutable_status_controller()->set_last_download_updates_result( |
| 187 result); | 182 result); |
| 188 return result; | 183 return result; |
| 189 } | 184 } |
| 190 | 185 |
| 191 void GetUpdatesProcessor::PrepareGetUpdates( | 186 void GetUpdatesProcessor::PrepareGetUpdates( |
| 192 ModelTypeSet gu_types, | 187 ModelTypeSet gu_types, |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 | 226 |
| 232 SyncerError result = SyncerProtoUtil::PostClientToServerMessage( | 227 SyncerError result = SyncerProtoUtil::PostClientToServerMessage( |
| 233 msg, &update_response, session, &partial_failure_data_types); | 228 msg, &update_response, session, &partial_failure_data_types); |
| 234 | 229 |
| 235 DVLOG(2) << SyncerProtoUtil::ClientToServerResponseDebugString( | 230 DVLOG(2) << SyncerProtoUtil::ClientToServerResponseDebugString( |
| 236 update_response); | 231 update_response); |
| 237 | 232 |
| 238 if (result == SERVER_RETURN_PARTIAL_FAILURE) { | 233 if (result == SERVER_RETURN_PARTIAL_FAILURE) { |
| 239 request_types->RemoveAll(partial_failure_data_types); | 234 request_types->RemoveAll(partial_failure_data_types); |
| 240 } else if (result != SYNCER_OK) { | 235 } else if (result != SYNCER_OK) { |
| 241 GetUpdatesResponseEvent response_event( | 236 GetUpdatesResponseEvent response_event(base::Time::Now(), update_response, |
| 242 base::Time::Now(), update_response, result); | 237 result); |
| 243 session->SendProtocolEvent(response_event); | 238 session->SendProtocolEvent(response_event); |
| 244 | 239 |
| 245 // Sync authorization expires every 60 mintues, so SYNC_AUTH_ERROR will | 240 // Sync authorization expires every 60 mintues, so SYNC_AUTH_ERROR will |
| 246 // appear every 60 minutes, and then sync services will refresh the | 241 // appear every 60 minutes, and then sync services will refresh the |
| 247 // authorization. Therefore SYNC_AUTH_ERROR is excluded here to reduce the | 242 // authorization. Therefore SYNC_AUTH_ERROR is excluded here to reduce the |
| 248 // ERROR messages in the log. | 243 // ERROR messages in the log. |
| 249 if (result != SYNC_AUTH_ERROR) { | 244 if (result != SYNC_AUTH_ERROR) { |
| 250 LOG(ERROR) << "PostClientToServerMessage() failed during GetUpdates"; | 245 LOG(ERROR) << "PostClientToServerMessage() failed during GetUpdates"; |
| 251 } | 246 } |
| 252 | 247 |
| 253 return result; | 248 return result; |
| 254 } | 249 } |
| 255 | 250 |
| 256 DVLOG(1) << "GetUpdates returned " | 251 DVLOG(1) << "GetUpdates returned " |
| 257 << update_response.get_updates().entries_size() | 252 << update_response.get_updates().entries_size() << " updates."; |
| 258 << " updates."; | |
| 259 | |
| 260 | 253 |
| 261 if (session->context()->debug_info_getter()) { | 254 if (session->context()->debug_info_getter()) { |
| 262 // Clear debug info now that we have successfully sent it to the server. | 255 // Clear debug info now that we have successfully sent it to the server. |
| 263 DVLOG(1) << "Clearing client debug info."; | 256 DVLOG(1) << "Clearing client debug info."; |
| 264 session->context()->debug_info_getter()->ClearDebugInfo(); | 257 session->context()->debug_info_getter()->ClearDebugInfo(); |
| 265 } | 258 } |
| 266 | 259 |
| 267 if (need_encryption_key || | 260 if (need_encryption_key || |
| 268 update_response.get_updates().encryption_keys_size() > 0) { | 261 update_response.get_updates().encryption_keys_size() > 0) { |
| 269 syncable::Directory* dir = session->context()->directory(); | 262 syncable::Directory* dir = session->context()->directory(); |
| 270 status->set_last_get_key_result( | 263 status->set_last_get_key_result( |
| 271 HandleGetEncryptionKeyResponse(update_response, dir)); | 264 HandleGetEncryptionKeyResponse(update_response, dir)); |
| 272 } | 265 } |
| 273 | 266 |
| 274 SyncerError process_result = | 267 SyncerError process_result = |
| 275 ProcessResponse(update_response.get_updates(), *request_types, status); | 268 ProcessResponse(update_response.get_updates(), *request_types, status); |
| 276 | 269 |
| 277 GetUpdatesResponseEvent response_event( | 270 GetUpdatesResponseEvent response_event(base::Time::Now(), update_response, |
| 278 base::Time::Now(), update_response, process_result); | 271 process_result); |
| 279 session->SendProtocolEvent(response_event); | 272 session->SendProtocolEvent(response_event); |
| 280 | 273 |
| 281 DVLOG(1) << "GetUpdates result: " << process_result; | 274 DVLOG(1) << "GetUpdates result: " << process_result; |
| 282 | 275 |
| 283 return process_result; | 276 return process_result; |
| 284 } | 277 } |
| 285 | 278 |
| 286 SyncerError GetUpdatesProcessor::ProcessResponse( | 279 SyncerError GetUpdatesProcessor::ProcessResponse( |
| 287 const sync_pb::GetUpdatesResponse& gu_response, | 280 const sync_pb::GetUpdatesResponse& gu_response, |
| 288 ModelTypeSet request_types, | 281 ModelTypeSet request_types, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 309 | 302 |
| 310 syncer::SyncerError GetUpdatesProcessor::ProcessGetUpdatesResponse( | 303 syncer::SyncerError GetUpdatesProcessor::ProcessGetUpdatesResponse( |
| 311 ModelTypeSet gu_types, | 304 ModelTypeSet gu_types, |
| 312 const sync_pb::GetUpdatesResponse& gu_response, | 305 const sync_pb::GetUpdatesResponse& gu_response, |
| 313 sessions::StatusController* status_controller) { | 306 sessions::StatusController* status_controller) { |
| 314 TypeSyncEntityMap updates_by_type; | 307 TypeSyncEntityMap updates_by_type; |
| 315 PartitionUpdatesByType(gu_response, gu_types, &updates_by_type); | 308 PartitionUpdatesByType(gu_response, gu_types, &updates_by_type); |
| 316 DCHECK_EQ(gu_types.Size(), updates_by_type.size()); | 309 DCHECK_EQ(gu_types.Size(), updates_by_type.size()); |
| 317 | 310 |
| 318 TypeToIndexMap progress_index_by_type; | 311 TypeToIndexMap progress_index_by_type; |
| 319 PartitionProgressMarkersByType(gu_response, | 312 PartitionProgressMarkersByType(gu_response, gu_types, |
| 320 gu_types, | |
| 321 &progress_index_by_type); | 313 &progress_index_by_type); |
| 322 if (gu_types.Size() != progress_index_by_type.size()) { | 314 if (gu_types.Size() != progress_index_by_type.size()) { |
| 323 NOTREACHED() << "Missing progress markers in GetUpdates response."; | 315 NOTREACHED() << "Missing progress markers in GetUpdates response."; |
| 324 return syncer::SERVER_RESPONSE_VALIDATION_FAILED; | 316 return syncer::SERVER_RESPONSE_VALIDATION_FAILED; |
| 325 } | 317 } |
| 326 | 318 |
| 327 TypeToIndexMap context_by_type; | 319 TypeToIndexMap context_by_type; |
| 328 PartitionContextMutationsByType(gu_response, gu_types, &context_by_type); | 320 PartitionContextMutationsByType(gu_response, gu_types, &context_by_type); |
| 329 | 321 |
| 330 // Iterate over these maps in parallel, processing updates for each type. | 322 // Iterate over these maps in parallel, processing updates for each type. |
| 331 TypeToIndexMap::iterator progress_marker_iter = | 323 TypeToIndexMap::iterator progress_marker_iter = |
| 332 progress_index_by_type.begin(); | 324 progress_index_by_type.begin(); |
| 333 TypeSyncEntityMap::iterator updates_iter = updates_by_type.begin(); | 325 TypeSyncEntityMap::iterator updates_iter = updates_by_type.begin(); |
| 334 for (; (progress_marker_iter != progress_index_by_type.end() | 326 for (; (progress_marker_iter != progress_index_by_type.end() && |
| 335 && updates_iter != updates_by_type.end()); | 327 updates_iter != updates_by_type.end()); |
| 336 ++progress_marker_iter, ++updates_iter) { | 328 ++progress_marker_iter, ++updates_iter) { |
| 337 DCHECK_EQ(progress_marker_iter->first, updates_iter->first); | 329 DCHECK_EQ(progress_marker_iter->first, updates_iter->first); |
| 338 ModelType type = progress_marker_iter->first; | 330 ModelType type = progress_marker_iter->first; |
| 339 | 331 |
| 340 UpdateHandlerMap::iterator update_handler_iter = | 332 UpdateHandlerMap::iterator update_handler_iter = |
| 341 update_handler_map_->find(type); | 333 update_handler_map_->find(type); |
| 342 | 334 |
| 343 sync_pb::DataTypeContext context; | 335 sync_pb::DataTypeContext context; |
| 344 TypeToIndexMap::iterator context_iter = context_by_type.find(type); | 336 TypeToIndexMap::iterator context_iter = context_by_type.find(type); |
| 345 if (context_iter != context_by_type.end()) | 337 if (context_iter != context_by_type.end()) |
| 346 context.CopyFrom(gu_response.context_mutations(context_iter->second)); | 338 context.CopyFrom(gu_response.context_mutations(context_iter->second)); |
| 347 | 339 |
| 348 if (update_handler_iter != update_handler_map_->end()) { | 340 if (update_handler_iter != update_handler_map_->end()) { |
| 349 syncer::SyncerError result = | 341 syncer::SyncerError result = |
| 350 update_handler_iter->second->ProcessGetUpdatesResponse( | 342 update_handler_iter->second->ProcessGetUpdatesResponse( |
| 351 gu_response.new_progress_marker(progress_marker_iter->second), | 343 gu_response.new_progress_marker(progress_marker_iter->second), |
| 352 context, | 344 context, updates_iter->second, status_controller); |
| 353 updates_iter->second, | |
| 354 status_controller); | |
| 355 if (result != syncer::SYNCER_OK) | 345 if (result != syncer::SYNCER_OK) |
| 356 return result; | 346 return result; |
| 357 } else { | 347 } else { |
| 358 DLOG(WARNING) | 348 DLOG(WARNING) << "Ignoring received updates of a type we can't handle. " |
| 359 << "Ignoring received updates of a type we can't handle. " | 349 << "Type is: " << ModelTypeToString(type); |
| 360 << "Type is: " << ModelTypeToString(type); | |
| 361 continue; | 350 continue; |
| 362 } | 351 } |
| 363 } | 352 } |
| 364 DCHECK(progress_marker_iter == progress_index_by_type.end() && | 353 DCHECK(progress_marker_iter == progress_index_by_type.end() && |
| 365 updates_iter == updates_by_type.end()); | 354 updates_iter == updates_by_type.end()); |
| 366 | 355 |
| 367 return syncer::SYNCER_OK; | 356 return syncer::SYNCER_OK; |
| 368 } | 357 } |
| 369 | 358 |
| 370 void GetUpdatesProcessor::ApplyUpdates( | 359 void GetUpdatesProcessor::ApplyUpdates( |
| 371 ModelTypeSet gu_types, | 360 ModelTypeSet gu_types, |
| 372 sessions::StatusController* status_controller) { | 361 sessions::StatusController* status_controller) { |
| 373 status_controller->set_get_updates_request_types(gu_types); | 362 status_controller->set_get_updates_request_types(gu_types); |
| 374 delegate_.ApplyUpdates(gu_types, status_controller, update_handler_map_); | 363 delegate_.ApplyUpdates(gu_types, status_controller, update_handler_map_); |
| 375 } | 364 } |
| 376 | 365 |
| 377 void GetUpdatesProcessor::CopyClientDebugInfo( | 366 void GetUpdatesProcessor::CopyClientDebugInfo( |
| 378 sessions::DebugInfoGetter* debug_info_getter, | 367 sessions::DebugInfoGetter* debug_info_getter, |
| 379 sync_pb::DebugInfo* debug_info) { | 368 sync_pb::DebugInfo* debug_info) { |
| 380 DVLOG(1) << "Copying client debug info to send."; | 369 DVLOG(1) << "Copying client debug info to send."; |
| 381 debug_info_getter->GetDebugInfo(debug_info); | 370 debug_info_getter->GetDebugInfo(debug_info); |
| 382 } | 371 } |
| 383 | 372 |
| 384 } // namespace syncer | 373 } // namespace syncer |
| OLD | NEW |