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