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 #include "net/spdy/spdy_buffer.h"
6
7 #include <cstring>
8 #include <utility>
9
10 #include "base/check_op.h"
11 #include "base/functional/callback.h"
12 #include "base/trace_event/memory_usage_estimator.h"
13 #include "net/base/io_buffer.h"
14 #include "net/third_party/quiche/src/quiche/spdy/core/spdy_protocol.h"
15
16 namespace net {
17
18 namespace {
19
20 // Bound on largest frame any SPDY version has allowed.
21 const size_t kMaxSpdyFrameSize = 0x00ffffff;
22
23 // Makes a spdy::SpdySerializedFrame with |size| bytes of data copied from
24 // |data|. |data| must be non-NULL and |size| must be positive.
MakeSpdySerializedFrame(const char * data,size_t size)25 std::unique_ptr<spdy::SpdySerializedFrame> MakeSpdySerializedFrame(
26 const char* data,
27 size_t size) {
28 DCHECK(data);
29 CHECK_GT(size, 0u);
30 CHECK_LE(size, kMaxSpdyFrameSize);
31
32 auto frame_data = std::make_unique<char[]>(size);
33 std::memcpy(frame_data.get(), data, size);
34 return std::make_unique<spdy::SpdySerializedFrame>(frame_data.release(), size,
35 true /* owns_buffer */);
36 }
37
38 } // namespace
39
40 // This class is an IOBuffer implementation that simply holds a
41 // reference to a SharedFrame object and a fixed offset. Used by
42 // SpdyBuffer::GetIOBufferForRemainingData().
43 class SpdyBuffer::SharedFrameIOBuffer : public IOBuffer {
44 public:
SharedFrameIOBuffer(const scoped_refptr<SharedFrame> & shared_frame,size_t offset)45 SharedFrameIOBuffer(const scoped_refptr<SharedFrame>& shared_frame,
46 size_t offset)
47 : IOBuffer(shared_frame->data->data() + offset),
48 shared_frame_(shared_frame) {}
49
50 SharedFrameIOBuffer(const SharedFrameIOBuffer&) = delete;
51 SharedFrameIOBuffer& operator=(const SharedFrameIOBuffer&) = delete;
52
53 private:
~SharedFrameIOBuffer()54 ~SharedFrameIOBuffer() override {
55 // Prevent ~IOBuffer() from trying to delete |data_|.
56 data_ = nullptr;
57 }
58
59 const scoped_refptr<SharedFrame> shared_frame_;
60 };
61
SpdyBuffer(std::unique_ptr<spdy::SpdySerializedFrame> frame)62 SpdyBuffer::SpdyBuffer(std::unique_ptr<spdy::SpdySerializedFrame> frame)
63 : shared_frame_(base::MakeRefCounted<SharedFrame>(std::move(frame))) {}
64
65 // The given data may not be strictly a SPDY frame; we (ab)use
66 // |frame_| just as a container.
SpdyBuffer(const char * data,size_t size)67 SpdyBuffer::SpdyBuffer(const char* data, size_t size)
68 : shared_frame_(base::MakeRefCounted<SharedFrame>()) {
69 CHECK_GT(size, 0u);
70 CHECK_LE(size, kMaxSpdyFrameSize);
71 shared_frame_->data = MakeSpdySerializedFrame(data, size);
72 }
73
~SpdyBuffer()74 SpdyBuffer::~SpdyBuffer() {
75 if (GetRemainingSize() > 0)
76 ConsumeHelper(GetRemainingSize(), DISCARD);
77 }
78
GetRemainingData() const79 const char* SpdyBuffer::GetRemainingData() const {
80 return shared_frame_->data->data() + offset_;
81 }
82
GetRemainingSize() const83 size_t SpdyBuffer::GetRemainingSize() const {
84 return shared_frame_->data->size() - offset_;
85 }
86
AddConsumeCallback(const ConsumeCallback & consume_callback)87 void SpdyBuffer::AddConsumeCallback(const ConsumeCallback& consume_callback) {
88 consume_callbacks_.push_back(consume_callback);
89 }
90
Consume(size_t consume_size)91 void SpdyBuffer::Consume(size_t consume_size) {
92 ConsumeHelper(consume_size, CONSUME);
93 }
94
GetIOBufferForRemainingData()95 scoped_refptr<IOBuffer> SpdyBuffer::GetIOBufferForRemainingData() {
96 return base::MakeRefCounted<SharedFrameIOBuffer>(shared_frame_, offset_);
97 }
98
ConsumeHelper(size_t consume_size,ConsumeSource consume_source)99 void SpdyBuffer::ConsumeHelper(size_t consume_size,
100 ConsumeSource consume_source) {
101 DCHECK_GE(consume_size, 1u);
102 DCHECK_LE(consume_size, GetRemainingSize());
103 offset_ += consume_size;
104 for (std::vector<ConsumeCallback>::const_iterator it =
105 consume_callbacks_.begin(); it != consume_callbacks_.end(); ++it) {
106 it->Run(consume_size, consume_source);
107 }
108 }
109
110 } // namespace net
111