ExtensionSyncService: listen for relevant changes instead of being explicitly called...
[chromium-blink-merge.git] / chrome / browser / sync_file_system / sync_process_runner.h
blob118ca70917e89177ecc92ff85f804e586209847a
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_
8 #include <string>
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
25 // with each other.
26 class SyncProcessRunner {
27 public:
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
32 // is running).
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;
46 class Client {
47 public:
48 virtual ~Client() {}
49 virtual void OnSyncIdle() {}
50 virtual SyncServiceState GetSyncServiceState() = 0;
51 virtual SyncFileSystemService* GetSyncService() = 0;
54 class TimerHelper {
55 public:
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;
63 protected:
64 TimerHelper() {}
67 SyncProcessRunner(const std::string& name,
68 Client* client,
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.
77 void Schedule();
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();
85 protected:
86 void OnChangesUpdated(int64 pending_changes);
87 SyncFileSystemService* GetSyncService();
89 private:
90 void Finished(const base::TimeTicks& start_time, SyncStatusCode status);
91 void Run();
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();
102 void CheckIfIdle();
104 std::string name_;
105 Client* client_;
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_