This source file includes following definitions.
- IsSyncDisabled
- ProgressMarkersMatch
- probably_has_latest_progress_markers_
- OnStateChanged
- UpdateHasLatestProgressMarkers
- HasLatestProgressMarkers
- IsSyncDisabled
- timed_out_
- GetTimeoutDuration
- Wait
- IsExitConditionSatisfied
- GetDebugMessage
- OnServiceStateChanged
- OnTimeout
- TimedOut
#include "chrome/browser/sync/test/integration/quiesce_status_change_checker.h"
#include "base/format_macros.h"
#include "base/scoped_observer.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "chrome/browser/sync/profile_sync_service.h"
#include "sync/internal_api/public/sessions/sync_session_snapshot.h"
namespace {
bool IsSyncDisabled(ProfileSyncService* service) {
return !service->setup_in_progress() && !service->HasSyncSetupCompleted();
}
bool ProgressMarkersMatch(const ProfileSyncService* service1,
const ProfileSyncService* service2) {
const syncer::ModelTypeSet common_types =
Intersection(service1->GetActiveDataTypes(),
service2->GetActiveDataTypes());
const syncer::sessions::SyncSessionSnapshot& snap1 =
service1->GetLastSessionSnapshot();
const syncer::sessions::SyncSessionSnapshot& snap2 =
service2->GetLastSessionSnapshot();
for (syncer::ModelTypeSet::Iterator type_it = common_types.First();
type_it.Good(); type_it.Inc()) {
syncer::ProgressMarkerMap::const_iterator pm_it1 =
snap1.download_progress_markers().find(type_it.Get());
if (pm_it1 == snap1.download_progress_markers().end()) {
return false;
}
syncer::ProgressMarkerMap::const_iterator pm_it2 =
snap2.download_progress_markers().find(type_it.Get());
if (pm_it2 == snap2.download_progress_markers().end()) {
return false;
}
if (pm_it1->second != pm_it2->second) {
return false;
}
}
return true;
}
}
class ProgressMarkerWatcher : public ProfileSyncServiceObserver {
public:
ProgressMarkerWatcher(
ProfileSyncService* service,
QuiesceStatusChangeChecker* quiesce_checker);
virtual ~ProgressMarkerWatcher();
virtual void OnStateChanged() OVERRIDE;
bool HasLatestProgressMarkers();
bool IsSyncDisabled();
private:
void UpdateHasLatestProgressMarkers();
ProfileSyncService* service_;
QuiesceStatusChangeChecker* quiesce_checker_;
ScopedObserver<ProfileSyncService, ProgressMarkerWatcher> scoped_observer_;
bool probably_has_latest_progress_markers_;
};
ProgressMarkerWatcher::ProgressMarkerWatcher(
ProfileSyncService* service,
QuiesceStatusChangeChecker* quiesce_checker)
: service_(service),
quiesce_checker_(quiesce_checker),
scoped_observer_(this),
probably_has_latest_progress_markers_(false) {
scoped_observer_.Add(service);
UpdateHasLatestProgressMarkers();
}
ProgressMarkerWatcher::~ProgressMarkerWatcher() { }
void ProgressMarkerWatcher::OnStateChanged() {
UpdateHasLatestProgressMarkers();
quiesce_checker_->OnServiceStateChanged(service_);
}
void ProgressMarkerWatcher::UpdateHasLatestProgressMarkers() {
if (IsSyncDisabled()) {
probably_has_latest_progress_markers_ = false;
return;
}
const syncer::sessions::SyncSessionSnapshot& snap =
service_->GetLastSessionSnapshot();
probably_has_latest_progress_markers_ =
snap.model_neutral_state().num_successful_commits == 0 &&
!service_->HasUnsyncedItems();
}
bool ProgressMarkerWatcher::HasLatestProgressMarkers() {
return probably_has_latest_progress_markers_;
}
bool ProgressMarkerWatcher::IsSyncDisabled() {
return ::IsSyncDisabled(service_);
}
QuiesceStatusChangeChecker::QuiesceStatusChangeChecker(
std::vector<ProfileSyncService*> services)
: services_(services), timed_out_(false) {
DCHECK_LE(1U, services_.size());
for (size_t i = 0; i < services_.size(); ++i) {
observers_.push_back(new ProgressMarkerWatcher(services[i], this));
}
}
QuiesceStatusChangeChecker::~QuiesceStatusChangeChecker() {}
base::TimeDelta QuiesceStatusChangeChecker::GetTimeoutDuration() {
return base::TimeDelta::FromSeconds(45);
}
void QuiesceStatusChangeChecker::Wait() {
DVLOG(1) << "Await: " << GetDebugMessage();
if (IsExitConditionSatisfied()) {
DVLOG(1) << "Await -> Exit before waiting: " << GetDebugMessage();
return;
}
base::OneShotTimer<QuiesceStatusChangeChecker> timer;
timer.Start(FROM_HERE,
GetTimeoutDuration(),
base::Bind(&QuiesceStatusChangeChecker::OnTimeout,
base::Unretained(this)));
{
base::MessageLoop* loop = base::MessageLoop::current();
base::MessageLoop::ScopedNestableTaskAllower allow(loop);
loop->Run();
}
}
bool QuiesceStatusChangeChecker::IsExitConditionSatisfied() {
for (ScopedVector<ProgressMarkerWatcher>::const_iterator it =
observers_.begin(); it != observers_.end(); ++it) {
if ((*it)->IsSyncDisabled()) {
continue;
}
if (!(*it)->HasLatestProgressMarkers()) {
VLOG(1) << "Not quiesced: Progress markers are old.";
return false;
}
}
std::vector<ProfileSyncService*> enabled_services;
for (std::vector<ProfileSyncService*>::const_iterator it = services_.begin();
it != services_.end(); ++it) {
if (!IsSyncDisabled(*it)) {
enabled_services.push_back(*it);
}
}
if (enabled_services.size() <= 1) {
return true;
}
std::vector<ProfileSyncService*>::const_iterator it1 =
enabled_services.begin();
std::vector<ProfileSyncService*>::const_iterator it2 =
enabled_services.begin();
it2++;
while (it2 != enabled_services.end()) {
if (!ProgressMarkersMatch(*it1, *it2)) {
VLOG(1) << "Not quiesced: Progress marker mismatch.";
return false;
}
it1++;
it2++;
}
return true;
}
std::string QuiesceStatusChangeChecker::GetDebugMessage() const {
return base::StringPrintf("Waiting for quiescence of %" PRIuS " clients",
services_.size());
}
void QuiesceStatusChangeChecker::OnServiceStateChanged(
ProfileSyncService* service) {
if (IsExitConditionSatisfied()) {
DVLOG(1) << "Await -> Condition met: " << GetDebugMessage();
base::MessageLoop::current()->QuitWhenIdle();
}
}
void QuiesceStatusChangeChecker::OnTimeout() {
DVLOG(1) << "Await -> Timed out: " << GetDebugMessage();
timed_out_ = true;
base::MessageLoop::current()->QuitWhenIdle();
}
bool QuiesceStatusChangeChecker::TimedOut() const {
return timed_out_;
}