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 virtual ~SyncScheduler();
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.
72 virtual void Start(Mode mode
) = 0;
74 // Schedules the configuration task specified by |params|. Returns true if
75 // the configuration task executed immediately, false if it had to be
76 // scheduled for a later attempt. |params.ready_task| is invoked whenever the
77 // configuration task executes. |params.retry_task| is invoked once if the
78 // configuration task could not execute. |params.ready_task| will still be
79 // called when configuration finishes.
80 // Note: must already be in CONFIGURATION mode.
81 virtual void ScheduleConfiguration(const ConfigurationParams
& params
) = 0;
83 // Request that the syncer avoid starting any new tasks and prepare for
85 virtual void Stop() = 0;
87 // The meat and potatoes. All three of the following methods will post a
88 // delayed task to attempt the actual nudge (see ScheduleNudgeImpl).
90 // NOTE: |desired_delay| is best-effort. If a nudge is already scheduled to
91 // depart earlier than Now() + delay, the scheduler can and will prefer to
92 // batch the two so that only one nudge is sent (at the earlier time). Also,
93 // as always with delayed tasks and timers, it's possible the task gets run
94 // any time after |desired_delay|.
96 // The LocalNudge indicates that we've made a local change, and that the
97 // syncer should plan to commit this to the server some time soon.
98 virtual void ScheduleLocalNudge(
99 const base::TimeDelta
& desired_delay
,
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(
108 const base::TimeDelta
& desired_delay
,
110 const tracked_objects::Location
& nudge_location
) = 0;
112 // Invalidations are notifications the server sends to let us know when other
113 // clients have committed data. We need to contact the sync server (being
114 // careful to pass along the "hints" delivered with those invalidations) in
115 // order to fetch the update.
116 virtual void ScheduleInvalidationNudge(
117 const base::TimeDelta
& desired_delay
,
118 syncer::ModelType type
,
119 scoped_ptr
<InvalidationInterface
> invalidation
,
120 const tracked_objects::Location
& nudge_location
) = 0;
122 // Change status of notifications in the SyncSessionContext.
123 virtual void SetNotificationsEnabled(bool notifications_enabled
) = 0;
125 virtual base::TimeDelta
GetSessionsCommitDelay() const = 0;
127 // Called when credentials are updated by the user.
128 virtual void OnCredentialsUpdated() = 0;
130 // Called when the network layer detects a connection status change.
131 virtual void OnConnectionStatusChange() = 0;
134 } // namespace syncer
136 #endif // SYNC_ENGINE_SYNC_SCHEDULER_H_