web_bundle_chunked_buffer.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2021 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 COMPONENTS_WEB_PACKAGE_WEB_BUNDLE_CHUNKED_BUFFER_H_
  5. #define COMPONENTS_WEB_PACKAGE_WEB_BUNDLE_CHUNKED_BUFFER_H_
  6. #include <vector>
  7. #include "base/gtest_prod_util.h"
  8. #include "base/memory/ref_counted_memory.h"
  9. #include "base/memory/scoped_refptr.h"
  10. #include "mojo/public/cpp/system/data_pipe_producer.h"
  11. namespace web_package {
  12. // WebBundleChunkedBuffer keeps the appended bytes as a RefCountedBytes, so we
  13. // don't need to copy the bytes while creating a DataSource to read the data
  14. // using a DataPipeProducer.
  15. class WebBundleChunkedBuffer {
  16. public:
  17. WebBundleChunkedBuffer();
  18. ~WebBundleChunkedBuffer();
  19. // Disallow copy and assign.
  20. WebBundleChunkedBuffer(const WebBundleChunkedBuffer&) = delete;
  21. WebBundleChunkedBuffer& operator=(const WebBundleChunkedBuffer&) = delete;
  22. // Append the bytes as a Chunk.
  23. void Append(const uint8_t* data, size_t num_bytes);
  24. // Returns the available length of bytes after |offset|. If it is larger than
  25. // |max_length| returns |max_length|,
  26. uint64_t GetAvailableLength(uint64_t offset, uint64_t max_length) const;
  27. // Returns whether it contains all of [|offset|, |offset| + |length|) range.
  28. // When |length| is 0, always returns true.
  29. bool ContainsAll(uint64_t offset, uint64_t length) const;
  30. // Read the data to |out|. Returns the total length of the read bytes.
  31. uint64_t ReadData(uint64_t offset, uint64_t max_length, uint8_t* out) const;
  32. // Creates a DataSource to read the data using a DataPipeProducer. If there
  33. // is no data to read, returns nullptr.
  34. std::unique_ptr<mojo::DataPipeProducer::DataSource> CreateDataSource(
  35. uint64_t offset,
  36. uint64_t max_length) const;
  37. // Returns the buffer size.
  38. uint64_t size() const;
  39. private:
  40. friend class WebBundleChunkedBufferTest;
  41. FRIEND_TEST_ALL_PREFIXES(WebBundleChunkedBufferTest, EmptyBuffer);
  42. FRIEND_TEST_ALL_PREFIXES(WebBundleChunkedBufferTest, PartialBuffer);
  43. FRIEND_TEST_ALL_PREFIXES(WebBundleChunkedBufferTest, FindChunk);
  44. class Chunk {
  45. public:
  46. Chunk(uint64_t start_pos, scoped_refptr<const base::RefCountedBytes> bytes);
  47. ~Chunk();
  48. // Allow copy and assign.
  49. Chunk(const Chunk&);
  50. Chunk(Chunk&&);
  51. Chunk& operator=(const Chunk&) = default;
  52. Chunk& operator=(Chunk&&) = default;
  53. uint64_t start_pos() const;
  54. uint64_t end_pos() const;
  55. uint64_t size() const;
  56. const uint8_t* data() const;
  57. private:
  58. uint64_t start_pos_;
  59. scoped_refptr<const base::RefCountedBytes> bytes_;
  60. };
  61. using ChunkVector = std::vector<Chunk>;
  62. explicit WebBundleChunkedBuffer(ChunkVector chunks);
  63. bool empty() const;
  64. uint64_t start_pos() const;
  65. uint64_t end_pos() const;
  66. // Returns the iterator of |chunks_|, which Chunk contains the byte at |pos|.
  67. // If |chunks_| doesn't contains the byte at |pos|, returns |chunks_.end()|.
  68. ChunkVector::const_iterator FindChunk(uint64_t pos) const;
  69. // Creates a new WebBundleChunkedBuffer which keeps a part of |chunks_|
  70. // containing the bytes of [|offset|, |offset| + |length|) range.
  71. std::unique_ptr<const WebBundleChunkedBuffer> CreatePartialBuffer(
  72. uint64_t offset,
  73. uint64_t length) const;
  74. ChunkVector chunks_;
  75. };
  76. } // namespace web_package
  77. #endif // COMPONENTS_WEB_PACKAGE_WEB_BUNDLE_CHUNKED_BUFFER_H_