OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_DOMAIN_RELIABILITY_CONTEXT_H_ | 5 #ifndef COMPONENTS_DOMAIN_RELIABILITY_CONTEXT_H_ |
6 #define COMPONENTS_DOMAIN_RELIABILITY_CONTEXT_H_ | 6 #define COMPONENTS_DOMAIN_RELIABILITY_CONTEXT_H_ |
7 | 7 |
8 #include <deque> | 8 #include <deque> |
9 #include <map> | 9 #include <vector> |
10 | 10 |
11 #include "base/memory/ref_counted.h" | |
12 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
13 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
14 #include "base/time/time.h" | 13 #include "base/time/time.h" |
15 #include "components/domain_reliability/beacon.h" | |
16 #include "components/domain_reliability/config.h" | 14 #include "components/domain_reliability/config.h" |
17 #include "components/domain_reliability/domain_reliability_export.h" | 15 #include "components/domain_reliability/domain_reliability_export.h" |
18 #include "components/domain_reliability/scheduler.h" | 16 #include "components/domain_reliability/scheduler.h" |
19 #include "components/domain_reliability/uploader.h" | 17 #include "components/domain_reliability/uploader.h" |
20 #include "components/domain_reliability/util.h" | 18 #include "components/domain_reliability/util.h" |
21 | 19 |
22 class GURL; | 20 class GURL; |
23 | 21 |
24 namespace domain_reliability { | 22 namespace domain_reliability { |
25 | 23 |
| 24 struct DomainReliabilityBeacon; |
26 class DomainReliabilityDispatcher; | 25 class DomainReliabilityDispatcher; |
27 class MockableTime; | 26 class MockableTime; |
28 | 27 |
29 // The per-domain context for the Domain Reliability client; includes the | 28 // The per-domain context for the Domain Reliability client; includes the |
30 // domain's config and per-resource beacon queues. | 29 // domain's config and per-resource beacon queues. |
31 class DOMAIN_RELIABILITY_EXPORT DomainReliabilityContext { | 30 class DOMAIN_RELIABILITY_EXPORT DomainReliabilityContext { |
32 public: | 31 public: |
33 DomainReliabilityContext( | 32 DomainReliabilityContext( |
34 MockableTime* time, | 33 MockableTime* time, |
35 const DomainReliabilityScheduler::Params& scheduler_params, | 34 const DomainReliabilityScheduler::Params& scheduler_params, |
| 35 const char* upload_reporter_string, |
36 DomainReliabilityDispatcher* dispatcher, | 36 DomainReliabilityDispatcher* dispatcher, |
37 DomainReliabilityUploader* uploader, | 37 DomainReliabilityUploader* uploader, |
38 scoped_ptr<const DomainReliabilityConfig> config); | 38 scoped_ptr<const DomainReliabilityConfig> config); |
39 virtual ~DomainReliabilityContext(); | 39 ~DomainReliabilityContext(); |
40 | 40 |
41 void AddBeacon(const DomainReliabilityBeacon& beacon, const GURL& url); | 41 // Notifies the context of a beacon on its domain(s); may or may not save the |
| 42 // actual beacon to be uploaded, depending on the sample rates in the config, |
| 43 // but will increment one of the request counters in any case. |
| 44 void OnBeacon(const GURL& url, const DomainReliabilityBeacon& beacon); |
42 | 45 |
43 void GetQueuedDataForTesting( | 46 void GetQueuedDataForTesting( |
44 int resource_index, | 47 size_t resource_index, |
45 std::vector<DomainReliabilityBeacon>* beacons_out, | 48 std::vector<DomainReliabilityBeacon>* beacons_out, |
46 int* successful_requests_out, | 49 unsigned* successful_requests_out, |
47 int* failed_requests_out) const; | 50 unsigned* failed_requests_out) const; |
48 | 51 |
49 const DomainReliabilityConfig& config() { return *config_.get(); } | 52 const DomainReliabilityConfig& config() { return *config_.get(); } |
50 | 53 |
51 // Maximum number of beacons queued per context; if more than this many are | 54 // Maximum number of beacons queued per context; if more than this many are |
52 // queued; the oldest beacons will be removed. | 55 // queued; the oldest beacons will be removed. |
53 static const int kMaxQueuedBeacons; | 56 static const size_t kMaxQueuedBeacons; |
54 | 57 |
55 private: | 58 private: |
56 // Resource-specific state (queued beacons and request counts). | 59 class ResourceState; |
57 class ResourceState { | |
58 public: | |
59 ResourceState(DomainReliabilityContext* context, | |
60 const DomainReliabilityConfig::Resource* config); | |
61 ~ResourceState(); | |
62 | |
63 scoped_ptr<base::Value> ToValue(base::TimeTicks upload_time) const; | |
64 | |
65 // Remembers the current state of the resource data when an upload starts. | |
66 void MarkUpload(); | |
67 | |
68 // Uses the state remembered by |MarkUpload| to remove successfully uploaded | |
69 // data but keep beacons and request counts added after the upload started. | |
70 void CommitUpload(); | |
71 | |
72 // Gets the start time of the oldest beacon, if there are any. Returns true | |
73 // and sets |oldest_start_out| if so; otherwise, returns false. | |
74 bool GetOldestBeaconStart(base::TimeTicks* oldest_start_out) const; | |
75 // Removes the oldest beacon. DCHECKs if there isn't one. | |
76 void RemoveOldestBeacon(); | |
77 | |
78 DomainReliabilityContext* context; | |
79 const DomainReliabilityConfig::Resource* config; | |
80 | |
81 std::deque<DomainReliabilityBeacon> beacons; | |
82 int successful_requests; | |
83 int failed_requests; | |
84 | |
85 // State saved during uploads; if an upload succeeds, these are used to | |
86 // remove uploaded data from the beacon list and request counters. | |
87 size_t uploading_beacons_size; | |
88 int uploading_successful_requests; | |
89 int uploading_failed_requests; | |
90 | |
91 private: | |
92 DISALLOW_COPY_AND_ASSIGN(ResourceState); | |
93 }; | |
94 | 60 |
95 typedef ScopedVector<ResourceState> ResourceStateVector; | 61 typedef ScopedVector<ResourceState> ResourceStateVector; |
96 typedef ResourceStateVector::const_iterator ResourceStateIterator; | 62 typedef ResourceStateVector::const_iterator ResourceStateIterator; |
97 | 63 |
98 void InitializeResourceStates(); | 64 void InitializeResourceStates(); |
99 void ScheduleUpload(base::TimeDelta min_delay, base::TimeDelta max_delay); | 65 void ScheduleUpload(base::TimeDelta min_delay, base::TimeDelta max_delay); |
100 void StartUpload(); | 66 void StartUpload(); |
101 void OnUploadComplete(bool success); | 67 void OnUploadComplete(bool success); |
102 | 68 |
103 scoped_ptr<const base::Value> CreateReport(base::TimeTicks upload_time) const; | 69 scoped_ptr<const base::Value> CreateReport(base::TimeTicks upload_time) const; |
104 | 70 |
105 // Remembers the current state of the context when an upload starts. | 71 // Remembers the current state of the context when an upload starts. |
106 void MarkUpload(); | 72 void MarkUpload(); |
107 | 73 |
108 // Uses the state remembered by |MarkUpload| to remove successfully uploaded | 74 // Uses the state remembered by |MarkUpload| to remove successfully uploaded |
109 // data but keep beacons and request counts added after the upload started. | 75 // data but keep beacons and request counts added after the upload started. |
110 void CommitUpload(); | 76 void CommitUpload(); |
111 | 77 |
112 // Finds and removes the oldest beacon. DCHECKs if there is none. (Called | 78 // Finds and removes the oldest beacon. DCHECKs if there is none. (Called |
113 // when there are too many beacons queued.) | 79 // when there are too many beacons queued.) |
114 void RemoveOldestBeacon(); | 80 void RemoveOldestBeacon(); |
115 | 81 |
116 scoped_ptr<const DomainReliabilityConfig> config_; | 82 scoped_ptr<const DomainReliabilityConfig> config_; |
117 MockableTime* time_; | 83 MockableTime* time_; |
| 84 const char* upload_reporter_string_; |
118 DomainReliabilityScheduler scheduler_; | 85 DomainReliabilityScheduler scheduler_; |
119 DomainReliabilityDispatcher* dispatcher_; | 86 DomainReliabilityDispatcher* dispatcher_; |
120 DomainReliabilityUploader* uploader_; | 87 DomainReliabilityUploader* uploader_; |
121 | 88 |
122 // Each ResourceState in |states_| corresponds to the Resource of the same | 89 // Each ResourceState in |states_| corresponds to the Resource of the same |
123 // index in the config. | 90 // index in the config. |
124 ResourceStateVector states_; | 91 ResourceStateVector states_; |
125 int beacon_count_; | 92 size_t beacon_count_; |
126 int uploading_beacon_count_; | 93 size_t uploading_beacon_count_; |
127 base::TimeTicks upload_time_; | 94 base::TimeTicks upload_time_; |
128 base::TimeTicks last_upload_time_; | 95 base::TimeTicks last_upload_time_; |
129 | 96 |
130 base::WeakPtrFactory<DomainReliabilityContext> weak_factory_; | 97 base::WeakPtrFactory<DomainReliabilityContext> weak_factory_; |
131 | 98 |
132 DISALLOW_COPY_AND_ASSIGN(DomainReliabilityContext); | 99 DISALLOW_COPY_AND_ASSIGN(DomainReliabilityContext); |
133 }; | 100 }; |
134 | 101 |
135 } // namespace domain_reliability | 102 } // namespace domain_reliability |
136 | 103 |
137 #endif // COMPONENTS_DOMAIN_RELIABILITY_CONTEXT_H_ | 104 #endif // COMPONENTS_DOMAIN_RELIABILITY_CONTEXT_H_ |
OLD | NEW |