Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(299)

Unified Diff: net/filter/brotli_stream_source.cc

Issue 1662763002: [ON HOLD] Implement pull-based design for content decoding (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Refactor common logic Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/filter/brotli_stream_source.cc
diff --git a/net/filter/brotli_stream_source.cc b/net/filter/brotli_stream_source.cc
new file mode 100644
index 0000000000000000000000000000000000000000..e938515ca75eaa81b6e0beb3bffa0c1e646944a5
--- /dev/null
+++ b/net/filter/brotli_stream_source.cc
@@ -0,0 +1,126 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/filter/brotli_stream_source.h"
+
+#include "base/bind.h"
+#include "base/bit_cast.h"
+#include "base/metrics/histogram_macros.h"
+#include "net/filter/block_buffer.h"
+
+namespace net {
+
+BrotliStreamSource::BrotliStreamSource(scoped_ptr<StreamSource> previous)
+ : StreamSource(StreamSource::SOURCE_BROTLI, std::move(previous)),
+ decoding_status_(DecodingStatus::DECODING_IN_PROGRESS),
+ used_memory_(0),
+ used_memory_maximum_(0),
+ produced_bytes_(0),
+ consumed_bytes_(0) {
+ brotli_state_ = BrotliCreateState(AllocateMemory, FreeMemory, this);
+ CHECK(brotli_state_);
+}
+
+BrotliStreamSource::~BrotliStreamSource() {
+ BrotliDestroyState(brotli_state_);
+ brotli_state_ = nullptr;
+ DCHECK(used_memory_ == 0);
+
+ UMA_HISTOGRAM_ENUMERATION(
+ "BrotliStreamSource.Status", static_cast<int>(decoding_status_),
+ static_cast<int>(DecodingStatus::DECODING_STATUS_COUNT));
+ if (decoding_status_ == DecodingStatus::DECODING_DONE) {
+ // CompressionPercent is undefined when there is no output produced.
+ if (produced_bytes_ != 0) {
+ UMA_HISTOGRAM_PERCENTAGE(
+ "BrotliStreamSource.CompressionPercent",
+ static_cast<int>((consumed_bytes_ * 100) / produced_bytes_));
+ }
+ }
+ // All code here is for gathering stats, and can be removed when
+ // BrotliStreamSource is considered stable.
+ static const int kBuckets = 48;
+ static const int64_t kMaxKb = 1 << (kBuckets / 3); // 64MiB in KiB
+ UMA_HISTOGRAM_CUSTOM_COUNTS("BrotliStreamSource.UsedMemoryKB",
+ used_memory_maximum_ / 1024, 1, kMaxKb, kBuckets);
+}
+
+Error BrotliStreamSource::ReadInternal(IOBuffer* dest_buffer,
+ size_t dest_buffer_size,
+ size_t* bytes_read) {
+ const uint8_t* next_in = bit_cast<uint8_t*>(buffer_->bytes());
+ size_t available_in = buffer_->bytes_left();
+ uint8_t* next_out = bit_cast<uint8_t*>(dest_buffer->data());
+ size_t available_out = dest_buffer_size;
+ size_t total_out = 0;
+ BrotliResult result =
+ BrotliDecompressStream(&available_in, &next_in, &available_out, &next_out,
+ &total_out, brotli_state_);
+
+ size_t bytes_used = buffer_->bytes_left() - available_in;
+ size_t bytes_written = dest_buffer_size - available_out;
+ CHECK_GE(bytes_used, 0u);
+ CHECK_GE(bytes_written, 0u);
+ *bytes_read = bytes_written;
+ produced_bytes_ += bytes_written;
+ consumed_bytes_ += bytes_used;
+
+ buffer_->WasDrained(bytes_used);
+
+ switch (result) {
+ case BROTLI_RESULT_NEEDS_MORE_OUTPUT:
+ // Fall through.
+ case BROTLI_RESULT_SUCCESS:
+ decoding_status_ = DecodingStatus::DECODING_DONE;
+ return OK;
+ case BROTLI_RESULT_NEEDS_MORE_INPUT:
+ decoding_status_ = DecodingStatus::DECODING_IN_PROGRESS;
+ if (bytes_written > 0) {
+ return OK;
+ }
+ break;
+ // If the decompressor threw an error, fail synchronously.
+ default:
+ decoding_status_ = DecodingStatus::DECODING_ERROR;
+ return ERR_CONTENT_DECODING_FAILED;
+ }
+
+ DCHECK_EQ(BROTLI_RESULT_NEEDS_MORE_INPUT, result);
+
+ // Since Decompress needs more input, it has consumed all existing input.
+ DCHECK(!buffer_->HasMoreBytes());
+
+ return OK;
+}
+
+void* BrotliStreamSource::AllocateMemory(void* opaque, size_t size) {
+ BrotliStreamSource* filter = reinterpret_cast<BrotliStreamSource*>(opaque);
+ return filter->AllocateMemoryInternal(size);
+}
+
+void BrotliStreamSource::FreeMemory(void* opaque, void* address) {
+ BrotliStreamSource* filter = reinterpret_cast<BrotliStreamSource*>(opaque);
+ filter->FreeMemoryInternal(address);
+}
+
+void* BrotliStreamSource::AllocateMemoryInternal(size_t size) {
+ size_t* array = reinterpret_cast<size_t*>(malloc(size + sizeof(size_t)));
+ if (!array)
+ return nullptr;
+ used_memory_ += size;
+ if (used_memory_maximum_ < used_memory_)
+ used_memory_maximum_ = used_memory_;
+ array[0] = size;
+ return &array[1];
+}
+
+void BrotliStreamSource::FreeMemoryInternal(void* address) {
+ if (!address)
+ return;
+ size_t* array = reinterpret_cast<size_t*>(address);
+ used_memory_ -= array[-1];
+ free(&array[-1]);
+}
+
+} // namespace net

Powered by Google App Engine
This is Rietveld 408576698