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 #ifdef UNSAFE_BUFFERS_BUILD 6 // TODO(crbug.com/40284755): Remove this and spanify to fix the errors. 7 #pragma allow_unsafe_buffers 8 #endif 9 10 #include "net/spdy/spdy_read_queue.h" 11 12 #include <algorithm> 13 #include <utility> 14 15 #include "base/check_op.h" 16 #include "net/spdy/spdy_buffer.h" 17 18 namespace net { 19 20 SpdyReadQueue::SpdyReadQueue() = default; 21 ~SpdyReadQueue()22SpdyReadQueue::~SpdyReadQueue() { 23 Clear(); 24 } 25 IsEmpty() const26bool SpdyReadQueue::IsEmpty() const { 27 DCHECK_EQ(queue_.empty(), total_size_ == 0); 28 return queue_.empty(); 29 } 30 GetTotalSize() const31size_t SpdyReadQueue::GetTotalSize() const { 32 return total_size_; 33 } 34 Enqueue(std::unique_ptr<SpdyBuffer> buffer)35void SpdyReadQueue::Enqueue(std::unique_ptr<SpdyBuffer> buffer) { 36 DCHECK_GT(buffer->GetRemainingSize(), 0u); 37 total_size_ += buffer->GetRemainingSize(); 38 queue_.push_back(std::move(buffer)); 39 } 40 Dequeue(char * out,size_t len)41size_t SpdyReadQueue::Dequeue(char* out, size_t len) { 42 DCHECK_GT(len, 0u); 43 size_t bytes_copied = 0; 44 while (!queue_.empty() && bytes_copied < len) { 45 SpdyBuffer* buffer = queue_.front().get(); 46 size_t bytes_to_copy = 47 std::min(len - bytes_copied, buffer->GetRemainingSize()); 48 memcpy(out + bytes_copied, buffer->GetRemainingData(), bytes_to_copy); 49 bytes_copied += bytes_to_copy; 50 if (bytes_to_copy == buffer->GetRemainingSize()) 51 queue_.pop_front(); 52 else 53 buffer->Consume(bytes_to_copy); 54 } 55 total_size_ -= bytes_copied; 56 return bytes_copied; 57 } 58 Clear()59void SpdyReadQueue::Clear() { 60 queue_.clear(); 61 total_size_ = 0; 62 } 63 64 } // namespace net 65