Changes for start_with_ext Telemetry test to work on Windows.
[chromium-blink-merge.git] / chrome / browser / sync / backup_rollback_controller.cc
blob1c8dae7025a39943a02f325ebb6e8ce03d0304c3
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.
5 #include "chrome/browser/sync/backup_rollback_controller.h"
7 #include "base/command_line.h"
8 #include "base/location.h"
9 #include "base/metrics/field_trial.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "chrome/browser/sync/supervised_user_signin_manager_wrapper.h"
13 #include "chrome/common/chrome_switches.h"
14 #include "components/sync_driver/sync_prefs.h"
16 namespace browser_sync {
18 #if defined(ENABLE_PRE_SYNC_BACKUP)
19 // Number of rollback attempts to try before giving up.
20 static const int kRollbackLimits = 3;
22 // Finch experiment name and group.
23 static char kSyncBackupFinchName[] = "SyncBackup";
24 static char kSyncBackupFinchDisabled[] = "disabled";
25 #endif
27 BackupRollbackController::BackupRollbackController(
28 sync_driver::SyncPrefs* sync_prefs,
29 const SupervisedUserSigninManagerWrapper* signin,
30 base::Closure start_backup,
31 base::Closure start_rollback)
32 : sync_prefs_(sync_prefs),
33 signin_(signin),
34 start_backup_(start_backup),
35 start_rollback_(start_rollback) {
38 BackupRollbackController::~BackupRollbackController() {
41 bool BackupRollbackController::StartBackup() {
42 if (!IsBackupEnabled())
43 return false;
45 // Disable rollback to previous backup DB because it will be overwritten by
46 // new backup.
47 sync_prefs_->SetRemainingRollbackTries(0);
48 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, start_backup_);
49 return true;
52 bool BackupRollbackController::StartRollback() {
53 if (!IsBackupEnabled())
54 return false;
56 // Don't roll back if disabled or user is signed in.
57 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
58 switches::kSyncDisableRollback) ||
59 !signin_->GetEffectiveUsername().empty()) {
60 sync_prefs_->SetRemainingRollbackTries(0);
61 return false;
64 int rollback_tries = sync_prefs_->GetRemainingRollbackTries();
65 if (rollback_tries <= 0)
66 return false; // No pending rollback.
68 sync_prefs_->SetRemainingRollbackTries(rollback_tries - 1);
69 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, start_rollback_);
70 return true;
73 void BackupRollbackController::OnRollbackReceived() {
74 #if defined(ENABLE_PRE_SYNC_BACKUP)
75 sync_prefs_->SetRemainingRollbackTries(kRollbackLimits);
76 #endif
79 void BackupRollbackController::OnRollbackDone() {
80 #if defined(ENABLE_PRE_SYNC_BACKUP)
81 sync_prefs_->SetRemainingRollbackTries(0);
82 #endif
85 // static
86 bool BackupRollbackController::IsBackupEnabled() {
87 #if defined(ENABLE_PRE_SYNC_BACKUP)
88 const std::string group_name =
89 base::FieldTrialList::FindFullName(kSyncBackupFinchName);
91 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
92 switches::kSyncDisableBackup) ||
93 group_name == kSyncBackupFinchDisabled) {
94 return false;
96 return true;
97 #else
98 return false;
99 #endif
102 } // namespace browser_sync