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.
4
5#ifndef CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNC_PROCESS_RUNNER_H_
6#define CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNC_PROCESS_RUNNER_H_
7
8#include <string>
9
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"
15
16namespace sync_file_system {
17
18class SyncFileSystemService;
19
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.
23//
24// Note that multiple SyncProcessRunner doesn't coordinate its sync schedule
25// with each other.
26class SyncProcessRunner {
27 public:
28  // Default delay when more changes are available.
29  static const int64 kSyncDelayInMilliseconds;
30
31  // Default delay when the previous change has had an error (but remote service
32  // is running).
33  static const int64 kSyncDelayWithSyncError;
34
35  // Default delay when there're more than 10 pending changes.
36  static const int64 kSyncDelayFastInMilliseconds;
37  static const int kPendingChangeThresholdForFastSync;
38
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;
42
43  // Default delay when there're no changes.
44  static const int64 kSyncDelayMaxInMilliseconds;
45
46  class Client {
47   public:
48    virtual ~Client() {}
49    virtual void OnSyncIdle() {}
50    virtual SyncServiceState GetSyncServiceState() = 0;
51    virtual SyncFileSystemService* GetSyncService() = 0;
52  };
53
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;
62
63   protected:
64    TimerHelper() {}
65  };
66
67  SyncProcessRunner(const std::string& name,
68                    Client* client,
69                    scoped_ptr<TimerHelper> timer_helper,
70                    size_t max_parallel_task);
71  virtual ~SyncProcessRunner();
72
73  // Subclass must implement this.
74  virtual void StartSync(const SyncStatusCallback& callback) = 0;
75
76  // Schedules a new sync.
77  void Schedule();
78
79  int64 pending_changes() const { return pending_changes_; }
80
81  // Returns the current service state.  Default implementation returns
82  // sync_service()->GetSyncServiceState().
83  virtual SyncServiceState GetServiceState();
84
85 protected:
86  void OnChangesUpdated(int64 pending_changes);
87  SyncFileSystemService* GetSyncService();
88
89 private:
90  void Finished(const base::TimeTicks& start_time, SyncStatusCode status);
91  void Run();
92  void ScheduleInternal(int64 delay);
93
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);
97
98  // Clears old throttling setting that is already over.
99  void ResetOldThrottling();
100  void ResetThrottling();
101
102  void CheckIfIdle();
103
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_;
112
113  base::TimeTicks throttle_from_;
114  base::TimeTicks throttle_until_;
115
116  int64 pending_changes_;
117  base::WeakPtrFactory<SyncProcessRunner> factory_;
118
119  DISALLOW_COPY_AND_ASSIGN(SyncProcessRunner);
120};
121
122}  // namespace sync_file_system
123
124#endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNC_PROCESS_RUNNER_H_
125