1 // Copyright 2013 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 #ifndef CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNC_PROCESS_RUNNER_H_
6 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNC_PROCESS_RUNNER_H_
10 #include "base/basictypes.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/timer/timer.h"
13 #include "chrome/browser/sync_file_system/sync_callbacks.h"
14 #include "chrome/browser/sync_file_system/sync_service_state.h"
16 namespace sync_file_system {
18 class SyncFileSystemService;
20 // A base class to schedule a sync.
21 // Each subclass must implement StartSync().
22 // An instance of this class is supposed to be owned by SyncFileSystemService.
24 // Note that multiple SyncProcessRunner doesn't coordinate its sync schedule
26 class SyncProcessRunner {
28 // Default delay when more changes are available.
29 static const int64 kSyncDelayInMilliseconds;
31 // Default delay when the previous change has had an error (but remote service
33 static const int64 kSyncDelayWithSyncError;
35 // Default delay when there're more than 10 pending changes.
36 static const int64 kSyncDelayFastInMilliseconds;
37 static const int kPendingChangeThresholdForFastSync;
39 // Default delay when remote service is temporarily unavailable.
40 // The delay backs off exponentially from initial value on repeated failure.
41 static const int64 kSyncDelaySlowInMilliseconds;
43 // Default delay when there're no changes.
44 static const int64 kSyncDelayMaxInMilliseconds;
49 virtual void OnSyncIdle() {}
50 virtual SyncServiceState GetSyncServiceState() = 0;
51 virtual SyncFileSystemService* GetSyncService() = 0;
56 virtual ~TimerHelper() {}
57 virtual bool IsRunning() = 0;
58 virtual void Start(const tracked_objects::Location& from_here,
59 const base::TimeDelta& delay,
60 const base::Closure& closure) = 0;
61 virtual base::TimeTicks Now() const = 0;
67 SyncProcessRunner(const std::string& name,
69 scoped_ptr<TimerHelper> timer_helper,
70 size_t max_parallel_task);
71 virtual ~SyncProcessRunner();
73 // Subclass must implement this.
74 virtual void StartSync(const SyncStatusCallback& callback) = 0;
76 // Schedules a new sync.
79 int64 pending_changes() const { return pending_changes_; }
81 // Returns the current service state. Default implementation returns
82 // sync_service()->GetSyncServiceState().
83 virtual SyncServiceState GetServiceState();
86 void OnChangesUpdated(int64 pending_changes);
87 SyncFileSystemService* GetSyncService();
90 void Finished(const base::TimeTicks& start_time, SyncStatusCode status);
92 void ScheduleInternal(int64 delay);
94 // Throttles new sync for |base_delay| milliseconds for an error case.
95 // If new sync is already throttled, back off the duration.
96 void ThrottleSync(int64 base_delay);
98 // Clears old throttling setting that is already over.
99 void ResetOldThrottling();
100 void ResetThrottling();
106 size_t max_parallel_task_;
107 size_t running_tasks_;
108 scoped_ptr<TimerHelper> timer_helper_;
109 base::TimeTicks last_run_;
110 base::TimeTicks last_scheduled_;
111 SyncServiceState service_state_;
113 base::TimeTicks throttle_from_;
114 base::TimeTicks throttle_until_;
116 int64 pending_changes_;
117 base::WeakPtrFactory<SyncProcessRunner> factory_;
119 DISALLOW_COPY_AND_ASSIGN(SyncProcessRunner);
122 } // namespace sync_file_system
124 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNC_PROCESS_RUNNER_H_