| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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/filter/brotli_stream_source.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bit_cast.h" |
| 9 #include "base/metrics/histogram_macros.h" |
| 10 #include "net/filter/block_buffer.h" |
| 11 |
| 12 namespace net { |
| 13 |
| 14 BrotliStreamSource::BrotliStreamSource(scoped_ptr<StreamSource> previous) |
| 15 : buffer_(new BlockBuffer()), |
| 16 previous_(std::move(previous)), |
| 17 used_memory_(0), |
| 18 used_memory_maximum_(0), |
| 19 produced_bytes_(0) { |
| 20 brotli_state_ = BrotliCreateState(AllocateMemory, FreeMemory, this); |
| 21 CHECK(brotli_state_); |
| 22 } |
| 23 |
| 24 BrotliStreamSource::~BrotliStreamSource() { |
| 25 BrotliDestroyState(brotli_state_); |
| 26 brotli_state_ = nullptr; |
| 27 DCHECK(used_memory_ == 0); |
| 28 |
| 29 #if 0 |
| 30 UMA_HISTOGRAM_ENUMERATION( |
| 31 "BrotliStreamSource.Status", static_cast<int>(decoding_status_), |
| 32 static_cast<int>(DecodingStatus::DECODING_STATUS_COUNT)); |
| 33 if (decoding_status_ == DecodingStatus::DECODING_DONE) { |
| 34 // CompressionPercent is undefined when there is no output produced. |
| 35 if (produced_bytes_ != 0) { |
| 36 UMA_HISTOGRAM_PERCENTAGE( |
| 37 "BrotliStreamSource.CompressionPercent", |
| 38 static_cast<int>((consumed_bytes_ * 100) / produced_bytes_)); |
| 39 } |
| 40 } |
| 41 #endif |
| 42 // All code here is for gathering stats, and can be removed when |
| 43 // BrotliStreamSource is considered stable. |
| 44 static const int kBuckets = 48; |
| 45 static const int64_t kMaxKb = 1 << (kBuckets / 3); // 64MiB in KiB |
| 46 UMA_HISTOGRAM_CUSTOM_COUNTS("BrotliStreamSource.UsedMemoryKB", |
| 47 used_memory_maximum_ / 1024, 1, kMaxKb, kBuckets); |
| 48 } |
| 49 |
| 50 Error BrotliStreamSource::Read(IOBuffer* dest_buffer, |
| 51 size_t dest_buffer_size, |
| 52 size_t* bytes_read, |
| 53 const OnReadCompleteCallback& callback) { |
| 54 *bytes_read = 0; |
| 55 |
| 56 // Loop on reading the previous source until either: |
| 57 // * BrotliDecompressStream() returns some data, in which case this method |
| 58 // completes synchronously, or |
| 59 // * Read() does not complete synchronously, in which case OnReadComplete() |
| 60 // is responsible for finishing the decompression. |
| 61 Error error; |
| 62 do { |
| 63 const uint8_t* next_in = bit_cast<uint8_t*>(buffer_->bytes()); |
| 64 size_t available_in = buffer_->bytes_left(); |
| 65 uint8_t* next_out = bit_cast<uint8_t*>(dest_buffer->data()); |
| 66 size_t available_out = dest_buffer_size; |
| 67 size_t total_out = 0; |
| 68 BrotliResult result = |
| 69 BrotliDecompressStream(&available_in, &next_in, &available_out, |
| 70 &next_out, &total_out, brotli_state_); |
| 71 |
| 72 size_t bytes_used = buffer_->bytes_left() - available_in; |
| 73 size_t bytes_written = dest_buffer_size - available_out; |
| 74 if (bytes_written > 0) { |
| 75 *bytes_read = bytes_written; |
| 76 produced_bytes_ += *bytes_read; |
| 77 } |
| 78 |
| 79 buffer_->WasDrained(bytes_used); |
| 80 |
| 81 switch (result) { |
| 82 case BROTLI_RESULT_NEEDS_MORE_OUTPUT: |
| 83 // Fall through. |
| 84 case BROTLI_RESULT_SUCCESS: |
| 85 return OK; |
| 86 case BROTLI_RESULT_NEEDS_MORE_INPUT: |
| 87 if (bytes_written > 0) { |
| 88 return OK; |
| 89 } |
| 90 break; |
| 91 // If the decompressor threw an error, fail synchronously. |
| 92 default: |
| 93 return ERR_CONTENT_DECODING_FAILED; |
| 94 } |
| 95 |
| 96 DCHECK_EQ(BROTLI_RESULT_NEEDS_MORE_INPUT, result); |
| 97 |
| 98 // Since Decompress needs more input, it has consumed all existing input. |
| 99 DCHECK(!buffer_->HasMoreBytes()); |
| 100 |
| 101 // Dispatch a read to refill the input buffer. |
| 102 size_t previous_bytes_read; |
| 103 error = previous_->Read( |
| 104 buffer_->buffer(), buffer_->size(), &previous_bytes_read, |
| 105 base::Bind(&BrotliStreamSource::OnReadComplete, base::Unretained(this), |
| 106 callback, base::Unretained(dest_buffer), dest_buffer_size)); |
| 107 |
| 108 // OK with 0 bytes read means EOF. Since the buffer is already empty, and |
| 109 // Decompress already failed to return any more data, this source is also |
| 110 // at EOF. Just return that synchronously. |
| 111 if (error == OK && previous_bytes_read == 0) |
| 112 return OK; |
| 113 |
| 114 // If the underlying read completed synchronously, mark the buffer as |
| 115 // refilled and try again. |
| 116 if (error == OK) |
| 117 buffer_->WasRefilled(previous_bytes_read); |
| 118 } while (error == OK); |
| 119 |
| 120 if (error == ERR_IO_PENDING) |
| 121 pending_read_buffer_ = dest_buffer; |
| 122 |
| 123 return error; |
| 124 } |
| 125 |
| 126 size_t BrotliStreamSource::GetBytesOutput() const { |
| 127 return produced_bytes_; |
| 128 } |
| 129 |
| 130 void BrotliStreamSource::OnReadComplete(const OnReadCompleteCallback& callback, |
| 131 IOBuffer* dest_buffer, |
| 132 size_t dest_buffer_size, |
| 133 Error error, |
| 134 size_t bytes_read) { |
| 135 DCHECK(!buffer_->HasMoreBytes()); |
| 136 DCHECK_EQ(dest_buffer, pending_read_buffer_.get()); |
| 137 |
| 138 // Take a ref for the lifetime of this function. |
| 139 scoped_refptr<IOBuffer> dest_ref(dest_buffer); |
| 140 pending_read_buffer_ = nullptr; |
| 141 |
| 142 // If the underlying read failed, fail this read directly. |
| 143 if (error != OK) { |
| 144 callback.Run(error, bytes_read); |
| 145 return; |
| 146 } |
| 147 |
| 148 if (bytes_read == 0) { |
| 149 // EOF. Since the buffer is empty, there is no more data to decompress (any |
| 150 // internally buffered data would have been drained already before calling |
| 151 // the previous stream's Read). Return EOF to our caller. |
| 152 callback.Run(OK, 0); |
| 153 return; |
| 154 } |
| 155 |
| 156 // Mark the buffer as refilled and try decompressing. |
| 157 buffer_->WasRefilled(bytes_read); |
| 158 |
| 159 // Recurse. If this Read completes synchronously, this method runs the |
| 160 // callback; if it does not, Read will have posted an asynchronous read that |
| 161 // will later re-invoke OnReadComplete to run the callback. |
| 162 error = Read(dest_buffer, dest_buffer_size, &bytes_read, callback); |
| 163 if (error != ERR_IO_PENDING) { |
| 164 callback.Run(error, bytes_read); |
| 165 } |
| 166 } |
| 167 |
| 168 void* BrotliStreamSource::AllocateMemory(void* opaque, size_t size) { |
| 169 BrotliStreamSource* filter = reinterpret_cast<BrotliStreamSource*>(opaque); |
| 170 return filter->AllocateMemoryInternal(size); |
| 171 } |
| 172 |
| 173 void BrotliStreamSource::FreeMemory(void* opaque, void* address) { |
| 174 BrotliStreamSource* filter = reinterpret_cast<BrotliStreamSource*>(opaque); |
| 175 filter->FreeMemoryInternal(address); |
| 176 } |
| 177 |
| 178 void* BrotliStreamSource::AllocateMemoryInternal(size_t size) { |
| 179 size_t* array = reinterpret_cast<size_t*>(malloc(size + sizeof(size_t))); |
| 180 if (!array) |
| 181 return nullptr; |
| 182 used_memory_ += size; |
| 183 if (used_memory_maximum_ < used_memory_) |
| 184 used_memory_maximum_ = used_memory_; |
| 185 array[0] = size; |
| 186 return &array[1]; |
| 187 } |
| 188 |
| 189 void BrotliStreamSource::FreeMemoryInternal(void* address) { |
| 190 if (!address) |
| 191 return; |
| 192 size_t* array = reinterpret_cast<size_t*>(address); |
| 193 used_memory_ -= array[-1]; |
| 194 free(&array[-1]); |
| 195 } |
| 196 |
| 197 } // namespace net |
| OLD | NEW |