OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // StatusController handles all counter and status related number crunching and | 5 // StatusController handles all counter and status related number crunching and |
6 // state tracking on behalf of a SyncSession. It 'controls' the model data | 6 // state tracking on behalf of a SyncSession. |
7 // defined in session_state.h. The most important feature of StatusController | 7 // |
8 // is the ScopedModelSafetyRestriction. When one of these is active, the | 8 // The most important feature of StatusController is the |
9 // underlying data set exposed via accessors is swapped out to the appropriate | 9 // ScopedModelSafeGroupRestriction. Some of its functions expose per-thread |
10 // set for the restricted ModelSafeGroup behind the scenes. For example, if | 10 // state, and can be called only when the restriction is in effect. For |
11 // GROUP_UI is set, then accessors such as conflict_progress() and commit_ids() | 11 // example, if GROUP_UI is set then the value returned from |
12 // are implicitly restricted to returning only data pertaining to GROUP_UI. | 12 // commit_id_projection() will be useful for iterating over the commit IDs of |
13 // You can see which parts of status fall into this "restricted" category, or | 13 // items that live on the UI thread. |
14 // the global "shared" category for all model types, by looking at the struct | 14 // |
15 // declarations in session_state.h. If these accessors are invoked without a | 15 // Other parts of its state are global, and do not require the restriction. |
16 // restriction in place, this is a violation and will cause debug assertions | |
17 // to surface improper use of the API in development. Likewise for | |
18 // invocation of "shared" accessors when a restriction is in place; for | |
19 // safety's sake, an assertion will fire. | |
20 // | 16 // |
21 // NOTE: There is no concurrent access protection provided by this class. It | 17 // NOTE: There is no concurrent access protection provided by this class. It |
22 // assumes one single thread is accessing this class for each unique | 18 // assumes one single thread is accessing this class for each unique |
23 // ModelSafeGroup, and also only one single thread (in practice, the | 19 // ModelSafeGroup, and also only one single thread (in practice, the |
24 // SyncerThread) responsible for all "shared" access when no restriction is in | 20 // SyncerThread) responsible for all "shared" access when no restriction is in |
25 // place. Thus, every bit of data is to be accessed mutually exclusively with | 21 // place. Thus, every bit of data is to be accessed mutually exclusively with |
26 // respect to threads. | 22 // respect to threads. |
27 // | 23 // |
28 // StatusController can also track if changes occur to certain parts of state | 24 // StatusController can also track if changes occur to certain parts of state |
29 // so that various parts of the sync engine can avoid broadcasting | 25 // so that various parts of the sync engine can avoid broadcasting |
30 // notifications if no changes occurred. | 26 // notifications if no changes occurred. |
31 | 27 |
32 #ifndef SYNC_SESSIONS_STATUS_CONTROLLER_H_ | 28 #ifndef SYNC_SESSIONS_STATUS_CONTROLLER_H_ |
33 #define SYNC_SESSIONS_STATUS_CONTROLLER_H_ | 29 #define SYNC_SESSIONS_STATUS_CONTROLLER_H_ |
34 | 30 |
35 #include <map> | 31 #include <map> |
36 #include <vector> | 32 #include <vector> |
37 | 33 |
38 #include "base/logging.h" | 34 #include "base/logging.h" |
39 #include "base/stl_util.h" | 35 #include "base/stl_util.h" |
40 #include "base/time.h" | 36 #include "base/time.h" |
41 #include "sync/internal_api/public/sessions/model_neutral_state.h" | 37 #include "sync/internal_api/public/sessions/model_neutral_state.h" |
42 #include "sync/sessions/ordered_commit_set.h" | 38 #include "sync/sessions/ordered_commit_set.h" |
43 #include "sync/sessions/session_state.h" | |
44 | 39 |
45 namespace syncer { | 40 namespace syncer { |
46 namespace sessions { | 41 namespace sessions { |
47 | 42 |
48 class StatusController { | 43 class StatusController { |
49 public: | 44 public: |
50 explicit StatusController(const ModelSafeRoutingInfo& routes); | 45 explicit StatusController(const ModelSafeRoutingInfo& routes); |
51 ~StatusController(); | 46 ~StatusController(); |
52 | 47 |
53 // Progress counters. All const methods may return NULL if the | |
54 // progress structure doesn't exist, but all non-const methods | |
55 // auto-create. | |
56 const std::set<syncable::Id>* simple_conflict_ids() const; | |
57 std::set<syncable::Id>* mutable_simple_conflict_ids(); | |
58 const std::set<syncable::Id>* GetUnrestrictedSimpleConflictIds( | |
59 ModelSafeGroup group) const; | |
60 | |
61 // ClientToServer messages. | 48 // ClientToServer messages. |
62 const ModelTypeSet updates_request_types() const { | 49 const ModelTypeSet updates_request_types() const { |
63 return model_neutral_.updates_request_types; | 50 return model_neutral_.updates_request_types; |
64 } | 51 } |
65 void set_updates_request_types(ModelTypeSet value) { | 52 void set_updates_request_types(ModelTypeSet value) { |
66 model_neutral_.updates_request_types = value; | 53 model_neutral_.updates_request_types = value; |
67 } | 54 } |
68 const sync_pb::ClientToServerResponse& updates_response() const { | 55 const sync_pb::ClientToServerResponse& updates_response() const { |
69 return model_neutral_.updates_response; | 56 return model_neutral_.updates_response; |
70 } | 57 } |
71 sync_pb::ClientToServerResponse* mutable_updates_response() { | 58 sync_pb::ClientToServerResponse* mutable_updates_response() { |
72 return &model_neutral_.updates_response; | 59 return &model_neutral_.updates_response; |
73 } | 60 } |
74 | 61 |
75 // Changelog related state. | 62 // Changelog related state. |
76 int64 num_server_changes_remaining() const { | 63 int64 num_server_changes_remaining() const { |
77 return model_neutral_.num_server_changes_remaining; | 64 return model_neutral_.num_server_changes_remaining; |
78 } | 65 } |
79 | 66 |
80 const OrderedCommitSet::Projection& commit_id_projection( | 67 const OrderedCommitSet::Projection& commit_id_projection( |
81 const sessions::OrderedCommitSet &commit_set) { | 68 const sessions::OrderedCommitSet &commit_set) { |
82 DCHECK(group_restriction_in_effect_) | 69 DCHECK(group_restriction_in_effect_) |
83 << "No group restriction for projection."; | 70 << "No group restriction for projection."; |
84 return commit_set.GetCommitIdProjection(group_restriction_); | 71 return commit_set.GetCommitIdProjection(group_restriction_); |
85 } | 72 } |
86 | 73 |
87 // Control parameters for sync cycles. | |
88 bool conflicts_resolved() const { | |
89 return model_neutral_.conflicts_resolved; | |
90 } | |
91 | |
92 // If a GetUpdates for any data type resulted in downloading an update that | |
93 // is in conflict, this method returns true. | |
94 // Note: this includes unresolvable conflicts. | |
95 bool HasConflictingUpdates() const; | |
96 | |
97 // Various conflict counters. | 74 // Various conflict counters. |
98 int num_encryption_conflicts() const; | 75 int num_encryption_conflicts() const; |
99 int num_hierarchy_conflicts() const; | 76 int num_hierarchy_conflicts() const; |
100 int num_server_conflicts() const; | 77 int num_server_conflicts() const; |
101 | 78 |
102 int num_simple_conflicts() const; | |
103 | |
104 // Aggregate sum of all conflicting items over all conflict types. | 79 // Aggregate sum of all conflicting items over all conflict types. |
105 int TotalNumConflictingItems() const; | 80 int TotalNumConflictingItems() const; |
106 | 81 |
107 // Number of successfully applied updates. | 82 // Number of successfully applied updates. |
108 int num_updates_applied() const; | 83 int num_updates_applied() const; |
109 | 84 |
110 int num_server_overwrites() const; | 85 int num_server_overwrites() const; |
111 | 86 |
112 // Returns the number of updates received from the sync server. | 87 // Returns the number of updates received from the sync server. |
113 int64 CountUpdates() const; | 88 int64 CountUpdates() const; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 void increment_num_reflected_updates_downloaded_by(int value); | 128 void increment_num_reflected_updates_downloaded_by(int value); |
154 | 129 |
155 // Update application and conflict resolution counters. | 130 // Update application and conflict resolution counters. |
156 void increment_num_updates_applied_by(int value); | 131 void increment_num_updates_applied_by(int value); |
157 void increment_num_encryption_conflicts_by(int value); | 132 void increment_num_encryption_conflicts_by(int value); |
158 void increment_num_hierarchy_conflicts_by(int value); | 133 void increment_num_hierarchy_conflicts_by(int value); |
159 void increment_num_server_conflicts(); | 134 void increment_num_server_conflicts(); |
160 void increment_num_local_overwrites(); | 135 void increment_num_local_overwrites(); |
161 void increment_num_server_overwrites(); | 136 void increment_num_server_overwrites(); |
162 | 137 |
163 // TODO(rlarocque): Remove these after conflict resolution refactor. | |
164 void update_conflicts_resolved(bool resolved); | |
165 void reset_conflicts_resolved(); | |
166 | |
167 // Commit counters. | 138 // Commit counters. |
168 void increment_num_successful_commits(); | 139 void increment_num_successful_commits(); |
169 void increment_num_successful_bookmark_commits(); | 140 void increment_num_successful_bookmark_commits(); |
170 void set_num_successful_bookmark_commits(int value); | 141 void set_num_successful_bookmark_commits(int value); |
171 | 142 |
172 // Server communication status tracking. | 143 // Server communication status tracking. |
173 void set_sync_protocol_error(const SyncProtocolError& error); | 144 void set_sync_protocol_error(const SyncProtocolError& error); |
174 void set_last_get_key_result(const SyncerError result); | 145 void set_last_get_key_result(const SyncerError result); |
175 void set_last_download_updates_result(const SyncerError result); | 146 void set_last_download_updates_result(const SyncerError result); |
176 void set_commit_result(const SyncerError result); | 147 void set_commit_result(const SyncerError result); |
(...skipping 14 matching lines...) Expand all Loading... |
191 // restriction. | 162 // restriction. |
192 bool ActiveGroupRestrictionIncludesModel(ModelType model) const { | 163 bool ActiveGroupRestrictionIncludesModel(ModelType model) const { |
193 if (!group_restriction_in_effect_) | 164 if (!group_restriction_in_effect_) |
194 return true; | 165 return true; |
195 ModelSafeRoutingInfo::const_iterator it = routing_info_.find(model); | 166 ModelSafeRoutingInfo::const_iterator it = routing_info_.find(model); |
196 if (it == routing_info_.end()) | 167 if (it == routing_info_.end()) |
197 return false; | 168 return false; |
198 return group_restriction() == it->second; | 169 return group_restriction() == it->second; |
199 } | 170 } |
200 | 171 |
201 // Returns the state, if it exists, or NULL otherwise. | |
202 const PerModelSafeGroupState* GetModelSafeGroupState( | |
203 bool restrict, ModelSafeGroup group) const; | |
204 | |
205 // Helper to lazily create objects for per-ModelSafeGroup state. | |
206 PerModelSafeGroupState* GetOrCreateModelSafeGroupState( | |
207 bool restrict, ModelSafeGroup group); | |
208 | |
209 ModelNeutralState model_neutral_; | 172 ModelNeutralState model_neutral_; |
210 std::map<ModelSafeGroup, PerModelSafeGroupState*> per_model_group_; | |
211 | |
212 STLValueDeleter<std::map<ModelSafeGroup, PerModelSafeGroupState*> > | |
213 per_model_group_deleter_; | |
214 | 173 |
215 // Used to fail read/write operations on state that don't obey the current | 174 // Used to fail read/write operations on state that don't obey the current |
216 // active ModelSafeWorker contract. | 175 // active ModelSafeWorker contract. |
217 bool group_restriction_in_effect_; | 176 bool group_restriction_in_effect_; |
218 ModelSafeGroup group_restriction_; | 177 ModelSafeGroup group_restriction_; |
219 | 178 |
220 const ModelSafeRoutingInfo routing_info_; | 179 const ModelSafeRoutingInfo routing_info_; |
221 | 180 |
222 base::Time sync_start_time_; | 181 base::Time sync_start_time_; |
223 | 182 |
(...skipping 17 matching lines...) Expand all Loading... |
241 } | 200 } |
242 private: | 201 private: |
243 StatusController* status_; | 202 StatusController* status_; |
244 DISALLOW_COPY_AND_ASSIGN(ScopedModelSafeGroupRestriction); | 203 DISALLOW_COPY_AND_ASSIGN(ScopedModelSafeGroupRestriction); |
245 }; | 204 }; |
246 | 205 |
247 } // namespace sessions | 206 } // namespace sessions |
248 } // namespace syncer | 207 } // namespace syncer |
249 | 208 |
250 #endif // SYNC_SESSIONS_STATUS_CONTROLLER_H_ | 209 #endif // SYNC_SESSIONS_STATUS_CONTROLLER_H_ |
OLD | NEW |