xref: /aosp_15_r20/external/cronet/net/spdy/spdy_buffer.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2013 The Chromium Authors
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>
9 #include <memory>
10 #include <vector>
11 
12 #include "base/functional/callback_forward.h"
13 #include "base/memory/ref_counted.h"
14 #include "net/base/net_export.h"
15 
16 namespace spdy {
17 class SpdySerializedFrame;
18 }  // namespace spdy
19 
20 namespace net {
21 
22 class IOBuffer;
23 
24 // SpdyBuffer is a class to hold data read from or to be written to a
25 // SPDY connection. It is similar to a DrainableIOBuffer but is not
26 // ref-counted and will include a way to get notified when Consume()
27 // is called.
28 //
29 // NOTE(akalin): This explicitly does not inherit from IOBuffer to
30 // avoid the needless ref-counting and to avoid working around the
31 // fact that IOBuffer member functions are not virtual.
32 class NET_EXPORT_PRIVATE SpdyBuffer {
33  public:
34   // The source of a call to a ConsumeCallback.
35   enum ConsumeSource {
36     // Called via a call to Consume().
37     CONSUME,
38     // Called via the SpdyBuffer being destroyed.
39     DISCARD
40   };
41 
42   // A Callback that gets called when bytes are consumed with the
43   // (non-zero) number of bytes consumed and the source of the
44   // consume. May be called any number of times with CONSUME as the
45   // source followed by at most one call with DISCARD as the
46   // source. The sum of the number of bytes consumed equals the total
47   // size of the buffer.
48   typedef base::RepeatingCallback<void(size_t, ConsumeSource)> ConsumeCallback;
49 
50   // Construct with the data in the given frame. Assumes that data is
51   // owned by |frame| or outlives it.
52   explicit SpdyBuffer(std::unique_ptr<spdy::SpdySerializedFrame> frame);
53 
54   // Construct with a copy of the given raw data. |data| must be
55   // non-NULL and |size| must be non-zero.
56   SpdyBuffer(const char* data, size_t size);
57 
58   SpdyBuffer(const SpdyBuffer&) = delete;
59   SpdyBuffer& operator=(const SpdyBuffer&) = delete;
60 
61   // If there are bytes remaining in the buffer, triggers a call to
62   // any consume callbacks with a DISCARD source.
63   ~SpdyBuffer();
64 
65   // Returns the remaining (unconsumed) data.
66   const char* GetRemainingData() const;
67 
68   // Returns the number of remaining (unconsumed) bytes.
69   size_t GetRemainingSize() const;
70 
71   // Add a callback to be called when bytes are consumed. The
72   // ConsumeCallback should not do anything complicated; ideally it
73   // should only update a counter. In particular, it must *not* cause
74   // the SpdyBuffer itself to be destroyed.
75   void AddConsumeCallback(const ConsumeCallback& consume_callback);
76 
77   // Consume the given number of bytes, which must be positive but not
78   // greater than GetRemainingSize().
79   void Consume(size_t consume_size);
80 
81   // Returns an IOBuffer pointing to the data starting at
82   // GetRemainingData(). Use with care; the returned IOBuffer is not
83   // updated when Consume() is called. However, it may still be used
84   // past the lifetime of this object.
85   //
86   // This is used with Socket::Write(), which takes an IOBuffer* that
87   // may be written to even after the socket itself is destroyed. (See
88   // http://crbug.com/249725 .)
89   scoped_refptr<IOBuffer> GetIOBufferForRemainingData();
90 
91  private:
92   void ConsumeHelper(size_t consume_size, ConsumeSource consume_source);
93 
94   // Ref-count the passed-in spdy::SpdySerializedFrame to support the semantics
95   // of |GetIOBufferForRemainingData()|.
96   typedef base::RefCountedData<std::unique_ptr<spdy::SpdySerializedFrame>>
97       SharedFrame;
98 
99   class SharedFrameIOBuffer;
100 
101   const scoped_refptr<SharedFrame> shared_frame_;
102   std::vector<ConsumeCallback> consume_callbacks_;
103   size_t offset_ = 0;
104 };
105 
106 }  // namespace net
107 
108 #endif  // NET_SPDY_SPDY_BUFFER_H_
109