Commit dce7e988 authored by Marc Treib's avatar Marc Treib Committed by Commit Bot

Sync: Pass GetPreferredDataTypes as a callback into StartupController

This makes a few things a bit nicer: Now StartupController doesn't have
to know about the set of registered data types anymore, which are really
not its business. As a consequence, it's not necessary to call Reset()
before using the StartupController.
This also removes one of two uses of SyncPrefs from StartupController;
I hope to remove the remaining one soon.

Bug: 854978
Change-Id: I75c8971342554eb190287724eff2ff3238ffbff6
Reviewed-on: https://chromium-review.googlesource.com/1110220
Commit-Queue: Marc Treib <treib@chromium.org>
Reviewed-by: default avatarJan Krcal <jkrcal@chromium.org>
Cr-Commit-Position: refs/heads/master@{#569337}
parent c59c0f9f
...@@ -232,6 +232,8 @@ void ProfileSyncService::Initialize() { ...@@ -232,6 +232,8 @@ void ProfileSyncService::Initialize() {
startup_controller_ = std::make_unique<syncer::StartupController>( startup_controller_ = std::make_unique<syncer::StartupController>(
&sync_prefs_, &sync_prefs_,
base::BindRepeating(&ProfileSyncService::GetPreferredDataTypes,
base::Unretained(this)),
base::BindRepeating(&ProfileSyncService::CanSyncStart, base::BindRepeating(&ProfileSyncService::CanSyncStart,
base::Unretained(this)), base::Unretained(this)),
base::BindRepeating(&ProfileSyncService::StartUpSlowEngineComponents, base::BindRepeating(&ProfileSyncService::StartUpSlowEngineComponents,
...@@ -338,7 +340,6 @@ void ProfileSyncService::Initialize() { ...@@ -338,7 +340,6 @@ void ProfileSyncService::Initialize() {
memory_pressure_listener_ = std::make_unique<base::MemoryPressureListener>( memory_pressure_listener_ = std::make_unique<base::MemoryPressureListener>(
base::BindRepeating(&ProfileSyncService::OnMemoryPressure, base::BindRepeating(&ProfileSyncService::OnMemoryPressure,
sync_enabled_weak_factory_.GetWeakPtr())); sync_enabled_weak_factory_.GetWeakPtr()));
startup_controller_->Reset(GetRegisteredDataTypes());
// Auto-start means the first time the profile starts up, sync should start up // Auto-start means the first time the profile starts up, sync should start up
// immediately. // immediately.
...@@ -693,7 +694,7 @@ void ProfileSyncService::ShutdownImpl(syncer::ShutdownReason reason) { ...@@ -693,7 +694,7 @@ void ProfileSyncService::ShutdownImpl(syncer::ShutdownReason reason) {
sync_enabled_weak_factory_.InvalidateWeakPtrs(); sync_enabled_weak_factory_.InvalidateWeakPtrs();
startup_controller_->Reset(GetRegisteredDataTypes()); startup_controller_->Reset();
// If the sync DB is getting destroyed, the local DeviceInfo is no longer // If the sync DB is getting destroyed, the local DeviceInfo is no longer
// valid and should be cleared from the cache. // valid and should be cleared from the cache.
......
...@@ -57,12 +57,15 @@ enum DeferredInitTrigger { ...@@ -57,12 +57,15 @@ enum DeferredInitTrigger {
} // namespace } // namespace
StartupController::StartupController(const SyncPrefs* sync_prefs, StartupController::StartupController(
base::RepeatingCallback<bool()> can_start, const SyncPrefs* sync_prefs,
base::RepeatingClosure start_engine) base::RepeatingCallback<ModelTypeSet()> get_preferred_data_types,
base::RepeatingCallback<bool()> can_start,
base::RepeatingClosure start_engine)
: sync_prefs_(sync_prefs), : sync_prefs_(sync_prefs),
can_start_(std::move(can_start)), get_preferred_data_types_callback_(std::move(get_preferred_data_types)),
start_engine_(std::move(start_engine)), can_start_callback_(std::move(can_start)),
start_engine_callback_(std::move(start_engine)),
bypass_setup_complete_(false), bypass_setup_complete_(false),
received_start_request_(false), received_start_request_(false),
setup_in_progress_(false), setup_in_progress_(false),
...@@ -70,14 +73,13 @@ StartupController::StartupController(const SyncPrefs* sync_prefs, ...@@ -70,14 +73,13 @@ StartupController::StartupController(const SyncPrefs* sync_prefs,
StartupController::~StartupController() {} StartupController::~StartupController() {}
void StartupController::Reset(const ModelTypeSet& registered_types) { void StartupController::Reset() {
received_start_request_ = false; received_start_request_ = false;
bypass_setup_complete_ = false; bypass_setup_complete_ = false;
start_up_time_ = base::Time(); start_up_time_ = base::Time();
start_engine_time_ = base::Time(); start_engine_time_ = base::Time();
// Don't let previous timers affect us post-reset. // Don't let previous timers affect us post-reset.
weak_factory_.InvalidateWeakPtrs(); weak_factory_.InvalidateWeakPtrs();
registered_types_ = registered_types;
} }
void StartupController::SetSetupInProgress(bool setup_in_progress) { void StartupController::SetSetupInProgress(bool setup_in_progress) {
...@@ -94,7 +96,7 @@ void StartupController::StartUp(StartUpDeferredOption deferred_option) { ...@@ -94,7 +96,7 @@ void StartupController::StartUp(StartUpDeferredOption deferred_option) {
} }
if (deferred_option == STARTUP_DEFERRED && IsDeferredStartupEnabled() && if (deferred_option == STARTUP_DEFERRED && IsDeferredStartupEnabled() &&
sync_prefs_->GetPreferredDataTypes(registered_types_).Has(SESSIONS)) { get_preferred_data_types_callback_.Run().Has(SESSIONS)) {
if (first_start) { if (first_start) {
base::SequencedTaskRunnerHandle::Get()->PostDelayedTask( base::SequencedTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE, FROM_HERE,
...@@ -107,12 +109,12 @@ void StartupController::StartUp(StartUpDeferredOption deferred_option) { ...@@ -107,12 +109,12 @@ void StartupController::StartUp(StartUpDeferredOption deferred_option) {
if (start_engine_time_.is_null()) { if (start_engine_time_.is_null()) {
start_engine_time_ = base::Time::Now(); start_engine_time_ = base::Time::Now();
start_engine_.Run(); start_engine_callback_.Run();
} }
} }
void StartupController::TryStart() { void StartupController::TryStart() {
if (!can_start_.Run()) { if (!can_start_callback_.Run()) {
return; return;
} }
......
...@@ -29,9 +29,11 @@ class StartupController { ...@@ -29,9 +29,11 @@ class StartupController {
STARTED STARTED
}; };
StartupController(const SyncPrefs* sync_prefs, StartupController(
base::RepeatingCallback<bool()> can_start, const SyncPrefs* sync_prefs,
base::RepeatingClosure start_engine); base::RepeatingCallback<ModelTypeSet()> get_preferred_data_types,
base::RepeatingCallback<bool()> can_start,
base::RepeatingClosure start_engine);
~StartupController(); ~StartupController();
// Starts up sync if it is requested by the user and preconditions are met. // Starts up sync if it is requested by the user and preconditions are met.
...@@ -54,7 +56,7 @@ class StartupController { ...@@ -54,7 +56,7 @@ class StartupController {
// touch values that are explicitly set and reset by higher layers to // touch values that are explicitly set and reset by higher layers to
// tell this class whether a setup UI dialog is being shown to the user. // tell this class whether a setup UI dialog is being shown to the user.
// See setup_in_progress_. // See setup_in_progress_.
void Reset(const ModelTypeSet& registered_types); void Reset();
// Sets the setup in progress flag and tries to start sync if it's true. // Sets the setup in progress flag and tries to start sync if it's true.
void SetSetupInProgress(bool setup_in_progress); void SetSetupInProgress(bool setup_in_progress);
...@@ -75,16 +77,16 @@ class StartupController { ...@@ -75,16 +77,16 @@ class StartupController {
const SyncPrefs* sync_prefs_; const SyncPrefs* sync_prefs_;
const base::RepeatingCallback<ModelTypeSet()>
get_preferred_data_types_callback_;
// A function that can be invoked repeatedly to determine whether sync can be // A function that can be invoked repeatedly to determine whether sync can be
// started. |start_engine_| should not be invoked unless this returns true. // started. |start_engine_| should not be invoked unless this returns true.
const base::RepeatingCallback<bool()> can_start_; const base::RepeatingCallback<bool()> can_start_callback_;
// The callback we invoke when it's time to call expensive // The callback we invoke when it's time to call expensive
// startup routines for the sync engine. // startup routines for the sync engine.
const base::RepeatingClosure start_engine_; const base::RepeatingClosure start_engine_callback_;
// Used to compute preferred_types from SyncPrefs as-needed.
ModelTypeSet registered_types_;
// If true, will bypass the FirstSetupComplete check when triggering sync // If true, will bypass the FirstSetupComplete check when triggering sync
// startup. Set in TryStartImmediately. // startup. Set in TryStartImmediately.
......
...@@ -18,7 +18,8 @@ namespace syncer { ...@@ -18,7 +18,8 @@ namespace syncer {
class StartupControllerTest : public testing::Test { class StartupControllerTest : public testing::Test {
public: public:
StartupControllerTest() : can_start_(false), started_(false) {} StartupControllerTest()
: preferred_types_(UserTypes()), can_start_(false), started_(false) {}
void SetUp() override { void SetUp() override {
base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
...@@ -28,10 +29,17 @@ class StartupControllerTest : public testing::Test { ...@@ -28,10 +29,17 @@ class StartupControllerTest : public testing::Test {
sync_prefs_ = std::make_unique<SyncPrefs>(&pref_service_); sync_prefs_ = std::make_unique<SyncPrefs>(&pref_service_);
controller_ = std::make_unique<StartupController>( controller_ = std::make_unique<StartupController>(
sync_prefs_.get(), sync_prefs_.get(),
base::Bind(&StartupControllerTest::CanStart, base::Unretained(this)), base::BindRepeating(&StartupControllerTest::GetPreferredDataTypes,
base::Bind(&StartupControllerTest::FakeStartBackend, base::Unretained(this)),
base::Unretained(this))); base::BindRepeating(&StartupControllerTest::CanStart,
controller_->Reset(UserTypes()); base::Unretained(this)),
base::BindRepeating(&StartupControllerTest::FakeStartBackend,
base::Unretained(this)));
controller_->Reset();
}
void SetPreferredDataTypes(const ModelTypeSet& types) {
preferred_types_ = types;
} }
void SetCanStart(bool can_start) { can_start_ = can_start; } void SetCanStart(bool can_start) { can_start_ = can_start; }
...@@ -58,9 +66,11 @@ class StartupControllerTest : public testing::Test { ...@@ -58,9 +66,11 @@ class StartupControllerTest : public testing::Test {
SyncPrefs* sync_prefs() { return sync_prefs_.get(); } SyncPrefs* sync_prefs() { return sync_prefs_.get(); }
private: private:
ModelTypeSet GetPreferredDataTypes() { return preferred_types_; }
bool CanStart() { return can_start_; } bool CanStart() { return can_start_; }
void FakeStartBackend() { started_ = true; } void FakeStartBackend() { started_ = true; }
ModelTypeSet preferred_types_;
bool can_start_; bool can_start_;
bool started_; bool started_;
base::MessageLoop message_loop_; base::MessageLoop message_loop_;
...@@ -133,9 +143,7 @@ TEST_F(StartupControllerTest, NoDeferralWithoutSessionsSync) { ...@@ -133,9 +143,7 @@ TEST_F(StartupControllerTest, NoDeferralWithoutSessionsSync) {
types.Remove(PROXY_TABS); types.Remove(PROXY_TABS);
types.Remove(TYPED_URLS); types.Remove(TYPED_URLS);
types.Remove(SUPERVISED_USER_SETTINGS); types.Remove(SUPERVISED_USER_SETTINGS);
sync_prefs()->SetKeepEverythingSynced(false); SetPreferredDataTypes(types);
sync_prefs()->SetPreferredDataTypes(UserTypes(), types);
controller()->Reset(UserTypes());
sync_prefs()->SetFirstSetupComplete(); sync_prefs()->SetFirstSetupComplete();
SetCanStart(true); SetCanStart(true);
...@@ -187,7 +195,7 @@ TEST_F(StartupControllerTest, ResetDuringSetup) { ...@@ -187,7 +195,7 @@ TEST_F(StartupControllerTest, ResetDuringSetup) {
controller()->SetSetupInProgress(true); controller()->SetSetupInProgress(true);
// This could happen if the UI triggers a stop-syncing permanently call. // This could happen if the UI triggers a stop-syncing permanently call.
controller()->Reset(UserTypes()); controller()->Reset();
// From the UI's point of view, setup is still in progress. // From the UI's point of view, setup is still in progress.
EXPECT_TRUE(controller()->IsSetupInProgress()); EXPECT_TRUE(controller()->IsSetupInProgress());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment