1 // Copyright 2014 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 COMPONENTS_DOMAIN_RELIABILITY_SCHEDULER_H_
6 #define COMPONENTS_DOMAIN_RELIABILITY_SCHEDULER_H_
7 
8 #include <stddef.h>
9 
10 #include <memory>
11 #include <vector>
12 
13 #include "base/callback.h"
14 #include "base/time/time.h"
15 #include "components/domain_reliability/domain_reliability_export.h"
16 #include "components/domain_reliability/uploader.h"
17 #include "net/base/backoff_entry.h"
18 
19 namespace base {
20 class Value;
21 }  // namespace base
22 
23 namespace domain_reliability {
24 
25 class MockableTime;
26 
27 // Determines when an upload should be scheduled. A domain's config will
28 // specify minimum and maximum upload delays; the minimum upload delay ensures
29 // that Chrome will not send too many upload requests to a site by waiting at
30 // least that long after the first beacon, while the maximum upload delay makes
31 // sure the server receives the reports while they are still fresh.
32 //
33 // When everything is working fine, the scheduler will return precisely that
34 // interval. If all uploaders have failed, then the beginning or ending points
35 // of the interval may be pushed later to accomodate the retry with exponential
36 // backoff.
37 //
38 // See dispatcher.h for an explanation of what happens with the scheduled
39 // interval.
40 class DOMAIN_RELIABILITY_EXPORT DomainReliabilityScheduler {
41  public:
42   typedef base::RepeatingCallback<void(base::TimeDelta, base::TimeDelta)>
43       ScheduleUploadCallback;
44 
45   struct Params {
46    public:
47     base::TimeDelta minimum_upload_delay;
48     base::TimeDelta maximum_upload_delay;
49     base::TimeDelta upload_retry_interval;
50 
51     static Params GetFromFieldTrialsOrDefaults();
52   };
53 
54   DomainReliabilityScheduler(const MockableTime* time,
55                              size_t num_collectors,
56                              const Params& params,
57                              const ScheduleUploadCallback& callback);
58   ~DomainReliabilityScheduler();
59 
60   // If there is no upload pending, schedules an upload based on the provided
61   // parameters (some time between the minimum and maximum delay from now).
62   // May call the ScheduleUploadCallback.
63   void OnBeaconAdded();
64 
65   // Returns which collector to use for an upload that is about to start. Must
66   // be called exactly once during or after the ScheduleUploadCallback but
67   // before OnUploadComplete is called. (Also records the upload start time for
68   // future retries, if the upload ends up failing.)
69   size_t OnUploadStart();
70 
71   // Updates the scheduler state based on the result of an upload. Must be
72   // called exactly once after |OnUploadStart|. |result| should be the result
73   // passed to the upload callback by the Uploader.
74   void OnUploadComplete(const DomainReliabilityUploader::UploadResult& result);
75 
76   std::unique_ptr<base::Value> GetWebUIData() const;
77 
78   // Disables jitter in BackoffEntries to make scheduling deterministic for
79   // unit tests.
80   void MakeDeterministicForTesting();
81 
82  private:
83   void MaybeScheduleUpload();
84 
85   void GetNextUploadTimeAndCollector(base::TimeTicks now,
86                                      base::TimeTicks* upload_time_out,
87                                      size_t* collector_index_out);
88 
89   const MockableTime* time_;
90   Params params_;
91   ScheduleUploadCallback callback_;
92   net::BackoffEntry::Policy backoff_policy_;
93   std::vector<std::unique_ptr<net::BackoffEntry>> collectors_;
94 
95   // Whether there are beacons that have not yet been uploaded. Set when a
96   // beacon arrives or an upload fails, and cleared when an upload starts.
97   bool upload_pending_;
98 
99   // Whether the scheduler has called the ScheduleUploadCallback to schedule
100   // the next upload. Set when an upload is scheduled and cleared when the
101   // upload starts.
102   bool upload_scheduled_;
103 
104   // Whether the last scheduled upload is in progress. Set when the upload
105   // starts and cleared when the upload completes (successfully or not).
106   bool upload_running_;
107 
108   // Index of the collector selected for the next upload.  (Set in
109   // |OnUploadStart| and cleared in |OnUploadComplete|.)
110   size_t collector_index_;
111 
112   // Time of the first beacon that was not included in the last successful
113   // upload.
114   base::TimeTicks first_beacon_time_;
115 
116   // first_beacon_time_ saved during uploads.  Restored if upload fails.
117   base::TimeTicks old_first_beacon_time_;
118 
119   // Extra bits to return in GetWebUIData.
120   base::TimeTicks scheduled_min_time_;
121   base::TimeTicks scheduled_max_time_;
122   // Whether the other last_upload_* fields are populated.
123   bool last_upload_finished_;
124   base::TimeTicks last_upload_start_time_;
125   base::TimeTicks last_upload_end_time_;
126   size_t last_upload_collector_index_;
127   bool last_upload_success_;
128 
129   DISALLOW_COPY_AND_ASSIGN(DomainReliabilityScheduler);
130 };
131 
132 }  // namespace domain_reliability
133 
134 #endif  // COMPONENTS_DOMAIN_RELIABILITY_SCHEDULER_H_
135