Chromium Code Reviews| 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/filter_stream_source.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/callback_helpers.h" | |
| 9 #include "base/metrics/histogram_macros.h" | |
| 10 #include "base/strings/string_util.h" | |
| 11 #include "net/base/io_buffer.h" | |
| 12 #include "net/filter/block_buffer.h" | |
| 13 | |
| 14 namespace net { | |
| 15 | |
| 16 FilterStreamSource::FilterStreamSource(SourceType type, | |
| 17 std::unique_ptr<StreamSource> previous) | |
| 18 : StreamSource(type, std::move(previous)) { | |
| 19 DCHECK(previous_); | |
| 20 } | |
| 21 | |
| 22 FilterStreamSource::~FilterStreamSource() {} | |
| 23 | |
| 24 Error FilterStreamSource::Read(IOBuffer* dest_buffer, | |
| 25 size_t buffer_size, | |
| 26 size_t* bytes_read, | |
| 27 const OnReadCompleteCallback& callback) { | |
| 28 *bytes_read = 0; | |
| 29 | |
| 30 Error error = OK; | |
| 31 // Allocate a BlockBuffer during first Read(). | |
| 32 if (!buffer_) | |
| 33 buffer_.reset(new BlockBuffer()); | |
| 34 // Loop on reading until either: | |
| 35 // * ReadInternal() returns some data, in which case this method completes | |
| 36 // synchronously, or | |
| 37 // * previous_->Read() does not complete synchronously, in which case | |
| 38 // OnReadComplete() is responsible for finishing the decompression. | |
| 39 while (error == OK) { | |
|
mmenke
2016/04/29 19:10:54
Hrm...Wonder if a do loop approach would be a litt
xunjieli
2016/07/20 21:00:47
Done.
| |
| 40 size_t single_bytes_read = 0; | |
| 41 error = ReadInternal(dest_buffer, buffer_size, &single_bytes_read); | |
| 42 | |
| 43 if (error == ERR_CONTENT_DECODING_FAILED) { | |
| 44 UMA_HISTOGRAM_ENUMERATION("Net.ContentDecodingFailed.FilterType", type(), | |
| 45 TYPE_MAX); | |
| 46 } | |
| 47 | |
| 48 *bytes_read += single_bytes_read; | |
| 49 | |
| 50 // ReadInternal() returns synchronously or an error occurred, return right | |
| 51 // here. ReadInternal() is not allowed to return ERR_IO_PENDING. | |
| 52 DCHECK_NE(ERR_IO_PENDING, error); | |
| 53 if ((error == OK && single_bytes_read > 0) || error != OK) | |
|
Randy Smith (Not in Mondays)
2016/04/26 21:54:01
I believe what this means is that this function wi
xunjieli
2016/07/20 21:00:47
Done.
| |
| 54 return error; | |
| 55 | |
| 56 // Needs more input, it has consumed all existing input. | |
| 57 DCHECK(!buffer_ || !buffer_->HasMoreBytes()); | |
|
mmenke
2016/04/29 19:10:54
nit: I'd say it's not worth DCHECKing on buffer_
xunjieli
2016/07/20 21:00:47
Done.
| |
| 58 | |
| 59 // Dispatch a read to refill the input buffer. | |
| 60 size_t previous_bytes_read = 0; | |
| 61 // Using base::Unretained(this) is safe, because |this| owns |previous_|. | |
| 62 error = previous_->Read( | |
| 63 buffer_->buffer(), buffer_->size(), &previous_bytes_read, | |
| 64 base::Bind(&FilterStreamSource::OnReadComplete, base::Unretained(this), | |
| 65 callback, base::Unretained(dest_buffer), buffer_size)); | |
| 66 | |
| 67 // OK with 0 bytes read means EOF. Since the buffer is already empty, and | |
| 68 // ReadInternal already failed to return any more data, this source is also | |
| 69 // at EOF. Just return that synchronously. | |
| 70 if (error == OK && previous_bytes_read == 0) | |
| 71 return OK; | |
| 72 | |
| 73 // If the underlying read completed synchronously, mark the buffer as | |
| 74 // refilled and try again. | |
| 75 if (error == OK) | |
| 76 buffer_->WasRefilled(previous_bytes_read); | |
| 77 } | |
| 78 | |
| 79 DCHECK(!buffer_ || !buffer_->HasMoreBytes()); | |
| 80 | |
| 81 return error; | |
| 82 } | |
| 83 | |
| 84 bool FilterStreamSource::Init() { | |
| 85 return true; | |
| 86 } | |
| 87 | |
| 88 void FilterStreamSource::OnReadComplete(const OnReadCompleteCallback& callback, | |
| 89 IOBuffer* dest_buffer, | |
| 90 size_t dest_buffer_size, | |
| 91 Error error, | |
| 92 size_t bytes_read) { | |
| 93 DCHECK(!buffer_ || !buffer_->HasMoreBytes()); | |
| 94 | |
| 95 // Take a ref for the lifetime of this function. | |
| 96 scoped_refptr<IOBuffer> dest_ref(dest_buffer); | |
| 97 | |
| 98 // (1) If the underlying read failed, fail this read directly. | |
| 99 // (2) EOF. Since the buffer is empty, there is no more data to decompress | |
| 100 // (any internally buffered data would have been drained already before | |
| 101 // calling the previous stream's Read). Return EOF to our caller. | |
| 102 if (error != OK || (error == OK && bytes_read == 0)) { | |
| 103 callback.Run(error, bytes_read); | |
| 104 return; | |
| 105 } | |
| 106 | |
| 107 // Mark the buffer as refilled and try Read again. | |
| 108 buffer_->WasRefilled(bytes_read); | |
| 109 | |
| 110 // Recurse. If Read completes synchronously, run the |callback|, | |
| 111 // Otherwise, Read will have posted an asynchronous read that | |
| 112 // will later re-invoke OnReadComplete to run the |callback|. | |
| 113 error = Read(dest_buffer, dest_buffer_size, &bytes_read, callback); | |
| 114 if (error != ERR_IO_PENDING) | |
| 115 callback.Run(error, bytes_read); | |
| 116 } | |
| 117 | |
| 118 } // namespace net | |
| OLD | NEW |