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/memory/scoped_ptr.h"
12 #include "base/time/time.h"
13 #include "sync/base/sync_export.h"
14 #include "sync/internal_api/public/non_blocking_sync_common.h"
15 #include "sync/protocol/sync.pb.h"
19 // Manages the pending commit and update state for an entity on the sync
22 // It should be considered a helper class internal to the
23 // ModelTypeSyncWorker.
25 // Maintains the state associated with a particular sync entity which is
26 // necessary for decision-making on the sync thread. It can track pending
27 // commit state, received update state, and can detect conflicts.
29 // This object may or may not contain state associated with a pending commit.
30 // If no commit is pending, the |is_commit_pending_| flag will be set to false
31 // and many of this object's fields will be cleared.
32 class SYNC_EXPORT EntityTracker
{
36 // Initialize a new entity based on an update response.
37 static EntityTracker
* FromServerUpdate(const std::string
& id_string
,
38 const std::string
& client_tag_hash
,
41 // Initialize a new entity based on a commit request.
42 static EntityTracker
* FromCommitRequest(
43 const std::string
& id_string
,
44 const std::string
& client_tag_hash
,
45 int64 sequence_number
,
49 const std::string
& non_unique_name
,
51 const sync_pb::EntitySpecifics
& specifics
);
53 // Returns true if this entity should be commited to the server.
54 bool IsCommitPending() const;
56 // Populates a sync_pb::SyncEntity for a commit. Also sets the
57 // |sequence_number|, so we can track it throughout the commit process.
58 void PrepareCommitProto(sync_pb::SyncEntity
* commit_entity
,
59 int64
* sequence_number
) const;
61 // Updates this entity with data from the latest version that the
62 // model asked us to commit. May clobber state related to the
63 // model's previous commit attempt(s).
64 void RequestCommit(const std::string
& id
,
65 const std::string
& client_tag_hash
,
66 int64 sequence_number
,
70 const std::string
& non_unique_name
,
72 const sync_pb::EntitySpecifics
& specifics
);
74 // Handles the receipt of a commit response.
76 // Since commits happen entirely on the sync thread, we can safely assume
77 // that our item's state at the end of the commit is the same as it was at
79 void ReceiveCommitResponse(const std::string
& response_id
,
80 int64 response_version
,
81 int64 sequence_number
);
83 // Handles receipt of an update from the server.
84 void ReceiveUpdate(int64 version
);
86 // Handles the receipt of an pending update from the server.
88 // Returns true if the tracker decides this item is worth keeping. Returns
89 // false if the item is discarded, which could happen if the version number
91 bool ReceivePendingUpdate(const UpdateResponseData
& data
);
93 // Functions to fetch the latest pending update.
94 bool HasPendingUpdate() const;
95 UpdateResponseData
GetPendingUpdate() const;
97 // Clears the pending update. Allows us to resume regular commit behavior.
98 void ClearPendingUpdate();
101 // Initializes received update state. Does not initialize state related to
102 // pending commits and sets |is_commit_pending_| to false.
103 EntityTracker(const std::string
& id
,
104 const std::string
& client_tag_hash
,
105 int64 highest_commit_response_version
,
106 int64 highest_gu_response_version
);
108 // Initializes all fields. Sets |is_commit_pending_| to true.
109 EntityTracker(const std::string
& id
,
110 const std::string
& client_tag_hash
,
111 int64 highest_commit_response_version
,
112 int64 highest_gu_response_version
,
113 bool is_commit_pending
,
114 int64 sequence_number
,
118 const std::string
& non_unique_name
,
120 const sync_pb::EntitySpecifics
& specifics
);
122 // Checks if the current state indicates a conflict.
124 // This can be true only while a call to this object is in progress.
125 // Conflicts are always cleared before the method call ends.
126 bool IsInConflict() const;
128 // Checks if the server knows about this item.
129 bool IsServerKnown() const;
131 // Clears flag and optionally clears state associated with a pending commit.
132 void ClearPendingCommit();
134 // The ID for this entry. May be empty if the entry has never been committed.
137 // The hashed client tag for this entry.
138 std::string client_tag_hash_
;
140 // The highest version seen in a commit response for this entry.
141 int64 highest_commit_response_version_
;
143 // The highest version seen in a GU response for this entry.
144 int64 highest_gu_response_version_
;
146 // Flag that indicates whether or not we're waiting for a chance to commit
148 bool is_commit_pending_
;
150 // Used to track in-flight commit requests on the model thread. All we need
151 // to do here is return it back to the model thread when the pending commit
152 // is completed and confirmed. Not valid if no commit is pending.
153 int64 sequence_number_
;
155 // The following fields are valid only when a commit is pending.
156 // This is where we store the data that is to be sent up to the server
157 // at the next possible opportunity.
161 std::string non_unique_name_
;
163 sync_pb::EntitySpecifics specifics_
;
165 // An update for this item which can't be applied right now. The presence of
166 // an pending update prevents commits. As of this writing, the only source
167 // of pending updates is updates we can't decrypt right now.
168 scoped_ptr
<UpdateResponseData
> pending_update_
;
170 DISALLOW_COPY_AND_ASSIGN(EntityTracker
);
173 } // namespace syncer
175 #endif // SYNC_ENGINE_ENTITY_TRACKER_H_