| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "sync/sessions/sync_session.h" | 5 #include "sync/sessions/sync_session.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <iterator> | 8 #include <iterator> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 SyncSession::SyncSession(SyncSessionContext* context, Delegate* delegate, | 70 SyncSession::SyncSession(SyncSessionContext* context, Delegate* delegate, |
| 71 const SyncSourceInfo& source, | 71 const SyncSourceInfo& source, |
| 72 const ModelSafeRoutingInfo& routing_info, | 72 const ModelSafeRoutingInfo& routing_info, |
| 73 const std::vector<ModelSafeWorker*>& workers) | 73 const std::vector<ModelSafeWorker*>& workers) |
| 74 : context_(context), | 74 : context_(context), |
| 75 source_(source), | 75 source_(source), |
| 76 write_transaction_(NULL), | 76 write_transaction_(NULL), |
| 77 delegate_(delegate), | 77 delegate_(delegate), |
| 78 workers_(workers), | 78 workers_(workers), |
| 79 routing_info_(routing_info), | 79 routing_info_(routing_info), |
| 80 enabled_groups_(ComputeEnabledGroups(routing_info_, workers_)) { | 80 enabled_groups_(ComputeEnabledGroups(routing_info_, workers_)), |
| 81 finished_(false) { |
| 81 status_controller_.reset(new StatusController(routing_info_)); | 82 status_controller_.reset(new StatusController(routing_info_)); |
| 82 std::sort(workers_.begin(), workers_.end()); | 83 std::sort(workers_.begin(), workers_.end()); |
| 83 } | 84 } |
| 84 | 85 |
| 85 SyncSession::~SyncSession() {} | 86 SyncSession::~SyncSession() {} |
| 86 | 87 |
| 87 void SyncSession::Coalesce(const SyncSession& session) { | 88 void SyncSession::Coalesce(const SyncSession& session) { |
| 88 if (context_ != session.context() || delegate_ != session.delegate_) { | 89 if (context_ != session.context() || delegate_ != session.delegate_) { |
| 89 NOTREACHED(); | 90 NOTREACHED(); |
| 90 return; | 91 return; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 107 session.routing_info_.begin(); | 108 session.routing_info_.begin(); |
| 108 it != session.routing_info_.end(); | 109 it != session.routing_info_.end(); |
| 109 ++it) { | 110 ++it) { |
| 110 routing_info_[it->first] = it->second; | 111 routing_info_[it->first] = it->second; |
| 111 } | 112 } |
| 112 | 113 |
| 113 // Now update enabled groups. | 114 // Now update enabled groups. |
| 114 enabled_groups_ = ComputeEnabledGroups(routing_info_, workers_); | 115 enabled_groups_ = ComputeEnabledGroups(routing_info_, workers_); |
| 115 } | 116 } |
| 116 | 117 |
| 117 void SyncSession::RebaseRoutingInfoWithLatest( | 118 void SyncSession::RebaseRoutingInfoWithLatest(const SyncSession& session) { |
| 118 const ModelSafeRoutingInfo& routing_info, | |
| 119 const std::vector<ModelSafeWorker*>& workers) { | |
| 120 ModelSafeRoutingInfo temp_routing_info; | 119 ModelSafeRoutingInfo temp_routing_info; |
| 121 | 120 |
| 122 // Take the intersection and also set the routing info(it->second) from the | 121 // Take the intersecion and also set the routing info(it->second) from the |
| 123 // passed in session. | 122 // passed in session. |
| 124 for (ModelSafeRoutingInfo::const_iterator it = | 123 for (ModelSafeRoutingInfo::const_iterator it = |
| 125 routing_info.begin(); it != routing_info.end(); | 124 session.routing_info_.begin(); it != session.routing_info_.end(); |
| 126 ++it) { | 125 ++it) { |
| 127 if (routing_info_.find(it->first) != routing_info_.end()) { | 126 if (routing_info_.find(it->first) != routing_info_.end()) { |
| 128 temp_routing_info[it->first] = it->second; | 127 temp_routing_info[it->first] = it->second; |
| 129 } | 128 } |
| 130 } | 129 } |
| 130 |
| 131 // Now swap it. |
| 131 routing_info_.swap(temp_routing_info); | 132 routing_info_.swap(temp_routing_info); |
| 132 | 133 |
| 133 PurgeStaleStates(&source_.types, routing_info); | 134 // Now update the payload map. |
| 135 PurgeStaleStates(&source_.types, session.routing_info_); |
| 134 | 136 |
| 135 // Now update the workers. | 137 // Now update the workers. |
| 136 std::vector<ModelSafeWorker*> temp; | 138 std::vector<ModelSafeWorker*> temp; |
| 137 std::vector<ModelSafeWorker*> sorted_workers = workers; | |
| 138 std::sort(sorted_workers.begin(), sorted_workers.end()); | |
| 139 std::set_intersection(workers_.begin(), workers_.end(), | 139 std::set_intersection(workers_.begin(), workers_.end(), |
| 140 sorted_workers.begin(), sorted_workers.end(), | 140 session.workers_.begin(), session.workers_.end(), |
| 141 std::back_inserter(temp)); | 141 std::back_inserter(temp)); |
| 142 workers_.swap(temp); | 142 workers_.swap(temp); |
| 143 | 143 |
| 144 // Now update enabled groups. | 144 // Now update enabled groups. |
| 145 enabled_groups_ = ComputeEnabledGroups(routing_info_, workers_); | 145 enabled_groups_ = ComputeEnabledGroups(routing_info_, workers_); |
| 146 } | 146 } |
| 147 | 147 |
| 148 void SyncSession::PrepareForAnotherSyncCycle() { | 148 void SyncSession::PrepareForAnotherSyncCycle() { |
| 149 finished_ = false; |
| 149 source_.updates_source = | 150 source_.updates_source = |
| 150 sync_pb::GetUpdatesCallerInfo::SYNC_CYCLE_CONTINUATION; | 151 sync_pb::GetUpdatesCallerInfo::SYNC_CYCLE_CONTINUATION; |
| 151 status_controller_.reset(new StatusController(routing_info_)); | 152 status_controller_.reset(new StatusController(routing_info_)); |
| 152 } | 153 } |
| 153 | 154 |
| 154 SyncSessionSnapshot SyncSession::TakeSnapshot() const { | 155 SyncSessionSnapshot SyncSession::TakeSnapshot() const { |
| 155 syncable::Directory* dir = context_->directory(); | 156 syncable::Directory* dir = context_->directory(); |
| 156 | 157 |
| 157 bool is_share_useable = true; | 158 bool is_share_useable = true; |
| 158 ModelTypeSet initial_sync_ended; | 159 ModelTypeSet initial_sync_ended; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 175 download_progress_markers, | 176 download_progress_markers, |
| 176 HasMoreToSync(), | 177 HasMoreToSync(), |
| 177 delegate_->IsSyncingCurrentlySilenced(), | 178 delegate_->IsSyncingCurrentlySilenced(), |
| 178 status_controller_->num_encryption_conflicts(), | 179 status_controller_->num_encryption_conflicts(), |
| 179 status_controller_->num_hierarchy_conflicts(), | 180 status_controller_->num_hierarchy_conflicts(), |
| 180 status_controller_->num_simple_conflicts(), | 181 status_controller_->num_simple_conflicts(), |
| 181 status_controller_->num_server_conflicts(), | 182 status_controller_->num_server_conflicts(), |
| 182 source_, | 183 source_, |
| 183 context_->notifications_enabled(), | 184 context_->notifications_enabled(), |
| 184 dir->GetEntriesCount(), | 185 dir->GetEntriesCount(), |
| 185 status_controller_->sync_start_time()); | 186 status_controller_->sync_start_time(), |
| 187 !Succeeded()); |
| 186 } | 188 } |
| 187 | 189 |
| 188 void SyncSession::SendEventNotification(SyncEngineEvent::EventCause cause) { | 190 void SyncSession::SendEventNotification(SyncEngineEvent::EventCause cause) { |
| 189 SyncEngineEvent event(cause); | 191 SyncEngineEvent event(cause); |
| 190 event.snapshot = TakeSnapshot(); | 192 event.snapshot = TakeSnapshot(); |
| 191 | 193 |
| 192 DVLOG(1) << "Sending event with snapshot: " << event.snapshot.ToString(); | 194 DVLOG(1) << "Sending event with snapshot: " << event.snapshot.ToString(); |
| 193 context()->NotifyListeners(event); | 195 context()->NotifyListeners(event); |
| 194 } | 196 } |
| 195 | 197 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 210 enabled_groups.begin(); it != enabled_groups.end(); ++it) { | 212 enabled_groups.begin(); it != enabled_groups.end(); ++it) { |
| 211 const std::set<syncable::Id>* ids = | 213 const std::set<syncable::Id>* ids = |
| 212 status_controller_->GetUnrestrictedSimpleConflictIds(*it); | 214 status_controller_->GetUnrestrictedSimpleConflictIds(*it); |
| 213 if (ids && ids->size() > 0) { | 215 if (ids && ids->size() > 0) { |
| 214 enabled_groups_with_conflicts.insert(*it); | 216 enabled_groups_with_conflicts.insert(*it); |
| 215 } | 217 } |
| 216 } | 218 } |
| 217 return enabled_groups_with_conflicts; | 219 return enabled_groups_with_conflicts; |
| 218 } | 220 } |
| 219 | 221 |
| 220 bool SyncSession::DidReachServer() const { | 222 namespace { |
| 223 |
| 224 // Returns false iff one of the command results had an error. |
| 225 bool HadErrors(const ModelNeutralState& state) { |
| 226 const bool get_key_error = SyncerErrorIsError(state.last_get_key_result); |
| 227 const bool download_updates_error = |
| 228 SyncerErrorIsError(state.last_download_updates_result); |
| 229 const bool commit_error = SyncerErrorIsError(state.commit_result); |
| 230 return get_key_error || download_updates_error || commit_error; |
| 231 } |
| 232 } // namespace |
| 233 |
| 234 bool SyncSession::Succeeded() const { |
| 235 return finished_ && !HadErrors(status_controller_->model_neutral_state()); |
| 236 } |
| 237 |
| 238 bool SyncSession::SuccessfullyReachedServer() const { |
| 221 const ModelNeutralState& state = status_controller_->model_neutral_state(); | 239 const ModelNeutralState& state = status_controller_->model_neutral_state(); |
| 222 return state.last_get_key_result >= FIRST_SERVER_RETURN_VALUE || | 240 bool reached_server = state.last_get_key_result == SYNCER_OK || |
| 223 state.last_download_updates_result >= FIRST_SERVER_RETURN_VALUE || | 241 state.last_download_updates_result == SYNCER_OK; |
| 224 state.commit_result >= FIRST_SERVER_RETURN_VALUE; | 242 // It's possible that we reached the server on one attempt, then had an error |
| 243 // on the next (or didn't perform some of the server-communicating commands). |
| 244 // We want to verify that, for all commands attempted, we successfully spoke |
| 245 // with the server. Therefore, we verify no errors and at least one SYNCER_OK. |
| 246 return reached_server && !HadErrors(state); |
| 247 } |
| 248 |
| 249 void SyncSession::SetFinished() { |
| 250 finished_ = true; |
| 225 } | 251 } |
| 226 | 252 |
| 227 } // namespace sessions | 253 } // namespace sessions |
| 228 } // namespace syncer | 254 } // namespace syncer |
| OLD | NEW |