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/common/chrome_switches.h"
13 #include "components/sync_driver/signin_manager_wrapper.h"
14 #include "components/sync_driver/sync_driver_switches.h"
15 #include "components/sync_driver/sync_prefs.h"
17 namespace browser_sync
{
19 #if defined(ENABLE_PRE_SYNC_BACKUP)
20 // Number of rollback attempts to try before giving up.
21 static const int kRollbackLimits
= 3;
23 // Finch experiment name and group.
24 static char kSyncBackupFinchName
[] = "SyncBackup";
25 static char kSyncBackupFinchDisabled
[] = "disabled";
28 BackupRollbackController::BackupRollbackController(
29 sync_driver::SyncPrefs
* sync_prefs
,
30 const SigninManagerWrapper
* signin
,
31 base::Closure start_backup
,
32 base::Closure start_rollback
)
33 : sync_prefs_(sync_prefs
),
35 start_backup_(start_backup
),
36 start_rollback_(start_rollback
) {
39 BackupRollbackController::~BackupRollbackController() {
42 bool BackupRollbackController::StartBackup() {
43 if (!IsBackupEnabled())
46 // Disable rollback to previous backup DB because it will be overwritten by
48 sync_prefs_
->SetRemainingRollbackTries(0);
49 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE
, start_backup_
);
53 bool BackupRollbackController::StartRollback() {
54 if (!IsBackupEnabled())
57 // Don't roll back if disabled or user is signed in.
58 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
59 switches::kSyncDisableRollback
) ||
60 !signin_
->GetEffectiveUsername().empty()) {
61 sync_prefs_
->SetRemainingRollbackTries(0);
65 int rollback_tries
= sync_prefs_
->GetRemainingRollbackTries();
66 if (rollback_tries
<= 0)
67 return false; // No pending rollback.
69 sync_prefs_
->SetRemainingRollbackTries(rollback_tries
- 1);
70 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE
, start_rollback_
);
74 void BackupRollbackController::OnRollbackReceived() {
75 #if defined(ENABLE_PRE_SYNC_BACKUP)
76 sync_prefs_
->SetRemainingRollbackTries(kRollbackLimits
);
80 void BackupRollbackController::OnRollbackDone() {
81 #if defined(ENABLE_PRE_SYNC_BACKUP)
82 sync_prefs_
->SetRemainingRollbackTries(0);
87 bool BackupRollbackController::IsBackupEnabled() {
88 #if defined(ENABLE_PRE_SYNC_BACKUP)
89 const std::string group_name
=
90 base::FieldTrialList::FindFullName(kSyncBackupFinchName
);
92 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
93 switches::kSyncDisableBackup
) ||
94 group_name
== kSyncBackupFinchDisabled
) {
103 } // namespace browser_sync