processor_entity.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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_MODEL_PROCESSOR_ENTITY_H_
  5. #define COMPONENTS_SYNC_MODEL_PROCESSOR_ENTITY_H_
  6. #include <stdint.h>
  7. #include <map>
  8. #include <memory>
  9. #include <string>
  10. #include <utility>
  11. #include "base/time/time.h"
  12. #include "components/sync/protocol/entity_metadata.pb.h"
  13. namespace sync_pb {
  14. class EntitySpecifics;
  15. } // namespace sync_pb
  16. namespace syncer {
  17. class ClientTagHash;
  18. struct EntityData;
  19. struct CommitRequestData;
  20. struct CommitResponseData;
  21. struct UpdateResponseData;
  22. // This class is used by the ClientTagBasedModelTypeProcessor to track the state
  23. // of each entity with its type. It can be considered a helper class internal to
  24. // the processor. It manages the metadata for its entity and caches entity data
  25. // upon a local change until commit confirmation is received.
  26. class ProcessorEntity {
  27. public:
  28. // Construct an instance representing a new locally-created item.
  29. static std::unique_ptr<ProcessorEntity> CreateNew(
  30. const std::string& storage_key,
  31. const ClientTagHash& client_tag_hash,
  32. const std::string& id,
  33. base::Time creation_time);
  34. // Construct an instance representing an item loaded from storage on init.
  35. static std::unique_ptr<ProcessorEntity> CreateFromMetadata(
  36. const std::string& storage_key,
  37. sync_pb::EntityMetadata metadata);
  38. ~ProcessorEntity();
  39. const std::string& storage_key() const { return storage_key_; }
  40. const sync_pb::EntityMetadata& metadata() const { return metadata_; }
  41. const EntityData& commit_data() { return *commit_data_; }
  42. // Returns true if this data is out of sync with the server.
  43. // A commit may or may not be in progress at this time.
  44. bool IsUnsynced() const;
  45. // Returns true if this data is out of sync with the sync thread.
  46. //
  47. // There may or may not be a commit in progress for this item, but there's
  48. // definitely no commit in progress for this (most up to date) version of
  49. // this item.
  50. bool RequiresCommitRequest() const;
  51. // Whether commit data is needed to be cached before a commit request can be
  52. // created. Note that deletions do not require cached data.
  53. bool RequiresCommitData() const;
  54. // Whether it's safe to clear the metadata for this entity. This means that
  55. // the entity is deleted and it is up to date with the server (i.e. is *not*
  56. // unsynced).
  57. bool CanClearMetadata() const;
  58. // Returns true if the specified `update_version` is already known, i.e. is
  59. // small or equal to the last known server version.
  60. // This is the case for reflections, but can also be true in some other edge
  61. // cases (e.g. updates were received out of order).
  62. bool IsVersionAlreadyKnown(int64_t update_version) const;
  63. // Records that an update from the server was received but ignores its data.
  64. void RecordIgnoredRemoteUpdate(const UpdateResponseData& response_data);
  65. // Records an update from the server assuming its data is the new data for
  66. // this entity.
  67. void RecordAcceptedRemoteUpdate(const UpdateResponseData& response_data,
  68. sync_pb::EntitySpecifics trimmed_specifics);
  69. // Squashes a pending commit with an update from the server.
  70. void RecordForcedRemoteUpdate(const UpdateResponseData& response_data,
  71. sync_pb::EntitySpecifics trimmed_specifics);
  72. // Applies a local change to this item.
  73. void RecordLocalUpdate(std::unique_ptr<EntityData> data,
  74. sync_pb::EntitySpecifics trimmed_specifics);
  75. // Applies a local deletion to this item. Returns true if entity was
  76. // previously committed to server and tombstone should be sent.
  77. bool RecordLocalDeletion();
  78. // Initializes a message representing this item's uncommitted state
  79. // and assumes that it is forwarded to the sync engine for commiting.
  80. void InitializeCommitRequestData(CommitRequestData* request);
  81. // Receives a successful commit response.
  82. //
  83. // Successful commit responses can overwrite an item's ID.
  84. //
  85. // Note that the receipt of a successful commit response does not necessarily
  86. // unset IsUnsynced(). If many local changes occur in quick succession, it's
  87. // possible that the committed item was already out of date by the time it
  88. // reached the server.
  89. void ReceiveCommitResponse(const CommitResponseData& data, bool commit_only);
  90. // Clears any in-memory sync state associated with outstanding commits.
  91. void ClearTransientSyncState();
  92. // Update storage_key_. Allows setting storage key for datatypes that don't
  93. // generate storage key from syncer::EntityData. Should only be called for
  94. // an entity initialized with empty storage key.
  95. void SetStorageKey(const std::string& storage_key);
  96. // Undoes SetStorageKey(), which is needed in certain conflict resolution
  97. // scenarios.
  98. void ClearStorageKey();
  99. // Takes the passed commit data updates its fields with values from metadata
  100. // and caches it in the instance.
  101. void SetCommitData(std::unique_ptr<EntityData> data);
  102. // Check if the instance has cached commit data.
  103. bool HasCommitData() const;
  104. // Check whether |data| matches the stored specifics hash.
  105. bool MatchesData(const EntityData& data) const;
  106. // Check whether the current metadata of an unsynced entity matches the stored
  107. // base specifics hash.
  108. bool MatchesOwnBaseData() const;
  109. // Check whether |data| matches the stored base specifics hash.
  110. bool MatchesBaseData(const EntityData& data) const;
  111. // Increment sequence number in the metadata. This will also update the
  112. // base_specifics_hash if the entity was not already unsynced.
  113. void IncrementSequenceNumber(base::Time modification_time);
  114. // Returns the estimate of dynamically allocated memory in bytes.
  115. size_t EstimateMemoryUsage() const;
  116. private:
  117. friend class ProcessorEntityTest;
  118. // The constructor swaps the data from the passed metadata.
  119. ProcessorEntity(const std::string& storage_key,
  120. sync_pb::EntityMetadata metadata);
  121. // Check whether |specifics| matches the stored specifics_hash.
  122. bool MatchesSpecificsHash(const sync_pb::EntitySpecifics& specifics) const;
  123. // Update hash string for EntitySpecifics in the metadata.
  124. void UpdateSpecificsHash(const sync_pb::EntitySpecifics& specifics);
  125. // Storage key. Should always be available.
  126. std::string storage_key_;
  127. // Serializable Sync metadata.
  128. sync_pb::EntityMetadata metadata_;
  129. // Sync data that exists for items being committed only. The data is moved
  130. // away when sending the commit request.
  131. std::unique_ptr<EntityData> commit_data_;
  132. // The sequence number of the last item sent to the sync thread.
  133. int64_t commit_requested_sequence_number_;
  134. // The time when this entity transition from being synced to being unsynced
  135. // (i.e. a local change happened).
  136. base::Time unsynced_time_;
  137. };
  138. } // namespace syncer
  139. #endif // COMPONENTS_SYNC_MODEL_PROCESSOR_ENTITY_H_