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 {
18 class ActualTimer : public MockableTime::Timer {
20 // Initialize base timer with retain_user_info and is_repeating false.
21 ActualTimer() : base_timer_(false, false) {}
23 ~ActualTimer() override {}
25 // MockableTime::Timer implementation:
26 void Start(const tracked_objects::Location& posted_from,
27 base::TimeDelta delay,
28 const base::Closure& user_task) override {
29 base_timer_.Start(posted_from, delay, user_task);
32 void Stop() override { base_timer_.Stop(); }
34 bool IsRunning() override { return base_timer_.IsRunning(); }
37 base::Timer base_timer_;
40 const struct NetErrorMapping {
42 const char* beacon_status;
45 { net::ERR_ABORTED, "aborted" },
46 { net::ERR_TIMED_OUT, "tcp.connection.timed_out" },
47 { net::ERR_CONNECTION_CLOSED, "tcp.connection.closed" },
48 { net::ERR_CONNECTION_RESET, "tcp.connection.reset" },
49 { net::ERR_CONNECTION_REFUSED, "tcp.connection.refused" },
50 { net::ERR_CONNECTION_ABORTED, "tcp.connection.aborted" },
51 { net::ERR_CONNECTION_FAILED, "tcp.connection.failed" },
52 { net::ERR_NAME_NOT_RESOLVED, "dns" },
53 { net::ERR_SSL_PROTOCOL_ERROR, "ssl.protocol.error" },
54 { net::ERR_ADDRESS_INVALID, "tcp.connection.address_invalid" },
55 { net::ERR_ADDRESS_UNREACHABLE, "tcp.connection.address_unreachable" },
56 { net::ERR_CONNECTION_TIMED_OUT, "tcp.connection.timed_out" },
57 { net::ERR_NAME_RESOLUTION_FAILED, "dns" },
58 { net::ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN,
59 "ssl.pinned_key_not_in_cert_chain" },
60 { net::ERR_CERT_COMMON_NAME_INVALID, "ssl.cert.name_invalid" },
61 { net::ERR_CERT_DATE_INVALID, "ssl.cert.date_invalid" },
62 { net::ERR_CERT_AUTHORITY_INVALID, "ssl.cert.authority_invalid" },
63 { net::ERR_CERT_REVOKED, "ssl.cert.revoked" },
64 { net::ERR_CERT_INVALID, "ssl.cert.invalid" },
65 { net::ERR_EMPTY_RESPONSE, "http.empty_response" },
66 { net::ERR_SPDY_PING_FAILED, "spdy.ping_failed" },
67 { net::ERR_SPDY_PROTOCOL_ERROR, "spdy.protocol" },
68 { net::ERR_QUIC_PROTOCOL_ERROR, "quic.protocol" },
69 { net::ERR_DNS_MALFORMED_RESPONSE, "dns.protocol" },
70 { net::ERR_DNS_SERVER_FAILED, "dns.server" },
71 { net::ERR_DNS_TIMED_OUT, "dns.timed_out" },
77 bool GetDomainReliabilityBeaconStatus(
79 int http_response_code,
80 std::string* beacon_status_out) {
81 if (net_error == net::OK) {
82 if (http_response_code >= 400 && http_response_code < 600)
83 *beacon_status_out = "http.error";
85 *beacon_status_out = "ok";
89 // TODO(ttuttle): Consider sorting and using binary search?
90 for (size_t i = 0; i < arraysize(net_error_map); i++) {
91 if (net_error_map[i].net_error == net_error) {
92 *beacon_status_out = net_error_map[i].beacon_status;
99 // TODO(ttuttle): Consider using NPN/ALPN instead, if there's a good way to
100 // differentiate HTTP and HTTPS.
101 std::string GetDomainReliabilityProtocol(
102 net::HttpResponseInfo::ConnectionInfo connection_info,
103 bool ssl_info_populated) {
104 switch (connection_info) {
105 case net::HttpResponseInfo::CONNECTION_INFO_UNKNOWN:
107 case net::HttpResponseInfo::CONNECTION_INFO_HTTP1:
108 return ssl_info_populated ? "HTTPS" : "HTTP";
109 case net::HttpResponseInfo::CONNECTION_INFO_DEPRECATED_SPDY2:
110 case net::HttpResponseInfo::CONNECTION_INFO_SPDY3:
111 case net::HttpResponseInfo::CONNECTION_INFO_SPDY4:
113 case net::HttpResponseInfo::CONNECTION_INFO_QUIC1_SPDY3:
115 case net::HttpResponseInfo::NUM_OF_CONNECTION_INFOS:
123 MockableTime::Timer::~Timer() {}
124 MockableTime::Timer::Timer() {}
126 MockableTime::~MockableTime() {}
127 MockableTime::MockableTime() {}
129 ActualTime::ActualTime() {}
130 ActualTime::~ActualTime() {}
132 base::Time ActualTime::Now() { return base::Time::Now(); }
133 base::TimeTicks ActualTime::NowTicks() { return base::TimeTicks::Now(); }
135 scoped_ptr<MockableTime::Timer> ActualTime::CreateTimer() {
136 return scoped_ptr<MockableTime::Timer>(new ActualTimer());
139 MockableTimeBackoffEntry::MockableTimeBackoffEntry(
140 const net::BackoffEntry::Policy* const policy,
142 : net::BackoffEntry(policy),
146 MockableTimeBackoffEntry::~MockableTimeBackoffEntry() {}
148 base::TimeTicks MockableTimeBackoffEntry::ImplGetTimeNow() const {
149 return time_->NowTicks();
152 } // namespace domain_reliability