syncer_proto_util.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_SYNC_ENGINE_SYNCER_PROTO_UTIL_H_
  5. #define COMPONENTS_SYNC_ENGINE_SYNCER_PROTO_UTIL_H_
  6. #include <string>
  7. #include "base/gtest_prod_util.h"
  8. #include "base/time/time.h"
  9. #include "components/sync/base/model_type.h"
  10. #include "components/sync/base/syncer_error.h"
  11. #include "components/sync/engine/cycle/sync_cycle.h"
  12. namespace sync_pb {
  13. class ClientToServerMessage;
  14. class ClientToServerResponse;
  15. class ClientToServerResponse_Error;
  16. class SyncEntity;
  17. } // namespace sync_pb
  18. namespace syncer {
  19. class ServerConnectionManager;
  20. struct SyncProtocolError;
  21. // Returns the types to migrate from the data in |response|.
  22. ModelTypeSet GetTypesToMigrate(const sync_pb::ClientToServerResponse& response);
  23. // Builds a SyncProtocolError from the data in |error|.
  24. SyncProtocolError ConvertErrorPBToSyncProtocolError(
  25. const sync_pb::ClientToServerResponse_Error& error);
  26. class SyncerProtoUtil {
  27. public:
  28. SyncerProtoUtil(const SyncerProtoUtil&) = delete;
  29. SyncerProtoUtil& operator=(const SyncerProtoUtil&) = delete;
  30. // Adds all fields that must be sent on every request, which includes store
  31. // birthday, protocol version, client chips, api keys, etc. |msg| must be not
  32. // null. Must be called before calling PostClientToServerMessage().
  33. static void AddRequiredFieldsToClientToServerMessage(
  34. const SyncCycle* cycle,
  35. sync_pb::ClientToServerMessage* msg);
  36. // Posts the given message and fills the buffer with the returned value.
  37. // Returns true on success. Also handles store birthday verification: will
  38. // produce a SyncError if the birthday is incorrect. Before calling this
  39. // method, AddRequiredFieldsToClientToServerMessage() must be called.
  40. static SyncerError PostClientToServerMessage(
  41. const sync_pb::ClientToServerMessage& msg,
  42. sync_pb::ClientToServerResponse* response,
  43. SyncCycle* cycle,
  44. ModelTypeSet* partial_failure_data_types);
  45. // Specifies where entity's position should be updated from the data in
  46. // GetUpdates message.
  47. static bool ShouldMaintainPosition(const sync_pb::SyncEntity& sync_entity);
  48. // Specifies where entity's parent ID should be updated from the data in
  49. // GetUpdates message.
  50. static bool ShouldMaintainHierarchy(const sync_pb::SyncEntity& sync_entity);
  51. // Get a debug string representation of the client to server response.
  52. static std::string ClientToServerResponseDebugString(
  53. const sync_pb::ClientToServerResponse& response);
  54. // Get update contents as a string. Intended for logging, and intended
  55. // to have a smaller footprint than the protobuf's built-in pretty printer.
  56. static std::string SyncEntityDebugString(const sync_pb::SyncEntity& entry);
  57. // Set the protocol version field in the outgoing message.
  58. static void SetProtocolVersion(sync_pb::ClientToServerMessage* msg);
  59. private:
  60. SyncerProtoUtil() = default;
  61. // Helper functions for PostClientToServerMessage.
  62. // Analyzes error fields and store birthday in response message, compares
  63. // store birthday with the one in the sync cycle and returns corresponding
  64. // SyncProtocolError. If needed updates store birthday in the cycle context.
  65. // This function makes it easier to test error handling.
  66. static SyncProtocolError GetProtocolErrorFromResponse(
  67. const sync_pb::ClientToServerResponse& response,
  68. SyncCycleContext* context);
  69. // Returns true if sync is disabled by admin for a dasher account.
  70. static bool IsSyncDisabledByAdmin(
  71. const sync_pb::ClientToServerResponse& response);
  72. // Post the message using the scm, and do some processing on the returned
  73. // headers. Decode the server response.
  74. static bool PostAndProcessHeaders(ServerConnectionManager* scm,
  75. const sync_pb::ClientToServerMessage& msg,
  76. sync_pb::ClientToServerResponse* response);
  77. static base::TimeDelta GetThrottleDelay(
  78. const sync_pb::ClientToServerResponse& response);
  79. friend class LoopbackServerTest;
  80. friend class SyncerProtoUtilTest;
  81. FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest, AddRequestBirthday);
  82. FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest, PostAndProcessHeaders);
  83. FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest, HandleThrottlingNoDatatypes);
  84. FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest, HandleThrottlingWithDatatypes);
  85. };
  86. } // namespace syncer
  87. #endif // COMPONENTS_SYNC_ENGINE_SYNCER_PROTO_UTIL_H_