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/protocol/sync.pb.h"
17 struct UpdateResponseData
;
18 } // namespace syncer_v2
22 // Manages the pending commit and update state for an entity on the sync
25 // It should be considered a helper class internal to the
26 // ModelTypeSyncWorker.
28 // Maintains the state associated with a particular sync entity which is
29 // necessary for decision-making on the sync thread. It can track pending
30 // commit state, received update state, and can detect conflicts.
32 // This object may or may not contain state associated with a pending commit.
33 // If no commit is pending, the |is_commit_pending_| flag will be set to false
34 // and many of this object's fields will be cleared.
35 class SYNC_EXPORT EntityTracker
{
39 // Initialize a new entity based on an update response.
40 static scoped_ptr
<EntityTracker
> FromServerUpdate(
41 const std::string
& id_string
,
42 const std::string
& client_tag_hash
,
45 // Initialize a new entity based on a commit request.
46 static scoped_ptr
<EntityTracker
> FromCommitRequest(
47 const std::string
& id_string
,
48 const std::string
& client_tag_hash
,
49 int64 sequence_number
,
53 const std::string
& non_unique_name
,
55 const sync_pb::EntitySpecifics
& specifics
);
57 // Returns true if this entity should be commited to the server.
58 bool IsCommitPending() const;
60 // Populates a sync_pb::SyncEntity for a commit. Also sets the
61 // |sequence_number|, so we can track it throughout the commit process.
62 void PrepareCommitProto(sync_pb::SyncEntity
* commit_entity
,
63 int64
* sequence_number
) const;
65 // Updates this entity with data from the latest version that the
66 // model asked us to commit. May clobber state related to the
67 // model's previous commit attempt(s).
68 void RequestCommit(const std::string
& id
,
69 const std::string
& client_tag_hash
,
70 int64 sequence_number
,
74 const std::string
& non_unique_name
,
76 const sync_pb::EntitySpecifics
& specifics
);
78 // Handles the receipt of a commit response.
80 // Since commits happen entirely on the sync thread, we can safely assume
81 // that our item's state at the end of the commit is the same as it was at
83 void ReceiveCommitResponse(const std::string
& response_id
,
84 int64 response_version
,
85 int64 sequence_number
);
87 // Handles receipt of an update from the server.
88 void ReceiveUpdate(int64 version
);
90 // Handles the receipt of an pending update from the server.
92 // Returns true if the tracker decides this item is worth keeping. Returns
93 // false if the item is discarded, which could happen if the version number
95 bool ReceivePendingUpdate(const syncer_v2::UpdateResponseData
& data
);
97 // Functions to fetch the latest pending update.
98 bool HasPendingUpdate() const;
99 syncer_v2::UpdateResponseData
GetPendingUpdate() const;
101 // Clears the pending update. Allows us to resume regular commit behavior.
102 void ClearPendingUpdate();
105 // Initializes received update state. Does not initialize state related to
106 // pending commits and sets |is_commit_pending_| to false.
107 EntityTracker(const std::string
& id
,
108 const std::string
& client_tag_hash
,
109 int64 highest_commit_response_version
,
110 int64 highest_gu_response_version
);
112 // Initializes all fields. Sets |is_commit_pending_| to true.
113 EntityTracker(const std::string
& id
,
114 const std::string
& client_tag_hash
,
115 int64 highest_commit_response_version
,
116 int64 highest_gu_response_version
,
117 bool is_commit_pending
,
118 int64 sequence_number
,
122 const std::string
& non_unique_name
,
124 const sync_pb::EntitySpecifics
& specifics
);
126 // Checks if the current state indicates a conflict.
128 // This can be true only while a call to this object is in progress.
129 // Conflicts are always cleared before the method call ends.
130 bool IsInConflict() const;
132 // Checks if the server knows about this item.
133 bool IsServerKnown() const;
135 // Clears flag and optionally clears state associated with a pending commit.
136 void ClearPendingCommit();
138 // The ID for this entry. May be empty if the entry has never been committed.
141 // The hashed client tag for this entry.
142 std::string client_tag_hash_
;
144 // The highest version seen in a commit response for this entry.
145 int64 highest_commit_response_version_
;
147 // The highest version seen in a GU response for this entry.
148 int64 highest_gu_response_version_
;
150 // Flag that indicates whether or not we're waiting for a chance to commit
152 bool is_commit_pending_
;
154 // Used to track in-flight commit requests on the model thread. All we need
155 // to do here is return it back to the model thread when the pending commit
156 // is completed and confirmed. Not valid if no commit is pending.
157 int64 sequence_number_
;
159 // The following fields are valid only when a commit is pending.
160 // This is where we store the data that is to be sent up to the server
161 // at the next possible opportunity.
165 std::string non_unique_name_
;
167 sync_pb::EntitySpecifics specifics_
;
169 // An update for this item which can't be applied right now. The presence of
170 // an pending update prevents commits. As of this writing, the only source
171 // of pending updates is updates we can't decrypt right now.
172 scoped_ptr
<syncer_v2::UpdateResponseData
> pending_update_
;
174 DISALLOW_COPY_AND_ASSIGN(EntityTracker
);
177 } // namespace syncer
179 #endif // SYNC_ENGINE_ENTITY_TRACKER_H_