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_SYNC_THREAD_SYNC_ENTITY_H_ |
| 6 #define SYNC_ENGINE_SYNC_THREAD_SYNC_ENTITY_H_ |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "base/basictypes.h" |
| 11 #include "base/time/time.h" |
| 12 #include "sync/base/sync_export.h" |
| 13 #include "sync/protocol/sync.pb.h" |
| 14 |
| 15 namespace syncer { |
| 16 |
| 17 // Manages the pending commit and update state for an entity on the syncthread. |
| 18 // |
| 19 // This object maintains the state associated with a particular sync entity |
| 20 // which are required to make decisions about the entity on the sync thread. |
| 21 // The sync thread's data is ephemeral and always out of date, since the "true" |
| 22 // local state lives on the model thread and the "true" server state lives on |
| 23 // the server. It uses counters and sequence numbers to detect and manage |
| 24 // conflicts between the sources of truth and its cached data. |
| 25 class SYNC_EXPORT SyncThreadSyncEntity { |
| 26 public: |
| 27 ~SyncThreadSyncEntity(); |
| 28 |
| 29 static SyncThreadSyncEntity* FromServerUpdate( |
| 30 const std::string& id_string, |
| 31 const std::string& client_tag_hash, |
| 32 int64 version); |
| 33 static SyncThreadSyncEntity* FromCommitRequest( |
| 34 const std::string& id_string, |
| 35 const std::string& client_tag_hash, |
| 36 int64 sequence_number, |
| 37 int64 base_version, |
| 38 base::Time ctime, |
| 39 base::Time mtime, |
| 40 const std::string& non_unique_name, |
| 41 bool deleted, |
| 42 const sync_pb::EntitySpecifics& specifics); |
| 43 |
| 44 bool IsCommitPending() const; |
| 45 |
| 46 void PrepareCommitProto(sync_pb::SyncEntity* commit_entity, |
| 47 int64* sequence_number) const; |
| 48 |
| 49 void RequestCommit(const std::string& id, |
| 50 const std::string& client_tag_hash, |
| 51 int64 sequence_number, |
| 52 int64 base_version, |
| 53 base::Time ctime, |
| 54 base::Time mtime, |
| 55 const std::string& non_unique_name, |
| 56 bool deleted, |
| 57 const sync_pb::EntitySpecifics& specifics); |
| 58 void ReceiveCommitResponse(int64 version, int64 sequence_number); |
| 59 |
| 60 void ReceiveUpdate(int64 version); |
| 61 |
| 62 sync_pb::SyncEntity& GetCurrentSpecifics(); |
| 63 |
| 64 private: |
| 65 // Constructor that doesn't bother to initialize any pending commit fields. |
| 66 SyncThreadSyncEntity(const std::string& id, |
| 67 const std::string& client_tag_hash, |
| 68 int64 highest_commit_response_version, |
| 69 int64 highest_gu_response_version); |
| 70 |
| 71 // Constructor that initializes all fields. |
| 72 SyncThreadSyncEntity(const std::string& id, |
| 73 const std::string& client_tag_hash, |
| 74 int64 highest_commit_response_version, |
| 75 int64 highest_gu_response_version, |
| 76 bool is_commit_pending, |
| 77 int64 sequence_number, |
| 78 int64 base_version, |
| 79 base::Time ctime, |
| 80 base::Time mtime, |
| 81 const std::string& non_unique_name, |
| 82 bool deleted, |
| 83 const sync_pb::EntitySpecifics& specifics); |
| 84 |
| 85 // Checks if the current state indicates a conflict. |
| 86 // |
| 87 // This can be true only while a call to this object is in progress. |
| 88 // Conflicts are always cleared before the method call ends. |
| 89 bool IsInConflict() const; |
| 90 |
| 91 // Clears flag and optionally clears state associated with a pending commit. |
| 92 void ClearPendingCommit(); |
| 93 |
| 94 // The ID for this entry. May be empty if the entry has never been committed. |
| 95 std::string id_; |
| 96 |
| 97 // The hashed client tag for this entry. |
| 98 std::string client_tag_hash_; |
| 99 |
| 100 // The highest version seen in a commit response for this entry. |
| 101 int64 highest_commit_response_version_; |
| 102 |
| 103 // The highest version seen in a GU response for this entry. |
| 104 int64 highest_gu_response_version_; |
| 105 |
| 106 // Flag that indicates whether or not we're waiting for a chance to commit |
| 107 // this item. |
| 108 bool is_commit_pending_; |
| 109 |
| 110 // Used to track in-flight commit requests on the model thread. All we need |
| 111 // to do here is return it back to the model thread when the pending commit |
| 112 // is completed and confirmed. Not valid if no commit is pending. |
| 113 int64 sequence_number_; |
| 114 |
| 115 // The following fields are valid only when a commit is pending. |
| 116 // This is where we store the data that is to be sent up to the server |
| 117 // at the next possible opportunity. |
| 118 int64 base_version_; |
| 119 base::Time ctime_; |
| 120 base::Time mtime_; |
| 121 std::string non_unique_name_; |
| 122 bool deleted_; |
| 123 sync_pb::EntitySpecifics specifics_; |
| 124 |
| 125 DISALLOW_COPY_AND_ASSIGN(SyncThreadSyncEntity); |
| 126 }; |
| 127 |
| 128 } // namespace syncer |
| 129 |
| 130 #endif // SYNC_ENGINE_SYNC_THREAD_SYNC_ENTITY_H_ |
OLD | NEW |