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) { | |
82 status_controller_.reset(new StatusController(routing_info_)); | 81 status_controller_.reset(new StatusController(routing_info_)); |
83 std::sort(workers_.begin(), workers_.end()); | 82 std::sort(workers_.begin(), workers_.end()); |
84 } | 83 } |
85 | 84 |
86 SyncSession::~SyncSession() {} | 85 SyncSession::~SyncSession() {} |
87 | 86 |
88 void SyncSession::Coalesce(const SyncSession& session) { | 87 void SyncSession::Coalesce(const SyncSession& session) { |
89 if (context_ != session.context() || delegate_ != session.delegate_) { | 88 if (context_ != session.context() || delegate_ != session.delegate_) { |
90 NOTREACHED(); | 89 NOTREACHED(); |
91 return; | 90 return; |
(...skipping 16 matching lines...) Expand all Loading... |
108 session.routing_info_.begin(); | 107 session.routing_info_.begin(); |
109 it != session.routing_info_.end(); | 108 it != session.routing_info_.end(); |
110 ++it) { | 109 ++it) { |
111 routing_info_[it->first] = it->second; | 110 routing_info_[it->first] = it->second; |
112 } | 111 } |
113 | 112 |
114 // Now update enabled groups. | 113 // Now update enabled groups. |
115 enabled_groups_ = ComputeEnabledGroups(routing_info_, workers_); | 114 enabled_groups_ = ComputeEnabledGroups(routing_info_, workers_); |
116 } | 115 } |
117 | 116 |
118 void SyncSession::RebaseRoutingInfoWithLatest(const SyncSession& session) { | 117 void SyncSession::RebaseRoutingInfoWithLatest( |
| 118 const ModelSafeRoutingInfo& routing_info, |
| 119 const std::vector<ModelSafeWorker*>& workers) { |
119 ModelSafeRoutingInfo temp_routing_info; | 120 ModelSafeRoutingInfo temp_routing_info; |
120 | 121 |
121 // Take the intersecion and also set the routing info(it->second) from the | 122 // Take the intersection and also set the routing info(it->second) from the |
122 // passed in session. | 123 // passed in session. |
123 for (ModelSafeRoutingInfo::const_iterator it = | 124 for (ModelSafeRoutingInfo::const_iterator it = |
124 session.routing_info_.begin(); it != session.routing_info_.end(); | 125 routing_info.begin(); it != routing_info.end(); |
125 ++it) { | 126 ++it) { |
126 if (routing_info_.find(it->first) != routing_info_.end()) { | 127 if (routing_info_.find(it->first) != routing_info_.end()) { |
127 temp_routing_info[it->first] = it->second; | 128 temp_routing_info[it->first] = it->second; |
128 } | 129 } |
129 } | 130 } |
130 | |
131 // Now swap it. | |
132 routing_info_.swap(temp_routing_info); | 131 routing_info_.swap(temp_routing_info); |
133 | 132 |
134 // Now update the payload map. | 133 PurgeStaleStates(&source_.types, routing_info); |
135 PurgeStaleStates(&source_.types, session.routing_info_); | |
136 | 134 |
137 // Now update the workers. | 135 // Now update the workers. |
138 std::vector<ModelSafeWorker*> temp; | 136 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 session.workers_.begin(), session.workers_.end(), | 140 sorted_workers.begin(), sorted_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; | |
150 source_.updates_source = | 149 source_.updates_source = |
151 sync_pb::GetUpdatesCallerInfo::SYNC_CYCLE_CONTINUATION; | 150 sync_pb::GetUpdatesCallerInfo::SYNC_CYCLE_CONTINUATION; |
152 status_controller_.reset(new StatusController(routing_info_)); | 151 status_controller_.reset(new StatusController(routing_info_)); |
153 } | 152 } |
154 | 153 |
155 SyncSessionSnapshot SyncSession::TakeSnapshot() const { | 154 SyncSessionSnapshot SyncSession::TakeSnapshot() const { |
156 syncable::Directory* dir = context_->directory(); | 155 syncable::Directory* dir = context_->directory(); |
157 | 156 |
158 bool is_share_useable = true; | 157 bool is_share_useable = true; |
159 ModelTypeSet initial_sync_ended; | 158 ModelTypeSet initial_sync_ended; |
(...skipping 19 matching lines...) Expand all Loading... |
179 download_progress_markers, | 178 download_progress_markers, |
180 HasMoreToSync(), | 179 HasMoreToSync(), |
181 delegate_->IsSyncingCurrentlySilenced(), | 180 delegate_->IsSyncingCurrentlySilenced(), |
182 status_controller_->TotalNumEncryptionConflictingItems(), | 181 status_controller_->TotalNumEncryptionConflictingItems(), |
183 status_controller_->TotalNumHierarchyConflictingItems(), | 182 status_controller_->TotalNumHierarchyConflictingItems(), |
184 status_controller_->TotalNumSimpleConflictingItems(), | 183 status_controller_->TotalNumSimpleConflictingItems(), |
185 status_controller_->TotalNumServerConflictingItems(), | 184 status_controller_->TotalNumServerConflictingItems(), |
186 source_, | 185 source_, |
187 context_->notifications_enabled(), | 186 context_->notifications_enabled(), |
188 dir->GetEntriesCount(), | 187 dir->GetEntriesCount(), |
189 status_controller_->sync_start_time(), | 188 status_controller_->sync_start_time()); |
190 !Succeeded()); | |
191 } | 189 } |
192 | 190 |
193 void SyncSession::SendEventNotification(SyncEngineEvent::EventCause cause) { | 191 void SyncSession::SendEventNotification(SyncEngineEvent::EventCause cause) { |
194 SyncEngineEvent event(cause); | 192 SyncEngineEvent event(cause); |
195 event.snapshot = TakeSnapshot(); | 193 event.snapshot = TakeSnapshot(); |
196 | 194 |
197 DVLOG(1) << "Sending event with snapshot: " << event.snapshot.ToString(); | 195 DVLOG(1) << "Sending event with snapshot: " << event.snapshot.ToString(); |
198 context()->NotifyListeners(event); | 196 context()->NotifyListeners(event); |
199 } | 197 } |
200 | 198 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 if (update_progress && | 232 if (update_progress && |
235 (update_progress->VerifiedUpdatesBegin() != | 233 (update_progress->VerifiedUpdatesBegin() != |
236 update_progress->VerifiedUpdatesEnd())) { | 234 update_progress->VerifiedUpdatesEnd())) { |
237 enabled_groups_with_verified_updates.insert(*it); | 235 enabled_groups_with_verified_updates.insert(*it); |
238 } | 236 } |
239 } | 237 } |
240 | 238 |
241 return enabled_groups_with_verified_updates; | 239 return enabled_groups_with_verified_updates; |
242 } | 240 } |
243 | 241 |
244 namespace { | 242 bool SyncSession::DidReachServer() const { |
245 | |
246 // Returns false iff one of the command results had an error. | |
247 bool HadErrors(const ModelNeutralState& state) { | |
248 const bool get_key_error = SyncerErrorIsError(state.last_get_key_result); | |
249 const bool download_updates_error = | |
250 SyncerErrorIsError(state.last_download_updates_result); | |
251 const bool commit_error = SyncerErrorIsError(state.commit_result); | |
252 return get_key_error || download_updates_error || commit_error; | |
253 } | |
254 } // namespace | |
255 | |
256 bool SyncSession::Succeeded() const { | |
257 return finished_ && !HadErrors(status_controller_->model_neutral_state()); | |
258 } | |
259 | |
260 bool SyncSession::SuccessfullyReachedServer() const { | |
261 const ModelNeutralState& state = status_controller_->model_neutral_state(); | 243 const ModelNeutralState& state = status_controller_->model_neutral_state(); |
262 bool reached_server = state.last_get_key_result == SYNCER_OK || | 244 return state.last_get_key_result >= FIRST_SERVER_RETURN_VALUE || |
263 state.last_download_updates_result == SYNCER_OK; | 245 state.last_download_updates_result >= FIRST_SERVER_RETURN_VALUE || |
264 // It's possible that we reached the server on one attempt, then had an error | 246 state.commit_result >= FIRST_SERVER_RETURN_VALUE; |
265 // on the next (or didn't perform some of the server-communicating commands). | |
266 // We want to verify that, for all commands attempted, we successfully spoke | |
267 // with the server. Therefore, we verify no errors and at least one SYNCER_OK. | |
268 return reached_server && !HadErrors(state); | |
269 } | |
270 | |
271 void SyncSession::SetFinished() { | |
272 finished_ = true; | |
273 } | 247 } |
274 | 248 |
275 } // namespace sessions | 249 } // namespace sessions |
276 } // namespace syncer | 250 } // namespace syncer |
OLD | NEW |