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/message_loop/message_loop.h"
9 #include "base/metrics/field_trial.h"
10 #include "chrome/browser/sync/supervised_user_signin_manager_wrapper.h"
11 #include "chrome/common/chrome_switches.h"
12 #include "components/sync_driver/sync_prefs.h"
14 namespace browser_sync
{
16 #if defined(ENABLE_PRE_SYNC_BACKUP)
17 // Number of rollback attempts to try before giving up.
18 static const int kRollbackLimits
= 3;
20 // Finch experiment name and group.
21 static char kSyncBackupFinchName
[] = "SyncBackup";
22 static char kSyncBackupFinchDisabled
[] = "disabled";
25 BackupRollbackController::BackupRollbackController(
26 sync_driver::SyncPrefs
* sync_prefs
,
27 const SupervisedUserSigninManagerWrapper
* signin
,
28 base::Closure start_backup
,
29 base::Closure start_rollback
)
30 : sync_prefs_(sync_prefs
),
32 start_backup_(start_backup
),
33 start_rollback_(start_rollback
) {
36 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::MessageLoop::current()->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::MessageLoop::current()->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 browser_sync