sync_process_runner.h revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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 "base/basictypes.h"
9#include "base/memory/weak_ptr.h"
10#include "base/timer/timer.h"
11#include "chrome/browser/sync_file_system/sync_callbacks.h"
12#include "chrome/browser/sync_file_system/sync_service_state.h"
13
14namespace sync_file_system {
15
16class SyncFileSystemService;
17
18// A base class to schedule a sync.
19// Each subclass must implement StartSync().
20// An instance of this class is supposed to be owned by SyncFileSystemService.
21//
22// Note that multiple SyncProcessRunner doesn't coordinate its sync schedule
23// with each other.
24class SyncProcessRunner {
25 public:
26  typedef base::Callback<void(const SyncStatusCallback&)> Task;
27  SyncProcessRunner(const std::string& name,
28                    SyncFileSystemService* sync_service);
29  virtual ~SyncProcessRunner();
30
31  // Subclass must implement this.
32  virtual void StartSync(const SyncStatusCallback& callback) = 0;
33
34  // Schedules a new sync.
35  void Schedule();
36  void ScheduleIfNotRunning();
37
38  int64 pending_changes() const { return pending_changes_; }
39
40 protected:
41  void OnChangesUpdated(int64 pending_changes);
42  SyncFileSystemService* sync_service() { return sync_service_; }
43
44  // Returns the current service state.  Default implementation returns
45  // sync_service()->GetSyncServiceState().
46  virtual SyncServiceState GetServiceState();
47
48 private:
49  void Finished(SyncStatusCode status);
50  void Run();
51  void ScheduleInternal(int64 delay);
52
53  std::string name_;
54  SyncFileSystemService* sync_service_;
55  base::OneShotTimer<SyncProcessRunner> timer_;
56  base::Time last_scheduled_;
57  int64 current_delay_;
58  int64 last_delay_;
59  int64 pending_changes_;
60  bool running_;
61  base::WeakPtrFactory<SyncProcessRunner> factory_;
62
63  DISALLOW_COPY_AND_ASSIGN(SyncProcessRunner);
64};
65
66}  // namespace sync_file_system
67
68#endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNC_PROCESS_RUNNER_H_
69