Revert of sync: Add non-blocking type encryption support (https://codereview.chromium...
[chromium-blink-merge.git] / sync / engine / entity_tracker.cc
blob0f10906ab15c67fc253b0e24c2115d650c0ef4b0
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 #include "sync/engine/entity_tracker.h"
7 #include "base/logging.h"
8 #include "sync/internal_api/public/base/model_type.h"
9 #include "sync/internal_api/public/non_blocking_sync_common.h"
10 #include "sync/syncable/syncable_util.h"
11 #include "sync/util/time.h"
13 namespace syncer {
15 EntityTracker* EntityTracker::FromServerUpdate(
16 const std::string& id_string,
17 const std::string& client_tag_hash,
18 int64 received_version) {
19 return new EntityTracker(id_string, client_tag_hash, 0, received_version);
22 EntityTracker* EntityTracker::FromCommitRequest(
23 const std::string& id_string,
24 const std::string& client_tag_hash,
25 int64 sequence_number,
26 int64 base_version,
27 base::Time ctime,
28 base::Time mtime,
29 const std::string& non_unique_name,
30 bool deleted,
31 const sync_pb::EntitySpecifics& specifics) {
32 return new EntityTracker(id_string,
33 client_tag_hash,
36 true,
37 sequence_number,
38 base_version,
39 ctime,
40 mtime,
41 non_unique_name,
42 deleted,
43 specifics);
46 // Constructor that does not set any pending commit fields.
47 EntityTracker::EntityTracker(const std::string& id,
48 const std::string& client_tag_hash,
49 int64 highest_commit_response_version,
50 int64 highest_gu_response_version)
51 : id_(id),
52 client_tag_hash_(client_tag_hash),
53 highest_commit_response_version_(highest_commit_response_version),
54 highest_gu_response_version_(highest_gu_response_version),
55 is_commit_pending_(false),
56 sequence_number_(0),
57 base_version_(0),
58 deleted_(false) {
61 EntityTracker::EntityTracker(const std::string& id,
62 const std::string& client_tag_hash,
63 int64 highest_commit_response_version,
64 int64 highest_gu_response_version,
65 bool is_commit_pending,
66 int64 sequence_number,
67 int64 base_version,
68 base::Time ctime,
69 base::Time mtime,
70 const std::string& non_unique_name,
71 bool deleted,
72 const sync_pb::EntitySpecifics& specifics)
73 : id_(id),
74 client_tag_hash_(client_tag_hash),
75 highest_commit_response_version_(highest_commit_response_version),
76 highest_gu_response_version_(highest_gu_response_version),
77 is_commit_pending_(is_commit_pending),
78 sequence_number_(sequence_number),
79 base_version_(base_version),
80 ctime_(ctime),
81 mtime_(mtime),
82 non_unique_name_(non_unique_name),
83 deleted_(deleted),
84 specifics_(specifics) {
87 EntityTracker::~EntityTracker() {
90 bool EntityTracker::IsCommitPending() const {
91 return is_commit_pending_;
94 void EntityTracker::PrepareCommitProto(sync_pb::SyncEntity* commit_entity,
95 int64* sequence_number) const {
96 // Set ID if we have a server-assigned ID. Otherwise, it will be up to
97 // our caller to assign a client-unique initial ID.
98 if (base_version_ != kUncommittedVersion) {
99 commit_entity->set_id_string(id_);
102 commit_entity->set_client_defined_unique_tag(client_tag_hash_);
103 commit_entity->set_version(base_version_);
104 commit_entity->set_deleted(deleted_);
105 commit_entity->set_folder(false);
106 commit_entity->set_name(non_unique_name_);
107 if (!deleted_) {
108 commit_entity->set_ctime(TimeToProtoTime(ctime_));
109 commit_entity->set_mtime(TimeToProtoTime(mtime_));
110 commit_entity->mutable_specifics()->CopyFrom(specifics_);
113 *sequence_number = sequence_number_;
116 void EntityTracker::RequestCommit(const std::string& id,
117 const std::string& client_tag_hash,
118 int64 sequence_number,
119 int64 base_version,
120 base::Time ctime,
121 base::Time mtime,
122 const std::string& non_unique_name,
123 bool deleted,
124 const sync_pb::EntitySpecifics& specifics) {
125 DCHECK_GE(base_version, base_version_)
126 << "Base version should never decrease";
128 DCHECK_GE(sequence_number, sequence_number_)
129 << "Sequence number should never decrease";
131 // Update our book-keeping counters.
132 base_version_ = base_version;
133 sequence_number_ = sequence_number;
135 // Do our counter values indicate a conflict? If so, don't commit.
137 // There's no need to inform the model thread of the conflict. The
138 // conflicting update has already been posted to its task runner; it will
139 // figure it out as soon as it runs that task.
140 is_commit_pending_ = true;
141 if (IsInConflict()) {
142 ClearPendingCommit();
143 return;
146 // We don't commit deletions of server-unknown items.
147 if (deleted && !IsServerKnown()) {
148 ClearPendingCommit();
149 return;
152 // Otherwise, we should store the data associated with this pending commit
153 // so we're ready to commit at the next possible opportunity.
155 // We intentionally don't update the id_ here. Good ID values come from the
156 // server and always pass through the sync thread first. There's no way the
157 // model thread could have a better ID value than we do.
159 // This entity is identified by its client tag. That value can never change.
160 DCHECK_EQ(client_tag_hash_, client_tag_hash);
162 // Set the fields for the pending commit.
163 ctime_ = ctime;
164 mtime_ = mtime;
165 non_unique_name_ = non_unique_name;
166 deleted_ = deleted;
167 specifics_ = specifics;
170 void EntityTracker::ReceiveCommitResponse(const std::string& response_id,
171 int64 response_version,
172 int64 sequence_number) {
173 // Commit responses, especially after the first commit, can update our ID.
174 id_ = response_id;
176 DCHECK_GT(response_version, highest_commit_response_version_)
177 << "Had expected higher response version."
178 << " id: " << id_;
180 // Commits are synchronous, so there's no reason why the sequence numbers
181 // wouldn't match.
182 DCHECK_EQ(sequence_number_, sequence_number)
183 << "Unexpected sequence number mismatch."
184 << " id: " << id_;
186 highest_commit_response_version_ = response_version;
188 // Because an in-progress commit blocks the sync thread, we can assume that
189 // the item we just committed successfully is exactly the one we have now.
190 // Nothing changed it while the commit was happening. Since we're now in
191 // sync with the server, we can clear the pending commit.
192 ClearPendingCommit();
195 void EntityTracker::ReceiveUpdate(int64 version) {
196 highest_gu_response_version_ =
197 std::max(highest_gu_response_version_, version);
199 if (IsInConflict()) {
200 // Incoming update clobbers the pending commit on the sync thread.
201 // The model thread can re-request this commit later if it wants to.
202 ClearPendingCommit();
206 bool EntityTracker::IsInConflict() const {
207 if (!is_commit_pending_)
208 return false;
210 if (highest_gu_response_version_ <= highest_commit_response_version_) {
211 // The most recent server state was created in a commit made by this
212 // client. We're fully up to date, and therefore not in conflict.
213 return false;
214 } else {
215 // The most recent server state was written by someone else.
216 // Did the model thread have the most up to date version when it issued the
217 // commit request?
218 if (base_version_ >= highest_gu_response_version_) {
219 return false; // Yes.
220 } else {
221 return true; // No.
226 bool EntityTracker::IsServerKnown() const {
227 return base_version_ != kUncommittedVersion;
230 void EntityTracker::ClearPendingCommit() {
231 is_commit_pending_ = false;
233 // Clearing the specifics might free up some memory. It can't hurt to try.
234 specifics_.Clear();
237 } // namespace syncer