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/spdy/hpack_entry.h"
7 #include "base/logging.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "net/spdy/hpack_string_util.h"
15 const uint32 kReferencedMask
= 0x80000000;
16 const uint32 kTouchCountMask
= 0x7fffffff;
20 const uint32
HpackEntry::kSizeOverhead
= 32;
22 const uint32
HpackEntry::kUntouched
= 0x7fffffff;
24 HpackEntry::HpackEntry() : referenced_and_touch_count_(kUntouched
) {}
26 HpackEntry::HpackEntry(base::StringPiece name
, base::StringPiece value
)
27 : name_(name
.as_string()),
28 value_(value
.as_string()),
29 referenced_and_touch_count_(kUntouched
) {}
31 bool HpackEntry::IsReferenced() const {
32 return ((referenced_and_touch_count_
& kReferencedMask
) != 0);
35 uint32
HpackEntry::TouchCount() const {
36 return referenced_and_touch_count_
& kTouchCountMask
;
39 size_t HpackEntry::Size() const {
40 return name_
.size() + value_
.size() + kSizeOverhead
;
43 std::string
HpackEntry::GetDebugString() const {
44 const char* is_referenced_str
= (IsReferenced() ? "true" : "false");
45 std::string touch_count_str
= "(untouched)";
46 if (TouchCount() != kUntouched
)
47 touch_count_str
= base::IntToString(TouchCount());
48 return "{ name: \"" + name_
+ "\", value: \"" + value_
+
49 "\", referenced: " + is_referenced_str
+ ", touch_count: " +
50 touch_count_str
+ " }";
53 bool HpackEntry::Equals(const HpackEntry
& other
) const {
55 StringPiecesEqualConstantTime(name_
, other
.name_
) &&
56 StringPiecesEqualConstantTime(value_
, other
.value_
) &&
57 (referenced_and_touch_count_
== other
.referenced_and_touch_count_
);
60 void HpackEntry::SetReferenced(bool referenced
) {
61 referenced_and_touch_count_
&= kTouchCountMask
;
63 referenced_and_touch_count_
|= kReferencedMask
;
66 void HpackEntry::AddTouches(uint32 additional_touch_count
) {
67 uint32 new_touch_count
= TouchCount();
68 if (new_touch_count
== kUntouched
)
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
;
76 void HpackEntry::ClearTouches() {
77 referenced_and_touch_count_
&= kReferencedMask
;
78 referenced_and_touch_count_
|= kUntouched
;