OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // A class to schedule syncer tasks intelligently. | 5 // A class to schedule syncer tasks intelligently. |
6 #ifndef SYNC_ENGINE_SYNC_SCHEDULER_H_ | 6 #ifndef SYNC_ENGINE_SYNC_SCHEDULER_H_ |
7 #define SYNC_ENGINE_SYNC_SCHEDULER_H_ | 7 #define SYNC_ENGINE_SYNC_SCHEDULER_H_ |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 18 matching lines...) Expand all Loading... |
29 class MessageLoop; | 29 class MessageLoop; |
30 | 30 |
31 namespace tracked_objects { | 31 namespace tracked_objects { |
32 class Location; | 32 class Location; |
33 } // namespace tracked_objects | 33 } // namespace tracked_objects |
34 | 34 |
35 namespace syncer { | 35 namespace syncer { |
36 | 36 |
37 struct ServerConnectionEvent; | 37 struct ServerConnectionEvent; |
38 | 38 |
| 39 struct ConfigurationParams { |
| 40 enum KeystoreKeyStatus { |
| 41 KEYSTORE_KEY_UNNECESSARY, |
| 42 KEYSTORE_KEY_NEEDED |
| 43 }; |
| 44 ConfigurationParams(); |
| 45 ConfigurationParams( |
| 46 const sync_pb::GetUpdatesCallerInfo::GetUpdatesSource& source, |
| 47 const syncer::ModelTypeSet& types_to_download, |
| 48 const syncer::ModelSafeRoutingInfo& routing_info, |
| 49 KeystoreKeyStatus keystore_key_status, |
| 50 const base::Closure& ready_task); |
| 51 ~ConfigurationParams(); |
| 52 |
| 53 // Source for the configuration. |
| 54 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source; |
| 55 // The types that should be downloaded. |
| 56 syncer::ModelTypeSet types_to_download; |
| 57 // The new routing info (superset of types to be downloaded). |
| 58 ModelSafeRoutingInfo routing_info; |
| 59 // Whether we need to perform a GetKey command. |
| 60 KeystoreKeyStatus keystore_key_status; |
| 61 // Callback to invoke on configuration completion. |
| 62 base::Closure ready_task; |
| 63 }; |
| 64 |
39 class SyncScheduler : public sessions::SyncSession::Delegate { | 65 class SyncScheduler : public sessions::SyncSession::Delegate { |
40 public: | 66 public: |
41 enum Mode { | 67 enum Mode { |
42 // In this mode, the thread only performs configuration tasks. This is | 68 // In this mode, the thread only performs configuration tasks. This is |
43 // designed to make the case where we want to download updates for a | 69 // designed to make the case where we want to download updates for a |
44 // specific type only, and not continue syncing until we are moved into | 70 // specific type only, and not continue syncing until we are moved into |
45 // normal mode. | 71 // normal mode. |
46 CONFIGURATION_MODE, | 72 CONFIGURATION_MODE, |
47 // Resumes polling and allows nudges, drops configuration tasks. Runs | 73 // Resumes polling and allows nudges, drops configuration tasks. Runs |
48 // through entire sync cycle. | 74 // through entire sync cycle. |
49 NORMAL_MODE, | 75 NORMAL_MODE, |
50 }; | 76 }; |
51 | 77 |
52 // All methods of SyncScheduler must be called on the same thread | 78 // All methods of SyncScheduler must be called on the same thread |
53 // (except for RequestEarlyExit()). | 79 // (except for RequestEarlyExit()). |
54 | 80 |
55 // |name| is a display string to identify the syncer thread. Takes | 81 // |name| is a display string to identify the syncer thread. Takes |
56 // |ownership of |syncer|. | 82 // |ownership of |syncer|. |
57 SyncScheduler(const std::string& name, | 83 SyncScheduler(const std::string& name, |
58 sessions::SyncSessionContext* context, Syncer* syncer); | 84 sessions::SyncSessionContext* context, Syncer* syncer); |
59 | 85 |
60 // Calls Stop(). | 86 // Calls Stop(). |
61 virtual ~SyncScheduler(); | 87 virtual ~SyncScheduler(); |
62 | 88 |
63 // Start the scheduler with the given mode. If the scheduler is | 89 // Start the scheduler with the given mode. If the scheduler is |
64 // already started, switch to the given mode, although some | 90 // already started, switch to the given mode, although some |
65 // scheduled tasks from the old mode may still run. If non-NULL, | 91 // scheduled tasks from the old mode may still run. |
66 // |callback| will be invoked when the mode has been changed to | 92 virtual void Start(Mode mode); |
67 // |mode|. Takes ownership of |callback|. | 93 |
68 void Start(Mode mode, const base::Closure& callback); | 94 // Schedules the configuration task specified by |params|. Returns true if |
| 95 // the configuration task executed immediately, false if it had to be |
| 96 // scheduled for a later attempt. |params.ready_task| is invoked whenever the |
| 97 // configuration task executes. |
| 98 // Note: must already be in CONFIGURATION mode. |
| 99 virtual bool ScheduleConfiguration(const ConfigurationParams& params); |
69 | 100 |
70 // Request that any running syncer task stop as soon as possible and | 101 // Request that any running syncer task stop as soon as possible and |
71 // cancel all scheduled tasks. This function can be called from any thread, | 102 // cancel all scheduled tasks. This function can be called from any thread, |
72 // and should in fact be called from a thread that isn't the sync loop to | 103 // and should in fact be called from a thread that isn't the sync loop to |
73 // allow preempting ongoing sync cycles. | 104 // allow preempting ongoing sync cycles. |
74 // Invokes |callback| from the sync loop once syncer is idle and all tasks | 105 // Invokes |callback| from the sync loop once syncer is idle and all tasks |
75 // are cancelled. | 106 // are cancelled. |
76 void RequestStop(const base::Closure& callback); | 107 void RequestStop(const base::Closure& callback); |
77 | 108 |
78 // The meat and potatoes. Both of these methods will post a delayed task | 109 // The meat and potatoes. Both of these methods will post a delayed task |
79 // to attempt the actual nudge (see ScheduleNudgeImpl). | 110 // to attempt the actual nudge (see ScheduleNudgeImpl). |
80 void ScheduleNudgeAsync(const base::TimeDelta& delay, NudgeSource source, | 111 void ScheduleNudgeAsync(const base::TimeDelta& delay, NudgeSource source, |
81 syncer::ModelTypeSet types, | 112 syncer::ModelTypeSet types, |
82 const tracked_objects::Location& nudge_location); | 113 const tracked_objects::Location& nudge_location); |
83 void ScheduleNudgeWithPayloadsAsync( | 114 void ScheduleNudgeWithPayloadsAsync( |
84 const base::TimeDelta& delay, NudgeSource source, | 115 const base::TimeDelta& delay, NudgeSource source, |
85 const syncer::ModelTypePayloadMap& types_with_payloads, | 116 const syncer::ModelTypePayloadMap& types_with_payloads, |
86 const tracked_objects::Location& nudge_location); | 117 const tracked_objects::Location& nudge_location); |
87 | 118 |
88 // Schedule a configuration cycle. May execute immediately or at a later time | |
89 // (depending on backoff/throttle state). | |
90 // Note: The source argument of this function must come from the subset of | |
91 // GetUpdatesCallerInfo values related to configurations. | |
92 void ScheduleConfiguration( | |
93 syncer::ModelTypeSet types, | |
94 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source); | |
95 | |
96 void CleanupDisabledTypes(); | 119 void CleanupDisabledTypes(); |
97 | 120 |
98 // Change status of notifications in the SyncSessionContext. | 121 // Change status of notifications in the SyncSessionContext. |
99 void set_notifications_enabled(bool notifications_enabled); | 122 void set_notifications_enabled(bool notifications_enabled); |
100 | 123 |
101 base::TimeDelta sessions_commit_delay() const; | 124 base::TimeDelta sessions_commit_delay() const; |
102 | 125 |
103 // DDOS avoidance function. Calculates how long we should wait before trying | 126 // DDOS avoidance function. Calculates how long we should wait before trying |
104 // again after a failed sync attempt, where the last delay was |base_delay|. | 127 // again after a failed sync attempt, where the last delay was |base_delay|. |
105 // TODO(tim): Look at URLRequestThrottlerEntryInterface. | 128 // TODO(tim): Look at URLRequestThrottlerEntryInterface. |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 // during initial sync or reconfiguration. We don't run all steps of | 173 // during initial sync or reconfiguration. We don't run all steps of |
151 // the sync cycle for these (e.g. CleanupDisabledTypes is skipped). | 174 // the sync cycle for these (e.g. CleanupDisabledTypes is skipped). |
152 CONFIGURATION, | 175 CONFIGURATION, |
153 // The user disabled some types and we have to clean up the data | 176 // The user disabled some types and we have to clean up the data |
154 // for those. | 177 // for those. |
155 CLEANUP_DISABLED_TYPES, | 178 CLEANUP_DISABLED_TYPES, |
156 }; | 179 }; |
157 SyncSessionJob(); | 180 SyncSessionJob(); |
158 SyncSessionJob(SyncSessionJobPurpose purpose, base::TimeTicks start, | 181 SyncSessionJob(SyncSessionJobPurpose purpose, base::TimeTicks start, |
159 linked_ptr<sessions::SyncSession> session, bool is_canary_job, | 182 linked_ptr<sessions::SyncSession> session, bool is_canary_job, |
| 183 const ConfigurationParams& config_params, |
160 const tracked_objects::Location& nudge_location); | 184 const tracked_objects::Location& nudge_location); |
161 ~SyncSessionJob(); | 185 ~SyncSessionJob(); |
162 static const char* GetPurposeString(SyncSessionJobPurpose purpose); | 186 static const char* GetPurposeString(SyncSessionJobPurpose purpose); |
163 | 187 |
164 SyncSessionJobPurpose purpose; | 188 SyncSessionJobPurpose purpose; |
165 base::TimeTicks scheduled_start; | 189 base::TimeTicks scheduled_start; |
166 linked_ptr<sessions::SyncSession> session; | 190 linked_ptr<sessions::SyncSession> session; |
167 bool is_canary_job; | 191 bool is_canary_job; |
| 192 ConfigurationParams config_params; |
168 | 193 |
169 // This is the location the job came from. Used for debugging. | 194 // This is the location the job came from. Used for debugging. |
170 // In case of multiple nudges getting coalesced this stores the | 195 // In case of multiple nudges getting coalesced this stores the |
171 // first location that came in. | 196 // first location that came in. |
172 tracked_objects::Location from_here; | 197 tracked_objects::Location from_here; |
173 }; | 198 }; |
174 friend class SyncSchedulerTest; | 199 friend class SyncSchedulerTest; |
175 friend class SyncSchedulerWhiteboxTest; | 200 friend class SyncSchedulerWhiteboxTest; |
176 friend class SyncerTest; | 201 friend class SyncerTest; |
177 | 202 |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 scoped_ptr<Syncer> syncer_; | 422 scoped_ptr<Syncer> syncer_; |
398 | 423 |
399 sessions::SyncSessionContext *session_context_; | 424 sessions::SyncSessionContext *session_context_; |
400 | 425 |
401 DISALLOW_COPY_AND_ASSIGN(SyncScheduler); | 426 DISALLOW_COPY_AND_ASSIGN(SyncScheduler); |
402 }; | 427 }; |
403 | 428 |
404 } // namespace syncer | 429 } // namespace syncer |
405 | 430 |
406 #endif // SYNC_ENGINE_SYNC_SCHEDULER_H_ | 431 #endif // SYNC_ENGINE_SYNC_SCHEDULER_H_ |
OLD | NEW |