[SyncFS] Build indexes from FileTracker entries on disk.
[chromium-blink-merge.git] / sync / engine / entity_tracker.h
blobdb2d09bf3cf349841ecdb4102e5a30ebb234b043
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_
8 #include <string>
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"
15 namespace syncer {
17 // Manages the pending commit and update state for an entity on the sync
18 // thread.
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 {
31 public:
32 ~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,
37 int64 version);
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,
44 int64 base_version,
45 base::Time ctime,
46 base::Time mtime,
47 const std::string& non_unique_name,
48 bool deleted,
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,
65 int64 base_version,
66 base::Time ctime,
67 base::Time mtime,
68 const std::string& non_unique_name,
69 bool deleted,
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
76 // the start.
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);
84 private:
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,
99 int64 base_version,
100 base::Time ctime,
101 base::Time mtime,
102 const std::string& non_unique_name,
103 bool deleted,
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.
119 std::string id_;
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
131 // this item.
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.
142 int64 base_version_;
143 base::Time ctime_;
144 base::Time mtime_;
145 std::string non_unique_name_;
146 bool deleted_;
147 sync_pb::EntitySpecifics specifics_;
149 DISALLOW_COPY_AND_ASSIGN(EntityTracker);
152 } // namespace syncer
154 #endif // SYNC_ENGINE_ENTITY_TRACKER_H_