Replace MessageLoopProxy usage with ThreadTaskRunnerHandle in extensions module.
[chromium-blink-merge.git] / net / server / http_connection.cc
blob3401f8161d82977e845b1af6694caf3886b0167c
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
3 // found in the LICENSE file.
5 #include "net/server/http_connection.h"
7 #include "base/logging.h"
8 #include "net/server/web_socket.h"
9 #include "net/socket/stream_socket.h"
11 namespace net {
13 HttpConnection::ReadIOBuffer::ReadIOBuffer()
14 : base_(new GrowableIOBuffer()),
15 max_buffer_size_(kDefaultMaxBufferSize) {
16 SetCapacity(kInitialBufSize);
19 HttpConnection::ReadIOBuffer::~ReadIOBuffer() {
20 data_ = NULL; // base_ owns data_.
23 int HttpConnection::ReadIOBuffer::GetCapacity() const {
24 return base_->capacity();
27 void HttpConnection::ReadIOBuffer::SetCapacity(int capacity) {
28 DCHECK_LE(GetSize(), capacity);
29 base_->SetCapacity(capacity);
30 data_ = base_->data();
33 bool HttpConnection::ReadIOBuffer::IncreaseCapacity() {
34 if (GetCapacity() >= max_buffer_size_) {
35 LOG(ERROR) << "Too large read data is pending: capacity=" << GetCapacity()
36 << ", max_buffer_size=" << max_buffer_size_
37 << ", read=" << GetSize();
38 return false;
41 int new_capacity = GetCapacity() * kCapacityIncreaseFactor;
42 if (new_capacity > max_buffer_size_)
43 new_capacity = max_buffer_size_;
44 SetCapacity(new_capacity);
45 return true;
48 char* HttpConnection::ReadIOBuffer::StartOfBuffer() const {
49 return base_->StartOfBuffer();
52 int HttpConnection::ReadIOBuffer::GetSize() const {
53 return base_->offset();
56 void HttpConnection::ReadIOBuffer::DidRead(int bytes) {
57 DCHECK_GE(RemainingCapacity(), bytes);
58 base_->set_offset(base_->offset() + bytes);
59 data_ = base_->data();
62 int HttpConnection::ReadIOBuffer::RemainingCapacity() const {
63 return base_->RemainingCapacity();
66 void HttpConnection::ReadIOBuffer::DidConsume(int bytes) {
67 int previous_size = GetSize();
68 int unconsumed_size = previous_size - bytes;
69 DCHECK_LE(0, unconsumed_size);
70 if (unconsumed_size > 0) {
71 // Move unconsumed data to the start of buffer.
72 memmove(StartOfBuffer(), StartOfBuffer() + bytes, unconsumed_size);
74 base_->set_offset(unconsumed_size);
75 data_ = base_->data();
77 // If capacity is too big, reduce it.
78 if (GetCapacity() > kMinimumBufSize &&
79 GetCapacity() > previous_size * kCapacityIncreaseFactor) {
80 int new_capacity = GetCapacity() / kCapacityIncreaseFactor;
81 if (new_capacity < kMinimumBufSize)
82 new_capacity = kMinimumBufSize;
83 // realloc() within GrowableIOBuffer::SetCapacity() could move data even
84 // when size is reduced. If unconsumed_size == 0, i.e. no data exists in
85 // the buffer, free internal buffer first to guarantee no data move.
86 if (!unconsumed_size)
87 base_->SetCapacity(0);
88 SetCapacity(new_capacity);
92 HttpConnection::QueuedWriteIOBuffer::QueuedWriteIOBuffer()
93 : total_size_(0),
94 max_buffer_size_(kDefaultMaxBufferSize) {
97 HttpConnection::QueuedWriteIOBuffer::~QueuedWriteIOBuffer() {
98 data_ = NULL; // pending_data_ owns data_.
101 bool HttpConnection::QueuedWriteIOBuffer::IsEmpty() const {
102 return pending_data_.empty();
105 bool HttpConnection::QueuedWriteIOBuffer::Append(const std::string& data) {
106 if (data.empty())
107 return true;
109 if (total_size_ + static_cast<int>(data.size()) > max_buffer_size_) {
110 LOG(ERROR) << "Too large write data is pending: size="
111 << total_size_ + data.size()
112 << ", max_buffer_size=" << max_buffer_size_;
113 return false;
116 pending_data_.push(data);
117 total_size_ += data.size();
119 // If new data is the first pending data, updates data_.
120 if (pending_data_.size() == 1)
121 data_ = const_cast<char*>(pending_data_.front().data());
122 return true;
125 void HttpConnection::QueuedWriteIOBuffer::DidConsume(int size) {
126 DCHECK_GE(total_size_, size);
127 DCHECK_GE(GetSizeToWrite(), size);
128 if (size == 0)
129 return;
131 if (size < GetSizeToWrite()) {
132 data_ += size;
133 } else { // size == GetSizeToWrite(). Updates data_ to next pending data.
134 pending_data_.pop();
135 data_ = IsEmpty() ? NULL : const_cast<char*>(pending_data_.front().data());
137 total_size_ -= size;
140 int HttpConnection::QueuedWriteIOBuffer::GetSizeToWrite() const {
141 if (IsEmpty()) {
142 DCHECK_EQ(0, total_size_);
143 return 0;
145 DCHECK_GE(data_, pending_data_.front().data());
146 int consumed = static_cast<int>(data_ - pending_data_.front().data());
147 DCHECK_GT(static_cast<int>(pending_data_.front().size()), consumed);
148 return pending_data_.front().size() - consumed;
151 HttpConnection::HttpConnection(int id, scoped_ptr<StreamSocket> socket)
152 : id_(id),
153 socket_(socket.Pass()),
154 read_buf_(new ReadIOBuffer()),
155 write_buf_(new QueuedWriteIOBuffer()) {
158 HttpConnection::~HttpConnection() {
161 void HttpConnection::SetWebSocket(scoped_ptr<WebSocket> web_socket) {
162 DCHECK(!web_socket_);
163 web_socket_ = web_socket.Pass();
166 } // namespace net