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 "components/sync_driver/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 "components/sync_driver/signin_manager_wrapper.h"
13 #include "components/sync_driver/sync_driver_switches.h"
14 #include "components/sync_driver/sync_prefs.h"
16 namespace sync_driver
{
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";
27 BackupRollbackController::BackupRollbackController(
28 sync_driver::SyncPrefs
* sync_prefs
,
29 const SigninManagerWrapper
* signin
,
30 base::Closure start_backup
,
31 base::Closure start_rollback
)
32 : sync_prefs_(sync_prefs
),
34 start_backup_(start_backup
),
35 start_rollback_(start_rollback
) {}
37 BackupRollbackController::~BackupRollbackController() {}
39 bool BackupRollbackController::StartBackup() {
40 if (!IsBackupEnabled())
43 // Disable rollback to previous backup DB because it will be overwritten by
45 sync_prefs_
->SetRemainingRollbackTries(0);
46 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE
, start_backup_
);
50 bool BackupRollbackController::StartRollback() {
51 if (!IsBackupEnabled())
54 // Don't roll back if disabled or user is signed in.
55 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
56 switches::kSyncDisableRollback
) ||
57 !signin_
->GetEffectiveUsername().empty()) {
58 sync_prefs_
->SetRemainingRollbackTries(0);
62 int rollback_tries
= sync_prefs_
->GetRemainingRollbackTries();
63 if (rollback_tries
<= 0)
64 return false; // No pending rollback.
66 sync_prefs_
->SetRemainingRollbackTries(rollback_tries
- 1);
67 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE
, start_rollback_
);
71 void BackupRollbackController::OnRollbackReceived() {
72 #if defined(ENABLE_PRE_SYNC_BACKUP)
73 sync_prefs_
->SetRemainingRollbackTries(kRollbackLimits
);
77 void BackupRollbackController::OnRollbackDone() {
78 #if defined(ENABLE_PRE_SYNC_BACKUP)
79 sync_prefs_
->SetRemainingRollbackTries(0);
84 bool BackupRollbackController::IsBackupEnabled() {
85 #if defined(ENABLE_PRE_SYNC_BACKUP)
86 const std::string group_name
=
87 base::FieldTrialList::FindFullName(kSyncBackupFinchName
);
89 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
90 switches::kSyncDisableBackup
) ||
91 group_name
== kSyncBackupFinchDisabled
) {
100 } // namespace sync_driver