Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(310)

Side by Side Diff: sync/engine/sync_scheduler_impl.h

Issue 10701046: sync: Remove SyncManager::TestingMode in favour of InternalComponentsFactory. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: remove SetSchedulerForTest Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 #ifndef SYNC_ENGINE_SYNC_SCHEDULER_IMPL_H_
6 #ifndef SYNC_ENGINE_SYNC_SCHEDULER_H_ 6 #define SYNC_ENGINE_SYNC_SCHEDULER_IMPL_H_
7 #define SYNC_ENGINE_SYNC_SCHEDULER_H_
8 7
9 #include <string> 8 #include <string>
10 9
11 #include "base/callback.h" 10 #include "sync/engine/sync_scheduler.h"
12 #include "base/compiler_specific.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/memory/linked_ptr.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/observer_list.h"
18 #include "base/time.h"
19 #include "base/timer.h"
20 #include "sync/engine/net/server_connection_manager.h"
21 #include "sync/engine/nudge_source.h"
22 #include "sync/engine/syncer.h"
23 #include "sync/internal_api/public/base/model_type_payload_map.h"
24 #include "sync/internal_api/public/engine/polling_constants.h"
25 #include "sync/internal_api/public/util/weak_handle.h"
26 #include "sync/sessions/sync_session.h"
27 #include "sync/sessions/sync_session_context.h"
28
29 class MessageLoop;
30
31 namespace tracked_objects {
32 class Location;
33 } // namespace tracked_objects
34 11
35 namespace syncer { 12 namespace syncer {
36 13
37 struct ServerConnectionEvent; 14 class SyncSchedulerImpl : public SyncScheduler {
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
65 class SyncScheduler : public sessions::SyncSession::Delegate {
66 public: 15 public:
67 enum Mode {
68 // In this mode, the thread only performs configuration tasks. This is
69 // designed to make the case where we want to download updates for a
70 // specific type only, and not continue syncing until we are moved into
71 // normal mode.
72 CONFIGURATION_MODE,
73 // Resumes polling and allows nudges, drops configuration tasks. Runs
74 // through entire sync cycle.
75 NORMAL_MODE,
76 };
77
78 // All methods of SyncScheduler must be called on the same thread
79 // (except for RequestEarlyExit()).
80
81 // |name| is a display string to identify the syncer thread. Takes 16 // |name| is a display string to identify the syncer thread. Takes
82 // |ownership of |syncer|. 17 // |ownership of |syncer|.
83 SyncScheduler(const std::string& name, 18 SyncSchedulerImpl(const std::string& name,
84 sessions::SyncSessionContext* context, Syncer* syncer); 19 sessions::SyncSessionContext* context, Syncer* syncer);
85 20
86 // Calls Stop(). 21 // Calls Stop().
87 virtual ~SyncScheduler(); 22 virtual ~SyncSchedulerImpl();
88 23
89 // Start the scheduler with the given mode. If the scheduler is 24 virtual void Start(Mode mode) OVERRIDE;
90 // already started, switch to the given mode, although some 25 virtual bool ScheduleConfiguration(
91 // scheduled tasks from the old mode may still run. 26 const ConfigurationParams& params) OVERRIDE;
92 virtual void Start(Mode mode); 27 virtual void RequestStop(const base::Closure& callback) OVERRIDE;
93 28 virtual void ScheduleNudgeAsync(
94 // Schedules the configuration task specified by |params|. Returns true if 29 const base::TimeDelta& delay,
95 // the configuration task executed immediately, false if it had to be 30 NudgeSource source,
96 // scheduled for a later attempt. |params.ready_task| is invoked whenever the 31 syncer::ModelTypeSet types,
97 // configuration task executes. 32 const tracked_objects::Location& nudge_location) OVERRIDE;
98 // Note: must already be in CONFIGURATION mode. 33 virtual void ScheduleNudgeWithPayloadsAsync(
99 virtual bool ScheduleConfiguration(const ConfigurationParams& params);
100
101 // Request that any running syncer task stop as soon as possible and
102 // cancel all scheduled tasks. This function can be called from any thread,
103 // and should in fact be called from a thread that isn't the sync loop to
104 // allow preempting ongoing sync cycles.
105 // Invokes |callback| from the sync loop once syncer is idle and all tasks
106 // are cancelled.
107 void RequestStop(const base::Closure& callback);
108
109 // The meat and potatoes. Both of these methods will post a delayed task
110 // to attempt the actual nudge (see ScheduleNudgeImpl).
111 void ScheduleNudgeAsync(const base::TimeDelta& delay, NudgeSource source,
112 syncer::ModelTypeSet types,
113 const tracked_objects::Location& nudge_location);
114 void ScheduleNudgeWithPayloadsAsync(
115 const base::TimeDelta& delay, NudgeSource source, 34 const base::TimeDelta& delay, NudgeSource source,
116 const syncer::ModelTypePayloadMap& types_with_payloads, 35 const syncer::ModelTypePayloadMap& types_with_payloads,
117 const tracked_objects::Location& nudge_location); 36 const tracked_objects::Location& nudge_location) OVERRIDE;
37 virtual void SetNotificationsEnabled(bool notifications_enabled) OVERRIDE;
118 38
119 void CleanupDisabledTypes(); 39 virtual base::TimeDelta GetSessionsCommitDelay() const OVERRIDE;
120 40
121 // Change status of notifications in the SyncSessionContext. 41 virtual void OnCredentialsUpdated() OVERRIDE;
122 void set_notifications_enabled(bool notifications_enabled); 42 virtual void OnConnectionStatusChange() OVERRIDE;
123
124 base::TimeDelta sessions_commit_delay() const;
125
126 // DDOS avoidance function. Calculates how long we should wait before trying
127 // again after a failed sync attempt, where the last delay was |base_delay|.
128 // TODO(tim): Look at URLRequestThrottlerEntryInterface.
129 static base::TimeDelta GetRecommendedDelay(const base::TimeDelta& base_delay);
130
131 // Called when credentials are updated by the user.
132 void OnCredentialsUpdated();
133
134 // Called when the network layer detects a connection status change.
135 void OnConnectionStatusChange();
136 43
137 // SyncSession::Delegate implementation. 44 // SyncSession::Delegate implementation.
138 virtual void OnSilencedUntil( 45 virtual void OnSilencedUntil(
139 const base::TimeTicks& silenced_until) OVERRIDE; 46 const base::TimeTicks& silenced_until) OVERRIDE;
140 virtual bool IsSyncingCurrentlySilenced() OVERRIDE; 47 virtual bool IsSyncingCurrentlySilenced() OVERRIDE;
141 virtual void OnReceivedShortPollIntervalUpdate( 48 virtual void OnReceivedShortPollIntervalUpdate(
142 const base::TimeDelta& new_interval) OVERRIDE; 49 const base::TimeDelta& new_interval) OVERRIDE;
143 virtual void OnReceivedLongPollIntervalUpdate( 50 virtual void OnReceivedLongPollIntervalUpdate(
144 const base::TimeDelta& new_interval) OVERRIDE; 51 const base::TimeDelta& new_interval) OVERRIDE;
145 virtual void OnReceivedSessionsCommitDelay( 52 virtual void OnReceivedSessionsCommitDelay(
146 const base::TimeDelta& new_delay) OVERRIDE; 53 const base::TimeDelta& new_delay) OVERRIDE;
147 virtual void OnShouldStopSyncingPermanently() OVERRIDE; 54 virtual void OnShouldStopSyncingPermanently() OVERRIDE;
148 virtual void OnSyncProtocolError( 55 virtual void OnSyncProtocolError(
149 const sessions::SyncSessionSnapshot& snapshot) OVERRIDE; 56 const sessions::SyncSessionSnapshot& snapshot) OVERRIDE;
150 57
58 // DDOS avoidance function. Calculates how long we should wait before trying
59 // again after a failed sync attempt, where the last delay was |base_delay|.
60 // TODO(tim): Look at URLRequestThrottlerEntryInterface.
61 static base::TimeDelta GetRecommendedDelay(const base::TimeDelta& base_delay);
62
151 private: 63 private:
152 enum JobProcessDecision { 64 enum JobProcessDecision {
153 // Indicates we should continue with the current job. 65 // Indicates we should continue with the current job.
154 CONTINUE, 66 CONTINUE,
155 // Indicates that we should save it to be processed later. 67 // Indicates that we should save it to be processed later.
156 SAVE, 68 SAVE,
157 // Indicates we should drop this job. 69 // Indicates we should drop this job.
158 DROP, 70 DROP,
159 }; 71 };
160 72
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 WaitInterval(Mode mode, base::TimeDelta length); 159 WaitInterval(Mode mode, base::TimeDelta length);
248 160
249 static const char* GetModeString(Mode mode); 161 static const char* GetModeString(Mode mode);
250 162
251 Mode mode; 163 Mode mode;
252 164
253 // This bool is set to true if we have observed a nudge during this 165 // This bool is set to true if we have observed a nudge during this
254 // interval and mode == EXPONENTIAL_BACKOFF. 166 // interval and mode == EXPONENTIAL_BACKOFF.
255 bool had_nudge; 167 bool had_nudge;
256 base::TimeDelta length; 168 base::TimeDelta length;
257 base::OneShotTimer<SyncScheduler> timer; 169 base::OneShotTimer<SyncSchedulerImpl> timer;
258 170
259 // Configure jobs are saved only when backing off or throttling. So we 171 // Configure jobs are saved only when backing off or throttling. So we
260 // expose the pointer here. 172 // expose the pointer here.
261 scoped_ptr<SyncSessionJob> pending_configure_job; 173 scoped_ptr<SyncSessionJob> pending_configure_job;
262 }; 174 };
263 175
264 static const char* GetModeString(Mode mode); 176 static const char* GetModeString(Mode mode);
265 177
266 static const char* GetDecisionString(JobProcessDecision decision); 178 static const char* GetDecisionString(JobProcessDecision decision);
267 179
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
363 void UpdateServerConnectionManagerStatus( 275 void UpdateServerConnectionManagerStatus(
364 HttpResponse::ServerConnectionCode code); 276 HttpResponse::ServerConnectionCode code);
365 277
366 // Called once the first time thread_ is started to broadcast an initial 278 // Called once the first time thread_ is started to broadcast an initial
367 // session snapshot containing data like initial_sync_ended. Important when 279 // session snapshot containing data like initial_sync_ended. Important when
368 // the client starts up and does not need to perform an initial sync. 280 // the client starts up and does not need to perform an initial sync.
369 void SendInitialSnapshot(); 281 void SendInitialSnapshot();
370 282
371 virtual void OnActionableError(const sessions::SyncSessionSnapshot& snapshot); 283 virtual void OnActionableError(const sessions::SyncSessionSnapshot& snapshot);
372 284
373 base::WeakPtrFactory<SyncScheduler> weak_ptr_factory_; 285 base::WeakPtrFactory<SyncSchedulerImpl> weak_ptr_factory_;
374 286
375 // A second factory specially for weak_handle_this_, to allow the handle 287 // A second factory specially for weak_handle_this_, to allow the handle
376 // to be const and alleviate threading concerns. 288 // to be const and alleviate threading concerns.
377 base::WeakPtrFactory<SyncScheduler> weak_ptr_factory_for_weak_handle_; 289 base::WeakPtrFactory<SyncSchedulerImpl> weak_ptr_factory_for_weak_handle_;
378 290
379 // For certain methods that need to worry about X-thread posting. 291 // For certain methods that need to worry about X-thread posting.
380 const WeakHandle<SyncScheduler> weak_handle_this_; 292 const WeakHandle<SyncSchedulerImpl> weak_handle_this_;
381 293
382 // Used for logging. 294 // Used for logging.
383 const std::string name_; 295 const std::string name_;
384 296
385 // The message loop this object is on. Almost all methods have to 297 // The message loop this object is on. Almost all methods have to
386 // be called on this thread. 298 // be called on this thread.
387 MessageLoop* const sync_loop_; 299 MessageLoop* const sync_loop_;
388 300
389 // Set in Start(), unset in Stop(). 301 // Set in Start(), unset in Stop().
390 bool started_; 302 bool started_;
391 303
392 // Modifiable versions of kDefaultLongPollIntervalSeconds which can be 304 // Modifiable versions of kDefaultLongPollIntervalSeconds which can be
393 // updated by the server. 305 // updated by the server.
394 base::TimeDelta syncer_short_poll_interval_seconds_; 306 base::TimeDelta syncer_short_poll_interval_seconds_;
395 base::TimeDelta syncer_long_poll_interval_seconds_; 307 base::TimeDelta syncer_long_poll_interval_seconds_;
396 308
397 // Server-tweakable sessions commit delay. 309 // Server-tweakable sessions commit delay.
398 base::TimeDelta sessions_commit_delay_; 310 base::TimeDelta sessions_commit_delay_;
399 311
400 // Periodic timer for polling. See AdjustPolling. 312 // Periodic timer for polling. See AdjustPolling.
401 base::RepeatingTimer<SyncScheduler> poll_timer_; 313 base::RepeatingTimer<SyncSchedulerImpl> poll_timer_;
402 314
403 // The mode of operation. 315 // The mode of operation.
404 Mode mode_; 316 Mode mode_;
405 317
406 // TODO(tim): Bug 26339. This needs to track more than just time I think, 318 // TODO(tim): Bug 26339. This needs to track more than just time I think,
407 // since the nudges could be for different types. Current impl doesn't care. 319 // since the nudges could be for different types. Current impl doesn't care.
408 base::TimeTicks last_sync_session_end_time_; 320 base::TimeTicks last_sync_session_end_time_;
409 321
410 // The latest connection code we got while trying to connect. 322 // The latest connection code we got while trying to connect.
411 HttpResponse::ServerConnectionCode connection_code_; 323 HttpResponse::ServerConnectionCode connection_code_;
412 324
413 // Tracks in-flight nudges so we can coalesce. 325 // Tracks in-flight nudges so we can coalesce.
414 scoped_ptr<SyncSessionJob> pending_nudge_; 326 scoped_ptr<SyncSessionJob> pending_nudge_;
415 327
416 // Current wait state. Null if we're not in backoff and not throttled. 328 // Current wait state. Null if we're not in backoff and not throttled.
417 scoped_ptr<WaitInterval> wait_interval_; 329 scoped_ptr<WaitInterval> wait_interval_;
418 330
419 scoped_ptr<DelayProvider> delay_provider_; 331 scoped_ptr<DelayProvider> delay_provider_;
420 332
421 // Invoked to run through the sync cycle. 333 // Invoked to run through the sync cycle.
422 scoped_ptr<Syncer> syncer_; 334 scoped_ptr<Syncer> syncer_;
423 335
424 sessions::SyncSessionContext *session_context_; 336 sessions::SyncSessionContext *session_context_;
425 337
426 DISALLOW_COPY_AND_ASSIGN(SyncScheduler); 338 DISALLOW_COPY_AND_ASSIGN(SyncSchedulerImpl);
427 }; 339 };
428 340
429 } // namespace syncer 341 } // namespace syncer
430 342
431 #endif // SYNC_ENGINE_SYNC_SCHEDULER_H_ 343 #endif // SYNC_ENGINE_SYNC_SCHEDULER_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698