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 sync 18 // thread. 19 // 20 // It should be considered a helper class internal to the 21 // NonBlockingTypeProcessorCore. 22 // 23 // Maintains the state associated with a particular sync entity which is 24 // necessary for decision-making on the sync thread. It can track pending 25 // commit state, received update state, and can detect conflicts. 26 // 27 // This object may or may not contain state associated with a pending commit. 28 // If no commit is pending, the |is_commit_pending_| flag will be set to false 29 // and many of this object's fields will be cleared. 30 class SYNC_EXPORT SyncThreadSyncEntity { 31 public: 32 ~SyncThreadSyncEntity(); 33 34 // Initialize a new entity based on an update response. 35 static SyncThreadSyncEntity* FromServerUpdate( 36 const std::string& id_string, 37 const std::string& client_tag_hash, 38 int64 version); 39 40 // Initialize a new entity based on a commit request. 41 static SyncThreadSyncEntity* FromCommitRequest( 42 const std::string& id_string, 43 const std::string& client_tag_hash, 44 int64 sequence_number, 45 int64 base_version, 46 base::Time ctime, 47 base::Time mtime, 48 const std::string& non_unique_name, 49 bool deleted, 50 const sync_pb::EntitySpecifics& specifics); 51 52 // Returns true if this entity should be commited to the server. 53 bool IsCommitPending() const; 54 55 // Populates a sync_pb::SyncEntity for a commit. Also sets the 56 // |sequence_number|, so we can track it throughout the commit process. 57 void PrepareCommitProto(sync_pb::SyncEntity* commit_entity, 58 int64* sequence_number) const; 59 60 // Updates this entity with data from the latest version that the 61 // model asked us to commit. May clobber state related to the 62 // model's previous commit attempt(s). 63 void RequestCommit(const std::string& id, 64 const std::string& client_tag_hash, 65 int64 sequence_number, 66 int64 base_version, 67 base::Time ctime, 68 base::Time mtime, 69 const std::string& non_unique_name, 70 bool deleted, 71 const sync_pb::EntitySpecifics& specifics); 72 73 // Handles the receipt of a commit response. 74 // 75 // Since commits happen entirely on the sync thread, we can safely assume 76 // that our item's state at the end of the commit is the same as it was at 77 // the start. 78 void ReceiveCommitResponse(const std::string& response_id, 79 int64 response_version, 80 int64 sequence_number); 81 82 // Handles receipt of an update from the server. 83 void ReceiveUpdate(int64 version); 84 85 private: 86 // Initializes received update state. Does not initialize state related to 87 // pending commits and sets |is_commit_pending_| to false. 88 SyncThreadSyncEntity(const std::string& id, 89 const std::string& client_tag_hash, 90 int64 highest_commit_response_version, 91 int64 highest_gu_response_version); 92 93 // Initializes all fields. Sets |is_commit_pending_| to true. 94 SyncThreadSyncEntity(const std::string& id, 95 const std::string& client_tag_hash, 96 int64 highest_commit_response_version, 97 int64 highest_gu_response_version, 98 bool is_commit_pending, 99 int64 sequence_number, 100 int64 base_version, 101 base::Time ctime, 102 base::Time mtime, 103 const std::string& non_unique_name, 104 bool deleted, 105 const sync_pb::EntitySpecifics& specifics); 106 107 // Checks if the current state indicates a conflict. 108 // 109 // This can be true only while a call to this object is in progress. 110 // Conflicts are always cleared before the method call ends. 111 bool IsInConflict() const; 112 113 // Checks if the server knows about this item. 114 bool IsServerKnown() const; 115 116 // Clears flag and optionally clears state associated with a pending commit. 117 void ClearPendingCommit(); 118 119 // The ID for this entry. May be empty if the entry has never been committed. 120 std::string id_; 121 122 // The hashed client tag for this entry. 123 std::string client_tag_hash_; 124 125 // The highest version seen in a commit response for this entry. 126 int64 highest_commit_response_version_; 127 128 // The highest version seen in a GU response for this entry. 129 int64 highest_gu_response_version_; 130 131 // Flag that indicates whether or not we're waiting for a chance to commit 132 // this item. 133 bool is_commit_pending_; 134 135 // Used to track in-flight commit requests on the model thread. All we need 136 // to do here is return it back to the model thread when the pending commit 137 // is completed and confirmed. Not valid if no commit is pending. 138 int64 sequence_number_; 139 140 // The following fields are valid only when a commit is pending. 141 // This is where we store the data that is to be sent up to the server 142 // at the next possible opportunity. 143 int64 base_version_; 144 base::Time ctime_; 145 base::Time mtime_; 146 std::string non_unique_name_; 147 bool deleted_; 148 sync_pb::EntitySpecifics specifics_; 149 150 DISALLOW_COPY_AND_ASSIGN(SyncThreadSyncEntity); 151 }; 152 153 } // namespace syncer 154 155 #endif // SYNC_ENGINE_SYNC_THREAD_SYNC_ENTITY_H_ 156