| 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> | 5 #include <algorithm> |
| 6 #include <functional> | 6 #include <functional> |
| 7 | 7 |
| 8 #include "base/debug/trace_event.h" | 8 #include "base/debug/trace_event.h" |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 13 | 13 |
| 14 #include "chrome/browser/sync/glue/model_association_manager.h" | 14 #include "chrome/browser/sync/glue/model_association_manager.h" |
| 15 | 15 |
| 16 #include "content/public/browser/browser_thread.h" | |
| 17 #include <algorithm> | 16 #include <algorithm> |
| 18 #include <functional> | 17 #include <functional> |
| 18 #include "content/public/browser/browser_thread.h" |
| 19 | 19 |
| 20 #include "base/debug/trace_event.h" | 20 #include "base/debug/trace_event.h" |
| 21 | 21 |
| 22 #include "base/logging.h" | 22 #include "base/logging.h" |
| 23 #include "base/message_loop.h" | 23 #include "base/message_loop.h" |
| 24 #include "base/metrics/histogram.h" | 24 #include "base/metrics/histogram.h" |
| 25 | 25 |
| 26 | 26 |
| 27 using content::BrowserThread; | 27 using content::BrowserThread; |
| 28 using syncable::ModelTypeSet; | 28 using syncer::ModelTypeSet; |
| 29 | 29 |
| 30 namespace browser_sync { | 30 namespace browser_sync { |
| 31 // The amount of time we wait for a datatype to load. If the type has | 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 | 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 | 33 // finishes loading we will do a configure to associate this type. Note |
| 34 // that in most cases types finish loading before this timeout. | 34 // that in most cases types finish loading before this timeout. |
| 35 const int64 kDataTypeLoadWaitTimeInSeconds = 120; | 35 const int64 kDataTypeLoadWaitTimeInSeconds = 120; |
| 36 namespace { | 36 namespace { |
| 37 | 37 |
| 38 static const syncable::ModelType kStartOrder[] = { | 38 static const syncer::ModelType kStartOrder[] = { |
| 39 syncable::NIGORI, // Listed for completeness. | 39 syncer::NIGORI, // Listed for completeness. |
| 40 syncable::BOOKMARKS, // UI thread datatypes. | 40 syncer::BOOKMARKS, // UI thread datatypes. |
| 41 syncable::PREFERENCES, | 41 syncer::PREFERENCES, |
| 42 syncable::EXTENSIONS, | 42 syncer::EXTENSIONS, |
| 43 syncable::APPS, | 43 syncer::APPS, |
| 44 syncable::THEMES, | 44 syncer::THEMES, |
| 45 syncable::SEARCH_ENGINES, | 45 syncer::SEARCH_ENGINES, |
| 46 syncable::SESSIONS, | 46 syncer::SESSIONS, |
| 47 syncable::APP_NOTIFICATIONS, | 47 syncer::APP_NOTIFICATIONS, |
| 48 syncable::AUTOFILL, // Non-UI thread datatypes. | 48 syncer::AUTOFILL, // Non-UI thread datatypes. |
| 49 syncable::AUTOFILL_PROFILE, | 49 syncer::AUTOFILL_PROFILE, |
| 50 syncable::EXTENSION_SETTINGS, | 50 syncer::EXTENSION_SETTINGS, |
| 51 syncable::APP_SETTINGS, | 51 syncer::APP_SETTINGS, |
| 52 syncable::TYPED_URLS, | 52 syncer::TYPED_URLS, |
| 53 syncable::PASSWORDS, | 53 syncer::PASSWORDS, |
| 54 }; | 54 }; |
| 55 | 55 |
| 56 COMPILE_ASSERT(arraysize(kStartOrder) == | 56 COMPILE_ASSERT(arraysize(kStartOrder) == |
| 57 syncable::MODEL_TYPE_COUNT - syncable::FIRST_REAL_MODEL_TYPE, | 57 syncer::MODEL_TYPE_COUNT - syncer::FIRST_REAL_MODEL_TYPE, |
| 58 kStartOrder_IncorrectSize); | 58 kStartOrder_IncorrectSize); |
| 59 | 59 |
| 60 // Comparator used when sorting data type controllers. | 60 // Comparator used when sorting data type controllers. |
| 61 class SortComparator : public std::binary_function<DataTypeController*, | 61 class SortComparator : public std::binary_function<DataTypeController*, |
| 62 DataTypeController*, | 62 DataTypeController*, |
| 63 bool> { | 63 bool> { |
| 64 public: | 64 public: |
| 65 explicit SortComparator(std::map<syncable::ModelType, int>* order) | 65 explicit SortComparator(std::map<syncer::ModelType, int>* order) |
| 66 : order_(order) { } | 66 : order_(order) { } |
| 67 | 67 |
| 68 // Returns true if lhs precedes rhs. | 68 // Returns true if lhs precedes rhs. |
| 69 bool operator() (DataTypeController* lhs, DataTypeController* rhs) { | 69 bool operator() (DataTypeController* lhs, DataTypeController* rhs) { |
| 70 return (*order_)[lhs->type()] < (*order_)[rhs->type()]; | 70 return (*order_)[lhs->type()] < (*order_)[rhs->type()]; |
| 71 } | 71 } |
| 72 | 72 |
| 73 private: | 73 private: |
| 74 std::map<syncable::ModelType, int>* order_; | 74 std::map<syncer::ModelType, int>* order_; |
| 75 }; | 75 }; |
| 76 | 76 |
| 77 } // namespace | 77 } // namespace |
| 78 | 78 |
| 79 ModelAssociationManager::ModelAssociationManager( | 79 ModelAssociationManager::ModelAssociationManager( |
| 80 const DataTypeController::TypeMap* controllers, | 80 const DataTypeController::TypeMap* controllers, |
| 81 ModelAssociationResultProcessor* processor) | 81 ModelAssociationResultProcessor* processor) |
| 82 : state_(IDLE), | 82 : state_(IDLE), |
| 83 currently_associating_(NULL), | 83 currently_associating_(NULL), |
| 84 controllers_(controllers), | 84 controllers_(controllers), |
| 85 result_processor_(processor), | 85 result_processor_(processor), |
| 86 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 86 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 87 | 87 |
| 88 // Ensure all data type controllers are stopped. | 88 // Ensure all data type controllers are stopped. |
| 89 for (DataTypeController::TypeMap::const_iterator it = controllers_->begin(); | 89 for (DataTypeController::TypeMap::const_iterator it = controllers_->begin(); |
| 90 it != controllers_->end(); ++it) { | 90 it != controllers_->end(); ++it) { |
| 91 DCHECK_EQ(DataTypeController::NOT_RUNNING, (*it).second->state()); | 91 DCHECK_EQ(DataTypeController::NOT_RUNNING, (*it).second->state()); |
| 92 } | 92 } |
| 93 | 93 |
| 94 // Build a ModelType -> order map for sorting. | 94 // Build a ModelType -> order map for sorting. |
| 95 for (int i = 0; i < static_cast<int>(arraysize(kStartOrder)); i++) | 95 for (int i = 0; i < static_cast<int>(arraysize(kStartOrder)); i++) |
| 96 start_order_[kStartOrder[i]] = i; | 96 start_order_[kStartOrder[i]] = i; |
| 97 } | 97 } |
| 98 | 98 |
| 99 ModelAssociationManager::~ModelAssociationManager() { | 99 ModelAssociationManager::~ModelAssociationManager() { |
| 100 } | 100 } |
| 101 | 101 |
| 102 void ModelAssociationManager::Initialize( | 102 void ModelAssociationManager::Initialize(syncer::ModelTypeSet desired_types) { |
| 103 syncable::ModelTypeSet desired_types) { | |
| 104 // TODO(tim): Bug 134550. CHECKing to ensure no reentrancy on dev channel. | 103 // TODO(tim): Bug 134550. CHECKing to ensure no reentrancy on dev channel. |
| 105 // Remove this. | 104 // Remove this. |
| 106 CHECK_EQ(state_, IDLE); | 105 CHECK_EQ(state_, IDLE); |
| 107 needs_start_.clear(); | 106 needs_start_.clear(); |
| 108 needs_stop_.clear(); | 107 needs_stop_.clear(); |
| 109 failed_datatypes_info_.clear(); | 108 failed_datatypes_info_.clear(); |
| 110 desired_types_ = desired_types; | 109 desired_types_ = desired_types; |
| 111 state_ = INITIALIZED_TO_CONFIGURE; | 110 state_ = INITIALIZED_TO_CONFIGURE; |
| 112 | 111 |
| 113 DVLOG(1) << "ModelAssociationManager: Initializing"; | 112 DVLOG(1) << "ModelAssociationManager: Initializing"; |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 dtc->Stop(); | 221 dtc->Stop(); |
| 223 DVLOG(1) << "ModelAssociationManager: Stopped " << dtc->name(); | 222 DVLOG(1) << "ModelAssociationManager: Stopped " << dtc->name(); |
| 224 } | 223 } |
| 225 } | 224 } |
| 226 | 225 |
| 227 if (need_to_call_model_association_done) { | 226 if (need_to_call_model_association_done) { |
| 228 DVLOG(1) << "ModelAssociationManager: Calling OnModelAssociationDone"; | 227 DVLOG(1) << "ModelAssociationManager: Calling OnModelAssociationDone"; |
| 229 DataTypeManager::ConfigureResult result(DataTypeManager::ABORTED, | 228 DataTypeManager::ConfigureResult result(DataTypeManager::ABORTED, |
| 230 desired_types_, | 229 desired_types_, |
| 231 failed_datatypes_info_, | 230 failed_datatypes_info_, |
| 232 syncable::ModelTypeSet()); | 231 syncer::ModelTypeSet()); |
| 233 result_processor_->OnModelAssociationDone(result); | 232 result_processor_->OnModelAssociationDone(result); |
| 234 } | 233 } |
| 235 | 234 |
| 236 failed_datatypes_info_.clear(); | 235 failed_datatypes_info_.clear(); |
| 237 } | 236 } |
| 238 | 237 |
| 239 bool ModelAssociationManager::GetControllersNeedingStart( | 238 bool ModelAssociationManager::GetControllersNeedingStart( |
| 240 std::vector<DataTypeController*>* needs_start) { | 239 std::vector<DataTypeController*>* needs_start) { |
| 241 DVLOG(1) << "ModelAssociationManager: GetControllersNeedingStart"; | 240 DVLOG(1) << "ModelAssociationManager: GetControllersNeedingStart"; |
| 242 // Add any data type controllers into the needs_start_ list that are | 241 // Add any data type controllers into the needs_start_ list that are |
| 243 // currently NOT_RUNNING or STOPPING. | 242 // currently NOT_RUNNING or STOPPING. |
| 244 bool found_any = false; | 243 bool found_any = false; |
| 245 for (ModelTypeSet::Iterator it = desired_types_.First(); | 244 for (ModelTypeSet::Iterator it = desired_types_.First(); |
| 246 it.Good(); it.Inc()) { | 245 it.Good(); it.Inc()) { |
| 247 DataTypeController::TypeMap::const_iterator dtc = | 246 DataTypeController::TypeMap::const_iterator dtc = |
| 248 controllers_->find(it.Get()); | 247 controllers_->find(it.Get()); |
| 249 if (dtc != controllers_->end() && | 248 if (dtc != controllers_->end() && |
| 250 (dtc->second->state() == DataTypeController::NOT_RUNNING || | 249 (dtc->second->state() == DataTypeController::NOT_RUNNING || |
| 251 dtc->second->state() == DataTypeController::STOPPING)) { | 250 dtc->second->state() == DataTypeController::STOPPING)) { |
| 252 found_any = true; | 251 found_any = true; |
| 253 if (needs_start) | 252 if (needs_start) |
| 254 needs_start->push_back(dtc->second.get()); | 253 needs_start->push_back(dtc->second.get()); |
| 255 if (dtc->second->state() == DataTypeController::DISABLED) { | 254 if (dtc->second->state() == DataTypeController::DISABLED) { |
| 256 DVLOG(1) << "ModelAssociationManager: Found "\ | 255 DVLOG(1) << "ModelAssociationManager: Found "\ |
| 257 << syncable::ModelTypeToString(dtc->second->type()) | 256 << syncer::ModelTypeToString(dtc->second->type()) |
| 258 << " in disabled state."; | 257 << " in disabled state."; |
| 259 } | 258 } |
| 260 } | 259 } |
| 261 } | 260 } |
| 262 return found_any; | 261 return found_any; |
| 263 } | 262 } |
| 264 | 263 |
| 265 void ModelAssociationManager::AppendToFailedDatatypesAndLogError( | 264 void ModelAssociationManager::AppendToFailedDatatypesAndLogError( |
| 266 DataTypeController::StartResult result, | 265 DataTypeController::StartResult result, |
| 267 const syncer::SyncError& error) { | 266 const syncer::SyncError& error) { |
| 268 failed_datatypes_info_.push_back(error); | 267 failed_datatypes_info_.push_back(error); |
| 269 LOG(ERROR) << "Failed to associate models for " | 268 LOG(ERROR) << "Failed to associate models for " |
| 270 << syncable::ModelTypeToString(error.type()); | 269 << syncer::ModelTypeToString(error.type()); |
| 271 UMA_HISTOGRAM_ENUMERATION("Sync.ConfigureFailed", | 270 UMA_HISTOGRAM_ENUMERATION("Sync.ConfigureFailed", |
| 272 error.type(), | 271 error.type(), |
| 273 syncable::MODEL_TYPE_COUNT); | 272 syncer::MODEL_TYPE_COUNT); |
| 274 } | 273 } |
| 275 | 274 |
| 276 void ModelAssociationManager::TypeStartCallback( | 275 void ModelAssociationManager::TypeStartCallback( |
| 277 DataTypeController::StartResult result, | 276 DataTypeController::StartResult result, |
| 278 const syncer::SyncError& error) { | 277 const syncer::SyncError& error) { |
| 279 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 278 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 280 TRACE_EVENT_END0("sync", "ModelAssociation"); | 279 TRACE_EVENT_END0("sync", "ModelAssociation"); |
| 281 | 280 |
| 282 DVLOG(1) << "ModelAssociationManager: TypeStartCallback"; | 281 DVLOG(1) << "ModelAssociationManager: TypeStartCallback"; |
| 283 if (state_ == ABORTED) { | 282 if (state_ == ABORTED) { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 | 333 |
| 335 std::list<syncer::SyncError> errors; | 334 std::list<syncer::SyncError> errors; |
| 336 errors.push_back(error); | 335 errors.push_back(error); |
| 337 | 336 |
| 338 // Put our state to idle. | 337 // Put our state to idle. |
| 339 state_ = IDLE; | 338 state_ = IDLE; |
| 340 | 339 |
| 341 DataTypeManager::ConfigureResult configure_result(configure_status, | 340 DataTypeManager::ConfigureResult configure_result(configure_status, |
| 342 desired_types_, | 341 desired_types_, |
| 343 errors, | 342 errors, |
| 344 syncable::ModelTypeSet()); | 343 syncer::ModelTypeSet()); |
| 345 result_processor_->OnModelAssociationDone(configure_result); | 344 result_processor_->OnModelAssociationDone(configure_result); |
| 346 } | 345 } |
| 347 | 346 |
| 348 void ModelAssociationManager::LoadModelForNextType() { | 347 void ModelAssociationManager::LoadModelForNextType() { |
| 349 DVLOG(1) << "ModelAssociationManager: LoadModelForNextType"; | 348 DVLOG(1) << "ModelAssociationManager: LoadModelForNextType"; |
| 350 if (!needs_start_.empty()) { | 349 if (!needs_start_.empty()) { |
| 351 DVLOG(1) << "ModelAssociationManager: Starting " << needs_start_[0]->name(); | 350 DVLOG(1) << "ModelAssociationManager: Starting " << needs_start_[0]->name(); |
| 352 | 351 |
| 353 DataTypeController* dtc = needs_start_[0]; | 352 DataTypeController* dtc = needs_start_[0]; |
| 354 needs_start_.erase(needs_start_.begin()); | 353 needs_start_.erase(needs_start_.begin()); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 367 | 366 |
| 368 DVLOG(1) << "ModelAssociationManager: All types have models loaded." | 367 DVLOG(1) << "ModelAssociationManager: All types have models loaded." |
| 369 << "Moving on to StartAssociatingNextType."; | 368 << "Moving on to StartAssociatingNextType."; |
| 370 | 369 |
| 371 // If all controllers have their |LoadModels| invoked then pass onto | 370 // If all controllers have their |LoadModels| invoked then pass onto |
| 372 // |StartAssociatingNextType|. | 371 // |StartAssociatingNextType|. |
| 373 StartAssociatingNextType(); | 372 StartAssociatingNextType(); |
| 374 } | 373 } |
| 375 | 374 |
| 376 void ModelAssociationManager::ModelLoadCallback( | 375 void ModelAssociationManager::ModelLoadCallback( |
| 377 syncable::ModelType type, syncer::SyncError error) { | 376 syncer::ModelType type, syncer::SyncError error) { |
| 378 DVLOG(1) << "ModelAssociationManager: ModelLoadCallback for " | 377 DVLOG(1) << "ModelAssociationManager: ModelLoadCallback for " |
| 379 << syncable::ModelTypeToString(type); | 378 << syncer::ModelTypeToString(type); |
| 380 if (state_ == CONFIGURING) { | 379 if (state_ == CONFIGURING) { |
| 381 DVLOG(1) << "ModelAssociationManager: ModelLoadCallback while configuring"; | 380 DVLOG(1) << "ModelAssociationManager: ModelLoadCallback while configuring"; |
| 382 for (std::vector<DataTypeController*>::iterator it = | 381 for (std::vector<DataTypeController*>::iterator it = |
| 383 pending_model_load_.begin(); | 382 pending_model_load_.begin(); |
| 384 it != pending_model_load_.end(); | 383 it != pending_model_load_.end(); |
| 385 ++it) { | 384 ++it) { |
| 386 if ((*it)->type() == type) { | 385 if ((*it)->type() == type) { |
| 387 // Each type is given |kDataTypeLoadWaitTimeInSeconds| time to load | 386 // Each type is given |kDataTypeLoadWaitTimeInSeconds| time to load |
| 388 // (as controlled by the timer.). If the type does not load in that | 387 // (as controlled by the timer.). If the type does not load in that |
| 389 // time we move on to the next type. However if the type does | 388 // time we move on to the next type. However if the type does |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 460 // things like encryption, which may still need to be sorted out before we | 459 // things like encryption, which may still need to be sorted out before we |
| 461 // can announce we're "Done" configuration entirely. | 460 // can announce we're "Done" configuration entirely. |
| 462 if (GetControllersNeedingStart(NULL)) { | 461 if (GetControllersNeedingStart(NULL)) { |
| 463 DVLOG(1) << "ModelAssociationManager: GetControllersNeedingStart" | 462 DVLOG(1) << "ModelAssociationManager: GetControllersNeedingStart" |
| 464 << " returned true. Blocking DataTypeManager"; | 463 << " returned true. Blocking DataTypeManager"; |
| 465 | 464 |
| 466 DataTypeManager::ConfigureResult configure_result( | 465 DataTypeManager::ConfigureResult configure_result( |
| 467 DataTypeManager::CONFIGURE_BLOCKED, | 466 DataTypeManager::CONFIGURE_BLOCKED, |
| 468 desired_types_, | 467 desired_types_, |
| 469 failed_datatypes_info_, | 468 failed_datatypes_info_, |
| 470 syncable::ModelTypeSet()); | 469 syncer::ModelTypeSet()); |
| 471 state_ = IDLE; | 470 state_ = IDLE; |
| 472 result_processor_->OnModelAssociationDone(configure_result); | 471 result_processor_->OnModelAssociationDone(configure_result); |
| 473 return; | 472 return; |
| 474 } | 473 } |
| 475 | 474 |
| 476 DataTypeManager::ConfigureStatus configure_status = DataTypeManager::OK; | 475 DataTypeManager::ConfigureStatus configure_status = DataTypeManager::OK; |
| 477 | 476 |
| 478 if (!failed_datatypes_info_.empty() || | 477 if (!failed_datatypes_info_.empty() || |
| 479 !GetTypesWaitingToLoad().Empty()) { | 478 !GetTypesWaitingToLoad().Empty()) { |
| 480 // We have not configured all types that we have been asked to configure. | 479 // We have not configured all types that we have been asked to configure. |
| 481 // Either we have failed types or types that have not completed loading | 480 // Either we have failed types or types that have not completed loading |
| 482 // yet. | 481 // yet. |
| 483 DVLOG(1) << "ModelAssociationManager: setting partial success"; | 482 DVLOG(1) << "ModelAssociationManager: setting partial success"; |
| 484 configure_status = DataTypeManager::PARTIAL_SUCCESS; | 483 configure_status = DataTypeManager::PARTIAL_SUCCESS; |
| 485 } | 484 } |
| 486 | 485 |
| 487 DataTypeManager::ConfigureResult result(configure_status, | 486 DataTypeManager::ConfigureResult result(configure_status, |
| 488 desired_types_, | 487 desired_types_, |
| 489 failed_datatypes_info_, | 488 failed_datatypes_info_, |
| 490 GetTypesWaitingToLoad()); | 489 GetTypesWaitingToLoad()); |
| 491 result_processor_->OnModelAssociationDone(result); | 490 result_processor_->OnModelAssociationDone(result); |
| 492 return; | 491 return; |
| 493 } | 492 } |
| 494 | 493 |
| 495 syncable::ModelTypeSet ModelAssociationManager::GetTypesWaitingToLoad() { | 494 syncer::ModelTypeSet ModelAssociationManager::GetTypesWaitingToLoad() { |
| 496 syncable::ModelTypeSet result; | 495 syncer::ModelTypeSet result; |
| 497 for (std::vector<DataTypeController*>::const_iterator it = | 496 for (std::vector<DataTypeController*>::const_iterator it = |
| 498 pending_model_load_.begin(); | 497 pending_model_load_.begin(); |
| 499 it != pending_model_load_.end(); | 498 it != pending_model_load_.end(); |
| 500 ++it) { | 499 ++it) { |
| 501 result.Put((*it)->type()); | 500 result.Put((*it)->type()); |
| 502 } | 501 } |
| 503 return result; | 502 return result; |
| 504 } | 503 } |
| 505 | 504 |
| 506 base::OneShotTimer<ModelAssociationManager>* | 505 base::OneShotTimer<ModelAssociationManager>* |
| 507 ModelAssociationManager::GetTimerForTesting() { | 506 ModelAssociationManager::GetTimerForTesting() { |
| 508 return &timer_; | 507 return &timer_; |
| 509 } | 508 } |
| 510 | 509 |
| 511 } // namespace browser_sync | 510 } // namespace browser_sync |
| OLD | NEW |