Max Bogue | fef332d | 2016-07-28 22:09:09 | [diff] [blame] | 1 | // Copyright 2012 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. |
skym | d5e2661 | 2016-10-18 15:40:05 | [diff] [blame] | 4 | |
Max Bogue | fef332d | 2016-07-28 22:09:09 | [diff] [blame] | 5 | #ifndef COMPONENTS_SYNC_ENGINE_IMPL_SYNC_SCHEDULER_H_ |
| 6 | #define COMPONENTS_SYNC_ENGINE_IMPL_SYNC_SCHEDULER_H_ |
| 7 | |
| 8 | #include <memory> |
| 9 | #include <string> |
| 10 | |
| 11 | #include "base/callback.h" |
| 12 | #include "base/compiler_specific.h" |
| 13 | #include "base/time/time.h" |
| 14 | #include "components/sync/base/invalidation_interface.h" |
maxbogue | 3067278a | 2016-08-19 23:16:18 | [diff] [blame] | 15 | #include "components/sync/engine_impl/cycle/sync_cycle.h" |
Max Bogue | fef332d | 2016-07-28 22:09:09 | [diff] [blame] | 16 | #include "components/sync/engine_impl/nudge_source.h" |
Helen Li | 7137772 | 2017-11-06 23:10:24 | [diff] [blame] | 17 | #include "net/base/network_change_notifier.h" |
Max Bogue | fef332d | 2016-07-28 22:09:09 | [diff] [blame] | 18 | |
Brett Wilson | abbb960 | 2017-09-11 23:26:39 | [diff] [blame] | 19 | namespace base { |
Max Bogue | fef332d | 2016-07-28 22:09:09 | [diff] [blame] | 20 | class Location; |
Brett Wilson | abbb960 | 2017-09-11 23:26:39 | [diff] [blame] | 21 | } // namespace base |
Max Bogue | fef332d | 2016-07-28 22:09:09 | [diff] [blame] | 22 | |
| 23 | namespace syncer { |
| 24 | |
maxbogue | 35515f47 | 2016-08-13 01:55:25 | [diff] [blame] | 25 | struct ConfigurationParams { |
Max Bogue | fef332d | 2016-07-28 22:09:09 | [diff] [blame] | 26 | ConfigurationParams(); |
Marc Treib | 3c526eca | 2018-04-04 13:40:03 | [diff] [blame] | 27 | ConfigurationParams(sync_pb::SyncEnums::GetUpdatesOrigin origin, |
| 28 | ModelTypeSet types_to_download, |
| 29 | const base::Closure& ready_task, |
| 30 | const base::Closure& retry_task); |
Max Bogue | fef332d | 2016-07-28 22:09:09 | [diff] [blame] | 31 | ConfigurationParams(const ConfigurationParams& other); |
| 32 | ~ConfigurationParams(); |
| 33 | |
Marc Treib | 3c526eca | 2018-04-04 13:40:03 | [diff] [blame] | 34 | // Origin for the configuration. |
| 35 | sync_pb::SyncEnums::GetUpdatesOrigin origin; |
Max Bogue | fef332d | 2016-07-28 22:09:09 | [diff] [blame] | 36 | // The types that should be downloaded. |
| 37 | ModelTypeSet types_to_download; |
Max Bogue | fef332d | 2016-07-28 22:09:09 | [diff] [blame] | 38 | // Callback to invoke on configuration completion. |
| 39 | base::Closure ready_task; |
| 40 | // Callback to invoke on configuration failure. |
| 41 | base::Closure retry_task; |
| 42 | }; |
| 43 | |
maxbogue | 35515f47 | 2016-08-13 01:55:25 | [diff] [blame] | 44 | struct ClearParams { |
Max Bogue | fef332d | 2016-07-28 22:09:09 | [diff] [blame] | 45 | explicit ClearParams(const base::Closure& report_success_task); |
| 46 | ClearParams(const ClearParams& other); |
| 47 | ~ClearParams(); |
| 48 | |
| 49 | // Callback to invoke on successful completion. |
| 50 | base::Closure report_success_task; |
| 51 | }; |
| 52 | |
skym | d5e2661 | 2016-10-18 15:40:05 | [diff] [blame] | 53 | // A class to schedule syncer tasks intelligently. |
maxbogue | 3067278a | 2016-08-19 23:16:18 | [diff] [blame] | 54 | class SyncScheduler : public SyncCycle::Delegate { |
Max Bogue | fef332d | 2016-07-28 22:09:09 | [diff] [blame] | 55 | public: |
| 56 | enum Mode { |
| 57 | // In this mode, the thread only performs configuration tasks. This is |
| 58 | // designed to make the case where we want to download updates for a |
| 59 | // specific type only, and not continue syncing until we are moved into |
| 60 | // normal mode. |
| 61 | CONFIGURATION_MODE, |
| 62 | // This mode is used to issue a clear server data command. The scheduler |
| 63 | // may only transition to this mode from the CONFIGURATION_MODE. When in |
| 64 | // this mode, the only schedulable operation is |SchedulerClearServerData|. |
| 65 | CLEAR_SERVER_DATA_MODE, |
| 66 | // Resumes polling and allows nudges, drops configuration tasks. Runs |
| 67 | // through entire sync cycle. |
| 68 | NORMAL_MODE, |
| 69 | }; |
| 70 | |
| 71 | // All methods of SyncScheduler must be called on the same thread |
| 72 | // (except for RequestEarlyExit()). |
| 73 | |
| 74 | SyncScheduler(); |
| 75 | ~SyncScheduler() override; |
| 76 | |
| 77 | // Start the scheduler with the given mode. If the scheduler is |
| 78 | // already started, switch to the given mode, although some |
| 79 | // scheduled tasks from the old mode may still run. |last_poll_time| will |
| 80 | // be used to decide what the poll timer should be initialized with. |
| 81 | virtual void Start(Mode mode, base::Time last_poll_time) = 0; |
| 82 | |
| 83 | // Schedules the configuration task specified by |params|. Returns true if |
| 84 | // the configuration task executed immediately, false if it had to be |
| 85 | // scheduled for a later attempt. |params.ready_task| is invoked whenever the |
| 86 | // configuration task executes. |params.retry_task| is invoked once if the |
| 87 | // configuration task could not execute. |params.ready_task| will still be |
| 88 | // called when configuration finishes. |
| 89 | // Note: must already be in CONFIGURATION mode. |
| 90 | virtual void ScheduleConfiguration(const ConfigurationParams& params) = 0; |
| 91 | |
| 92 | // Schedules clear of server data in preparation for transitioning to |
| 93 | // passphrase encryption. The scheduler must be in CLEAR_SERVER_DATA_MODE |
| 94 | // before calling this method. |
| 95 | virtual void ScheduleClearServerData(const ClearParams& params) = 0; |
| 96 | |
| 97 | // Request that the syncer avoid starting any new tasks and prepare for |
| 98 | // shutdown. |
| 99 | virtual void Stop() = 0; |
| 100 | |
| 101 | // The meat and potatoes. All three of the following methods will post a |
| 102 | // delayed task to attempt the actual nudge (see ScheduleNudgeImpl). |
| 103 | // |
| 104 | // NOTE: |desired_delay| is best-effort. If a nudge is already scheduled to |
| 105 | // depart earlier than Now() + delay, the scheduler can and will prefer to |
| 106 | // batch the two so that only one nudge is sent (at the earlier time). Also, |
| 107 | // as always with delayed tasks and timers, it's possible the task gets run |
| 108 | // any time after |desired_delay|. |
| 109 | |
| 110 | // The LocalNudge indicates that we've made a local change, and that the |
| 111 | // syncer should plan to commit this to the server some time soon. |
Brett Wilson | abbb960 | 2017-09-11 23:26:39 | [diff] [blame] | 112 | virtual void ScheduleLocalNudge(ModelTypeSet types, |
| 113 | const base::Location& nudge_location) = 0; |
Max Bogue | fef332d | 2016-07-28 22:09:09 | [diff] [blame] | 114 | |
| 115 | // The LocalRefreshRequest occurs when we decide for some reason to manually |
| 116 | // request updates. This should be used sparingly. For example, one of its |
| 117 | // uses is to fetch the latest tab sync data when it's relevant to the UI on |
| 118 | // platforms where tab sync is not registered for invalidations. |
| 119 | virtual void ScheduleLocalRefreshRequest( |
| 120 | ModelTypeSet types, |
Brett Wilson | abbb960 | 2017-09-11 23:26:39 | [diff] [blame] | 121 | const base::Location& nudge_location) = 0; |
Max Bogue | fef332d | 2016-07-28 22:09:09 | [diff] [blame] | 122 | |
| 123 | // Invalidations are notifications the server sends to let us know when other |
| 124 | // clients have committed data. We need to contact the sync server (being |
| 125 | // careful to pass along the "hints" delivered with those invalidations) in |
| 126 | // order to fetch the update. |
| 127 | virtual void ScheduleInvalidationNudge( |
maxbogue | 7e006db | 2016-10-03 19:48:28 | [diff] [blame] | 128 | ModelType type, |
Max Bogue | fef332d | 2016-07-28 22:09:09 | [diff] [blame] | 129 | std::unique_ptr<InvalidationInterface> invalidation, |
Brett Wilson | abbb960 | 2017-09-11 23:26:39 | [diff] [blame] | 130 | const base::Location& nudge_location) = 0; |
Max Bogue | fef332d | 2016-07-28 22:09:09 | [diff] [blame] | 131 | |
| 132 | // Requests a non-blocking initial sync request for the specified type. |
| 133 | // |
| 134 | // Many types can only complete initial sync while the scheduler is in |
| 135 | // configure mode, but a few of them are able to perform their initial sync |
| 136 | // while the scheduler is in normal mode. This non-blocking initial sync |
| 137 | // can be requested through this function. |
maxbogue | 7e006db | 2016-10-03 19:48:28 | [diff] [blame] | 138 | virtual void ScheduleInitialSyncNudge(ModelType model_type) = 0; |
Max Bogue | fef332d | 2016-07-28 22:09:09 | [diff] [blame] | 139 | |
maxbogue | 3067278a | 2016-08-19 23:16:18 | [diff] [blame] | 140 | // Change status of notifications in the SyncCycleContext. |
Max Bogue | fef332d | 2016-07-28 22:09:09 | [diff] [blame] | 141 | virtual void SetNotificationsEnabled(bool notifications_enabled) = 0; |
| 142 | |
| 143 | // Called when credentials are updated by the user. |
| 144 | virtual void OnCredentialsUpdated() = 0; |
| 145 | |
| 146 | // Called when the network layer detects a connection status change. |
Helen Li | 7137772 | 2017-11-06 23:10:24 | [diff] [blame] | 147 | virtual void OnConnectionStatusChange( |
| 148 | net::NetworkChangeNotifier::ConnectionType type) = 0; |
Max Bogue | fef332d | 2016-07-28 22:09:09 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | } // namespace syncer |
| 152 | |
| 153 | #endif // COMPONENTS_SYNC_ENGINE_IMPL_SYNC_SCHEDULER_H_ |