spdy_buffer.h 3.7 KB

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