| 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 #include "net/spdy/spdy_header_block.h" | 5 #include "net/spdy/spdy_header_block.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "net/base/arena.h" | 15 #include "net/base/arena.h" |
| 16 #include "net/http/http_log_util.h" | 16 #include "net/http/http_log_util.h" |
| 17 #include "net/log/net_log_capture_mode.h" | 17 #include "net/log/net_log_capture_mode.h" |
| 18 #include "net/spdy/platform/api/spdy_estimate_memory_usage.h" | 18 #include "net/spdy/platform/api/spdy_estimate_memory_usage.h" |
| 19 #include "net/spdy/platform/api/spdy_string_utils.h" | 19 #include "net/spdy/platform/api/spdy_string_utils.h" |
| 20 | 20 |
| 21 using std::string; | |
| 22 | |
| 23 namespace net { | 21 namespace net { |
| 24 namespace { | 22 namespace { |
| 25 | 23 |
| 26 // By default, linked_hash_map's internal map allocates space for 100 map | 24 // By default, linked_hash_map's internal map allocates space for 100 map |
| 27 // buckets on construction, which is larger than necessary. Standard library | 25 // buckets on construction, which is larger than necessary. Standard library |
| 28 // unordered map implementations use a list of prime numbers to set the bucket | 26 // unordered map implementations use a list of prime numbers to set the bucket |
| 29 // count for a particular capacity. |kInitialMapBuckets| is chosen to reduce | 27 // count for a particular capacity. |kInitialMapBuckets| is chosen to reduce |
| 30 // memory usage for small header blocks, at the cost of having to rehash for | 28 // memory usage for small header blocks, at the cost of having to rehash for |
| 31 // large header blocks. | 29 // large header blocks. |
| 32 const size_t kInitialMapBuckets = 11; | 30 const size_t kInitialMapBuckets = 11; |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 key_, HeaderValue(storage_, key_, storage_->Write(value)))) | 200 key_, HeaderValue(storage_, key_, storage_->Write(value)))) |
| 203 .first; | 201 .first; |
| 204 } else { | 202 } else { |
| 205 DVLOG(1) << "Updating key: " << key_ << " with value: " << value; | 203 DVLOG(1) << "Updating key: " << key_ << " with value: " << value; |
| 206 lookup_result_->second = | 204 lookup_result_->second = |
| 207 HeaderValue(storage_, key_, storage_->Write(value)); | 205 HeaderValue(storage_, key_, storage_->Write(value)); |
| 208 } | 206 } |
| 209 return *this; | 207 return *this; |
| 210 } | 208 } |
| 211 | 209 |
| 212 string SpdyHeaderBlock::ValueProxy::as_string() const { | 210 SpdyString SpdyHeaderBlock::ValueProxy::as_string() const { |
| 213 if (lookup_result_ == block_->end()) { | 211 if (lookup_result_ == block_->end()) { |
| 214 return ""; | 212 return ""; |
| 215 } else { | 213 } else { |
| 216 return std::string(lookup_result_->second.value()); | 214 return SpdyString(lookup_result_->second.value()); |
| 217 } | 215 } |
| 218 } | 216 } |
| 219 | 217 |
| 220 SpdyHeaderBlock::SpdyHeaderBlock() : block_(kInitialMapBuckets) {} | 218 SpdyHeaderBlock::SpdyHeaderBlock() : block_(kInitialMapBuckets) {} |
| 221 | 219 |
| 222 SpdyHeaderBlock::SpdyHeaderBlock(SpdyHeaderBlock&& other) = default; | 220 SpdyHeaderBlock::SpdyHeaderBlock(SpdyHeaderBlock&& other) = default; |
| 223 | 221 |
| 224 SpdyHeaderBlock::~SpdyHeaderBlock() {} | 222 SpdyHeaderBlock::~SpdyHeaderBlock() {} |
| 225 | 223 |
| 226 SpdyHeaderBlock& SpdyHeaderBlock::operator=(SpdyHeaderBlock&& other) { | 224 SpdyHeaderBlock& SpdyHeaderBlock::operator=(SpdyHeaderBlock&& other) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 238 } | 236 } |
| 239 | 237 |
| 240 bool SpdyHeaderBlock::operator==(const SpdyHeaderBlock& other) const { | 238 bool SpdyHeaderBlock::operator==(const SpdyHeaderBlock& other) const { |
| 241 return size() == other.size() && std::equal(begin(), end(), other.begin()); | 239 return size() == other.size() && std::equal(begin(), end(), other.begin()); |
| 242 } | 240 } |
| 243 | 241 |
| 244 bool SpdyHeaderBlock::operator!=(const SpdyHeaderBlock& other) const { | 242 bool SpdyHeaderBlock::operator!=(const SpdyHeaderBlock& other) const { |
| 245 return !(operator==(other)); | 243 return !(operator==(other)); |
| 246 } | 244 } |
| 247 | 245 |
| 248 string SpdyHeaderBlock::DebugString() const { | 246 SpdyString SpdyHeaderBlock::DebugString() const { |
| 249 if (empty()) { | 247 if (empty()) { |
| 250 return "{}"; | 248 return "{}"; |
| 251 } | 249 } |
| 252 | 250 |
| 253 string output = "\n{\n"; | 251 SpdyString output = "\n{\n"; |
| 254 for (auto it = begin(); it != end(); ++it) { | 252 for (auto it = begin(); it != end(); ++it) { |
| 255 SpdyStrAppend(&output, " ", it->first, " ", it->second, "\n"); | 253 SpdyStrAppend(&output, " ", it->first, " ", it->second, "\n"); |
| 256 } | 254 } |
| 257 SpdyStrAppend(&output, "}\n"); | 255 SpdyStrAppend(&output, "}\n"); |
| 258 return output; | 256 return output; |
| 259 } | 257 } |
| 260 | 258 |
| 261 void SpdyHeaderBlock::clear() { | 259 void SpdyHeaderBlock::clear() { |
| 262 block_.clear(); | 260 block_.clear(); |
| 263 storage_.reset(); | 261 storage_.reset(); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 const base::DictionaryValue* header_dict = NULL; | 352 const base::DictionaryValue* header_dict = NULL; |
| 355 | 353 |
| 356 if (!event_param || | 354 if (!event_param || |
| 357 !event_param->GetAsDictionary(&dict) || | 355 !event_param->GetAsDictionary(&dict) || |
| 358 !dict->GetDictionary("headers", &header_dict)) { | 356 !dict->GetDictionary("headers", &header_dict)) { |
| 359 return false; | 357 return false; |
| 360 } | 358 } |
| 361 | 359 |
| 362 for (base::DictionaryValue::Iterator it(*header_dict); !it.IsAtEnd(); | 360 for (base::DictionaryValue::Iterator it(*header_dict); !it.IsAtEnd(); |
| 363 it.Advance()) { | 361 it.Advance()) { |
| 364 string value; | 362 SpdyString value; |
| 365 if (!it.value().GetAsString(&value)) { | 363 if (!it.value().GetAsString(&value)) { |
| 366 headers->clear(); | 364 headers->clear(); |
| 367 return false; | 365 return false; |
| 368 } | 366 } |
| 369 (*headers)[it.key()] = value; | 367 (*headers)[it.key()] = value; |
| 370 } | 368 } |
| 371 return true; | 369 return true; |
| 372 } | 370 } |
| 373 | 371 |
| 374 size_t SpdyHeaderBlock::bytes_allocated() const { | 372 size_t SpdyHeaderBlock::bytes_allocated() const { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 392 for (++it; it != fragments.end(); ++it) { | 390 for (++it; it != fragments.end(); ++it) { |
| 393 memcpy(dst, separator.data(), separator.size()); | 391 memcpy(dst, separator.data(), separator.size()); |
| 394 dst += separator.size(); | 392 dst += separator.size(); |
| 395 memcpy(dst, it->data(), it->size()); | 393 memcpy(dst, it->data(), it->size()); |
| 396 dst += it->size(); | 394 dst += it->size(); |
| 397 } | 395 } |
| 398 return dst - original_dst; | 396 return dst - original_dst; |
| 399 } | 397 } |
| 400 | 398 |
| 401 } // namespace net | 399 } // namespace net |
| OLD | NEW |