blob: 94ee97fe6d5ab40889084e73986b86e41d53b734 [file] [log] [blame]
[email protected]ca690b02013-04-17 10:38:431// 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.
4
5#ifndef NET_SPDY_SPDY_BUFFER_H_
6#define NET_SPDY_SPDY_BUFFER_H_
7
8#include <cstddef>
[email protected]09a8d9172013-04-17 19:23:499#include <vector>
[email protected]ca690b02013-04-17 10:38:4310
[email protected]09a8d9172013-04-17 19:23:4911#include "base/callback_forward.h"
Avi Drissman13fc8932015-12-20 04:40:4612#include "base/macros.h"
[email protected]08881f9a2013-07-12 19:30:3113#include "base/memory/ref_counted.h"
[email protected]ca690b02013-04-17 10:38:4314#include "base/memory/scoped_ptr.h"
15#include "net/base/net_export.h"
16
17namespace net {
18
19class IOBuffer;
20class SpdyFrame;
21
22// SpdyBuffer is a class to hold data read from or to be written to a
23// SPDY connection. It is similar to a DrainableIOBuffer but is not
24// ref-counted and will include a way to get notified when Consume()
25// is called.
26//
27// NOTE(akalin): This explicitly does not inherit from IOBuffer to
28// avoid the needless ref-counting and to avoid working around the
29// fact that IOBuffer member functions are not virtual.
30class NET_EXPORT_PRIVATE SpdyBuffer {
31 public:
[email protected]8a938fed2013-04-18 08:31:5832 // The source of a call to a ConsumeCallback.
33 enum ConsumeSource {
34 // Called via a call to Consume().
35 CONSUME,
36 // Called via the SpdyBuffer being destroyed.
37 DISCARD
38 };
39
40 // A Callback that gets called when bytes are consumed with the
41 // (non-zero) number of bytes consumed and the source of the
42 // consume. May be called any number of times with CONSUME as the
43 // source followed by at most one call with DISCARD as the
44 // source. The sum of the number of bytes consumed equals the total
45 // size of the buffer.
46 typedef base::Callback<void(size_t, ConsumeSource)> ConsumeCallback;
[email protected]09a8d9172013-04-17 19:23:4947
[email protected]ca690b02013-04-17 10:38:4348 // Construct with the data in the given frame. Assumes that data is
49 // owned by |frame| or outlives it.
50 explicit SpdyBuffer(scoped_ptr<SpdyFrame> frame);
51
52 // Construct with a copy of the given raw data. |data| must be
53 // non-NULL and |size| must be non-zero.
54 SpdyBuffer(const char* data, size_t size);
55
[email protected]8a938fed2013-04-18 08:31:5856 // If there are bytes remaining in the buffer, triggers a call to
57 // any consume callbacks with a DISCARD source.
[email protected]ca690b02013-04-17 10:38:4358 ~SpdyBuffer();
59
60 // Returns the remaining (unconsumed) data.
61 const char* GetRemainingData() const;
62
63 // Returns the number of remaining (unconsumed) bytes.
64 size_t GetRemainingSize() const;
65
[email protected]8a938fed2013-04-18 08:31:5866 // Add a callback to be called when bytes are consumed. The
67 // ConsumeCallback should not do anything complicated; ideally it
68 // should only update a counter. In particular, it must *not* cause
69 // the SpdyBuffer itself to be destroyed.
[email protected]09a8d9172013-04-17 19:23:4970 void AddConsumeCallback(const ConsumeCallback& consume_callback);
71
[email protected]ca690b02013-04-17 10:38:4372 // Consume the given number of bytes, which must be positive but not
73 // greater than GetRemainingSize().
[email protected]ca690b02013-04-17 10:38:4374 void Consume(size_t consume_size);
75
76 // Returns an IOBuffer pointing to the data starting at
[email protected]08881f9a2013-07-12 19:30:3177 // GetRemainingData(). Use with care; the returned IOBuffer is not
78 // updated when Consume() is called. However, it may still be used
79 // past the lifetime of this object.
80 //
81 // This is used with Socket::Write(), which takes an IOBuffer* that
82 // may be written to even after the socket itself is destroyed. (See
83 // http://crbug.com/249725 .)
[email protected]ca690b02013-04-17 10:38:4384 IOBuffer* GetIOBufferForRemainingData();
85
86 private:
[email protected]8a938fed2013-04-18 08:31:5887 void ConsumeHelper(size_t consume_size, ConsumeSource consume_source);
88
[email protected]08881f9a2013-07-12 19:30:3189 // Ref-count the passed-in SpdyFrame to support the semantics of
90 // |GetIOBufferForRemainingData()|.
91 typedef base::RefCountedData<scoped_ptr<SpdyFrame> > SharedFrame;
92
93 class SharedFrameIOBuffer;
94
95 const scoped_refptr<SharedFrame> shared_frame_;
[email protected]09a8d9172013-04-17 19:23:4996 std::vector<ConsumeCallback> consume_callbacks_;
[email protected]ca690b02013-04-17 10:38:4397 size_t offset_;
98
99 DISALLOW_COPY_AND_ASSIGN(SpdyBuffer);
100};
101
102} // namespace net
103
104#endif // NET_SPDY_SPDY_BUFFER_H_