| 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 | 10 |
| 11 #include "base/trace_event/trace_event.h" | 11 #include "base/trace_event/trace_event.h" |
| 12 #include "components/sync/engine/events/get_updates_response_event.h" | 12 #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/sync_cycle.h" |
| 13 #include "components/sync/engine_impl/get_updates_delegate.h" | 15 #include "components/sync/engine_impl/get_updates_delegate.h" |
| 14 #include "components/sync/engine_impl/syncer_proto_util.h" | 16 #include "components/sync/engine_impl/syncer_proto_util.h" |
| 15 #include "components/sync/engine_impl/update_handler.h" | 17 #include "components/sync/engine_impl/update_handler.h" |
| 16 #include "components/sync/protocol/sync.pb.h" | 18 #include "components/sync/protocol/sync.pb.h" |
| 17 #include "components/sync/sessions_impl/status_controller.h" | |
| 18 #include "components/sync/sessions_impl/sync_session.h" | |
| 19 #include "components/sync/syncable/directory.h" | 19 #include "components/sync/syncable/directory.h" |
| 20 #include "components/sync/syncable/nigori_handler.h" | 20 #include "components/sync/syncable/nigori_handler.h" |
| 21 #include "components/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(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) { |
| 42 bool success = false; | 42 bool success = false; |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 continue; | 129 continue; |
| 130 } | 130 } |
| 131 index_map->insert(std::make_pair(model_type, i)); | 131 index_map->insert(std::make_pair(model_type, i)); |
| 132 } | 132 } |
| 133 } | 133 } |
| 134 | 134 |
| 135 // Initializes the parts of the GetUpdatesMessage that depend on shared state, | 135 // Initializes the parts of the GetUpdatesMessage that depend on shared state, |
| 136 // like the ShouldRequestEncryptionKey() status. This is kept separate from the | 136 // like the ShouldRequestEncryptionKey() status. This is kept separate from the |
| 137 // 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 |
| 138 // to test. | 138 // to test. |
| 139 void InitDownloadUpdatesContext(sessions::SyncSession* session, | 139 void InitDownloadUpdatesContext(SyncCycle* cycle, |
| 140 bool create_mobile_bookmarks_folder, | 140 bool create_mobile_bookmarks_folder, |
| 141 sync_pb::ClientToServerMessage* message) { | 141 sync_pb::ClientToServerMessage* message) { |
| 142 message->set_share(session->context()->account_name()); | 142 message->set_share(cycle->context()->account_name()); |
| 143 message->set_message_contents(sync_pb::ClientToServerMessage::GET_UPDATES); | 143 message->set_message_contents(sync_pb::ClientToServerMessage::GET_UPDATES); |
| 144 | 144 |
| 145 sync_pb::GetUpdatesMessage* get_updates = message->mutable_get_updates(); | 145 sync_pb::GetUpdatesMessage* get_updates = message->mutable_get_updates(); |
| 146 | 146 |
| 147 // 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 |
| 148 // 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 |
| 149 // (e.g. Bookmark URLs but not their containing folders). | 149 // (e.g. Bookmark URLs but not their containing folders). |
| 150 get_updates->set_fetch_folders(true); | 150 get_updates->set_fetch_folders(true); |
| 151 | 151 |
| 152 get_updates->set_create_mobile_bookmarks_folder( | 152 get_updates->set_create_mobile_bookmarks_folder( |
| 153 create_mobile_bookmarks_folder); | 153 create_mobile_bookmarks_folder); |
| 154 bool need_encryption_key = ShouldRequestEncryptionKey(session->context()); | 154 bool need_encryption_key = ShouldRequestEncryptionKey(cycle->context()); |
| 155 get_updates->set_need_encryption_key(need_encryption_key); | 155 get_updates->set_need_encryption_key(need_encryption_key); |
| 156 | 156 |
| 157 // Set legacy GetUpdatesMessage.GetUpdatesCallerInfo information. | 157 // Set legacy GetUpdatesMessage.GetUpdatesCallerInfo information. |
| 158 get_updates->mutable_caller_info()->set_notifications_enabled( | 158 get_updates->mutable_caller_info()->set_notifications_enabled( |
| 159 session->context()->notifications_enabled()); | 159 cycle->context()->notifications_enabled()); |
| 160 } | 160 } |
| 161 | 161 |
| 162 } // namespace | 162 } // namespace |
| 163 | 163 |
| 164 GetUpdatesProcessor::GetUpdatesProcessor(UpdateHandlerMap* update_handler_map, | 164 GetUpdatesProcessor::GetUpdatesProcessor(UpdateHandlerMap* update_handler_map, |
| 165 const GetUpdatesDelegate& delegate) | 165 const GetUpdatesDelegate& delegate) |
| 166 : update_handler_map_(update_handler_map), delegate_(delegate) {} | 166 : update_handler_map_(update_handler_map), delegate_(delegate) {} |
| 167 | 167 |
| 168 GetUpdatesProcessor::~GetUpdatesProcessor() {} | 168 GetUpdatesProcessor::~GetUpdatesProcessor() {} |
| 169 | 169 |
| 170 SyncerError GetUpdatesProcessor::DownloadUpdates( | 170 SyncerError GetUpdatesProcessor::DownloadUpdates( |
| 171 ModelTypeSet* request_types, | 171 ModelTypeSet* request_types, |
| 172 sessions::SyncSession* session, | 172 SyncCycle* cycle, |
| 173 bool create_mobile_bookmarks_folder) { | 173 bool create_mobile_bookmarks_folder) { |
| 174 TRACE_EVENT0("sync", "DownloadUpdates"); | 174 TRACE_EVENT0("sync", "DownloadUpdates"); |
| 175 | 175 |
| 176 sync_pb::ClientToServerMessage message; | 176 sync_pb::ClientToServerMessage message; |
| 177 InitDownloadUpdatesContext(session, create_mobile_bookmarks_folder, &message); | 177 InitDownloadUpdatesContext(cycle, create_mobile_bookmarks_folder, &message); |
| 178 PrepareGetUpdates(*request_types, &message); | 178 PrepareGetUpdates(*request_types, &message); |
| 179 | 179 |
| 180 SyncerError result = ExecuteDownloadUpdates(request_types, session, &message); | 180 SyncerError result = ExecuteDownloadUpdates(request_types, cycle, &message); |
| 181 session->mutable_status_controller()->set_last_download_updates_result( | 181 cycle->mutable_status_controller()->set_last_download_updates_result(result); |
| 182 result); | |
| 183 return result; | 182 return result; |
| 184 } | 183 } |
| 185 | 184 |
| 186 void GetUpdatesProcessor::PrepareGetUpdates( | 185 void GetUpdatesProcessor::PrepareGetUpdates( |
| 187 ModelTypeSet gu_types, | 186 ModelTypeSet gu_types, |
| 188 sync_pb::ClientToServerMessage* message) { | 187 sync_pb::ClientToServerMessage* message) { |
| 189 sync_pb::GetUpdatesMessage* get_updates = message->mutable_get_updates(); | 188 sync_pb::GetUpdatesMessage* get_updates = message->mutable_get_updates(); |
| 190 | 189 |
| 191 for (ModelTypeSet::Iterator it = gu_types.First(); it.Good(); it.Inc()) { | 190 for (ModelTypeSet::Iterator it = gu_types.First(); it.Good(); it.Inc()) { |
| 192 UpdateHandlerMap::iterator handler_it = update_handler_map_->find(it.Get()); | 191 UpdateHandlerMap::iterator handler_it = update_handler_map_->find(it.Get()); |
| 193 DCHECK(handler_it != update_handler_map_->end()) | 192 DCHECK(handler_it != update_handler_map_->end()) |
| 194 << "Failed to look up handler for " << ModelTypeToString(it.Get()); | 193 << "Failed to look up handler for " << ModelTypeToString(it.Get()); |
| 195 sync_pb::DataTypeProgressMarker* progress_marker = | 194 sync_pb::DataTypeProgressMarker* progress_marker = |
| 196 get_updates->add_from_progress_marker(); | 195 get_updates->add_from_progress_marker(); |
| 197 handler_it->second->GetDownloadProgress(progress_marker); | 196 handler_it->second->GetDownloadProgress(progress_marker); |
| 198 progress_marker->clear_gc_directive(); | 197 progress_marker->clear_gc_directive(); |
| 199 | 198 |
| 200 sync_pb::DataTypeContext context; | 199 sync_pb::DataTypeContext context; |
| 201 handler_it->second->GetDataTypeContext(&context); | 200 handler_it->second->GetDataTypeContext(&context); |
| 202 if (!context.context().empty()) | 201 if (!context.context().empty()) |
| 203 get_updates->add_client_contexts()->Swap(&context); | 202 get_updates->add_client_contexts()->Swap(&context); |
| 204 } | 203 } |
| 205 | 204 |
| 206 delegate_.HelpPopulateGuMessage(get_updates); | 205 delegate_.HelpPopulateGuMessage(get_updates); |
| 207 } | 206 } |
| 208 | 207 |
| 209 SyncerError GetUpdatesProcessor::ExecuteDownloadUpdates( | 208 SyncerError GetUpdatesProcessor::ExecuteDownloadUpdates( |
| 210 ModelTypeSet* request_types, | 209 ModelTypeSet* request_types, |
| 211 sessions::SyncSession* session, | 210 SyncCycle* cycle, |
| 212 sync_pb::ClientToServerMessage* msg) { | 211 sync_pb::ClientToServerMessage* msg) { |
| 213 sync_pb::ClientToServerResponse update_response; | 212 sync_pb::ClientToServerResponse update_response; |
| 214 sessions::StatusController* status = session->mutable_status_controller(); | 213 StatusController* status = cycle->mutable_status_controller(); |
| 215 bool need_encryption_key = ShouldRequestEncryptionKey(session->context()); | 214 bool need_encryption_key = ShouldRequestEncryptionKey(cycle->context()); |
| 216 | 215 |
| 217 if (session->context()->debug_info_getter()) { | 216 if (cycle->context()->debug_info_getter()) { |
| 218 sync_pb::DebugInfo* debug_info = msg->mutable_debug_info(); | 217 sync_pb::DebugInfo* debug_info = msg->mutable_debug_info(); |
| 219 CopyClientDebugInfo(session->context()->debug_info_getter(), debug_info); | 218 CopyClientDebugInfo(cycle->context()->debug_info_getter(), debug_info); |
| 220 } | 219 } |
| 221 | 220 |
| 222 session->SendProtocolEvent( | 221 cycle->SendProtocolEvent( |
| 223 *(delegate_.GetNetworkRequestEvent(base::Time::Now(), *msg))); | 222 *(delegate_.GetNetworkRequestEvent(base::Time::Now(), *msg))); |
| 224 | 223 |
| 225 ModelTypeSet partial_failure_data_types; | 224 ModelTypeSet partial_failure_data_types; |
| 226 | 225 |
| 227 SyncerError result = SyncerProtoUtil::PostClientToServerMessage( | 226 SyncerError result = SyncerProtoUtil::PostClientToServerMessage( |
| 228 msg, &update_response, session, &partial_failure_data_types); | 227 msg, &update_response, cycle, &partial_failure_data_types); |
| 229 | 228 |
| 230 DVLOG(2) << SyncerProtoUtil::ClientToServerResponseDebugString( | 229 DVLOG(2) << SyncerProtoUtil::ClientToServerResponseDebugString( |
| 231 update_response); | 230 update_response); |
| 232 | 231 |
| 233 if (result == SERVER_RETURN_PARTIAL_FAILURE) { | 232 if (result == SERVER_RETURN_PARTIAL_FAILURE) { |
| 234 request_types->RemoveAll(partial_failure_data_types); | 233 request_types->RemoveAll(partial_failure_data_types); |
| 235 } else if (result != SYNCER_OK) { | 234 } else if (result != SYNCER_OK) { |
| 236 GetUpdatesResponseEvent response_event(base::Time::Now(), update_response, | 235 GetUpdatesResponseEvent response_event(base::Time::Now(), update_response, |
| 237 result); | 236 result); |
| 238 session->SendProtocolEvent(response_event); | 237 cycle->SendProtocolEvent(response_event); |
| 239 | 238 |
| 240 // Sync authorization expires every 60 mintues, so SYNC_AUTH_ERROR will | 239 // Sync authorization expires every 60 mintues, so SYNC_AUTH_ERROR will |
| 241 // appear every 60 minutes, and then sync services will refresh the | 240 // appear every 60 minutes, and then sync services will refresh the |
| 242 // authorization. Therefore SYNC_AUTH_ERROR is excluded here to reduce the | 241 // authorization. Therefore SYNC_AUTH_ERROR is excluded here to reduce the |
| 243 // ERROR messages in the log. | 242 // ERROR messages in the log. |
| 244 if (result != SYNC_AUTH_ERROR) { | 243 if (result != SYNC_AUTH_ERROR) { |
| 245 LOG(ERROR) << "PostClientToServerMessage() failed during GetUpdates"; | 244 LOG(ERROR) << "PostClientToServerMessage() failed during GetUpdates"; |
| 246 } | 245 } |
| 247 | 246 |
| 248 return result; | 247 return result; |
| 249 } | 248 } |
| 250 | 249 |
| 251 DVLOG(1) << "GetUpdates returned " | 250 DVLOG(1) << "GetUpdates returned " |
| 252 << update_response.get_updates().entries_size() << " updates."; | 251 << update_response.get_updates().entries_size() << " updates."; |
| 253 | 252 |
| 254 if (session->context()->debug_info_getter()) { | 253 if (cycle->context()->debug_info_getter()) { |
| 255 // Clear debug info now that we have successfully sent it to the server. | 254 // Clear debug info now that we have successfully sent it to the server. |
| 256 DVLOG(1) << "Clearing client debug info."; | 255 DVLOG(1) << "Clearing client debug info."; |
| 257 session->context()->debug_info_getter()->ClearDebugInfo(); | 256 cycle->context()->debug_info_getter()->ClearDebugInfo(); |
| 258 } | 257 } |
| 259 | 258 |
| 260 if (need_encryption_key || | 259 if (need_encryption_key || |
| 261 update_response.get_updates().encryption_keys_size() > 0) { | 260 update_response.get_updates().encryption_keys_size() > 0) { |
| 262 syncable::Directory* dir = session->context()->directory(); | 261 syncable::Directory* dir = cycle->context()->directory(); |
| 263 status->set_last_get_key_result( | 262 status->set_last_get_key_result( |
| 264 HandleGetEncryptionKeyResponse(update_response, dir)); | 263 HandleGetEncryptionKeyResponse(update_response, dir)); |
| 265 } | 264 } |
| 266 | 265 |
| 267 SyncerError process_result = | 266 SyncerError process_result = |
| 268 ProcessResponse(update_response.get_updates(), *request_types, status); | 267 ProcessResponse(update_response.get_updates(), *request_types, status); |
| 269 | 268 |
| 270 GetUpdatesResponseEvent response_event(base::Time::Now(), update_response, | 269 GetUpdatesResponseEvent response_event(base::Time::Now(), update_response, |
| 271 process_result); | 270 process_result); |
| 272 session->SendProtocolEvent(response_event); | 271 cycle->SendProtocolEvent(response_event); |
| 273 | 272 |
| 274 DVLOG(1) << "GetUpdates result: " << process_result; | 273 DVLOG(1) << "GetUpdates result: " << process_result; |
| 275 | 274 |
| 276 return process_result; | 275 return process_result; |
| 277 } | 276 } |
| 278 | 277 |
| 279 SyncerError GetUpdatesProcessor::ProcessResponse( | 278 SyncerError GetUpdatesProcessor::ProcessResponse( |
| 280 const sync_pb::GetUpdatesResponse& gu_response, | 279 const sync_pb::GetUpdatesResponse& gu_response, |
| 281 ModelTypeSet request_types, | 280 ModelTypeSet request_types, |
| 282 sessions::StatusController* status) { | 281 StatusController* status) { |
| 283 status->increment_num_updates_downloaded_by(gu_response.entries_size()); | 282 status->increment_num_updates_downloaded_by(gu_response.entries_size()); |
| 284 | 283 |
| 285 // 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 |
| 286 // that field is being set incorrectly, we're in big trouble. | 285 // that field is being set incorrectly, we're in big trouble. |
| 287 if (!gu_response.has_changes_remaining()) { | 286 if (!gu_response.has_changes_remaining()) { |
| 288 return SERVER_RESPONSE_VALIDATION_FAILED; | 287 return SERVER_RESPONSE_VALIDATION_FAILED; |
| 289 } | 288 } |
| 290 | 289 |
| 291 syncer::SyncerError result = | 290 syncer::SyncerError result = |
| 292 ProcessGetUpdatesResponse(request_types, gu_response, status); | 291 ProcessGetUpdatesResponse(request_types, gu_response, status); |
| 293 if (result != syncer::SYNCER_OK) | 292 if (result != syncer::SYNCER_OK) |
| 294 return result; | 293 return result; |
| 295 | 294 |
| 296 if (gu_response.changes_remaining() == 0) { | 295 if (gu_response.changes_remaining() == 0) { |
| 297 return SYNCER_OK; | 296 return SYNCER_OK; |
| 298 } else { | 297 } else { |
| 299 return SERVER_MORE_TO_DOWNLOAD; | 298 return SERVER_MORE_TO_DOWNLOAD; |
| 300 } | 299 } |
| 301 } | 300 } |
| 302 | 301 |
| 303 syncer::SyncerError GetUpdatesProcessor::ProcessGetUpdatesResponse( | 302 syncer::SyncerError GetUpdatesProcessor::ProcessGetUpdatesResponse( |
| 304 ModelTypeSet gu_types, | 303 ModelTypeSet gu_types, |
| 305 const sync_pb::GetUpdatesResponse& gu_response, | 304 const sync_pb::GetUpdatesResponse& gu_response, |
| 306 sessions::StatusController* status_controller) { | 305 StatusController* status_controller) { |
| 307 TypeSyncEntityMap updates_by_type; | 306 TypeSyncEntityMap updates_by_type; |
| 308 PartitionUpdatesByType(gu_response, gu_types, &updates_by_type); | 307 PartitionUpdatesByType(gu_response, gu_types, &updates_by_type); |
| 309 DCHECK_EQ(gu_types.Size(), updates_by_type.size()); | 308 DCHECK_EQ(gu_types.Size(), updates_by_type.size()); |
| 310 | 309 |
| 311 TypeToIndexMap progress_index_by_type; | 310 TypeToIndexMap progress_index_by_type; |
| 312 PartitionProgressMarkersByType(gu_response, gu_types, | 311 PartitionProgressMarkersByType(gu_response, gu_types, |
| 313 &progress_index_by_type); | 312 &progress_index_by_type); |
| 314 if (gu_types.Size() != progress_index_by_type.size()) { | 313 if (gu_types.Size() != progress_index_by_type.size()) { |
| 315 NOTREACHED() << "Missing progress markers in GetUpdates response."; | 314 NOTREACHED() << "Missing progress markers in GetUpdates response."; |
| 316 return syncer::SERVER_RESPONSE_VALIDATION_FAILED; | 315 return syncer::SERVER_RESPONSE_VALIDATION_FAILED; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 349 << "Type is: " << ModelTypeToString(type); | 348 << "Type is: " << ModelTypeToString(type); |
| 350 continue; | 349 continue; |
| 351 } | 350 } |
| 352 } | 351 } |
| 353 DCHECK(progress_marker_iter == progress_index_by_type.end() && | 352 DCHECK(progress_marker_iter == progress_index_by_type.end() && |
| 354 updates_iter == updates_by_type.end()); | 353 updates_iter == updates_by_type.end()); |
| 355 | 354 |
| 356 return syncer::SYNCER_OK; | 355 return syncer::SYNCER_OK; |
| 357 } | 356 } |
| 358 | 357 |
| 359 void GetUpdatesProcessor::ApplyUpdates( | 358 void GetUpdatesProcessor::ApplyUpdates(ModelTypeSet gu_types, |
| 360 ModelTypeSet gu_types, | 359 StatusController* status_controller) { |
| 361 sessions::StatusController* status_controller) { | |
| 362 status_controller->set_get_updates_request_types(gu_types); | 360 status_controller->set_get_updates_request_types(gu_types); |
| 363 delegate_.ApplyUpdates(gu_types, status_controller, update_handler_map_); | 361 delegate_.ApplyUpdates(gu_types, status_controller, update_handler_map_); |
| 364 } | 362 } |
| 365 | 363 |
| 366 void GetUpdatesProcessor::CopyClientDebugInfo( | 364 void GetUpdatesProcessor::CopyClientDebugInfo( |
| 367 sessions::DebugInfoGetter* debug_info_getter, | 365 DebugInfoGetter* debug_info_getter, |
| 368 sync_pb::DebugInfo* debug_info) { | 366 sync_pb::DebugInfo* debug_info) { |
| 369 DVLOG(1) << "Copying client debug info to send."; | 367 DVLOG(1) << "Copying client debug info to send."; |
| 370 debug_info_getter->GetDebugInfo(debug_info); | 368 debug_info_getter->GetDebugInfo(debug_info); |
| 371 } | 369 } |
| 372 | 370 |
| 373 } // namespace syncer | 371 } // namespace syncer |
| OLD | NEW |