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 16 matching lines...) Expand all Loading... |
176 download_progress_markers, | 175 download_progress_markers, |
177 HasMoreToSync(), | 176 HasMoreToSync(), |
178 delegate_->IsSyncingCurrentlySilenced(), | 177 delegate_->IsSyncingCurrentlySilenced(), |
179 status_controller_->num_encryption_conflicts(), | 178 status_controller_->num_encryption_conflicts(), |
180 status_controller_->num_hierarchy_conflicts(), | 179 status_controller_->num_hierarchy_conflicts(), |
181 status_controller_->num_simple_conflicts(), | 180 status_controller_->num_simple_conflicts(), |
182 status_controller_->num_server_conflicts(), | 181 status_controller_->num_server_conflicts(), |
183 source_, | 182 source_, |
184 context_->notifications_enabled(), | 183 context_->notifications_enabled(), |
185 dir->GetEntriesCount(), | 184 dir->GetEntriesCount(), |
186 status_controller_->sync_start_time(), | 185 status_controller_->sync_start_time()); |
187 !Succeeded()); | |
188 } | 186 } |
189 | 187 |
190 void SyncSession::SendEventNotification(SyncEngineEvent::EventCause cause) { | 188 void SyncSession::SendEventNotification(SyncEngineEvent::EventCause cause) { |
191 SyncEngineEvent event(cause); | 189 SyncEngineEvent event(cause); |
192 event.snapshot = TakeSnapshot(); | 190 event.snapshot = TakeSnapshot(); |
193 | 191 |
194 DVLOG(1) << "Sending event with snapshot: " << event.snapshot.ToString(); | 192 DVLOG(1) << "Sending event with snapshot: " << event.snapshot.ToString(); |
195 context()->NotifyListeners(event); | 193 context()->NotifyListeners(event); |
196 } | 194 } |
197 | 195 |
(...skipping 14 matching lines...) Expand all Loading... |
212 enabled_groups.begin(); it != enabled_groups.end(); ++it) { | 210 enabled_groups.begin(); it != enabled_groups.end(); ++it) { |
213 const std::set<syncable::Id>* ids = | 211 const std::set<syncable::Id>* ids = |
214 status_controller_->GetUnrestrictedSimpleConflictIds(*it); | 212 status_controller_->GetUnrestrictedSimpleConflictIds(*it); |
215 if (ids && ids->size() > 0) { | 213 if (ids && ids->size() > 0) { |
216 enabled_groups_with_conflicts.insert(*it); | 214 enabled_groups_with_conflicts.insert(*it); |
217 } | 215 } |
218 } | 216 } |
219 return enabled_groups_with_conflicts; | 217 return enabled_groups_with_conflicts; |
220 } | 218 } |
221 | 219 |
222 namespace { | 220 bool SyncSession::DidReachServer() const { |
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 { | |
239 const ModelNeutralState& state = status_controller_->model_neutral_state(); | 221 const ModelNeutralState& state = status_controller_->model_neutral_state(); |
240 bool reached_server = state.last_get_key_result == SYNCER_OK || | 222 return state.last_get_key_result >= FIRST_SERVER_RETURN_VALUE || |
241 state.last_download_updates_result == SYNCER_OK; | 223 state.last_download_updates_result >= FIRST_SERVER_RETURN_VALUE || |
242 // It's possible that we reached the server on one attempt, then had an error | 224 state.commit_result >= FIRST_SERVER_RETURN_VALUE; |
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; | |
251 } | 225 } |
252 | 226 |
253 } // namespace sessions | 227 } // namespace sessions |
254 } // namespace syncer | 228 } // namespace syncer |
OLD | NEW |