Lots of random cleanups, mostly for native_theme_win.cc:
[chromium-blink-merge.git] / net / quic / crypto / quic_server_info.cc
blob519d7b5fd6015bd2c0a42b2c7148fc06b125ea91
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 "net/quic/crypto/quic_server_info.h"
7 #include <limits>
9 #include "base/pickle.h"
11 using std::string;
13 namespace {
15 const int kQuicCryptoConfigVersion = 1;
17 } // namespace
19 namespace net {
21 QuicServerInfo::State::State() {}
23 QuicServerInfo::State::~State() {}
25 void QuicServerInfo::State::Clear() {
26 server_config.clear();
27 source_address_token.clear();
28 server_config_sig.clear();
29 certs.clear();
32 QuicServerInfo::QuicServerInfo(const QuicServerId& server_id)
33 : server_id_(server_id) {
36 QuicServerInfo::~QuicServerInfo() {
39 const QuicServerInfo::State& QuicServerInfo::state() const {
40 return state_;
43 QuicServerInfo::State* QuicServerInfo::mutable_state() {
44 return &state_;
47 bool QuicServerInfo::Parse(const string& data) {
48 State* state = mutable_state();
50 state->Clear();
52 bool r = ParseInner(data);
53 if (!r)
54 state->Clear();
55 return r;
58 bool QuicServerInfo::ParseInner(const string& data) {
59 State* state = mutable_state();
61 // No data was read from the disk cache.
62 if (data.empty()) {
63 return false;
66 Pickle p(data.data(), data.size());
67 PickleIterator iter(p);
69 int version = -1;
70 if (!p.ReadInt(&iter, &version)) {
71 DVLOG(1) << "Missing version";
72 return false;
75 if (version != kQuicCryptoConfigVersion) {
76 DVLOG(1) << "Unsupported version";
77 return false;
80 if (!p.ReadString(&iter, &state->server_config)) {
81 DVLOG(1) << "Malformed server_config";
82 return false;
84 if (!p.ReadString(&iter, &state->source_address_token)) {
85 DVLOG(1) << "Malformed source_address_token";
86 return false;
88 if (!p.ReadString(&iter, &state->server_config_sig)) {
89 DVLOG(1) << "Malformed server_config_sig";
90 return false;
93 // Read certs.
94 uint32 num_certs;
95 if (!p.ReadUInt32(&iter, &num_certs)) {
96 DVLOG(1) << "Malformed num_certs";
97 return false;
100 for (uint32 i = 0; i < num_certs; i++) {
101 string cert;
102 if (!p.ReadString(&iter, &cert)) {
103 DVLOG(1) << "Malformed cert";
104 return false;
106 state->certs.push_back(cert);
109 return true;
112 string QuicServerInfo::Serialize() {
113 string pickled_data = SerializeInner();
114 state_.Clear();
115 return pickled_data;
118 string QuicServerInfo::SerializeInner() const {
119 Pickle p(sizeof(Pickle::Header));
121 if (!p.WriteInt(kQuicCryptoConfigVersion) ||
122 !p.WriteString(state_.server_config) ||
123 !p.WriteString(state_.source_address_token) ||
124 !p.WriteString(state_.server_config_sig) ||
125 state_.certs.size() > std::numeric_limits<uint32>::max() ||
126 !p.WriteUInt32(state_.certs.size())) {
127 return string();
130 for (size_t i = 0; i < state_.certs.size(); i++) {
131 if (!p.WriteString(state_.certs[i])) {
132 return string();
136 return string(reinterpret_cast<const char *>(p.data()), p.size());
139 QuicServerInfoFactory::~QuicServerInfoFactory() {}
141 } // namespace net