Make castv2 performance test work.
[chromium-blink-merge.git] / components / domain_reliability / context.h
blob528c8ab168721174099e89acf9c9b4acaac63621
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_CONTEXT_H_
6 #define COMPONENTS_DOMAIN_RELIABILITY_CONTEXT_H_
8 #include <deque>
9 #include <vector>
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/time/time.h"
14 #include "components/domain_reliability/beacon.h"
15 #include "components/domain_reliability/config.h"
16 #include "components/domain_reliability/domain_reliability_export.h"
17 #include "components/domain_reliability/scheduler.h"
18 #include "components/domain_reliability/uploader.h"
20 class GURL;
22 namespace base {
23 class Value;
26 namespace domain_reliability {
28 class DomainReliabilityDispatcher;
29 class DomainReliabilityUploader;
30 class MockableTime;
32 // The per-domain context for the Domain Reliability client; includes the
33 // domain's config and per-resource beacon queues.
34 class DOMAIN_RELIABILITY_EXPORT DomainReliabilityContext {
35 public:
36 class DOMAIN_RELIABILITY_EXPORT Factory {
37 public:
38 virtual ~Factory();
39 virtual scoped_ptr<DomainReliabilityContext> CreateContextForConfig(
40 scoped_ptr<const DomainReliabilityConfig> config) = 0;
43 DomainReliabilityContext(
44 MockableTime* time,
45 const DomainReliabilityScheduler::Params& scheduler_params,
46 const std::string& upload_reporter_string,
47 const base::TimeTicks* last_network_change_time,
48 DomainReliabilityDispatcher* dispatcher,
49 DomainReliabilityUploader* uploader,
50 scoped_ptr<const DomainReliabilityConfig> config);
51 ~DomainReliabilityContext();
53 // Notifies the context of a beacon on its domain(s); may or may not save the
54 // actual beacon to be uploaded, depending on the sample rates in the config,
55 // but will increment one of the request counters in any case.
56 void OnBeacon(const GURL& url, const DomainReliabilityBeacon& beacon);
58 // Called to clear browsing data, since beacons are like browsing history.
59 void ClearBeacons();
61 // Gets a Value containing data that can be formatted into a web page for
62 // debugging purposes.
63 scoped_ptr<base::Value> GetWebUIData() const;
65 void GetQueuedBeaconsForTesting(
66 std::vector<DomainReliabilityBeacon>* beacons_out) const;
68 void GetRequestCountsForTesting(
69 size_t resource_index,
70 uint32* successful_requests_out,
71 uint32* failed_requests_out) const;
73 const DomainReliabilityConfig& config() const { return *config_.get(); }
75 // Maximum number of beacons queued per context; if more than this many are
76 // queued; the oldest beacons will be removed.
77 static const size_t kMaxQueuedBeacons;
79 private:
80 class ResourceState;
82 typedef std::deque<DomainReliabilityBeacon> BeaconDeque;
83 typedef ScopedVector<ResourceState> ResourceStateVector;
84 typedef ResourceStateVector::const_iterator ResourceStateIterator;
86 void InitializeResourceStates();
87 void ScheduleUpload(base::TimeDelta min_delay, base::TimeDelta max_delay);
88 void StartUpload();
89 void OnUploadComplete(const DomainReliabilityUploader::UploadResult& result);
91 scoped_ptr<const base::Value> CreateReport(base::TimeTicks upload_time) const;
93 // Remembers the current state of the context when an upload starts. Can be
94 // called multiple times in a row (without |CommitUpload|) if uploads fail
95 // and are retried.
96 void MarkUpload();
98 // Uses the state remembered by |MarkUpload| to remove successfully uploaded
99 // data but keep beacons and request counts added after the upload started.
100 void CommitUpload();
102 void RollbackUpload();
104 // Finds and removes the oldest beacon. DCHECKs if there is none. (Called
105 // when there are too many beacons queued.)
106 void RemoveOldestBeacon();
108 scoped_ptr<const DomainReliabilityConfig> config_;
109 MockableTime* time_;
110 const std::string& upload_reporter_string_;
111 DomainReliabilityScheduler scheduler_;
112 DomainReliabilityDispatcher* dispatcher_;
113 DomainReliabilityUploader* uploader_;
115 BeaconDeque beacons_;
116 size_t uploading_beacons_size_;
117 // Each ResourceState in |states_| corresponds to the Resource of the same
118 // index in the config.
119 ResourceStateVector states_;
120 base::TimeTicks upload_time_;
121 base::TimeTicks last_upload_time_;
122 // The last network change time is not tracked per-context, so this is a
123 // pointer to that value in a wider (e.g. per-Monitor or unittest) scope.
124 const base::TimeTicks* last_network_change_time_;
126 base::WeakPtrFactory<DomainReliabilityContext> weak_factory_;
128 DISALLOW_COPY_AND_ASSIGN(DomainReliabilityContext);
131 } // namespace domain_reliability
133 #endif // COMPONENTS_DOMAIN_RELIABILITY_CONTEXT_H_