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