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 #ifndef NET_SPDY_SPDY_BUFFER_H_
6 #define NET_SPDY_SPDY_BUFFER_H_
11 #include "base/basictypes.h"
12 #include "base/callback_forward.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "net/base/net_export.h"
21 // SpdyBuffer is a class to hold data read from or to be written to a
22 // SPDY connection. It is similar to a DrainableIOBuffer but is not
23 // ref-counted and will include a way to get notified when Consume()
26 // NOTE(akalin): This explicitly does not inherit from IOBuffer to
27 // avoid the needless ref-counting and to avoid working around the
28 // fact that IOBuffer member functions are not virtual.
29 class NET_EXPORT_PRIVATE SpdyBuffer
{
31 // The source of a call to a ConsumeCallback.
33 // Called via a call to Consume().
35 // Called via the SpdyBuffer being destroyed.
39 // A Callback that gets called when bytes are consumed with the
40 // (non-zero) number of bytes consumed and the source of the
41 // consume. May be called any number of times with CONSUME as the
42 // source followed by at most one call with DISCARD as the
43 // source. The sum of the number of bytes consumed equals the total
44 // size of the buffer.
45 typedef base::Callback
<void(size_t, ConsumeSource
)> ConsumeCallback
;
47 // Construct with the data in the given frame. Assumes that data is
48 // owned by |frame| or outlives it.
49 explicit SpdyBuffer(scoped_ptr
<SpdyFrame
> frame
);
51 // Construct with a copy of the given raw data. |data| must be
52 // non-NULL and |size| must be non-zero.
53 SpdyBuffer(const char* data
, size_t size
);
55 // If there are bytes remaining in the buffer, triggers a call to
56 // any consume callbacks with a DISCARD source.
59 // Returns the remaining (unconsumed) data.
60 const char* GetRemainingData() const;
62 // Returns the number of remaining (unconsumed) bytes.
63 size_t GetRemainingSize() const;
65 // Add a callback to be called when bytes are consumed. The
66 // ConsumeCallback should not do anything complicated; ideally it
67 // should only update a counter. In particular, it must *not* cause
68 // the SpdyBuffer itself to be destroyed.
69 void AddConsumeCallback(const ConsumeCallback
& consume_callback
);
71 // Consume the given number of bytes, which must be positive but not
72 // greater than GetRemainingSize().
73 void Consume(size_t consume_size
);
75 // Returns an IOBuffer pointing to the data starting at
76 // GetRemainingData(). Use with care; the returned IOBuffer must not
77 // be used past the lifetime of this object, and it is not updated
78 // when Consume() is called.
79 IOBuffer
* GetIOBufferForRemainingData();
82 void ConsumeHelper(size_t consume_size
, ConsumeSource consume_source
);
84 const scoped_ptr
<SpdyFrame
> frame_
;
85 std::vector
<ConsumeCallback
> consume_callbacks_
;
88 DISALLOW_COPY_AND_ASSIGN(SpdyBuffer
);
93 #endif // NET_SPDY_SPDY_BUFFER_H_