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