OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef SYNC_ENGINE_WORKER_ENTITY_TRACKER_H_ | |
6 #define SYNC_ENGINE_WORKER_ENTITY_TRACKER_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <memory> | |
11 #include <string> | |
12 | |
13 #include "base/macros.h" | |
14 #include "base/time/time.h" | |
15 #include "sync/base/sync_export.h" | |
16 #include "sync/protocol/sync.pb.h" | |
17 | |
18 namespace syncer_v2 { | |
19 struct CommitRequestData; | |
20 struct CommitResponseData; | |
21 struct UpdateResponseData; | |
22 | |
23 // Manages the pending commit and update state for an entity on the sync | |
24 // thread. | |
25 // | |
26 // It should be considered a helper class internal to the | |
27 // ModelTypeWorker. | |
28 // | |
29 // Maintains the state associated with a particular sync entity which is | |
30 // necessary for decision-making on the sync thread. It can track pending | |
31 // commit state, received update state, and can detect conflicts. | |
32 // | |
33 // This object may contain state associated with a pending commit, pending | |
34 // update, or both. | |
35 class SYNC_EXPORT WorkerEntityTracker { | |
36 public: | |
37 // Initializes the entity tracker's main fields. Does not initialize state | |
38 // related to a pending commit. | |
39 WorkerEntityTracker(const std::string& id, | |
40 const std::string& client_tag_hash); | |
41 | |
42 ~WorkerEntityTracker(); | |
43 | |
44 // Returns true if this entity should be commited to the server. | |
45 bool HasPendingCommit() const; | |
46 | |
47 // Populates a sync_pb::SyncEntity for a commit. | |
48 void PopulateCommitProto(sync_pb::SyncEntity* commit_entity) const; | |
49 | |
50 // Updates this entity with data from the latest version that the | |
51 // model asked us to commit. May clobber state related to the | |
52 // model's previous commit attempt(s). | |
53 void RequestCommit(const CommitRequestData& data); | |
54 | |
55 // Tracks the receipt of a commit response and fills in some local-only data | |
56 // on it to be passed back to the processor. | |
57 void ReceiveCommitResponse(CommitResponseData* ack); | |
58 | |
59 // Handles receipt of an update from the server. | |
60 void ReceiveUpdate(int64_t version); | |
61 | |
62 // Handles the receipt of an encrypted update from the server. | |
63 // | |
64 // Returns true if the tracker decides this item is worth keeping. Returns | |
65 // false if the item is discarded, which could happen if the version number | |
66 // is out of date. | |
67 bool ReceiveEncryptedUpdate(const UpdateResponseData& data); | |
68 | |
69 // Functions to fetch the latest encrypted update. | |
70 bool HasEncryptedUpdate() const; | |
71 UpdateResponseData GetEncryptedUpdate() const; | |
72 | |
73 // Clears the encrypted update. Allows us to resume regular commit behavior. | |
74 void ClearEncryptedUpdate(); | |
75 | |
76 private: | |
77 // Checks if the current state indicates a conflict. | |
78 // | |
79 // This can be true only while a call to this object is in progress. | |
80 // Conflicts are always cleared before the method call ends. | |
81 bool IsInConflict() const; | |
82 | |
83 // Checks if the server knows about this item. | |
84 bool IsServerKnown() const; | |
85 | |
86 // Clears flag and optionally clears state associated with a pending commit. | |
87 void ClearPendingCommit(); | |
88 | |
89 // The ID for this entry. May be empty if the entry has never been committed. | |
90 std::string id_; | |
91 | |
92 // The hashed client tag for this entry. | |
93 std::string client_tag_hash_; | |
94 | |
95 // The highest version seen in a commit response for this entry. | |
96 int64_t highest_commit_response_version_; | |
97 | |
98 // The highest version seen in a GU response for this entry. | |
99 int64_t highest_gu_response_version_; | |
100 | |
101 // Used to track in-flight commit requests on the model thread. All we need | |
102 // to do here is return it back to the model thread when the pending commit | |
103 // is completed and confirmed. Not valid if no commit is pending. | |
104 int64_t sequence_number_; | |
105 | |
106 // The server version on which this item is based. | |
107 int64_t base_version_; | |
108 | |
109 // A commit for this entity waiting for a sync cycle to be committed. | |
110 std::unique_ptr<CommitRequestData> pending_commit_; | |
111 | |
112 // The specifics hash for the pending commit if there is one, "" otherwise. | |
113 std::string pending_commit_specifics_hash_; | |
114 | |
115 // An update for this entity which can't be applied right now. The presence | |
116 // of an pending update prevents commits. As of this writing, the only | |
117 // source of pending updates is updates that can't currently be decrypted. | |
118 std::unique_ptr<UpdateResponseData> encrypted_update_; | |
119 | |
120 DISALLOW_COPY_AND_ASSIGN(WorkerEntityTracker); | |
121 }; | |
122 | |
123 } // namespace syncer_v2 | |
124 | |
125 #endif // SYNC_ENGINE_WORKER_ENTITY_TRACKER_H_ | |
OLD | NEW |