Add ICU message format support
[chromium-blink-merge.git] / sync / engine / entity_tracker.h
blob8d40c914c223e9260072da5256e63d01315dea0a
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/memory/scoped_ptr.h"
12 #include "base/time/time.h"
13 #include "sync/base/sync_export.h"
14 #include "sync/protocol/sync.pb.h"
16 namespace syncer_v2 {
17 struct UpdateResponseData;
18 } // namespace syncer_v2
20 namespace syncer {
22 // Manages the pending commit and update state for an entity on the sync
23 // thread.
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 {
36 public:
37 ~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,
43 int64 version);
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,
50 int64 base_version,
51 base::Time ctime,
52 base::Time mtime,
53 const std::string& non_unique_name,
54 bool deleted,
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,
71 int64 base_version,
72 base::Time ctime,
73 base::Time mtime,
74 const std::string& non_unique_name,
75 bool deleted,
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
82 // the start.
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
94 // is out of date.
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();
104 private:
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,
119 int64 base_version,
120 base::Time ctime,
121 base::Time mtime,
122 const std::string& non_unique_name,
123 bool deleted,
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.
139 std::string id_;
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
151 // this item.
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.
162 int64 base_version_;
163 base::Time ctime_;
164 base::Time mtime_;
165 std::string non_unique_name_;
166 bool deleted_;
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_