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 <algorithm> | |
6 #include <functional> | |
7 | |
8 #include "base/debug/trace_event.h" | |
9 | |
10 #include "base/logging.h" | |
11 #include "base/message_loop.h" | |
12 #include "base/metrics/histogram.h" | |
13 | |
5 #include "chrome/browser/sync/glue/model_association_manager.h" | 14 #include "chrome/browser/sync/glue/model_association_manager.h" |
6 | 15 |
7 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
8 #include <algorithm> | 17 #include <algorithm> |
9 #include <functional> | 18 #include <functional> |
10 | 19 |
11 #include "base/debug/trace_event.h" | 20 #include "base/debug/trace_event.h" |
12 | 21 |
13 #include "base/logging.h" | 22 #include "base/logging.h" |
14 #include "base/message_loop.h" | 23 #include "base/message_loop.h" |
15 #include "base/metrics/histogram.h" | 24 #include "base/metrics/histogram.h" |
16 | 25 |
17 | 26 |
18 using content::BrowserThread; | 27 using content::BrowserThread; |
19 using syncable::ModelTypeSet; | 28 using syncable::ModelTypeSet; |
20 | 29 |
21 namespace browser_sync { | 30 namespace browser_sync { |
31 // The amount of time we wait for a datatype to load. If the type has | |
32 // not finished loading we move on to the next type. Once this type | |
33 // finishes loading we will do a configure to associate this type. Note | |
34 // that in most cases types finish loading before this timeout. | |
35 const int64 kDataTypeLoadWaitTimeInSeconds = 120; | |
22 namespace { | 36 namespace { |
23 | 37 |
24 static const syncable::ModelType kStartOrder[] = { | 38 static const syncable::ModelType kStartOrder[] = { |
25 syncable::NIGORI, // Listed for completeness. | 39 syncable::NIGORI, // Listed for completeness. |
26 syncable::BOOKMARKS, | 40 syncable::BOOKMARKS, |
27 syncable::PREFERENCES, | 41 syncable::PREFERENCES, |
28 syncable::AUTOFILL, | 42 syncable::AUTOFILL, |
29 syncable::AUTOFILL_PROFILE, | 43 syncable::AUTOFILL_PROFILE, |
30 syncable::EXTENSION_SETTINGS, | 44 syncable::EXTENSION_SETTINGS, |
31 syncable::EXTENSIONS, | 45 syncable::EXTENSIONS, |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
87 | 101 |
88 void ModelAssociationManager::Initialize( | 102 void ModelAssociationManager::Initialize( |
89 syncable::ModelTypeSet desired_types) { | 103 syncable::ModelTypeSet desired_types) { |
90 DCHECK_EQ(state_, IDLE); | 104 DCHECK_EQ(state_, IDLE); |
91 needs_start_.clear(); | 105 needs_start_.clear(); |
92 needs_stop_.clear(); | 106 needs_stop_.clear(); |
93 failed_datatypes_info_.clear(); | 107 failed_datatypes_info_.clear(); |
94 desired_types_ = desired_types; | 108 desired_types_ = desired_types; |
95 state_ = INITIAILIZED_TO_CONFIGURE; | 109 state_ = INITIAILIZED_TO_CONFIGURE; |
96 | 110 |
111 VLOG(0) << "ModelAssociationManager: Initializing"; | |
112 | |
113 // Stop the types that are still loading from the previous configuration. | |
114 // If they are enabled we will start them here once again. | |
115 for (std::vector<DataTypeController*>::const_iterator it = | |
116 pending_model_load_.begin(); | |
117 it != pending_model_load_.end(); | |
118 ++it) { | |
119 VLOG(0) << "ModelAssociationManager: Stopping " | |
120 << (*it)->name() | |
121 << " before initialization"; | |
122 (*it)->Stop(); | |
123 } | |
124 | |
125 pending_model_load_.clear(); | |
126 waiting_to_associate_.clear(); | |
127 currently_associating_ = NULL; | |
128 | |
tim (not reviewing)
2012/05/23 03:18:52
extra newline
lipalani1
2012/05/23 17:47:20
Done.
| |
129 | |
97 // We need to calculate our |needs_start_| and |needs_stop_| list. | 130 // We need to calculate our |needs_start_| and |needs_stop_| list. |
98 GetControllersNeedingStart(&needs_start_); | 131 GetControllersNeedingStart(&needs_start_); |
99 // Sort these according to kStartOrder. | 132 // Sort these according to kStartOrder. |
100 std::sort(needs_start_.begin(), | 133 std::sort(needs_start_.begin(), |
101 needs_start_.end(), | 134 needs_start_.end(), |
102 SortComparator(&start_order_)); | 135 SortComparator(&start_order_)); |
103 | 136 |
104 // Add any data type controllers into that needs_stop_ list that are | 137 // Add any data type controllers into that needs_stop_ list that are |
105 // currently MODEL_STARTING, ASSOCIATING, RUNNING or DISABLED. | 138 // currently MODEL_STARTING, ASSOCIATING, RUNNING or DISABLED. |
106 for (DataTypeController::TypeMap::const_iterator it = controllers_->begin(); | 139 for (DataTypeController::TypeMap::const_iterator it = controllers_->begin(); |
107 it != controllers_->end(); ++it) { | 140 it != controllers_->end(); ++it) { |
108 DataTypeController* dtc = (*it).second; | 141 DataTypeController* dtc = (*it).second; |
109 if (!desired_types.Has(dtc->type()) && ( | 142 if (!desired_types.Has(dtc->type()) && ( |
110 dtc->state() == DataTypeController::MODEL_STARTING || | 143 dtc->state() == DataTypeController::MODEL_STARTING || |
111 dtc->state() == DataTypeController::ASSOCIATING || | 144 dtc->state() == DataTypeController::ASSOCIATING || |
112 dtc->state() == DataTypeController::RUNNING || | 145 dtc->state() == DataTypeController::RUNNING || |
113 dtc->state() == DataTypeController::DISABLED)) { | 146 dtc->state() == DataTypeController::DISABLED)) { |
114 needs_stop_.push_back(dtc); | 147 needs_stop_.push_back(dtc); |
115 DVLOG(1) << "Will stop " << dtc->name(); | 148 VLOG(0) << "ModelTypeToString: Will stop " << dtc->name(); |
116 } | 149 } |
117 } | 150 } |
118 // Sort these according to kStartOrder. | 151 // Sort these according to kStartOrder. |
119 std::sort(needs_stop_.begin(), | 152 std::sort(needs_stop_.begin(), |
120 needs_stop_.end(), | 153 needs_stop_.end(), |
121 SortComparator(&start_order_)); | 154 SortComparator(&start_order_)); |
122 } | 155 } |
123 | 156 |
124 void ModelAssociationManager::StartAssociationAsync() { | 157 void ModelAssociationManager::StartAssociationAsync() { |
125 DCHECK_EQ(state_, INITIAILIZED_TO_CONFIGURE); | 158 DCHECK_EQ(state_, INITIAILIZED_TO_CONFIGURE); |
126 state_ = CONFIGURING; | 159 state_ = CONFIGURING; |
160 VLOG(0) << "ModelAssociationManager: Going to start model association"; | |
127 LoadModelForNextType(); | 161 LoadModelForNextType(); |
128 } | 162 } |
129 | 163 |
130 void ModelAssociationManager::ResetForReconfiguration() { | 164 void ModelAssociationManager::ResetForReconfiguration() { |
131 DCHECK_EQ(state_, INITIAILIZED_TO_CONFIGURE); | 165 DCHECK_EQ(state_, INITIAILIZED_TO_CONFIGURE); |
132 state_ = IDLE; | 166 state_ = IDLE; |
167 VLOG(0) << "ModelAssociationManager: Reseting for reconfiguration"; | |
133 needs_start_.clear(); | 168 needs_start_.clear(); |
134 needs_stop_.clear(); | 169 needs_stop_.clear(); |
135 failed_datatypes_info_.clear(); | 170 failed_datatypes_info_.clear(); |
136 } | 171 } |
137 | 172 |
138 void ModelAssociationManager::StopDisabledTypes() { | 173 void ModelAssociationManager::StopDisabledTypes() { |
139 DCHECK_EQ(state_, INITIAILIZED_TO_CONFIGURE); | 174 DCHECK_EQ(state_, INITIAILIZED_TO_CONFIGURE); |
175 VLOG(0) << "ModelAssociationManager: Stopping disabled types."; | |
tim (not reviewing)
2012/05/23 03:18:52
DVLOG(1) (here and elsewhere)?
lipalani1
2012/05/23 17:47:20
Done.
| |
140 // Stop requested data types. | 176 // Stop requested data types. |
141 for (size_t i = 0; i < needs_stop_.size(); ++i) { | 177 for (size_t i = 0; i < needs_stop_.size(); ++i) { |
142 DVLOG(1) << "Stopping " << needs_stop_[i]->name(); | 178 VLOG(0) << "ModelAssociationManager: Stopping " << needs_stop_[i]->name(); |
143 needs_stop_[i]->Stop(); | 179 needs_stop_[i]->Stop(); |
144 } | 180 } |
145 needs_stop_.clear(); | 181 needs_stop_.clear(); |
146 } | 182 } |
147 | 183 |
148 void ModelAssociationManager::Stop() { | 184 void ModelAssociationManager::Stop() { |
149 bool need_to_call_model_association_done = false; | 185 bool need_to_call_model_association_done = false; |
186 VLOG(0) << "ModelAssociationManager: Stopping MAM"; | |
150 if (state_ == CONFIGURING) { | 187 if (state_ == CONFIGURING) { |
188 VLOG(0) << "ModelAssociationManager: In the middle of configuratio while" | |
189 << " stopping"; | |
151 state_ = ABORTED; | 190 state_ = ABORTED; |
152 DCHECK(currently_associating_ != NULL || | 191 DCHECK(currently_associating_ != NULL || |
153 needs_start_.size() > 0 || | 192 needs_start_.size() > 0 || |
154 pending_model_load_.size() > 0 || | 193 pending_model_load_.size() > 0 || |
155 waiting_to_associate_.size() > 0); | 194 waiting_to_associate_.size() > 0); |
156 | 195 |
157 if (currently_associating_) { | 196 if (currently_associating_) { |
197 VLOG(0) << "ModelAssociationManager: stopping " | |
198 << currently_associating_->name(); | |
158 currently_associating_->Stop(); | 199 currently_associating_->Stop(); |
159 } else { | 200 } else { |
160 // DTCs in other lists would be stopped below. | 201 // DTCs in other lists would be stopped below. |
161 state_ = IDLE; | 202 state_ = IDLE; |
162 } | 203 } |
163 | 204 |
164 DCHECK_EQ(IDLE, state_); | 205 DCHECK_EQ(IDLE, state_); |
165 | 206 |
166 // We are in the midle of model association. We need to inform the caller | 207 // We are in the midle of model association. We need to inform the caller |
167 // so the caller can send notificationst to PSS layer. | 208 // so the caller can send notificationst to PSS layer. |
168 need_to_call_model_association_done = true; | 209 need_to_call_model_association_done = true; |
169 } | 210 } |
170 | 211 |
171 // Now continue stopping any types that have already started. | 212 // Now continue stopping any types that have already started. |
172 DCHECK(state_ == IDLE || | 213 DCHECK(state_ == IDLE || |
173 state_ == INITIAILIZED_TO_CONFIGURE); | 214 state_ == INITIAILIZED_TO_CONFIGURE); |
174 for (DataTypeController::TypeMap::const_iterator it = controllers_->begin(); | 215 for (DataTypeController::TypeMap::const_iterator it = controllers_->begin(); |
175 it != controllers_->end(); ++it) { | 216 it != controllers_->end(); ++it) { |
176 DataTypeController* dtc = (*it).second; | 217 DataTypeController* dtc = (*it).second; |
177 if (dtc->state() != DataTypeController::NOT_RUNNING && | 218 if (dtc->state() != DataTypeController::NOT_RUNNING && |
178 dtc->state() != DataTypeController::STOPPING) { | 219 dtc->state() != DataTypeController::STOPPING) { |
179 dtc->Stop(); | 220 dtc->Stop(); |
180 DVLOG(1) << "Stopped " << dtc->name(); | 221 VLOG(0) << "ModelAssociationManager: Stopped " << dtc->name(); |
181 } | 222 } |
182 } | 223 } |
183 | 224 |
184 if (need_to_call_model_association_done) { | 225 if (need_to_call_model_association_done) { |
226 VLOG(0) << "ModelAssociationManager: Calling OnModelAssociationDone"; | |
185 DataTypeManager::ConfigureResult result(DataTypeManager::ABORTED, | 227 DataTypeManager::ConfigureResult result(DataTypeManager::ABORTED, |
186 desired_types_, | 228 desired_types_, |
187 failed_datatypes_info_); | 229 failed_datatypes_info_, |
230 syncable::ModelTypeSet()); | |
188 result_processor_->OnModelAssociationDone(result); | 231 result_processor_->OnModelAssociationDone(result); |
189 } | 232 } |
190 | 233 |
191 failed_datatypes_info_.clear(); | 234 failed_datatypes_info_.clear(); |
192 } | 235 } |
193 | 236 |
194 bool ModelAssociationManager::GetControllersNeedingStart( | 237 bool ModelAssociationManager::GetControllersNeedingStart( |
195 std::vector<DataTypeController*>* needs_start) { | 238 std::vector<DataTypeController*>* needs_start) { |
239 VLOG(0) << "ModelAssociationManager: GetControllersNeedingStart"; | |
196 // Add any data type controllers into the needs_start_ list that are | 240 // Add any data type controllers into the needs_start_ list that are |
197 // currently NOT_RUNNING or STOPPING. | 241 // currently NOT_RUNNING or STOPPING. |
198 bool found_any = false; | 242 bool found_any = false; |
199 for (ModelTypeSet::Iterator it = desired_types_.First(); | 243 for (ModelTypeSet::Iterator it = desired_types_.First(); |
200 it.Good(); it.Inc()) { | 244 it.Good(); it.Inc()) { |
201 DataTypeController::TypeMap::const_iterator dtc = | 245 DataTypeController::TypeMap::const_iterator dtc = |
202 controllers_->find(it.Get()); | 246 controllers_->find(it.Get()); |
203 if (dtc != controllers_->end() && | 247 if (dtc != controllers_->end() && |
204 (dtc->second->state() == DataTypeController::NOT_RUNNING || | 248 (dtc->second->state() == DataTypeController::NOT_RUNNING || |
205 dtc->second->state() == DataTypeController::STOPPING)) { | 249 dtc->second->state() == DataTypeController::STOPPING)) { |
206 found_any = true; | 250 found_any = true; |
207 if (needs_start) | 251 if (needs_start) |
208 needs_start->push_back(dtc->second.get()); | 252 needs_start->push_back(dtc->second.get()); |
209 if (dtc->second->state() == DataTypeController::DISABLED) { | 253 if (dtc->second->state() == DataTypeController::DISABLED) { |
210 DVLOG(1) << "Found " << syncable::ModelTypeToString(dtc->second->type()) | 254 VLOG(0) << "ModelAssociationManager: Found "\ |
255 << syncable::ModelTypeToString(dtc->second->type()) | |
211 << " in disabled state."; | 256 << " in disabled state."; |
212 } | 257 } |
213 } | 258 } |
214 } | 259 } |
215 return found_any; | 260 return found_any; |
216 } | 261 } |
217 | 262 |
263 void ModelAssociationManager::HandleFailedTypes( | |
tim (not reviewing)
2012/05/23 03:18:52
Actually, since this doesn't totally "handle" fail
lipalani1
2012/05/23 17:47:20
Done.
| |
264 DataTypeController::StartResult result, | |
265 const SyncError& error) { | |
266 failed_datatypes_info_.push_back(error); | |
267 LOG(ERROR) << "Failed to associate models for " | |
268 << syncable::ModelTypeToString(error.type()); | |
269 UMA_HISTOGRAM_ENUMERATION("Sync.ConfigureFailed", | |
270 error.type(), | |
271 syncable::MODEL_TYPE_COUNT); | |
272 } | |
273 | |
218 void ModelAssociationManager::TypeStartCallback( | 274 void ModelAssociationManager::TypeStartCallback( |
219 DataTypeController::StartResult result, | 275 DataTypeController::StartResult result, |
220 const SyncError& error) { | 276 const SyncError& error) { |
221 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 277 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
222 | 278 |
279 VLOG(0) << "ModelAssociationManager: TypeStartCallback"; | |
223 if (state_ == ABORTED) { | 280 if (state_ == ABORTED) { |
224 // Now that we have finished with the current type we can stop | 281 // Now that we have finished with the current type we can stop |
225 // if abort was called. | 282 // if abort was called. |
283 VLOG(0) << "ModelAssociationManager: Doing an early return" | |
284 << " because of abort"; | |
226 state_ = IDLE; | 285 state_ = IDLE; |
227 return; | 286 return; |
228 } | 287 } |
229 | 288 |
230 DCHECK(state_ == CONFIGURING); | 289 DCHECK(state_ == CONFIGURING); |
231 | 290 |
232 // We are done with this type. Clear it. | 291 // We are done with this type. Clear it. |
233 DataTypeController* started_dtc = currently_associating_; | 292 DataTypeController* started_dtc = currently_associating_; |
234 currently_associating_ = NULL; | 293 currently_associating_ = NULL; |
235 | 294 |
236 if (result == DataTypeController::ASSOCIATION_FAILED) { | 295 if (result == DataTypeController::ASSOCIATION_FAILED) { |
237 failed_datatypes_info_.push_back(error); | 296 VLOG(0) << "ModelAssociationManager: Encountered a failed type"; |
238 LOG(ERROR) << "Failed to associate models for " | 297 HandleFailedTypes(result, error); |
239 << syncable::ModelTypeToString(error.type()); | |
240 UMA_HISTOGRAM_ENUMERATION("Sync.ConfigureFailed", | |
241 error.type(), | |
242 syncable::MODEL_TYPE_COUNT); | |
243 } | 298 } |
244 | 299 |
245 // If the type started normally, continue to the next type. | 300 // If the type started normally, continue to the next type. |
246 // If the type is waiting for the cryptographer, continue to the next type. | 301 // If the type is waiting for the cryptographer, continue to the next type. |
247 // Once the cryptographer is ready, we'll attempt to restart this type. | 302 // Once the cryptographer is ready, we'll attempt to restart this type. |
248 // If this type encountered a type specific error continue to the next type. | 303 // If this type encountered a type specific error continue to the next type. |
249 if (result == DataTypeController::NEEDS_CRYPTO || | 304 if (result == DataTypeController::NEEDS_CRYPTO || |
250 result == DataTypeController::OK || | 305 result == DataTypeController::OK || |
251 result == DataTypeController::OK_FIRST_RUN || | 306 result == DataTypeController::OK_FIRST_RUN || |
252 result == DataTypeController::ASSOCIATION_FAILED) { | 307 result == DataTypeController::ASSOCIATION_FAILED) { |
308 VLOG(0) << "ModelAssociationManager: type start callback returned " | |
309 << result << " so calling LoadModelForNextType"; | |
253 LoadModelForNextType(); | 310 LoadModelForNextType(); |
254 return; | 311 return; |
255 } | 312 } |
256 | 313 |
257 // Any other result requires reconfiguration. Pass it on through the callback. | 314 // Any other result requires reconfiguration. Pass it on through the callback. |
258 LOG(ERROR) << "Failed to configure " << started_dtc->name(); | 315 LOG(ERROR) << "Failed to configure " << started_dtc->name(); |
259 DCHECK(error.IsSet()); | 316 DCHECK(error.IsSet()); |
260 DCHECK_EQ(started_dtc->type(), error.type()); | 317 DCHECK_EQ(started_dtc->type(), error.type()); |
261 DataTypeManager::ConfigureStatus configure_status = | 318 DataTypeManager::ConfigureStatus configure_status = |
262 DataTypeManager::ABORTED; | 319 DataTypeManager::ABORTED; |
(...skipping 10 matching lines...) Expand all Loading... | |
273 } | 330 } |
274 | 331 |
275 std::list<SyncError> errors; | 332 std::list<SyncError> errors; |
276 errors.push_back(error); | 333 errors.push_back(error); |
277 | 334 |
278 // Put our state to idle. | 335 // Put our state to idle. |
279 state_ = IDLE; | 336 state_ = IDLE; |
280 | 337 |
281 DataTypeManager::ConfigureResult configure_result(configure_status, | 338 DataTypeManager::ConfigureResult configure_result(configure_status, |
282 desired_types_, | 339 desired_types_, |
283 errors); | 340 errors, |
341 syncable::ModelTypeSet()); | |
284 result_processor_->OnModelAssociationDone(configure_result); | 342 result_processor_->OnModelAssociationDone(configure_result); |
285 } | 343 } |
286 | 344 |
287 void ModelAssociationManager::LoadModelForNextType() { | 345 void ModelAssociationManager::LoadModelForNextType() { |
346 VLOG(0) << "ModelAssociationManager: LoadModelForNextType"; | |
288 if (!needs_start_.empty()) { | 347 if (!needs_start_.empty()) { |
289 DVLOG(1) << "Starting " << needs_start_[0]->name(); | 348 VLOG(0) << "ModelAssociationManager: Starting " << needs_start_[0]->name(); |
290 | 349 |
291 DataTypeController* dtc = needs_start_[0]; | 350 DataTypeController* dtc = needs_start_[0]; |
292 needs_start_.erase(needs_start_.begin()); | 351 needs_start_.erase(needs_start_.begin()); |
293 // Move from |needs_start_| to |pending_model_load_|. | 352 // Move from |needs_start_| to |pending_model_load_|. |
294 pending_model_load_.push_back(dtc); | 353 pending_model_load_.insert(pending_model_load_.begin(), dtc); |
354 timer_.Start(FROM_HERE, | |
355 base::TimeDelta::FromSeconds(kDataTypeLoadWaitTimeInSeconds), | |
356 this, | |
357 &ModelAssociationManager::LoadModelForNextType); | |
295 dtc->LoadModels(base::Bind( | 358 dtc->LoadModels(base::Bind( |
296 &ModelAssociationManager::ModelLoadCallback, | 359 &ModelAssociationManager::ModelLoadCallback, |
297 weak_ptr_factory_.GetWeakPtr())); | 360 weak_ptr_factory_.GetWeakPtr())); |
361 | |
298 return; | 362 return; |
299 } | 363 } |
300 | 364 |
365 VLOG(0) << "ModelAssociationManager: All types have models loaded." | |
366 << "Moving on to StartAssociatingNextType."; | |
367 | |
301 // If all controllers have their |LoadModels| invoked then pass onto | 368 // If all controllers have their |LoadModels| invoked then pass onto |
302 // |StartAssociatingNextType|. | 369 // |StartAssociatingNextType|. |
303 StartAssociatingNextType(); | 370 StartAssociatingNextType(); |
304 } | 371 } |
305 | 372 |
306 void ModelAssociationManager::ModelLoadCallback( | 373 void ModelAssociationManager::ModelLoadCallback( |
307 syncable::ModelType type, SyncError error) { | 374 syncable::ModelType type, SyncError error) { |
308 DCHECK_EQ(state_, CONFIGURING); | 375 VLOG(0) << "ModelAssociationManager: ModelLoadCallback for " |
309 | 376 << syncable::ModelTypeToString(type); |
310 for (std::vector<DataTypeController*>::iterator it = | 377 if (state_ == CONFIGURING) { |
311 pending_model_load_.begin(); | 378 VLOG(0) << "ModelAssociationManager: ModelLoadCallback while configuring"; |
312 it != pending_model_load_.end(); | 379 for (std::vector<DataTypeController*>::iterator it = |
313 ++it) { | 380 pending_model_load_.begin(); |
314 if ((*it)->type() == type) { | 381 it != pending_model_load_.end(); |
315 DataTypeController* dtc = *it; | 382 ++it) { |
316 pending_model_load_.erase(it); | 383 if ((*it)->type() == type) { |
317 if (!error.IsSet()) { | 384 // Each type is given |kDataTypeLoadWaitTimeInSeconds| time to load |
318 waiting_to_associate_.push_back(dtc); | 385 // (as controlled by the timer.). If the type does not load in that |
319 StartAssociatingNextType(); | 386 // time we move on to the next type. However if the type does |
320 } else { | 387 // finish loading in that time we want to stop the timer. We stop |
321 // Treat it like a regular error. | 388 // the timer, if the type that loaded is the same as the type that |
322 DCHECK(currently_associating_ == NULL); | 389 // we started the timer for(as indicated by the type on the head |
323 currently_associating_ = dtc; | 390 // of the list). |
324 TypeStartCallback(DataTypeController::ASSOCIATION_FAILED, error); | 391 // Note: Regardless of this timer value the associations will always |
392 // take place serially. The only thing this timer controls is how serial | |
393 // the model load is. If this timer has a value of zero seconds then | |
394 // the model loads will all be parallel. | |
395 if (it == pending_model_load_.begin()) { | |
396 VLOG(0) << "ModelAssociationManager: Stopping timer"; | |
397 timer_.Stop(); | |
398 } | |
399 DataTypeController* dtc = *it; | |
400 pending_model_load_.erase(it); | |
401 if (!error.IsSet()) { | |
402 VLOG(0) << "ModelAssociationManager:" | |
403 << " Calling StartAssociatingNextType"; | |
404 waiting_to_associate_.push_back(dtc); | |
405 StartAssociatingNextType(); | |
406 } else { | |
407 VLOG(0) << "ModelAssociationManager: Encountered error loading"; | |
408 // Treat it like a regular error. | |
409 HandleFailedTypes(DataTypeController::ASSOCIATION_FAILED, error); | |
410 } | |
411 return; | |
325 } | 412 } |
326 return; | |
327 } | 413 } |
414 NOTREACHED(); | |
415 return; | |
416 } else { | |
417 VLOG(0) << "ModelAssociationManager: Models loaded after configure cycle" | |
418 << "Informing DTM"; | |
419 // This datatype finished loading after the deadline imposed by the | |
420 // originating configuration cycle. Inform the DataTypeManager that the | |
421 // type has loaded, so that association may begin. | |
422 result_processor_->OnTypesLoaded(); | |
328 } | 423 } |
329 | 424 |
330 NOTREACHED(); | |
331 } | 425 } |
332 | 426 |
333 | |
334 void ModelAssociationManager::StartAssociatingNextType() { | 427 void ModelAssociationManager::StartAssociatingNextType() { |
335 DCHECK_EQ(state_, CONFIGURING); | 428 DCHECK_EQ(state_, CONFIGURING); |
336 DCHECK_EQ(currently_associating_, static_cast<DataTypeController*>(NULL)); | 429 DCHECK_EQ(currently_associating_, static_cast<DataTypeController*>(NULL)); |
430 | |
431 VLOG(0) << "ModelAssociationManager: StartAssociatingNextType"; | |
337 if (!waiting_to_associate_.empty()) { | 432 if (!waiting_to_associate_.empty()) { |
338 DVLOG(1) << "Starting " << waiting_to_associate_[0]->name(); | 433 VLOG(0) << "ModelAssociationManager: Starting " |
434 << waiting_to_associate_[0]->name(); | |
339 TRACE_EVENT_BEGIN1("sync", "ModelAssociation", | 435 TRACE_EVENT_BEGIN1("sync", "ModelAssociation", |
340 "DataType", | 436 "DataType", |
341 ModelTypeToString(waiting_to_associate_[0]->type())); | 437 ModelTypeToString(waiting_to_associate_[0]->type())); |
342 DataTypeController* dtc = waiting_to_associate_[0]; | 438 DataTypeController* dtc = waiting_to_associate_[0]; |
343 waiting_to_associate_.erase(waiting_to_associate_.begin()); | 439 waiting_to_associate_.erase(waiting_to_associate_.begin()); |
344 currently_associating_ = dtc; | 440 currently_associating_ = dtc; |
345 dtc->StartAssociating(base::Bind( | 441 dtc->StartAssociating(base::Bind( |
346 &ModelAssociationManager::TypeStartCallback, | 442 &ModelAssociationManager::TypeStartCallback, |
347 weak_ptr_factory_.GetWeakPtr())); | 443 weak_ptr_factory_.GetWeakPtr())); |
348 return; | 444 return; |
349 } | 445 } |
350 | 446 |
351 // We are done with this cycle of association. | 447 // We are done with this cycle of association. |
352 state_ = IDLE; | 448 state_ = IDLE; |
353 // Do a fresh calculation to see if controllers need starting to account for | 449 // Do a fresh calculation to see if controllers need starting to account for |
354 // things like encryption, which may still need to be sorted out before we | 450 // things like encryption, which may still need to be sorted out before we |
355 // can announce we're "Done" configuration entirely. | 451 // can announce we're "Done" configuration entirely. |
356 if (GetControllersNeedingStart(NULL)) { | 452 if (GetControllersNeedingStart(NULL)) { |
357 DVLOG(1) << "GetControllersNeedingStart returned true." | 453 VLOG(0) << "ModelAssociationManager: GetControllersNeedingStart" |
358 << " Blocking DataTypeManager"; | 454 << " returned true. Blocking DataTypeManager"; |
359 | 455 |
360 DataTypeManager::ConfigureResult configure_result( | 456 DataTypeManager::ConfigureResult configure_result( |
361 DataTypeManager::CONFIGURE_BLOCKED, | 457 DataTypeManager::CONFIGURE_BLOCKED, |
362 desired_types_, | 458 desired_types_, |
363 failed_datatypes_info_); | 459 failed_datatypes_info_, |
460 syncable::ModelTypeSet()); | |
364 state_ = IDLE; | 461 state_ = IDLE; |
365 result_processor_->OnModelAssociationDone(configure_result); | 462 result_processor_->OnModelAssociationDone(configure_result); |
366 return; | 463 return; |
367 } | 464 } |
368 | 465 |
369 DataTypeManager::ConfigureStatus configure_status = DataTypeManager::OK; | 466 DataTypeManager::ConfigureStatus configure_status = DataTypeManager::OK; |
370 if (!failed_datatypes_info_.empty()) | 467 |
468 if (!failed_datatypes_info_.empty() || | |
469 !GetTypesWaitingToLoad().Empty()) { | |
470 // We have not configured all types that we have been asked to configure. | |
471 // Either we have failed types or types that have not completed loading | |
472 // yet. | |
473 VLOG(0) << "ModelAssociationManager: setting partial success"; | |
371 configure_status = DataTypeManager::PARTIAL_SUCCESS; | 474 configure_status = DataTypeManager::PARTIAL_SUCCESS; |
475 } | |
372 | 476 |
373 DataTypeManager::ConfigureResult result(configure_status, | 477 DataTypeManager::ConfigureResult result(configure_status, |
374 desired_types_, | 478 desired_types_, |
375 failed_datatypes_info_); | 479 failed_datatypes_info_, |
480 GetTypesWaitingToLoad()); | |
376 result_processor_->OnModelAssociationDone(result); | 481 result_processor_->OnModelAssociationDone(result); |
377 return; | 482 return; |
378 } | 483 } |
379 | 484 |
485 syncable::ModelTypeSet ModelAssociationManager::GetTypesWaitingToLoad() { | |
486 syncable::ModelTypeSet result; | |
487 for (std::vector<DataTypeController*>::const_iterator it = | |
488 pending_model_load_.begin(); | |
489 it != pending_model_load_.end(); | |
490 ++it) { | |
491 result.Put((*it)->type()); | |
492 } | |
493 return result; | |
494 } | |
495 | |
496 base::OneShotTimer<ModelAssociationManager>* | |
497 ModelAssociationManager::GetTimerForTesting() { | |
498 return &timer_; | |
499 } | |
500 | |
380 } // namespace browser_sync | 501 } // namespace browser_sync |
381 | 502 |
OLD | NEW |