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_SYNC_THREAD_SYNC_ENTITY_H_
6 #define SYNC_ENGINE_SYNC_THREAD_SYNC_ENTITY_H_
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"
17 // Manages the pending commit and update state for an entity on the sync
20 // It should be considered a helper class internal to the
21 // NonBlockingTypeProcessorCore.
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.
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 {
32 ~SyncThreadSyncEntity();
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,
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,
48 const std::string& non_unique_name,
50 const sync_pb::EntitySpecifics& specifics);
52 // Returns true if this entity should be commited to the server.
53 bool IsCommitPending() const;
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;
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,
69 const std::string& non_unique_name,
71 const sync_pb::EntitySpecifics& specifics);
73 // Handles the receipt of a commit response.
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
78 void ReceiveCommitResponse(const std::string& response_id,
79 int64 response_version,
80 int64 sequence_number);
82 // Handles receipt of an update from the server.
83 void ReceiveUpdate(int64 version);
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);
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,
103 const std::string& non_unique_name,
105 const sync_pb::EntitySpecifics& specifics);
107 // Checks if the current state indicates a conflict.
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;
113 // Checks if the server knows about this item.
114 bool IsServerKnown() const;
116 // Clears flag and optionally clears state associated with a pending commit.
117 void ClearPendingCommit();
119 // The ID for this entry. May be empty if the entry has never been committed.
122 // The hashed client tag for this entry.
123 std::string client_tag_hash_;
125 // The highest version seen in a commit response for this entry.
126 int64 highest_commit_response_version_;
128 // The highest version seen in a GU response for this entry.
129 int64 highest_gu_response_version_;
131 // Flag that indicates whether or not we're waiting for a chance to commit
133 bool is_commit_pending_;
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_;
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.
146 std::string non_unique_name_;
148 sync_pb::EntitySpecifics specifics_;
150 DISALLOW_COPY_AND_ASSIGN(SyncThreadSyncEntity);
153 } // namespace syncer
155 #endif // SYNC_ENGINE_SYNC_THREAD_SYNC_ENTITY_H_