Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / net / spdy / hpack_header_table.cc
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.
4
5 #include "net/spdy/hpack_header_table.h"
6
7 #include "base/logging.h"
8 #include "net/spdy/hpack_string_util.h"
9
10 namespace net {
11
12 HpackHeaderTable::HpackHeaderTable() : size_(0), max_size_(4096) {}
13
14 HpackHeaderTable::~HpackHeaderTable() {}
15
16 uint32 HpackHeaderTable::GetEntryCount() const {
17   size_t size = entries_.size();
18   DCHECK_LE(size, kuint32max);
19   return static_cast<uint32>(size);
20 }
21
22 const HpackEntry& HpackHeaderTable::GetEntry(uint32 index) const {
23   CHECK_GE(index, 1u);
24   CHECK_LE(index, GetEntryCount());
25   return entries_[index-1];
26 }
27
28 HpackEntry* HpackHeaderTable::GetMutableEntry(uint32 index) {
29   CHECK_GE(index, 1u);
30   CHECK_LE(index, GetEntryCount());
31   return &entries_[index-1];
32 }
33
34 void HpackHeaderTable::SetMaxSize(uint32 max_size) {
35   max_size_ = max_size;
36   while (size_ > max_size_) {
37     CHECK(!entries_.empty());
38     size_ -= entries_.back().Size();
39     entries_.pop_back();
40   }
41 }
42
43 void HpackHeaderTable::TryAddEntry(
44     const HpackEntry& entry,
45     uint32* index,
46     std::vector<uint32>* removed_referenced_indices) {
47   *index = 0;
48   removed_referenced_indices->clear();
49
50   // The algorithm used here is described in 3.3.3. We're assuming
51   // that the given entry is caching the name/value.
52   size_t target_size = 0;
53   size_t size_t_max_size = static_cast<size_t>(max_size_);
54   if (entry.Size() <= size_t_max_size) {
55     // The conditional implies the difference can fit in 32 bits.
56     target_size = size_t_max_size - entry.Size();
57   }
58   while (static_cast<size_t>(size_) > target_size) {
59     DCHECK(!entries_.empty());
60     if (entries_.back().IsReferenced()) {
61       removed_referenced_indices->push_back(entries_.size());
62     }
63     size_ -= entries_.back().Size();
64     entries_.pop_back();
65   }
66
67   if (entry.Size() <= size_t_max_size) {
68     // Implied by the exit condition of the while loop above and the
69     // condition of the if.
70     DCHECK_LE(static_cast<size_t>(size_) + entry.Size(), size_t_max_size);
71     size_ += entry.Size();
72     *index = 1;
73     entries_.push_front(entry);
74   }
75 }
76
77 }  // namespace net