OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 // StatusController handles all counter and status related number crunching and | |
6 // state tracking on behalf of a SyncSession. It 'controls' the model data | |
7 // defined in session_state.h. The most important feature of StatusController | |
8 // is the ScopedModelSafetyRestriction. When one of these is active, the | |
9 // underlying data set exposed via accessors is swapped out to the appropriate | |
10 // set for the restricted ModelSafeGroup behind the scenes. For example, if | |
11 // GROUP_UI is set, then accessors such as conflict_progress() and commit_ids() | |
12 // are implicitly restricted to returning only data pertaining to GROUP_UI. | |
13 // You can see which parts of status fall into this "restricted" category, or | |
14 // the global "shared" category for all model types, by looking at the struct | |
15 // declarations in session_state.h. If these accessors are invoked without a | |
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 // | |
21 // NOTE: There is no concurrent access protection provided by this class. It | |
22 // assumes one single thread is accessing this class for each unique | |
23 // ModelSafeGroup, and also only one single thread (in practice, the | |
24 // 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 | |
26 // respect to threads. | |
27 // | |
28 // StatusController can also track if changes occur to certain parts of state | |
29 // so that various parts of the sync engine can avoid broadcasting | |
30 // notifications if no changes occurred. | |
31 | |
32 #ifndef CHROME_BROWSER_SYNC_SESSIONS_STATUS_CONTROLLER_H_ | |
33 #define CHROME_BROWSER_SYNC_SESSIONS_STATUS_CONTROLLER_H_ | |
34 #pragma once | |
35 | |
36 #include <vector> | |
37 #include <map> | |
38 | |
39 #include "base/logging.h" | |
40 #include "base/stl_util.h" | |
41 #include "base/time.h" | |
42 #include "chrome/browser/sync/sessions/ordered_commit_set.h" | |
43 #include "chrome/browser/sync/sessions/session_state.h" | |
44 | |
45 namespace browser_sync { | |
46 namespace sessions { | |
47 | |
48 class StatusController { | |
49 public: | |
50 explicit StatusController(const ModelSafeRoutingInfo& routes); | |
51 ~StatusController(); | |
52 | |
53 // Returns true if some portion of the session state has changed (is dirty) | |
54 // since it was created or was last reset. | |
55 bool TestAndClearIsDirty(); | |
56 | |
57 // Progress counters. All const methods may return NULL if the | |
58 // progress structure doesn't exist, but all non-const methods | |
59 // auto-create. | |
60 const ConflictProgress* conflict_progress() const; | |
61 ConflictProgress* mutable_conflict_progress(); | |
62 const UpdateProgress* update_progress() const; | |
63 UpdateProgress* mutable_update_progress(); | |
64 const ConflictProgress* GetUnrestrictedConflictProgress( | |
65 ModelSafeGroup group) const; | |
66 ConflictProgress* GetUnrestrictedMutableConflictProgressForTest( | |
67 ModelSafeGroup group); | |
68 const UpdateProgress* GetUnrestrictedUpdateProgress( | |
69 ModelSafeGroup group) const; | |
70 UpdateProgress* GetUnrestrictedMutableUpdateProgressForTest( | |
71 ModelSafeGroup group); | |
72 | |
73 // ClientToServer messages. | |
74 const ClientToServerMessage& commit_message() { | |
75 return shared_.commit_message; | |
76 } | |
77 ClientToServerMessage* mutable_commit_message() { | |
78 return &shared_.commit_message; | |
79 } | |
80 const ClientToServerResponse& commit_response() const { | |
81 return shared_.commit_response; | |
82 } | |
83 ClientToServerResponse* mutable_commit_response() { | |
84 return &shared_.commit_response; | |
85 } | |
86 const syncable::ModelTypeSet updates_request_types() const { | |
87 return shared_.updates_request_types; | |
88 } | |
89 void set_updates_request_types(syncable::ModelTypeSet value) { | |
90 shared_.updates_request_types = value; | |
91 } | |
92 const ClientToServerResponse& updates_response() const { | |
93 return shared_.updates_response; | |
94 } | |
95 ClientToServerResponse* mutable_updates_response() { | |
96 return &shared_.updates_response; | |
97 } | |
98 | |
99 // Errors and SyncerStatus. | |
100 const ErrorCounters& error() const { | |
101 return shared_.error.value(); | |
102 } | |
103 const SyncerStatus& syncer_status() const { | |
104 return shared_.syncer_status.value(); | |
105 } | |
106 | |
107 // Changelog related state. | |
108 int64 num_server_changes_remaining() const { | |
109 return shared_.num_server_changes_remaining.value(); | |
110 } | |
111 | |
112 // Commit path data. | |
113 const std::vector<syncable::Id>& commit_ids() const { | |
114 DCHECK(!group_restriction_in_effect_) << "Group restriction in effect!"; | |
115 return shared_.commit_set.GetAllCommitIds(); | |
116 } | |
117 const OrderedCommitSet::Projection& commit_id_projection() { | |
118 DCHECK(group_restriction_in_effect_) | |
119 << "No group restriction for projection."; | |
120 return shared_.commit_set.GetCommitIdProjection(group_restriction_); | |
121 } | |
122 const syncable::Id& GetCommitIdAt(size_t index) { | |
123 DCHECK(CurrentCommitIdProjectionHasIndex(index)); | |
124 return shared_.commit_set.GetCommitIdAt(index); | |
125 } | |
126 syncable::ModelType GetCommitModelTypeAt(size_t index) { | |
127 DCHECK(CurrentCommitIdProjectionHasIndex(index)); | |
128 return shared_.commit_set.GetModelTypeAt(index); | |
129 } | |
130 syncable::ModelType GetUnrestrictedCommitModelTypeAt(size_t index) const { | |
131 DCHECK(!group_restriction_in_effect_) << "Group restriction in effect!"; | |
132 return shared_.commit_set.GetModelTypeAt(index); | |
133 } | |
134 const std::vector<int64>& unsynced_handles() const { | |
135 DCHECK(!group_restriction_in_effect_) | |
136 << "unsynced_handles is unrestricted."; | |
137 return shared_.unsynced_handles.value(); | |
138 } | |
139 | |
140 // Control parameters for sync cycles. | |
141 bool conflicts_resolved() const { | |
142 return shared_.control_params.conflicts_resolved; | |
143 } | |
144 bool did_commit_items() const { | |
145 return shared_.control_params.items_committed; | |
146 } | |
147 | |
148 // If a GetUpdates for any data type resulted in downloading an update that | |
149 // is in conflict, this method returns true. | |
150 // Note: this includes unresolvable conflicts. | |
151 bool HasConflictingUpdates() const; | |
152 | |
153 // Aggregate sums of various types of conflict counters accross all | |
154 // ConflictProgress objects (one for each ModelSafeGroup currently in-use). | |
155 int TotalNumEncryptionConflictingItems() const; | |
156 int TotalNumHierarchyConflictingItems() const; | |
157 int TotalNumServerConflictingItems() const; | |
158 int TotalNumSimpleConflictingItems() const; | |
159 | |
160 // Aggregate sum of SimpleConflictingItemSize() and other | |
161 // ${Type}ConflictingItemSize() methods over all ConflictProgress objects (one | |
162 // for each ModelSafeGroup currently in-use). | |
163 int TotalNumConflictingItems() const; | |
164 | |
165 // Returns the number of updates received from the sync server. | |
166 int64 CountUpdates() const; | |
167 | |
168 // Returns true iff any of the commit ids added during this session are | |
169 // bookmark related, and the bookmark group restriction is in effect. | |
170 bool HasBookmarkCommitActivity() const { | |
171 return ActiveGroupRestrictionIncludesModel(syncable::BOOKMARKS) && | |
172 shared_.commit_set.HasBookmarkCommitId(); | |
173 } | |
174 | |
175 // Returns true if the last download_updates_command received a valid | |
176 // server response. | |
177 bool download_updates_succeeded() const { | |
178 return updates_response().has_get_updates(); | |
179 } | |
180 | |
181 // Returns true if the last updates response indicated that we were fully | |
182 // up to date. This is subtle: if it's false, it could either mean that | |
183 // the server said there WAS more to download, or it could mean that we | |
184 // were unable to reach the server. If we didn't request every enabled | |
185 // datatype, then we can't say for sure that there's nothing left to | |
186 // download: in that case, this also returns false. | |
187 bool ServerSaysNothingMoreToDownload() const; | |
188 | |
189 ModelSafeGroup group_restriction() const { | |
190 return group_restriction_; | |
191 } | |
192 | |
193 base::Time sync_start_time() const { | |
194 // The time at which we sent the first GetUpdates command for this sync. | |
195 return sync_start_time_; | |
196 } | |
197 | |
198 // Check whether a particular model is included by the active group | |
199 // restriction. | |
200 bool ActiveGroupRestrictionIncludesModel(syncable::ModelType model) const { | |
201 if (!group_restriction_in_effect_) | |
202 return true; | |
203 ModelSafeRoutingInfo::const_iterator it = routing_info_.find(model); | |
204 if (it == routing_info_.end()) | |
205 return false; | |
206 return group_restriction() == it->second; | |
207 } | |
208 | |
209 // A toolbelt full of methods for updating counters and flags. | |
210 void set_num_server_changes_remaining(int64 changes_remaining); | |
211 void set_invalid_store(bool invalid_store); | |
212 void set_num_successful_bookmark_commits(int value); | |
213 void increment_num_successful_commits(); | |
214 void increment_num_successful_bookmark_commits(); | |
215 void increment_num_updates_downloaded_by(int value); | |
216 void increment_num_tombstone_updates_downloaded_by(int value); | |
217 void set_types_needing_local_migration(syncable::ModelTypeSet types); | |
218 void set_unsynced_handles(const std::vector<int64>& unsynced_handles); | |
219 void increment_num_local_overwrites(); | |
220 void increment_num_server_overwrites(); | |
221 void set_sync_protocol_error(const SyncProtocolError& error); | |
222 void set_last_download_updates_result(const SyncerError result); | |
223 void set_last_post_commit_result(const SyncerError result); | |
224 void set_last_process_commit_response_result(const SyncerError result); | |
225 | |
226 void set_commit_set(const OrderedCommitSet& commit_set); | |
227 void update_conflicts_resolved(bool resolved); | |
228 void reset_conflicts_resolved(); | |
229 void set_items_committed(); | |
230 | |
231 void UpdateStartTime(); | |
232 | |
233 void set_debug_info_sent(); | |
234 | |
235 bool debug_info_sent() const; | |
236 | |
237 private: | |
238 friend class ScopedModelSafeGroupRestriction; | |
239 | |
240 // Returns true iff the commit id projection for |group_restriction_| | |
241 // references position |index| into the full set of commit ids in play. | |
242 bool CurrentCommitIdProjectionHasIndex(size_t index); | |
243 | |
244 // Returns the state, if it exists, or NULL otherwise. | |
245 const PerModelSafeGroupState* GetModelSafeGroupState( | |
246 bool restrict, ModelSafeGroup group) const; | |
247 | |
248 // Helper to lazily create objects for per-ModelSafeGroup state. | |
249 PerModelSafeGroupState* GetOrCreateModelSafeGroupState( | |
250 bool restrict, ModelSafeGroup group); | |
251 | |
252 AllModelTypeState shared_; | |
253 std::map<ModelSafeGroup, PerModelSafeGroupState*> per_model_group_; | |
254 | |
255 STLValueDeleter<std::map<ModelSafeGroup, PerModelSafeGroupState*> > | |
256 per_model_group_deleter_; | |
257 | |
258 // Set to true if any DirtyOnWrite pieces of state we maintain are changed. | |
259 // Reset to false by TestAndClearIsDirty. | |
260 bool is_dirty_; | |
261 | |
262 // Used to fail read/write operations on state that don't obey the current | |
263 // active ModelSafeWorker contract. | |
264 bool group_restriction_in_effect_; | |
265 ModelSafeGroup group_restriction_; | |
266 | |
267 const ModelSafeRoutingInfo routing_info_; | |
268 | |
269 base::Time sync_start_time_; | |
270 | |
271 DISALLOW_COPY_AND_ASSIGN(StatusController); | |
272 }; | |
273 | |
274 // A utility to restrict access to only those parts of the given | |
275 // StatusController that pertain to the specified ModelSafeGroup. | |
276 class ScopedModelSafeGroupRestriction { | |
277 public: | |
278 ScopedModelSafeGroupRestriction(StatusController* to_restrict, | |
279 ModelSafeGroup restriction) | |
280 : status_(to_restrict) { | |
281 DCHECK(!status_->group_restriction_in_effect_); | |
282 status_->group_restriction_ = restriction; | |
283 status_->group_restriction_in_effect_ = true; | |
284 } | |
285 ~ScopedModelSafeGroupRestriction() { | |
286 DCHECK(status_->group_restriction_in_effect_); | |
287 status_->group_restriction_in_effect_ = false; | |
288 } | |
289 private: | |
290 StatusController* status_; | |
291 DISALLOW_COPY_AND_ASSIGN(ScopedModelSafeGroupRestriction); | |
292 }; | |
293 | |
294 } | |
295 } | |
296 | |
297 #endif // CHROME_BROWSER_SYNC_SESSIONS_STATUS_CONTROLLER_H_ | |
OLD | NEW |