| 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/session_state.h" | |
| 6 | |
| 7 #include <map> | |
| 8 #include <set> | |
| 9 #include <string> | |
| 10 #include <utility> | |
| 11 #include <vector> | |
| 12 | |
| 13 #include "base/base64.h" | |
| 14 #include "base/json/json_writer.h" | |
| 15 #include "base/memory/scoped_ptr.h" | |
| 16 #include "base/values.h" | |
| 17 #include "chrome/browser/sync/protocol/proto_enum_conversions.h" | |
| 18 | |
| 19 using std::set; | |
| 20 using std::vector; | |
| 21 | |
| 22 namespace browser_sync { | |
| 23 namespace sessions { | |
| 24 | |
| 25 SyncSourceInfo::SyncSourceInfo() | |
| 26 : updates_source(sync_pb::GetUpdatesCallerInfo::UNKNOWN) {} | |
| 27 | |
| 28 SyncSourceInfo::SyncSourceInfo( | |
| 29 const syncable::ModelTypePayloadMap& t) | |
| 30 : updates_source(sync_pb::GetUpdatesCallerInfo::UNKNOWN), types(t) {} | |
| 31 | |
| 32 SyncSourceInfo::SyncSourceInfo( | |
| 33 const sync_pb::GetUpdatesCallerInfo::GetUpdatesSource& u, | |
| 34 const syncable::ModelTypePayloadMap& t) | |
| 35 : updates_source(u), types(t) {} | |
| 36 | |
| 37 SyncSourceInfo::~SyncSourceInfo() {} | |
| 38 | |
| 39 DictionaryValue* SyncSourceInfo::ToValue() const { | |
| 40 DictionaryValue* value = new DictionaryValue(); | |
| 41 value->SetString("updatesSource", | |
| 42 GetUpdatesSourceString(updates_source)); | |
| 43 value->Set("types", syncable::ModelTypePayloadMapToValue(types)); | |
| 44 return value; | |
| 45 } | |
| 46 | |
| 47 SyncerStatus::SyncerStatus() | |
| 48 : invalid_store(false), | |
| 49 num_successful_commits(0), | |
| 50 num_successful_bookmark_commits(0), | |
| 51 num_updates_downloaded_total(0), | |
| 52 num_tombstone_updates_downloaded_total(0), | |
| 53 num_local_overwrites(0), | |
| 54 num_server_overwrites(0) { | |
| 55 } | |
| 56 | |
| 57 SyncerStatus::~SyncerStatus() { | |
| 58 } | |
| 59 | |
| 60 DictionaryValue* SyncerStatus::ToValue() const { | |
| 61 DictionaryValue* value = new DictionaryValue(); | |
| 62 value->SetBoolean("invalidStore", invalid_store); | |
| 63 value->SetInteger("numSuccessfulCommits", num_successful_commits); | |
| 64 value->SetInteger("numSuccessfulBookmarkCommits", | |
| 65 num_successful_bookmark_commits); | |
| 66 value->SetInteger("numUpdatesDownloadedTotal", | |
| 67 num_updates_downloaded_total); | |
| 68 value->SetInteger("numTombstoneUpdatesDownloadedTotal", | |
| 69 num_tombstone_updates_downloaded_total); | |
| 70 value->SetInteger("numLocalOverwrites", num_local_overwrites); | |
| 71 value->SetInteger("numServerOverwrites", num_server_overwrites); | |
| 72 return value; | |
| 73 } | |
| 74 | |
| 75 DictionaryValue* DownloadProgressMarkersToValue( | |
| 76 const std::string | |
| 77 (&download_progress_markers)[syncable::MODEL_TYPE_COUNT]) { | |
| 78 DictionaryValue* value = new DictionaryValue(); | |
| 79 for (int i = syncable::FIRST_REAL_MODEL_TYPE; | |
| 80 i < syncable::MODEL_TYPE_COUNT; ++i) { | |
| 81 // TODO(akalin): Unpack the value into a protobuf. | |
| 82 std::string base64_marker; | |
| 83 bool encoded = | |
| 84 base::Base64Encode(download_progress_markers[i], &base64_marker); | |
| 85 DCHECK(encoded); | |
| 86 value->SetString( | |
| 87 syncable::ModelTypeToString(syncable::ModelTypeFromInt(i)), | |
| 88 base64_marker); | |
| 89 } | |
| 90 return value; | |
| 91 } | |
| 92 | |
| 93 ErrorCounters::ErrorCounters() | |
| 94 : last_download_updates_result(UNSET), | |
| 95 last_post_commit_result(UNSET), | |
| 96 last_process_commit_response_result(UNSET) { | |
| 97 } | |
| 98 | |
| 99 SyncSessionSnapshot::SyncSessionSnapshot( | |
| 100 const SyncerStatus& syncer_status, | |
| 101 const ErrorCounters& errors, | |
| 102 int64 num_server_changes_remaining, | |
| 103 bool is_share_usable, | |
| 104 syncable::ModelTypeSet initial_sync_ended, | |
| 105 const std::string | |
| 106 (&download_progress_markers)[syncable::MODEL_TYPE_COUNT], | |
| 107 bool more_to_sync, | |
| 108 bool is_silenced, | |
| 109 int64 unsynced_count, | |
| 110 int num_encryption_conflicts, | |
| 111 int num_hierarchy_conflicts, | |
| 112 int num_simple_conflicts, | |
| 113 int num_server_conflicts, | |
| 114 bool did_commit_items, | |
| 115 const SyncSourceInfo& source, | |
| 116 size_t num_entries, | |
| 117 base::Time sync_start_time, | |
| 118 bool retry_scheduled) | |
| 119 : syncer_status(syncer_status), | |
| 120 errors(errors), | |
| 121 num_server_changes_remaining(num_server_changes_remaining), | |
| 122 is_share_usable(is_share_usable), | |
| 123 initial_sync_ended(initial_sync_ended), | |
| 124 download_progress_markers(), | |
| 125 has_more_to_sync(more_to_sync), | |
| 126 is_silenced(is_silenced), | |
| 127 unsynced_count(unsynced_count), | |
| 128 num_encryption_conflicts(num_encryption_conflicts), | |
| 129 num_hierarchy_conflicts(num_hierarchy_conflicts), | |
| 130 num_simple_conflicts(num_simple_conflicts), | |
| 131 num_server_conflicts(num_server_conflicts), | |
| 132 did_commit_items(did_commit_items), | |
| 133 source(source), | |
| 134 num_entries(num_entries), | |
| 135 sync_start_time(sync_start_time), | |
| 136 retry_scheduled(retry_scheduled) { | |
| 137 for (int i = syncable::FIRST_REAL_MODEL_TYPE; | |
| 138 i < syncable::MODEL_TYPE_COUNT; ++i) { | |
| 139 const_cast<std::string&>(this->download_progress_markers[i]).assign( | |
| 140 download_progress_markers[i]); | |
| 141 } | |
| 142 } | |
| 143 | |
| 144 SyncSessionSnapshot::~SyncSessionSnapshot() {} | |
| 145 | |
| 146 DictionaryValue* SyncSessionSnapshot::ToValue() const { | |
| 147 DictionaryValue* value = new DictionaryValue(); | |
| 148 value->Set("syncerStatus", syncer_status.ToValue()); | |
| 149 // We don't care too much if we lose precision here. | |
| 150 value->SetInteger("numServerChangesRemaining", | |
| 151 static_cast<int>(num_server_changes_remaining)); | |
| 152 value->SetBoolean("isShareUsable", is_share_usable); | |
| 153 value->Set("initialSyncEnded", | |
| 154 syncable::ModelTypeSetToValue(initial_sync_ended)); | |
| 155 value->Set("downloadProgressMarkers", | |
| 156 DownloadProgressMarkersToValue(download_progress_markers)); | |
| 157 value->SetBoolean("hasMoreToSync", has_more_to_sync); | |
| 158 value->SetBoolean("isSilenced", is_silenced); | |
| 159 // We don't care too much if we lose precision here, also. | |
| 160 value->SetInteger("unsyncedCount", | |
| 161 static_cast<int>(unsynced_count)); | |
| 162 value->SetInteger("numEncryptionConflicts", | |
| 163 num_encryption_conflicts); | |
| 164 value->SetInteger("numHierarchyConflicts", | |
| 165 num_hierarchy_conflicts); | |
| 166 value->SetInteger("numSimpleConflicts", | |
| 167 num_simple_conflicts); | |
| 168 value->SetInteger("numServerConflicts", | |
| 169 num_server_conflicts); | |
| 170 value->SetBoolean("didCommitItems", did_commit_items); | |
| 171 value->SetInteger("numEntries", num_entries); | |
| 172 value->Set("source", source.ToValue()); | |
| 173 return value; | |
| 174 } | |
| 175 | |
| 176 std::string SyncSessionSnapshot::ToString() const { | |
| 177 scoped_ptr<DictionaryValue> value(ToValue()); | |
| 178 std::string json; | |
| 179 base::JSONWriter::Write(value.get(), true, &json); | |
| 180 return json; | |
| 181 } | |
| 182 | |
| 183 ConflictProgress::ConflictProgress(bool* dirty_flag) | |
| 184 : num_server_conflicting_items(0), num_hierarchy_conflicting_items(0), | |
| 185 num_encryption_conflicting_items(0), dirty_(dirty_flag) { | |
| 186 } | |
| 187 | |
| 188 ConflictProgress::~ConflictProgress() { | |
| 189 } | |
| 190 | |
| 191 bool ConflictProgress::HasSimpleConflictItem(const syncable::Id& id) const { | |
| 192 return simple_conflicting_item_ids_.count(id) > 0; | |
| 193 } | |
| 194 | |
| 195 std::set<syncable::Id>::const_iterator | |
| 196 ConflictProgress::SimpleConflictingItemsBegin() const { | |
| 197 return simple_conflicting_item_ids_.begin(); | |
| 198 } | |
| 199 std::set<syncable::Id>::const_iterator | |
| 200 ConflictProgress::SimpleConflictingItemsEnd() const { | |
| 201 return simple_conflicting_item_ids_.end(); | |
| 202 } | |
| 203 | |
| 204 void ConflictProgress::AddSimpleConflictingItemById( | |
| 205 const syncable::Id& the_id) { | |
| 206 std::pair<std::set<syncable::Id>::iterator, bool> ret = | |
| 207 simple_conflicting_item_ids_.insert(the_id); | |
| 208 if (ret.second) | |
| 209 *dirty_ = true; | |
| 210 } | |
| 211 | |
| 212 void ConflictProgress::EraseSimpleConflictingItemById( | |
| 213 const syncable::Id& the_id) { | |
| 214 int items_erased = simple_conflicting_item_ids_.erase(the_id); | |
| 215 if (items_erased != 0) | |
| 216 *dirty_ = true; | |
| 217 } | |
| 218 | |
| 219 void ConflictProgress::AddEncryptionConflictingItemById( | |
| 220 const syncable::Id& the_id) { | |
| 221 std::pair<std::set<syncable::Id>::iterator, bool> ret = | |
| 222 unresolvable_conflicting_item_ids_.insert(the_id); | |
| 223 if (ret.second) { | |
| 224 num_encryption_conflicting_items++; | |
| 225 *dirty_ = true; | |
| 226 } | |
| 227 } | |
| 228 | |
| 229 void ConflictProgress::AddHierarchyConflictingItemById( | |
| 230 const syncable::Id& the_id) { | |
| 231 std::pair<std::set<syncable::Id>::iterator, bool> ret = | |
| 232 unresolvable_conflicting_item_ids_.insert(the_id); | |
| 233 if (ret.second) { | |
| 234 num_hierarchy_conflicting_items++; | |
| 235 *dirty_ = true; | |
| 236 } | |
| 237 } | |
| 238 | |
| 239 void ConflictProgress::AddServerConflictingItemById( | |
| 240 const syncable::Id& the_id) { | |
| 241 std::pair<std::set<syncable::Id>::iterator, bool> ret = | |
| 242 unresolvable_conflicting_item_ids_.insert(the_id); | |
| 243 if (ret.second) { | |
| 244 num_server_conflicting_items++; | |
| 245 *dirty_ = true; | |
| 246 } | |
| 247 } | |
| 248 | |
| 249 UpdateProgress::UpdateProgress() {} | |
| 250 | |
| 251 UpdateProgress::~UpdateProgress() {} | |
| 252 | |
| 253 void UpdateProgress::AddVerifyResult(const VerifyResult& verify_result, | |
| 254 const sync_pb::SyncEntity& entity) { | |
| 255 verified_updates_.push_back(std::make_pair(verify_result, entity)); | |
| 256 } | |
| 257 | |
| 258 void UpdateProgress::AddAppliedUpdate(const UpdateAttemptResponse& response, | |
| 259 const syncable::Id& id) { | |
| 260 applied_updates_.push_back(std::make_pair(response, id)); | |
| 261 } | |
| 262 | |
| 263 std::vector<AppliedUpdate>::iterator UpdateProgress::AppliedUpdatesBegin() { | |
| 264 return applied_updates_.begin(); | |
| 265 } | |
| 266 | |
| 267 std::vector<VerifiedUpdate>::const_iterator | |
| 268 UpdateProgress::VerifiedUpdatesBegin() const { | |
| 269 return verified_updates_.begin(); | |
| 270 } | |
| 271 | |
| 272 std::vector<AppliedUpdate>::const_iterator | |
| 273 UpdateProgress::AppliedUpdatesEnd() const { | |
| 274 return applied_updates_.end(); | |
| 275 } | |
| 276 | |
| 277 std::vector<VerifiedUpdate>::const_iterator | |
| 278 UpdateProgress::VerifiedUpdatesEnd() const { | |
| 279 return verified_updates_.end(); | |
| 280 } | |
| 281 | |
| 282 int UpdateProgress::SuccessfullyAppliedUpdateCount() const { | |
| 283 int count = 0; | |
| 284 for (std::vector<AppliedUpdate>::const_iterator it = | |
| 285 applied_updates_.begin(); | |
| 286 it != applied_updates_.end(); | |
| 287 ++it) { | |
| 288 if (it->first == SUCCESS) | |
| 289 count++; | |
| 290 } | |
| 291 return count; | |
| 292 } | |
| 293 | |
| 294 // Returns true if at least one update application failed due to a conflict | |
| 295 // during this sync cycle. | |
| 296 bool UpdateProgress::HasConflictingUpdates() const { | |
| 297 std::vector<AppliedUpdate>::const_iterator it; | |
| 298 for (it = applied_updates_.begin(); it != applied_updates_.end(); ++it) { | |
| 299 if (it->first != SUCCESS) { | |
| 300 return true; | |
| 301 } | |
| 302 } | |
| 303 return false; | |
| 304 } | |
| 305 | |
| 306 AllModelTypeState::AllModelTypeState(bool* dirty_flag) | |
| 307 : unsynced_handles(dirty_flag), | |
| 308 syncer_status(dirty_flag), | |
| 309 error(dirty_flag), | |
| 310 num_server_changes_remaining(dirty_flag, 0), | |
| 311 commit_set(ModelSafeRoutingInfo()) { | |
| 312 } | |
| 313 | |
| 314 AllModelTypeState::~AllModelTypeState() {} | |
| 315 | |
| 316 PerModelSafeGroupState::PerModelSafeGroupState(bool* dirty_flag) | |
| 317 : conflict_progress(dirty_flag) { | |
| 318 } | |
| 319 | |
| 320 PerModelSafeGroupState::~PerModelSafeGroupState() { | |
| 321 } | |
| 322 | |
| 323 } // namespace sessions | |
| 324 } // namespace browser_sync | |
| OLD | NEW |