Upload upstream chromium 71.0.3578.0
[platform/framework/web/chromium-efl.git] / pdf / out_of_process_instance.h
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_OUT_OF_PROCESS_INSTANCE_H_
6 #define PDF_OUT_OF_PROCESS_INSTANCE_H_
7
8 #include <stdint.h>
9 #include <string.h>
10
11 #include <memory>
12 #include <set>
13 #include <string>
14 #include <utility>
15 #include <vector>
16
17 #include "base/containers/queue.h"
18 #include "base/macros.h"
19 #include "pdf/paint_manager.h"
20 #include "pdf/pdf_engine.h"
21 #include "pdf/preview_mode_client.h"
22 #include "ppapi/c/private/ppb_pdf.h"
23 #include "ppapi/c/private/ppp_pdf.h"
24 #include "ppapi/cpp/dev/printing_dev.h"
25 #include "ppapi/cpp/dev/scriptable_object_deprecated.h"
26 #include "ppapi/cpp/graphics_2d.h"
27 #include "ppapi/cpp/image_data.h"
28 #include "ppapi/cpp/input_event.h"
29 #include "ppapi/cpp/instance.h"
30 #include "ppapi/cpp/private/find_private.h"
31 #include "ppapi/cpp/private/uma_private.h"
32 #include "ppapi/cpp/url_loader.h"
33 #include "ppapi/utility/completion_callback_factory.h"
34
35 namespace pp {
36 class TextInput_Dev;
37 }
38
39 namespace chrome_pdf {
40
41 class OutOfProcessInstance : public pp::Instance,
42                              public pp::Find_Private,
43                              public pp::Printing_Dev,
44                              public PaintManager::Client,
45                              public PDFEngine::Client,
46                              public PreviewModeClient::Client {
47  public:
48   explicit OutOfProcessInstance(PP_Instance instance);
49   ~OutOfProcessInstance() override;
50
51   // pp::Instance implementation.
52   bool Init(uint32_t argc, const char* argn[], const char* argv[]) override;
53   void HandleMessage(const pp::Var& message) override;
54   bool HandleInputEvent(const pp::InputEvent& event) override;
55   void DidChangeView(const pp::View& view) override;
56   void DidChangeFocus(bool has_focus) override;
57
58   // pp::Find_Private implementation.
59   bool StartFind(const std::string& text, bool case_sensitive) override;
60   void SelectFindResult(bool forward) override;
61   void StopFind() override;
62
63   // pp::PaintManager::Client implementation.
64   void OnPaint(const std::vector<pp::Rect>& paint_rects,
65                std::vector<PaintManager::ReadyRect>* ready,
66                std::vector<pp::Rect>* pending) override;
67
68   // pp::Printing_Dev implementation.
69   uint32_t QuerySupportedPrintOutputFormats() override;
70   int32_t PrintBegin(const PP_PrintSettings_Dev& print_settings) override;
71   pp::Resource PrintPages(const PP_PrintPageNumberRange_Dev* page_ranges,
72                           uint32_t page_range_count) override;
73   void PrintEnd() override;
74   bool IsPrintScalingDisabled() override;
75
76   // pp::Private implementation.
77   pp::Var GetLinkAtPosition(const pp::Point& point);
78   void GetPrintPresetOptionsFromDocument(PP_PdfPrintPresetOptions_Dev* options);
79   void EnableAccessibility();
80   void SetCaretPosition(const pp::FloatPoint& position);
81   void MoveRangeSelectionExtent(const pp::FloatPoint& extent);
82   void SetSelectionBounds(const pp::FloatPoint& base,
83                           const pp::FloatPoint& extent);
84   bool CanEditText();
85   bool HasEditableText();
86   void ReplaceSelection(const std::string& text);
87   bool CanUndo();
88   bool CanRedo();
89   void Undo();
90   void Redo();
91   int32_t PdfPrintBegin(const PP_PrintSettings_Dev* print_settings,
92                         const PP_PdfPrintSettings_Dev* pdf_print_settings);
93
94   void FlushCallback(int32_t result);
95   void DidOpen(int32_t result);
96   void DidOpenPreview(int32_t result);
97
98   // Called to print without re-entrancy issues.
99   void OnPrint(int32_t);
100
101   // PDFEngine::Client implementation.
102   void DocumentSizeUpdated(const pp::Size& size) override;
103   void Invalidate(const pp::Rect& rect) override;
104   void DidScroll(const pp::Point& point) override;
105   void ScrollToX(int x_in_screen_coords) override;
106   void ScrollToY(int y_in_screen_coords, bool compensate_for_toolbar) override;
107   void ScrollBy(const pp::Point& point) override;
108   void ScrollToPage(int page) override;
109   void NavigateTo(const std::string& url,
110                   WindowOpenDisposition disposition) override;
111   void UpdateCursor(PP_CursorType_Dev cursor) override;
112   void UpdateTickMarks(const std::vector<pp::Rect>& tickmarks) override;
113   void NotifyNumberOfFindResultsChanged(int total, bool final_result) override;
114   void NotifySelectedFindResultChanged(int current_find_index) override;
115   void NotifyPageBecameVisible(
116       const PDFEngine::PageFeatures* page_features) override;
117   void GetDocumentPassword(
118       pp::CompletionCallbackWithOutput<pp::Var> callback) override;
119   void Beep() override;
120   void Alert(const std::string& message) override;
121   bool Confirm(const std::string& message) override;
122   std::string Prompt(const std::string& question,
123                      const std::string& default_answer) override;
124   std::string GetURL() override;
125   void Email(const std::string& to,
126              const std::string& cc,
127              const std::string& bcc,
128              const std::string& subject,
129              const std::string& body) override;
130   void Print() override;
131   void SubmitForm(const std::string& url,
132                   const void* data,
133                   int length) override;
134   pp::URLLoader CreateURLLoader() override;
135   std::vector<SearchStringResult> SearchString(const base::char16* string,
136                                                const base::char16* term,
137                                                bool case_sensitive) override;
138   void DocumentPaintOccurred() override;
139   void DocumentLoadComplete(
140       const PDFEngine::DocumentFeatures& document_features,
141       uint32_t file_size) override;
142   void DocumentLoadFailed() override;
143   void FontSubstituted() override;
144   pp::Instance* GetPluginInstance() override;
145   void DocumentHasUnsupportedFeature(const std::string& feature) override;
146   void DocumentLoadProgress(uint32_t available, uint32_t doc_size) override;
147   void FormTextFieldFocusChange(bool in_focus) override;
148   bool IsPrintPreview() override;
149   uint32_t GetBackgroundColor() override;
150   void CancelBrowserDownload() override;
151   void IsSelectingChanged(bool is_selecting) override;
152   void SelectionChanged(const pp::Rect& left, const pp::Rect& right) override;
153   void IsEditModeChanged(bool is_edit_mode) override;
154   float GetToolbarHeightInScreenCoords() override;
155
156   // PreviewModeClient::Client implementation.
157   void PreviewDocumentLoadComplete() override;
158   void PreviewDocumentLoadFailed() override;
159
160   // Helper functions for implementing PPP_PDF.
161   void RotateClockwise();
162   void RotateCounterclockwise();
163
164  private:
165   void ResetRecentlySentFindUpdate(int32_t);
166
167   // Called whenever the plugin geometry changes to update the location of the
168   // background parts, and notifies the pdf engine.
169   void OnGeometryChanged(double old_zoom, float old_device_scale);
170
171   // Figures out the location of any background rectangles (i.e. those that
172   // aren't painted by the PDF engine).
173   void CalculateBackgroundParts();
174
175   // Computes document width/height in device pixels, based on current zoom and
176   // device scale
177   int GetDocumentPixelWidth() const;
178   int GetDocumentPixelHeight() const;
179
180   // Draws a rectangle with the specified dimensions and color in our buffer.
181   void FillRect(const pp::Rect& rect, uint32_t color);
182
183   void LoadUrl(const std::string& url, bool is_print_preview);
184
185   // Creates a URL loader and allows it to access all urls, i.e. not just the
186   // frame's origin.
187   pp::URLLoader CreateURLLoaderInternal();
188
189   void Save(const std::string& token);
190   void ConsumeSaveToken(const std::string& token);
191
192   void FormDidOpen(int32_t result);
193
194   void UserMetricsRecordAction(const std::string& action);
195
196   // Start loading accessibility information.
197   void LoadAccessibility();
198
199   // Send accessibility information about the given page index.
200   void SendNextAccessibilityPage(int32_t page_index);
201
202   // Send the accessibility information about the current viewport. This is
203   // done once when accessibility is first loaded and again when the geometry
204   // changes.
205   void SendAccessibilityViewportInfo();
206
207   enum DocumentLoadState {
208     LOAD_STATE_LOADING,
209     LOAD_STATE_COMPLETE,
210     LOAD_STATE_FAILED,
211   };
212
213   // Set new zoom scale.
214   void SetZoom(double scale);
215
216   // Reduces the document to 1 page and appends |print_preview_page_count_| - 1
217   // blank pages to the document for print preview.
218   void AppendBlankPrintPreviewPages();
219
220   // Process the preview page data information. |src_url| specifies the preview
221   // page data location. The |src_url| is in the format:
222   // chrome://print/id/page_number/print.pdf
223   // |dest_page_index| specifies the blank page index that needs to be replaced
224   // with the new page data.
225   void ProcessPreviewPageInfo(const std::string& src_url, int dest_page_index);
226   // Load the next available preview page into the blank page.
227   void LoadAvailablePreviewPage();
228
229   // Called after a preview page has loaded or failed to load.
230   void LoadNextPreviewPage();
231
232   // Send a notification that the print preview has loaded.
233   void SendPrintPreviewLoadedNotification();
234
235   // Bound the given scroll offset to the document.
236   pp::FloatPoint BoundScrollOffsetToDocument(
237       const pp::FloatPoint& scroll_offset);
238
239   // Wrappers for |uma_| so histogram reporting only occurs when the PDF Viewer
240   // is not being used for print preview.
241   void HistogramCustomCounts(const std::string& name,
242                              int32_t sample,
243                              int32_t min,
244                              int32_t max,
245                              uint32_t bucket_count);
246   void HistogramEnumeration(const std::string& name,
247                             int32_t sample,
248                             int32_t boundary_value);
249
250   // Wrapper for |uma_| so PrintPreview.PdfAction histogram reporting only
251   // occurs when the PDF Viewer is being used inside print preview.
252   void PrintPreviewHistogramEnumeration(int32_t sample);
253
254   pp::ImageData image_data_;
255   // Used when the plugin is embedded in a page and we have to create the loader
256   // ourself.
257   pp::URLLoader embed_loader_;
258   pp::URLLoader embed_preview_loader_;
259
260   PP_CursorType_Dev cursor_;  // The current cursor.
261
262   // Size, in pixels, of plugin rectangle.
263   pp::Size plugin_size_;
264   // Size, in DIPs, of plugin rectangle.
265   pp::Size plugin_dip_size_;
266   // Remaining area, in pixels, to render the pdf in after accounting for
267   // horizontal centering.
268   pp::Rect available_area_;
269   // Size of entire document in pixels (i.e. if each page is 800 pixels high and
270   // there are 10 pages, the height will be 8000).
271   pp::Size document_size_;
272   // The scroll offset in CSS pixels.
273   pp::Point scroll_offset_;
274
275   // Enumeration of pinch states.
276   // This should match PinchPhase enum in
277   // chrome/browser/resources/pdf/viewport.js
278   enum PinchPhase {
279     PINCH_NONE = 0,
280     PINCH_START = 1,
281     PINCH_UPDATE_ZOOM_OUT = 2,
282     PINCH_UPDATE_ZOOM_IN = 3,
283     PINCH_END = 4
284   };
285
286   // Current zoom factor.
287   double zoom_;
288   // True if we request a new bitmap rendering.
289   bool needs_reraster_;
290   // The scroll position for the last raster, before any transformations are
291   // applied.
292   pp::FloatPoint scroll_offset_at_last_raster_;
293   // True if last bitmap was smaller than screen.
294   bool last_bitmap_smaller_;
295   // Current device scale factor. Multiply by |device_scale_| to convert from
296   // viewport to screen coordinates. Divide by |device_scale_| to convert from
297   // screen to viewport coordinates.
298   float device_scale_;
299   // True if the plugin is full-page.
300   bool full_;
301
302   PaintManager paint_manager_;
303
304   struct BackgroundPart {
305     pp::Rect location;
306     uint32_t color;
307   };
308   std::vector<BackgroundPart> background_parts_;
309
310   struct PrintSettings {
311     PrintSettings() { Clear(); }
312
313     void Clear();
314
315     // This is set to true when PdfPrintBegin() is called and false when
316     // PrintEnd() is called.
317     bool is_printing;
318
319     // To know whether this was an actual print operation, so we don't double
320     // count UMA logging.
321     bool print_pages_called;
322
323     // Generic print settings.
324     PP_PrintSettings_Dev pepper_print_settings;
325
326     // PDF-specific print settings.
327     PP_PdfPrintSettings_Dev pdf_print_settings;
328   };
329
330   PrintSettings print_settings_;
331
332   std::unique_ptr<PDFEngine> engine_;
333
334   // The PreviewModeClient used for print preview. Will be passed to
335   // |preview_engine_|.
336   std::unique_ptr<PreviewModeClient> preview_client_;
337
338   // This engine is used to render the individual preview page data. This is
339   // used only in print preview mode. This will use |PreviewModeClient|
340   // interface which has very limited access to the pp::Instance.
341   std::unique_ptr<PDFEngine> preview_engine_;
342
343   std::string url_;
344
345   // Used for submitting forms.
346   pp::URLLoader form_loader_;
347
348   pp::CompletionCallbackFactory<OutOfProcessInstance> callback_factory_;
349
350   // The callback for receiving the password from the page.
351   std::unique_ptr<pp::CompletionCallbackWithOutput<pp::Var>> password_callback_;
352
353   // True if we haven't painted the plugin viewport yet.
354   bool first_paint_;
355
356   DocumentLoadState document_load_state_;
357   DocumentLoadState preview_document_load_state_;
358
359   // A UMA resource for histogram reporting.
360   pp::UMAPrivate uma_;
361
362   // Used so that we only tell the browser once about an unsupported feature, to
363   // avoid the infobar going up more than once.
364   bool told_browser_about_unsupported_feature_;
365
366   // Keeps track of which unsupported features we reported, so we avoid spamming
367   // the stats if a feature shows up many times per document.
368   std::set<std::string> unsupported_features_reported_;
369
370   // Keeps track of whether font substitution has been reported, so we avoid
371   // spamming the stats if a document requested multiple substitutes.
372   bool font_substitution_reported_;
373
374   // Number of pages in print preview mode for non-PDF source, 0 if print
375   // previewing a PDF, and -1 if not in print preview mode.
376   int print_preview_page_count_;
377
378   // Number of pages loaded in print preview mode for non-PDF source. Always
379   // less than or equal to |print_preview_page_count_|.
380   int print_preview_loaded_page_count_;
381
382   // Used to manage loaded print preview page information. A |PreviewPageInfo|
383   // consists of data source URL string and the page index in the destination
384   // document.
385   // The URL string embeds a page number that can be found with
386   // ExtractPrintPreviewPageIndex(). This page number is always greater than 0.
387   // The page index is always in the range of [0, print_preview_page_count_).
388   using PreviewPageInfo = std::pair<std::string, int>;
389   base::queue<PreviewPageInfo> preview_pages_info_;
390
391   // Used to signal the browser about focus changes to trigger the OSK.
392   // TODO(abodenha@chromium.org) Implement full IME support in the plugin.
393   // http://crbug.com/132565
394   std::unique_ptr<pp::TextInput_Dev> text_input_;
395
396   // The last document load progress value sent to the web page.
397   double last_progress_sent_;
398
399   // Whether an update to the number of find results found was sent less than
400   // |kFindResultCooldownMs| milliseconds ago.
401   bool recently_sent_find_update_;
402
403   // The tickmarks.
404   std::vector<pp::Rect> tickmarks_;
405
406   // Whether the plugin has received a viewport changed message. Nothing should
407   // be painted until this is received.
408   bool received_viewport_message_;
409
410   // If true, this means we told the RenderView that we're starting a network
411   // request so that it can start the throbber. We will tell it again once the
412   // document finishes loading.
413   bool did_call_start_loading_;
414
415   // If this is true, then don't scroll the plugin in response to DidChangeView
416   // messages. This will be true when the extension page is in the process of
417   // zooming the plugin so that flickering doesn't occur while zooming.
418   bool stop_scrolling_;
419
420   // The background color of the PDF viewer.
421   uint32_t background_color_;
422
423   // The blank space above the first page of the document reserved for the
424   // toolbar.
425   int top_toolbar_height_in_viewport_coords_;
426
427   // Whether each page had its features processed.
428   std::vector<bool> page_is_processed_;
429
430   // Annotation types that were already counted for this document.
431   std::set<int> annotation_types_counted_;
432
433   bool edit_mode_ = false;
434
435   // The current state of accessibility: either off, enabled but waiting
436   // for the document to load, or fully loaded.
437   enum AccessibilityState {
438     ACCESSIBILITY_STATE_OFF,
439     ACCESSIBILITY_STATE_PENDING,  // Enabled but waiting for doc to load.
440     ACCESSIBILITY_STATE_LOADED
441   } accessibility_state_;
442
443   // True if the plugin is loaded in print preview, otherwise false.
444   bool is_print_preview_;
445
446   // Used for UMA. Do not delete entries, and keep in sync with histograms.xml.
447   enum PdfActionBuckets {
448     PRINT_PREVIEW_SHOWN = 0,
449     ROTATE = 1,
450     SELECT_TEXT = 2,
451     UPDATE_ZOOM = 3,
452     PDFACTION_BUCKET_BOUNDARY,
453   };
454
455   // Array indicating what events have been recorded for print preview metrics.
456   bool preview_action_recorded_[PDFACTION_BUCKET_BOUNDARY];
457
458   DISALLOW_COPY_AND_ASSIGN(OutOfProcessInstance);
459 };
460
461 }  // namespace chrome_pdf
462
463 #endif  // PDF_OUT_OF_PROCESS_INSTANCE_H_