| OLD | NEW |
| (Empty) |
| 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 | |
| 5 #include "net/spdy/spdy_buffer.h" | |
| 6 | |
| 7 #include <cstring> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/callback.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "base/macros.h" | |
| 13 #include "net/base/io_buffer.h" | |
| 14 #include "net/spdy/platform/api/spdy_estimate_memory_usage.h" | |
| 15 #include "net/spdy/spdy_protocol.h" | |
| 16 | |
| 17 namespace net { | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 // Bound on largest frame any SPDY version has allowed. | |
| 22 const size_t kMaxSpdyFrameSize = 0x00ffffff; | |
| 23 | |
| 24 // Makes a SpdySerializedFrame with |size| bytes of data copied from |data|. | |
| 25 // |data| must be non-NULL and |size| must be positive. | |
| 26 std::unique_ptr<SpdySerializedFrame> MakeSpdySerializedFrame(const char* data, | |
| 27 size_t size) { | |
| 28 DCHECK(data); | |
| 29 CHECK_GT(size, 0u); | |
| 30 CHECK_LE(size, kMaxSpdyFrameSize); | |
| 31 std::unique_ptr<char[]> frame_data(new char[size]); | |
| 32 std::memcpy(frame_data.get(), data, size); | |
| 33 std::unique_ptr<SpdySerializedFrame> frame(new SpdySerializedFrame( | |
| 34 frame_data.release(), size, true /* owns_buffer */)); | |
| 35 return frame; | |
| 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: | |
| 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 private: | |
| 51 ~SharedFrameIOBuffer() override { | |
| 52 // Prevent ~IOBuffer() from trying to delete |data_|. | |
| 53 data_ = NULL; | |
| 54 } | |
| 55 | |
| 56 const scoped_refptr<SharedFrame> shared_frame_; | |
| 57 | |
| 58 DISALLOW_COPY_AND_ASSIGN(SharedFrameIOBuffer); | |
| 59 }; | |
| 60 | |
| 61 SpdyBuffer::SpdyBuffer(std::unique_ptr<SpdySerializedFrame> frame) | |
| 62 : shared_frame_(new SharedFrame()), offset_(0) { | |
| 63 shared_frame_->data = std::move(frame); | |
| 64 } | |
| 65 | |
| 66 // The given data may not be strictly a SPDY frame; we (ab)use | |
| 67 // |frame_| just as a container. | |
| 68 SpdyBuffer::SpdyBuffer(const char* data, size_t size) : | |
| 69 shared_frame_(new SharedFrame()), | |
| 70 offset_(0) { | |
| 71 CHECK_GT(size, 0u); | |
| 72 CHECK_LE(size, kMaxSpdyFrameSize); | |
| 73 shared_frame_->data = MakeSpdySerializedFrame(data, size); | |
| 74 } | |
| 75 | |
| 76 SpdyBuffer::~SpdyBuffer() { | |
| 77 if (GetRemainingSize() > 0) | |
| 78 ConsumeHelper(GetRemainingSize(), DISCARD); | |
| 79 } | |
| 80 | |
| 81 const char* SpdyBuffer::GetRemainingData() const { | |
| 82 return shared_frame_->data->data() + offset_; | |
| 83 } | |
| 84 | |
| 85 size_t SpdyBuffer::GetRemainingSize() const { | |
| 86 return shared_frame_->data->size() - offset_; | |
| 87 } | |
| 88 | |
| 89 void SpdyBuffer::AddConsumeCallback(const ConsumeCallback& consume_callback) { | |
| 90 consume_callbacks_.push_back(consume_callback); | |
| 91 } | |
| 92 | |
| 93 void SpdyBuffer::Consume(size_t consume_size) { | |
| 94 ConsumeHelper(consume_size, CONSUME); | |
| 95 }; | |
| 96 | |
| 97 IOBuffer* SpdyBuffer::GetIOBufferForRemainingData() { | |
| 98 return new SharedFrameIOBuffer(shared_frame_, offset_); | |
| 99 } | |
| 100 | |
| 101 size_t SpdyBuffer::EstimateMemoryUsage() const { | |
| 102 // TODO(xunjieli): Estimate |consume_callbacks_|. https://crbug.com/669108. | |
| 103 return SpdyEstimateMemoryUsage(shared_frame_->data); | |
| 104 } | |
| 105 | |
| 106 void SpdyBuffer::ConsumeHelper(size_t consume_size, | |
| 107 ConsumeSource consume_source) { | |
| 108 DCHECK_GE(consume_size, 1u); | |
| 109 DCHECK_LE(consume_size, GetRemainingSize()); | |
| 110 offset_ += consume_size; | |
| 111 for (std::vector<ConsumeCallback>::const_iterator it = | |
| 112 consume_callbacks_.begin(); it != consume_callbacks_.end(); ++it) { | |
| 113 it->Run(consume_size, consume_source); | |
| 114 } | |
| 115 }; | |
| 116 | |
| 117 } // namespace net | |
| OLD | NEW |