a9ab1b0518b507b2a7d4cc50cbe91200499e2e33
[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_LT(index, GetEntryCount());
24   return entries_[index];
25 }
26
27 HpackEntry* HpackHeaderTable::GetMutableEntry(uint32 index) {
28   CHECK_LT(index, GetEntryCount());
29   return &entries_[index];
30 }
31
32 void HpackHeaderTable::SetMaxSize(uint32 max_size) {
33   max_size_ = max_size;
34   while (size_ > max_size_) {
35     CHECK(!entries_.empty());
36     size_ -= entries_.back().Size();
37     entries_.pop_back();
38   }
39 }
40
41 void HpackHeaderTable::TryAddEntry(
42     const HpackEntry& entry,
43     int32* index,
44     std::vector<uint32>* removed_referenced_indices) {
45   *index = -1;
46   removed_referenced_indices->clear();
47
48   // The algorithm used here is described in 3.3.3. We're assuming
49   // that the given entry is caching the name/value.
50   size_t target_size = 0;
51   size_t size_t_max_size = static_cast<size_t>(max_size_);
52   if (entry.Size() <= size_t_max_size) {
53     // The conditional implies the difference can fit in 32 bits.
54     target_size = size_t_max_size - entry.Size();
55   }
56   while ((static_cast<size_t>(size_) > target_size) && !entries_.empty()) {
57     if (entries_.back().IsReferenced()) {
58       removed_referenced_indices->push_back(entries_.size() - 1);
59     }
60     size_ -= entries_.back().Size();
61     entries_.pop_back();
62   }
63
64   if (entry.Size() <= size_t_max_size) {
65     // Implied by the exit condition of the while loop above and the
66     // condition of the if.
67     DCHECK_LE(static_cast<size_t>(size_) + entry.Size(), size_t_max_size);
68     size_ += entry.Size();
69     *index = 0;
70     entries_.push_front(entry);
71   }
72 }
73
74 }  // namespace net