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 #ifndef CHROME_BROWSER_SYNC_STARTUP_CONTROLLER_H_
6 #define CHROME_BROWSER_SYNC_STARTUP_CONTROLLER_H_
8 #include "base/callback.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/time/time.h"
11 #include "sync/internal_api/public/base/model_type.h"
13 class ProfileOAuth2TokenService
;
14 class SigninManagerWrapper
;
16 namespace sync_driver
{
20 namespace browser_sync
{
22 // Defines the type of behavior the sync engine should use. If configured for
23 // AUTO_START, the sync engine will automatically call SetSyncSetupCompleted()
24 // and start downloading data types as soon as sync credentials are available.
25 // If configured for MANUAL_START, sync will not start until the user
26 // completes sync setup, at which point the UI makes an explicit call to
27 // complete sync setup.
28 enum ProfileSyncServiceStartBehavior
{
33 // This class is used by ProfileSyncService to manage all logic and state
34 // pertaining to initialization of the SyncBackendHost (colloquially referred
35 // to as "the backend").
36 class StartupController
{
38 StartupController(ProfileSyncServiceStartBehavior start_behavior
,
39 const ProfileOAuth2TokenService
* token_service
,
40 const sync_driver::SyncPrefs
* sync_prefs
,
41 const SigninManagerWrapper
* signin
,
42 base::Closure start_backend
);
45 // Starts up sync if it is requested by the user and preconditions are met.
46 // Returns true if these preconditions are met, although does not imply
47 // the backend was started.
50 // Called when a datatype (SyncableService) has a need for sync to start
51 // ASAP, presumably because a local change event has occurred but we're
52 // still in deferred start mode, meaning the SyncableService hasn't been
53 // told to MergeDataAndStartSyncing yet.
54 // It is expected that |type| is a currently active datatype.
55 void OnDataTypeRequestsSyncStartup(syncer::ModelType type
);
57 // Prepares this object for a new attempt to start sync, forgetting
58 // whether or not preconditions were previously met.
59 // NOTE: This resets internal state managed by this class, but does not
60 // touch values that are explicitly set and reset by higher layers to
61 // tell this class whether a setup UI dialog is being shown to the user.
62 // See setup_in_progress_.
63 void Reset(const syncer::ModelTypeSet registered_types
);
65 void set_setup_in_progress(bool in_progress
);
66 bool setup_in_progress() const { return setup_in_progress_
; }
67 bool auto_start_enabled() const { return auto_start_enabled_
; }
68 base::Time
start_backend_time() const { return start_backend_time_
; }
69 std::string
GetBackendInitializationStateString() const;
71 void OverrideFallbackTimeoutForTest(const base::TimeDelta
& timeout
);
73 enum StartUpDeferredOption
{
74 STARTUP_BACKEND_DEFERRED
,
77 // Returns true if all conditions to start the backend are met.
78 bool StartUp(StartUpDeferredOption deferred_option
);
79 void OnFallbackStartupTimerExpired();
81 // Records time spent in deferred state with UMA histograms.
82 void RecordTimeDeferred();
84 // True if we should start sync ASAP because either a SyncableService has
85 // requested it, or we're done waiting for a sign and decided to go ahead.
86 bool received_start_request_
;
88 // The time that StartUp() is called. This is used to calculate time spent
89 // in the deferred state; that is, after StartUp and before invoking the
90 // start_backend_ callback.
91 base::Time start_up_time_
;
93 // If |true|, there is setup UI visible so we should not start downloading
95 // Note: this is explicitly controlled by higher layers (UI) and is meant to
96 // reflect what the UI claims the setup state to be. Therefore, only set this
97 // due to explicit requests to do so via set_setup_in_progress.
98 bool setup_in_progress_
;
100 // If true, we want to automatically start sync signin whenever we have
101 // credentials (user doesn't need to go through the startup flow). This is
102 // typically enabled on platforms (like ChromeOS) that have their own
103 // distinct signin flow.
104 const bool auto_start_enabled_
;
106 const sync_driver::SyncPrefs
* sync_prefs_
;
108 const ProfileOAuth2TokenService
* token_service_
;
110 const SigninManagerWrapper
* signin_
;
112 // The callback we invoke when it's time to call expensive
113 // startup routines for the sync backend.
114 base::Closure start_backend_
;
116 // The time at which we invoked the start_backend_ callback.
117 base::Time start_backend_time_
;
119 base::TimeDelta fallback_timeout_
;
121 // Used to compute preferred_types from SyncPrefs as-needed.
122 syncer::ModelTypeSet registered_types_
;
124 // True before calling |start_backend_| for the first time. False after that.
127 base::WeakPtrFactory
<StartupController
> weak_factory_
;
130 } // namespace browser_sync
132 #endif // CHROME_BROWSER_SYNC_STARTUP_CONTROLLER_H_