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.
5 #ifndef PDF_DOCUMENT_LOADER_IMPL_H_
6 #define PDF_DOCUMENT_LOADER_IMPL_H_
11 #include "base/macros.h"
12 #include "pdf/chunk_stream.h"
13 #include "pdf/document_loader.h"
14 #include "ppapi/utility/completion_callback_factory.h"
16 namespace chrome_pdf {
18 class DocumentLoaderImpl : public DocumentLoader {
20 // Number was chosen in https://crbug.com/78264#c8
21 static constexpr uint32_t kDefaultRequestSize = 65536;
23 explicit DocumentLoaderImpl(Client* client);
24 ~DocumentLoaderImpl() override;
27 bool Init(std::unique_ptr<URLLoaderWrapper> loader,
28 const std::string& url) override;
29 bool GetBlock(uint32_t position, uint32_t size, void* buf) const override;
30 bool IsDataAvailable(uint32_t position, uint32_t size) const override;
31 void RequestData(uint32_t position, uint32_t size) override;
32 bool IsDocumentComplete() const override;
33 void SetDocumentSize(uint32_t size) override;
34 uint32_t GetDocumentSize() const override;
35 uint32_t BytesReceived() const override;
36 void ClearPendingRequests() override;
38 // Exposed for unit tests.
39 void SetPartialLoadingEnabled(bool enabled);
40 bool is_partial_loader_active() const { return is_partial_loader_active_; }
43 using DataStream = ChunkStream<kDefaultRequestSize>;
50 uint32_t chunk_index = 0;
51 uint32_t data_size = 0;
52 std::unique_ptr<DataStream::ChunkData> chunk_data;
55 // Called by the completion callback of the document's URLLoader.
56 void DidOpenPartial(int32_t result);
58 // Call to read data from the document's URLLoader.
61 // Called by the completion callback of the document's URLLoader.
62 void DidRead(int32_t result);
64 bool ShouldCancelLoading() const;
65 void ContinueDownload();
67 // Called when we complete server request.
70 bool SaveBuffer(char* input, uint32_t input_size);
73 uint32_t EndOfCurrentChunk() const;
75 Client* const client_;
77 std::unique_ptr<URLLoaderWrapper> loader_;
79 pp::CompletionCallbackFactory<DocumentLoaderImpl> loader_factory_;
81 DataStream chunk_stream_;
82 bool partial_loading_enabled_ = true;
83 bool is_partial_loader_active_ = false;
85 static constexpr uint32_t kReadBufferSize = 256 * 1024;
86 char buffer_[kReadBufferSize];
88 // The current chunk DocumentLoader is working with.
91 // In units of Chunks.
92 RangeSet pending_requests_;
94 uint32_t bytes_received_ = 0;
96 DISALLOW_COPY_AND_ASSIGN(DocumentLoaderImpl);
99 } // namespace chrome_pdf
101 #endif // PDF_DOCUMENT_LOADER_IMPL_H_