Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / browser / sync / backup_rollback_controller.cc
bloba80d703da669e7f1be0ee237e25518e63e56f700
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";
26 #endif
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),
34 signin_(signin),
35 start_backup_(start_backup),
36 start_rollback_(start_rollback) {
39 BackupRollbackController::~BackupRollbackController() {
42 bool BackupRollbackController::StartBackup() {
43 if (!IsBackupEnabled())
44 return false;
46 // Disable rollback to previous backup DB because it will be overwritten by
47 // new backup.
48 sync_prefs_->SetRemainingRollbackTries(0);
49 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, start_backup_);
50 return true;
53 bool BackupRollbackController::StartRollback() {
54 if (!IsBackupEnabled())
55 return false;
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);
62 return false;
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_);
71 return true;
74 void BackupRollbackController::OnRollbackReceived() {
75 #if defined(ENABLE_PRE_SYNC_BACKUP)
76 sync_prefs_->SetRemainingRollbackTries(kRollbackLimits);
77 #endif
80 void BackupRollbackController::OnRollbackDone() {
81 #if defined(ENABLE_PRE_SYNC_BACKUP)
82 sync_prefs_->SetRemainingRollbackTries(0);
83 #endif
86 // static
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) {
95 return false;
97 return true;
98 #else
99 return false;
100 #endif
103 } // namespace browser_sync