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/time/time.h" 11 #include "base/timer/timer.h" 12 #include "net/base/net_errors.h" 13 14 namespace domain_reliability { 15 16 namespace { 17 18 class ActualTimer : public MockableTime::Timer { 19 public: 20 // Initialize base timer with retain_user_info and is_repeating false. 21 ActualTimer() : base_timer_(false, false) {} 22 23 virtual ~ActualTimer() {} 24 25 // MockableTime::Timer implementation: 26 virtual 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); 30 } 31 32 virtual void Stop() OVERRIDE { 33 base_timer_.Stop(); 34 } 35 36 virtual bool IsRunning() OVERRIDE { 37 return base_timer_.IsRunning(); 38 } 39 40 private: 41 base::Timer base_timer_; 42 }; 43 44 const struct NetErrorMapping { 45 int net_error; 46 const char* beacon_status; 47 } net_error_map[] = { 48 { net::OK, "ok" }, 49 { net::ERR_TIMED_OUT, "tcp.connection.timed_out" }, 50 { net::ERR_CONNECTION_CLOSED, "tcp.connection.closed" }, 51 { net::ERR_CONNECTION_RESET, "tcp.connection.reset" }, 52 { net::ERR_CONNECTION_REFUSED, "tcp.connection.refused" }, 53 { net::ERR_CONNECTION_ABORTED, "tcp.connection.aborted" }, 54 { net::ERR_CONNECTION_FAILED, "tcp.connection.failed" }, 55 { net::ERR_NAME_NOT_RESOLVED, "dns" }, 56 { net::ERR_SSL_PROTOCOL_ERROR, "ssl.protocol.error" }, 57 { net::ERR_ADDRESS_INVALID, "tcp.connection.address_invalid" }, 58 { net::ERR_ADDRESS_UNREACHABLE, "tcp.connection.address_unreachable" }, 59 { net::ERR_CONNECTION_TIMED_OUT, "tcp.connection.timed_out" }, 60 { net::ERR_NAME_RESOLUTION_FAILED, "dns" }, 61 { net::ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN, 62 "ssl.cert.pinned_key_not_in_cert_chain" }, 63 { net::ERR_CERT_COMMON_NAME_INVALID, "ssl.cert.name_invalid" }, 64 { net::ERR_CERT_DATE_INVALID, "ssl.cert.date_invalid" }, 65 { net::ERR_CERT_AUTHORITY_INVALID, "ssl.cert.authority_invalid" }, 66 { net::ERR_CERT_REVOKED, "ssl.cert.revoked" }, 67 { net::ERR_CERT_INVALID, "ssl.cert.invalid" }, 68 { net::ERR_EMPTY_RESPONSE, "http.response.empty" }, 69 { net::ERR_SPDY_PING_FAILED, "spdy.ping_failed" }, 70 { net::ERR_SPDY_PROTOCOL_ERROR, "spdy.protocol" }, 71 { net::ERR_QUIC_PROTOCOL_ERROR, "quic.protocol" }, 72 { net::ERR_DNS_MALFORMED_RESPONSE, "dns.protocol" }, 73 { net::ERR_DNS_SERVER_FAILED, "dns.server" }, 74 { net::ERR_DNS_TIMED_OUT, "dns.timed_out" }, 75 { net::ERR_INSECURE_RESPONSE, "ssl" }, 76 { net::ERR_CONTENT_LENGTH_MISMATCH, "http.response.content_length_mismatch" }, 77 { net::ERR_INCOMPLETE_CHUNKED_ENCODING, 78 "http.response.incomplete_chunked_encoding" }, 79 { net::ERR_SSL_VERSION_OR_CIPHER_MISMATCH, 80 "ssl.version_or_cipher_mismatch" }, 81 { net::ERR_BAD_SSL_CLIENT_AUTH_CERT, "ssl.bad_client_auth_cert" }, 82 { net::ERR_INVALID_CHUNKED_ENCODING, 83 "http.response.invalid_chunked_encoding" }, 84 { net::ERR_RESPONSE_HEADERS_TRUNCATED, "http.response.headers.truncated" }, 85 { net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, 86 "http.request.range_not_satisfiable" }, 87 { net::ERR_INVALID_RESPONSE, "http.response.invalid" }, 88 { net::ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION, 89 "http.response.headers.multiple_content_disposition" }, 90 { net::ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_LENGTH, 91 "http.response.headers.multiple_content_length" }, 92 { net::ERR_SSL_UNRECOGNIZED_NAME_ALERT, "ssl.unrecognized_name_alert" } 93 }; 94 95 } // namespace 96 97 // static 98 bool GetDomainReliabilityBeaconStatus( 99 int net_error, 100 int http_response_code, 101 std::string* beacon_status_out) { 102 if (net_error == net::OK) { 103 if (http_response_code >= 400 && http_response_code < 600) 104 *beacon_status_out = "http.error"; 105 else 106 *beacon_status_out = "ok"; 107 return true; 108 } 109 110 // TODO(ttuttle): Consider sorting and using binary search? 111 for (size_t i = 0; i < arraysize(net_error_map); i++) { 112 if (net_error_map[i].net_error == net_error) { 113 *beacon_status_out = net_error_map[i].beacon_status; 114 return true; 115 } 116 } 117 return false; 118 } 119 120 // TODO(ttuttle): Consider using NPN/ALPN instead, if there's a good way to 121 // differentiate HTTP and HTTPS. 122 std::string GetDomainReliabilityProtocol( 123 net::HttpResponseInfo::ConnectionInfo connection_info, 124 bool ssl_info_populated) { 125 switch (connection_info) { 126 case net::HttpResponseInfo::CONNECTION_INFO_UNKNOWN: 127 return ""; 128 case net::HttpResponseInfo::CONNECTION_INFO_HTTP1: 129 return ssl_info_populated ? "HTTPS" : "HTTP"; 130 case net::HttpResponseInfo::CONNECTION_INFO_DEPRECATED_SPDY2: 131 case net::HttpResponseInfo::CONNECTION_INFO_SPDY3: 132 case net::HttpResponseInfo::CONNECTION_INFO_SPDY4: 133 return "SPDY"; 134 case net::HttpResponseInfo::CONNECTION_INFO_QUIC1_SPDY3: 135 return "QUIC"; 136 case net::HttpResponseInfo::NUM_OF_CONNECTION_INFOS: 137 NOTREACHED(); 138 return ""; 139 } 140 NOTREACHED(); 141 return ""; 142 } 143 144 MockableTime::Timer::~Timer() {} 145 MockableTime::Timer::Timer() {} 146 147 MockableTime::~MockableTime() {} 148 MockableTime::MockableTime() {} 149 150 ActualTime::ActualTime() {} 151 ActualTime::~ActualTime() {} 152 153 base::Time ActualTime::Now() { return base::Time::Now(); } 154 base::TimeTicks ActualTime::NowTicks() { return base::TimeTicks::Now(); } 155 156 scoped_ptr<MockableTime::Timer> ActualTime::CreateTimer() { 157 return scoped_ptr<MockableTime::Timer>(new ActualTimer()); 158 } 159 160 } // namespace domain_reliability 161