• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef CHROME_BROWSER_SYNC_STARTUP_CONTROLLER_H_
6 #define CHROME_BROWSER_SYNC_STARTUP_CONTROLLER_H_
7 
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"
12 
13 class ProfileOAuth2TokenService;
14 class SupervisedUserSigninManagerWrapper;
15 
16 namespace sync_driver {
17 class SyncPrefs;
18 }
19 
20 namespace browser_sync {
21 
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 {
29   AUTO_START,
30   MANUAL_START,
31 };
32 
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 {
37  public:
38   StartupController(ProfileSyncServiceStartBehavior start_behavior,
39                     const ProfileOAuth2TokenService* token_service,
40                     const sync_driver::SyncPrefs* sync_prefs,
41                     const SupervisedUserSigninManagerWrapper* signin,
42                     base::Closure start_backend);
43   ~StartupController();
44 
45   // Starts up sync if it is not suppressed and preconditions are met.
46   // Returns true if these preconditions are met, although does not imply
47   // the backend was started.
48   bool TryStart();
49 
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);
56 
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);
64 
65   void set_setup_in_progress(bool in_progress);
setup_in_progress()66   bool setup_in_progress() const { return setup_in_progress_; }
auto_start_enabled()67   bool auto_start_enabled() const { return auto_start_enabled_; }
start_backend_time()68   base::Time start_backend_time() const { return start_backend_time_; }
69   std::string GetBackendInitializationStateString() const;
70 
71   void OverrideFallbackTimeoutForTest(const base::TimeDelta& timeout);
72  private:
73   enum StartUpDeferredOption {
74     STARTUP_BACKEND_DEFERRED,
75     STARTUP_IMMEDIATE
76   };
77   // Returns true if all conditions to start the backend are met.
78   bool StartUp(StartUpDeferredOption deferred_option);
79   void OnFallbackStartupTimerExpired();
80 
81   // Records time spent in deferred state with UMA histograms.
82   void RecordTimeDeferred();
83 
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_;
87 
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_;
92 
93   // If |true|, there is setup UI visible so we should not start downloading
94   // data types.
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_;
99 
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_;
105 
106   const sync_driver::SyncPrefs* sync_prefs_;
107 
108   const ProfileOAuth2TokenService* token_service_;
109 
110   const SupervisedUserSigninManagerWrapper* signin_;
111 
112   // The callback we invoke when it's time to call expensive
113   // startup routines for the sync backend.
114   base::Closure start_backend_;
115 
116   // The time at which we invoked the start_backend_ callback.
117   base::Time start_backend_time_;
118 
119   base::TimeDelta fallback_timeout_;
120 
121   // Used to compute preferred_types from SyncPrefs as-needed.
122   syncer::ModelTypeSet registered_types_;
123 
124   // True before calling |start_backend_| for the first time. False after that.
125   bool first_start_;
126 
127   base::WeakPtrFactory<StartupController> weak_factory_;
128 };
129 
130 }  // namespace browser_sync
131 
132 #endif  // CHROME_BROWSER_SYNC_STARTUP_CONTROLLER_H_
133