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. 4 5 #include "components/domain_reliability/util.h" 6 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" 14 15 namespace domain_reliability { 16 17 namespace { 18 19 class ActualTimer : public MockableTime::Timer { 20 public: 21 // Initialize base timer with retain_user_info and is_repeating false. 22 ActualTimer() : base_timer_(false, false) {} 23 24 virtual ~ActualTimer() {} 25 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); 31 } 32 33 virtual void Stop() OVERRIDE { 34 base_timer_.Stop(); 35 } 36 37 virtual bool IsRunning() OVERRIDE { 38 return base_timer_.IsRunning(); 39 } 40 41 private: 42 base::Timer base_timer_; 43 }; 44 45 const struct NetErrorMapping { 46 int net_error; 47 const char* beacon_status; 48 } net_error_map[] = { 49 { net::OK, "ok" }, 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" }, 76 }; 77 78 } // namespace 79 80 // static 81 bool GetDomainReliabilityBeaconStatus( 82 int net_error, 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); 88 else 89 *beacon_status_out = "ok"; 90 return true; 91 } 92 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; 97 return true; 98 } 99 } 100 return false; 101 } 102 103 MockableTime::Timer::~Timer() {} 104 MockableTime::Timer::Timer() {} 105 106 MockableTime::~MockableTime() {} 107 MockableTime::MockableTime() {} 108 109 ActualTime::ActualTime() {} 110 ActualTime::~ActualTime() {} 111 112 base::Time ActualTime::Now() { return base::Time::Now(); } 113 base::TimeTicks ActualTime::NowTicks() { return base::TimeTicks::Now(); } 114 115 scoped_ptr<MockableTime::Timer> ActualTime::CreateTimer() { 116 return scoped_ptr<MockableTime::Timer>(new ActualTimer()); 117 } 118 119 } // namespace domain_reliability 120