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