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.
5 #include "sync/sessions/status_controller.h"
9 #include "base/basictypes.h"
10 #include "sync/internal_api/public/base/model_type.h"
11 #include "sync/protocol/sync_protocol_error.h"
16 StatusController::StatusController(const ModelSafeRoutingInfo
& routes
)
17 : per_model_group_deleter_(&per_model_group_
),
18 group_restriction_in_effect_(false),
19 group_restriction_(GROUP_PASSIVE
),
20 routing_info_(routes
) {
23 StatusController::~StatusController() {}
25 const UpdateProgress
* StatusController::update_progress() const {
26 const PerModelSafeGroupState
* state
=
27 GetModelSafeGroupState(true, group_restriction_
);
28 return state
? &state
->update_progress
: NULL
;
31 UpdateProgress
* StatusController::mutable_update_progress() {
32 return &GetOrCreateModelSafeGroupState(
33 true, group_restriction_
)->update_progress
;
36 const std::set
<syncable::Id
>* StatusController::simple_conflict_ids() const {
37 const PerModelSafeGroupState
* state
=
38 GetModelSafeGroupState(true, group_restriction_
);
39 return state
? &state
->simple_conflict_ids
: NULL
;
42 std::set
<syncable::Id
>* StatusController::mutable_simple_conflict_ids() {
43 return &GetOrCreateModelSafeGroupState(
44 true, group_restriction_
)->simple_conflict_ids
;
47 const std::set
<syncable::Id
>*
48 StatusController::GetUnrestrictedSimpleConflictIds(
49 ModelSafeGroup group
) const {
50 const PerModelSafeGroupState
* state
= GetModelSafeGroupState(false, group
);
51 return state
? &state
->simple_conflict_ids
: NULL
;
54 const UpdateProgress
* StatusController::GetUnrestrictedUpdateProgress(
55 ModelSafeGroup group
) const {
56 const PerModelSafeGroupState
* state
= GetModelSafeGroupState(false, group
);
57 return state
? &state
->update_progress
: NULL
;
61 StatusController::GetUnrestrictedMutableUpdateProgressForTest(
62 ModelSafeGroup group
) {
63 return &GetOrCreateModelSafeGroupState(false, group
)->update_progress
;
66 const PerModelSafeGroupState
* StatusController::GetModelSafeGroupState(
67 bool restrict
, ModelSafeGroup group
) const {
68 DCHECK_EQ(restrict
, group_restriction_in_effect_
);
69 std::map
<ModelSafeGroup
, PerModelSafeGroupState
*>::const_iterator it
=
70 per_model_group_
.find(group
);
71 return (it
== per_model_group_
.end()) ? NULL
: it
->second
;
74 PerModelSafeGroupState
* StatusController::GetOrCreateModelSafeGroupState(
75 bool restrict
, ModelSafeGroup group
) {
76 DCHECK_EQ(restrict
, group_restriction_in_effect_
);
77 std::map
<ModelSafeGroup
, PerModelSafeGroupState
*>::iterator it
=
78 per_model_group_
.find(group
);
79 if (it
== per_model_group_
.end()) {
80 PerModelSafeGroupState
* state
= new PerModelSafeGroupState();
81 it
= per_model_group_
.insert(std::make_pair(group
, state
)).first
;
86 void StatusController::increment_num_updates_downloaded_by(int value
) {
87 model_neutral_
.num_updates_downloaded_total
+= value
;
90 void StatusController::set_types_needing_local_migration(ModelTypeSet types
) {
91 model_neutral_
.types_needing_local_migration
= types
;
94 void StatusController::increment_num_tombstone_updates_downloaded_by(
96 model_neutral_
.num_tombstone_updates_downloaded_total
+= value
;
99 void StatusController::increment_num_reflected_updates_downloaded_by(
101 model_neutral_
.num_reflected_updates_downloaded_total
+= value
;
104 void StatusController::set_num_server_changes_remaining(
105 int64 changes_remaining
) {
106 model_neutral_
.num_server_changes_remaining
= changes_remaining
;
109 void StatusController::UpdateStartTime() {
110 sync_start_time_
= base::Time::Now();
113 void StatusController::set_num_successful_bookmark_commits(int value
) {
114 model_neutral_
.num_successful_bookmark_commits
= value
;
117 void StatusController::increment_num_successful_bookmark_commits() {
118 model_neutral_
.num_successful_bookmark_commits
++;
121 void StatusController::increment_num_successful_commits() {
122 model_neutral_
.num_successful_commits
++;
125 void StatusController::increment_num_updates_applied() {
126 model_neutral_
.num_updates_applied
++;
129 void StatusController::increment_num_encryption_conflicts() {
130 model_neutral_
.num_encryption_conflicts
++;
133 void StatusController::increment_num_server_conflicts() {
134 model_neutral_
.num_server_conflicts
++;
137 void StatusController::set_num_hierarchy_conflicts(int value
) {
138 model_neutral_
.num_hierarchy_conflicts
= value
;
141 void StatusController::increment_num_local_overwrites() {
142 model_neutral_
.num_local_overwrites
++;
145 void StatusController::increment_num_server_overwrites() {
146 model_neutral_
.num_server_overwrites
++;
149 void StatusController::set_sync_protocol_error(
150 const SyncProtocolError
& error
) {
151 model_neutral_
.sync_protocol_error
= error
;
154 void StatusController::set_last_get_key_result(const SyncerError result
) {
155 model_neutral_
.last_get_key_result
= result
;
158 void StatusController::set_last_download_updates_result(
159 const SyncerError result
) {
160 model_neutral_
.last_download_updates_result
= result
;
163 void StatusController::set_commit_result(const SyncerError result
) {
164 model_neutral_
.commit_result
= result
;
167 SyncerError
StatusController::last_get_key_result() const {
168 return model_neutral_
.last_get_key_result
;
171 void StatusController::update_conflicts_resolved(bool resolved
) {
172 model_neutral_
.conflicts_resolved
|= resolved
;
174 void StatusController::reset_conflicts_resolved() {
175 model_neutral_
.conflicts_resolved
= false;
178 // Returns the number of updates received from the sync server.
179 int64
StatusController::CountUpdates() const {
180 const sync_pb::ClientToServerResponse
& updates
=
181 model_neutral_
.updates_response
;
182 if (updates
.has_get_updates()) {
183 return updates
.get_updates().entries().size();
189 bool StatusController::HasConflictingUpdates() const {
190 DCHECK(!group_restriction_in_effect_
)
191 << "HasConflictingUpdates applies to all ModelSafeGroups";
192 std::map
<ModelSafeGroup
, PerModelSafeGroupState
*>::const_iterator it
=
193 per_model_group_
.begin();
194 for (; it
!= per_model_group_
.end(); ++it
) {
195 if (it
->second
->update_progress
.HasConflictingUpdates())
201 int StatusController::num_encryption_conflicts() const {
202 return model_neutral_
.num_encryption_conflicts
;
205 int StatusController::num_hierarchy_conflicts() const {
206 DCHECK(!group_restriction_in_effect_
)
207 << "num_hierarchy_conflicts applies to all ModelSafeGroups";
208 return model_neutral_
.num_hierarchy_conflicts
;
211 int StatusController::num_simple_conflicts() const {
212 DCHECK(!group_restriction_in_effect_
)
213 << "num_simple_conflicts applies to all ModelSafeGroups";
214 std::map
<ModelSafeGroup
, PerModelSafeGroupState
*>::const_iterator it
=
215 per_model_group_
.begin();
217 for (; it
!= per_model_group_
.end(); ++it
) {
218 sum
+= it
->second
->simple_conflict_ids
.size();
223 int StatusController::num_server_conflicts() const {
224 DCHECK(!group_restriction_in_effect_
)
225 << "num_server_conflicts applies to all ModelSafeGroups";
226 return model_neutral_
.num_server_conflicts
;
229 int StatusController::TotalNumConflictingItems() const {
230 DCHECK(!group_restriction_in_effect_
)
231 << "TotalNumConflictingItems applies to all ModelSafeGroups";
233 sum
+= num_simple_conflicts();
234 sum
+= num_encryption_conflicts();
235 sum
+= num_hierarchy_conflicts();
236 sum
+= num_server_conflicts();
240 bool StatusController::ServerSaysNothingMoreToDownload() const {
241 if (!download_updates_succeeded())
244 if (!updates_response().get_updates().has_changes_remaining()) {
245 NOTREACHED(); // Server should always send changes remaining.
246 return false; // Avoid looping forever.
248 // Changes remaining is an estimate, but if it's estimated to be
249 // zero, that's firm and we don't have to ask again.
250 return updates_response().get_updates().changes_remaining() == 0;
253 void StatusController::set_debug_info_sent() {
254 model_neutral_
.debug_info_sent
= true;
257 bool StatusController::debug_info_sent() const {
258 return model_neutral_
.debug_info_sent
;
261 } // namespace sessions
262 } // namespace syncer