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