Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_SPDY_SPDY_HEADER_BLOCK_H_ | 5 #ifndef NET_SPDY_SPDY_HEADER_BLOCK_H_ |
| 6 #define NET_SPDY_SPDY_HEADER_BLOCK_H_ | 6 #define NET_SPDY_SPDY_HEADER_BLOCK_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <memory> | |
| 9 #include <string> | 10 #include <string> |
| 10 | 11 |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/strings/string_piece.h" | |
| 14 #include "net/base/linked_hash_map.h" | |
| 11 #include "net/base/net_export.h" | 15 #include "net/base/net_export.h" |
| 12 #include "net/log/net_log.h" | 16 #include "net/log/net_log.h" |
| 13 | 17 |
| 14 namespace net { | 18 namespace net { |
| 15 | 19 |
| 16 // A data structure for holding a set of headers from either a | 20 // Allows arg-dependent lookup to work for logging's operator<<. |
| 17 // SYN_STREAM or SYN_REPLY frame. | 21 using ::operator<<; |
| 18 typedef std::map<std::string, std::string> SpdyHeaderBlock; | 22 |
| 23 using base::StringPiece; | |
|
Ryan Hamilton
2015/09/24 17:54:06
I didn't think it was permitted by the style guide
Bence
2015/09/24 21:14:51
Done. You are right.
https://google-styleguide.g
| |
| 24 | |
| 25 // This class provides a key-value map that can be used to store SPDY header | |
| 26 // names and values. This data structure preserves insertion order. | |
| 27 // | |
| 28 // Under the hood, this data structure uses large, contiguous blocks of memory | |
| 29 // to store names and values. Lookups may be performed with StringPiece keys, | |
| 30 // and values are returned as StringPieces (via StringPieceProxy, below). | |
| 31 // Value StringPieces are valid as long as the SpdyHeaderBlock exists; allocated | |
| 32 // memory is never freed until SpdyHeaderBlock's destruction. | |
| 33 // | |
| 34 // This implementation does not make much of an effort to minimize wasted space. | |
| 35 // It's expected that keys are rarely deleted from a SpdyHeaderBlock. | |
| 36 class NET_EXPORT SpdyHeaderBlock { | |
| 37 private: | |
| 38 typedef linked_hash_map<StringPiece, StringPiece> MapType; | |
| 39 class Storage; | |
| 40 | |
| 41 public: | |
| 42 typedef MapType::iterator iterator; | |
| 43 typedef MapType::const_iterator const_iterator; | |
| 44 typedef MapType::value_type value_type; | |
| 45 typedef MapType::reverse_iterator reverse_iterator; | |
| 46 | |
| 47 class StringPieceProxy; | |
| 48 | |
| 49 SpdyHeaderBlock(); | |
| 50 SpdyHeaderBlock(const SpdyHeaderBlock& other); | |
| 51 ~SpdyHeaderBlock(); | |
| 52 | |
| 53 SpdyHeaderBlock& operator=(const SpdyHeaderBlock& other); | |
| 54 | |
| 55 // These methods delegate to our MapType member. | |
| 56 iterator begin() { return block_.begin(); } | |
| 57 iterator end() { return block_.end(); } | |
| 58 const_iterator begin() const { return block_.begin(); } | |
| 59 const_iterator end() const { return block_.end(); } | |
| 60 bool empty() const { return block_.empty(); } | |
| 61 size_t size() const { return block_.size(); } | |
| 62 iterator find(StringPiece key) { return block_.find(key); } | |
| 63 const_iterator find(StringPiece key) const { return block_.find(key); } | |
| 64 reverse_iterator rbegin() { return block_.rbegin(); } | |
| 65 void erase(StringPiece key) { block_.erase(key); } | |
| 66 | |
| 67 // Clears both our MapType member and the memory used to hold headers. | |
| 68 void clear(); | |
| 69 | |
| 70 // These methods copy data into our backing storage. | |
| 71 void insert(const MapType::value_type& value); | |
| 72 void ReplaceOrAppendHeader(const StringPiece key, const StringPiece value); | |
| 73 | |
| 74 // Allows either lookup or mutation of the value associated with a key. | |
| 75 StringPieceProxy operator[](const StringPiece key); | |
| 76 | |
| 77 bool operator==(const SpdyHeaderBlock& other) const; | |
| 78 | |
| 79 // This object provides automatic conversions that allow SpdyHeaderBlock to be | |
| 80 // nearly a drop-in replacement for linked_hash_map<string, string>. It reads | |
| 81 // data from or writes data to a SpdyHeaderBlock::Storage. | |
| 82 class StringPieceProxy { | |
| 83 public: | |
| 84 ~StringPieceProxy(); | |
| 85 | |
| 86 // Assignment modifies the underlying SpdyHeaderBlock. | |
| 87 StringPieceProxy& operator=(const StringPiece other); | |
| 88 | |
| 89 // Allows a StringPieceProxy to be automatically converted to a StringPiece. | |
| 90 // This makes SpdyHeaderBlock::operator[] easy to use with StringPieces. | |
| 91 operator StringPiece() const; | |
| 92 | |
| 93 // Reserves |size| bytes in the underlying storage. | |
| 94 void reserve(size_t size); | |
| 95 | |
| 96 std::string as_string() const { | |
| 97 return static_cast<StringPiece>(*this).as_string(); | |
| 98 } | |
| 99 | |
| 100 private: | |
| 101 friend class SpdyHeaderBlock; | |
| 102 | |
| 103 StringPieceProxy(SpdyHeaderBlock::MapType* block, | |
| 104 SpdyHeaderBlock::Storage* storage, | |
| 105 SpdyHeaderBlock::MapType::iterator lookup_result, | |
| 106 const StringPiece key); | |
| 107 | |
| 108 SpdyHeaderBlock::MapType* block_; | |
| 109 SpdyHeaderBlock::Storage* storage_; | |
| 110 SpdyHeaderBlock::MapType::iterator lookup_result_; | |
| 111 const StringPiece key_; | |
| 112 | |
| 113 // Contains only POD members; explicitly copyable. | |
| 114 }; | |
| 115 | |
| 116 private: | |
| 117 void Write(const StringPiece s); | |
| 118 void AppendHeader(const StringPiece key, const StringPiece value); | |
| 119 | |
| 120 MapType block_; | |
| 121 scoped_ptr<Storage> storage_; | |
| 122 }; | |
| 19 | 123 |
| 20 // Converts a SpdyHeaderBlock into NetLog event parameters. | 124 // Converts a SpdyHeaderBlock into NetLog event parameters. |
| 21 NET_EXPORT scoped_ptr<base::Value> SpdyHeaderBlockNetLogCallback( | 125 NET_EXPORT scoped_ptr<base::Value> SpdyHeaderBlockNetLogCallback( |
| 22 const SpdyHeaderBlock* headers, | 126 const SpdyHeaderBlock* headers, |
| 23 NetLogCaptureMode capture_mode); | 127 NetLogCaptureMode capture_mode); |
| 24 | 128 |
| 25 // Converts NetLog event parameters into a SPDY header block and writes them | 129 // Converts NetLog event parameters into a SPDY header block and writes them |
| 26 // to |headers|. |event_param| must have been created by | 130 // to |headers|. |event_param| must have been created by |
| 27 // SpdyHeaderBlockNetLogCallback. On failure, returns false and clears | 131 // SpdyHeaderBlockNetLogCallback. On failure, returns false and clears |
| 28 // |headers|. | 132 // |headers|. |
| 29 NET_EXPORT bool SpdyHeaderBlockFromNetLogParam( | 133 NET_EXPORT bool SpdyHeaderBlockFromNetLogParam( |
| 30 const base::Value* event_param, | 134 const base::Value* event_param, |
| 31 SpdyHeaderBlock* headers); | 135 SpdyHeaderBlock* headers); |
| 32 | 136 |
| 33 } // namespace net | 137 } // namespace net |
| 34 | 138 |
| 35 #endif // NET_SPDY_SPDY_HEADER_BLOCK_H_ | 139 #endif // NET_SPDY_SPDY_HEADER_BLOCK_H_ |
| OLD | NEW |