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.
5 // A class to schedule syncer tasks intelligently.
6 #ifndef SYNC_ENGINE_SYNC_SCHEDULER_H_
7 #define SYNC_ENGINE_SYNC_SCHEDULER_H_
11 #include "base/callback.h"
12 #include "base/compiler_specific.h"
13 #include "base/time/time.h"
14 #include "sync/base/sync_export.h"
15 #include "sync/engine/nudge_source.h"
16 #include "sync/internal_api/public/base/invalidation_interface.h"
17 #include "sync/sessions/sync_session.h"
19 namespace tracked_objects
{
21 } // namespace tracked_objects
25 struct ServerConnectionEvent
;
27 struct SYNC_EXPORT_PRIVATE ConfigurationParams
{
28 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();
37 // Source for the configuration.
38 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source
;
39 // The types that should be downloaded.
40 ModelTypeSet types_to_download
;
41 // The new routing info (superset of types to be downloaded).
42 ModelSafeRoutingInfo routing_info
;
43 // Callback to invoke on configuration completion.
44 base::Closure ready_task
;
45 // Callback to invoke on configuration failure.
46 base::Closure retry_task
;
49 class SYNC_EXPORT_PRIVATE SyncScheduler
50 : public sessions::SyncSession::Delegate
{
53 // In this mode, the thread only performs configuration tasks. This is
54 // designed to make the case where we want to download updates for a
55 // specific type only, and not continue syncing until we are moved into
58 // Resumes polling and allows nudges, drops configuration tasks. Runs
59 // through entire sync cycle.
63 // All methods of SyncScheduler must be called on the same thread
64 // (except for RequestEarlyExit()).
67 ~SyncScheduler() override
;
69 // Start the scheduler with the given mode. If the scheduler is
70 // already started, switch to the given mode, although some
71 // scheduled tasks from the old mode may still run. |last_poll_time| will
72 // be used to decide what the poll timer should be initialized with.
73 virtual void Start(Mode mode
, base::Time last_poll_time
) = 0;
75 // Schedules the configuration task specified by |params|. Returns true if
76 // the configuration task executed immediately, false if it had to be
77 // scheduled for a later attempt. |params.ready_task| is invoked whenever the
78 // configuration task executes. |params.retry_task| is invoked once if the
79 // configuration task could not execute. |params.ready_task| will still be
80 // called when configuration finishes.
81 // Note: must already be in CONFIGURATION mode.
82 virtual void ScheduleConfiguration(const ConfigurationParams
& params
) = 0;
84 // Request that the syncer avoid starting any new tasks and prepare for
86 virtual void Stop() = 0;
88 // The meat and potatoes. All three of the following methods will post a
89 // delayed task to attempt the actual nudge (see ScheduleNudgeImpl).
91 // NOTE: |desired_delay| is best-effort. If a nudge is already scheduled to
92 // depart earlier than Now() + delay, the scheduler can and will prefer to
93 // batch the two so that only one nudge is sent (at the earlier time). Also,
94 // as always with delayed tasks and timers, it's possible the task gets run
95 // any time after |desired_delay|.
97 // The LocalNudge indicates that we've made a local change, and that the
98 // syncer should plan to commit this to the server some time soon.
99 virtual void ScheduleLocalNudge(
101 const tracked_objects::Location
& nudge_location
) = 0;
103 // The LocalRefreshRequest occurs when we decide for some reason to manually
104 // request updates. This should be used sparingly. For example, one of its
105 // uses is to fetch the latest tab sync data when it's relevant to the UI on
106 // platforms where tab sync is not registered for invalidations.
107 virtual void ScheduleLocalRefreshRequest(
109 const tracked_objects::Location
& nudge_location
) = 0;
111 // Invalidations are notifications the server sends to let us know when other
112 // clients have committed data. We need to contact the sync server (being
113 // careful to pass along the "hints" delivered with those invalidations) in
114 // order to fetch the update.
115 virtual void ScheduleInvalidationNudge(
116 syncer::ModelType type
,
117 scoped_ptr
<InvalidationInterface
> invalidation
,
118 const tracked_objects::Location
& nudge_location
) = 0;
120 // Requests a non-blocking initial sync request for the specified type.
122 // Many types can only complete initial sync while the scheduler is in
123 // configure mode, but a few of them are able to perform their initial sync
124 // while the scheduler is in normal mode. This non-blocking initial sync
125 // can be requested through this function.
126 virtual void ScheduleInitialSyncNudge(syncer::ModelType model_type
) = 0;
128 // Change status of notifications in the SyncSessionContext.
129 virtual void SetNotificationsEnabled(bool notifications_enabled
) = 0;
131 // Called when credentials are updated by the user.
132 virtual void OnCredentialsUpdated() = 0;
134 // Called when the network layer detects a connection status change.
135 virtual void OnConnectionStatusChange() = 0;
138 } // namespace syncer
140 #endif // SYNC_ENGINE_SYNC_SCHEDULER_H_