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 | 8 |
9 #include "base/basictypes.h" | |
10 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
11 #include "base/logging.h" | 10 #include "base/logging.h" |
12 | 11 |
13 namespace { | 12 namespace { |
14 | 13 |
15 static const size_t kHeaderLength = sizeof(uint32); | 14 static const size_t kHeaderLength = sizeof(uint32_t); |
16 | 15 |
17 static_assert(sizeof(size_t) >= kHeaderLength, | 16 static_assert(sizeof(size_t) >= kHeaderLength, |
18 "chunked byte buffer not supported on this architecture"); | 17 "chunked byte buffer not supported on this architecture"); |
19 | 18 |
20 uint32 ReadBigEndian32(const uint8* buffer) { | 19 uint32_t ReadBigEndian32(const uint8_t* buffer) { |
21 return (static_cast<uint32>(buffer[3])) | | 20 return (static_cast<uint32_t>(buffer[3])) | |
22 (static_cast<uint32>(buffer[2]) << 8) | | 21 (static_cast<uint32_t>(buffer[2]) << 8) | |
23 (static_cast<uint32>(buffer[1]) << 16) | | 22 (static_cast<uint32_t>(buffer[1]) << 16) | |
24 (static_cast<uint32>(buffer[0]) << 24); | 23 (static_cast<uint32_t>(buffer[0]) << 24); |
25 } | 24 } |
26 | 25 |
27 } // namespace | 26 } // namespace |
28 | 27 |
29 namespace content { | 28 namespace content { |
30 | 29 |
31 ChunkedByteBuffer::ChunkedByteBuffer() | 30 ChunkedByteBuffer::ChunkedByteBuffer() |
32 : partial_chunk_(new Chunk()), | 31 : partial_chunk_(new Chunk()), |
33 total_bytes_stored_(0) { | 32 total_bytes_stored_(0) { |
34 } | 33 } |
35 | 34 |
36 ChunkedByteBuffer::~ChunkedByteBuffer() { | 35 ChunkedByteBuffer::~ChunkedByteBuffer() { |
37 Clear(); | 36 Clear(); |
38 } | 37 } |
39 | 38 |
40 void ChunkedByteBuffer::Append(const uint8* start, size_t length) { | 39 void ChunkedByteBuffer::Append(const uint8_t* start, size_t length) { |
41 size_t remaining_bytes = length; | 40 size_t remaining_bytes = length; |
42 const uint8* next_data = start; | 41 const uint8_t* next_data = start; |
43 | 42 |
44 while (remaining_bytes > 0) { | 43 while (remaining_bytes > 0) { |
45 DCHECK(partial_chunk_ != NULL); | 44 DCHECK(partial_chunk_ != NULL); |
46 size_t insert_length = 0; | 45 size_t insert_length = 0; |
47 bool header_completed = false; | 46 bool header_completed = false; |
48 bool content_completed = false; | 47 bool content_completed = false; |
49 std::vector<uint8>* insert_target; | 48 std::vector<uint8_t>* insert_target; |
50 | 49 |
51 if (partial_chunk_->header.size() < kHeaderLength) { | 50 if (partial_chunk_->header.size() < kHeaderLength) { |
52 const size_t bytes_to_complete_header = | 51 const size_t bytes_to_complete_header = |
53 kHeaderLength - partial_chunk_->header.size(); | 52 kHeaderLength - partial_chunk_->header.size(); |
54 insert_length = std::min(bytes_to_complete_header, remaining_bytes); | 53 insert_length = std::min(bytes_to_complete_header, remaining_bytes); |
55 insert_target = &partial_chunk_->header; | 54 insert_target = &partial_chunk_->header; |
56 header_completed = (remaining_bytes >= bytes_to_complete_header); | 55 header_completed = (remaining_bytes >= bytes_to_complete_header); |
57 } else { | 56 } else { |
58 DCHECK_LT(partial_chunk_->content->size(), | 57 DCHECK_LT(partial_chunk_->content->size(), |
59 partial_chunk_->ExpectedContentLength()); | 58 partial_chunk_->ExpectedContentLength()); |
(...skipping 29 matching lines...) Expand all Loading... |
89 partial_chunk_->ExpectedContentLength()); | 88 partial_chunk_->ExpectedContentLength()); |
90 chunks_.push_back(partial_chunk_.release()); | 89 chunks_.push_back(partial_chunk_.release()); |
91 partial_chunk_.reset(new Chunk()); | 90 partial_chunk_.reset(new Chunk()); |
92 } | 91 } |
93 } | 92 } |
94 DCHECK_EQ(next_data, start + length); | 93 DCHECK_EQ(next_data, start + length); |
95 total_bytes_stored_ += length; | 94 total_bytes_stored_ += length; |
96 } | 95 } |
97 | 96 |
98 void ChunkedByteBuffer::Append(const std::string& string) { | 97 void ChunkedByteBuffer::Append(const std::string& string) { |
99 Append(reinterpret_cast<const uint8*>(string.data()), string.size()); | 98 Append(reinterpret_cast<const uint8_t*>(string.data()), string.size()); |
100 } | 99 } |
101 | 100 |
102 bool ChunkedByteBuffer::HasChunks() const { | 101 bool ChunkedByteBuffer::HasChunks() const { |
103 return !chunks_.empty(); | 102 return !chunks_.empty(); |
104 } | 103 } |
105 | 104 |
106 scoped_ptr< std::vector<uint8> > ChunkedByteBuffer::PopChunk() { | 105 scoped_ptr<std::vector<uint8_t>> ChunkedByteBuffer::PopChunk() { |
107 if (chunks_.empty()) | 106 if (chunks_.empty()) |
108 return scoped_ptr< std::vector<uint8> >(); | 107 return scoped_ptr<std::vector<uint8_t>>(); |
109 scoped_ptr<Chunk> chunk(*chunks_.begin()); | 108 scoped_ptr<Chunk> chunk(*chunks_.begin()); |
110 chunks_.weak_erase(chunks_.begin()); | 109 chunks_.weak_erase(chunks_.begin()); |
111 DCHECK_EQ(chunk->header.size(), kHeaderLength); | 110 DCHECK_EQ(chunk->header.size(), kHeaderLength); |
112 DCHECK_EQ(chunk->content->size(), chunk->ExpectedContentLength()); | 111 DCHECK_EQ(chunk->content->size(), chunk->ExpectedContentLength()); |
113 total_bytes_stored_ -= chunk->content->size(); | 112 total_bytes_stored_ -= chunk->content->size(); |
114 total_bytes_stored_ -= kHeaderLength; | 113 total_bytes_stored_ -= kHeaderLength; |
115 return chunk->content.Pass(); | 114 return chunk->content.Pass(); |
116 } | 115 } |
117 | 116 |
118 void ChunkedByteBuffer::Clear() { | 117 void ChunkedByteBuffer::Clear() { |
119 chunks_.clear(); | 118 chunks_.clear(); |
120 partial_chunk_.reset(new Chunk()); | 119 partial_chunk_.reset(new Chunk()); |
121 total_bytes_stored_ = 0; | 120 total_bytes_stored_ = 0; |
122 } | 121 } |
123 | 122 |
124 ChunkedByteBuffer::Chunk::Chunk() | 123 ChunkedByteBuffer::Chunk::Chunk() : content(new std::vector<uint8_t>()) {} |
125 : content(new std::vector<uint8>()) { | |
126 } | |
127 | 124 |
128 ChunkedByteBuffer::Chunk::~Chunk() { | 125 ChunkedByteBuffer::Chunk::~Chunk() { |
129 } | 126 } |
130 | 127 |
131 size_t ChunkedByteBuffer::Chunk::ExpectedContentLength() const { | 128 size_t ChunkedByteBuffer::Chunk::ExpectedContentLength() const { |
132 DCHECK_EQ(header.size(), kHeaderLength); | 129 DCHECK_EQ(header.size(), kHeaderLength); |
133 return static_cast<size_t>(ReadBigEndian32(&header[0])); | 130 return static_cast<size_t>(ReadBigEndian32(&header[0])); |
134 } | 131 } |
135 | 132 |
136 } // namespace content | 133 } // namespace content |
OLD | NEW |