OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "sync/engine/entity_tracker.h" | 5 #include "sync/engine/entity_tracker.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "sync/internal_api/public/base/model_type.h" | 8 #include "sync/internal_api/public/base/model_type.h" |
9 #include "sync/internal_api/public/non_blocking_sync_common.h" | 9 #include "sync/internal_api/public/non_blocking_sync_common.h" |
10 #include "sync/syncable/syncable_util.h" | 10 #include "sync/syncable/syncable_util.h" |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 highest_commit_response_version_ = response_version; | 186 highest_commit_response_version_ = response_version; |
187 | 187 |
188 // Because an in-progress commit blocks the sync thread, we can assume that | 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. | 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 | 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. | 191 // sync with the server, we can clear the pending commit. |
192 ClearPendingCommit(); | 192 ClearPendingCommit(); |
193 } | 193 } |
194 | 194 |
195 void EntityTracker::ReceiveUpdate(int64 version) { | 195 void EntityTracker::ReceiveUpdate(int64 version) { |
196 if (version <= highest_gu_response_version_) | 196 highest_gu_response_version_ = |
197 return; | 197 std::max(highest_gu_response_version_, version); |
198 | |
199 highest_gu_response_version_ = version; | |
200 | |
201 // Got an applicable update newer than any pending updates. It must be safe | |
202 // to discard the old pending update, if there was one. | |
203 ClearPendingUpdate(); | |
204 | 198 |
205 if (IsInConflict()) { | 199 if (IsInConflict()) { |
206 // Incoming update clobbers the pending commit on the sync thread. | 200 // Incoming update clobbers the pending commit on the sync thread. |
207 // The model thread can re-request this commit later if it wants to. | 201 // The model thread can re-request this commit later if it wants to. |
208 ClearPendingCommit(); | 202 ClearPendingCommit(); |
209 } | 203 } |
210 } | 204 } |
211 | 205 |
212 bool EntityTracker::ReceivePendingUpdate(const UpdateResponseData& data) { | |
213 if (data.response_version < highest_gu_response_version_) | |
214 return false; | |
215 | |
216 highest_gu_response_version_ = data.response_version; | |
217 pending_update_.reset(new UpdateResponseData(data)); | |
218 ClearPendingCommit(); | |
219 return true; | |
220 } | |
221 | |
222 bool EntityTracker::HasPendingUpdate() const { | |
223 return !!pending_update_; | |
224 } | |
225 | |
226 UpdateResponseData EntityTracker::GetPendingUpdate() const { | |
227 return *pending_update_; | |
228 } | |
229 | |
230 void EntityTracker::ClearPendingUpdate() { | |
231 pending_update_.reset(); | |
232 } | |
233 | |
234 bool EntityTracker::IsInConflict() const { | 206 bool EntityTracker::IsInConflict() const { |
235 if (!is_commit_pending_) | 207 if (!is_commit_pending_) |
236 return false; | 208 return false; |
237 | 209 |
238 if (HasPendingUpdate()) | |
239 return true; | |
240 | |
241 if (highest_gu_response_version_ <= highest_commit_response_version_) { | 210 if (highest_gu_response_version_ <= highest_commit_response_version_) { |
242 // The most recent server state was created in a commit made by this | 211 // The most recent server state was created in a commit made by this |
243 // client. We're fully up to date, and therefore not in conflict. | 212 // client. We're fully up to date, and therefore not in conflict. |
244 return false; | 213 return false; |
245 } else { | 214 } else { |
246 // The most recent server state was written by someone else. | 215 // The most recent server state was written by someone else. |
247 // Did the model thread have the most up to date version when it issued the | 216 // Did the model thread have the most up to date version when it issued the |
248 // commit request? | 217 // commit request? |
249 if (base_version_ >= highest_gu_response_version_) { | 218 if (base_version_ >= highest_gu_response_version_) { |
250 return false; // Yes. | 219 return false; // Yes. |
251 } else { | 220 } else { |
252 return true; // No. | 221 return true; // No. |
253 } | 222 } |
254 } | 223 } |
255 } | 224 } |
256 | 225 |
257 bool EntityTracker::IsServerKnown() const { | 226 bool EntityTracker::IsServerKnown() const { |
258 return base_version_ != kUncommittedVersion; | 227 return base_version_ != kUncommittedVersion; |
259 } | 228 } |
260 | 229 |
261 void EntityTracker::ClearPendingCommit() { | 230 void EntityTracker::ClearPendingCommit() { |
262 is_commit_pending_ = false; | 231 is_commit_pending_ = false; |
263 | 232 |
264 // Clearing the specifics might free up some memory. It can't hurt to try. | 233 // Clearing the specifics might free up some memory. It can't hurt to try. |
265 specifics_.Clear(); | 234 specifics_.Clear(); |
266 } | 235 } |
267 | 236 |
268 } // namespace syncer | 237 } // namespace syncer |
OLD | NEW |