Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / components / domain_reliability / util.cc
blob1fc38baccd6776b1b0717c978fa83e32ccb2c06d
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 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) {}
23 virtual ~ActualTimer() {}
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);
32 virtual void Stop() OVERRIDE {
33 base_timer_.Stop();
36 virtual bool IsRunning() OVERRIDE {
37 return base_timer_.IsRunning();
40 private:
41 base::Timer base_timer_;
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.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.empty_response" },
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" },
77 } // namespace
79 // static
80 bool GetDomainReliabilityBeaconStatus(
81 int net_error,
82 int http_response_code,
83 std::string* beacon_status_out) {
84 if (net_error == net::OK) {
85 if (http_response_code >= 400 && http_response_code < 600)
86 *beacon_status_out = "http.error";
87 else
88 *beacon_status_out = "ok";
89 return true;
92 // TODO(ttuttle): Consider sorting and using binary search?
93 for (size_t i = 0; i < arraysize(net_error_map); i++) {
94 if (net_error_map[i].net_error == net_error) {
95 *beacon_status_out = net_error_map[i].beacon_status;
96 return true;
99 return false;
102 // TODO(ttuttle): Consider using NPN/ALPN instead, if there's a good way to
103 // differentiate HTTP and HTTPS.
104 std::string GetDomainReliabilityProtocol(
105 net::HttpResponseInfo::ConnectionInfo connection_info,
106 bool ssl_info_populated) {
107 switch (connection_info) {
108 case net::HttpResponseInfo::CONNECTION_INFO_UNKNOWN:
109 return "";
110 case net::HttpResponseInfo::CONNECTION_INFO_HTTP1:
111 return ssl_info_populated ? "HTTPS" : "HTTP";
112 case net::HttpResponseInfo::CONNECTION_INFO_DEPRECATED_SPDY2:
113 case net::HttpResponseInfo::CONNECTION_INFO_SPDY3:
114 case net::HttpResponseInfo::CONNECTION_INFO_SPDY4:
115 return "SPDY";
116 case net::HttpResponseInfo::CONNECTION_INFO_QUIC1_SPDY3:
117 return "QUIC";
118 case net::HttpResponseInfo::NUM_OF_CONNECTION_INFOS:
119 NOTREACHED();
120 return "";
122 NOTREACHED();
123 return "";
126 MockableTime::Timer::~Timer() {}
127 MockableTime::Timer::Timer() {}
129 MockableTime::~MockableTime() {}
130 MockableTime::MockableTime() {}
132 ActualTime::ActualTime() {}
133 ActualTime::~ActualTime() {}
135 base::Time ActualTime::Now() { return base::Time::Now(); }
136 base::TimeTicks ActualTime::NowTicks() { return base::TimeTicks::Now(); }
138 scoped_ptr<MockableTime::Timer> ActualTime::CreateTimer() {
139 return scoped_ptr<MockableTime::Timer>(new ActualTimer());
142 } // namespace domain_reliability