session_state.cc revision 731df977c0511bca2206b5f333555b1205ff1f43
1// Copyright (c) 2009 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/session_state.h"
6
7#include <set>
8#include <vector>
9
10using std::set;
11using std::vector;
12
13namespace browser_sync {
14namespace sessions {
15
16SyncSessionSnapshot::SyncSessionSnapshot(
17    const SyncerStatus& syncer_status,
18    const ErrorCounters& errors,
19    int64 num_server_changes_remaining,
20    int64 max_local_timestamp,
21    bool is_share_usable,
22    const syncable::ModelTypeBitSet& initial_sync_ended,
23    bool more_to_sync,
24    bool is_silenced,
25    int64 unsynced_count,
26    int num_conflicting_updates,
27    bool did_commit_items)
28    : syncer_status(syncer_status),
29      errors(errors),
30      num_server_changes_remaining(num_server_changes_remaining),
31      max_local_timestamp(max_local_timestamp),
32      is_share_usable(is_share_usable),
33      initial_sync_ended(initial_sync_ended),
34      has_more_to_sync(more_to_sync),
35      is_silenced(is_silenced),
36      unsynced_count(unsynced_count),
37      num_conflicting_updates(num_conflicting_updates),
38      did_commit_items(did_commit_items) {
39}
40
41SyncSessionSnapshot::~SyncSessionSnapshot() {}
42
43ConflictProgress::ConflictProgress(bool* dirty_flag) : dirty_(dirty_flag) {}
44
45ConflictProgress::~ConflictProgress() {
46  CleanupSets();
47}
48
49IdToConflictSetMap::const_iterator ConflictProgress::IdToConflictSetFind(
50    const syncable::Id& the_id) const {
51  return id_to_conflict_set_.find(the_id);
52}
53
54IdToConflictSetMap::const_iterator
55ConflictProgress::IdToConflictSetBegin() const {
56  return id_to_conflict_set_.begin();
57}
58
59IdToConflictSetMap::const_iterator
60ConflictProgress::IdToConflictSetEnd() const {
61  return id_to_conflict_set_.end();
62}
63
64IdToConflictSetMap::size_type ConflictProgress::IdToConflictSetSize() const {
65  return id_to_conflict_set_.size();
66}
67
68const ConflictSet* ConflictProgress::IdToConflictSetGet(
69    const syncable::Id& the_id) {
70  return id_to_conflict_set_[the_id];
71}
72
73std::set<ConflictSet*>::const_iterator
74ConflictProgress::ConflictSetsBegin() const {
75  return conflict_sets_.begin();
76}
77
78std::set<ConflictSet*>::const_iterator
79ConflictProgress::ConflictSetsEnd() const {
80  return conflict_sets_.end();
81}
82
83std::set<ConflictSet*>::size_type
84ConflictProgress::ConflictSetsSize() const {
85  return conflict_sets_.size();
86}
87
88std::set<syncable::Id>::iterator
89ConflictProgress::ConflictingItemsBegin() {
90  return conflicting_item_ids_.begin();
91}
92std::set<syncable::Id>::const_iterator
93ConflictProgress::ConflictingItemsBeginConst() const {
94  return conflicting_item_ids_.begin();
95}
96std::set<syncable::Id>::const_iterator
97ConflictProgress::ConflictingItemsEnd() const {
98  return conflicting_item_ids_.end();
99}
100
101void ConflictProgress::AddConflictingItemById(const syncable::Id& the_id) {
102  std::pair<std::set<syncable::Id>::iterator, bool> ret =
103    conflicting_item_ids_.insert(the_id);
104  if (ret.second)
105    *dirty_ = true;
106}
107
108void ConflictProgress::EraseConflictingItemById(const syncable::Id& the_id) {
109  int items_erased = conflicting_item_ids_.erase(the_id);
110  if (items_erased != 0)
111    *dirty_ = true;
112}
113
114void ConflictProgress::MergeSets(const syncable::Id& id1,
115                                 const syncable::Id& id2) {
116  // There are no single item sets, we just leave those entries == 0
117  vector<syncable::Id>* set1 = id_to_conflict_set_[id1];
118  vector<syncable::Id>* set2 = id_to_conflict_set_[id2];
119  vector<syncable::Id>* rv = 0;
120  if (0 == set1 && 0 == set2) {
121    // Neither item currently has a set so we build one.
122    rv = new vector<syncable::Id>();
123    rv->push_back(id1);
124    if (id1 != id2) {
125      rv->push_back(id2);
126    } else {
127      LOG(WARNING) << "[BUG] Attempting to merge two identical conflict ids.";
128    }
129    conflict_sets_.insert(rv);
130  } else if (0 == set1) {
131    // Add the item to the existing set.
132    rv = set2;
133    rv->push_back(id1);
134  } else if (0 == set2) {
135    // Add the item to the existing set.
136    rv = set1;
137    rv->push_back(id2);
138  } else if (set1 == set2) {
139    // It's the same set already.
140    return;
141  } else {
142    // Merge the two sets.
143    rv = set1;
144    // Point all the second sets id's back to the first.
145    vector<syncable::Id>::iterator i;
146    for (i = set2->begin() ; i != set2->end() ; ++i) {
147      id_to_conflict_set_[*i] = rv;
148    }
149    // Copy the second set to the first.
150    rv->insert(rv->end(), set2->begin(), set2->end());
151    conflict_sets_.erase(set2);
152    delete set2;
153  }
154  id_to_conflict_set_[id1] = id_to_conflict_set_[id2] = rv;
155}
156
157void ConflictProgress::CleanupSets() {
158  // Clean up all the sets.
159  set<ConflictSet*>::iterator i;
160  for (i = conflict_sets_.begin(); i != conflict_sets_.end(); i++) {
161    delete *i;
162  }
163  conflict_sets_.clear();
164  id_to_conflict_set_.clear();
165}
166
167UpdateProgress::UpdateProgress() {}
168
169UpdateProgress::~UpdateProgress() {}
170
171void UpdateProgress::AddVerifyResult(const VerifyResult& verify_result,
172                                     const sync_pb::SyncEntity& entity) {
173  verified_updates_.push_back(std::make_pair(verify_result, entity));
174}
175
176void UpdateProgress::AddAppliedUpdate(const UpdateAttemptResponse& response,
177    const syncable::Id& id) {
178  applied_updates_.push_back(std::make_pair(response, id));
179}
180
181std::vector<AppliedUpdate>::iterator UpdateProgress::AppliedUpdatesBegin() {
182  return applied_updates_.begin();
183}
184
185std::vector<VerifiedUpdate>::const_iterator
186UpdateProgress::VerifiedUpdatesBegin() const {
187  return verified_updates_.begin();
188}
189
190std::vector<AppliedUpdate>::const_iterator
191UpdateProgress::AppliedUpdatesEnd() const {
192  return applied_updates_.end();
193}
194
195std::vector<VerifiedUpdate>::const_iterator
196UpdateProgress::VerifiedUpdatesEnd() const {
197  return verified_updates_.end();
198}
199
200int UpdateProgress::SuccessfullyAppliedUpdateCount() const {
201  int count = 0;
202  for (std::vector<AppliedUpdate>::const_iterator it =
203       applied_updates_.begin();
204       it != applied_updates_.end();
205       ++it) {
206    if (it->first == SUCCESS)
207      count++;
208  }
209  return count;
210}
211
212// Returns true if at least one update application failed due to a conflict
213// during this sync cycle.
214bool UpdateProgress::HasConflictingUpdates() const {
215  std::vector<AppliedUpdate>::const_iterator it;
216  for (it = applied_updates_.begin(); it != applied_updates_.end(); ++it) {
217    if (it->first == CONFLICT) {
218      return true;
219    }
220  }
221  return false;
222}
223
224AllModelTypeState::AllModelTypeState(bool* dirty_flag)
225    : unsynced_handles(dirty_flag),
226      syncer_status(dirty_flag),
227      error_counters(dirty_flag),
228      num_server_changes_remaining(dirty_flag, 0),
229      commit_set(ModelSafeRoutingInfo()) {
230}
231
232AllModelTypeState::~AllModelTypeState() {}
233
234PerModelSafeGroupState::PerModelSafeGroupState(bool* dirty_flag)
235    : conflict_progress(dirty_flag) {
236}
237
238PerModelSafeGroupState::~PerModelSafeGroupState() {
239}
240
241PerModelTypeState::PerModelTypeState(bool* dirty_flag)
242    : current_download_timestamp(dirty_flag, 0) {
243}
244
245PerModelTypeState::~PerModelTypeState() {
246}
247
248}  // namespace sessions
249}  // namespace browser_sync
250