| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/syncer_proto_util.h" | 5 #include "components/sync/engine_impl/syncer_proto_util.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| 11 #include "components/sync/base/model_type.h" |
| 12 #include "components/sync/base/time.h" |
| 13 #include "components/sync/engine_impl/net/server_connection_manager.h" |
| 14 #include "components/sync/engine_impl/syncer.h" |
| 15 #include "components/sync/engine_impl/syncer_types.h" |
| 16 #include "components/sync/engine_impl/traffic_logger.h" |
| 17 #include "components/sync/protocol/sync_enums.pb.h" |
| 18 #include "components/sync/protocol/sync_protocol_error.h" |
| 19 #include "components/sync/sessions_impl/sync_session.h" |
| 20 #include "components/sync/syncable/directory.h" |
| 21 #include "components/sync/syncable/entry.h" |
| 22 #include "components/sync/syncable/syncable-inl.h" |
| 23 #include "components/sync/syncable/syncable_proto_util.h" |
| 11 #include "google_apis/google_api_keys.h" | 24 #include "google_apis/google_api_keys.h" |
| 12 #include "sync/engine/net/server_connection_manager.h" | |
| 13 #include "sync/engine/syncer.h" | |
| 14 #include "sync/engine/syncer_types.h" | |
| 15 #include "sync/engine/traffic_logger.h" | |
| 16 #include "sync/internal_api/public/base/model_type.h" | |
| 17 #include "sync/protocol/sync_enums.pb.h" | |
| 18 #include "sync/protocol/sync_protocol_error.h" | |
| 19 #include "sync/sessions/sync_session.h" | |
| 20 #include "sync/syncable/directory.h" | |
| 21 #include "sync/syncable/entry.h" | |
| 22 #include "sync/syncable/syncable-inl.h" | |
| 23 #include "sync/syncable/syncable_proto_util.h" | |
| 24 #include "sync/util/time.h" | |
| 25 | 25 |
| 26 using std::string; | 26 using std::string; |
| 27 using std::stringstream; | 27 using std::stringstream; |
| 28 using sync_pb::ClientToServerMessage; | 28 using sync_pb::ClientToServerMessage; |
| 29 using sync_pb::ClientToServerResponse; | 29 using sync_pb::ClientToServerResponse; |
| 30 | 30 |
| 31 namespace syncer { | 31 namespace syncer { |
| 32 | 32 |
| 33 using sessions::SyncSession; | 33 using sessions::SyncSession; |
| 34 using syncable::BASE_VERSION; | 34 using syncable::BASE_VERSION; |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 sync_protocol_error.error_description = error.error_description(); | 219 sync_protocol_error.error_description = error.error_description(); |
| 220 sync_protocol_error.url = error.url(); | 220 sync_protocol_error.url = error.url(); |
| 221 sync_protocol_error.action = PBActionToClientAction(error.action()); | 221 sync_protocol_error.action = PBActionToClientAction(error.action()); |
| 222 | 222 |
| 223 if (error.error_data_type_ids_size() > 0) { | 223 if (error.error_data_type_ids_size() > 0) { |
| 224 // THROTTLED and PARTIAL_FAILURE are currently the only error codes | 224 // THROTTLED and PARTIAL_FAILURE are currently the only error codes |
| 225 // that uses |error_data_types|. | 225 // that uses |error_data_types|. |
| 226 // In both cases, |error_data_types| are throttled. | 226 // In both cases, |error_data_types| are throttled. |
| 227 for (int i = 0; i < error.error_data_type_ids_size(); ++i) { | 227 for (int i = 0; i < error.error_data_type_ids_size(); ++i) { |
| 228 int field_number = error.error_data_type_ids(i); | 228 int field_number = error.error_data_type_ids(i); |
| 229 ModelType model_type = | 229 ModelType model_type = GetModelTypeFromSpecificsFieldNumber(field_number); |
| 230 GetModelTypeFromSpecificsFieldNumber(field_number); | |
| 231 if (!IsRealDataType(model_type)) { | 230 if (!IsRealDataType(model_type)) { |
| 232 DLOG(WARNING) << "Unknown field number " << field_number; | 231 DLOG(WARNING) << "Unknown field number " << field_number; |
| 233 continue; | 232 continue; |
| 234 } | 233 } |
| 235 sync_protocol_error.error_data_types.Put(model_type); | 234 sync_protocol_error.error_data_types.Put(model_type); |
| 236 } | 235 } |
| 237 } | 236 } |
| 238 | 237 |
| 239 return sync_protocol_error; | 238 return sync_protocol_error; |
| 240 } | 239 } |
| 241 | 240 |
| 242 // static | 241 // static |
| 243 bool SyncerProtoUtil::VerifyResponseBirthday( | 242 bool SyncerProtoUtil::VerifyResponseBirthday( |
| 244 const ClientToServerResponse& response, | 243 const ClientToServerResponse& response, |
| 245 syncable::Directory* dir) { | 244 syncable::Directory* dir) { |
| 246 | |
| 247 std::string local_birthday = dir->store_birthday(); | 245 std::string local_birthday = dir->store_birthday(); |
| 248 | 246 |
| 249 if (local_birthday.empty()) { | 247 if (local_birthday.empty()) { |
| 250 if (!response.has_store_birthday()) { | 248 if (!response.has_store_birthday()) { |
| 251 LOG(WARNING) << "Expected a birthday on first sync."; | 249 LOG(WARNING) << "Expected a birthday on first sync."; |
| 252 return false; | 250 return false; |
| 253 } | 251 } |
| 254 | 252 |
| 255 DVLOG(1) << "New store birthday: " << response.store_birthday(); | 253 DVLOG(1) << "New store birthday: " << response.store_birthday(); |
| 256 dir->set_store_birthday(response.store_birthday()); | 254 dir->set_store_birthday(response.store_birthday()); |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 return throttle_delay; | 360 return throttle_delay; |
| 363 } | 361 } |
| 364 | 362 |
| 365 // static | 363 // static |
| 366 SyncerError SyncerProtoUtil::PostClientToServerMessage( | 364 SyncerError SyncerProtoUtil::PostClientToServerMessage( |
| 367 ClientToServerMessage* msg, | 365 ClientToServerMessage* msg, |
| 368 ClientToServerResponse* response, | 366 ClientToServerResponse* response, |
| 369 SyncSession* session, | 367 SyncSession* session, |
| 370 ModelTypeSet* partial_failure_data_types) { | 368 ModelTypeSet* partial_failure_data_types) { |
| 371 CHECK(response); | 369 CHECK(response); |
| 372 DCHECK(!msg->get_updates().has_from_timestamp()); // Deprecated. | 370 DCHECK(!msg->get_updates().has_from_timestamp()); // Deprecated. |
| 373 DCHECK(!msg->get_updates().has_requested_types()); // Deprecated. | 371 DCHECK(!msg->get_updates().has_requested_types()); // Deprecated. |
| 374 | 372 |
| 375 // Add must-have fields. | 373 // Add must-have fields. |
| 376 SetProtocolVersion(msg); | 374 SetProtocolVersion(msg); |
| 377 AddRequestBirthday(session->context()->directory(), msg); | 375 AddRequestBirthday(session->context()->directory(), msg); |
| 378 DCHECK(msg->has_store_birthday() || !IsBirthdayRequired(*msg)); | 376 DCHECK(msg->has_store_birthday() || !IsBirthdayRequired(*msg)); |
| 379 AddBagOfChips(session->context()->directory(), msg); | 377 AddBagOfChips(session->context()->directory(), msg); |
| 380 msg->set_api_key(google_apis::GetAPIKey()); | 378 msg->set_api_key(google_apis::GetAPIKey()); |
| 381 msg->mutable_client_status()->CopyFrom(session->context()->client_status()); | 379 msg->mutable_client_status()->CopyFrom(session->context()->client_status()); |
| 382 msg->set_invalidator_client_id(session->context()->invalidator_client_id()); | 380 msg->set_invalidator_client_id(session->context()->invalidator_client_id()); |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 469 case SYNC_SUCCESS: | 467 case SYNC_SUCCESS: |
| 470 LogResponseProfilingData(*response); | 468 LogResponseProfilingData(*response); |
| 471 return SYNCER_OK; | 469 return SYNCER_OK; |
| 472 case THROTTLED: | 470 case THROTTLED: |
| 473 if (sync_protocol_error.error_data_types.Empty()) { | 471 if (sync_protocol_error.error_data_types.Empty()) { |
| 474 DLOG(WARNING) << "Client fully throttled by syncer."; | 472 DLOG(WARNING) << "Client fully throttled by syncer."; |
| 475 session->delegate()->OnThrottled(GetThrottleDelay(*response)); | 473 session->delegate()->OnThrottled(GetThrottleDelay(*response)); |
| 476 } else { | 474 } else { |
| 477 DLOG(WARNING) << "Some types throttled by syncer."; | 475 DLOG(WARNING) << "Some types throttled by syncer."; |
| 478 session->delegate()->OnTypesThrottled( | 476 session->delegate()->OnTypesThrottled( |
| 479 sync_protocol_error.error_data_types, | 477 sync_protocol_error.error_data_types, GetThrottleDelay(*response)); |
| 480 GetThrottleDelay(*response)); | |
| 481 } | 478 } |
| 482 return SERVER_RETURN_THROTTLED; | 479 return SERVER_RETURN_THROTTLED; |
| 483 case TRANSIENT_ERROR: | 480 case TRANSIENT_ERROR: |
| 484 return SERVER_RETURN_TRANSIENT_ERROR; | 481 return SERVER_RETURN_TRANSIENT_ERROR; |
| 485 case MIGRATION_DONE: | 482 case MIGRATION_DONE: |
| 486 LOG_IF(ERROR, 0 >= response->migrated_data_type_id_size()) | 483 LOG_IF(ERROR, 0 >= response->migrated_data_type_id_size()) |
| 487 << "MIGRATION_DONE but no types specified."; | 484 << "MIGRATION_DONE but no types specified."; |
| 488 session->delegate()->OnReceivedMigrationRequest( | 485 session->delegate()->OnReceivedMigrationRequest( |
| 489 GetTypesToMigrate(*response)); | 486 GetTypesToMigrate(*response)); |
| 490 return SERVER_RETURN_MIGRATION_DONE; | 487 return SERVER_RETURN_MIGRATION_DONE; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 511 NOTREACHED(); | 508 NOTREACHED(); |
| 512 return UNSET; | 509 return UNSET; |
| 513 } | 510 } |
| 514 } | 511 } |
| 515 | 512 |
| 516 // static | 513 // static |
| 517 bool SyncerProtoUtil::ShouldMaintainPosition( | 514 bool SyncerProtoUtil::ShouldMaintainPosition( |
| 518 const sync_pb::SyncEntity& sync_entity) { | 515 const sync_pb::SyncEntity& sync_entity) { |
| 519 // Maintain positions for bookmarks that are not server-defined top-level | 516 // Maintain positions for bookmarks that are not server-defined top-level |
| 520 // folders. | 517 // folders. |
| 521 return GetModelType(sync_entity) == BOOKMARKS | 518 return GetModelType(sync_entity) == BOOKMARKS && |
| 522 && !(sync_entity.folder() && | 519 !(sync_entity.folder() && |
| 523 !sync_entity.server_defined_unique_tag().empty()); | 520 !sync_entity.server_defined_unique_tag().empty()); |
| 524 } | 521 } |
| 525 | 522 |
| 526 // static | 523 // static |
| 527 bool SyncerProtoUtil::ShouldMaintainHierarchy( | 524 bool SyncerProtoUtil::ShouldMaintainHierarchy( |
| 528 const sync_pb::SyncEntity& sync_entity) { | 525 const sync_pb::SyncEntity& sync_entity) { |
| 529 // Maintain hierarchy for bookmarks or top-level items. | 526 // Maintain hierarchy for bookmarks or top-level items. |
| 530 return GetModelType(sync_entity) == BOOKMARKS || | 527 return GetModelType(sync_entity) == BOOKMARKS || |
| 531 sync_entity.parent_id_string() == "0"; | 528 sync_entity.parent_id_string() == "0"; |
| 532 } | 529 } |
| 533 | 530 |
| 534 // static | 531 // static |
| 535 const std::string& SyncerProtoUtil::NameFromSyncEntity( | 532 const std::string& SyncerProtoUtil::NameFromSyncEntity( |
| 536 const sync_pb::SyncEntity& entry) { | 533 const sync_pb::SyncEntity& entry) { |
| 537 if (entry.has_non_unique_name()) | 534 if (entry.has_non_unique_name()) |
| 538 return entry.non_unique_name(); | 535 return entry.non_unique_name(); |
| 539 return entry.name(); | 536 return entry.name(); |
| 540 } | 537 } |
| 541 | 538 |
| 542 // static | 539 // static |
| 543 const std::string& SyncerProtoUtil::NameFromCommitEntryResponse( | 540 const std::string& SyncerProtoUtil::NameFromCommitEntryResponse( |
| 544 const sync_pb::CommitResponse_EntryResponse& entry) { | 541 const sync_pb::CommitResponse_EntryResponse& entry) { |
| 545 if (entry.has_non_unique_name()) | 542 if (entry.has_non_unique_name()) |
| 546 return entry.non_unique_name(); | 543 return entry.non_unique_name(); |
| 547 return entry.name(); | 544 return entry.name(); |
| 548 } | 545 } |
| 549 | 546 |
| 550 // static | 547 // static |
| 551 void SyncerProtoUtil::PersistBagOfChips(syncable::Directory* dir, | 548 void SyncerProtoUtil::PersistBagOfChips( |
| 549 syncable::Directory* dir, |
| 552 const sync_pb::ClientToServerResponse& response) { | 550 const sync_pb::ClientToServerResponse& response) { |
| 553 if (!response.has_new_bag_of_chips()) | 551 if (!response.has_new_bag_of_chips()) |
| 554 return; | 552 return; |
| 555 std::string bag_of_chips; | 553 std::string bag_of_chips; |
| 556 if (response.new_bag_of_chips().SerializeToString(&bag_of_chips)) | 554 if (response.new_bag_of_chips().SerializeToString(&bag_of_chips)) |
| 557 dir->set_bag_of_chips(bag_of_chips); | 555 dir->set_bag_of_chips(bag_of_chips); |
| 558 } | 556 } |
| 559 | 557 |
| 560 std::string SyncerProtoUtil::SyncEntityDebugString( | 558 std::string SyncerProtoUtil::SyncEntityDebugString( |
| 561 const sync_pb::SyncEntity& entry) { | 559 const sync_pb::SyncEntity& entry) { |
| 562 const std::string& mtime_str = | 560 const std::string& mtime_str = |
| 563 GetTimeDebugString(ProtoTimeToTime(entry.mtime())); | 561 GetTimeDebugString(ProtoTimeToTime(entry.mtime())); |
| 564 const std::string& ctime_str = | 562 const std::string& ctime_str = |
| 565 GetTimeDebugString(ProtoTimeToTime(entry.ctime())); | 563 GetTimeDebugString(ProtoTimeToTime(entry.ctime())); |
| 566 return base::StringPrintf( | 564 return base::StringPrintf( |
| 567 "id: %s, parent_id: %s, " | 565 "id: %s, parent_id: %s, " |
| 568 "version: %" PRId64"d, " | 566 "version: %" PRId64 |
| 569 "mtime: %" PRId64"d (%s), " | 567 "d, " |
| 570 "ctime: %" PRId64"d (%s), " | 568 "mtime: %" PRId64 |
| 571 "name: %s, sync_timestamp: %" PRId64"d, " | 569 "d (%s), " |
| 570 "ctime: %" PRId64 |
| 571 "d (%s), " |
| 572 "name: %s, sync_timestamp: %" PRId64 |
| 573 "d, " |
| 572 "%s ", | 574 "%s ", |
| 573 entry.id_string().c_str(), | 575 entry.id_string().c_str(), entry.parent_id_string().c_str(), |
| 574 entry.parent_id_string().c_str(), | 576 entry.version(), entry.mtime(), mtime_str.c_str(), entry.ctime(), |
| 575 entry.version(), | 577 ctime_str.c_str(), entry.name().c_str(), entry.sync_timestamp(), |
| 576 entry.mtime(), mtime_str.c_str(), | 578 entry.deleted() ? "deleted, " : ""); |
| 577 entry.ctime(), ctime_str.c_str(), | |
| 578 entry.name().c_str(), entry.sync_timestamp(), | |
| 579 entry.deleted() ? "deleted, ":""); | |
| 580 } | 579 } |
| 581 | 580 |
| 582 namespace { | 581 namespace { |
| 583 std::string GetUpdatesResponseString( | 582 std::string GetUpdatesResponseString( |
| 584 const sync_pb::GetUpdatesResponse& response) { | 583 const sync_pb::GetUpdatesResponse& response) { |
| 585 std::string output; | 584 std::string output; |
| 586 output.append("GetUpdatesResponse:\n"); | 585 output.append("GetUpdatesResponse:\n"); |
| 587 for (int i = 0; i < response.entries_size(); i++) { | 586 for (int i = 0; i < response.entries_size(); i++) { |
| 588 output.append(SyncerProtoUtil::SyncEntityDebugString(response.entries(i))); | 587 output.append(SyncerProtoUtil::SyncEntityDebugString(response.entries(i))); |
| 589 output.append("\n"); | 588 output.append("\n"); |
| 590 } | 589 } |
| 591 return output; | 590 return output; |
| 592 } | 591 } |
| 593 } // namespace | 592 } // namespace |
| 594 | 593 |
| 595 std::string SyncerProtoUtil::ClientToServerResponseDebugString( | 594 std::string SyncerProtoUtil::ClientToServerResponseDebugString( |
| 596 const ClientToServerResponse& response) { | 595 const ClientToServerResponse& response) { |
| 597 // Add more handlers as needed. | 596 // Add more handlers as needed. |
| 598 std::string output; | 597 std::string output; |
| 599 if (response.has_get_updates()) | 598 if (response.has_get_updates()) |
| 600 output.append(GetUpdatesResponseString(response.get_updates())); | 599 output.append(GetUpdatesResponseString(response.get_updates())); |
| 601 return output; | 600 return output; |
| 602 } | 601 } |
| 603 | 602 |
| 604 } // namespace syncer | 603 } // namespace syncer |
| OLD | NEW |