| OLD | NEW |
| (Empty) |
| 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 | |
| 5 #ifndef COMPONENTS_SYNC_CORE_PROCESSOR_ENTITY_TRACKER_H_ | |
| 6 #define COMPONENTS_SYNC_CORE_PROCESSOR_ENTITY_TRACKER_H_ | |
| 7 | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <memory> | |
| 11 #include <string> | |
| 12 | |
| 13 #include "base/time/time.h" | |
| 14 #include "components/sync/model/entity_data.h" | |
| 15 #include "components/sync/protocol/entity_metadata.pb.h" | |
| 16 | |
| 17 namespace syncer { | |
| 18 struct CommitRequestData; | |
| 19 struct CommitResponseData; | |
| 20 struct UpdateResponseData; | |
| 21 | |
| 22 // This class is used by the SharedModelTypeProcessor to track the state of each | |
| 23 // entity with its type. It can be considered a helper class internal to the | |
| 24 // processor. It manages the metadata for its entity and caches entity data | |
| 25 // upon a local change until commit confirmation is received. | |
| 26 class ProcessorEntityTracker { | |
| 27 public: | |
| 28 // Construct an instance representing a new locally-created item. | |
| 29 static std::unique_ptr<ProcessorEntityTracker> CreateNew( | |
| 30 const std::string& storage_key, | |
| 31 const std::string& client_tag_hash, | |
| 32 const std::string& id, | |
| 33 base::Time creation_time); | |
| 34 | |
| 35 // Construct an instance representing an item loaded from storage on init. | |
| 36 // This method swaps out the contents of |metadata|. | |
| 37 static std::unique_ptr<ProcessorEntityTracker> CreateFromMetadata( | |
| 38 const std::string& storage_key, | |
| 39 sync_pb::EntityMetadata* metadata); | |
| 40 | |
| 41 ~ProcessorEntityTracker(); | |
| 42 | |
| 43 const std::string& storage_key() const { return storage_key_; } | |
| 44 const sync_pb::EntityMetadata& metadata() const { return metadata_; } | |
| 45 const EntityDataPtr& commit_data() const { return commit_data_; } | |
| 46 | |
| 47 // Returns true if this data is out of sync with the server. | |
| 48 // A commit may or may not be in progress at this time. | |
| 49 bool IsUnsynced() const; | |
| 50 | |
| 51 // Returns true if this data is out of sync with the sync thread. | |
| 52 // | |
| 53 // There may or may not be a commit in progress for this item, but there's | |
| 54 // definitely no commit in progress for this (most up to date) version of | |
| 55 // this item. | |
| 56 bool RequiresCommitRequest() const; | |
| 57 | |
| 58 // Whether commit data is needed to be cached before a commit request can be | |
| 59 // created. Note that deletions do not require cached data. | |
| 60 bool RequiresCommitData() const; | |
| 61 | |
| 62 // Whether it's safe to clear the metadata for this entity. This means that | |
| 63 // the entity is deleted and either knowledge of this entity has never left | |
| 64 // this client or it is up to date with the server. | |
| 65 bool CanClearMetadata() const; | |
| 66 | |
| 67 // Returns true if the specified update version does not contain new data. | |
| 68 bool UpdateIsReflection(int64_t update_version) const; | |
| 69 | |
| 70 // Records that an update from the server was received but ignores its data. | |
| 71 void RecordIgnoredUpdate(const UpdateResponseData& response_data); | |
| 72 | |
| 73 // Records an update from the server assuming its data is the new data for | |
| 74 // this entity. | |
| 75 void RecordAcceptedUpdate(const UpdateResponseData& response_data); | |
| 76 | |
| 77 // Squashes a pending commit with an update from the server. | |
| 78 void RecordForcedUpdate(const UpdateResponseData& response_data); | |
| 79 | |
| 80 // Applies a local change to this item. | |
| 81 void MakeLocalChange(std::unique_ptr<EntityData> data); | |
| 82 | |
| 83 // Applies a local deletion to this item. | |
| 84 void Delete(); | |
| 85 | |
| 86 // Initializes a message representing this item's uncommitted state | |
| 87 // and assumes that it is forwarded to the sync engine for commiting. | |
| 88 void InitializeCommitRequestData(CommitRequestData* request); | |
| 89 | |
| 90 // Receives a successful commit response. | |
| 91 // | |
| 92 // Successful commit responses can overwrite an item's ID. | |
| 93 // | |
| 94 // Note that the receipt of a successful commit response does not necessarily | |
| 95 // unset IsUnsynced(). If many local changes occur in quick succession, it's | |
| 96 // possible that the committed item was already out of date by the time it | |
| 97 // reached the server. | |
| 98 void ReceiveCommitResponse(const CommitResponseData& data); | |
| 99 | |
| 100 // Clears any in-memory sync state associated with outstanding commits. | |
| 101 void ClearTransientSyncState(); | |
| 102 | |
| 103 // Takes the passed commit data and caches it in the instance. | |
| 104 // The data is swapped from the input struct without copying. | |
| 105 void CacheCommitData(EntityData* data); | |
| 106 | |
| 107 // Caches the a copy of |data_ptr|, which doesn't copy the data itself. | |
| 108 void CacheCommitData(const EntityDataPtr& data_ptr); | |
| 109 | |
| 110 // Check if the instance has cached commit data. | |
| 111 bool HasCommitData() const; | |
| 112 | |
| 113 // Check whether |data| matches the stored specifics hash. | |
| 114 bool MatchesData(const EntityData& data) const; | |
| 115 | |
| 116 // Check whether |data| matches the stored base (shared between client and | |
| 117 // server) specifics hash. | |
| 118 bool MatchesBaseData(const EntityData& data) const; | |
| 119 | |
| 120 // Increment sequence number in the metadata. This will also update the | |
| 121 // base_specifics_hash if the entity was not already unsynced. | |
| 122 void IncrementSequenceNumber(); | |
| 123 | |
| 124 private: | |
| 125 friend class ProcessorEntityTrackerTest; | |
| 126 | |
| 127 // The constructor swaps the data from the passed metadata. | |
| 128 ProcessorEntityTracker(const std::string& storage_key, | |
| 129 sync_pb::EntityMetadata* metadata); | |
| 130 | |
| 131 // Check whether |specifics| matches the stored specifics_hash. | |
| 132 bool MatchesSpecificsHash(const sync_pb::EntitySpecifics& specifics) const; | |
| 133 | |
| 134 // Update hash string for EntitySpecifics in the metadata. | |
| 135 void UpdateSpecificsHash(const sync_pb::EntitySpecifics& specifics); | |
| 136 | |
| 137 // Storage key. Should always be available. | |
| 138 const std::string storage_key_; | |
| 139 | |
| 140 // Serializable Sync metadata. | |
| 141 sync_pb::EntityMetadata metadata_; | |
| 142 | |
| 143 // Sync data that exists for items being committed only. | |
| 144 // The data is reset once commit confirmation is received. | |
| 145 EntityDataPtr commit_data_; | |
| 146 | |
| 147 // The sequence number of the last item sent to the sync thread. | |
| 148 int64_t commit_requested_sequence_number_; | |
| 149 }; | |
| 150 | |
| 151 } // namespace syncer | |
| 152 | |
| 153 #endif // COMPONENTS_SYNC_CORE_PROCESSOR_ENTITY_TRACKER_H_ | |
| OLD | NEW |