| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_SYNC_DRIVER_STARTUP_CONTROLLER_H_ | 5 #ifndef COMPONENTS_SYNC_DRIVER_STARTUP_CONTROLLER_H_ |
| 6 #define COMPONENTS_SYNC_DRIVER_STARTUP_CONTROLLER_H_ | 6 #define COMPONENTS_SYNC_DRIVER_STARTUP_CONTROLLER_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
| 12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "components/sync/base/model_type.h" | 13 #include "components/sync/base/model_type.h" |
| 14 | 14 |
| 15 namespace syncer { | 15 namespace syncer { |
| 16 | 16 |
| 17 class SyncPrefs; | 17 class SyncPrefs; |
| 18 | 18 |
| 19 // This class is used by ProfileSyncService to manage all logic and state | 19 // This class is used by ProfileSyncService to manage all logic and state |
| 20 // pertaining to initialization of the SyncBackendHost (colloquially referred | 20 // pertaining to initialization of the SyncEngine. |
| 21 // to as "the backend"). | |
| 22 class StartupController { | 21 class StartupController { |
| 23 public: | 22 public: |
| 24 StartupController(const SyncPrefs* sync_prefs, | 23 StartupController(const SyncPrefs* sync_prefs, |
| 25 base::Callback<bool()> can_start, | 24 base::Callback<bool()> can_start, |
| 26 base::Closure start_backend); | 25 base::Closure start_engine); |
| 27 ~StartupController(); | 26 ~StartupController(); |
| 28 | 27 |
| 29 // Starts up sync if it is requested by the user and preconditions are met. | 28 // Starts up sync if it is requested by the user and preconditions are met. |
| 30 // Returns true if these preconditions are met, although does not imply | 29 // Returns true if these preconditions are met, although does not imply |
| 31 // the backend was started. | 30 // the engine was started. |
| 32 bool TryStart(); | 31 bool TryStart(); |
| 33 | 32 |
| 34 // Same as TryStart() above, but bypasses deferred startup and the first setup | 33 // Same as TryStart() above, but bypasses deferred startup and the first setup |
| 35 // complete check. | 34 // complete check. |
| 36 bool TryStartImmediately(); | 35 bool TryStartImmediately(); |
| 37 | 36 |
| 38 // Called when a datatype (SyncableService) has a need for sync to start | 37 // Called when a datatype (SyncableService) has a need for sync to start |
| 39 // ASAP, presumably because a local change event has occurred but we're | 38 // ASAP, presumably because a local change event has occurred but we're |
| 40 // still in deferred start mode, meaning the SyncableService hasn't been | 39 // still in deferred start mode, meaning the SyncableService hasn't been |
| 41 // told to MergeDataAndStartSyncing yet. | 40 // told to MergeDataAndStartSyncing yet. |
| 42 // It is expected that |type| is a currently active datatype. | 41 // It is expected that |type| is a currently active datatype. |
| 43 void OnDataTypeRequestsSyncStartup(ModelType type); | 42 void OnDataTypeRequestsSyncStartup(ModelType type); |
| 44 | 43 |
| 45 // Prepares this object for a new attempt to start sync, forgetting | 44 // Prepares this object for a new attempt to start sync, forgetting |
| 46 // whether or not preconditions were previously met. | 45 // whether or not preconditions were previously met. |
| 47 // NOTE: This resets internal state managed by this class, but does not | 46 // NOTE: This resets internal state managed by this class, but does not |
| 48 // touch values that are explicitly set and reset by higher layers to | 47 // touch values that are explicitly set and reset by higher layers to |
| 49 // tell this class whether a setup UI dialog is being shown to the user. | 48 // tell this class whether a setup UI dialog is being shown to the user. |
| 50 // See setup_in_progress_. | 49 // See setup_in_progress_. |
| 51 void Reset(const ModelTypeSet registered_types); | 50 void Reset(const ModelTypeSet registered_types); |
| 52 | 51 |
| 53 // Sets the setup in progress flag and tries to start sync if it's true. | 52 // Sets the setup in progress flag and tries to start sync if it's true. |
| 54 void SetSetupInProgress(bool setup_in_progress); | 53 void SetSetupInProgress(bool setup_in_progress); |
| 55 | 54 |
| 56 bool IsSetupInProgress() const { return setup_in_progress_; } | 55 bool IsSetupInProgress() const { return setup_in_progress_; } |
| 57 base::Time start_backend_time() const { return start_backend_time_; } | 56 base::Time start_engine_time() const { return start_engine_time_; } |
| 58 std::string GetBackendInitializationStateString() const; | 57 std::string GetEngineInitializationStateString() const; |
| 59 | 58 |
| 60 void OverrideFallbackTimeoutForTest(const base::TimeDelta& timeout); | 59 void OverrideFallbackTimeoutForTest(const base::TimeDelta& timeout); |
| 61 | 60 |
| 62 private: | 61 private: |
| 63 enum StartUpDeferredOption { STARTUP_BACKEND_DEFERRED, STARTUP_IMMEDIATE }; | 62 enum StartUpDeferredOption { STARTUP_DEFERRED, STARTUP_IMMEDIATE }; |
| 64 // Returns true if all conditions to start the backend are met. | 63 // Returns true if all conditions to start the engine are met. |
| 65 bool StartUp(StartUpDeferredOption deferred_option); | 64 bool StartUp(StartUpDeferredOption deferred_option); |
| 66 void OnFallbackStartupTimerExpired(); | 65 void OnFallbackStartupTimerExpired(); |
| 67 | 66 |
| 68 // Records time spent in deferred state with UMA histograms. | 67 // Records time spent in deferred state with UMA histograms. |
| 69 void RecordTimeDeferred(); | 68 void RecordTimeDeferred(); |
| 70 | 69 |
| 71 // If true, will bypass the FirstSetupComplete check when triggering sync | 70 // If true, will bypass the FirstSetupComplete check when triggering sync |
| 72 // startup. | 71 // startup. |
| 73 bool bypass_setup_complete_; | 72 bool bypass_setup_complete_; |
| 74 | 73 |
| 75 // True if we should start sync ASAP because either a SyncableService has | 74 // True if we should start sync ASAP because either a SyncableService has |
| 76 // requested it, or we're done waiting for a sign and decided to go ahead. | 75 // requested it, or we're done waiting for a sign and decided to go ahead. |
| 77 bool received_start_request_; | 76 bool received_start_request_; |
| 78 | 77 |
| 79 // The time that StartUp() is called. This is used to calculate time spent | 78 // The time that StartUp() is called. This is used to calculate time spent |
| 80 // in the deferred state; that is, after StartUp and before invoking the | 79 // in the deferred state; that is, after StartUp and before invoking the |
| 81 // start_backend_ callback. | 80 // start_engine_ callback. |
| 82 base::Time start_up_time_; | 81 base::Time start_up_time_; |
| 83 | 82 |
| 84 // If |true|, there is setup UI visible so we should not start downloading | 83 // If |true|, there is setup UI visible so we should not start downloading |
| 85 // data types. | 84 // data types. |
| 86 // Note: this is explicitly controlled by higher layers (UI) and is meant to | 85 // Note: this is explicitly controlled by higher layers (UI) and is meant to |
| 87 // reflect what the UI claims the setup state to be. Therefore, only set this | 86 // reflect what the UI claims the setup state to be. Therefore, only set this |
| 88 // due to explicit requests to do so via SetSetupInProgress. | 87 // due to explicit requests to do so via SetSetupInProgress. |
| 89 bool setup_in_progress_; | 88 bool setup_in_progress_; |
| 90 | 89 |
| 91 const SyncPrefs* sync_prefs_; | 90 const SyncPrefs* sync_prefs_; |
| 92 | 91 |
| 93 // A function that can be invoked repeatedly to determine whether sync can be | 92 // A function that can be invoked repeatedly to determine whether sync can be |
| 94 // started. |start_backend_| should not be invoked unless this returns true. | 93 // started. |start_engine_| should not be invoked unless this returns true. |
| 95 base::Callback<bool()> can_start_; | 94 base::Callback<bool()> can_start_; |
| 96 | 95 |
| 97 // The callback we invoke when it's time to call expensive | 96 // The callback we invoke when it's time to call expensive |
| 98 // startup routines for the sync backend. | 97 // startup routines for the sync engine. |
| 99 base::Closure start_backend_; | 98 base::Closure start_engine_; |
| 100 | 99 |
| 101 // The time at which we invoked the start_backend_ callback. | 100 // The time at which we invoked the start_engine_ callback. |
| 102 base::Time start_backend_time_; | 101 base::Time start_engine_time_; |
| 103 | 102 |
| 104 base::TimeDelta fallback_timeout_; | 103 base::TimeDelta fallback_timeout_; |
| 105 | 104 |
| 106 // Used to compute preferred_types from SyncPrefs as-needed. | 105 // Used to compute preferred_types from SyncPrefs as-needed. |
| 107 ModelTypeSet registered_types_; | 106 ModelTypeSet registered_types_; |
| 108 | 107 |
| 109 base::WeakPtrFactory<StartupController> weak_factory_; | 108 base::WeakPtrFactory<StartupController> weak_factory_; |
| 110 }; | 109 }; |
| 111 | 110 |
| 112 } // namespace syncer | 111 } // namespace syncer |
| 113 | 112 |
| 114 #endif // COMPONENTS_SYNC_DRIVER_STARTUP_CONTROLLER_H_ | 113 #endif // COMPONENTS_SYNC_DRIVER_STARTUP_CONTROLLER_H_ |
| OLD | NEW |