document_loader_impl.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2018 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 PDF_LOADER_DOCUMENT_LOADER_IMPL_H_
  5. #define PDF_LOADER_DOCUMENT_LOADER_IMPL_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <string>
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "pdf/loader/chunk_stream.h"
  12. #include "pdf/loader/document_loader.h"
  13. namespace chrome_pdf {
  14. class DocumentLoaderImpl : public DocumentLoader {
  15. public:
  16. // Number was chosen in https://crbug.com/78264#c8
  17. static constexpr uint32_t kDefaultRequestSize = 65536;
  18. explicit DocumentLoaderImpl(Client* client);
  19. DocumentLoaderImpl(const DocumentLoaderImpl&) = delete;
  20. DocumentLoaderImpl& operator=(const DocumentLoaderImpl&) = delete;
  21. ~DocumentLoaderImpl() override;
  22. // DocumentLoader:
  23. bool Init(std::unique_ptr<URLLoaderWrapper> loader,
  24. const std::string& url) override;
  25. bool GetBlock(uint32_t position, uint32_t size, void* buf) const override;
  26. bool IsDataAvailable(uint32_t position, uint32_t size) const override;
  27. void RequestData(uint32_t position, uint32_t size) override;
  28. bool IsDocumentComplete() const override;
  29. uint32_t GetDocumentSize() const override;
  30. uint32_t BytesReceived() const override;
  31. void ClearPendingRequests() override;
  32. // Exposed for unit tests.
  33. void SetPartialLoadingEnabled(bool enabled);
  34. bool is_partial_loader_active() const { return is_partial_loader_active_; }
  35. private:
  36. using DataStream = ChunkStream<kDefaultRequestSize>;
  37. struct Chunk {
  38. Chunk();
  39. ~Chunk();
  40. void Clear();
  41. uint32_t chunk_index = 0;
  42. uint32_t data_size = 0;
  43. std::unique_ptr<DataStream::ChunkData> chunk_data;
  44. };
  45. // Called by the completion callback of the document's URLLoader.
  46. void DidOpenPartial(int32_t result);
  47. // Call to read data from the document's URLLoader.
  48. void ReadMore();
  49. // Called by the completion callback of the document's URLLoader.
  50. void DidRead(int32_t result);
  51. bool ShouldCancelLoading() const;
  52. void ContinueDownload();
  53. // Called when we complete server request.
  54. void ReadComplete();
  55. bool SaveBuffer(char* input, uint32_t input_size);
  56. void SaveChunkData();
  57. uint32_t EndOfCurrentChunk() const;
  58. const raw_ptr<Client> client_;
  59. std::string url_;
  60. std::unique_ptr<URLLoaderWrapper> loader_;
  61. DataStream chunk_stream_;
  62. bool partial_loading_enabled_; // Default determined by `kPdfPartialLoading`.
  63. bool is_partial_loader_active_ = false;
  64. static constexpr uint32_t kReadBufferSize = 256 * 1024;
  65. char buffer_[kReadBufferSize];
  66. // The current chunk DocumentLoader is working with.
  67. Chunk chunk_;
  68. // In units of Chunks.
  69. RangeSet pending_requests_;
  70. uint32_t bytes_received_ = 0;
  71. base::WeakPtrFactory<DocumentLoaderImpl> weak_factory_{this};
  72. };
  73. } // namespace chrome_pdf
  74. #endif // PDF_LOADER_DOCUMENT_LOADER_IMPL_H_