commit_contribution_impl.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_SYNC_ENGINE_COMMIT_CONTRIBUTION_IMPL_H_
  5. #define COMPONENTS_SYNC_ENGINE_COMMIT_CONTRIBUTION_IMPL_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include "base/callback.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "components/sync/base/passphrase_enums.h"
  11. #include "components/sync/engine/commit_and_get_updates_types.h"
  12. #include "components/sync/engine/commit_contribution.h"
  13. #include "components/sync/protocol/data_type_progress_marker.pb.h"
  14. namespace sync_pb {
  15. class ClientToServerMessage;
  16. class SyncEntity;
  17. class ClientToServerResponse;
  18. } // namespace sync_pb
  19. namespace syncer {
  20. class Cryptographer;
  21. class ModelTypeWorker;
  22. // A sync type's contribution to an outgoing commit message.
  23. //
  24. // Helps build a commit message and process its response. It collaborates
  25. // closely with the ModelTypeWorker.
  26. class CommitContributionImpl : public CommitContribution {
  27. public:
  28. // If a non-null |cryptographer| is passed, it must outlive this object and
  29. // will be used to encrypt the constructed commit. Otherwise, the commit won't
  30. // be encrypted. Only one of |on_commit_response_callback| and
  31. // |on_full_commit_failure_callback| will be called.
  32. // TODO(rushans): there is still possible rare case when both of these
  33. // callbacks are never called, i.e. if get updates from the server fails.
  34. CommitContributionImpl(
  35. ModelType type,
  36. const sync_pb::DataTypeContext& context,
  37. CommitRequestDataList commit_requests,
  38. base::OnceCallback<void(const CommitResponseDataList&,
  39. const FailedCommitResponseDataList&)>
  40. on_commit_response_callback,
  41. base::OnceCallback<void(SyncCommitError)> on_full_commit_failure_callback,
  42. Cryptographer* cryptographer,
  43. PassphraseType passphrase_type,
  44. bool only_commit_specifics);
  45. CommitContributionImpl(const CommitContributionImpl&) = delete;
  46. CommitContributionImpl& operator=(const CommitContributionImpl&) = delete;
  47. ~CommitContributionImpl() override;
  48. // Implementation of CommitContribution
  49. void AddToCommitMessage(sync_pb::ClientToServerMessage* msg) override;
  50. SyncerError ProcessCommitResponse(
  51. const sync_pb::ClientToServerResponse& response,
  52. StatusController* status) override;
  53. void ProcessCommitFailure(SyncCommitError commit_error) override;
  54. size_t GetNumEntries() const override;
  55. // Public for testing.
  56. // Copies data to be committed from CommitRequestData into SyncEntity proto.
  57. static void PopulateCommitProto(ModelType type,
  58. const CommitRequestData& commit_entity,
  59. sync_pb::SyncEntity* commit_proto);
  60. private:
  61. // Generates id for new entities and encrypts entity if needed.
  62. void AdjustCommitProto(sync_pb::SyncEntity* commit_proto);
  63. const ModelType type_;
  64. // A callback to inform the object that created this contribution about commit
  65. // result.
  66. base::OnceCallback<void(const CommitResponseDataList&,
  67. const FailedCommitResponseDataList&)>
  68. on_commit_response_callback_;
  69. // A callback to inform the object that created this contribution about commit
  70. // failure. This callback differs from |on_commit_response_callback_| and will
  71. // be called when the server respond with any error code or do not respond at
  72. // all (i.e. there is no internet connection).
  73. base::OnceCallback<void(SyncCommitError)> on_full_commit_failure_callback_;
  74. // Null if |type_| is not encrypted. Otherwise this is used to encrypt the
  75. // committed entities.
  76. const raw_ptr<Cryptographer> cryptographer_;
  77. const PassphraseType passphrase_type_;
  78. // The type-global context information.
  79. const sync_pb::DataTypeContext context_;
  80. // The list of entities to be committed.
  81. CommitRequestDataList commit_requests_;
  82. // The index in the commit message where this contribution's entities are
  83. // added. Used to correlate per-item requests with per-item responses.
  84. size_t entries_start_index_;
  85. // Don't send any metadata to server, only specifics. This is needed for
  86. // commit only types to save bandwidth.
  87. bool only_commit_specifics_;
  88. };
  89. } // namespace syncer
  90. #endif // COMPONENTS_SYNC_ENGINE_COMMIT_CONTRIBUTION_IMPL_H_