• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 namespace sync_file_system {
17 
18 class 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.
26 class SyncProcessRunner {
27  public:
28   typedef base::Callback<void(const SyncStatusCallback&)> Task;
29   SyncProcessRunner(const std::string& name,
30                     SyncFileSystemService* sync_service);
31   virtual ~SyncProcessRunner();
32 
33   // Subclass must implement this.
34   virtual void StartSync(const SyncStatusCallback& callback) = 0;
35 
36   // Schedules a new sync.
37   void Schedule();
38   void ScheduleIfNotRunning();
39 
pending_changes()40   int64 pending_changes() const { return pending_changes_; }
41 
42  protected:
43   void OnChangesUpdated(int64 pending_changes);
sync_service()44   SyncFileSystemService* sync_service() { return sync_service_; }
45 
46   // Returns the current service state.  Default implementation returns
47   // sync_service()->GetSyncServiceState().
48   virtual SyncServiceState GetServiceState();
49 
50  private:
51   void Finished(SyncStatusCode status);
52   void Run();
53   void ScheduleInternal(int64 delay);
54 
55   std::string name_;
56   SyncFileSystemService* sync_service_;
57   base::OneShotTimer<SyncProcessRunner> timer_;
58   base::Time last_scheduled_;
59   int64 current_delay_;
60   int64 last_delay_;
61   int64 pending_changes_;
62   bool running_;
63   base::WeakPtrFactory<SyncProcessRunner> factory_;
64 
65   DISALLOW_COPY_AND_ASSIGN(SyncProcessRunner);
66 };
67 
68 }  // namespace sync_file_system
69 
70 #endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNC_PROCESS_RUNNER_H_
71