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 <list> | 8 #include <list> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "pdf/chunk_stream.h" | 13 #include "pdf/chunk_stream.h" |
14 #include "ppapi/cpp/url_loader.h" | 14 #include "ppapi/cpp/url_loader.h" |
15 #include "ppapi/utility/completion_callback_factory.h" | 15 #include "ppapi/utility/completion_callback_factory.h" |
16 | 16 |
17 #define kDefaultRequestSize 32768u | 17 #define kDefaultRequestSize 32768u |
18 | 18 |
19 namespace chrome_pdf { | 19 namespace chrome_pdf { |
20 | 20 |
21 class DocumentLoader { | 21 class DocumentLoader { |
22 public: | 22 public: |
23 class Client { | 23 class Client { |
24 public: | 24 public: |
| 25 virtual ~Client(); |
| 26 |
25 // Gets the pp::Instance object. | 27 // Gets the pp::Instance object. |
26 virtual pp::Instance* GetPluginInstance() = 0; | 28 virtual pp::Instance* GetPluginInstance() = 0; |
27 // Creates new URLLoader based on client settings. | 29 // Creates new URLLoader based on client settings. |
28 virtual pp::URLLoader CreateURLLoader() = 0; | 30 virtual pp::URLLoader CreateURLLoader() = 0; |
29 // Notification called when partial information about document is available. | 31 // Notification called when partial information about document is available. |
30 // Only called for urls that returns full content size and supports byte | 32 // Only called for urls that returns full content size and supports byte |
31 // range requests. | 33 // range requests. |
32 virtual void OnPartialDocumentLoaded() = 0; | 34 virtual void OnPartialDocumentLoaded() = 0; |
33 // Notification called when all outstanding pending requests are complete. | 35 // Notification called when all outstanding pending requests are complete. |
34 virtual void OnPendingRequestComplete() = 0; | 36 virtual void OnPendingRequestComplete() = 0; |
35 // Notification called when new data is available. | 37 // Notification called when new data is available. |
36 virtual void OnNewDataAvailable() = 0; | 38 virtual void OnNewDataAvailable() = 0; |
37 // Notification called when document is fully loaded. | 39 // Notification called when document is fully loaded. |
38 virtual void OnDocumentComplete() = 0; | 40 virtual void OnDocumentComplete() = 0; |
39 }; | 41 }; |
40 | 42 |
41 explicit DocumentLoader(Client* client); | 43 explicit DocumentLoader(Client* client); |
42 virtual ~DocumentLoader(); | 44 ~DocumentLoader(); |
43 | 45 |
44 bool Init(const pp::URLLoader& loader, | 46 bool Init(const pp::URLLoader& loader, |
45 const std::string& url, | 47 const std::string& url, |
46 const std::string& headers); | 48 const std::string& headers); |
47 | 49 |
48 // Data access interface. Return true is sucessful. | 50 // Data access interface. Return true is sucessful. |
49 bool GetBlock(uint32 position, uint32 size, void* buf) const; | 51 bool GetBlock(uint32_t position, uint32_t size, void* buf) const; |
50 | 52 |
51 // Data availability interface. Return true data avaialble. | 53 // Data availability interface. Return true data avaialble. |
52 bool IsDataAvailable(uint32 position, uint32 size) const; | 54 bool IsDataAvailable(uint32_t position, uint32_t size) const; |
53 | 55 |
54 // Data availability interface. Return true data avaialble. | 56 // Data availability interface. Return true data avaialble. |
55 void RequestData(uint32 position, uint32 size); | 57 void RequestData(uint32_t position, uint32_t size); |
56 | 58 |
57 bool IsDocumentComplete() const; | 59 bool IsDocumentComplete() const; |
58 uint32 document_size() const { return document_size_; } | 60 uint32_t document_size() const { return document_size_; } |
59 | 61 |
60 // Return number of bytes available. | 62 // Return number of bytes available. |
61 uint32 GetAvailableData() const; | 63 uint32_t GetAvailableData() const; |
62 | 64 |
63 // Clear pending requests from the queue. | 65 // Clear pending requests from the queue. |
64 void ClearPendingRequests(); | 66 void ClearPendingRequests(); |
65 | 67 |
66 bool is_partial_document() { return partial_document_; } | 68 bool is_partial_document() const { return partial_document_; } |
67 | 69 |
68 private: | 70 private: |
69 // Called by the completion callback of the document's URLLoader. | 71 // Called by the completion callback of the document's URLLoader. |
70 void DidOpen(int32_t result); | 72 void DidOpen(int32_t result); |
71 // Call to read data from the document's URLLoader. | 73 // Call to read data from the document's URLLoader. |
72 void ReadMore(); | 74 void ReadMore(); |
73 // Called by the completion callback of the document's URLLoader. | 75 // Called by the completion callback of the document's URLLoader. |
74 void DidRead(int32_t result); | 76 void DidRead(int32_t result); |
75 | 77 |
76 // If the headers have a byte-range response, writes the start and end | |
77 // positions and returns true if at least the start position was parsed. | |
78 // The end position will be set to 0 if it was not found or parsed from the | |
79 // response. | |
80 // Returns false if not even a start position could be parsed. | |
81 static bool GetByteRange(const std::string& headers, uint32* start, | |
82 uint32* end); | |
83 | |
84 // If the headers have a multi-part response, returns the boundary name. | |
85 // Otherwise returns an empty string. | |
86 static std::string GetMultiPartBoundary(const std::string& headers); | |
87 | |
88 // Called when we detect that partial document load is possible. | 78 // Called when we detect that partial document load is possible. |
89 void LoadPartialDocument(); | 79 void LoadPartialDocument(); |
90 // Called when we have to load full document. | 80 // Called when we have to load full document. |
91 void LoadFullDocument(); | 81 void LoadFullDocument(); |
92 // Download pending requests. | 82 // Download pending requests. |
93 void DownloadPendingRequests(); | 83 void DownloadPendingRequests(); |
94 // Called when we complete server request and read all data from it. | 84 // Called when we complete server request and read all data from it. |
95 void ReadComplete(); | 85 void ReadComplete(); |
96 // Creates request to download size byte of data data starting from position. | 86 // Creates request to download size byte of data data starting from position. |
97 pp::URLRequestInfo GetRequest(uint32 position, uint32 size) const; | 87 pp::URLRequestInfo GetRequest(uint32_t position, uint32_t size) const; |
98 // Returns current request size in bytes. | 88 // Returns current request size in bytes. |
99 uint32 GetRequestSize() const; | 89 uint32_t GetRequestSize() const; |
100 | 90 |
101 Client* client_; | 91 Client* client_; |
102 std::string url_; | 92 std::string url_; |
103 pp::URLLoader loader_; | 93 pp::URLLoader loader_; |
104 pp::CompletionCallbackFactory<DocumentLoader> loader_factory_; | 94 pp::CompletionCallbackFactory<DocumentLoader> loader_factory_; |
105 ChunkStream chunk_stream_; | 95 ChunkStream chunk_stream_; |
106 bool partial_document_; | 96 bool partial_document_; |
107 bool request_pending_; | 97 bool request_pending_; |
108 typedef std::list<std::pair<size_t, size_t> > PendingRequests; | 98 typedef std::list<std::pair<size_t, size_t> > PendingRequests; |
109 PendingRequests pending_requests_; | 99 PendingRequests pending_requests_; |
110 char buffer_[kDefaultRequestSize]; | 100 char buffer_[kDefaultRequestSize]; |
111 uint32 current_pos_; | 101 uint32_t current_pos_; |
112 uint32 current_chunk_size_; | 102 uint32_t current_chunk_size_; |
113 uint32 current_chunk_read_; | 103 uint32_t current_chunk_read_; |
114 uint32 document_size_; | 104 uint32_t document_size_; |
115 bool header_request_; | 105 bool header_request_; |
116 bool is_multipart_; | 106 bool is_multipart_; |
117 std::string multipart_boundary_; | 107 std::string multipart_boundary_; |
118 uint32 requests_count_; | 108 uint32_t requests_count_; |
119 std::list<std::vector<unsigned char> > chunk_buffer_; | 109 std::list<std::vector<unsigned char> > chunk_buffer_; |
120 }; | 110 }; |
121 | 111 |
122 } // namespace chrome_pdf | 112 } // namespace chrome_pdf |
123 | 113 |
124 #endif // PDF_DOCUMENT_LOADER_H_ | 114 #endif // PDF_DOCUMENT_LOADER_H_ |
OLD | NEW |