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 #include "components/domain_reliability/context.h" | 5 #include "components/domain_reliability/context.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 typedef BeaconDeque::const_iterator BeaconConstIterator; | 31 typedef BeaconDeque::const_iterator BeaconConstIterator; |
32 } // namespace | 32 } // namespace |
33 | 33 |
34 class DomainReliabilityContext::ResourceState { | 34 class DomainReliabilityContext::ResourceState { |
35 public: | 35 public: |
36 ResourceState(DomainReliabilityContext* context, | 36 ResourceState(DomainReliabilityContext* context, |
37 const DomainReliabilityConfig::Resource* config) | 37 const DomainReliabilityConfig::Resource* config) |
38 : context(context), | 38 : context(context), |
39 config(config), | 39 config(config), |
40 successful_requests(0), | 40 successful_requests(0), |
41 failed_requests(0) {} | 41 failed_requests(0), |
| 42 uploading_beacons_size(0), |
| 43 uploading_successful_requests(0), |
| 44 uploading_failed_requests(0) {} |
42 ~ResourceState() {} | 45 ~ResourceState() {} |
43 | 46 |
44 // Serializes the resource state into a Value to be included in an upload. | 47 // Serializes the resource state into a Value to be included in an upload. |
45 // If there is nothing to report (no beacons and all request counters are 0), | 48 // If there is nothing to report (no beacons and all request counters are 0), |
46 // returns a scoped_ptr to NULL instead so the resource can be omitted. | 49 // returns a scoped_ptr to NULL instead so the resource can be omitted. |
47 scoped_ptr<base::Value> ToValue(base::TimeTicks upload_time) const { | 50 scoped_ptr<base::Value> ToValue(base::TimeTicks upload_time) const { |
48 if (beacons.empty() && successful_requests == 0 && failed_requests == 0) | 51 if (beacons.empty() && successful_requests == 0 && failed_requests == 0) |
49 return scoped_ptr<base::Value>(); | 52 return scoped_ptr<base::Value>(); |
50 | 53 |
51 ListValue* beacons_value = new ListValue(); | 54 ListValue* beacons_value = new ListValue(); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 time_(time), | 132 time_(time), |
130 upload_reporter_string_(upload_reporter_string), | 133 upload_reporter_string_(upload_reporter_string), |
131 scheduler_(time, | 134 scheduler_(time, |
132 config_->collectors.size(), | 135 config_->collectors.size(), |
133 scheduler_params, | 136 scheduler_params, |
134 base::Bind(&DomainReliabilityContext::ScheduleUpload, | 137 base::Bind(&DomainReliabilityContext::ScheduleUpload, |
135 base::Unretained(this))), | 138 base::Unretained(this))), |
136 dispatcher_(dispatcher), | 139 dispatcher_(dispatcher), |
137 uploader_(uploader), | 140 uploader_(uploader), |
138 beacon_count_(0), | 141 beacon_count_(0), |
| 142 uploading_beacon_count_(0), |
139 weak_factory_(this) { | 143 weak_factory_(this) { |
140 InitializeResourceStates(); | 144 InitializeResourceStates(); |
141 } | 145 } |
142 | 146 |
143 DomainReliabilityContext::~DomainReliabilityContext() {} | 147 DomainReliabilityContext::~DomainReliabilityContext() {} |
144 | 148 |
145 void DomainReliabilityContext::OnBeacon(const GURL& url, | 149 void DomainReliabilityContext::OnBeacon(const GURL& url, |
146 const DomainReliabilityBeacon& beacon) { | 150 const DomainReliabilityBeacon& beacon) { |
147 size_t index = config_->GetResourceIndexForUrl(url); | 151 size_t index = config_->GetResourceIndexForUrl(url); |
148 if (index == DomainReliabilityConfig::kInvalidResourceIndex) | 152 if (index == DomainReliabilityConfig::kInvalidResourceIndex) |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 state->successful_requests = 0; | 189 state->successful_requests = 0; |
186 state->failed_requests = 0; | 190 state->failed_requests = 0; |
187 state->uploading_beacons_size = 0; | 191 state->uploading_beacons_size = 0; |
188 state->uploading_successful_requests = 0; | 192 state->uploading_successful_requests = 0; |
189 state->uploading_failed_requests = 0; | 193 state->uploading_failed_requests = 0; |
190 } | 194 } |
191 beacon_count_ = 0; | 195 beacon_count_ = 0; |
192 uploading_beacon_count_ = 0; | 196 uploading_beacon_count_ = 0; |
193 } | 197 } |
194 | 198 |
| 199 scoped_ptr<base::Value> DomainReliabilityContext::GetWebUIData() const { |
| 200 base::DictionaryValue* context_value = new base::DictionaryValue(); |
| 201 |
| 202 context_value->SetString("domain", config().domain); |
| 203 context_value->SetInteger("beacon_count", static_cast<int>(beacon_count_)); |
| 204 context_value->SetInteger("uploading_beacon_count", |
| 205 static_cast<int>(uploading_beacon_count_)); |
| 206 context_value->Set("scheduler", scheduler_.GetWebUIData()); |
| 207 |
| 208 return scoped_ptr<base::Value>(context_value); |
| 209 } |
| 210 |
195 void DomainReliabilityContext::GetQueuedDataForTesting( | 211 void DomainReliabilityContext::GetQueuedDataForTesting( |
196 size_t resource_index, | 212 size_t resource_index, |
197 std::vector<DomainReliabilityBeacon>* beacons_out, | 213 std::vector<DomainReliabilityBeacon>* beacons_out, |
198 uint32* successful_requests_out, | 214 uint32* successful_requests_out, |
199 uint32* failed_requests_out) const { | 215 uint32* failed_requests_out) const { |
200 DCHECK_NE(DomainReliabilityConfig::kInvalidResourceIndex, resource_index); | 216 DCHECK_NE(DomainReliabilityConfig::kInvalidResourceIndex, resource_index); |
201 DCHECK_GT(states_.size(), resource_index); | 217 DCHECK_GT(states_.size(), resource_index); |
202 const ResourceState& state = *states_[resource_index]; | 218 const ResourceState& state = *states_[resource_index]; |
203 if (beacons_out) | 219 if (beacons_out) |
204 beacons_out->assign(state.beacons.begin(), state.beacons.end()); | 220 beacons_out->assign(state.beacons.begin(), state.beacons.end()); |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 | 330 |
315 min_resource->RemoveOldestBeacon(); | 331 min_resource->RemoveOldestBeacon(); |
316 --beacon_count_; | 332 --beacon_count_; |
317 // If that just removed a beacon counted in uploading_beacon_count_, decrement | 333 // If that just removed a beacon counted in uploading_beacon_count_, decrement |
318 // that. | 334 // that. |
319 if (uploading_beacon_count_ > 0) | 335 if (uploading_beacon_count_ > 0) |
320 --uploading_beacon_count_; | 336 --uploading_beacon_count_; |
321 } | 337 } |
322 | 338 |
323 } // namespace domain_reliability | 339 } // namespace domain_reliability |
OLD | NEW |