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