File: sync_process_runner.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (131 lines) | stat: -rw-r--r-- 4,036 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNC_PROCESS_RUNNER_H_
#define CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNC_PROCESS_RUNNER_H_

#include <stddef.h>
#include <stdint.h>

#include <memory>
#include <string>

#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "chrome/browser/sync_file_system/sync_callbacks.h"
#include "chrome/browser/sync_file_system/sync_service_state.h"

namespace sync_file_system {

class SyncFileSystemService;

// A base class to schedule a sync.
// Each subclass must implement StartSync().
// An instance of this class is supposed to be owned by SyncFileSystemService.
//
// Note that multiple SyncProcessRunner doesn't coordinate its sync schedule
// with each other.
class SyncProcessRunner {
 public:
  // Default delay when more changes are available.
  static const int64_t kSyncDelayInMilliseconds;

  // Default delay when the previous change has had an error (but remote service
  // is running).
  static const int64_t kSyncDelayWithSyncError;

  // Default delay when there're more than 10 pending changes.
  static const int64_t kSyncDelayFastInMilliseconds;
  static const int kPendingChangeThresholdForFastSync;

  // Default delay when remote service is temporarily unavailable.
  // The delay backs off exponentially from initial value on repeated failure.
  static const int64_t kSyncDelaySlowInMilliseconds;

  // Default delay when there're no changes.
  static const int64_t kSyncDelayMaxInMilliseconds;

  class Client {
   public:
    virtual ~Client() = default;
    virtual void OnSyncIdle() {}
    virtual SyncServiceState GetSyncServiceState() = 0;
    virtual SyncFileSystemService* GetSyncService() = 0;
  };

  class TimerHelper {
   public:
    virtual ~TimerHelper() = default;
    virtual bool IsRunning() = 0;
    virtual void Start(const base::Location& from_here,
                       const base::TimeDelta& delay,
                       base::OnceClosure closure) = 0;
    virtual base::TimeTicks Now() const = 0;

   protected:
    TimerHelper() = default;
  };

  SyncProcessRunner(const std::string& name,
                    Client* client,
                    std::unique_ptr<TimerHelper> timer_helper,
                    size_t max_parallel_task);

  SyncProcessRunner(const SyncProcessRunner&) = delete;
  SyncProcessRunner& operator=(const SyncProcessRunner&) = delete;

  virtual ~SyncProcessRunner();

  // Subclass must implement this.
  virtual void StartSync(SyncStatusCallback callback) = 0;

  // Schedules a new sync.
  void Schedule();

  int64_t pending_changes() const { return pending_changes_; }

  // Returns the current service state.  Default implementation returns
  // sync_service()->GetSyncServiceState().
  virtual SyncServiceState GetServiceState();

 protected:
  void OnChangesUpdated(int64_t pending_changes);
  SyncFileSystemService* GetSyncService();

 private:
  void Finished(const base::TimeTicks& start_time, SyncStatusCode status);
  void Run();
  void ScheduleInternal(int64_t delay);

  // Throttles new sync for |base_delay| milliseconds for an error case.
  // If new sync is already throttled, back off the duration.
  void ThrottleSync(int64_t base_delay);

  // Clears old throttling setting that is already over.
  void ResetOldThrottling();
  void ResetThrottling();

  void CheckIfIdle();

  std::string name_;
  raw_ptr<Client> client_;
  size_t max_parallel_task_;
  size_t running_tasks_;
  std::unique_ptr<TimerHelper> timer_helper_;
  base::TimeTicks last_run_;
  base::TimeTicks last_scheduled_;
  SyncServiceState service_state_;

  base::TimeTicks throttle_from_;
  base::TimeTicks throttle_until_;

  int64_t pending_changes_;
  base::WeakPtrFactory<SyncProcessRunner> factory_{this};
};

}  // namespace sync_file_system

#endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNC_PROCESS_RUNNER_H_