Linux: Depend on liberation-fonts package for RPMs.
[chromium-blink-merge.git] / components / sync_driver / backup_rollback_controller.cc
blob685cf6fe5327cb15474dfcc634cc7e7d6d37e61f
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";
25 #endif
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),
33 signin_(signin),
34 start_backup_(start_backup),
35 start_rollback_(start_rollback) {}
37 BackupRollbackController::~BackupRollbackController() {}
39 bool BackupRollbackController::StartBackup() {
40 if (!IsBackupEnabled())
41 return false;
43 // Disable rollback to previous backup DB because it will be overwritten by
44 // new backup.
45 sync_prefs_->SetRemainingRollbackTries(0);
46 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, start_backup_);
47 return true;
50 bool BackupRollbackController::StartRollback() {
51 if (!IsBackupEnabled())
52 return false;
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);
59 return false;
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_);
68 return true;
71 void BackupRollbackController::OnRollbackReceived() {
72 #if defined(ENABLE_PRE_SYNC_BACKUP)
73 sync_prefs_->SetRemainingRollbackTries(kRollbackLimits);
74 #endif
77 void BackupRollbackController::OnRollbackDone() {
78 #if defined(ENABLE_PRE_SYNC_BACKUP)
79 sync_prefs_->SetRemainingRollbackTries(0);
80 #endif
83 // static
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) {
92 return false;
94 return true;
95 #else
96 return false;
97 #endif
100 } // namespace sync_driver