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.
5 #ifndef PDF_PDF_ENGINE_H_
6 #define PDF_PDF_ENGINE_H_
8 #include "build/build_config.h"
17 #include "base/strings/string16.h"
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 #include "ppapi/cpp/var_array.h"
34 const uint32 kBackgroundColor
= 0xFFCCCCCC;
36 namespace chrome_pdf
{
40 #if defined(OS_MACOSX)
41 const uint32 kDefaultKeyModifier
= PP_INPUTEVENT_MODIFIER_METAKEY
;
43 const uint32 kDefaultKeyModifier
= PP_INPUTEVENT_MODIFIER_CONTROLKEY
;
46 // Do one time initialization of the SDK.
48 // Tells the SDK that we're shutting down.
51 // This class encapsulates a PDF rendering engine.
55 enum DocumentPermission
{
57 PERMISSION_COPY_ACCESSIBLE
,
58 PERMISSION_PRINT_LOW_QUALITY
,
59 PERMISSION_PRINT_HIGH_QUALITY
,
62 // The interface that's provided to the rendering engine.
65 // Informs the client about the document's size in pixels.
66 virtual void DocumentSizeUpdated(const pp::Size
& size
) = 0;
68 // Informs the client that the given rect needs to be repainted.
69 virtual void Invalidate(const pp::Rect
& rect
) = 0;
71 // Informs the client to scroll the plugin area by the given offset.
72 virtual void Scroll(const pp::Point
& point
) = 0;
74 // Scroll the horizontal/vertical scrollbars to a given position.
75 virtual void ScrollToX(int position
) = 0;
76 virtual void ScrollToY(int position
) = 0;
78 // Scroll to the specified page.
79 virtual void ScrollToPage(int page
) = 0;
81 // Navigate to the given url.
82 virtual void NavigateTo(const std::string
& url
, bool open_in_new_tab
) = 0;
84 // Updates the cursor.
85 virtual void UpdateCursor(PP_CursorType_Dev cursor
) = 0;
87 // Updates the tick marks in the vertical scrollbar.
88 virtual void UpdateTickMarks(const std::vector
<pp::Rect
>& tickmarks
) = 0;
90 // Updates the number of find results for the current search term. If
91 // there are no matches 0 should be passed in. Only when the plugin has
92 // finished searching should it pass in the final count with final_result
94 virtual void NotifyNumberOfFindResultsChanged(int total
,
95 bool final_result
) = 0;
97 // Updates the index of the currently selected search item.
98 virtual void NotifySelectedFindResultChanged(int current_find_index
) = 0;
100 // Prompts the user for a password to open this document. The callback is
101 // called when the password is retrieved.
102 virtual void GetDocumentPassword(
103 pp::CompletionCallbackWithOutput
<pp::Var
> callback
) = 0;
105 // Puts up an alert with the given message.
106 virtual void Alert(const std::string
& message
) = 0;
108 // Puts up a confirm with the given message, and returns true if the user
109 // presses OK, or false if they press cancel.
110 virtual bool Confirm(const std::string
& message
) = 0;
112 // Puts up a prompt with the given message and default answer and returns
114 virtual std::string
Prompt(const std::string
& question
,
115 const std::string
& default_answer
) = 0;
117 // Returns the url of the pdf.
118 virtual std::string
GetURL() = 0;
121 virtual void Email(const std::string
& to
,
122 const std::string
& cc
,
123 const std::string
& bcc
,
124 const std::string
& subject
,
125 const std::string
& body
) = 0;
127 // Put up the print dialog.
128 virtual void Print() = 0;
130 // Submit the data using HTTP POST.
131 virtual void SubmitForm(const std::string
& url
,
135 // Pops up a file selection dialog and returns the result.
136 virtual std::string
ShowFileSelectionDialog() = 0;
138 // Creates and returns new URL loader for partial document requests.
139 virtual pp::URLLoader
CreateURLLoader() = 0;
141 // Calls the client's OnCallback() function in delay_in_ms with the given
143 virtual void ScheduleCallback(int id
, int delay_in_ms
) = 0;
145 // Searches the given string for "term" and returns the results. Unicode-
147 struct SearchStringResult
{
151 virtual void SearchString(const base::char16
* string
,
152 const base::char16
* term
,
154 std::vector
<SearchStringResult
>* results
) = 0;
156 // Notifies the client that the engine has painted a page from the document.
157 virtual void DocumentPaintOccurred() = 0;
159 // Notifies the client that the document has finished loading.
160 virtual void DocumentLoadComplete(int page_count
) = 0;
162 // Notifies the client that the document has failed to load.
163 virtual void DocumentLoadFailed() = 0;
165 virtual pp::Instance
* GetPluginInstance() = 0;
167 // Notifies that an unsupported feature in the PDF was encountered.
168 virtual void DocumentHasUnsupportedFeature(const std::string
& feature
) = 0;
170 // Notifies the client about document load progress.
171 virtual void DocumentLoadProgress(uint32 available
, uint32 doc_size
) = 0;
173 // Notifies the client about focus changes for form text fields.
174 virtual void FormTextFieldFocusChange(bool in_focus
) = 0;
176 // Returns true if the plugin has been opened within print preview.
177 virtual bool IsPrintPreview() = 0;
180 // Factory method to create an instance of the PDF Engine.
181 static PDFEngine
* Create(Client
* client
);
183 virtual ~PDFEngine() {}
184 // Most of these functions are similar to the Pepper functions of the same
185 // name, so not repeating the description here unless it's different.
186 virtual bool New(const char* url
) = 0;
187 virtual bool New(const char* url
,
188 const char* headers
) = 0;
189 virtual void PageOffsetUpdated(const pp::Point
& page_offset
) = 0;
190 virtual void PluginSizeUpdated(const pp::Size
& size
) = 0;
191 virtual void ScrolledToXPosition(int position
) = 0;
192 virtual void ScrolledToYPosition(int position
) = 0;
193 // Paint is called a series of times. Before these n calls are made, PrePaint
194 // is called once. After Paint is called n times, PostPaint is called once.
195 virtual void PrePaint() = 0;
196 virtual void Paint(const pp::Rect
& rect
,
197 pp::ImageData
* image_data
,
198 std::vector
<pp::Rect
>* ready
,
199 std::vector
<pp::Rect
>* pending
) = 0;
200 virtual void PostPaint() = 0;
201 virtual bool HandleDocumentLoad(const pp::URLLoader
& loader
) = 0;
202 virtual bool HandleEvent(const pp::InputEvent
& event
) = 0;
203 virtual uint32_t QuerySupportedPrintOutputFormats() = 0;
204 virtual void PrintBegin() = 0;
205 virtual pp::Resource
PrintPages(
206 const PP_PrintPageNumberRange_Dev
* page_ranges
,
207 uint32_t page_range_count
,
208 const PP_PrintSettings_Dev
& print_settings
) = 0;
209 virtual void PrintEnd() = 0;
210 virtual void StartFind(const char* text
, bool case_sensitive
) = 0;
211 virtual bool SelectFindResult(bool forward
) = 0;
212 virtual void StopFind() = 0;
213 virtual void ZoomUpdated(double new_zoom_level
) = 0;
214 virtual void RotateClockwise() = 0;
215 virtual void RotateCounterclockwise() = 0;
216 virtual std::string
GetSelectedText() = 0;
217 virtual std::string
GetLinkAtPosition(const pp::Point
& point
) = 0;
218 virtual bool IsSelecting() = 0;
219 // Checks the permissions associated with this document.
220 virtual bool HasPermission(DocumentPermission permission
) const = 0;
221 virtual void SelectAll() = 0;
222 // Gets the number of pages in the document.
223 virtual int GetNumberOfPages() = 0;
224 // Gets the 0-based page number of |destination|, or -1 if it does not exist.
225 virtual int GetNamedDestinationPage(const std::string
& destination
) = 0;
226 // Gets the index of the first visible page, or -1 if none are visible.
227 virtual int GetFirstVisiblePage() = 0;
228 // Gets the index of the most visible page, or -1 if none are visible.
229 virtual int GetMostVisiblePage() = 0;
230 // Gets the rectangle of the page including shadow.
231 virtual pp::Rect
GetPageRect(int index
) = 0;
232 // Gets the rectangle of the page excluding any additional areas.
233 virtual pp::Rect
GetPageContentsRect(int index
) = 0;
234 // Gets the offset of the vertical scrollbar from the top in document
236 virtual int GetVerticalScrollbarYPosition() = 0;
237 // Paints page thumbnail to the ImageData.
238 virtual void PaintThumbnail(pp::ImageData
* image_data
, int index
) = 0;
239 // Set color / grayscale rendering modes.
240 virtual void SetGrayscale(bool grayscale
) = 0;
241 // Callback for timer that's set with ScheduleCallback().
242 virtual void OnCallback(int id
) = 0;
243 // Gets the JSON representation of the PDF file
244 virtual std::string
GetPageAsJSON(int index
) = 0;
245 // Gets the PDF document's print scaling preference. True if the document can
247 virtual bool GetPrintScaling() = 0;
248 // Returns number of copies to be printed.
249 virtual int GetCopiesToPrint() = 0;
251 // Returns a VarArray of Bookmarks, each a VarDictionary containing the
252 // following key/values:
253 // - "title" - a string Var.
254 // - "page" - an int Var.
255 // - "children" - a VarArray(), with each entry containing a VarDictionary of
256 // the same structure.
257 virtual pp::VarArray
GetBookmarks() = 0;
259 // Append blank pages to make a 1-page document to a |num_pages| document.
260 // Always retain the first page data.
261 virtual void AppendBlankPages(int num_pages
) = 0;
262 // Append the first page of the document loaded with the |engine| to this
263 // document at page |index|.
264 virtual void AppendPage(PDFEngine
* engine
, int index
) = 0;
266 // Allow client to query and reset scroll positions in document coordinates.
267 // Note that this is meant for cases where the device scale factor changes,
268 // and not for general scrolling - the engine will not repaint due to this.
269 virtual pp::Point
GetScrollPosition() = 0;
270 virtual void SetScrollPosition(const pp::Point
& position
) = 0;
272 virtual bool IsProgressiveLoad() = 0;
273 virtual pp::VarDictionary
GetNamedDestinations() = 0;
276 // Interface for exports that wrap the PDF engine.
277 class PDFEngineExports
{
279 struct RenderingSettings
{
280 RenderingSettings(int dpi_x
,
282 const pp::Rect
& bounds
,
284 bool stretch_to_bounds
,
285 bool keep_aspect_ratio
,
286 bool center_in_bounds
,
288 : dpi_x(dpi_x
), dpi_y(dpi_y
), bounds(bounds
),
289 fit_to_bounds(fit_to_bounds
), stretch_to_bounds(stretch_to_bounds
),
290 keep_aspect_ratio(keep_aspect_ratio
),
291 center_in_bounds(center_in_bounds
), autorotate(autorotate
) {
297 bool stretch_to_bounds
;
298 bool keep_aspect_ratio
;
299 bool center_in_bounds
;
303 PDFEngineExports() {}
304 virtual ~PDFEngineExports() {}
305 static PDFEngineExports
* Create();
307 // See the definition of RenderPDFPageToDC in pdf.cc for details.
308 virtual bool RenderPDFPageToDC(const void* pdf_buffer
,
311 const RenderingSettings
& settings
,
314 // See the definition of RenderPDFPageToBitmap in pdf.cc for details.
315 virtual bool RenderPDFPageToBitmap(const void* pdf_buffer
,
318 const RenderingSettings
& settings
,
319 void* bitmap_buffer
) = 0;
321 virtual bool GetPDFDocInfo(const void* pdf_buffer
,
324 double* max_page_width
) = 0;
326 // See the definition of GetPDFPageSizeByIndex in pdf.cc for details.
327 virtual bool GetPDFPageSizeByIndex(const void* pdf_buffer
,
328 int pdf_buffer_size
, int page_number
,
329 double* width
, double* height
) = 0;
332 } // namespace chrome_pdf
334 #endif // PDF_PDF_ENGINE_H_