media: Add RAPPOR metrics for EME usage.
[chromium-blink-merge.git] / net / http / http_server_properties.cc
blob82ce06e7e57a8a74ac93c63d2fc44fe0de2d93a7
1 // Copyright (c) 2012 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 "net/http/http_server_properties.h"
7 #include "base/logging.h"
8 #include "base/metrics/histogram_macros.h"
9 #include "base/strings/stringprintf.h"
10 #include "net/socket/ssl_client_socket.h"
11 #include "net/ssl/ssl_config.h"
13 namespace net {
15 const char kAlternateProtocolHeader[] = "Alternate-Protocol";
17 namespace {
19 // The order of these strings much match the order of the enum definition
20 // for AlternateProtocol.
21 const char* const kAlternateProtocolStrings[] = {
22 "npn-spdy/2",
23 "npn-spdy/3",
24 "npn-spdy/3.1",
25 "npn-h2-14", // HTTP/2 draft-14. Called SPDY4 internally.
26 "npn-h2",
27 "quic"};
29 static_assert(arraysize(kAlternateProtocolStrings) ==
30 NUM_VALID_ALTERNATE_PROTOCOLS,
31 "kAlternateProtocolStrings has incorrect size");
33 } // namespace
35 void HistogramAlternateProtocolUsage(AlternateProtocolUsage usage) {
36 UMA_HISTOGRAM_ENUMERATION("Net.AlternateProtocolUsage", usage,
37 ALTERNATE_PROTOCOL_USAGE_MAX);
40 void HistogramBrokenAlternateProtocolLocation(
41 BrokenAlternateProtocolLocation location){
42 UMA_HISTOGRAM_ENUMERATION("Net.AlternateProtocolBrokenLocation", location,
43 BROKEN_ALTERNATE_PROTOCOL_LOCATION_MAX);
46 bool IsAlternateProtocolValid(AlternateProtocol protocol) {
47 return protocol >= ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION &&
48 protocol <= ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION;
51 const char* AlternateProtocolToString(AlternateProtocol protocol) {
52 switch (protocol) {
53 case DEPRECATED_NPN_SPDY_2:
54 case NPN_SPDY_3:
55 case NPN_SPDY_3_1:
56 case NPN_SPDY_4_14:
57 case NPN_SPDY_4:
58 case QUIC:
59 DCHECK(IsAlternateProtocolValid(protocol));
60 return kAlternateProtocolStrings[
61 protocol - ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION];
62 case UNINITIALIZED_ALTERNATE_PROTOCOL:
63 return "Uninitialized";
65 NOTREACHED();
66 return "";
69 AlternateProtocol AlternateProtocolFromString(const std::string& str) {
70 for (int i = ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION;
71 i <= ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION; ++i) {
72 AlternateProtocol protocol = static_cast<AlternateProtocol>(i);
73 if (str == AlternateProtocolToString(protocol))
74 return protocol;
76 return UNINITIALIZED_ALTERNATE_PROTOCOL;
79 AlternateProtocol AlternateProtocolFromNextProto(NextProto next_proto) {
80 switch (next_proto) {
81 case kProtoDeprecatedSPDY2:
82 return DEPRECATED_NPN_SPDY_2;
83 case kProtoSPDY3:
84 return NPN_SPDY_3;
85 case kProtoSPDY31:
86 return NPN_SPDY_3_1;
87 case kProtoSPDY4_14:
88 return NPN_SPDY_4_14;
89 case kProtoSPDY4:
90 return NPN_SPDY_4;
91 case kProtoQUIC1SPDY3:
92 return QUIC;
94 case kProtoUnknown:
95 case kProtoHTTP11:
96 break;
99 NOTREACHED() << "Invalid NextProto: " << next_proto;
100 return UNINITIALIZED_ALTERNATE_PROTOCOL;
103 std::string AlternativeService::ToString() const {
104 return base::StringPrintf("%s %s:%d", AlternateProtocolToString(protocol),
105 host.c_str(), port);
108 std::string AlternativeServiceInfo::ToString() const {
109 return base::StringPrintf("%s, p=%f", alternative_service.ToString().c_str(),
110 probability);
113 // static
114 void HttpServerProperties::ForceHTTP11(SSLConfig* ssl_config) {
115 ssl_config->next_protos.clear();
116 ssl_config->next_protos.push_back(kProtoHTTP11);
119 } // namespace net