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 #include "chrome/browser/sync/sessions/status_controller.h" | |
6 | |
7 #include <vector> | |
8 | |
9 #include "base/basictypes.h" | |
10 #include "chrome/browser/sync/protocol/sync_protocol_error.h" | |
11 #include "chrome/browser/sync/syncable/model_type.h" | |
12 | |
13 namespace browser_sync { | |
14 namespace sessions { | |
15 | |
16 using syncable::FIRST_REAL_MODEL_TYPE; | |
17 using syncable::MODEL_TYPE_COUNT; | |
18 | |
19 StatusController::StatusController(const ModelSafeRoutingInfo& routes) | |
20 : shared_(&is_dirty_), | |
21 per_model_group_deleter_(&per_model_group_), | |
22 is_dirty_(false), | |
23 group_restriction_in_effect_(false), | |
24 group_restriction_(GROUP_PASSIVE), | |
25 routing_info_(routes) { | |
26 } | |
27 | |
28 StatusController::~StatusController() {} | |
29 | |
30 bool StatusController::TestAndClearIsDirty() { | |
31 bool is_dirty = is_dirty_; | |
32 is_dirty_ = false; | |
33 return is_dirty; | |
34 } | |
35 | |
36 const UpdateProgress* StatusController::update_progress() const { | |
37 const PerModelSafeGroupState* state = | |
38 GetModelSafeGroupState(true, group_restriction_); | |
39 return state ? &state->update_progress : NULL; | |
40 } | |
41 | |
42 UpdateProgress* StatusController::mutable_update_progress() { | |
43 return &GetOrCreateModelSafeGroupState( | |
44 true, group_restriction_)->update_progress; | |
45 } | |
46 | |
47 const ConflictProgress* StatusController::conflict_progress() const { | |
48 const PerModelSafeGroupState* state = | |
49 GetModelSafeGroupState(true, group_restriction_); | |
50 return state ? &state->conflict_progress : NULL; | |
51 } | |
52 | |
53 ConflictProgress* StatusController::mutable_conflict_progress() { | |
54 return &GetOrCreateModelSafeGroupState( | |
55 true, group_restriction_)->conflict_progress; | |
56 } | |
57 | |
58 const ConflictProgress* StatusController::GetUnrestrictedConflictProgress( | |
59 ModelSafeGroup group) const { | |
60 const PerModelSafeGroupState* state = | |
61 GetModelSafeGroupState(false, group); | |
62 return state ? &state->conflict_progress : NULL; | |
63 } | |
64 | |
65 ConflictProgress* | |
66 StatusController::GetUnrestrictedMutableConflictProgressForTest( | |
67 ModelSafeGroup group) { | |
68 return &GetOrCreateModelSafeGroupState(false, group)->conflict_progress; | |
69 } | |
70 | |
71 const UpdateProgress* StatusController::GetUnrestrictedUpdateProgress( | |
72 ModelSafeGroup group) const { | |
73 const PerModelSafeGroupState* state = | |
74 GetModelSafeGroupState(false, group); | |
75 return state ? &state->update_progress : NULL; | |
76 } | |
77 | |
78 UpdateProgress* | |
79 StatusController::GetUnrestrictedMutableUpdateProgressForTest( | |
80 ModelSafeGroup group) { | |
81 return &GetOrCreateModelSafeGroupState(false, group)->update_progress; | |
82 } | |
83 | |
84 const PerModelSafeGroupState* StatusController::GetModelSafeGroupState( | |
85 bool restrict, ModelSafeGroup group) const { | |
86 DCHECK_EQ(restrict, group_restriction_in_effect_); | |
87 std::map<ModelSafeGroup, PerModelSafeGroupState*>::const_iterator it = | |
88 per_model_group_.find(group); | |
89 return (it == per_model_group_.end()) ? NULL : it->second; | |
90 } | |
91 | |
92 PerModelSafeGroupState* StatusController::GetOrCreateModelSafeGroupState( | |
93 bool restrict, ModelSafeGroup group) { | |
94 DCHECK_EQ(restrict, group_restriction_in_effect_); | |
95 std::map<ModelSafeGroup, PerModelSafeGroupState*>::iterator it = | |
96 per_model_group_.find(group); | |
97 if (it == per_model_group_.end()) { | |
98 PerModelSafeGroupState* state = new PerModelSafeGroupState(&is_dirty_); | |
99 it = per_model_group_.insert(std::make_pair(group, state)).first; | |
100 } | |
101 return it->second; | |
102 } | |
103 | |
104 void StatusController::increment_num_updates_downloaded_by(int value) { | |
105 shared_.syncer_status.mutate()->num_updates_downloaded_total += value; | |
106 } | |
107 | |
108 void StatusController::set_types_needing_local_migration( | |
109 syncable::ModelTypeSet types) { | |
110 shared_.syncer_status.mutate()->types_needing_local_migration = types; | |
111 } | |
112 | |
113 void StatusController::increment_num_tombstone_updates_downloaded_by( | |
114 int value) { | |
115 shared_.syncer_status.mutate()->num_tombstone_updates_downloaded_total += | |
116 value; | |
117 } | |
118 | |
119 void StatusController::set_num_server_changes_remaining( | |
120 int64 changes_remaining) { | |
121 if (shared_.num_server_changes_remaining.value() != changes_remaining) | |
122 *(shared_.num_server_changes_remaining.mutate()) = changes_remaining; | |
123 } | |
124 | |
125 void StatusController::set_invalid_store(bool invalid_store) { | |
126 if (shared_.syncer_status.value().invalid_store != invalid_store) | |
127 shared_.syncer_status.mutate()->invalid_store = invalid_store; | |
128 } | |
129 | |
130 void StatusController::UpdateStartTime() { | |
131 sync_start_time_ = base::Time::Now(); | |
132 } | |
133 | |
134 void StatusController::set_num_successful_bookmark_commits(int value) { | |
135 if (shared_.syncer_status.value().num_successful_bookmark_commits != value) | |
136 shared_.syncer_status.mutate()->num_successful_bookmark_commits = value; | |
137 } | |
138 | |
139 void StatusController::set_unsynced_handles( | |
140 const std::vector<int64>& unsynced_handles) { | |
141 if (!operator==(unsynced_handles, shared_.unsynced_handles.value())) { | |
142 *(shared_.unsynced_handles.mutate()) = unsynced_handles; | |
143 } | |
144 } | |
145 | |
146 void StatusController::increment_num_successful_bookmark_commits() { | |
147 set_num_successful_bookmark_commits( | |
148 shared_.syncer_status.value().num_successful_bookmark_commits + 1); | |
149 } | |
150 | |
151 void StatusController::increment_num_successful_commits() { | |
152 shared_.syncer_status.mutate()->num_successful_commits++; | |
153 } | |
154 | |
155 void StatusController::increment_num_local_overwrites() { | |
156 shared_.syncer_status.mutate()->num_local_overwrites++; | |
157 } | |
158 | |
159 void StatusController::increment_num_server_overwrites() { | |
160 shared_.syncer_status.mutate()->num_server_overwrites++; | |
161 } | |
162 | |
163 void StatusController::set_sync_protocol_error( | |
164 const SyncProtocolError& error) { | |
165 shared_.error.mutate()->sync_protocol_error = error; | |
166 } | |
167 | |
168 void StatusController::set_last_download_updates_result( | |
169 const SyncerError result) { | |
170 shared_.error.mutate()->last_download_updates_result = result; | |
171 } | |
172 | |
173 void StatusController::set_last_post_commit_result(const SyncerError result) { | |
174 shared_.error.mutate()->last_post_commit_result = result; | |
175 } | |
176 | |
177 void StatusController::set_last_process_commit_response_result( | |
178 const SyncerError result) { | |
179 shared_.error.mutate()->last_process_commit_response_result = result; | |
180 } | |
181 | |
182 void StatusController::set_commit_set(const OrderedCommitSet& commit_set) { | |
183 DCHECK(!group_restriction_in_effect_); | |
184 shared_.commit_set = commit_set; | |
185 } | |
186 | |
187 void StatusController::update_conflicts_resolved(bool resolved) { | |
188 shared_.control_params.conflicts_resolved |= resolved; | |
189 } | |
190 void StatusController::reset_conflicts_resolved() { | |
191 shared_.control_params.conflicts_resolved = false; | |
192 } | |
193 void StatusController::set_items_committed() { | |
194 shared_.control_params.items_committed = true; | |
195 } | |
196 | |
197 // Returns the number of updates received from the sync server. | |
198 int64 StatusController::CountUpdates() const { | |
199 const ClientToServerResponse& updates = shared_.updates_response; | |
200 if (updates.has_get_updates()) { | |
201 return updates.get_updates().entries().size(); | |
202 } else { | |
203 return 0; | |
204 } | |
205 } | |
206 | |
207 bool StatusController::CurrentCommitIdProjectionHasIndex(size_t index) { | |
208 OrderedCommitSet::Projection proj = | |
209 shared_.commit_set.GetCommitIdProjection(group_restriction_); | |
210 return std::binary_search(proj.begin(), proj.end(), index); | |
211 } | |
212 | |
213 bool StatusController::HasConflictingUpdates() const { | |
214 DCHECK(!group_restriction_in_effect_) | |
215 << "HasConflictingUpdates applies to all ModelSafeGroups"; | |
216 std::map<ModelSafeGroup, PerModelSafeGroupState*>::const_iterator it = | |
217 per_model_group_.begin(); | |
218 for (; it != per_model_group_.end(); ++it) { | |
219 if (it->second->update_progress.HasConflictingUpdates()) | |
220 return true; | |
221 } | |
222 return false; | |
223 } | |
224 | |
225 int StatusController::TotalNumEncryptionConflictingItems() const { | |
226 DCHECK(!group_restriction_in_effect_) | |
227 << "TotalNumEncryptionConflictingItems applies to all ModelSafeGroups"; | |
228 std::map<ModelSafeGroup, PerModelSafeGroupState*>::const_iterator it = | |
229 per_model_group_.begin(); | |
230 int sum = 0; | |
231 for (; it != per_model_group_.end(); ++it) { | |
232 sum += it->second->conflict_progress.EncryptionConflictingItemsSize(); | |
233 } | |
234 return sum; | |
235 } | |
236 | |
237 int StatusController::TotalNumHierarchyConflictingItems() const { | |
238 DCHECK(!group_restriction_in_effect_) | |
239 << "TotalNumHierarchyConflictingItems applies to all ModelSafeGroups"; | |
240 std::map<ModelSafeGroup, PerModelSafeGroupState*>::const_iterator it = | |
241 per_model_group_.begin(); | |
242 int sum = 0; | |
243 for (; it != per_model_group_.end(); ++it) { | |
244 sum += it->second->conflict_progress.HierarchyConflictingItemsSize(); | |
245 } | |
246 return sum; | |
247 } | |
248 | |
249 int StatusController::TotalNumSimpleConflictingItems() const { | |
250 DCHECK(!group_restriction_in_effect_) | |
251 << "TotalNumSimpleConflictingItems applies to all ModelSafeGroups"; | |
252 std::map<ModelSafeGroup, PerModelSafeGroupState*>::const_iterator it = | |
253 per_model_group_.begin(); | |
254 int sum = 0; | |
255 for (; it != per_model_group_.end(); ++it) { | |
256 sum += it->second->conflict_progress.SimpleConflictingItemsSize(); | |
257 } | |
258 return sum; | |
259 } | |
260 | |
261 int StatusController::TotalNumServerConflictingItems() const { | |
262 DCHECK(!group_restriction_in_effect_) | |
263 << "TotalNumServerConflictingItems applies to all ModelSafeGroups"; | |
264 std::map<ModelSafeGroup, PerModelSafeGroupState*>::const_iterator it = | |
265 per_model_group_.begin(); | |
266 int sum = 0; | |
267 for (; it != per_model_group_.end(); ++it) { | |
268 sum += it->second->conflict_progress.ServerConflictingItemsSize(); | |
269 } | |
270 return sum; | |
271 } | |
272 | |
273 int StatusController::TotalNumConflictingItems() const { | |
274 DCHECK(!group_restriction_in_effect_) | |
275 << "TotalNumConflictingItems applies to all ModelSafeGroups"; | |
276 std::map<ModelSafeGroup, PerModelSafeGroupState*>::const_iterator it = | |
277 per_model_group_.begin(); | |
278 int sum = 0; | |
279 for (; it != per_model_group_.end(); ++it) { | |
280 sum += it->second->conflict_progress.SimpleConflictingItemsSize(); | |
281 sum += it->second->conflict_progress.EncryptionConflictingItemsSize(); | |
282 sum += it->second->conflict_progress.HierarchyConflictingItemsSize(); | |
283 sum += it->second->conflict_progress.ServerConflictingItemsSize(); | |
284 } | |
285 return sum; | |
286 } | |
287 | |
288 bool StatusController::ServerSaysNothingMoreToDownload() const { | |
289 if (!download_updates_succeeded()) | |
290 return false; | |
291 | |
292 if (!updates_response().get_updates().has_changes_remaining()) { | |
293 NOTREACHED(); // Server should always send changes remaining. | |
294 return false; // Avoid looping forever. | |
295 } | |
296 // Changes remaining is an estimate, but if it's estimated to be | |
297 // zero, that's firm and we don't have to ask again. | |
298 return updates_response().get_updates().changes_remaining() == 0; | |
299 } | |
300 | |
301 void StatusController::set_debug_info_sent() { | |
302 shared_.control_params.debug_info_sent = true; | |
303 } | |
304 | |
305 bool StatusController::debug_info_sent() const { | |
306 return shared_.control_params.debug_info_sent; | |
307 } | |
308 | |
309 } // namespace sessions | |
310 } // namespace browser_sync | |
OLD | NEW |