blob: 2fa1d7128763c6f64e8d31dba6249117528e519e [file] [log] [blame]
Lei Zhange98901a2018-04-25 23:23:591// 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. Moon544e09532020-07-07 17:08:558#include <stdint.h>
9
Lei Zhange98901a2018-04-25 23:23:5910#include <memory>
11#include <string>
12
K. Moon544e09532020-07-07 17:08:5513#include "base/memory/weak_ptr.h"
Lei Zhange98901a2018-04-25 23:23:5914#include "pdf/chunk_stream.h"
15#include "pdf/document_loader.h"
Lei Zhange98901a2018-04-25 23:23:5916
17namespace chrome_pdf {
18
19class 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. Moon544e09532020-07-07 17:08:5525 DocumentLoaderImpl(const DocumentLoaderImpl&) = delete;
26 DocumentLoaderImpl& operator=(const DocumentLoaderImpl&) = delete;
Lei Zhange98901a2018-04-25 23:23:5927 ~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 Zhange98901a2018-04-25 23:23:5936 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 Zhange98901a2018-04-25 23:23:5981 DataStream chunk_stream_;
K. Moonf2a14142020-08-12 19:39:4082 bool partial_loading_enabled_; // Default determined by `kPdfPartialLoading`.
Lei Zhange98901a2018-04-25 23:23:5983 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. Moon544e09532020-07-07 17:08:5596 base::WeakPtrFactory<DocumentLoaderImpl> weak_factory_{this};
Lei Zhange98901a2018-04-25 23:23:5997};
98
99} // namespace chrome_pdf
100
101#endif // PDF_DOCUMENT_LOADER_IMPL_H_