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