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_ENTITY_TRACKER_H_
6 #define SYNC_ENGINE_ENTITY_TRACKER_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 // ModelTypeSyncWorker.
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 EntityTracker
{
34 // Initialize a new entity based on an update response.
35 static EntityTracker
* FromServerUpdate(const std::string
& id_string
,
36 const std::string
& client_tag_hash
,
39 // Initialize a new entity based on a commit request.
40 static EntityTracker
* FromCommitRequest(
41 const std::string
& id_string
,
42 const std::string
& client_tag_hash
,
43 int64 sequence_number
,
47 const std::string
& non_unique_name
,
49 const sync_pb::EntitySpecifics
& specifics
);
51 // Returns true if this entity should be commited to the server.
52 bool IsCommitPending() const;
54 // Populates a sync_pb::SyncEntity for a commit. Also sets the
55 // |sequence_number|, so we can track it throughout the commit process.
56 void PrepareCommitProto(sync_pb::SyncEntity
* commit_entity
,
57 int64
* sequence_number
) const;
59 // Updates this entity with data from the latest version that the
60 // model asked us to commit. May clobber state related to the
61 // model's previous commit attempt(s).
62 void RequestCommit(const std::string
& id
,
63 const std::string
& client_tag_hash
,
64 int64 sequence_number
,
68 const std::string
& non_unique_name
,
70 const sync_pb::EntitySpecifics
& specifics
);
72 // Handles the receipt of a commit response.
74 // Since commits happen entirely on the sync thread, we can safely assume
75 // that our item's state at the end of the commit is the same as it was at
77 void ReceiveCommitResponse(const std::string
& response_id
,
78 int64 response_version
,
79 int64 sequence_number
);
81 // Handles receipt of an update from the server.
82 void ReceiveUpdate(int64 version
);
85 // Initializes received update state. Does not initialize state related to
86 // pending commits and sets |is_commit_pending_| to false.
87 EntityTracker(const std::string
& id
,
88 const std::string
& client_tag_hash
,
89 int64 highest_commit_response_version
,
90 int64 highest_gu_response_version
);
92 // Initializes all fields. Sets |is_commit_pending_| to true.
93 EntityTracker(const std::string
& id
,
94 const std::string
& client_tag_hash
,
95 int64 highest_commit_response_version
,
96 int64 highest_gu_response_version
,
97 bool is_commit_pending
,
98 int64 sequence_number
,
102 const std::string
& non_unique_name
,
104 const sync_pb::EntitySpecifics
& specifics
);
106 // Checks if the current state indicates a conflict.
108 // This can be true only while a call to this object is in progress.
109 // Conflicts are always cleared before the method call ends.
110 bool IsInConflict() const;
112 // Checks if the server knows about this item.
113 bool IsServerKnown() const;
115 // Clears flag and optionally clears state associated with a pending commit.
116 void ClearPendingCommit();
118 // The ID for this entry. May be empty if the entry has never been committed.
121 // The hashed client tag for this entry.
122 std::string client_tag_hash_
;
124 // The highest version seen in a commit response for this entry.
125 int64 highest_commit_response_version_
;
127 // The highest version seen in a GU response for this entry.
128 int64 highest_gu_response_version_
;
130 // Flag that indicates whether or not we're waiting for a chance to commit
132 bool is_commit_pending_
;
134 // Used to track in-flight commit requests on the model thread. All we need
135 // to do here is return it back to the model thread when the pending commit
136 // is completed and confirmed. Not valid if no commit is pending.
137 int64 sequence_number_
;
139 // The following fields are valid only when a commit is pending.
140 // This is where we store the data that is to be sent up to the server
141 // at the next possible opportunity.
145 std::string non_unique_name_
;
147 sync_pb::EntitySpecifics specifics_
;
149 DISALLOW_COPY_AND_ASSIGN(EntityTracker
);
152 } // namespace syncer
154 #endif // SYNC_ENGINE_ENTITY_TRACKER_H_