15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/sync/test/integration/profile_sync_service_harness.h"
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <cstddef>
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <iterator>
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <ostream>
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <sstream>
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <vector>
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/compiler_specific.h"
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/json/json_writer.h"
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/logging.h"
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/strings/stringprintf.h"
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/timer/timer.h"
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/sync/about_sync_util.h"
21effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "chrome/browser/sync/profile_sync_service.h"
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/sync/profile_sync_service_factory.h"
23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/browser/sync/test/integration/quiesce_status_change_checker.h"
24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/browser/sync/test/integration/single_client_status_change_checker.h"
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/common/chrome_switches.h"
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/common/pref_names.h"
27cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/invalidation/p2p_invalidation_service.h"
28effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "components/signin/core/browser/profile_oauth2_token_service.h"
29effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "components/signin/core/browser/signin_manager_base.h"
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "components/sync_driver/data_type_controller.h"
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "google_apis/gaia/gaia_constants.h"
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "sync/internal_api/public/base/progress_marker_map.h"
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "sync/internal_api/public/util/sync_string_conversions.h"
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if defined(ENABLE_MANAGED_USERS)
36f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "chrome/browser/supervised_user/supervised_user_constants.h"
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)using syncer::sessions::SyncSessionSnapshot;
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace {
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
43effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochbool HasAuthError(ProfileSyncService* service) {
44effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  return service->GetAuthError().state() ==
45effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch             GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS ||
46effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch         service->GetAuthError().state() ==
47effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch             GoogleServiceAuthError::SERVICE_ERROR ||
48effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch         service->GetAuthError().state() ==
49effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch             GoogleServiceAuthError::REQUEST_CANCELED;
50effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
52effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochclass BackendInitializeChecker : public SingleClientStatusChangeChecker {
53effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch public:
54effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  explicit BackendInitializeChecker(ProfileSyncService* service)
55effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      : SingleClientStatusChangeChecker(service) {}
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual bool IsExitConditionSatisfied() OVERRIDE {
586e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    if (service()->backend_mode() != ProfileSyncService::SYNC)
596e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      return false;
60effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    if (service()->sync_initialized())
61effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      return true;
62effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // Backend initialization is blocked by an auth error.
63effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    if (HasAuthError(service()))
64effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      return true;
65effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // Backend initialization is blocked by a failure to fetch Oauth2 tokens.
66effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    if (service()->IsRetryingAccessTokenFetchForTest())
67effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      return true;
68effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // Still waiting on backend initialization.
69effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return false;
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
72a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual std::string GetDebugMessage() const OVERRIDE {
73effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return "Backend Initialize";
74a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
77effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochclass SyncSetupChecker : public SingleClientStatusChangeChecker {
78effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch public:
79effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  explicit SyncSetupChecker(ProfileSyncService* service)
80effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      : SingleClientStatusChangeChecker(service) {}
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
82effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  virtual bool IsExitConditionSatisfied() OVERRIDE {
83effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // Sync setup is complete, and the client is ready to sync new changes.
84effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    if (service()->ShouldPushChanges())
85effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      return true;
86effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // Sync is blocked because a custom passphrase is required.
87effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    if (service()->passphrase_required_reason() == syncer::REASON_DECRYPTION)
88effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      return true;
89effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // Sync is blocked by an auth error.
90effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    if (HasAuthError(service()))
91effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      return true;
92effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // Still waiting on sync setup.
93effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return false;
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
96effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  virtual std::string GetDebugMessage() const OVERRIDE {
97effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return "Sync Setup";
98effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  }
99effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch};
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
101effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochbool AwaitSyncSetupCompletion(ProfileSyncService* service) {
102effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  SyncSetupChecker checker(service);
103e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  checker.Wait();
104effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  return !checker.TimedOut();
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace
1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// static
1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)ProfileSyncServiceHarness* ProfileSyncServiceHarness::Create(
1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    Profile* profile,
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const std::string& username,
1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const std::string& password) {
114effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  return new ProfileSyncServiceHarness(profile, username, password);
1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)ProfileSyncServiceHarness::ProfileSyncServiceHarness(
1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    Profile* profile,
1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const std::string& username,
120effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    const std::string& password)
1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    : profile_(profile),
1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      service_(ProfileSyncServiceFactory::GetForProfile(profile)),
1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      username_(username),
1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      password_(password),
1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      oauth2_refesh_token_number_(0),
126effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      profile_debug_name_(profile->GetDebugName()) {
1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
129a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)ProfileSyncServiceHarness::~ProfileSyncServiceHarness() { }
1305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void ProfileSyncServiceHarness::SetCredentials(const std::string& username,
1325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                               const std::string& password) {
1335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  username_ = username;
1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  password_ = password;
1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool ProfileSyncServiceHarness::SetupSync() {
1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool result = SetupSync(syncer::ModelTypeSet::All());
1395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (result == false) {
1405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    std::string status = GetServiceStatus();
1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    LOG(ERROR) << profile_debug_name_
1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)               << ": SetupSync failed. Syncer status:\n" << status;
1435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  } else {
1445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    DVLOG(1) << profile_debug_name_ << ": SetupSync successful.";
1455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return result;
1475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool ProfileSyncServiceHarness::SetupSync(
1505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    syncer::ModelTypeSet synced_datatypes) {
1515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Initialize the sync client's profile sync service object.
1525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (service() == NULL) {
1535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    LOG(ERROR) << "SetupSync(): service() is null.";
1545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return false;
1555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Tell the sync service that setup is in progress so we don't start syncing
1585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // until we've finished configuration.
1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  service()->SetSetupInProgress(true);
1605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Authenticate sync client using GAIA credentials.
1625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  service()->signin()->SetAuthenticatedUsername(username_);
1631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  service()->GoogleSigninSucceeded(username_, username_, password_);
1645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if defined(ENABLE_MANAGED_USERS)
166f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::string account_id = profile_->IsSupervised() ?
167f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      supervised_users::kSupervisedUserPseudoEmail : username_;
1685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#else
1695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string account_id = username_;
1705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif
1715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK(!account_id.empty());
1725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ProfileOAuth2TokenServiceFactory::GetForProfile(profile_)->
1735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      UpdateCredentials(account_id, GenerateFakeOAuth2RefreshTokenString());
1745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Wait for the OnBackendInitialized() callback.
176effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  BackendInitializeChecker checker(service());
177e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  checker.Wait();
178effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
179effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (checker.TimedOut()) {
180effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    LOG(ERROR) << "OnBackendInitialized() timed out.";
181effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return false;
182effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  }
183effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
184effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!service()->sync_initialized()) {
1855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return false;
1865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Make sure that initial sync wasn't blocked by a missing passphrase.
1895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (service()->passphrase_required_reason() == syncer::REASON_DECRYPTION) {
1905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    LOG(ERROR) << "A passphrase is required for decryption. Sync cannot proceed"
1915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                  " until SetDecryptionPassphrase is called.";
1925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return false;
1935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Make sure that initial sync wasn't blocked by rejected credentials.
196effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (HasAuthError(service())) {
1975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    LOG(ERROR) << "Credentials were rejected. Sync cannot proceed.";
1985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return false;
1995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
2005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Choose the datatypes to be synced. If all datatypes are to be synced,
2025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // set sync_everything to true; otherwise, set it to false.
2035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool sync_everything =
2045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      synced_datatypes.Equals(syncer::ModelTypeSet::All());
2055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  service()->OnUserChoseDatatypes(sync_everything, synced_datatypes);
2065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Notify ProfileSyncService that we are done with configuration.
2085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FinishSyncSetup();
2095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Set an implicit passphrase for encryption if an explicit one hasn't already
2115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // been set. If an explicit passphrase has been set, immediately return false,
2125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // since a decryption passphrase is required.
2135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!service()->IsUsingSecondaryPassphrase()) {
2145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    service()->SetEncryptionPassphrase(password_, ProfileSyncService::IMPLICIT);
2155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  } else {
2165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    LOG(ERROR) << "A passphrase is required for decryption. Sync cannot proceed"
2175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                  " until SetDecryptionPassphrase is called.";
2185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return false;
2195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
2205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Wait for initial sync cycle to be completed.
2225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK(service()->sync_initialized());
223effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!AwaitSyncSetupCompletion(service())) {
224effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    LOG(ERROR) << "Initial sync cycle timed out.";
2255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return false;
2265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
2275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Make sure that initial sync wasn't blocked by a missing passphrase.
2295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (service()->passphrase_required_reason() == syncer::REASON_DECRYPTION) {
2305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    LOG(ERROR) << "A passphrase is required for decryption. Sync cannot proceed"
2315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                  " until SetDecryptionPassphrase is called.";
2325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return false;
2335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
2345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Make sure that initial sync wasn't blocked by rejected credentials.
2365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (service()->GetAuthError().state() ==
2375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS) {
2385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    LOG(ERROR) << "Credentials were rejected. Sync cannot proceed.";
2395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return false;
2405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
2415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return true;
2435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool ProfileSyncServiceHarness::AwaitMutualSyncCycleCompletion(
2465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ProfileSyncServiceHarness* partner) {
247a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::vector<ProfileSyncServiceHarness*> harnesses;
248a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  harnesses.push_back(this);
249a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  harnesses.push_back(partner);
250a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return AwaitQuiescence(harnesses);
2515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool ProfileSyncServiceHarness::AwaitGroupSyncCycleCompletion(
2545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    std::vector<ProfileSyncServiceHarness*>& partners) {
255a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return AwaitQuiescence(partners);
2565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// static
2595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool ProfileSyncServiceHarness::AwaitQuiescence(
2605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    std::vector<ProfileSyncServiceHarness*>& clients) {
261a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::vector<ProfileSyncService*> services;
262a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (clients.empty()) {
263a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return true;
2645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
2655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
266a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  for (std::vector<ProfileSyncServiceHarness*>::iterator it = clients.begin();
267a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)       it != clients.end(); ++it) {
268a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    services.push_back((*it)->service());
2695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
270a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  QuiesceStatusChangeChecker checker(services);
271e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  checker.Wait();
272effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  return !checker.TimedOut();
2735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)std::string ProfileSyncServiceHarness::GenerateFakeOAuth2RefreshTokenString() {
2765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return base::StringPrintf("oauth2_refresh_token_%d",
2775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                            ++oauth2_refesh_token_number_);
2785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool ProfileSyncServiceHarness::IsSyncDisabled() const {
2815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return !service()->setup_in_progress() &&
2825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)         !service()->HasSyncSetupCompleted();
2835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void ProfileSyncServiceHarness::FinishSyncSetup() {
2865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  service()->SetSetupInProgress(false);
2875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  service()->SetSyncSetupCompleted();
2885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)SyncSessionSnapshot ProfileSyncServiceHarness::GetLastSessionSnapshot() const {
2915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK(service() != NULL) << "Sync service has not yet been set up.";
2925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (service()->sync_initialized()) {
2935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return service()->GetLastSessionSnapshot();
2945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
2955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return SyncSessionSnapshot();
2965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool ProfileSyncServiceHarness::EnableSyncForDatatype(
2995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    syncer::ModelType datatype) {
3005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DVLOG(1) << GetClientInfoString(
3015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "EnableSyncForDatatype("
3025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      + std::string(syncer::ModelTypeToString(datatype)) + ")");
3035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (IsSyncDisabled())
3055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return SetupSync(syncer::ModelTypeSet(datatype));
3065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (service() == NULL) {
3085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    LOG(ERROR) << "EnableSyncForDatatype(): service() is null.";
3095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return false;
3105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
3115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  syncer::ModelTypeSet synced_datatypes = service()->GetPreferredDataTypes();
3135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (synced_datatypes.Has(datatype)) {
3145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    DVLOG(1) << "EnableSyncForDatatype(): Sync already enabled for datatype "
3155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)             << syncer::ModelTypeToString(datatype)
3165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)             << " on " << profile_debug_name_ << ".";
3175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return true;
3185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
3195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  synced_datatypes.Put(syncer::ModelTypeFromInt(datatype));
3215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  service()->OnUserChoseDatatypes(false, synced_datatypes);
322effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (AwaitSyncSetupCompletion(service())) {
3235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    DVLOG(1) << "EnableSyncForDatatype(): Enabled sync for datatype "
3245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)             << syncer::ModelTypeToString(datatype)
3255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)             << " on " << profile_debug_name_ << ".";
3265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return true;
3275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
3285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DVLOG(0) << GetClientInfoString("EnableSyncForDatatype failed");
3305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return false;
3315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
3325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool ProfileSyncServiceHarness::DisableSyncForDatatype(
3345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    syncer::ModelType datatype) {
3355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DVLOG(1) << GetClientInfoString(
3365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "DisableSyncForDatatype("
3375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      + std::string(syncer::ModelTypeToString(datatype)) + ")");
3385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (service() == NULL) {
3405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    LOG(ERROR) << "DisableSyncForDatatype(): service() is null.";
3415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return false;
3425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
3435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  syncer::ModelTypeSet synced_datatypes = service()->GetPreferredDataTypes();
3455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!synced_datatypes.Has(datatype)) {
3465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    DVLOG(1) << "DisableSyncForDatatype(): Sync already disabled for datatype "
3475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)             << syncer::ModelTypeToString(datatype)
3485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)             << " on " << profile_debug_name_ << ".";
3495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return true;
3505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
3515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  synced_datatypes.RetainAll(syncer::UserSelectableTypes());
3535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  synced_datatypes.Remove(datatype);
3545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  service()->OnUserChoseDatatypes(false, synced_datatypes);
355effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (AwaitSyncSetupCompletion(service())) {
3565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    DVLOG(1) << "DisableSyncForDatatype(): Disabled sync for datatype "
3575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)             << syncer::ModelTypeToString(datatype)
3585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)             << " on " << profile_debug_name_ << ".";
3595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return true;
3605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
3615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DVLOG(0) << GetClientInfoString("DisableSyncForDatatype failed");
3635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return false;
3645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
3655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool ProfileSyncServiceHarness::EnableSyncForAllDatatypes() {
3675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DVLOG(1) << GetClientInfoString("EnableSyncForAllDatatypes");
3685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (IsSyncDisabled())
3705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return SetupSync();
3715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (service() == NULL) {
3735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    LOG(ERROR) << "EnableSyncForAllDatatypes(): service() is null.";
3745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return false;
3755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
3765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  service()->OnUserChoseDatatypes(true, syncer::ModelTypeSet::All());
378effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (AwaitSyncSetupCompletion(service())) {
3795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    DVLOG(1) << "EnableSyncForAllDatatypes(): Enabled sync for all datatypes "
3805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)             << "on " << profile_debug_name_ << ".";
3815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return true;
3825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
3835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DVLOG(0) << GetClientInfoString("EnableSyncForAllDatatypes failed");
3855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return false;
3865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
3875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool ProfileSyncServiceHarness::DisableSyncForAllDatatypes() {
3895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DVLOG(1) << GetClientInfoString("DisableSyncForAllDatatypes");
3905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (service() == NULL) {
3925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    LOG(ERROR) << "DisableSyncForAllDatatypes(): service() is null.";
3935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return false;
3945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
3955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  service()->DisableForUser();
3975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DVLOG(1) << "DisableSyncForAllDatatypes(): Disabled sync for all "
3995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)           << "datatypes on " << profile_debug_name_;
4005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return true;
4015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
4025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// TODO(sync): Clean up this method in a separate CL. Remove all snapshot fields
4045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// and log shorter, more meaningful messages.
4055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)std::string ProfileSyncServiceHarness::GetClientInfoString(
4065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const std::string& message) const {
4075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::stringstream os;
4085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  os << profile_debug_name_ << ": " << message << ": ";
4095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (service()) {
4105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const SyncSessionSnapshot& snap = GetLastSessionSnapshot();
411effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    ProfileSyncService::Status status;
412effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    service()->QueryDetailedSyncStatus(&status);
4135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Capture select info from the sync session snapshot and syncer status.
4145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    os << ", has_unsynced_items: "
4155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)       << (service()->sync_initialized() ? service()->HasUnsyncedItems() : 0)
4165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)       << ", did_commit: "
4175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)       << (snap.model_neutral_state().num_successful_commits == 0 &&
4185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)           snap.model_neutral_state().commit_result == syncer::SYNCER_OK)
4195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)       << ", encryption conflicts: "
4205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)       << snap.num_encryption_conflicts()
4215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)       << ", hierarchy conflicts: "
4225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)       << snap.num_hierarchy_conflicts()
4235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)       << ", server conflicts: "
4245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)       << snap.num_server_conflicts()
4255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)       << ", num_updates_downloaded : "
4265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)       << snap.model_neutral_state().num_updates_downloaded_total
4275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)       << ", passphrase_required_reason: "
4285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)       << syncer::PassphraseRequiredReasonToString(
4295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)           service()->passphrase_required_reason())
4305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)       << ", notifications_enabled: "
4315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)       << status.notifications_enabled
4325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)       << ", service_is_pushing_changes: "
433effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch       << service()->ShouldPushChanges();
4345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  } else {
4355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    os << "Sync service not available";
4365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
4375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return os.str();
4385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
4395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool ProfileSyncServiceHarness::IsTypePreferred(syncer::ModelType type) {
4415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return service()->GetPreferredDataTypes().Has(type);
4425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
4435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)std::string ProfileSyncServiceHarness::GetServiceStatus() {
4455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_ptr<base::DictionaryValue> value(
4465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      sync_ui_util::ConstructAboutInformation(service()));
4475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string service_status;
4485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::JSONWriter::WriteWithOptions(value.get(),
4495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                     base::JSONWriter::OPTIONS_PRETTY_PRINT,
4505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                     &service_status);
4515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return service_status;
4525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
453