| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/sync/test/integration/profile_sync_service_harness.h" | 5 #include "chrome/browser/sync/test/integration/profile_sync_service_harness.h" |
| 6 | 6 |
| 7 #include <cstddef> | 7 #include <cstddef> |
| 8 #include <iterator> | 8 #include <iterator> |
| 9 #include <ostream> | 9 #include <ostream> |
| 10 #include <set> | 10 #include <set> |
| 11 #include <sstream> | 11 #include <sstream> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/base64.h" | |
| 15 #include "base/bind.h" | 14 #include "base/bind.h" |
| 16 #include "base/command_line.h" | 15 #include "base/command_line.h" |
| 17 #include "base/compiler_specific.h" | 16 #include "base/compiler_specific.h" |
| 18 #include "base/json/json_writer.h" | 17 #include "base/json/json_writer.h" |
| 19 #include "base/location.h" | 18 #include "base/location.h" |
| 20 #include "base/logging.h" | 19 #include "base/logging.h" |
| 21 #include "base/message_loop/message_loop.h" | 20 #include "base/message_loop/message_loop.h" |
| 22 #include "base/prefs/pref_service.h" | 21 #include "base/prefs/pref_service.h" |
| 23 #include "base/strings/stringprintf.h" | 22 #include "base/strings/stringprintf.h" |
| 24 #include "base/timer/timer.h" | 23 #include "base/timer/timer.h" |
| 25 #include "chrome/browser/chrome_notification_types.h" | 24 #include "chrome/browser/chrome_notification_types.h" |
| 26 #include "chrome/browser/invalidation/p2p_invalidation_service.h" | 25 #include "chrome/browser/invalidation/p2p_invalidation_service.h" |
| 27 #include "chrome/browser/profiles/profile.h" | 26 #include "chrome/browser/profiles/profile.h" |
| 28 #include "chrome/browser/signin/profile_oauth2_token_service.h" | 27 #include "chrome/browser/signin/profile_oauth2_token_service.h" |
| 29 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 28 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 30 #include "chrome/browser/signin/signin_manager_base.h" | 29 #include "chrome/browser/signin/signin_manager_base.h" |
| 31 #include "chrome/browser/sync/about_sync_util.h" | 30 #include "chrome/browser/sync/about_sync_util.h" |
| 32 #include "chrome/browser/sync/backend_migrator.h" | 31 #include "chrome/browser/sync/backend_migrator.h" |
| 33 #include "chrome/browser/sync/profile_sync_service_factory.h" | 32 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 34 #include "chrome/browser/sync/test/integration/p2p_invalidation_forwarder.h" | 33 #include "chrome/browser/sync/test/integration/p2p_invalidation_forwarder.h" |
| 34 #include "chrome/browser/sync/test/integration/quiesce_status_change_checker.h" |
| 35 #include "chrome/browser/sync/test/integration/single_client_status_change_check
er.h" | 35 #include "chrome/browser/sync/test/integration/single_client_status_change_check
er.h" |
| 36 #include "chrome/browser/sync/test/integration/status_change_checker.h" | 36 #include "chrome/browser/sync/test/integration/status_change_checker.h" |
| 37 #include "chrome/browser/sync/test/integration/updated_progress_marker_checker.h
" |
| 37 #include "chrome/common/chrome_switches.h" | 38 #include "chrome/common/chrome_switches.h" |
| 38 #include "chrome/common/pref_names.h" | 39 #include "chrome/common/pref_names.h" |
| 39 #include "components/sync_driver/data_type_controller.h" | 40 #include "components/sync_driver/data_type_controller.h" |
| 40 #include "content/public/browser/notification_service.h" | 41 #include "content/public/browser/notification_service.h" |
| 41 #include "google_apis/gaia/gaia_constants.h" | 42 #include "google_apis/gaia/gaia_constants.h" |
| 42 #include "sync/internal_api/public/base/progress_marker_map.h" | 43 #include "sync/internal_api/public/base/progress_marker_map.h" |
| 43 #include "sync/internal_api/public/sessions/sync_session_snapshot.h" | 44 #include "sync/internal_api/public/sessions/sync_session_snapshot.h" |
| 44 #include "sync/internal_api/public/util/sync_string_conversions.h" | 45 #include "sync/internal_api/public/util/sync_string_conversions.h" |
| 45 | 46 |
| 46 #if defined(ENABLE_MANAGED_USERS) | 47 #if defined(ENABLE_MANAGED_USERS) |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 p2p_invalidation_service); | 166 p2p_invalidation_service); |
| 166 } | 167 } |
| 167 | 168 |
| 168 ProfileSyncServiceHarness::ProfileSyncServiceHarness( | 169 ProfileSyncServiceHarness::ProfileSyncServiceHarness( |
| 169 Profile* profile, | 170 Profile* profile, |
| 170 const std::string& username, | 171 const std::string& username, |
| 171 const std::string& password, | 172 const std::string& password, |
| 172 P2PInvalidationService* p2p_invalidation_service) | 173 P2PInvalidationService* p2p_invalidation_service) |
| 173 : profile_(profile), | 174 : profile_(profile), |
| 174 service_(ProfileSyncServiceFactory::GetForProfile(profile)), | 175 service_(ProfileSyncServiceFactory::GetForProfile(profile)), |
| 175 progress_marker_partner_(NULL), | |
| 176 username_(username), | 176 username_(username), |
| 177 password_(password), | 177 password_(password), |
| 178 oauth2_refesh_token_number_(0), | 178 oauth2_refesh_token_number_(0), |
| 179 profile_debug_name_(profile->GetDebugName()), | 179 profile_debug_name_(profile->GetDebugName()), |
| 180 status_change_checker_(NULL) { | 180 status_change_checker_(NULL) { |
| 181 // Start listening for and emitting notifications of commits. | 181 // Start listening for and emitting notifications of commits. |
| 182 p2p_invalidation_forwarder_.reset( | 182 p2p_invalidation_forwarder_.reset( |
| 183 new P2PInvalidationForwarder(service_, p2p_invalidation_service)); | 183 new P2PInvalidationForwarder(service_, p2p_invalidation_service)); |
| 184 } | 184 } |
| 185 | 185 |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 "DoneWaitingForBackendInitialization"); | 348 "DoneWaitingForBackendInitialization"); |
| 349 AwaitStatusChange(&backend_initialized_checker); | 349 AwaitStatusChange(&backend_initialized_checker); |
| 350 return service()->sync_initialized(); | 350 return service()->sync_initialized(); |
| 351 } | 351 } |
| 352 | 352 |
| 353 // TODO(sync): As of today, we wait for a client to finish its commit activity | 353 // TODO(sync): As of today, we wait for a client to finish its commit activity |
| 354 // by checking if its progress markers are up to date. In future, once we have | 354 // by checking if its progress markers are up to date. In future, once we have |
| 355 // an in-process C++ server, this function can be reimplemented without relying | 355 // an in-process C++ server, this function can be reimplemented without relying |
| 356 // on progress markers. | 356 // on progress markers. |
| 357 bool ProfileSyncServiceHarness::AwaitCommitActivityCompletion() { | 357 bool ProfileSyncServiceHarness::AwaitCommitActivityCompletion() { |
| 358 DVLOG(1) << GetClientInfoString("AwaitCommitActivityCompletion"); | 358 UpdatedProgressMarkerChecker progress_marker_checker(service()); |
| 359 CallbackStatusChecker latest_progress_markers_checker( | 359 return AwaitStatusChange(&progress_marker_checker); |
| 360 service(), | |
| 361 base::Bind(&ProfileSyncServiceHarness::HasLatestProgressMarkers, | |
| 362 base::Unretained(this)), | |
| 363 "HasLatestProgressMarkers"); | |
| 364 AwaitStatusChange(&latest_progress_markers_checker); | |
| 365 return HasLatestProgressMarkers(); | |
| 366 } | 360 } |
| 367 | 361 |
| 368 bool ProfileSyncServiceHarness::AwaitSyncDisabled() { | 362 bool ProfileSyncServiceHarness::AwaitSyncDisabled() { |
| 369 DCHECK(service()->HasSyncSetupCompleted()); | 363 DCHECK(service()->HasSyncSetupCompleted()); |
| 370 DCHECK(!IsSyncDisabled()); | 364 DCHECK(!IsSyncDisabled()); |
| 371 CallbackStatusChecker sync_disabled_checker( | 365 CallbackStatusChecker sync_disabled_checker( |
| 372 service(), | 366 service(), |
| 373 base::Bind(&ProfileSyncServiceHarness::IsSyncDisabled, | 367 base::Bind(&ProfileSyncServiceHarness::IsSyncDisabled, |
| 374 base::Unretained(this)), | 368 base::Unretained(this)), |
| 375 "IsSyncDisabled"); | 369 "IsSyncDisabled"); |
| 376 return AwaitStatusChange(&sync_disabled_checker); | 370 return AwaitStatusChange(&sync_disabled_checker); |
| 377 } | 371 } |
| 378 | 372 |
| 379 bool ProfileSyncServiceHarness::AwaitSyncSetupCompletion() { | 373 bool ProfileSyncServiceHarness::AwaitSyncSetupCompletion() { |
| 380 CallbackStatusChecker sync_setup_complete_checker( | 374 CallbackStatusChecker sync_setup_complete_checker( |
| 381 service(), | 375 service(), |
| 382 base::Bind(&DoneWaitingForSyncSetup, base::Unretained(this)), | 376 base::Bind(&DoneWaitingForSyncSetup, base::Unretained(this)), |
| 383 "DoneWaitingForSyncSetup"); | 377 "DoneWaitingForSyncSetup"); |
| 384 return AwaitStatusChange(&sync_setup_complete_checker); | 378 return AwaitStatusChange(&sync_setup_complete_checker); |
| 385 } | 379 } |
| 386 | 380 |
| 387 bool ProfileSyncServiceHarness::AwaitMutualSyncCycleCompletion( | 381 bool ProfileSyncServiceHarness::AwaitMutualSyncCycleCompletion( |
| 388 ProfileSyncServiceHarness* partner) { | 382 ProfileSyncServiceHarness* partner) { |
| 389 DVLOG(1) << GetClientInfoString("AwaitMutualSyncCycleCompletion"); | 383 std::vector<ProfileSyncServiceHarness*> harnesses; |
| 390 if (!AwaitCommitActivityCompletion()) | 384 harnesses.push_back(this); |
| 391 return false; | 385 harnesses.push_back(partner); |
| 392 return partner->WaitUntilProgressMarkersMatch(this); | 386 return AwaitQuiescence(harnesses); |
| 393 } | 387 } |
| 394 | 388 |
| 395 bool ProfileSyncServiceHarness::AwaitGroupSyncCycleCompletion( | 389 bool ProfileSyncServiceHarness::AwaitGroupSyncCycleCompletion( |
| 396 std::vector<ProfileSyncServiceHarness*>& partners) { | 390 std::vector<ProfileSyncServiceHarness*>& partners) { |
| 397 DVLOG(1) << GetClientInfoString("AwaitGroupSyncCycleCompletion"); | 391 return AwaitQuiescence(partners); |
| 398 if (!AwaitCommitActivityCompletion()) | |
| 399 return false; | |
| 400 bool return_value = true; | |
| 401 for (std::vector<ProfileSyncServiceHarness*>::iterator it = | |
| 402 partners.begin(); it != partners.end(); ++it) { | |
| 403 if ((this != *it) && (!(*it)->IsSyncDisabled())) { | |
| 404 return_value = return_value && | |
| 405 (*it)->WaitUntilProgressMarkersMatch(this); | |
| 406 } | |
| 407 } | |
| 408 return return_value; | |
| 409 } | 392 } |
| 410 | 393 |
| 411 // static | 394 // static |
| 412 bool ProfileSyncServiceHarness::AwaitQuiescence( | 395 bool ProfileSyncServiceHarness::AwaitQuiescence( |
| 413 std::vector<ProfileSyncServiceHarness*>& clients) { | 396 std::vector<ProfileSyncServiceHarness*>& clients) { |
| 414 DVLOG(1) << "AwaitQuiescence."; | 397 std::vector<ProfileSyncService*> services; |
| 415 bool return_value = true; | 398 if (clients.empty()) { |
| 416 for (std::vector<ProfileSyncServiceHarness*>::iterator it = | 399 return true; |
| 417 clients.begin(); it != clients.end(); ++it) { | |
| 418 if (!(*it)->IsSyncDisabled()) { | |
| 419 return_value = return_value && | |
| 420 (*it)->AwaitGroupSyncCycleCompletion(clients); | |
| 421 } | |
| 422 } | 400 } |
| 423 return return_value; | |
| 424 } | |
| 425 | 401 |
| 426 bool ProfileSyncServiceHarness::WaitUntilProgressMarkersMatch( | 402 for (std::vector<ProfileSyncServiceHarness*>::iterator it = clients.begin(); |
| 427 ProfileSyncServiceHarness* partner) { | 403 it != clients.end(); ++it) { |
| 428 DVLOG(1) << GetClientInfoString("WaitUntilProgressMarkersMatch"); | 404 services.push_back((*it)->service()); |
| 429 | |
| 430 // TODO(rsimha): Replace the mechanism of matching up progress markers with | |
| 431 // one that doesn't require every client to have the same progress markers. | |
| 432 DCHECK(!progress_marker_partner_); | |
| 433 progress_marker_partner_ = partner; | |
| 434 bool return_value = false; | |
| 435 if (MatchesPartnerClient()) { | |
| 436 // Progress markers already match; don't wait. | |
| 437 return_value = true; | |
| 438 } else { | |
| 439 partner->service()->AddObserver(this); | |
| 440 CallbackStatusChecker matches_other_client_checker( | |
| 441 service(), | |
| 442 base::Bind(&ProfileSyncServiceHarness::MatchesPartnerClient, | |
| 443 base::Unretained(this)), | |
| 444 "MatchesPartnerClient"); | |
| 445 return_value = AwaitStatusChange(&matches_other_client_checker); | |
| 446 partner->service()->RemoveObserver(this); | |
| 447 } | 405 } |
| 448 progress_marker_partner_ = NULL; | 406 QuiesceStatusChangeChecker checker(services); |
| 449 return return_value; | 407 return clients[0]->AwaitStatusChange(&checker); |
| 450 } | 408 } |
| 451 | 409 |
| 452 bool ProfileSyncServiceHarness::AwaitStatusChange( | 410 bool ProfileSyncServiceHarness::AwaitStatusChange( |
| 453 StatusChangeChecker* checker) { | 411 StatusChangeChecker* checker) { |
| 454 DVLOG(1) << GetClientInfoString("AwaitStatusChange"); | 412 DVLOG(1) << GetClientInfoString("AwaitStatusChange"); |
| 455 | 413 |
| 456 if (IsSyncDisabled()) { | |
| 457 LOG(ERROR) << "Sync disabled for " << profile_debug_name_ << "."; | |
| 458 return false; | |
| 459 } | |
| 460 | |
| 461 DCHECK(checker); | 414 DCHECK(checker); |
| 462 if (checker->IsExitConditionSatisfied()) { | 415 if (checker->IsExitConditionSatisfied()) { |
| 463 DVLOG(1) << GetClientInfoString("AwaitStatusChange exiting early because " | 416 DVLOG(1) << GetClientInfoString("AwaitStatusChange exiting early because " |
| 464 "condition is already satisfied"); | 417 "condition is already satisfied"); |
| 465 return true; | 418 return true; |
| 466 } | 419 } |
| 467 | 420 |
| 468 DCHECK(status_change_checker_ == NULL); | 421 DCHECK(status_change_checker_ == NULL); |
| 469 status_change_checker_ = checker; | 422 status_change_checker_ = checker; |
| 470 status_change_checker_->InitObserver(this); | 423 status_change_checker_->InitObserver(this); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 514 | 467 |
| 515 bool ProfileSyncServiceHarness::HasAuthError() const { | 468 bool ProfileSyncServiceHarness::HasAuthError() const { |
| 516 return service()->GetAuthError().state() == | 469 return service()->GetAuthError().state() == |
| 517 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS || | 470 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS || |
| 518 service()->GetAuthError().state() == | 471 service()->GetAuthError().state() == |
| 519 GoogleServiceAuthError::SERVICE_ERROR || | 472 GoogleServiceAuthError::SERVICE_ERROR || |
| 520 service()->GetAuthError().state() == | 473 service()->GetAuthError().state() == |
| 521 GoogleServiceAuthError::REQUEST_CANCELED; | 474 GoogleServiceAuthError::REQUEST_CANCELED; |
| 522 } | 475 } |
| 523 | 476 |
| 524 // TODO(sync): Remove this method once we stop relying on self notifications and | |
| 525 // comparing progress markers. | |
| 526 bool ProfileSyncServiceHarness::HasLatestProgressMarkers() const { | |
| 527 const SyncSessionSnapshot& snap = GetLastSessionSnapshot(); | |
| 528 return snap.model_neutral_state().num_successful_commits == 0 && | |
| 529 !service()->HasUnsyncedItems(); | |
| 530 } | |
| 531 | |
| 532 void ProfileSyncServiceHarness::FinishSyncSetup() { | 477 void ProfileSyncServiceHarness::FinishSyncSetup() { |
| 533 service()->SetSetupInProgress(false); | 478 service()->SetSetupInProgress(false); |
| 534 service()->SetSyncSetupCompleted(); | 479 service()->SetSyncSetupCompleted(); |
| 535 } | 480 } |
| 536 | 481 |
| 537 bool ProfileSyncServiceHarness::AutoStartEnabled() { | 482 bool ProfileSyncServiceHarness::AutoStartEnabled() { |
| 538 return service()->auto_start_enabled(); | 483 return service()->auto_start_enabled(); |
| 539 } | 484 } |
| 540 | 485 |
| 541 bool ProfileSyncServiceHarness::MatchesPartnerClient() const { | |
| 542 DCHECK(progress_marker_partner_); | |
| 543 | |
| 544 // Only look for a match if we have at least one enabled datatype in | |
| 545 // common with the partner client. | |
| 546 const syncer::ModelTypeSet common_types = | |
| 547 Intersection(service()->GetActiveDataTypes(), | |
| 548 progress_marker_partner_->service()->GetActiveDataTypes()); | |
| 549 | |
| 550 DVLOG(2) << profile_debug_name_ << ", " | |
| 551 << progress_marker_partner_->profile_debug_name_ | |
| 552 << ": common types are " | |
| 553 << syncer::ModelTypeSetToString(common_types); | |
| 554 | |
| 555 for (syncer::ModelTypeSet::Iterator i = common_types.First(); | |
| 556 i.Good(); i.Inc()) { | |
| 557 const std::string marker = GetSerializedProgressMarker(i.Get()); | |
| 558 const std::string partner_marker = | |
| 559 progress_marker_partner_->GetSerializedProgressMarker(i.Get()); | |
| 560 if (marker != partner_marker) { | |
| 561 if (VLOG_IS_ON(2)) { | |
| 562 std::string marker_base64, partner_marker_base64; | |
| 563 base::Base64Encode(marker, &marker_base64); | |
| 564 base::Base64Encode(partner_marker, &partner_marker_base64); | |
| 565 DVLOG(2) << syncer::ModelTypeToString(i.Get()) << ": " | |
| 566 << profile_debug_name_ << " progress marker = " | |
| 567 << marker_base64 << ", " | |
| 568 << progress_marker_partner_->profile_debug_name_ | |
| 569 << " partner progress marker = " | |
| 570 << partner_marker_base64; | |
| 571 } | |
| 572 return false; | |
| 573 } | |
| 574 } | |
| 575 return true; | |
| 576 } | |
| 577 | |
| 578 SyncSessionSnapshot ProfileSyncServiceHarness::GetLastSessionSnapshot() const { | 486 SyncSessionSnapshot ProfileSyncServiceHarness::GetLastSessionSnapshot() const { |
| 579 DCHECK(service() != NULL) << "Sync service has not yet been set up."; | 487 DCHECK(service() != NULL) << "Sync service has not yet been set up."; |
| 580 if (service()->sync_initialized()) { | 488 if (service()->sync_initialized()) { |
| 581 return service()->GetLastSessionSnapshot(); | 489 return service()->GetLastSessionSnapshot(); |
| 582 } | 490 } |
| 583 return SyncSessionSnapshot(); | 491 return SyncSessionSnapshot(); |
| 584 } | 492 } |
| 585 | 493 |
| 586 bool ProfileSyncServiceHarness::EnableSyncForDatatype( | 494 bool ProfileSyncServiceHarness::EnableSyncForDatatype( |
| 587 syncer::ModelType datatype) { | 495 syncer::ModelType datatype) { |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 789 | 697 |
| 790 std::string ProfileSyncServiceHarness::GetServiceStatus() { | 698 std::string ProfileSyncServiceHarness::GetServiceStatus() { |
| 791 scoped_ptr<base::DictionaryValue> value( | 699 scoped_ptr<base::DictionaryValue> value( |
| 792 sync_ui_util::ConstructAboutInformation(service())); | 700 sync_ui_util::ConstructAboutInformation(service())); |
| 793 std::string service_status; | 701 std::string service_status; |
| 794 base::JSONWriter::WriteWithOptions(value.get(), | 702 base::JSONWriter::WriteWithOptions(value.get(), |
| 795 base::JSONWriter::OPTIONS_PRETTY_PRINT, | 703 base::JSONWriter::OPTIONS_PRETTY_PRINT, |
| 796 &service_status); | 704 &service_status); |
| 797 return service_status; | 705 return service_status; |
| 798 } | 706 } |
| OLD | NEW |