OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef PDF_DOCUMENT_LOADER_H_ | 5 #ifndef PDF_DOCUMENT_LOADER_H_ |
6 #define PDF_DOCUMENT_LOADER_H_ | 6 #define PDF_DOCUMENT_LOADER_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <list> | 11 #include <list> |
12 #include <memory> | |
13 #include <string> | 12 #include <string> |
14 #include <utility> | 13 #include <utility> |
15 #include <vector> | 14 #include <vector> |
16 | 15 |
17 #include "pdf/chunk_stream.h" | 16 #include "pdf/chunk_stream.h" |
| 17 #include "ppapi/cpp/url_loader.h" |
18 #include "ppapi/utility/completion_callback_factory.h" | 18 #include "ppapi/utility/completion_callback_factory.h" |
19 | 19 |
20 namespace chrome_pdf { | 20 namespace chrome_pdf { |
21 | 21 |
22 class URLLoaderWrapper; | |
23 | |
24 class DocumentLoader { | 22 class DocumentLoader { |
25 public: | 23 public: |
26 // Number was chosen in crbug.com/78264#c8 | |
27 static constexpr uint32_t kDefaultRequestSize = 65536; | |
28 | |
29 class Client { | 24 class Client { |
30 public: | 25 public: |
31 virtual ~Client(); | 26 virtual ~Client(); |
32 | 27 |
33 // Gets the pp::Instance object. | 28 // Gets the pp::Instance object. |
34 virtual pp::Instance* GetPluginInstance() = 0; | 29 virtual pp::Instance* GetPluginInstance() = 0; |
35 // Creates new URLLoader based on client settings. | 30 // Creates new URLLoader based on client settings. |
36 virtual std::unique_ptr<URLLoaderWrapper> CreateURLLoader() = 0; | 31 virtual pp::URLLoader CreateURLLoader() = 0; |
| 32 // Notification called when partial information about document is available. |
| 33 // Only called for urls that returns full content size and supports byte |
| 34 // range requests. |
| 35 virtual void OnPartialDocumentLoaded() = 0; |
37 // Notification called when all outstanding pending requests are complete. | 36 // Notification called when all outstanding pending requests are complete. |
38 virtual void OnPendingRequestComplete() = 0; | 37 virtual void OnPendingRequestComplete() = 0; |
39 // Notification called when new data is available. | 38 // Notification called when new data is available. |
40 virtual void OnNewDataAvailable() = 0; | 39 virtual void OnNewDataAvailable() = 0; |
| 40 // Notification called if document failed to load. |
| 41 virtual void OnDocumentFailed() = 0; |
41 // Notification called when document is fully loaded. | 42 // Notification called when document is fully loaded. |
42 virtual void OnDocumentComplete() = 0; | 43 virtual void OnDocumentComplete() = 0; |
43 // Notification called when document loading is canceled. | |
44 virtual void OnDocumentCanceled() = 0; | |
45 // Called when initial loader was closed. | |
46 virtual void CancelBrowserDownload() = 0; | |
47 }; | 44 }; |
48 | 45 |
49 explicit DocumentLoader(Client* client); | 46 explicit DocumentLoader(Client* client); |
50 ~DocumentLoader(); | 47 ~DocumentLoader(); |
51 | 48 |
52 bool Init(std::unique_ptr<URLLoaderWrapper> loader, const std::string& url); | 49 bool Init(const pp::URLLoader& loader, |
| 50 const std::string& url, |
| 51 const std::string& headers); |
53 | 52 |
54 // Data access interface. Return true is successful. | 53 // Data access interface. Return true is successful. |
55 bool GetBlock(uint32_t position, uint32_t size, void* buf) const; | 54 bool GetBlock(uint32_t position, uint32_t size, void* buf) const; |
56 | 55 |
57 // Data availability interface. Return true data available. | 56 // Data availability interface. Return true data available. |
58 bool IsDataAvailable(uint32_t position, uint32_t size) const; | 57 bool IsDataAvailable(uint32_t position, uint32_t size) const; |
59 | 58 |
60 // Data availability interface. Return true data available. | 59 // Data availability interface. Return true data available. |
61 void RequestData(uint32_t position, uint32_t size); | 60 void RequestData(uint32_t position, uint32_t size); |
62 | 61 |
63 bool IsDocumentComplete() const; | 62 bool IsDocumentComplete() const; |
64 uint32_t GetDocumentSize() const; | 63 uint32_t document_size() const { return document_size_; } |
65 uint32_t count_of_bytes_received() const { return count_of_bytes_received_; } | 64 |
66 float GetProgress() const; | 65 // Return number of bytes available. |
| 66 uint32_t GetAvailableData() const; |
67 | 67 |
68 // Clear pending requests from the queue. | 68 // Clear pending requests from the queue. |
69 void ClearPendingRequests(); | 69 void ClearPendingRequests(); |
70 | 70 |
71 void SetPartialLoadingEnabled(bool enabled); | 71 bool is_partial_document() const { return partial_document_; } |
72 | |
73 bool is_partial_loader_active() const { return is_partial_loader_active_; } | |
74 | 72 |
75 private: | 73 private: |
76 using DataStream = ChunkStream<kDefaultRequestSize>; | |
77 struct Chunk { | |
78 Chunk(); | |
79 ~Chunk(); | |
80 | |
81 void Clear(); | |
82 | |
83 uint32_t chunk_index = 0; | |
84 uint32_t data_size = 0; | |
85 std::unique_ptr<DataStream::ChunkData> chunk_data; | |
86 }; | |
87 | |
88 // Called by the completion callback of the document's URLLoader. | 74 // Called by the completion callback of the document's URLLoader. |
89 void DidOpenPartial(int32_t result); | 75 void DidOpen(int32_t result); |
90 // Call to read data from the document's URLLoader. | 76 // Call to read data from the document's URLLoader. |
91 void ReadMore(); | 77 void ReadMore(); |
92 // Called by the completion callback of the document's URLLoader. | 78 // Called by the completion callback of the document's URLLoader. |
93 void DidRead(int32_t result); | 79 void DidRead(int32_t result); |
94 | 80 |
95 bool ShouldCancelLoading() const; | 81 // Called when we detect that partial document load is possible. |
96 void ContinueDownload(); | 82 void LoadPartialDocument(); |
97 // Called when we complete server request. | 83 // Called when we have to load full document. |
| 84 void LoadFullDocument(); |
| 85 // Download pending requests. |
| 86 void DownloadPendingRequests(); |
| 87 // Remove completed ranges. |
| 88 void RemoveCompletedRanges(); |
| 89 // Returns true if we are already in progress satisfying the request, or just |
| 90 // about ready to start. This helps us avoid expensive jumping around, and |
| 91 // even worse leaving tiny gaps in the byte stream that might have to be |
| 92 // filled later. |
| 93 bool SatisfyingRequest(size_t pos, size_t size) const; |
| 94 // Called when we complete server request and read all data from it. |
98 void ReadComplete(); | 95 void ReadComplete(); |
| 96 // Creates request to download size byte of data data starting from position. |
| 97 pp::URLRequestInfo GetRequest(uint32_t position, uint32_t size) const; |
| 98 // Updates the rendering by the Client. |
| 99 void UpdateRendering(); |
99 | 100 |
100 bool SaveChunkData(char* input, uint32_t input_size); | 101 // Document below size will be downloaded in one chunk. |
| 102 static const uint32_t kMinFileSize = 64 * 1024; |
| 103 // Number was chosen in crbug.com/78264#c8 |
| 104 enum { kDefaultRequestSize = 65536 }; |
101 | 105 |
102 Client* const client_; | 106 Client* const client_; |
103 std::string url_; | 107 std::string url_; |
104 std::unique_ptr<URLLoaderWrapper> loader_; | 108 pp::URLLoader loader_; |
105 | |
106 pp::CompletionCallbackFactory<DocumentLoader> loader_factory_; | 109 pp::CompletionCallbackFactory<DocumentLoader> loader_factory_; |
107 | 110 ChunkStream chunk_stream_; |
108 DataStream chunk_stream_; | 111 bool partial_document_; |
109 bool partial_loading_enabled_ = true; | 112 bool request_pending_; |
110 bool is_partial_loader_active_ = false; | 113 typedef std::list<std::pair<size_t, size_t> > PendingRequests; |
111 | 114 PendingRequests pending_requests_; |
112 static constexpr uint32_t kReadBufferSize = 256 * 1024; | 115 // The starting position of the HTTP request currently being processed. |
113 char buffer_[kReadBufferSize]; | 116 size_t current_request_offset_; |
114 | 117 // The size of the byte range the current HTTP request must download before |
115 Chunk chunk_; | 118 // being cancelled. |
116 RangeSet pending_requests_; | 119 size_t current_request_size_; |
117 uint32_t count_of_bytes_received_ = 0; | 120 // The actual byte range size of the current HTTP request. This may be larger |
| 121 // than |current_request_size_| and the request may be cancelled before |
| 122 // reaching |current_request_offset_| + |current_request_extended_size_|. |
| 123 size_t current_request_extended_size_; |
| 124 char buffer_[kDefaultRequestSize]; |
| 125 uint32_t current_pos_; |
| 126 uint32_t current_chunk_size_; |
| 127 uint32_t current_chunk_read_; |
| 128 uint32_t document_size_; |
| 129 bool header_request_; |
| 130 bool is_multipart_; |
| 131 std::string multipart_boundary_; |
| 132 uint32_t requests_count_; |
| 133 std::vector<std::vector<unsigned char> > chunk_buffer_; |
118 }; | 134 }; |
119 | 135 |
120 } // namespace chrome_pdf | 136 } // namespace chrome_pdf |
121 | 137 |
122 #endif // PDF_DOCUMENT_LOADER_H_ | 138 #endif // PDF_DOCUMENT_LOADER_H_ |
OLD | NEW |