1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/sync/test/integration/migration_waiter.h"
6
7#include "base/logging.h"
8#include "chrome/browser/sync/test/integration/migration_watcher.h"
9
10MigrationWaiter::MigrationWaiter(syncer::ModelTypeSet expected_types,
11                                 MigrationWatcher* watcher)
12    : watcher_(watcher), expected_types_(expected_types) {
13  DCHECK(!expected_types_.Empty());
14  watcher_->set_migration_waiter(this);
15}
16
17MigrationWaiter::~MigrationWaiter() {
18  watcher_->clear_migration_waiter();
19}
20
21// Returns true when sync reports that there is no pending migration, and
22// migration is complete for all data types in |expected_types_|.
23bool MigrationWaiter::IsExitConditionSatisfied() {
24  return watcher_->GetMigratedTypes().HasAll(expected_types_) &&
25         !watcher_->HasPendingBackendMigration();
26}
27
28std::string MigrationWaiter::GetDebugMessage() const {
29  return "Waiting to migrate (" + ModelTypeSetToString(expected_types_) +
30         "); " + "Currently migrated: (" +
31         ModelTypeSetToString(watcher_->GetMigratedTypes()) + ")";
32}
33
34void MigrationWaiter::Wait() {
35  if (IsExitConditionSatisfied()) {
36    DVLOG(1) << "Skipping wait: " << GetDebugMessage();
37    return;
38  }
39
40  StartBlockingWait();
41}
42
43void MigrationWaiter::OnMigrationStateChange() {
44  CheckExitCondition();
45}
46