| OLD | NEW |
| (Empty) | |
| 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 // The 'sessions' namespace comprises all the pieces of state that are |
| 6 // combined to form a SyncSession instance. In that way, it can be thought of |
| 7 // as an extension of the SyncSession type itself. Session scoping gives |
| 8 // context to things like "conflict progress", "update progress", etc, and the |
| 9 // separation this file provides allows clients to only include the parts they |
| 10 // need rather than the entire session stack. |
| 11 |
| 12 #ifndef CHROME_BROWSER_SYNC_SESSIONS_SESSION_STATE_H_ |
| 13 #define CHROME_BROWSER_SYNC_SESSIONS_SESSION_STATE_H_ |
| 14 |
| 15 #include <set> |
| 16 |
| 17 #include "base/basictypes.h" |
| 18 #include "chrome/browser/sync/engine/syncer_types.h" |
| 19 #include "chrome/browser/sync/engine/syncproto.h" |
| 20 |
| 21 namespace syncable { |
| 22 class DirectoryManager; |
| 23 } |
| 24 |
| 25 namespace browser_sync { |
| 26 namespace sessions { |
| 27 |
| 28 class UpdateProgress; |
| 29 |
| 30 // Data describing the progress made relative to the changelog on the server. |
| 31 struct ChangelogProgress { |
| 32 ChangelogProgress() : current_sync_timestamp(0), |
| 33 num_server_changes_remaining(0) {} |
| 34 int64 current_sync_timestamp; |
| 35 int64 num_server_changes_remaining; |
| 36 }; |
| 37 |
| 38 // Data pertaining to the status of an active Syncer object. |
| 39 struct SyncerStatus { |
| 40 SyncerStatus() |
| 41 : over_quota(false), invalid_store(false), syncer_stuck(false), |
| 42 syncing(false), num_successful_commits(0) {} |
| 43 bool over_quota; |
| 44 // True when we get such an INVALID_STORE error from the server. |
| 45 bool invalid_store; |
| 46 // True iff we're stuck. |
| 47 bool syncer_stuck; |
| 48 bool syncing; |
| 49 int num_successful_commits; |
| 50 }; |
| 51 |
| 52 // Counters for various errors that can occur repeatedly during a sync session. |
| 53 struct ErrorCounters { |
| 54 ErrorCounters() : num_conflicting_commits(0), |
| 55 consecutive_problem_get_updates(0), |
| 56 consecutive_problem_commits(0), |
| 57 consecutive_transient_error_commits(0), |
| 58 consecutive_errors(0) {} |
| 59 int num_conflicting_commits; |
| 60 // Is reset when we get any updates (not on pings) and increments whenever |
| 61 // the request fails. |
| 62 int consecutive_problem_get_updates; |
| 63 |
| 64 // Consecutive_problem_commits_ resets whenever we commit any number of items |
| 65 // and increments whenever all commits fail for any reason. |
| 66 int consecutive_problem_commits; |
| 67 |
| 68 // Number of commits hitting transient errors since the last successful |
| 69 // commit. |
| 70 int consecutive_transient_error_commits; |
| 71 |
| 72 // Incremented when get_updates fails, commit fails, and when hitting |
| 73 // transient errors. When any of these succeed, this counter is reset. |
| 74 // TODO(chron): Reduce number of weird counters we use. |
| 75 int consecutive_errors; |
| 76 }; |
| 77 |
| 78 // An immutable snapshot of state from a SyncSession. Convenient to use as |
| 79 // part of notifications as it is inherently thread-safe. |
| 80 struct SyncSessionSnapshot { |
| 81 SyncSessionSnapshot(const SyncerStatus& syncer_status, |
| 82 const ErrorCounters& errors, |
| 83 const ChangelogProgress& changelog_progress, bool is_share_usable, |
| 84 bool more_to_sync, bool is_silenced, int64 unsynced_count, |
| 85 int num_conflicting_updates, bool did_commit_items) |
| 86 : syncer_status(syncer_status), |
| 87 errors(errors), |
| 88 changelog_progress(changelog_progress), |
| 89 is_share_usable(is_share_usable), |
| 90 has_more_to_sync(more_to_sync), |
| 91 is_silenced(is_silenced), |
| 92 unsynced_count(unsynced_count), |
| 93 num_conflicting_updates(num_conflicting_updates), |
| 94 did_commit_items(did_commit_items) {} |
| 95 const SyncerStatus syncer_status; |
| 96 const ErrorCounters errors; |
| 97 const ChangelogProgress changelog_progress; |
| 98 const bool is_share_usable; |
| 99 const bool has_more_to_sync; |
| 100 const bool is_silenced; |
| 101 const int64 unsynced_count; |
| 102 const int num_conflicting_updates; |
| 103 const bool did_commit_items; |
| 104 }; |
| 105 |
| 106 // Tracks progress of conflicts and their resolution using conflict sets. |
| 107 class ConflictProgress { |
| 108 public: |
| 109 ConflictProgress() : progress_changed_(false) {} |
| 110 ~ConflictProgress() { CleanupSets(); } |
| 111 // Various iterators, size, and retrieval functions for conflict sets. |
| 112 IdToConflictSetMap::const_iterator IdToConflictSetBegin() const; |
| 113 IdToConflictSetMap::const_iterator IdToConflictSetEnd() const; |
| 114 IdToConflictSetMap::size_type IdToConflictSetSize() const; |
| 115 IdToConflictSetMap::const_iterator IdToConflictSetFind( |
| 116 const syncable::Id& the_id) const; |
| 117 const ConflictSet* IdToConflictSetGet(const syncable::Id& the_id); |
| 118 std::set<ConflictSet*>::const_iterator ConflictSetsBegin() const; |
| 119 std::set<ConflictSet*>::const_iterator ConflictSetsEnd() const; |
| 120 std::set<ConflictSet*>::size_type ConflictSetsSize() const; |
| 121 |
| 122 // Various mutators for tracking commit conflicts. |
| 123 void AddConflictingItemById(const syncable::Id& the_id); |
| 124 void EraseConflictingItemById(const syncable::Id& the_id); |
| 125 int ConflictingItemsSize() const { return conflicting_item_ids_.size(); } |
| 126 std::set<syncable::Id>::iterator ConflictingItemsBegin(); |
| 127 std::set<syncable::Id>::const_iterator ConflictingItemsBeginConst() const; |
| 128 std::set<syncable::Id>::const_iterator ConflictingItemsEnd() const; |
| 129 |
| 130 void MergeSets(const syncable::Id& set1, const syncable::Id& set2); |
| 131 void CleanupSets(); |
| 132 |
| 133 bool progress_changed() const { return progress_changed_; } |
| 134 void reset_progress_changed() { progress_changed_ = false; } |
| 135 private: |
| 136 // TODO(sync): move away from sets if it makes more sense. |
| 137 std::set<syncable::Id> conflicting_item_ids_; |
| 138 std::map<syncable::Id, ConflictSet*> id_to_conflict_set_; |
| 139 std::set<ConflictSet*> conflict_sets_; |
| 140 |
| 141 // Whether a conflicting item was added or removed since |
| 142 // the last call to reset_progress_changed(), if any. |
| 143 bool progress_changed_; |
| 144 }; |
| 145 |
| 146 typedef std::pair<VerifyResult, sync_pb::SyncEntity> VerifiedUpdate; |
| 147 typedef std::pair<UpdateAttemptResponse, syncable::Id> AppliedUpdate; |
| 148 |
| 149 // Tracks update application and verification. |
| 150 class UpdateProgress { |
| 151 public: |
| 152 void AddVerifyResult(const VerifyResult& verify_result, |
| 153 const sync_pb::SyncEntity& entity); |
| 154 |
| 155 // Log a successful or failing update attempt. |
| 156 void AddAppliedUpdate(const UpdateAttemptResponse& response, |
| 157 const syncable::Id& id); |
| 158 |
| 159 // Various iterators. |
| 160 std::vector<AppliedUpdate>::iterator AppliedUpdatesBegin(); |
| 161 std::vector<VerifiedUpdate>::const_iterator VerifiedUpdatesBegin() const; |
| 162 std::vector<AppliedUpdate>::const_iterator AppliedUpdatesEnd() const; |
| 163 std::vector<VerifiedUpdate>::const_iterator VerifiedUpdatesEnd() const; |
| 164 |
| 165 // Returns the number of update application attempts. This includes both |
| 166 // failures and successes. |
| 167 int AppliedUpdatesSize() const { return applied_updates_.size(); } |
| 168 int VerifiedUpdatesSize() const { return verified_updates_.size(); } |
| 169 bool HasVerifiedUpdates() const { return !verified_updates_.empty(); } |
| 170 bool HasAppliedUpdates() const { return !applied_updates_.empty(); } |
| 171 |
| 172 // Count the number of successful update applications that have happend this |
| 173 // cycle. Note that if an item is successfully applied twice, it will be |
| 174 // double counted here. |
| 175 int SuccessfullyAppliedUpdateCount() const; |
| 176 |
| 177 // Returns true if at least one update application failed due to a conflict |
| 178 // during this sync cycle. |
| 179 bool HasConflictingUpdates() const; |
| 180 |
| 181 private: |
| 182 // Some container for updates that failed verification. |
| 183 std::vector<VerifiedUpdate> verified_updates_; |
| 184 |
| 185 // Stores the result of the various ApplyUpdate attempts we've made. |
| 186 // May contain duplicate entries. |
| 187 std::vector<AppliedUpdate> applied_updates_; |
| 188 }; |
| 189 |
| 190 // Transient state is a physical grouping of session state that can be reset |
| 191 // while that session is in flight. This is useful when multiple |
| 192 // SyncShare operations take place during a session. |
| 193 struct TransientState { |
| 194 TransientState() |
| 195 : conflict_sets_built(false), |
| 196 conflicts_resolved(false), |
| 197 items_committed(false), |
| 198 timestamp_dirty(false) {} |
| 199 UpdateProgress update_progress; |
| 200 ClientToServerMessage commit_message; |
| 201 ClientToServerResponse commit_response; |
| 202 ClientToServerResponse updates_response; |
| 203 std::vector<int64> unsynced_handles; |
| 204 std::vector<syncable::Id> commit_ids; |
| 205 bool conflict_sets_built; |
| 206 bool conflicts_resolved; |
| 207 bool items_committed; |
| 208 bool timestamp_dirty; |
| 209 }; |
| 210 |
| 211 } // namespace sessions |
| 212 } // namespace browser_sync |
| 213 |
| 214 #endif // CHROME_BROWSER_SYNC_SESSIONS_SESSION_STATE_H_ |
| OLD | NEW |