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/strings/stringprintf.h"
11 #include "base/time/time.h"
12 #include "base/timer/timer.h"
13 #include "net/base/net_errors.h"
15 namespace domain_reliability {
19 class ActualTimer : public MockableTime::Timer {
21 // Initialize base timer with retain_user_info and is_repeating false.
22 ActualTimer() : base_timer_(false, false) {}
24 virtual ~ActualTimer() {}
26 // MockableTime::Timer implementation:
27 virtual void Start(const tracked_objects::Location& posted_from,
28 base::TimeDelta delay,
29 const base::Closure& user_task) OVERRIDE {
30 base_timer_.Start(posted_from, delay, user_task);
33 virtual void Stop() OVERRIDE {
37 virtual bool IsRunning() OVERRIDE {
38 return base_timer_.IsRunning();
42 base::Timer base_timer_;
45 const struct NetErrorMapping {
47 const char* beacon_status;
50 { net::ERR_TIMED_OUT, "tcp.connection.timed_out" },
51 { net::ERR_CONNECTION_CLOSED, "tcp.connection.closed" },
52 { net::ERR_CONNECTION_RESET, "tcp.connection.reset" },
53 { net::ERR_CONNECTION_REFUSED, "tcp.connection.refused" },
54 { net::ERR_CONNECTION_ABORTED, "tcp.connection.aborted" },
55 { net::ERR_CONNECTION_FAILED, "tcp.connection.failed" },
56 { net::ERR_NAME_NOT_RESOLVED, "dns" },
57 { net::ERR_SSL_PROTOCOL_ERROR, "ssl.protocol.error" },
58 { net::ERR_ADDRESS_INVALID, "tcp.connection.address_invalid" },
59 { net::ERR_ADDRESS_UNREACHABLE, "tcp.connection.address_unreachable" },
60 { net::ERR_CONNECTION_TIMED_OUT, "tcp.connection.timed_out" },
61 { net::ERR_NAME_RESOLUTION_FAILED, "dns" },
62 { net::ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN,
63 "ssl.pinned_key_not_in_cert_chain" },
64 { net::ERR_CERT_COMMON_NAME_INVALID, "ssl.cert.name_invalid" },
65 { net::ERR_CERT_DATE_INVALID, "ssl.cert.date_invalid" },
66 { net::ERR_CERT_AUTHORITY_INVALID, "ssl.cert.authority_invalid" },
67 { net::ERR_CERT_REVOKED, "ssl.cert.revoked" },
68 { net::ERR_CERT_INVALID, "ssl.cert.invalid" },
69 { net::ERR_EMPTY_RESPONSE, "http.empty_response" },
70 { net::ERR_SPDY_PING_FAILED, "spdy.ping_failed" },
71 { net::ERR_SPDY_PROTOCOL_ERROR, "spdy.protocol" },
72 { net::ERR_QUIC_PROTOCOL_ERROR, "quic.protocol" },
73 { net::ERR_DNS_MALFORMED_RESPONSE, "dns.protocol" },
74 { net::ERR_DNS_SERVER_FAILED, "dns.server" },
75 { net::ERR_DNS_TIMED_OUT, "dns.timed_out" },
81 bool GetDomainReliabilityBeaconStatus(
83 int http_response_code,
84 std::string* beacon_status_out) {
85 if (net_error == net::OK) {
86 if (http_response_code >= 400 && http_response_code < 600)
87 *beacon_status_out = base::StringPrintf("http.%d", http_response_code);
89 *beacon_status_out = "ok";
93 // TODO(ttuttle): Consider sorting and using binary search?
94 for (size_t i = 0; i < arraysize(net_error_map); i++) {
95 if (net_error_map[i].net_error == net_error) {
96 *beacon_status_out = net_error_map[i].beacon_status;
103 MockableTime::Timer::~Timer() {}
104 MockableTime::Timer::Timer() {}
106 MockableTime::~MockableTime() {}
107 MockableTime::MockableTime() {}
109 ActualTime::ActualTime() {}
110 ActualTime::~ActualTime() {}
112 base::Time ActualTime::Now() { return base::Time::Now(); }
113 base::TimeTicks ActualTime::NowTicks() { return base::TimeTicks::Now(); }
115 scoped_ptr<MockableTime::Timer> ActualTime::CreateTimer() {
116 return scoped_ptr<MockableTime::Timer>(new ActualTimer());
119 } // namespace domain_reliability