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
3 // found in the LICENSE file.
5 #include "net/spdy/spdy_buffer.h"
9 #include "base/callback.h"
10 #include "base/logging.h"
11 #include "net/base/io_buffer.h"
12 #include "net/spdy/spdy_protocol.h"
18 // Makes a SpdyFrame with |size| bytes of data copied from
19 // |data|. |data| must be non-NULL and |size| must be positive.
20 scoped_ptr
<SpdyFrame
> MakeSpdyFrame(const char* data
, size_t size
) {
23 scoped_ptr
<char[]> frame_data(new char[size
]);
24 std::memcpy(frame_data
.get(), data
, size
);
25 scoped_ptr
<SpdyFrame
> frame(
26 new SpdyFrame(frame_data
.release(), size
, true /* owns_buffer */));
32 SpdyBuffer::SpdyBuffer(scoped_ptr
<SpdyFrame
> frame
)
33 : frame_(frame
.Pass()),
36 // The given data may not be strictly a SPDY frame; we (ab)use
37 // |frame_| just as a container.
38 SpdyBuffer::SpdyBuffer(const char* data
, size_t size
) :
39 frame_(MakeSpdyFrame(data
, size
)),
42 SpdyBuffer::~SpdyBuffer() {
43 if (GetRemainingSize() > 0)
44 ConsumeHelper(GetRemainingSize(), DISCARD
);
47 const char* SpdyBuffer::GetRemainingData() const {
48 return frame_
->data() + offset_
;
51 size_t SpdyBuffer::GetRemainingSize() const {
52 return frame_
->size() - offset_
;
55 void SpdyBuffer::AddConsumeCallback(const ConsumeCallback
& consume_callback
) {
56 consume_callbacks_
.push_back(consume_callback
);
59 void SpdyBuffer::Consume(size_t consume_size
) {
60 ConsumeHelper(consume_size
, CONSUME
);
63 IOBuffer
* SpdyBuffer::GetIOBufferForRemainingData() {
64 return new WrappedIOBuffer(GetRemainingData());
67 void SpdyBuffer::ConsumeHelper(size_t consume_size
,
68 ConsumeSource consume_source
) {
69 DCHECK_GE(consume_size
, 1u);
70 DCHECK_LE(consume_size
, GetRemainingSize());
71 offset_
+= consume_size
;
72 for (std::vector
<ConsumeCallback
>::const_iterator it
=
73 consume_callbacks_
.begin(); it
!= consume_callbacks_
.end(); ++it
) {
74 it
->Run(consume_size
, consume_source
);