| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/speech/chunked_byte_buffer.h" | 5 #include "content/browser/speech/chunked_byte_buffer.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 insert_target->insert(insert_target->end(), | 71 insert_target->insert(insert_target->end(), |
| 72 next_data, | 72 next_data, |
| 73 next_data + insert_length); | 73 next_data + insert_length); |
| 74 next_data += insert_length; | 74 next_data += insert_length; |
| 75 remaining_bytes -= insert_length; | 75 remaining_bytes -= insert_length; |
| 76 | 76 |
| 77 if (header_completed) { | 77 if (header_completed) { |
| 78 DCHECK_EQ(partial_chunk_->header.size(), kHeaderLength); | 78 DCHECK_EQ(partial_chunk_->header.size(), kHeaderLength); |
| 79 if (partial_chunk_->ExpectedContentLength() == 0) { | 79 if (partial_chunk_->ExpectedContentLength() == 0) { |
| 80 // Handle zero-byte chunks. | 80 // Handle zero-byte chunks. |
| 81 chunks_.push_back(partial_chunk_.release()); | 81 chunks_.push_back(std::move(partial_chunk_)); |
| 82 partial_chunk_.reset(new Chunk()); | 82 partial_chunk_.reset(new Chunk()); |
| 83 } else { | 83 } else { |
| 84 partial_chunk_->content->reserve( | 84 partial_chunk_->content->reserve( |
| 85 partial_chunk_->ExpectedContentLength()); | 85 partial_chunk_->ExpectedContentLength()); |
| 86 } | 86 } |
| 87 } else if (content_completed) { | 87 } else if (content_completed) { |
| 88 DCHECK_EQ(partial_chunk_->content->size(), | 88 DCHECK_EQ(partial_chunk_->content->size(), |
| 89 partial_chunk_->ExpectedContentLength()); | 89 partial_chunk_->ExpectedContentLength()); |
| 90 chunks_.push_back(partial_chunk_.release()); | 90 chunks_.push_back(std::move(partial_chunk_)); |
| 91 partial_chunk_.reset(new Chunk()); | 91 partial_chunk_.reset(new Chunk()); |
| 92 } | 92 } |
| 93 } | 93 } |
| 94 DCHECK_EQ(next_data, start + length); | 94 DCHECK_EQ(next_data, start + length); |
| 95 total_bytes_stored_ += length; | 95 total_bytes_stored_ += length; |
| 96 } | 96 } |
| 97 | 97 |
| 98 void ChunkedByteBuffer::Append(const std::string& string) { | 98 void ChunkedByteBuffer::Append(const std::string& string) { |
| 99 Append(reinterpret_cast<const uint8_t*>(string.data()), string.size()); | 99 Append(reinterpret_cast<const uint8_t*>(string.data()), string.size()); |
| 100 } | 100 } |
| 101 | 101 |
| 102 bool ChunkedByteBuffer::HasChunks() const { | 102 bool ChunkedByteBuffer::HasChunks() const { |
| 103 return !chunks_.empty(); | 103 return !chunks_.empty(); |
| 104 } | 104 } |
| 105 | 105 |
| 106 std::unique_ptr<std::vector<uint8_t>> ChunkedByteBuffer::PopChunk() { | 106 std::unique_ptr<std::vector<uint8_t>> ChunkedByteBuffer::PopChunk() { |
| 107 if (chunks_.empty()) | 107 if (chunks_.empty()) |
| 108 return std::unique_ptr<std::vector<uint8_t>>(); | 108 return std::unique_ptr<std::vector<uint8_t>>(); |
| 109 std::unique_ptr<Chunk> chunk(*chunks_.begin()); | 109 std::unique_ptr<Chunk> chunk = std::move(*chunks_.begin()); |
| 110 chunks_.weak_erase(chunks_.begin()); | 110 chunks_.erase(chunks_.begin()); |
| 111 DCHECK_EQ(chunk->header.size(), kHeaderLength); | 111 DCHECK_EQ(chunk->header.size(), kHeaderLength); |
| 112 DCHECK_EQ(chunk->content->size(), chunk->ExpectedContentLength()); | 112 DCHECK_EQ(chunk->content->size(), chunk->ExpectedContentLength()); |
| 113 total_bytes_stored_ -= chunk->content->size(); | 113 total_bytes_stored_ -= chunk->content->size(); |
| 114 total_bytes_stored_ -= kHeaderLength; | 114 total_bytes_stored_ -= kHeaderLength; |
| 115 return std::move(chunk->content); | 115 return std::move(chunk->content); |
| 116 } | 116 } |
| 117 | 117 |
| 118 void ChunkedByteBuffer::Clear() { | 118 void ChunkedByteBuffer::Clear() { |
| 119 chunks_.clear(); | 119 chunks_.clear(); |
| 120 partial_chunk_.reset(new Chunk()); | 120 partial_chunk_.reset(new Chunk()); |
| 121 total_bytes_stored_ = 0; | 121 total_bytes_stored_ = 0; |
| 122 } | 122 } |
| 123 | 123 |
| 124 ChunkedByteBuffer::Chunk::Chunk() : content(new std::vector<uint8_t>()) {} | 124 ChunkedByteBuffer::Chunk::Chunk() : content(new std::vector<uint8_t>()) {} |
| 125 | 125 |
| 126 ChunkedByteBuffer::Chunk::~Chunk() { | 126 ChunkedByteBuffer::Chunk::~Chunk() { |
| 127 } | 127 } |
| 128 | 128 |
| 129 size_t ChunkedByteBuffer::Chunk::ExpectedContentLength() const { | 129 size_t ChunkedByteBuffer::Chunk::ExpectedContentLength() const { |
| 130 DCHECK_EQ(header.size(), kHeaderLength); | 130 DCHECK_EQ(header.size(), kHeaderLength); |
| 131 return static_cast<size_t>(ReadBigEndian32(&header[0])); | 131 return static_cast<size_t>(ReadBigEndian32(&header[0])); |
| 132 } | 132 } |
| 133 | 133 |
| 134 } // namespace content | 134 } // namespace content |
| OLD | NEW |