Index: net/filter/brotli_filter.cc |
diff --git a/net/filter/brotli_filter.cc b/net/filter/brotli_filter.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a27b7347620f0c2cbc2b733158733452a9e129ff |
--- /dev/null |
+++ b/net/filter/brotli_filter.cc |
@@ -0,0 +1,146 @@ |
+// Copyright 2015 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_filter.h" |
+ |
+#include "base/basictypes.h" |
+#include "base/logging.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "third_party/brotli/dec/decode.h" |
+ |
+namespace net { |
+ |
+#if !defined(DISABLE_BROTLI_SUPPORT) |
+class BrotliFilter : public Filter { |
+ public: |
+ ~BrotliFilter() override { |
+ if (decoding_status_ != DECODING_UNINITIALIZED) |
+ BrotliStateCleanup(brotli_state_.get()); |
+ } |
+ |
+ BrotliFilter(FilterType type,int buffer_size) |
xunjieli
2015/12/02 18:26:30
nit: can we have constructor before destructor?
eustas
2015/12/03 12:32:06
Of course. Done.
|
+ : Filter(type), decoding_status_(DECODING_UNINITIALIZED) { |
+ InitBuffer(buffer_size); |
+ } |
+ |
+ // Initializes filter decoding mode and internal control blocks. |
+ // The function returns true if success and false otherwise. |
+ // The filter can only be initialized once. |
+ bool InitDecoding() { |
+ if (decoding_status_ != DECODING_UNINITIALIZED) |
+ return false; |
+ |
+ brotli_state_.reset(new BrotliState); |
+ if (!brotli_state_.get()) |
xunjieli
2015/12/02 18:26:30
This if-block looks weird. If |brotli_state_| is s
eustas
2015/12/03 12:32:06
C-style allocation check. Removed.
|
+ return false; |
+ |
+ BrotliStateInit(brotli_state_.get()); |
+ |
+ decoding_status_ = DECODING_IN_PROGRESS; |
+ return true; |
+ } |
+ |
+ // Decodes the pre-filter data and writes the output into the |dest_buffer| |
+ // passed in. |
+ // The function returns FilterStatus. See filter.h for its description. |
+ // |
+ // Upon entry, |*dest_len| is the total size (in number of chars) of the |
+ // destination buffer. Upon exit, |*dest_len| is the actual number of chars |
+ // written into the destination buffer. |
+ // |
+ // This function will fail if there is no pre-filter data in the |
+ // |stream_buffer_|. On the other hand, |*dest_len| can be 0 upon successful |
+ // return. For example, decompressor may process some pre-filter data |
+ // but not produce output yet. |
+ FilterStatus ReadFilteredData(char* dest_buffer, int* dest_len) override { |
+ if (!dest_buffer || !dest_len || *dest_len <= 0) |
+ return Filter::FILTER_ERROR; |
+ |
+ if (decoding_status_ == DECODING_DONE) { |
+ *dest_len = 0; |
+ return Filter::FILTER_DONE; |
+ } |
+ |
+ if (decoding_status_ != DECODING_IN_PROGRESS) |
+ return Filter::FILTER_ERROR; |
+ |
+ size_t available_in = stream_data_len_; |
+ const uint8_t* next_in = bit_cast<uint8_t*>(next_stream_data_); |
+ size_t available_out = *dest_len; |
+ uint8_t* next_out = bit_cast<uint8_t*>(dest_buffer); |
+ size_t total_out; |
+ BrotliResult result = |
+ BrotliDecompressStream(&available_in, &next_in, &available_out, |
+ &next_out, &total_out, brotli_state_.get()); |
+ int bytes_written = *dest_len - available_out; |
+ |
+ Filter::FilterStatus status; |
+ switch (result) { |
+ case BROTLI_RESULT_ERROR: { |
+ status = Filter::FILTER_ERROR; |
+ break; |
+ } |
+ // Fall through. |
+ case BROTLI_RESULT_NEEDS_MORE_OUTPUT: |
+ case BROTLI_RESULT_SUCCESS: { |
+ status = Filter::FILTER_OK; |
+ *dest_len = bytes_written; |
+ stream_data_len_ = available_in; |
+ next_stream_data_ = bit_cast<char*>(next_in); |
+ if (result == BROTLI_RESULT_SUCCESS) |
+ status = Filter::FILTER_DONE; |
+ break; |
+ } |
+ case BROTLI_RESULT_NEEDS_MORE_INPUT: { |
+ status = Filter::FILTER_NEED_MORE_DATA; |
+ *dest_len = bytes_written; |
+ stream_data_len_ = 0; |
+ next_stream_data_ = NULL; |
xunjieli
2015/12/02 18:26:30
nit: nullptr here.
eustas
2015/12/03 12:32:06
Done.
|
+ break; |
+ } |
+ default: { |
+ status = Filter::FILTER_ERROR; |
+ break; |
+ } |
+ } |
+ |
+ if (status == Filter::FILTER_DONE) { |
+ decoding_status_ = DECODING_DONE; |
+ } else if (status == Filter::FILTER_ERROR) { |
+ decoding_status_ = DECODING_ERROR; |
+ } |
+ |
+ return status; |
+ } |
+ |
+ private: |
+ enum DecodingStatus { |
+ DECODING_UNINITIALIZED, |
+ DECODING_IN_PROGRESS, |
+ DECODING_DONE, |
+ DECODING_ERROR |
+ }; |
+ |
+ // Tracks the status of decoding. |
+ // This variable is initialized by InitDecoding and updated only by |
+ // ReadFilteredData. |
+ DecodingStatus decoding_status_; |
+ |
+ scoped_ptr<BrotliStateStruct> brotli_state_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(BrotliFilter); |
xunjieli
2015/12/02 18:26:30
nit: need to include base/macro.h for DISALLOW_COP
eustas
2015/12/03 12:32:06
Done. Thank you.
|
+}; |
+#endif |
+ |
+Filter* CreateBrotliFilter(Filter::FilterType type_id, int buffer_size) { |
+#if !defined(DISABLE_BROTLI_SUPPORT) |
+ scoped_ptr<BrotliFilter> brotli_filter( |
+ new BrotliFilter(type_id, buffer_size)); |
+ return brotli_filter->InitDecoding() ? brotli_filter.release() : NULL; |
xunjieli
2015/12/02 18:26:30
nit: nullptr this line and on the next line.
eustas
2015/12/03 12:32:06
Fixed everywhere.
|
+#else |
+ return NULL; |
+#endif |
+} |
+ |
+} // namespace net |