GN + Android: extract android_standalone_library rule.
[chromium-blink-merge.git] / components / domain_reliability / util.cc
blob7ed3f1a4e4ff3cff4536119402972cd0d896c07d
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 #include "components/domain_reliability/util.h"
7 #include "base/callback.h"
8 #include "base/logging.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/time/time.h"
11 #include "base/timer/timer.h"
12 #include "net/base/net_errors.h"
14 namespace domain_reliability {
16 namespace {
18 const struct NetErrorMapping {
19 int net_error;
20 const char* beacon_status;
21 } net_error_map[] = {
22 { net::OK, "ok" },
23 { net::ERR_ABORTED, "aborted" },
24 { net::ERR_TIMED_OUT, "tcp.connection.timed_out" },
25 { net::ERR_CONNECTION_CLOSED, "tcp.connection.closed" },
26 { net::ERR_CONNECTION_RESET, "tcp.connection.reset" },
27 { net::ERR_CONNECTION_REFUSED, "tcp.connection.refused" },
28 { net::ERR_CONNECTION_ABORTED, "tcp.connection.aborted" },
29 { net::ERR_CONNECTION_FAILED, "tcp.connection.failed" },
30 { net::ERR_NAME_NOT_RESOLVED, "dns" },
31 { net::ERR_SSL_PROTOCOL_ERROR, "ssl.protocol.error" },
32 { net::ERR_ADDRESS_INVALID, "tcp.connection.address_invalid" },
33 { net::ERR_ADDRESS_UNREACHABLE, "tcp.connection.address_unreachable" },
34 { net::ERR_CONNECTION_TIMED_OUT, "tcp.connection.timed_out" },
35 { net::ERR_NAME_RESOLUTION_FAILED, "dns" },
36 { net::ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN,
37 "ssl.cert.pinned_key_not_in_cert_chain" },
38 { net::ERR_CERT_COMMON_NAME_INVALID, "ssl.cert.name_invalid" },
39 { net::ERR_CERT_DATE_INVALID, "ssl.cert.date_invalid" },
40 { net::ERR_CERT_AUTHORITY_INVALID, "ssl.cert.authority_invalid" },
41 { net::ERR_CERT_REVOKED, "ssl.cert.revoked" },
42 { net::ERR_CERT_INVALID, "ssl.cert.invalid" },
43 { net::ERR_EMPTY_RESPONSE, "http.response.empty" },
44 { net::ERR_SPDY_PING_FAILED, "spdy.ping_failed" },
45 { net::ERR_SPDY_PROTOCOL_ERROR, "spdy.protocol" },
46 { net::ERR_QUIC_PROTOCOL_ERROR, "quic.protocol" },
47 { net::ERR_DNS_MALFORMED_RESPONSE, "dns.protocol" },
48 { net::ERR_DNS_SERVER_FAILED, "dns.server" },
49 { net::ERR_DNS_TIMED_OUT, "dns.timed_out" },
50 { net::ERR_INSECURE_RESPONSE, "ssl" },
51 { net::ERR_CONTENT_LENGTH_MISMATCH, "http.response.content_length_mismatch" },
52 { net::ERR_INCOMPLETE_CHUNKED_ENCODING,
53 "http.response.incomplete_chunked_encoding" },
54 { net::ERR_SSL_VERSION_OR_CIPHER_MISMATCH,
55 "ssl.version_or_cipher_mismatch" },
56 { net::ERR_BAD_SSL_CLIENT_AUTH_CERT, "ssl.bad_client_auth_cert" },
57 { net::ERR_INVALID_CHUNKED_ENCODING,
58 "http.response.invalid_chunked_encoding" },
59 { net::ERR_RESPONSE_HEADERS_TRUNCATED, "http.response.headers.truncated" },
60 { net::ERR_REQUEST_RANGE_NOT_SATISFIABLE,
61 "http.request.range_not_satisfiable" },
62 { net::ERR_INVALID_RESPONSE, "http.response.invalid" },
63 { net::ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION,
64 "http.response.headers.multiple_content_disposition" },
65 { net::ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_LENGTH,
66 "http.response.headers.multiple_content_length" },
67 { net::ERR_SSL_UNRECOGNIZED_NAME_ALERT, "ssl.unrecognized_name_alert" }
70 } // namespace
72 // static
73 bool GetDomainReliabilityBeaconStatus(
74 int net_error,
75 int http_response_code,
76 std::string* beacon_status_out) {
77 if (net_error == net::OK) {
78 if (http_response_code >= 400 && http_response_code < 600)
79 *beacon_status_out = "http.error";
80 else
81 *beacon_status_out = "ok";
82 return true;
85 // TODO(ttuttle): Consider sorting and using binary search?
86 for (size_t i = 0; i < arraysize(net_error_map); i++) {
87 if (net_error_map[i].net_error == net_error) {
88 *beacon_status_out = net_error_map[i].beacon_status;
89 return true;
92 return false;
95 // TODO(ttuttle): Consider using NPN/ALPN instead, if there's a good way to
96 // differentiate HTTP and HTTPS.
97 std::string GetDomainReliabilityProtocol(
98 net::HttpResponseInfo::ConnectionInfo connection_info,
99 bool ssl_info_populated) {
100 switch (connection_info) {
101 case net::HttpResponseInfo::CONNECTION_INFO_UNKNOWN:
102 return "";
103 case net::HttpResponseInfo::CONNECTION_INFO_HTTP1:
104 return ssl_info_populated ? "HTTPS" : "HTTP";
105 case net::HttpResponseInfo::CONNECTION_INFO_DEPRECATED_SPDY2:
106 case net::HttpResponseInfo::CONNECTION_INFO_SPDY3:
107 case net::HttpResponseInfo::CONNECTION_INFO_HTTP2_14:
108 case net::HttpResponseInfo::CONNECTION_INFO_HTTP2_15:
109 return "SPDY";
110 case net::HttpResponseInfo::CONNECTION_INFO_QUIC1_SPDY3:
111 return "QUIC";
112 case net::HttpResponseInfo::NUM_OF_CONNECTION_INFOS:
113 NOTREACHED();
114 return "";
116 NOTREACHED();
117 return "";
120 int GetNetErrorFromURLRequestStatus(const net::URLRequestStatus& status) {
121 switch (status.status()) {
122 case net::URLRequestStatus::SUCCESS:
123 return net::OK;
124 case net::URLRequestStatus::CANCELED:
125 return net::ERR_ABORTED;
126 case net::URLRequestStatus::FAILED:
127 return status.error();
128 default:
129 NOTREACHED();
130 return net::ERR_FAILED;
134 void GetUploadResultFromResponseDetails(
135 int net_error,
136 int http_response_code,
137 base::TimeDelta retry_after,
138 DomainReliabilityUploader::UploadResult* result) {
139 if (net_error == net::OK && http_response_code == 200) {
140 result->status = DomainReliabilityUploader::UploadResult::SUCCESS;
141 return;
144 if (net_error == net::OK &&
145 http_response_code == 503 &&
146 retry_after != base::TimeDelta()) {
147 result->status = DomainReliabilityUploader::UploadResult::RETRY_AFTER;
148 result->retry_after = retry_after;
149 return;
152 result->status = DomainReliabilityUploader::UploadResult::FAILURE;
153 return;
156 namespace {
158 class ActualTimer : public MockableTime::Timer {
159 public:
160 // Initialize base timer with retain_user_info and is_repeating false.
161 ActualTimer() : base_timer_(false, false) {}
163 ~ActualTimer() override {}
165 // MockableTime::Timer implementation:
166 void Start(const tracked_objects::Location& posted_from,
167 base::TimeDelta delay,
168 const base::Closure& user_task) override {
169 base_timer_.Start(posted_from, delay, user_task);
172 void Stop() override { base_timer_.Stop(); }
174 bool IsRunning() override { return base_timer_.IsRunning(); }
176 private:
177 base::Timer base_timer_;
180 } // namespace
182 MockableTime::Timer::~Timer() {}
183 MockableTime::Timer::Timer() {}
185 MockableTime::~MockableTime() {}
186 MockableTime::MockableTime() {}
188 ActualTime::ActualTime() {}
189 ActualTime::~ActualTime() {}
191 base::Time ActualTime::Now() { return base::Time::Now(); }
192 base::TimeTicks ActualTime::NowTicks() { return base::TimeTicks::Now(); }
194 scoped_ptr<MockableTime::Timer> ActualTime::CreateTimer() {
195 return scoped_ptr<MockableTime::Timer>(new ActualTimer());
198 MockableTimeBackoffEntry::MockableTimeBackoffEntry(
199 const net::BackoffEntry::Policy* const policy,
200 MockableTime* time)
201 : net::BackoffEntry(policy),
202 time_(time) {
205 MockableTimeBackoffEntry::~MockableTimeBackoffEntry() {}
207 base::TimeTicks MockableTimeBackoffEntry::ImplGetTimeNow() const {
208 return time_->NowTicks();
211 } // namespace domain_reliability