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
3 // found in the LICENSE file.
5 #ifndef PDF_DOCUMENT_LOADER_H_
6 #define PDF_DOCUMENT_LOADER_H_
12 #include "base/basictypes.h"
13 #include "pdf/chunk_stream.h"
14 #include "ppapi/cpp/url_loader.h"
15 #include "ppapi/utility/completion_callback_factory.h"
17 #define kDefaultRequestSize 32768u
19 namespace chrome_pdf
{
21 class DocumentLoader
{
27 // Gets the pp::Instance object.
28 virtual pp::Instance
* GetPluginInstance() = 0;
29 // Creates new URLLoader based on client settings.
30 virtual pp::URLLoader
CreateURLLoader() = 0;
31 // Notification called when partial information about document is available.
32 // Only called for urls that returns full content size and supports byte
34 virtual void OnPartialDocumentLoaded() = 0;
35 // Notification called when all outstanding pending requests are complete.
36 virtual void OnPendingRequestComplete() = 0;
37 // Notification called when new data is available.
38 virtual void OnNewDataAvailable() = 0;
39 // Notification called when document is fully loaded.
40 virtual void OnDocumentComplete() = 0;
43 explicit DocumentLoader(Client
* client
);
46 bool Init(const pp::URLLoader
& loader
,
47 const std::string
& url
,
48 const std::string
& headers
);
50 // Data access interface. Return true is sucessful.
51 bool GetBlock(uint32_t position
, uint32_t size
, void* buf
) const;
53 // Data availability interface. Return true data avaialble.
54 bool IsDataAvailable(uint32_t position
, uint32_t size
) const;
56 // Data availability interface. Return true data avaialble.
57 void RequestData(uint32_t position
, uint32_t size
);
59 bool IsDocumentComplete() const;
60 uint32_t document_size() const { return document_size_
; }
62 // Return number of bytes available.
63 uint32_t GetAvailableData() const;
65 // Clear pending requests from the queue.
66 void ClearPendingRequests();
68 bool is_partial_document() const { return partial_document_
; }
71 // Called by the completion callback of the document's URLLoader.
72 void DidOpen(int32_t result
);
73 // Call to read data from the document's URLLoader.
75 // Called by the completion callback of the document's URLLoader.
76 void DidRead(int32_t result
);
78 // Called when we detect that partial document load is possible.
79 void LoadPartialDocument();
80 // Called when we have to load full document.
81 void LoadFullDocument();
82 // Download pending requests.
83 void DownloadPendingRequests();
84 // Called when we complete server request and read all data from it.
86 // Creates request to download size byte of data data starting from position.
87 pp::URLRequestInfo
GetRequest(uint32_t position
, uint32_t size
) const;
88 // Returns current request size in bytes.
89 uint32_t GetRequestSize() const;
93 pp::URLLoader loader_
;
94 pp::CompletionCallbackFactory
<DocumentLoader
> loader_factory_
;
95 ChunkStream chunk_stream_
;
96 bool partial_document_
;
97 bool request_pending_
;
98 typedef std::list
<std::pair
<size_t, size_t> > PendingRequests
;
99 PendingRequests pending_requests_
;
100 char buffer_
[kDefaultRequestSize
];
101 uint32_t current_pos_
;
102 uint32_t current_chunk_size_
;
103 uint32_t current_chunk_read_
;
104 uint32_t document_size_
;
105 bool header_request_
;
107 std::string multipart_boundary_
;
108 uint32_t requests_count_
;
109 std::list
<std::vector
<unsigned char> > chunk_buffer_
;
112 } // namespace chrome_pdf
114 #endif // PDF_DOCUMENT_LOADER_H_