Change to let media_stream_devices_controller.cc decide if a gUM request fail if...
[chromium-blink-merge.git] / components / domain_reliability / scheduler.h
blobee924d7d3e0e224cd067dea1e554f695fed57630
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.
5 #ifndef COMPONENTS_DOMAIN_RELIABILITY_SCHEDULER_H_
6 #define COMPONENTS_DOMAIN_RELIABILITY_SCHEDULER_H_
8 #include <vector>
10 #include "base/callback.h"
11 #include "base/time/time.h"
12 #include "components/domain_reliability/domain_reliability_export.h"
14 namespace domain_reliability {
16 class DomainReliabilityConfig;
17 class MockableTime;
19 // Determines when an upload should be scheduled. A domain's config will
20 // specify minimum and maximum upload delays; the minimum upload delay ensures
21 // that Chrome will not send too many upload requests to a site by waiting at
22 // least that long after the first beacon, while the maximum upload delay makes
23 // sure the server receives the reports while they are still fresh.
25 // When everything is working fine, the scheduler will return precisely that
26 // interval. If all uploaders have failed, then the beginning or ending points
27 // of the interval may be pushed later to accomodate the retry with exponential
28 // backoff.
30 // See dispatcher.h for an explanation of what happens with the scheduled
31 // interval.
32 class DOMAIN_RELIABILITY_EXPORT DomainReliabilityScheduler {
33 public:
34 typedef base::Callback<void(base::TimeDelta, base::TimeDelta)>
35 ScheduleUploadCallback;
37 struct Params {
38 public:
39 base::TimeDelta minimum_upload_delay;
40 base::TimeDelta maximum_upload_delay;
41 base::TimeDelta upload_retry_interval;
43 static Params GetFromFieldTrialsOrDefaults();
46 DomainReliabilityScheduler(MockableTime* time,
47 size_t num_collectors,
48 const Params& params,
49 const ScheduleUploadCallback& callback);
50 ~DomainReliabilityScheduler();
52 // Called when a beacon is added to the context. May call the provided
53 // ScheduleUploadCallback (if all previous beacons have been uploaded).
54 void OnBeaconAdded();
56 // Called when an upload is about to start.
57 void OnUploadStart(int* collector_index_out);
59 // Called when an upload has finished. May call the provided
60 // ScheduleUploadCallback (if the upload failed, or if beacons arrived
61 // during the upload).
62 void OnUploadComplete(bool success);
64 private:
65 struct CollectorState {
66 CollectorState();
68 // The number of consecutive failures to upload to this collector, or 0 if
69 // the most recent upload succeeded.
70 int failures;
71 base::TimeTicks next_upload;
74 void MaybeScheduleUpload();
76 void GetNextUploadTimeAndCollector(base::TimeTicks now,
77 base::TimeTicks* upload_time_out,
78 int* collector_index_out);
79 base::TimeDelta GetUploadRetryInterval(int failures);
81 MockableTime* time_;
82 std::vector<CollectorState> collectors_;
83 Params params_;
84 ScheduleUploadCallback callback_;
86 // Whether there are beacons that have not yet been uploaded. Set when a
87 // beacon arrives or an upload fails, and cleared when an upload starts.
88 bool upload_pending_;
90 // Whether the scheduler has called the ScheduleUploadCallback to schedule
91 // the next upload. Set when an upload is scheduled and cleared when the
92 // upload starts.
93 bool upload_scheduled_;
95 // Whether the last scheduled upload is in progress. Set when the upload
96 // starts and cleared when the upload completes (successfully or not).
97 bool upload_running_;
99 // Index of the collector selected for the next upload. (Set in
100 // |OnUploadStart| and cleared in |OnUploadComplete|.)
101 int collector_index_;
103 // Time of the first beacon that was not included in the last successful
104 // upload.
105 base::TimeTicks first_beacon_time_;
107 // first_beacon_time_ saved during uploads. Restored if upload fails.
108 base::TimeTicks old_first_beacon_time_;
111 } // namespace domain_reliability
113 #endif // COMPONENTS_DOMAIN_RELIABILITY_SCHEDULER_H_