| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/hpack_entry.h" | 5 #include "net/spdy/hpack_entry.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "net/spdy/hpack_string_util.h" | 9 #include "net/spdy/hpack_string_util.h" |
| 10 | 10 |
| 11 namespace net { | 11 namespace net { |
| 12 | 12 |
| 13 namespace { | 13 using base::StringPiece; |
| 14 | 14 |
| 15 const uint32 kReferencedMask = 0x80000000; | 15 const size_t HpackEntry::kSizeOverhead = 32; |
| 16 const uint32 kTouchCountMask = 0x7fffffff; | |
| 17 | 16 |
| 18 } // namespace | 17 bool HpackEntry::Comparator::operator() ( |
| 19 | 18 const HpackEntry* lhs, const HpackEntry* rhs) const { |
| 20 const uint32 HpackEntry::kSizeOverhead = 32; | 19 int result = lhs->name().compare(rhs->name()); |
| 21 | 20 if (result != 0) |
| 22 const uint32 HpackEntry::kUntouched = 0x7fffffff; | 21 return result < 0; |
| 23 | 22 result = lhs->value().compare(rhs->value()); |
| 24 HpackEntry::HpackEntry() : referenced_and_touch_count_(kUntouched) {} | 23 if (result != 0) |
| 25 | 24 return result < 0; |
| 26 HpackEntry::HpackEntry(base::StringPiece name, base::StringPiece value) | 25 DCHECK(lhs == rhs || lhs->Index() != rhs->Index()); |
| 27 : name_(name.as_string()), | 26 return lhs->Index() < rhs->Index(); |
| 28 value_(value.as_string()), | |
| 29 referenced_and_touch_count_(kUntouched) {} | |
| 30 | |
| 31 bool HpackEntry::IsReferenced() const { | |
| 32 return ((referenced_and_touch_count_ & kReferencedMask) != 0); | |
| 33 } | 27 } |
| 34 | 28 |
| 35 uint32 HpackEntry::TouchCount() const { | 29 HpackEntry::HpackEntry(StringPiece name, |
| 36 return referenced_and_touch_count_ & kTouchCountMask; | 30 StringPiece value, |
| 31 bool is_static, |
| 32 size_t insertion_index, |
| 33 const size_t* total_table_insertions_or_current_size) |
| 34 : name_(name.data(), name.size()), |
| 35 value_(value.data(), value.size()), |
| 36 is_static_(is_static), |
| 37 state_(0), |
| 38 insertion_index_(insertion_index), |
| 39 total_insertions_or_size_(total_table_insertions_or_current_size) { |
| 40 CHECK_NE(total_table_insertions_or_current_size, |
| 41 static_cast<const size_t*>(NULL)); |
| 37 } | 42 } |
| 38 | 43 |
| 44 HpackEntry::HpackEntry(StringPiece name, StringPiece value) |
| 45 : name_(name.data(), name.size()), |
| 46 value_(value.data(), value.size()), |
| 47 is_static_(false), |
| 48 state_(0), |
| 49 insertion_index_(0), |
| 50 total_insertions_or_size_(NULL) { |
| 51 } |
| 52 |
| 53 HpackEntry::HpackEntry() |
| 54 : is_static_(false), |
| 55 state_(0), |
| 56 insertion_index_(0), |
| 57 total_insertions_or_size_(NULL) { |
| 58 } |
| 59 |
| 60 HpackEntry::~HpackEntry() {} |
| 61 |
| 62 size_t HpackEntry::Index() const { |
| 63 if (total_insertions_or_size_ == NULL) { |
| 64 // This is a lookup instance. |
| 65 return 0; |
| 66 } else if (IsStatic()) { |
| 67 return 1 + insertion_index_ + *total_insertions_or_size_; |
| 68 } else { |
| 69 return *total_insertions_or_size_ - insertion_index_; |
| 70 } |
| 71 } |
| 72 |
| 73 // static |
| 74 size_t HpackEntry::Size(StringPiece name, StringPiece value) { |
| 75 return name.size() + value.size() + kSizeOverhead; |
| 76 } |
| 39 size_t HpackEntry::Size() const { | 77 size_t HpackEntry::Size() const { |
| 40 return name_.size() + value_.size() + kSizeOverhead; | 78 return Size(name(), value()); |
| 41 } | 79 } |
| 42 | 80 |
| 43 std::string HpackEntry::GetDebugString() const { | 81 std::string HpackEntry::GetDebugString() const { |
| 44 const char* is_referenced_str = (IsReferenced() ? "true" : "false"); | 82 return "{ name: \"" + name_ + |
| 45 std::string touch_count_str = "(untouched)"; | 83 "\", value: \"" + value_ + |
| 46 if (TouchCount() != kUntouched) | 84 "\", " + (IsStatic() ? "static" : "dynamic") + |
| 47 touch_count_str = base::IntToString(TouchCount()); | 85 ", state: " + base::IntToString(state_) + " }"; |
| 48 return "{ name: \"" + name_ + "\", value: \"" + value_ + | |
| 49 "\", referenced: " + is_referenced_str + ", touch_count: " + | |
| 50 touch_count_str + " }"; | |
| 51 } | |
| 52 | |
| 53 bool HpackEntry::Equals(const HpackEntry& other) const { | |
| 54 return | |
| 55 StringPiecesEqualConstantTime(name_, other.name_) && | |
| 56 StringPiecesEqualConstantTime(value_, other.value_) && | |
| 57 (referenced_and_touch_count_ == other.referenced_and_touch_count_); | |
| 58 } | |
| 59 | |
| 60 void HpackEntry::SetReferenced(bool referenced) { | |
| 61 referenced_and_touch_count_ &= kTouchCountMask; | |
| 62 if (referenced) | |
| 63 referenced_and_touch_count_ |= kReferencedMask; | |
| 64 } | |
| 65 | |
| 66 void HpackEntry::AddTouches(uint32 additional_touch_count) { | |
| 67 uint32 new_touch_count = TouchCount(); | |
| 68 if (new_touch_count == kUntouched) | |
| 69 new_touch_count = 0; | |
| 70 new_touch_count += additional_touch_count; | |
| 71 DCHECK_LT(new_touch_count, kUntouched); | |
| 72 referenced_and_touch_count_ &= kReferencedMask; | |
| 73 referenced_and_touch_count_ |= new_touch_count; | |
| 74 } | |
| 75 | |
| 76 void HpackEntry::ClearTouches() { | |
| 77 referenced_and_touch_count_ &= kReferencedMask; | |
| 78 referenced_and_touch_count_ |= kUntouched; | |
| 79 } | 86 } |
| 80 | 87 |
| 81 } // namespace net | 88 } // namespace net |
| OLD | NEW |