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 <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 12 matching lines...) Expand all Loading... |
23 class Value; | 23 class Value; |
24 } | 24 } |
25 | 25 |
26 namespace domain_reliability { | 26 namespace domain_reliability { |
27 | 27 |
28 class DomainReliabilityDispatcher; | 28 class DomainReliabilityDispatcher; |
29 class DomainReliabilityUploader; | 29 class DomainReliabilityUploader; |
30 class MockableTime; | 30 class MockableTime; |
31 | 31 |
32 // The per-domain context for the Domain Reliability client; includes the | 32 // The per-domain context for the Domain Reliability client; includes the |
33 // domain's config and per-resource beacon queues. | 33 // domain's config and beacon queue. |
34 class DOMAIN_RELIABILITY_EXPORT DomainReliabilityContext { | 34 class DOMAIN_RELIABILITY_EXPORT DomainReliabilityContext { |
35 public: | 35 public: |
36 class DOMAIN_RELIABILITY_EXPORT Factory { | 36 class DOMAIN_RELIABILITY_EXPORT Factory { |
37 public: | 37 public: |
38 virtual ~Factory(); | 38 virtual ~Factory(); |
39 virtual scoped_ptr<DomainReliabilityContext> CreateContextForConfig( | 39 virtual scoped_ptr<DomainReliabilityContext> CreateContextForConfig( |
40 scoped_ptr<const DomainReliabilityConfig> config) = 0; | 40 scoped_ptr<const DomainReliabilityConfig> config) = 0; |
41 }; | 41 }; |
42 | 42 |
43 DomainReliabilityContext( | 43 DomainReliabilityContext( |
44 MockableTime* time, | 44 MockableTime* time, |
45 const DomainReliabilityScheduler::Params& scheduler_params, | 45 const DomainReliabilityScheduler::Params& scheduler_params, |
46 const std::string& upload_reporter_string, | 46 const std::string& upload_reporter_string, |
47 const base::TimeTicks* last_network_change_time, | 47 const base::TimeTicks* last_network_change_time, |
48 DomainReliabilityDispatcher* dispatcher, | 48 DomainReliabilityDispatcher* dispatcher, |
49 DomainReliabilityUploader* uploader, | 49 DomainReliabilityUploader* uploader, |
50 scoped_ptr<const DomainReliabilityConfig> config); | 50 scoped_ptr<const DomainReliabilityConfig> config); |
51 ~DomainReliabilityContext(); | 51 ~DomainReliabilityContext(); |
52 | 52 |
53 // Notifies the context of a beacon on its domain(s); may or may not save the | 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, | 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. | 55 // but will increment one of the request counters in any case. |
56 void OnBeacon(const GURL& url, const DomainReliabilityBeacon& beacon); | 56 void OnBeacon(scoped_ptr<DomainReliabilityBeacon> beacon); |
57 | 57 |
58 // Called to clear browsing data, since beacons are like browsing history. | 58 // Called to clear browsing data, since beacons are like browsing history. |
59 void ClearBeacons(); | 59 void ClearBeacons(); |
60 | 60 |
61 // Gets a Value containing data that can be formatted into a web page for | 61 // Gets a Value containing data that can be formatted into a web page for |
62 // debugging purposes. | 62 // debugging purposes. |
63 scoped_ptr<base::Value> GetWebUIData() const; | 63 scoped_ptr<base::Value> GetWebUIData() const; |
64 | 64 |
| 65 // Gets the beacons queued for upload in this context. |*beacons_out| will be |
| 66 // cleared and filled with pointers to the beacons; the pointers remain valid |
| 67 // as long as no other requests are reported to the DomainReliabilityMonitor. |
65 void GetQueuedBeaconsForTesting( | 68 void GetQueuedBeaconsForTesting( |
66 std::vector<DomainReliabilityBeacon>* beacons_out) const; | 69 std::vector<const DomainReliabilityBeacon*>* beacons_out) const; |
67 | |
68 void GetRequestCountsForTesting( | |
69 size_t resource_index, | |
70 uint32* successful_requests_out, | |
71 uint32* failed_requests_out) const; | |
72 | 70 |
73 const DomainReliabilityConfig& config() const { return *config_.get(); } | 71 const DomainReliabilityConfig& config() const { return *config_.get(); } |
74 | 72 |
75 // Maximum number of beacons queued per context; if more than this many are | 73 // Maximum number of beacons queued per context; if more than this many are |
76 // queued; the oldest beacons will be removed. | 74 // queued; the oldest beacons will be removed. |
77 static const size_t kMaxQueuedBeacons; | 75 static const size_t kMaxQueuedBeacons; |
78 | 76 |
79 private: | 77 private: |
80 class ResourceState; | 78 // Deque of beacons owned by this context. (Deleted after uploading.) |
| 79 typedef std::deque<DomainReliabilityBeacon*> BeaconDeque; |
81 | 80 |
82 typedef std::deque<DomainReliabilityBeacon> BeaconDeque; | |
83 typedef ScopedVector<ResourceState> ResourceStateVector; | |
84 typedef ResourceStateVector::const_iterator ResourceStateIterator; | |
85 | |
86 void InitializeResourceStates(); | |
87 void ScheduleUpload(base::TimeDelta min_delay, base::TimeDelta max_delay); | 81 void ScheduleUpload(base::TimeDelta min_delay, base::TimeDelta max_delay); |
88 void StartUpload(); | 82 void StartUpload(); |
89 void OnUploadComplete(const DomainReliabilityUploader::UploadResult& result); | 83 void OnUploadComplete(const DomainReliabilityUploader::UploadResult& result); |
90 | 84 |
91 scoped_ptr<const base::Value> CreateReport(base::TimeTicks upload_time) const; | 85 scoped_ptr<const base::Value> CreateReport(base::TimeTicks upload_time, |
| 86 const GURL& collector_url) const; |
92 | 87 |
93 // Remembers the current state of the context when an upload starts. Can be | 88 // 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 | 89 // called multiple times in a row (without |CommitUpload|) if uploads fail |
95 // and are retried. | 90 // and are retried. |
96 void MarkUpload(); | 91 void MarkUpload(); |
97 | 92 |
98 // Uses the state remembered by |MarkUpload| to remove successfully uploaded | 93 // Uses the state remembered by |MarkUpload| to remove successfully uploaded |
99 // data but keep beacons and request counts added after the upload started. | 94 // data but keep beacons and request counts added after the upload started. |
100 void CommitUpload(); | 95 void CommitUpload(); |
101 | 96 |
102 void RollbackUpload(); | 97 void RollbackUpload(); |
103 | 98 |
104 // Finds and removes the oldest beacon. DCHECKs if there is none. (Called | 99 // Finds and removes the oldest beacon. DCHECKs if there is none. (Called |
105 // when there are too many beacons queued.) | 100 // when there are too many beacons queued.) |
106 void RemoveOldestBeacon(); | 101 void RemoveOldestBeacon(); |
107 | 102 |
108 scoped_ptr<const DomainReliabilityConfig> config_; | 103 scoped_ptr<const DomainReliabilityConfig> config_; |
109 MockableTime* time_; | 104 MockableTime* time_; |
110 const std::string& upload_reporter_string_; | 105 const std::string& upload_reporter_string_; |
111 DomainReliabilityScheduler scheduler_; | 106 DomainReliabilityScheduler scheduler_; |
112 DomainReliabilityDispatcher* dispatcher_; | 107 DomainReliabilityDispatcher* dispatcher_; |
113 DomainReliabilityUploader* uploader_; | 108 DomainReliabilityUploader* uploader_; |
114 | 109 |
115 BeaconDeque beacons_; | 110 BeaconDeque beacons_; |
116 size_t uploading_beacons_size_; | 111 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_; | 112 base::TimeTicks upload_time_; |
121 base::TimeTicks last_upload_time_; | 113 base::TimeTicks last_upload_time_; |
122 // The last network change time is not tracked per-context, so this is a | 114 // 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. | 115 // pointer to that value in a wider (e.g. per-Monitor or unittest) scope. |
124 const base::TimeTicks* last_network_change_time_; | 116 const base::TimeTicks* last_network_change_time_; |
125 | 117 |
126 base::WeakPtrFactory<DomainReliabilityContext> weak_factory_; | 118 base::WeakPtrFactory<DomainReliabilityContext> weak_factory_; |
127 | 119 |
128 DISALLOW_COPY_AND_ASSIGN(DomainReliabilityContext); | 120 DISALLOW_COPY_AND_ASSIGN(DomainReliabilityContext); |
129 }; | 121 }; |
130 | 122 |
131 } // namespace domain_reliability | 123 } // namespace domain_reliability |
132 | 124 |
133 #endif // COMPONENTS_DOMAIN_RELIABILITY_CONTEXT_H_ | 125 #endif // COMPONENTS_DOMAIN_RELIABILITY_CONTEXT_H_ |
OLD | NEW |