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_ENGINE_MODEL_THREAD_SYNC_ENTITY_H_ |
| 6 #define SYNC_ENGINE_MODEL_THREAD_SYNC_ENTITY_H_ |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "base/time/time.h" |
| 11 #include "sync/base/sync_export.h" |
| 12 #include "sync/engine/non_blocking_sync_common.h" |
| 13 #include "sync/protocol/sync.pb.h" |
| 14 |
| 15 namespace syncer { |
| 16 |
| 17 // This is the model thread's representation of a SyncEntity. |
| 18 // |
| 19 // The model thread sync entity receives updates from the model itself and |
| 20 // (asynchronously) from the sync server via the sync thread. From the point |
| 21 // of view of this class, updates from the server take precedence over local |
| 22 // changes, though the model may be given an opportunity to overwrite this |
| 23 // decision later. |
| 24 // |
| 25 // Sync will try to commit this entity's data to the sync server and local |
| 26 // storage. |
| 27 // |
| 28 // Most of the logic related to those processes live outside this class. This |
| 29 // class helps out a bit by offering some functions to serialize its data to |
| 30 // various formats and query the entity's status. |
| 31 class SYNC_EXPORT_PRIVATE ModelThreadSyncEntity { |
| 32 public: |
| 33 // Construct an instance representing a new locally-created item. |
| 34 static ModelThreadSyncEntity* NewLocalItem( |
| 35 const std::string& client_tag, |
| 36 const sync_pb::EntitySpecifics& specifics, |
| 37 base::Time now); |
| 38 |
| 39 // Construct an instance representing an item newly received from the server. |
| 40 static ModelThreadSyncEntity* FromServerUpdate( |
| 41 const std::string& id, |
| 42 const std::string& client_tag_hash, |
| 43 const std::string& non_unique_name, |
| 44 int64 version, |
| 45 const sync_pb::EntitySpecifics& specifics, |
| 46 bool deleted, |
| 47 base::Time ctime, |
| 48 base::Time mtime); |
| 49 |
| 50 // TODO(rlarocque): Implement FromDisk constructor when we implement storage. |
| 51 |
| 52 ~ModelThreadSyncEntity(); |
| 53 |
| 54 // Returns true if this data is out of sync with local storage. |
| 55 bool IsWriteRequired() const; |
| 56 |
| 57 // Returns true if this data is out of sync with the server. |
| 58 // A commit may or may not be in progress at this time. |
| 59 bool IsUnsynced() const; |
| 60 |
| 61 // Returns true if this data is out of sync with the sync thread. |
| 62 // |
| 63 // There may or may not be a commit in progress for this item, but there's |
| 64 // definitely no commit in progress for this (most up to date) version of |
| 65 // this item. |
| 66 bool RequiresCommitRequest() const; |
| 67 |
| 68 // Returns true if the specified update version does not contain new data. |
| 69 bool UpdateIsReflection(int64 update_version) const; |
| 70 |
| 71 // Returns true if the specified update version conflicts with local changes. |
| 72 bool UpdateIsInConflict(int64 update_version) const; |
| 73 |
| 74 // Applies an update from the sync server. |
| 75 // |
| 76 // Overrides any local changes. Check UpdateIsInConflict() before calling |
| 77 // this function if you want to handle conflicts differently. |
| 78 void ApplyUpdateFromServer(int64 update_version, |
| 79 bool deleted, |
| 80 const sync_pb::EntitySpecifics& specifics, |
| 81 base::Time mtime); |
| 82 |
| 83 // Applies a local change to this item. |
| 84 void MakeLocalChange(const std::string& client_tag, |
| 85 const sync_pb::EntitySpecifics& specifics); |
| 86 |
| 87 // Applies a local deletion to this item. |
| 88 void Delete(const std::string& client_tag); |
| 89 |
| 90 // Initializes a message representing this item's uncommitted state |
| 91 // to be forwarded to the sync server for committing. |
| 92 void InitializeCommitRequestData(CommitRequestData* request) const; |
| 93 |
| 94 // Notes that the current version of this item has been queued for commit. |
| 95 void SetCommitRequestInProgress(); |
| 96 |
| 97 // Receives a successful commit response. |
| 98 // |
| 99 // Sucssful commit responses can overwrite an item's ID. |
| 100 // |
| 101 // Note that the receipt of a successful commit response does not necessarily |
| 102 // unset IsUnsynced(). If many local changes occur in quick succession, it's |
| 103 // possible that the committed item was already out of date by the time it |
| 104 // reached the server. |
| 105 void ReceiveCommitResponse(const std::string& id, |
| 106 int64 sequence_number, |
| 107 int64 response_version); |
| 108 |
| 109 private: |
| 110 ModelThreadSyncEntity(int64 sequence_number, |
| 111 int64 commit_requested_sequence_number, |
| 112 int64 acked_sequence_number, |
| 113 int64 base_version, |
| 114 bool is_dirty, |
| 115 const std::string& id, |
| 116 const std::string& client_tag, |
| 117 const std::string& client_tag_hash, |
| 118 const std::string& non_unique_name, |
| 119 const sync_pb::EntitySpecifics& specifics, |
| 120 bool deleted, |
| 121 base::Time ctime, |
| 122 base::Time mtime); |
| 123 |
| 124 // A sequence number used to track in-progress commits. Each local change |
| 125 // increments this number. |
| 126 int64 sequence_number_; |
| 127 |
| 128 // The sequence number of the last item sent to the sync thread. |
| 129 int64 commit_requested_sequence_number_; |
| 130 |
| 131 // The sequence number of the last item known to be successfully committed. |
| 132 int64 acked_sequence_number_; |
| 133 |
| 134 // The server version on which this item is based. |
| 135 // |
| 136 // If there are no local changes, this is the version of the entity as we see |
| 137 // it here. |
| 138 // |
| 139 // If there are local changes, this is the version of the entity on which |
| 140 // those changes are based. |
| 141 int64 base_version_; |
| 142 |
| 143 // True if this entity is out of sync with local storage. |
| 144 bool is_dirty_; |
| 145 |
| 146 // The entity's ID. |
| 147 // |
| 148 // Most of the time, this is a server-assigned value. |
| 149 // |
| 150 // Prior to the item's first commit, we leave this value as an empty string. |
| 151 // The initial ID for a newly created item has to meet certain uniqueness |
| 152 // requirements, and we handle those on the sync thread. |
| 153 std::string id_; |
| 154 |
| 155 // The client tag of this entity. |
| 156 // |
| 157 // There are some cases where we don't know this value for a while. It |
| 158 // should be equal to the non_unique_name_, but I'd prefer to not rely on |
| 159 // that assumption. |
| 160 // |
| 161 // Any updates we receive from the server will contain a hash based on this |
| 162 // value, but not hte client tag itself. We will leave this field empty |
| 163 // until the client specifies our client_tag. |
| 164 std::string client_tag_; |
| 165 |
| 166 // A hash based on the client tag and model type. |
| 167 // Used for various map lookups. Should always be available. |
| 168 std::string client_tag_hash_; |
| 169 |
| 170 // A non-unique name associated with this entity. |
| 171 // |
| 172 // It is sometimes used for debugging. Most of the time it's equal to the |
| 173 // client ID. We don't use it for much at the moment. Required by the sync |
| 174 // protocol, though as far as I know it doesn't do anything with the value. |
| 175 std::string non_unique_name_; |
| 176 |
| 177 // A protobuf filled with type-specific information. Contains the most |
| 178 // up-to-date specifics, whether it be from the server or a locally modified |
| 179 // version. |
| 180 sync_pb::EntitySpecifics specifics_; |
| 181 |
| 182 // Whether or not the item is deleted. The |specifics_| field may be empty |
| 183 // if this flag is true. |
| 184 bool deleted_; |
| 185 |
| 186 // Entity creation and modification timestamps. |
| 187 // Assigned by the client and synced by the server, though the server usually |
| 188 // doesn't bother to inspect their values. |
| 189 base::Time ctime_; |
| 190 base::Time mtime_; |
| 191 }; |
| 192 |
| 193 } // namespace syncer |
| 194 |
| 195 #endif // SYNC_ENGINE_MODEL_THREAD_SYNC_ENTITY_H_ |
OLD | NEW |