| 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/engine/all_status.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/port.h" | |
| 11 #include "chrome/browser/sync/engine/net/server_connection_manager.h" | |
| 12 #include "chrome/browser/sync/protocol/service_constants.h" | |
| 13 #include "chrome/browser/sync/sessions/session_state.h" | |
| 14 #include "chrome/browser/sync/syncable/model_type.h" | |
| 15 | |
| 16 namespace browser_sync { | |
| 17 | |
| 18 AllStatus::AllStatus() { | |
| 19 status_.summary = sync_api::SyncManager::Status::OFFLINE; | |
| 20 status_.initial_sync_ended = true; | |
| 21 status_.notifications_enabled = false; | |
| 22 status_.cryptographer_ready = false; | |
| 23 status_.crypto_has_pending_keys = false; | |
| 24 } | |
| 25 | |
| 26 AllStatus::~AllStatus() { | |
| 27 } | |
| 28 | |
| 29 sync_api::SyncManager::Status AllStatus::CreateBlankStatus() const { | |
| 30 // Status is initialized with the previous status value. Variables | |
| 31 // whose values accumulate (e.g. lifetime counters like updates_received) | |
| 32 // are not to be cleared here. | |
| 33 sync_api::SyncManager::Status status = status_; | |
| 34 status.unsynced_count = 0; | |
| 35 status.encryption_conflicts = 0; | |
| 36 status.hierarchy_conflicts = 0; | |
| 37 status.simple_conflicts = 0; | |
| 38 status.server_conflicts = 0; | |
| 39 status.committed_count = 0; | |
| 40 status.initial_sync_ended = false; | |
| 41 status.updates_available = 0; | |
| 42 return status; | |
| 43 } | |
| 44 | |
| 45 sync_api::SyncManager::Status AllStatus::CalcSyncing( | |
| 46 const SyncEngineEvent &event) const { | |
| 47 sync_api::SyncManager::Status status = CreateBlankStatus(); | |
| 48 const sessions::SyncSessionSnapshot* snapshot = event.snapshot; | |
| 49 status.unsynced_count = static_cast<int>(snapshot->unsynced_count); | |
| 50 status.encryption_conflicts = snapshot->num_encryption_conflicts; | |
| 51 status.hierarchy_conflicts = snapshot->num_hierarchy_conflicts; | |
| 52 status.simple_conflicts = snapshot->num_simple_conflicts; | |
| 53 status.server_conflicts = snapshot->num_server_conflicts; | |
| 54 status.committed_count = snapshot->syncer_status.num_successful_commits; | |
| 55 | |
| 56 if (event.what_happened == SyncEngineEvent::SYNC_CYCLE_BEGIN) { | |
| 57 status.syncing = true; | |
| 58 } else if (event.what_happened == SyncEngineEvent::SYNC_CYCLE_ENDED) { | |
| 59 status.syncing = false; | |
| 60 } | |
| 61 | |
| 62 status.initial_sync_ended |= snapshot->is_share_usable; | |
| 63 | |
| 64 status.updates_available += snapshot->num_server_changes_remaining; | |
| 65 status.sync_protocol_error = snapshot->errors.sync_protocol_error; | |
| 66 | |
| 67 // Accumulate update count only once per session to avoid double-counting. | |
| 68 // TODO(ncarter): Make this realtime by having the syncer_status | |
| 69 // counter preserve its value across sessions. http://crbug.com/26339 | |
| 70 if (event.what_happened == SyncEngineEvent::SYNC_CYCLE_ENDED) { | |
| 71 status.updates_received += | |
| 72 snapshot->syncer_status.num_updates_downloaded_total; | |
| 73 status.tombstone_updates_received += | |
| 74 snapshot->syncer_status.num_tombstone_updates_downloaded_total; | |
| 75 status.num_local_overwrites_total += | |
| 76 snapshot->syncer_status.num_local_overwrites; | |
| 77 status.num_server_overwrites_total += | |
| 78 snapshot->syncer_status.num_server_overwrites; | |
| 79 if (snapshot->syncer_status.num_updates_downloaded_total == 0) { | |
| 80 ++status.empty_get_updates; | |
| 81 } else { | |
| 82 ++status.nonempty_get_updates; | |
| 83 } | |
| 84 if (snapshot->syncer_status.num_successful_commits == 0) { | |
| 85 ++status.sync_cycles_without_commits; | |
| 86 } else { | |
| 87 ++status.sync_cycles_with_commits; | |
| 88 } | |
| 89 if (snapshot->syncer_status.num_successful_commits == 0 && | |
| 90 snapshot->syncer_status.num_updates_downloaded_total == 0) { | |
| 91 ++status.useless_sync_cycles; | |
| 92 } else { | |
| 93 ++status.useful_sync_cycles; | |
| 94 } | |
| 95 } | |
| 96 return status; | |
| 97 } | |
| 98 | |
| 99 void AllStatus::CalcStatusChanges() { | |
| 100 const bool unsynced_changes = status_.unsynced_count > 0; | |
| 101 const bool online = status_.authenticated && | |
| 102 status_.server_reachable && status_.server_up; | |
| 103 if (online) { | |
| 104 if (status_.syncing) | |
| 105 status_.summary = sync_api::SyncManager::Status::SYNCING; | |
| 106 else | |
| 107 status_.summary = sync_api::SyncManager::Status::READY; | |
| 108 } else if (!status_.initial_sync_ended) { | |
| 109 status_.summary = sync_api::SyncManager::Status::OFFLINE_UNUSABLE; | |
| 110 } else if (unsynced_changes) { | |
| 111 status_.summary = sync_api::SyncManager::Status::OFFLINE_UNSYNCED; | |
| 112 } else { | |
| 113 status_.summary = sync_api::SyncManager::Status::OFFLINE; | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 void AllStatus::OnSyncEngineEvent(const SyncEngineEvent& event) { | |
| 118 ScopedStatusLock lock(this); | |
| 119 switch (event.what_happened) { | |
| 120 case SyncEngineEvent::SYNC_CYCLE_BEGIN: | |
| 121 case SyncEngineEvent::STATUS_CHANGED: | |
| 122 case SyncEngineEvent::SYNC_CYCLE_ENDED: | |
| 123 status_ = CalcSyncing(event); | |
| 124 break; | |
| 125 case SyncEngineEvent::STOP_SYNCING_PERMANENTLY: | |
| 126 case SyncEngineEvent::UPDATED_TOKEN: | |
| 127 case SyncEngineEvent::CLEAR_SERVER_DATA_FAILED: | |
| 128 case SyncEngineEvent::CLEAR_SERVER_DATA_SUCCEEDED: | |
| 129 break; | |
| 130 case SyncEngineEvent::ACTIONABLE_ERROR: | |
| 131 status_ = CreateBlankStatus(); | |
| 132 status_.sync_protocol_error = event.snapshot->errors.sync_protocol_error; | |
| 133 break; | |
| 134 default: | |
| 135 LOG(ERROR) << "Unrecognized Syncer Event: " << event.what_happened; | |
| 136 break; | |
| 137 } | |
| 138 } | |
| 139 | |
| 140 void AllStatus::HandleServerConnectionEvent( | |
| 141 const ServerConnectionEvent& event) { | |
| 142 ScopedStatusLock lock(this); | |
| 143 status_.server_up = IsGoodReplyFromServer(event.connection_code); | |
| 144 status_.server_reachable = event.server_reachable; | |
| 145 | |
| 146 if (event.connection_code == HttpResponse::SERVER_CONNECTION_OK) { | |
| 147 status_.authenticated = true; | |
| 148 } else { | |
| 149 status_.authenticated = false; | |
| 150 } | |
| 151 } | |
| 152 | |
| 153 sync_api::SyncManager::Status AllStatus::status() const { | |
| 154 base::AutoLock lock(mutex_); | |
| 155 return status_; | |
| 156 } | |
| 157 | |
| 158 void AllStatus::SetNotificationsEnabled(bool notifications_enabled) { | |
| 159 ScopedStatusLock lock(this); | |
| 160 status_.notifications_enabled = notifications_enabled; | |
| 161 } | |
| 162 | |
| 163 void AllStatus::IncrementNotificationsReceived() { | |
| 164 ScopedStatusLock lock(this); | |
| 165 ++status_.notifications_received; | |
| 166 } | |
| 167 | |
| 168 void AllStatus::SetEncryptedTypes(syncable::ModelTypeSet types) { | |
| 169 ScopedStatusLock lock(this); | |
| 170 status_.encrypted_types = types; | |
| 171 } | |
| 172 | |
| 173 void AllStatus::SetCryptographerReady(bool ready) { | |
| 174 ScopedStatusLock lock(this); | |
| 175 status_.cryptographer_ready = ready; | |
| 176 } | |
| 177 | |
| 178 void AllStatus::SetCryptoHasPendingKeys(bool has_pending_keys) { | |
| 179 ScopedStatusLock lock(this); | |
| 180 status_.crypto_has_pending_keys = has_pending_keys; | |
| 181 } | |
| 182 | |
| 183 void AllStatus::SetUniqueId(const std::string& guid) { | |
| 184 ScopedStatusLock lock(this); | |
| 185 status_.unique_id = guid; | |
| 186 } | |
| 187 | |
| 188 ScopedStatusLock::ScopedStatusLock(AllStatus* allstatus) | |
| 189 : allstatus_(allstatus) { | |
| 190 allstatus->mutex_.Acquire(); | |
| 191 } | |
| 192 | |
| 193 ScopedStatusLock::~ScopedStatusLock() { | |
| 194 allstatus_->CalcStatusChanges(); | |
| 195 allstatus_->mutex_.Release(); | |
| 196 } | |
| 197 | |
| 198 } // namespace browser_sync | |
| OLD | NEW |