Chromium Code Reviews| 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( | |
|
Nicolas Zea
2014/05/20 21:51:08
these should both return scoped_ptr's
| |
| 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 sync_pb::EntitySpecifics& specifics); | |
| 85 | |
| 86 // Applies a local deletion to this item. | |
| 87 void Delete(); | |
| 88 | |
| 89 // Initializes a message representing this item's uncommitted state | |
| 90 // to be forwarded to the sync server for committing. | |
| 91 void InitializeCommitRequestData(CommitRequestData* request) const; | |
| 92 | |
| 93 // Notes that the current version of this item has been queued for commit. | |
| 94 void SetCommitRequestInProgress(); | |
| 95 | |
| 96 // Receives a successful commit response. | |
| 97 // | |
| 98 // Sucssful commit responses can overwrite an item's ID. | |
| 99 // | |
| 100 // Note that the receipt of a successful commit response does not necessarily | |
| 101 // unset IsUnsynced(). If many local changes occur in quick succession, it's | |
| 102 // possible that the committed item was already out of date by the time it | |
| 103 // reached the server. | |
| 104 void ReceiveCommitResponse(const std::string& id, | |
| 105 int64 sequence_number, | |
| 106 int64 response_version); | |
| 107 | |
| 108 private: | |
| 109 ModelThreadSyncEntity(int64 sequence_number, | |
| 110 int64 commit_requested_sequence_number, | |
| 111 int64 acked_sequence_number, | |
| 112 int64 base_version, | |
| 113 bool is_dirty, | |
| 114 const std::string& id, | |
| 115 const std::string& client_tag_hash, | |
| 116 const std::string& non_unique_name, | |
| 117 const sync_pb::EntitySpecifics& specifics, | |
| 118 bool deleted, | |
| 119 base::Time ctime, | |
| 120 base::Time mtime); | |
| 121 | |
| 122 // A sequence number used to track in-progress commits. Each local change | |
| 123 // increments this number. | |
| 124 int64 sequence_number_; | |
| 125 | |
| 126 // The sequence number of the last item sent to the sync thread. | |
| 127 int64 commit_requested_sequence_number_; | |
| 128 | |
| 129 // The sequence number of the last item known to be successfully committed. | |
| 130 int64 acked_sequence_number_; | |
| 131 | |
| 132 // The server version on which this item is based. | |
| 133 // | |
| 134 // If there are no local changes, this is the version of the entity as we see | |
| 135 // it here. | |
| 136 // | |
| 137 // If there are local changes, this is the version of the entity on which | |
| 138 // those changes are based. | |
| 139 int64 base_version_; | |
| 140 | |
| 141 // True if this entity is out of sync with local storage. | |
| 142 bool is_dirty_; | |
| 143 | |
| 144 // The entity's ID. | |
| 145 // | |
| 146 // Most of the time, this is a server-assigned value. | |
| 147 // | |
| 148 // Prior to the item's first commit, we leave this value as an empty string. | |
| 149 // The initial ID for a newly created item has to meet certain uniqueness | |
| 150 // requirements, and we handle those on the sync thread. | |
| 151 std::string id_; | |
| 152 | |
| 153 // A hash based on the client tag and model type. | |
| 154 // Used for various map lookups. Should always be available. | |
| 155 std::string client_tag_hash_; | |
| 156 | |
| 157 // A non-unique name associated with this entity. | |
| 158 // | |
| 159 // It is sometimes used for debugging. Most of the time it's equal to the | |
| 160 // client tag. We don't use it for much at the moment. Required by the sync | |
| 161 // protocol, though as far as I know it doesn't do anything with the value. | |
| 162 std::string non_unique_name_; | |
| 163 | |
| 164 // A protobuf filled with type-specific information. Contains the most | |
| 165 // up-to-date specifics, whether it be from the server or a locally modified | |
| 166 // version. | |
| 167 sync_pb::EntitySpecifics specifics_; | |
| 168 | |
| 169 // Whether or not the item is deleted. The |specifics_| field may be empty | |
| 170 // if this flag is true. | |
| 171 bool deleted_; | |
| 172 | |
| 173 // Entity creation and modification timestamps. | |
| 174 // Assigned by the client and synced by the server, though the server usually | |
| 175 // doesn't bother to inspect their values. | |
| 176 base::Time ctime_; | |
| 177 base::Time mtime_; | |
| 178 }; | |
| 179 | |
| 180 } // namespace syncer | |
| 181 | |
| 182 #endif // SYNC_ENGINE_MODEL_THREAD_SYNC_ENTITY_H_ | |
| OLD | NEW |