1 // Copyright (c) 2012 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_PDF_ENGINE_H_
6 #define PDF_PDF_ENGINE_H_
7 
8 #include "build/build_config.h"
9 
10 #if defined(OS_WIN)
11 #include <windows.h>
12 #endif
13 
14 #include <string>
15 #include <vector>
16 
17 #include "base/strings/string16.h"
18 
19 #include "ppapi/c/dev/pp_cursor_type_dev.h"
20 #include "ppapi/c/dev/ppp_printing_dev.h"
21 #include "ppapi/c/ppb_input_event.h"
22 #include "ppapi/cpp/completion_callback.h"
23 #include "ppapi/cpp/image_data.h"
24 #include "ppapi/cpp/rect.h"
25 #include "ppapi/cpp/size.h"
26 #include "ppapi/cpp/url_loader.h"
27 
28 namespace pp {
29 class InputEvent;
30 }
31 
32 const uint32 kBackgroundColor = 0xFFCCCCCC;
33 
34 namespace chrome_pdf {
35 
36 class Stream;
37 
38 #if defined(OS_MACOSX)
39 const uint32 kDefaultKeyModifier = PP_INPUTEVENT_MODIFIER_METAKEY;
40 #else  // !OS_MACOSX
41 const uint32 kDefaultKeyModifier = PP_INPUTEVENT_MODIFIER_CONTROLKEY;
42 #endif  // OS_MACOSX
43 
44 // Do one time initialization of the SDK.  data is platform specific, on Windows
45 // it's the instance of the DLL and it's unused on other platforms.
46 bool InitializeSDK(void* data);
47 // Tells the SDK that we're shutting down.
48 void ShutdownSDK();
49 
50 // This class encapsulates a PDF rendering engine.
51 class PDFEngine {
52  public:
53 
54   enum DocumentPermission {
55     PERMISSION_COPY,
56     PERMISSION_COPY_ACCESSIBLE,
57     PERMISSION_PRINT_LOW_QUALITY,
58     PERMISSION_PRINT_HIGH_QUALITY,
59   };
60 
61   // The interface that's provided to the rendering engine.
62   class Client {
63    public:
64     // Informs the client about the document's size in pixels.
65     virtual void DocumentSizeUpdated(const pp::Size& size) = 0;
66 
67     // Informs the client that the given rect needs to be repainted.
68     virtual void Invalidate(const pp::Rect& rect) = 0;
69 
70     // Informs the client to scroll the plugin area by the given offset.
71     virtual void Scroll(const pp::Point& point) = 0;
72 
73     // Scroll the horizontal/vertical scrollbars to a given position.
74     virtual void ScrollToX(int position) = 0;
75     virtual void ScrollToY(int position) = 0;
76 
77     // Scroll to the specified page.
78     virtual void ScrollToPage(int page) = 0;
79 
80     // Navigate to the given url.
81     virtual void NavigateTo(const std::string& url, bool open_in_new_tab) = 0;
82 
83     // Updates the cursor.
84     virtual void UpdateCursor(PP_CursorType_Dev cursor) = 0;
85 
86     // Updates the tick marks in the vertical scrollbar.
87     virtual void UpdateTickMarks(const std::vector<pp::Rect>& tickmarks) = 0;
88 
89     // Updates the number of find results for the current search term.  If
90     // there are no matches 0 should be passed in.  Only when the plugin has
91     // finished searching should it pass in the final count with final_result
92     // set to true.
93     virtual void NotifyNumberOfFindResultsChanged(int total,
94                                                   bool final_result) = 0;
95 
96     // Updates the index of the currently selected search item.
97     virtual void NotifySelectedFindResultChanged(int current_find_index) = 0;
98 
99     // Prompts the user for a password to open this document. The callback is
100     // called when the password is retrieved.
101     virtual void GetDocumentPassword(
102         pp::CompletionCallbackWithOutput<pp::Var> callback) = 0;
103 
104     // Puts up an alert with the given message.
105     virtual void Alert(const std::string& message) = 0;
106 
107     // Puts up a confirm with the given message, and returns true if the user
108     // presses OK, or false if they press cancel.
109     virtual bool Confirm(const std::string& message) = 0;
110 
111     // Puts up a prompt with the given message and default answer and returns
112     // the answer.
113     virtual std::string Prompt(const std::string& question,
114                                const std::string& default_answer) = 0;
115 
116     // Returns the url of the pdf.
117     virtual std::string GetURL() = 0;
118 
119     // Send an email.
120     virtual void Email(const std::string& to,
121                        const std::string& cc,
122                        const std::string& bcc,
123                        const std::string& subject,
124                        const std::string& body) = 0;
125 
126     // Put up the print dialog.
127     virtual void Print() = 0;
128 
129     // Submit the data using HTTP POST.
130     virtual void SubmitForm(const std::string& url,
131                             const void* data,
132                             int length) = 0;
133 
134     // Pops up a file selection dialog and returns the result.
135     virtual std::string ShowFileSelectionDialog() = 0;
136 
137     // Creates and returns new URL loader for partial document requests.
138     virtual pp::URLLoader CreateURLLoader() = 0;
139 
140     // Calls the client's OnCallback() function in delay_in_ms with the given
141     // id.
142     virtual void ScheduleCallback(int id, int delay_in_ms) = 0;
143 
144     // Searches the given string for "term" and returns the results.  Unicode-
145     // aware.
146     struct SearchStringResult {
147       int start_index;
148       int length;
149     };
150     virtual void SearchString(const base::char16* string,
151                               const base::char16* term,
152                               bool case_sensitive,
153                               std::vector<SearchStringResult>* results) = 0;
154 
155     // Notifies the client that the engine has painted a page from the document.
156     virtual void DocumentPaintOccurred() = 0;
157 
158     // Notifies the client that the document has finished loading.
159     virtual void DocumentLoadComplete(int page_count) = 0;
160 
161     // Notifies the client that the document has failed to load.
162     virtual void DocumentLoadFailed() = 0;
163 
164     virtual pp::Instance* GetPluginInstance() = 0;
165 
166     // Notifies that an unsupported feature in the PDF was encountered.
167     virtual void DocumentHasUnsupportedFeature(const std::string& feature) = 0;
168 
169     // Notifies the client about document load progress.
170     virtual void DocumentLoadProgress(uint32 available, uint32 doc_size) = 0;
171 
172     // Notifies the client about focus changes for form text fields.
173     virtual void FormTextFieldFocusChange(bool in_focus) = 0;
174 
175     // Returns true if the plugin has been opened within print preview.
176     virtual bool IsPrintPreview() = 0;
177   };
178 
179   // Factory method to create an instance of the PDF Engine.
180   static PDFEngine* Create(Client* client);
181 
~PDFEngine()182   virtual ~PDFEngine() {}
183   // Most of these functions are similar to the Pepper functions of the same
184   // name, so not repeating the description here unless it's different.
185   virtual bool New(const char* url) = 0;
186   virtual bool New(const char* url,
187                    const char* headers) = 0;
188   virtual void PageOffsetUpdated(const pp::Point& page_offset) = 0;
189   virtual void PluginSizeUpdated(const pp::Size& size) = 0;
190   virtual void ScrolledToXPosition(int position) = 0;
191   virtual void ScrolledToYPosition(int position) = 0;
192   // Paint is called a series of times. Before these n calls are made, PrePaint
193   // is called once. After Paint is called n times, PostPaint is called once.
194   virtual void PrePaint() = 0;
195   virtual void Paint(const pp::Rect& rect,
196                      pp::ImageData* image_data,
197                      std::vector<pp::Rect>* ready,
198                      std::vector<pp::Rect>* pending) = 0;
199   virtual void PostPaint() = 0;
200   virtual bool HandleDocumentLoad(const pp::URLLoader& loader) = 0;
201   virtual bool HandleEvent(const pp::InputEvent& event) = 0;
202   virtual uint32_t QuerySupportedPrintOutputFormats() = 0;
203   virtual void PrintBegin() = 0;
204   virtual pp::Resource PrintPages(
205       const PP_PrintPageNumberRange_Dev* page_ranges,
206       uint32_t page_range_count,
207       const PP_PrintSettings_Dev& print_settings) = 0;
208   virtual void PrintEnd() = 0;
209   virtual void StartFind(const char* text, bool case_sensitive) = 0;
210   virtual bool SelectFindResult(bool forward) = 0;
211   virtual void StopFind() = 0;
212   virtual void ZoomUpdated(double new_zoom_level) = 0;
213   virtual void RotateClockwise() = 0;
214   virtual void RotateCounterclockwise() = 0;
215   virtual std::string GetSelectedText() = 0;
216   virtual std::string GetLinkAtPosition(const pp::Point& point) = 0;
217   virtual bool IsSelecting() = 0;
218   // Checks the permissions associated with this document.
219   virtual bool HasPermission(DocumentPermission permission) const = 0;
220   virtual void SelectAll() = 0;
221   // Gets the number of pages in the document.
222   virtual int GetNumberOfPages() = 0;
223   // Gets the 0-based page number of |destination|, or -1 if it does not exist.
224   virtual int GetNamedDestinationPage(const std::string& destination) = 0;
225   // Gets the index of the first visible page, or -1 if none are visible.
226   virtual int GetFirstVisiblePage() = 0;
227   // Gets the index of the most visible page, or -1 if none are visible.
228   virtual int GetMostVisiblePage() = 0;
229   // Gets the rectangle of the page including shadow.
230   virtual pp::Rect GetPageRect(int index) = 0;
231   // Gets the rectangle of the page excluding any additional areas.
232   virtual pp::Rect GetPageContentsRect(int index) = 0;
233   // Gets the offset of the vertical scrollbar from the top in document
234   // coordinates.
235   virtual int GetVerticalScrollbarYPosition() = 0;
236   // Paints page thumbnail to the ImageData.
237   virtual void PaintThumbnail(pp::ImageData* image_data, int index) = 0;
238   // Set color / grayscale rendering modes.
239   virtual void SetGrayscale(bool grayscale) = 0;
240   // Callback for timer that's set with ScheduleCallback().
241   virtual void OnCallback(int id) = 0;
242   // Gets the JSON representation of the PDF file
243   virtual std::string GetPageAsJSON(int index) = 0;
244   // Gets the PDF document's print scaling preference. True if the document can
245   // be scaled to fit.
246   virtual bool GetPrintScaling() = 0;
247 
248   // Append blank pages to make a 1-page document to a |num_pages| document.
249   // Always retain the first page data.
250   virtual void AppendBlankPages(int num_pages) = 0;
251   // Append the first page of the document loaded with the |engine| to this
252   // document at page |index|.
253   virtual void AppendPage(PDFEngine* engine, int index) = 0;
254 
255   // Allow client to query and reset scroll positions in document coordinates.
256   // Note that this is meant for cases where the device scale factor changes,
257   // and not for general scrolling - the engine will not repaint due to this.
258   virtual pp::Point GetScrollPosition() = 0;
259   virtual void SetScrollPosition(const pp::Point& position) = 0;
260 
261   virtual bool IsProgressiveLoad() = 0;
262 };
263 
264 // Interface for exports that wrap the PDF engine.
265 class PDFEngineExports {
266  public:
267   struct RenderingSettings {
RenderingSettingsRenderingSettings268    RenderingSettings(int dpi_x,
269                      int dpi_y,
270                      const pp::Rect& bounds,
271                      bool fit_to_bounds,
272                      bool stretch_to_bounds,
273                      bool keep_aspect_ratio,
274                      bool center_in_bounds,
275                      bool autorotate)
276       : dpi_x(dpi_x), dpi_y(dpi_y), bounds(bounds),
277         fit_to_bounds(fit_to_bounds), stretch_to_bounds(stretch_to_bounds),
278         keep_aspect_ratio(keep_aspect_ratio),
279         center_in_bounds(center_in_bounds), autorotate(autorotate) {
280     }
281     int dpi_x;
282     int dpi_y;
283     pp::Rect bounds;
284     bool fit_to_bounds;
285     bool stretch_to_bounds;
286     bool keep_aspect_ratio;
287     bool center_in_bounds;
288     bool autorotate;
289   };
290 
PDFEngineExports()291   PDFEngineExports() {}
~PDFEngineExports()292   virtual ~PDFEngineExports() {}
293   static PDFEngineExports* Create();
294 #if defined(OS_WIN)
295   // See the definition of RenderPDFPageToDC in pdf.cc for details.
296   virtual bool RenderPDFPageToDC(const void* pdf_buffer,
297                                  int buffer_size,
298                                  int page_number,
299                                  const RenderingSettings& settings,
300                                  HDC dc) = 0;
301 #endif  // OS_WIN
302   // See the definition of RenderPDFPageToBitmap in pdf.cc for details.
303   virtual bool RenderPDFPageToBitmap(const void* pdf_buffer,
304                                      int pdf_buffer_size,
305                                      int page_number,
306                                      const RenderingSettings& settings,
307                                      void* bitmap_buffer) = 0;
308 
309   virtual bool GetPDFDocInfo(const void* pdf_buffer,
310                              int buffer_size,
311                              int* page_count,
312                              double* max_page_width) = 0;
313 
314   // See the definition of GetPDFPageSizeByIndex in pdf.cc for details.
315   virtual bool GetPDFPageSizeByIndex(const void* pdf_buffer,
316                                      int pdf_buffer_size, int page_number,
317                                      double* width, double* height) = 0;
318 };
319 
320 }  // namespace chrome_pdf
321 
322 #endif  // PDF_PDF_ENGINE_H_
323