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 "sync/internal_api/public/sessions/sync_session_snapshot.h" 6 7 #include "base/json/json_writer.h" 8 #include "base/memory/scoped_ptr.h" 9 #include "base/values.h" 10 #include "sync/protocol/proto_enum_conversions.h" 11 12 namespace syncer { 13 namespace sessions { 14 15 SyncSessionSnapshot::SyncSessionSnapshot() 16 : is_silenced_(false), 17 num_encryption_conflicts_(0), 18 num_hierarchy_conflicts_(0), 19 num_server_conflicts_(0), 20 notifications_enabled_(false), 21 num_entries_(0), 22 num_entries_by_type_(MODEL_TYPE_COUNT, 0), 23 num_to_delete_entries_by_type_(MODEL_TYPE_COUNT, 0), 24 is_initialized_(false) { 25 } 26 27 SyncSessionSnapshot::SyncSessionSnapshot( 28 const ModelNeutralState& model_neutral_state, 29 const ProgressMarkerMap& download_progress_markers, 30 bool is_silenced, 31 int num_encryption_conflicts, 32 int num_hierarchy_conflicts, 33 int num_server_conflicts, 34 bool notifications_enabled, 35 size_t num_entries, 36 base::Time sync_start_time, 37 const std::vector<int>& num_entries_by_type, 38 const std::vector<int>& num_to_delete_entries_by_type, 39 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource legacy_updates_source) 40 : model_neutral_state_(model_neutral_state), 41 download_progress_markers_(download_progress_markers), 42 is_silenced_(is_silenced), 43 num_encryption_conflicts_(num_encryption_conflicts), 44 num_hierarchy_conflicts_(num_hierarchy_conflicts), 45 num_server_conflicts_(num_server_conflicts), 46 notifications_enabled_(notifications_enabled), 47 num_entries_(num_entries), 48 sync_start_time_(sync_start_time), 49 num_entries_by_type_(num_entries_by_type), 50 num_to_delete_entries_by_type_(num_to_delete_entries_by_type), 51 legacy_updates_source_(legacy_updates_source), 52 is_initialized_(true) { 53 } 54 55 SyncSessionSnapshot::~SyncSessionSnapshot() {} 56 57 base::DictionaryValue* SyncSessionSnapshot::ToValue() const { 58 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); 59 value->SetInteger("numSuccessfulCommits", 60 model_neutral_state_.num_successful_commits); 61 value->SetInteger("numSuccessfulBookmarkCommits", 62 model_neutral_state_.num_successful_bookmark_commits); 63 value->SetInteger("numUpdatesDownloadedTotal", 64 model_neutral_state_.num_updates_downloaded_total); 65 value->SetInteger("numTombstoneUpdatesDownloadedTotal", 66 model_neutral_state_.num_tombstone_updates_downloaded_total); 67 value->SetInteger("numReflectedUpdatesDownloadedTotal", 68 model_neutral_state_.num_reflected_updates_downloaded_total); 69 value->SetInteger("numLocalOverwrites", 70 model_neutral_state_.num_local_overwrites); 71 value->SetInteger("numServerOverwrites", 72 model_neutral_state_.num_server_overwrites); 73 value->SetInteger( 74 "numServerChangesRemaining", 75 static_cast<int>(model_neutral_state_.num_server_changes_remaining)); 76 value->Set("downloadProgressMarkers", 77 ProgressMarkerMapToValue(download_progress_markers_).release()); 78 value->SetBoolean("isSilenced", is_silenced_); 79 // We don't care too much if we lose precision here, also. 80 value->SetInteger("numEncryptionConflicts", 81 num_encryption_conflicts_); 82 value->SetInteger("numHierarchyConflicts", 83 num_hierarchy_conflicts_); 84 value->SetInteger("numServerConflicts", 85 num_server_conflicts_); 86 value->SetInteger("numEntries", num_entries_); 87 value->SetString("legacySource", 88 GetUpdatesSourceString(legacy_updates_source_)); 89 value->SetBoolean("notificationsEnabled", notifications_enabled_); 90 91 scoped_ptr<base::DictionaryValue> counter_entries( 92 new base::DictionaryValue()); 93 for (int i = FIRST_REAL_MODEL_TYPE; i < MODEL_TYPE_COUNT; i++) { 94 scoped_ptr<base::DictionaryValue> type_entries(new base::DictionaryValue()); 95 type_entries->SetInteger("numEntries", num_entries_by_type_[i]); 96 type_entries->SetInteger("numToDeleteEntries", 97 num_to_delete_entries_by_type_[i]); 98 99 const std::string model_type = ModelTypeToString(static_cast<ModelType>(i)); 100 counter_entries->Set(model_type, type_entries.release()); 101 } 102 value->Set("counter_entries", counter_entries.release()); 103 return value.release(); 104 } 105 106 std::string SyncSessionSnapshot::ToString() const { 107 scoped_ptr<base::DictionaryValue> value(ToValue()); 108 std::string json; 109 base::JSONWriter::WriteWithOptions(value.get(), 110 base::JSONWriter::OPTIONS_PRETTY_PRINT, 111 &json); 112 return json; 113 } 114 115 int64 SyncSessionSnapshot::num_server_changes_remaining() const { 116 return model_neutral_state().num_server_changes_remaining; 117 } 118 119 const ProgressMarkerMap& 120 SyncSessionSnapshot::download_progress_markers() const { 121 return download_progress_markers_; 122 } 123 124 bool SyncSessionSnapshot::is_silenced() const { 125 return is_silenced_; 126 } 127 128 int SyncSessionSnapshot::num_encryption_conflicts() const { 129 return num_encryption_conflicts_; 130 } 131 132 int SyncSessionSnapshot::num_hierarchy_conflicts() const { 133 return num_hierarchy_conflicts_; 134 } 135 136 int SyncSessionSnapshot::num_server_conflicts() const { 137 return num_server_conflicts_; 138 } 139 140 bool SyncSessionSnapshot::notifications_enabled() const { 141 return notifications_enabled_; 142 } 143 144 size_t SyncSessionSnapshot::num_entries() const { 145 return num_entries_; 146 } 147 148 base::Time SyncSessionSnapshot::sync_start_time() const { 149 return sync_start_time_; 150 } 151 152 bool SyncSessionSnapshot::is_initialized() const { 153 return is_initialized_; 154 } 155 156 const std::vector<int>& SyncSessionSnapshot::num_entries_by_type() const { 157 return num_entries_by_type_; 158 } 159 160 const std::vector<int>& 161 SyncSessionSnapshot::num_to_delete_entries_by_type() const { 162 return num_to_delete_entries_by_type_; 163 } 164 165 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource 166 SyncSessionSnapshot::legacy_updates_source() const { 167 return legacy_updates_source_; 168 } 169 170 } // namespace sessions 171 } // namespace syncer 172