OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/spdy_buffer.h" | 5 #include "net/spdy/spdy_buffer.h" |
6 | 6 |
7 #include <cstring> | 7 #include <cstring> |
8 | 8 |
| 9 #include "base/callback.h" |
9 #include "base/logging.h" | 10 #include "base/logging.h" |
10 #include "net/base/io_buffer.h" | 11 #include "net/base/io_buffer.h" |
11 #include "net/spdy/spdy_protocol.h" | 12 #include "net/spdy/spdy_protocol.h" |
12 | 13 |
13 namespace net { | 14 namespace net { |
14 | 15 |
15 namespace { | 16 namespace { |
16 | 17 |
17 // Makes a SpdyFrame with |size| bytes of data copied from | 18 // Makes a SpdyFrame with |size| bytes of data copied from |
18 // |data|. |data| must be non-NULL and |size| must be positive. | 19 // |data|. |data| must be non-NULL and |size| must be positive. |
(...skipping 12 matching lines...) Expand all Loading... |
31 SpdyBuffer::SpdyBuffer(scoped_ptr<SpdyFrame> frame) | 32 SpdyBuffer::SpdyBuffer(scoped_ptr<SpdyFrame> frame) |
32 : frame_(frame.Pass()), | 33 : frame_(frame.Pass()), |
33 offset_(0) {} | 34 offset_(0) {} |
34 | 35 |
35 // The given data may not be strictly a SPDY frame; we (ab)use | 36 // The given data may not be strictly a SPDY frame; we (ab)use |
36 // |frame_| just as a container. | 37 // |frame_| just as a container. |
37 SpdyBuffer::SpdyBuffer(const char* data, size_t size) : | 38 SpdyBuffer::SpdyBuffer(const char* data, size_t size) : |
38 frame_(MakeSpdyFrame(data, size)), | 39 frame_(MakeSpdyFrame(data, size)), |
39 offset_(0) {} | 40 offset_(0) {} |
40 | 41 |
41 SpdyBuffer::~SpdyBuffer() {} | 42 SpdyBuffer::~SpdyBuffer() { |
| 43 if (GetRemainingSize() > 0) |
| 44 Consume(GetRemainingSize()); |
| 45 } |
42 | 46 |
43 const char* SpdyBuffer::GetRemainingData() const { | 47 const char* SpdyBuffer::GetRemainingData() const { |
44 return frame_->data() + offset_; | 48 return frame_->data() + offset_; |
45 } | 49 } |
46 | 50 |
47 size_t SpdyBuffer::GetRemainingSize() const { | 51 size_t SpdyBuffer::GetRemainingSize() const { |
48 return frame_->size() - offset_; | 52 return frame_->size() - offset_; |
49 } | 53 } |
50 | 54 |
| 55 void SpdyBuffer::AddConsumeCallback(const ConsumeCallback& consume_callback) { |
| 56 consume_callbacks_.push_back(consume_callback); |
| 57 } |
| 58 |
51 void SpdyBuffer::Consume(size_t consume_size) { | 59 void SpdyBuffer::Consume(size_t consume_size) { |
52 DCHECK_GE(consume_size, 1u); | 60 DCHECK_GE(consume_size, 1u); |
53 DCHECK_LE(consume_size, GetRemainingSize()); | 61 DCHECK_LE(consume_size, GetRemainingSize()); |
54 offset_ += consume_size; | 62 offset_ += consume_size; |
| 63 for (std::vector<ConsumeCallback>::const_iterator it = |
| 64 consume_callbacks_.begin(); it != consume_callbacks_.end(); ++it) { |
| 65 it->Run(consume_size); |
| 66 } |
55 }; | 67 }; |
56 | 68 |
57 IOBuffer* SpdyBuffer::GetIOBufferForRemainingData() { | 69 IOBuffer* SpdyBuffer::GetIOBufferForRemainingData() { |
58 return new WrappedIOBuffer(GetRemainingData()); | 70 return new WrappedIOBuffer(GetRemainingData()); |
59 } | 71 } |
60 | 72 |
61 } // namespace net | 73 } // namespace net |
OLD | NEW |