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.
5 #ifndef SYNC_ENGINE_MODEL_TYPE_ENTITY_H_
6 #define SYNC_ENGINE_MODEL_TYPE_ENTITY_H_
10 #include "base/memory/scoped_ptr.h"
11 #include "base/time/time.h"
12 #include "sync/base/sync_export.h"
13 #include "sync/protocol/sync.pb.h"
16 struct CommitRequestData
;
17 } // namespace syncer_v2
21 // This is the model thread's representation of a SyncEntity.
23 // The model type entity receives updates from the model itself and
24 // (asynchronously) from the sync server via the sync thread. From the point
25 // of view of this class, updates from the server take precedence over local
26 // changes, though the model may be given an opportunity to overwrite this
29 // Sync will try to commit this entity's data to the sync server and local
32 // Most of the logic related to those processes live outside this class. This
33 // class helps out a bit by offering some functions to serialize its data to
34 // various formats and query the entity's status.
35 class SYNC_EXPORT_PRIVATE ModelTypeEntity
{
37 // Construct an instance representing a new locally-created item.
38 static scoped_ptr
<ModelTypeEntity
> NewLocalItem(
39 const std::string
& client_tag
,
40 const sync_pb::EntitySpecifics
& specifics
,
43 // Construct an instance representing an item newly received from the server.
44 static scoped_ptr
<ModelTypeEntity
> FromServerUpdate(
45 const std::string
& id
,
46 const std::string
& client_tag_hash
,
47 const std::string
& non_unique_name
,
49 const sync_pb::EntitySpecifics
& specifics
,
53 const std::string
& encryption_key_name
);
55 // TODO(rlarocque): Implement FromDisk constructor when we implement storage.
59 // Returns true if this data is out of sync with local storage.
60 bool IsWriteRequired() const;
62 // Returns true if this data is out of sync with the server.
63 // A commit may or may not be in progress at this time.
64 bool IsUnsynced() const;
66 // Returns true if this data is out of sync with the sync thread.
68 // There may or may not be a commit in progress for this item, but there's
69 // definitely no commit in progress for this (most up to date) version of
71 bool RequiresCommitRequest() const;
73 // Returns true if the specified update version does not contain new data.
74 bool UpdateIsReflection(int64 update_version
) const;
76 // Returns true if the specified update version conflicts with local changes.
77 bool UpdateIsInConflict(int64 update_version
) const;
79 // Applies an update from the sync server.
81 // Overrides any local changes. Check UpdateIsInConflict() before calling
82 // this function if you want to handle conflicts differently.
83 void ApplyUpdateFromServer(int64 update_version
,
85 const sync_pb::EntitySpecifics
& specifics
,
87 const std::string
& encryption_key_name
);
89 // Applies a local change to this item.
90 void MakeLocalChange(const sync_pb::EntitySpecifics
& specifics
);
92 // Schedule a commit if the |name| does not match this item's last known
93 // encryption key. The worker that performs the commit is expected to
94 // encrypt the item using the latest available key.
95 void UpdateDesiredEncryptionKey(const std::string
& name
);
97 // Applies a local deletion to this item.
100 // Initializes a message representing this item's uncommitted state
101 // to be forwarded to the sync server for committing.
102 void InitializeCommitRequestData(syncer_v2::CommitRequestData
* request
) const;
104 // Notes that the current version of this item has been queued for commit.
105 void SetCommitRequestInProgress();
107 // Receives a successful commit response.
109 // Sucssful commit responses can overwrite an item's ID.
111 // Note that the receipt of a successful commit response does not necessarily
112 // unset IsUnsynced(). If many local changes occur in quick succession, it's
113 // possible that the committed item was already out of date by the time it
114 // reached the server.
115 void ReceiveCommitResponse(const std::string
& id
,
116 int64 sequence_number
,
117 int64 response_version
,
118 const std::string
& encryption_key_name
);
120 // Clears any in-memory sync state associated with outstanding commits.
121 void ClearTransientSyncState();
123 // Clears all sync state. Invoked when a user signs out.
124 void ClearSyncState();
127 ModelTypeEntity(int64 sequence_number
,
128 int64 commit_requested_sequence_number
,
129 int64 acked_sequence_number
,
132 const std::string
& id
,
133 const std::string
& client_tag_hash
,
134 const std::string
& non_unique_name
,
135 const sync_pb::EntitySpecifics
& specifics
,
139 const std::string
& encryption_key_name
);
141 // A sequence number used to track in-progress commits. Each local change
142 // increments this number.
143 int64 sequence_number_
;
145 // The sequence number of the last item sent to the sync thread.
146 int64 commit_requested_sequence_number_
;
148 // The sequence number of the last item known to be successfully committed.
149 int64 acked_sequence_number_
;
151 // The server version on which this item is based.
153 // If there are no local changes, this is the version of the entity as we see
156 // If there are local changes, this is the version of the entity on which
157 // those changes are based.
160 // True if this entity is out of sync with local storage.
165 // Most of the time, this is a server-assigned value.
167 // Prior to the item's first commit, we leave this value as an empty string.
168 // The initial ID for a newly created item has to meet certain uniqueness
169 // requirements, and we handle those on the sync thread.
172 // A hash based on the client tag and model type.
173 // Used for various map lookups. Should always be available.
174 std::string client_tag_hash_
;
176 // A non-unique name associated with this entity.
178 // It is sometimes used for debugging. It gets saved to and restored from
181 // Its value is often related to the item's unhashed client tag, though this
182 // is not guaranteed and should not be relied on. May be hidden when
183 // encryption is enabled.
184 std::string non_unique_name_
;
186 // A protobuf filled with type-specific information. Contains the most
187 // up-to-date specifics, whether it be from the server or a locally modified
189 sync_pb::EntitySpecifics specifics_
;
191 // Whether or not the item is deleted. The |specifics_| field may be empty
192 // if this flag is true.
195 // Entity creation and modification timestamps.
196 // Assigned by the client and synced by the server, though the server usually
197 // doesn't bother to inspect their values.
201 // The name of the encryption key used to encrypt this item on the server.
202 // Empty when no encryption is in use.
203 std::string encryption_key_name_
;
206 } // namespace syncer
208 #endif // SYNC_ENGINE_MODEL_TYPE_ENTITY_H_