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 CommitRequestData
;
18 struct UpdateResponseData
;
20 // Manages the pending commit and update state for an entity on the sync
23 // It should be considered a helper class internal to the
26 // Maintains the state associated with a particular sync entity which is
27 // necessary for decision-making on the sync thread. It can track pending
28 // commit state, received update state, and can detect conflicts.
30 // This object may contain state associated with a pending commit, pending
32 class SYNC_EXPORT EntityTracker
{
36 // Initialize a new entity based on an update response.
37 static scoped_ptr
<EntityTracker
> FromUpdateResponse(
38 const UpdateResponseData
& data
);
40 // Initialize a new entity based on a commit request.
41 static scoped_ptr
<EntityTracker
> FromCommitRequest(
42 const CommitRequestData
& data
);
44 // Returns true if this entity should be commited to the server.
45 bool HasPendingCommit() const;
47 // Populates a sync_pb::SyncEntity for a commit. Also sets the
48 // |sequence_number|, so we can track it throughout the commit process.
49 void PrepareCommitProto(sync_pb::SyncEntity
* commit_entity
,
50 int64
* sequence_number
) const;
52 // Updates this entity with data from the latest version that the
53 // model asked us to commit. May clobber state related to the
54 // model's previous commit attempt(s).
55 void RequestCommit(const CommitRequestData
& data
);
57 // Handles the receipt of a commit response.
59 // Since commits happen entirely on the sync thread, we can safely assume
60 // that our item's state at the end of the commit is the same as it was at
62 void ReceiveCommitResponse(const std::string
& response_id
,
63 int64 response_version
,
64 int64 sequence_number
);
66 // Handles receipt of an update from the server.
67 void ReceiveUpdate(int64 version
);
69 // Handles the receipt of an pending update from the server.
71 // Returns true if the tracker decides this item is worth keeping. Returns
72 // false if the item is discarded, which could happen if the version number
74 bool ReceivePendingUpdate(const UpdateResponseData
& data
);
76 // Functions to fetch the latest pending update.
77 bool HasPendingUpdate() const;
78 UpdateResponseData
GetPendingUpdate() const;
80 // Clears the pending update. Allows us to resume regular commit behavior.
81 void ClearPendingUpdate();
84 // Initializes received update state. Does not initialize state related to
85 // pending commits and sets |is_commit_pending_| to false.
86 EntityTracker(const std::string
& id
,
87 const std::string
& client_tag_hash
,
88 int64 highest_commit_response_version
,
89 int64 highest_gu_response_version
);
91 // Initializes all fields. Sets |is_commit_pending_| to true.
92 EntityTracker(const std::string
& id
,
93 const std::string
& client_tag_hash
,
94 int64 highest_commit_response_version
,
95 int64 highest_gu_response_version
,
96 bool is_commit_pending
,
97 int64 sequence_number
,
101 const std::string
& non_unique_name
,
103 const sync_pb::EntitySpecifics
& specifics
);
105 // Checks if the current state indicates a conflict.
107 // This can be true only while a call to this object is in progress.
108 // Conflicts are always cleared before the method call ends.
109 bool IsInConflict() const;
111 // Checks if the server knows about this item.
112 bool IsServerKnown() const;
114 // Clears flag and optionally clears state associated with a pending commit.
115 void ClearPendingCommit();
117 // The ID for this entry. May be empty if the entry has never been committed.
120 // The hashed client tag for this entry.
121 std::string client_tag_hash_
;
123 // The highest version seen in a commit response for this entry.
124 int64 highest_commit_response_version_
;
126 // The highest version seen in a GU response for this entry.
127 int64 highest_gu_response_version_
;
129 // Flag that indicates whether or not we're waiting for a chance to commit
131 bool is_commit_pending_
;
133 // Used to track in-flight commit requests on the model thread. All we need
134 // to do here is return it back to the model thread when the pending commit
135 // is completed and confirmed. Not valid if no commit is pending.
136 int64 sequence_number_
;
138 // The server version on which this item is based.
141 // A commit for this entity waiting for a sync cycle to be committed.
142 scoped_ptr
<CommitRequestData
> pending_commit_
;
144 // An update for this entity which can't be applied right now. The presence
145 // of an pending update prevents commits. As of this writing, the only
146 // source of pending updates is updates that can't currently be decrypted.
147 scoped_ptr
<UpdateResponseData
> pending_update_
;
149 DISALLOW_COPY_AND_ASSIGN(EntityTracker
);
152 } // namespace syncer
154 #endif // SYNC_ENGINE_ENTITY_TRACKER_H_