Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / sync / backup_rollback_controller.cc
blobfdef5cb566ba16e336d281ccbea6bd3ba3a9309d
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";
23 #endif
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),
31 signin_(signin),
32 start_backup_(start_backup),
33 start_rollback_(start_rollback) {
36 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::MessageLoop::current()->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::MessageLoop::current()->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 browser_sync