blob: 423bec97a98a2181ecb38f5ed7f35ac988754db5 [file] [log] [blame]
Max Boguefef332d2016-07-28 22:09:091// 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.
skymd5e26612016-10-18 15:40:054
Max Boguefef332d2016-07-28 22:09:095#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"
maxbogue3067278a2016-08-19 23:16:1815#include "components/sync/engine_impl/cycle/sync_cycle.h"
Max Boguefef332d2016-07-28 22:09:0916#include "components/sync/engine_impl/nudge_source.h"
Helen Li71377722017-11-06 23:10:2417#include "net/base/network_change_notifier.h"
Max Boguefef332d2016-07-28 22:09:0918
Brett Wilsonabbb9602017-09-11 23:26:3919namespace base {
Max Boguefef332d2016-07-28 22:09:0920class Location;
Brett Wilsonabbb9602017-09-11 23:26:3921} // namespace base
Max Boguefef332d2016-07-28 22:09:0922
23namespace syncer {
24
maxbogue35515f472016-08-13 01:55:2525struct ConfigurationParams {
Max Boguefef332d2016-07-28 22:09:0926 ConfigurationParams();
Marc Treib3c526eca2018-04-04 13:40:0327 ConfigurationParams(sync_pb::SyncEnums::GetUpdatesOrigin origin,
28 ModelTypeSet types_to_download,
29 const base::Closure& ready_task,
30 const base::Closure& retry_task);
Max Boguefef332d2016-07-28 22:09:0931 ConfigurationParams(const ConfigurationParams& other);
32 ~ConfigurationParams();
33
Marc Treib3c526eca2018-04-04 13:40:0334 // Origin for the configuration.
35 sync_pb::SyncEnums::GetUpdatesOrigin origin;
Max Boguefef332d2016-07-28 22:09:0936 // The types that should be downloaded.
37 ModelTypeSet types_to_download;
Max Boguefef332d2016-07-28 22:09:0938 // 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
maxbogue35515f472016-08-13 01:55:2544struct ClearParams {
Max Boguefef332d2016-07-28 22:09:0945 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
skymd5e26612016-10-18 15:40:0553// A class to schedule syncer tasks intelligently.
maxbogue3067278a2016-08-19 23:16:1854class SyncScheduler : public SyncCycle::Delegate {
Max Boguefef332d2016-07-28 22:09:0955 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 Wilsonabbb9602017-09-11 23:26:39112 virtual void ScheduleLocalNudge(ModelTypeSet types,
113 const base::Location& nudge_location) = 0;
Max Boguefef332d2016-07-28 22:09:09114
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 Wilsonabbb9602017-09-11 23:26:39121 const base::Location& nudge_location) = 0;
Max Boguefef332d2016-07-28 22:09:09122
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(
maxbogue7e006db2016-10-03 19:48:28128 ModelType type,
Max Boguefef332d2016-07-28 22:09:09129 std::unique_ptr<InvalidationInterface> invalidation,
Brett Wilsonabbb9602017-09-11 23:26:39130 const base::Location& nudge_location) = 0;
Max Boguefef332d2016-07-28 22:09:09131
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.
maxbogue7e006db2016-10-03 19:48:28138 virtual void ScheduleInitialSyncNudge(ModelType model_type) = 0;
Max Boguefef332d2016-07-28 22:09:09139
maxbogue3067278a2016-08-19 23:16:18140 // Change status of notifications in the SyncCycleContext.
Max Boguefef332d2016-07-28 22:09:09141 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 Li71377722017-11-06 23:10:24147 virtual void OnConnectionStatusChange(
148 net::NetworkChangeNotifier::ConnectionType type) = 0;
Max Boguefef332d2016-07-28 22:09:09149};
150
151} // namespace syncer
152
153#endif // COMPONENTS_SYNC_ENGINE_IMPL_SYNC_SCHEDULER_H_