• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string>
10 #include <vector>
11 
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"
16 
17 #define kDefaultRequestSize 32768u
18 
19 namespace chrome_pdf {
20 
21 class DocumentLoader {
22  public:
23   class Client {
24   public:
25     // Gets the pp::Instance object.
26     virtual pp::Instance* GetPluginInstance() = 0;
27     // Creates new URLLoader based on client settings.
28     virtual pp::URLLoader CreateURLLoader() = 0;
29     // Notification called when partial information about document is available.
30     // Only called for urls that returns full content size and supports byte
31     // range requests.
32     virtual void OnPartialDocumentLoaded() = 0;
33     // Notification called when all outstanding pending requests are complete.
34     virtual void OnPendingRequestComplete() = 0;
35     // Notification called when new data is available.
36     virtual void OnNewDataAvailable() = 0;
37     // Notification called when document is fully loaded.
38     virtual void OnDocumentComplete() = 0;
39   };
40 
41   explicit DocumentLoader(Client* client);
42   virtual ~DocumentLoader();
43 
44   bool Init(const pp::URLLoader& loader,
45             const std::string& url,
46             const std::string& headers);
47 
48   // Data access interface. Return true is sucessful.
49   bool GetBlock(uint32 position, uint32 size, void* buf) const;
50 
51   // Data availability interface. Return true data avaialble.
52   bool IsDataAvailable(uint32 position, uint32 size) const;
53 
54   // Data availability interface. Return true data avaialble.
55   void RequestData(uint32 position, uint32 size);
56 
57   bool IsDocumentComplete() const;
document_size()58   uint32 document_size() const { return document_size_; }
59 
60   // Return number of bytes available.
61   uint32 GetAvailableData() const;
62 
63   // Clear pending requests from the queue.
64   void ClearPendingRequests();
65 
is_partial_document()66   bool is_partial_document() { return partial_document_; }
67 
68  private:
69   // Called by the completion callback of the document's URLLoader.
70   void DidOpen(int32_t result);
71   // Call to read data from the document's URLLoader.
72   void ReadMore();
73   // Called by the completion callback of the document's URLLoader.
74   void DidRead(int32_t result);
75 
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.
89   void LoadPartialDocument();
90   // Called when we have to load full document.
91   void LoadFullDocument();
92   // Download pending requests.
93   void DownloadPendingRequests();
94   // Called when we complete server request and read all data from it.
95   void ReadComplete();
96   // Creates request to download size byte of data data starting from position.
97   pp::URLRequestInfo GetRequest(uint32 position, uint32 size) const;
98   // Returns current request size in bytes.
99   uint32 GetRequestSize() const;
100 
101   Client* client_;
102   std::string url_;
103   pp::URLLoader loader_;
104   pp::CompletionCallbackFactory<DocumentLoader> loader_factory_;
105   ChunkStream chunk_stream_;
106   bool partial_document_;
107   bool request_pending_;
108   typedef std::list<std::pair<size_t, size_t> > PendingRequests;
109   PendingRequests pending_requests_;
110   char buffer_[kDefaultRequestSize];
111   uint32 current_pos_;
112   uint32 current_chunk_size_;
113   uint32 current_chunk_read_;
114   uint32 document_size_;
115   bool header_request_;
116   bool is_multipart_;
117   std::string multipart_boundary_;
118   uint32 requests_count_;
119   std::list<std::vector<unsigned char> > chunk_buffer_;
120 };
121 
122 }  // namespace chrome_pdf
123 
124 #endif  // PDF_DOCUMENT_LOADER_H_
125