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