sync_process_runner.h revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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 protected:
39  void OnChangesUpdated(int64 pending_changes);
40  SyncFileSystemService* sync_service() { return sync_service_; }
41
42  // Returns the current service state.  Default implementation returns
43  // sync_service()->GetSyncServiceState().
44  virtual SyncServiceState GetServiceState();
45
46 private:
47  void Finished(SyncStatusCode status);
48  void Run();
49  void ScheduleInternal(int64 delay);
50
51  std::string name_;
52  SyncFileSystemService* sync_service_;
53  base::OneShotTimer<SyncProcessRunner> timer_;
54  base::Time last_scheduled_;
55  int64 current_delay_;
56  int64 last_delay_;
57  int64 pending_changes_;
58  bool running_;
59  base::WeakPtrFactory<SyncProcessRunner> factory_;
60
61  DISALLOW_COPY_AND_ASSIGN(SyncProcessRunner);
62};
63
64}  // namespace sync_file_system
65
66#endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNC_PROCESS_RUNNER_H_
67