Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / components / domain_reliability / util.cc
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   ~ActualTimer() override {}
24
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);
30   }
31
32   void Stop() override { base_timer_.Stop(); }
33
34   bool IsRunning() override { return base_timer_.IsRunning(); }
35
36  private:
37   base::Timer base_timer_;
38 };
39
40 const struct NetErrorMapping {
41   int net_error;
42   const char* beacon_status;
43 } net_error_map[] = {
44   { net::OK, "ok" },
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" },
72 };
73
74 }  // namespace
75
76 // static
77 bool GetDomainReliabilityBeaconStatus(
78     int net_error,
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";
84     else
85       *beacon_status_out = "ok";
86     return true;
87   }
88
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;
93       return true;
94     }
95   }
96   return false;
97 }
98
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:
106       return "";
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:
112       return "SPDY";
113     case net::HttpResponseInfo::CONNECTION_INFO_QUIC1_SPDY3:
114       return "QUIC";
115     case net::HttpResponseInfo::NUM_OF_CONNECTION_INFOS:
116       NOTREACHED();
117       return "";
118   }
119   NOTREACHED();
120   return "";
121 }
122
123 MockableTime::Timer::~Timer() {}
124 MockableTime::Timer::Timer() {}
125
126 MockableTime::~MockableTime() {}
127 MockableTime::MockableTime() {}
128
129 ActualTime::ActualTime() {}
130 ActualTime::~ActualTime() {}
131
132 base::Time ActualTime::Now() { return base::Time::Now(); }
133 base::TimeTicks ActualTime::NowTicks() { return base::TimeTicks::Now(); }
134
135 scoped_ptr<MockableTime::Timer> ActualTime::CreateTimer() {
136   return scoped_ptr<MockableTime::Timer>(new ActualTimer());
137 }
138
139 MockableTimeBackoffEntry::MockableTimeBackoffEntry(
140     const net::BackoffEntry::Policy* const policy,
141     MockableTime* time)
142     : net::BackoffEntry(policy),
143       time_(time) {
144 }
145
146 MockableTimeBackoffEntry::~MockableTimeBackoffEntry() {}
147
148 base::TimeTicks MockableTimeBackoffEntry::ImplGetTimeNow() const {
149   return time_->NowTicks();
150 }
151
152 }  // namespace domain_reliability