filter_source_stream.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #ifndef NET_FILTER_FILTER_SOURCE_STREAM_H_
  5. #define NET_FILTER_FILTER_SOURCE_STREAM_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/memory/ref_counted.h"
  9. #include "base/types/expected.h"
  10. #include "net/base/completion_once_callback.h"
  11. #include "net/base/net_errors.h"
  12. #include "net/base/net_export.h"
  13. #include "net/filter/source_stream.h"
  14. namespace net {
  15. class DrainableIOBuffer;
  16. class IOBuffer;
  17. // FilterSourceStream represents SourceStreams that always have an upstream
  18. // from which undecoded input can be read. Except the ultimate upstream in
  19. // the filter chain, all other streams should implement FilterSourceStream
  20. // instead of SourceStream.
  21. class NET_EXPORT_PRIVATE FilterSourceStream : public SourceStream {
  22. public:
  23. // |upstream| is the SourceStream from which |this| will read data.
  24. // |upstream| cannot be null.
  25. FilterSourceStream(SourceType type, std::unique_ptr<SourceStream> upstream);
  26. FilterSourceStream(const FilterSourceStream&) = delete;
  27. FilterSourceStream& operator=(const FilterSourceStream&) = delete;
  28. ~FilterSourceStream() override;
  29. // SourceStream implementation.
  30. int Read(IOBuffer* read_buffer,
  31. int read_buffer_size,
  32. CompletionOnceCallback callback) override;
  33. std::string Description() const override;
  34. bool MayHaveMoreBytes() const override;
  35. static SourceType ParseEncodingType(const std::string& encoding);
  36. private:
  37. enum State {
  38. STATE_NONE,
  39. // Reading data from |upstream_| into |input_buffer_|.
  40. STATE_READ_DATA,
  41. // Reading data from |upstream_| completed.
  42. STATE_READ_DATA_COMPLETE,
  43. // Filtering data contained in |input_buffer_|.
  44. STATE_FILTER_DATA,
  45. // Filtering data contained in |input_buffer_| completed.
  46. STATE_FILTER_DATA_COMPLETE,
  47. STATE_DONE,
  48. };
  49. int DoLoop(int result);
  50. int DoReadData();
  51. int DoReadDataComplete(int result);
  52. int DoFilterData();
  53. // Helper method used as a callback argument passed to |upstream_->Read()|.
  54. void OnIOComplete(int result);
  55. // Subclasses should implement this method to filter data from
  56. // |input_buffer| and write to |output_buffer|.
  57. // This method must complete synchronously (i.e. It cannot return
  58. // ERR_IO_PENDING). If an unrecoverable error occurred, this should return
  59. // ERR_CONTENT_DECODING_FAILED or a more specific error code.
  60. //
  61. // If FilterData() returns 0, *|consumed_bytes| must be equal to
  62. // |input_buffer_size|. Upstream EOF is reached when FilterData() is called
  63. // with |upstream_eof_reached| = true.
  64. // TODO(xunjieli): consider allowing asynchronous response via callback
  65. // to support off-thread decompression.
  66. virtual base::expected<size_t, Error> FilterData(
  67. IOBuffer* output_buffer,
  68. size_t output_buffer_size,
  69. IOBuffer* input_buffer,
  70. size_t input_buffer_size,
  71. size_t* consumed_bytes,
  72. bool upstream_eof_reached) = 0;
  73. // Returns a string representation of the type of this FilterSourceStream.
  74. // This is for UMA logging.
  75. virtual std::string GetTypeAsString() const = 0;
  76. // Returns whether |this| still needs more input data from |upstream_|.
  77. // By default, |this| will continue reading until |upstream_| returns an error
  78. // or EOF. Subclass can override this to return false to skip reading all the
  79. // input from |upstream_|.
  80. virtual bool NeedMoreData() const;
  81. // The SourceStream from which |this| will read data from. Data flows from
  82. // |upstream_| to |this_|.
  83. std::unique_ptr<SourceStream> upstream_;
  84. State next_state_ = STATE_NONE;
  85. // Buffer for reading data out of |upstream_| and then for use by |this|
  86. // before the filtered data is returned through Read().
  87. scoped_refptr<IOBuffer> input_buffer_;
  88. // Wrapper around |input_buffer_| that makes visible only the unread data.
  89. // Keep this as a member because subclass might not drain everything in a
  90. // single FilterData().
  91. scoped_refptr<DrainableIOBuffer> drainable_input_buffer_;
  92. // Not null if there is a pending Read.
  93. scoped_refptr<IOBuffer> output_buffer_;
  94. size_t output_buffer_size_ = 0;
  95. CompletionOnceCallback callback_;
  96. // Reading from |upstream_| has returned 0 byte or an error code.
  97. bool upstream_end_reached_ = false;
  98. };
  99. } // namespace net
  100. #endif // NET_FILTER_FILTER_SOURCE_STREAM_H_