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_INSTANCE_H_
6 #define PDF_INSTANCE_H_
14 #include "base/memory/scoped_ptr.h"
15 #include "pdf/button.h"
16 #include "pdf/fading_controls.h"
17 #include "pdf/page_indicator.h"
18 #include "pdf/paint_manager.h"
19 #include "pdf/pdf_engine.h"
20 #include "pdf/preview_mode_client.h"
21 #include "pdf/progress_control.h"
22 #include "pdf/thumbnail_control.h"
24 #include "ppapi/c/private/ppb_pdf.h"
25 #include "ppapi/c/private/ppp_pdf.h"
26 #include "ppapi/cpp/dev/printing_dev.h"
27 #include "ppapi/cpp/dev/scriptable_object_deprecated.h"
28 #include "ppapi/cpp/dev/scrollbar_dev.h"
29 #include "ppapi/cpp/dev/selection_dev.h"
30 #include "ppapi/cpp/dev/widget_client_dev.h"
31 #include "ppapi/cpp/dev/zoom_dev.h"
32 #include "ppapi/cpp/graphics_2d.h"
33 #include "ppapi/cpp/image_data.h"
34 #include "ppapi/cpp/input_event.h"
35 #include "ppapi/cpp/private/find_private.h"
36 #include "ppapi/cpp/private/instance_private.h"
37 #include "ppapi/cpp/private/var_private.h"
38 #include "ppapi/cpp/url_loader.h"
39 #include "ppapi/utility/completion_callback_factory.h"
45 namespace chrome_pdf
{
47 struct ToolbarButtonInfo
;
49 class Instance
: public pp::InstancePrivate
,
50 public pp::Find_Private
,
51 public pp::Printing_Dev
,
52 public pp::Selection_Dev
,
53 public pp::WidgetClient_Dev
,
55 public PaintManager::Client
,
56 public PDFEngine::Client
,
57 public PreviewModeClient::Client
,
60 explicit Instance(PP_Instance instance
);
63 // pp::Instance implementation.
64 bool Init(uint32_t argc
,
66 const char* argv
[]) override
;
67 bool HandleDocumentLoad(const pp::URLLoader
& loader
) override
;
68 bool HandleInputEvent(const pp::InputEvent
& event
) override
;
69 void DidChangeView(const pp::View
& view
) override
;
70 pp::Var
GetInstanceObject() override
;
72 // pp::Find_Private implementation.
73 bool StartFind(const std::string
& text
, bool case_sensitive
) override
;
74 void SelectFindResult(bool forward
) override
;
75 void StopFind() override
;
77 // pp::PaintManager::Client implementation.
78 void OnPaint(const std::vector
<pp::Rect
>& paint_rects
,
79 std::vector
<PaintManager::ReadyRect
>* ready
,
80 std::vector
<pp::Rect
>* pending
) override
;
82 // pp::Printing_Dev implementation.
83 uint32_t QuerySupportedPrintOutputFormats() override
;
85 const PP_PrintSettings_Dev
& print_settings
) override
;
86 pp::Resource
PrintPages(
87 const PP_PrintPageNumberRange_Dev
* page_ranges
,
88 uint32_t page_range_count
) override
;
89 void PrintEnd() override
;
90 bool IsPrintScalingDisabled() override
;
92 // pp::Private implementation.
93 virtual pp::Var
GetLinkAtPosition(const pp::Point
& point
);
94 virtual void GetPrintPresetOptionsFromDocument(
95 PP_PdfPrintPresetOptions_Dev
* options
);
97 // PPP_Selection_Dev implementation.
98 pp::Var
GetSelectedText(bool html
) override
;
100 // WidgetClient_Dev implementation.
101 void InvalidateWidget(pp::Widget_Dev widget
,
102 const pp::Rect
& dirty_rect
) override
;
103 void ScrollbarValueChanged(pp::Scrollbar_Dev scrollbar
,
104 uint32_t value
) override
;
105 void ScrollbarOverlayChanged(pp::Scrollbar_Dev scrollbar
,
106 bool overlay
) override
;
108 // pp::Zoom_Dev implementation.
109 void Zoom(double scale
, bool text_only
) override
;
110 void ZoomChanged(double factor
); // Override.
112 void FlushCallback(int32_t result
);
113 void DidOpen(int32_t result
);
114 void DidOpenPreview(int32_t result
);
115 // If the given widget intersects the rectangle, paints it and adds the
117 void PaintIfWidgetIntersects(pp::Widget_Dev
* widget
,
118 const pp::Rect
& rect
,
119 std::vector
<PaintManager::ReadyRect
>* ready
,
120 std::vector
<pp::Rect
>* pending
);
122 // Called when the timer is fired.
123 void OnTimerFired(int32_t);
124 void OnClientTimerFired(int32_t id
);
126 // Called when the control timer is fired.
127 void OnControlTimerFired(int32_t,
128 const uint32
& control_id
,
129 const uint32
& timer_id
);
131 // Called to print without re-entrancy issues.
132 void OnPrint(int32_t);
134 // PDFEngine::Client implementation.
135 void DocumentSizeUpdated(const pp::Size
& size
) override
;
136 void Invalidate(const pp::Rect
& rect
) override
;
137 void Scroll(const pp::Point
& point
) override
;
138 void ScrollToX(int position
) override
;
139 void ScrollToY(int position
) override
;
140 void ScrollToPage(int page
) override
;
141 void NavigateTo(const std::string
& url
, bool open_in_new_tab
) override
;
142 void UpdateCursor(PP_CursorType_Dev cursor
) override
;
143 void UpdateTickMarks(const std::vector
<pp::Rect
>& tickmarks
) override
;
144 void NotifyNumberOfFindResultsChanged(int total
, bool final_result
) override
;
145 void NotifySelectedFindResultChanged(int current_find_index
) override
;
146 void GetDocumentPassword(
147 pp::CompletionCallbackWithOutput
<pp::Var
> callback
) override
;
148 void Alert(const std::string
& message
) override
;
149 bool Confirm(const std::string
& message
) override
;
150 std::string
Prompt(const std::string
& question
,
151 const std::string
& default_answer
) override
;
152 std::string
GetURL() override
;
153 void Email(const std::string
& to
,
154 const std::string
& cc
,
155 const std::string
& bcc
,
156 const std::string
& subject
,
157 const std::string
& body
) override
;
158 void Print() override
;
159 void SubmitForm(const std::string
& url
,
161 int length
) override
;
162 std::string
ShowFileSelectionDialog();
163 pp::URLLoader
CreateURLLoader() override
;
164 void ScheduleCallback(int id
, int delay_in_ms
) override
;
165 void SearchString(const base::char16
* string
,
166 const base::char16
* term
,
168 std::vector
<SearchStringResult
>* results
) override
;
169 void DocumentPaintOccurred() override
;
170 void DocumentLoadComplete(int page_count
) override
;
171 void DocumentLoadFailed() override
;
172 pp::Instance
* GetPluginInstance() override
;
173 void DocumentHasUnsupportedFeature(const std::string
& feature
) override
;
174 void DocumentLoadProgress(uint32 available
, uint32 doc_size
) override
;
175 void FormTextFieldFocusChange(bool in_focus
) override
;
176 bool IsPrintPreview() override
;
177 uint32
GetBackgroundColor() override
;
179 // ControlOwner implementation.
180 void OnEvent(uint32 control_id
, uint32 event_id
, void* data
) override
;
181 void Invalidate(uint32 control_id
, const pp::Rect
& rc
) override
;
182 uint32
ScheduleTimer(uint32 control_id
, uint32 timeout_ms
) override
;
183 void SetEventCapture(uint32 control_id
, bool set_capture
) override
;
184 void SetCursor(uint32 control_id
, PP_CursorType_Dev cursor_type
) override
;
185 pp::Instance
* GetInstance() override
;
187 bool dont_paint() const { return dont_paint_
; }
188 void set_dont_paint(bool dont_paint
) { dont_paint_
= dont_paint
; }
190 // Called by PDFScriptableObject.
191 bool HasScriptableMethod(const pp::Var
& method
, pp::Var
* exception
);
192 pp::Var
CallScriptableMethod(const pp::Var
& method
,
193 const std::vector
<pp::Var
>& args
,
196 // PreviewModeClient::Client implementation.
197 void PreviewDocumentLoadComplete() override
;
198 void PreviewDocumentLoadFailed() override
;
200 // Helper functions for implementing PPP_PDF.
201 void RotateClockwise();
202 void RotateCounterclockwise();
204 // Helper function to inform when mouse pointer is on scrollbar, returns true
205 // when mouse is on vertical or horizaontal scrollbar else returns false.
206 bool IsMouseOnScrollbar(const pp::InputEvent
& event
);
209 // Called whenever the plugin geometry changes to update the location of the
210 // scrollbars, background parts, and notifies the pdf engine.
211 void OnGeometryChanged(double old_zoom
, float old_device_scale
);
213 // Runs the given JS callback given in |callback|.
214 void RunCallback(int32_t, pp::Var callback
);
216 void CreateHorizontalScrollbar();
217 void CreateVerticalScrollbar();
218 void DestroyHorizontalScrollbar();
219 void DestroyVerticalScrollbar();
221 // Returns the thickness of a scrollbar. This returns the thickness when it's
222 // shown, so for overlay scrollbars it'll still be non-zero.
223 int GetScrollbarThickness();
225 // Returns the space we need to reserve for the scrollbar in the plugin area.
226 // If overlay scrollbars are used, this will be 0.
227 int GetScrollbarReservedThickness();
229 // Returns true if overlay scrollbars are in use.
230 bool IsOverlayScrollbar();
232 // Figures out the location of any background rectangles (i.e. those that
233 // aren't painted by the PDF engine).
234 void CalculateBackgroundParts();
236 // Computes document width/height in device pixels, based on current zoom and
238 int GetDocumentPixelWidth() const;
239 int GetDocumentPixelHeight() const;
241 // Draws a rectangle with the specified dimensions and color in our buffer.
242 void FillRect(const pp::Rect
& rect
, uint32 color
);
244 std::vector
<pp::ImageData
> GetThumbnailResources();
245 std::vector
<pp::ImageData
> GetProgressBarResources(pp::ImageData
* background
);
247 void CreateToolbar(const ToolbarButtonInfo
* tb_info
, size_t size
);
248 int GetToolbarRightOffset();
249 int GetToolbarBottomOffset();
250 void CreateProgressBar();
251 void ConfigureProgressBar();
252 void CreateThumbnails();
253 void CreatePageIndicator(bool always_visible
);
254 void ConfigurePageIndicator();
256 void PaintOverlayControl(Control
* ctrl
,
257 pp::ImageData
* image_data
,
258 std::vector
<PaintManager::ReadyRect
>* ready
);
260 void LoadUrl(const std::string
& url
);
261 void LoadPreviewUrl(const std::string
& url
);
262 void LoadUrlInternal(const std::string
& url
, pp::URLLoader
* loader
,
263 void (Instance::* method
)(int32_t));
265 // Creates a URL loader and allows it to access all urls, i.e. not just the
267 pp::URLLoader
CreateURLLoaderInternal();
269 // Figure out the initial page to display based on #page=N and #nameddest=foo
271 // Returns -1 if there is no valid fragment. The returned value is 0-based,
272 // whereas page=N is 1-based.
273 int GetInitialPage(const std::string
& url
);
275 void UpdateToolbarPosition(bool invalidate
);
276 void UpdateProgressBarPosition(bool invalidate
);
277 void UpdatePageIndicatorPosition(bool invalidate
);
279 void FormDidOpen(int32_t result
);
281 std::string
GetLocalizedString(PP_ResourceString id
);
283 void UserMetricsRecordAction(const std::string
& action
);
288 ZOOM_SCALE
, // Standard zooming mode, resize will not affect it.
289 ZOOM_FIT_TO_WIDTH
, // Maintain fit to width on resize.
290 ZOOM_FIT_TO_PAGE
, // Maintain fit to page on resize.
291 ZOOM_AUTO
// Maintain the default auto fitting mode on resize.
294 enum DocumentLoadState
{
300 // Set new zoom mode and scale. Scale will be ignored if mode != ZOOM_SCALE.
301 void SetZoom(ZoomMode zoom_mode
, double scale
);
303 // Updates internal zoom scale based on the plugin/document geometry and
305 void UpdateZoomScale();
307 // Simulates how Chrome "snaps" zooming up/down to the next nearest zoom level
308 // when the previous zoom level wasn't an integer. We do this so that
309 // pressing the zoom buttons has the same effect as the menu buttons, even if
310 // we start from a non-standard zoom level because of fit-width or fit-height.
311 double CalculateZoom(uint32 control_id
) const;
313 pp::ImageData
CreateResourceImage(PP_ResourceImage image_id
);
315 void DrawText(const pp::Point
& top_center
, PP_ResourceString id
);
317 // Set print preview mode, where the current PDF document is reduced to
318 // only one page, and then extended to |page_count| pages with
319 // |page_count| - 1 blank pages.
320 void SetPrintPreviewMode(int page_count
);
322 // Returns the page number to be displayed in the page indicator. If the
323 // plugin is running within print preview, the displayed number might be
324 // different from the index of the displayed page.
325 int GetPageNumberToDisplay();
327 // Process the preview page data information. |src_url| specifies the preview
328 // page data location. The |src_url| is in the format:
329 // chrome://print/id/page_number/print.pdf
330 // |dst_page_index| specifies the blank page index that needs to be replaced
331 // with the new page data.
332 void ProcessPreviewPageInfo(const std::string
& src_url
, int dst_page_index
);
333 // Load the next available preview page into the blank page.
334 void LoadAvailablePreviewPage();
336 // Enables autoscroll using origin as a neutral (center) point.
337 void EnableAutoscroll(const pp::Point
& origin
);
338 // Disables autoscroll and returns to normal functionality.
339 void DisableAutoscroll();
340 // Calculate autoscroll info and return proper mouse pointer and scroll
342 PP_CursorType_Dev
CalculateAutoscroll(const pp::Point
& mouse_pos
);
344 void ConfigureNumberImageGenerator();
346 NumberImageGenerator
* number_image_generator();
348 int GetScaled(int x
) const;
350 pp::ImageData image_data_
;
351 // Used when the plugin is embedded in a page and we have to create the loader
353 pp::CompletionCallbackFactory
<Instance
> loader_factory_
;
354 pp::URLLoader embed_loader_
;
355 pp::URLLoader embed_preview_loader_
;
357 scoped_ptr
<pp::Scrollbar_Dev
> h_scrollbar_
;
358 scoped_ptr
<pp::Scrollbar_Dev
> v_scrollbar_
;
359 int32 valid_v_range_
;
361 PP_CursorType_Dev cursor_
; // The current cursor.
363 // Used when selecting and dragging beyond the visible portion, in which case
364 // we want to scroll the document.
366 pp::MouseInputEvent last_mouse_event_
;
367 pp::CompletionCallbackFactory
<Instance
> timer_factory_
;
368 uint32 current_timer_id_
;
370 // Size, in pixels, of plugin rectangle.
371 pp::Size plugin_size_
;
372 // Size, in DIPs, of plugin rectangle.
373 pp::Size plugin_dip_size_
;
374 // Remaining area, in pixels, to render the pdf in after accounting for
375 // scrollbars/toolbars and horizontal centering.
376 pp::Rect available_area_
;
377 // Size of entire document in pixels (i.e. if each page is 800 pixels high and
378 // there are 10 pages, the height will be 8000).
379 pp::Size document_size_
;
381 double zoom_
; // Current zoom factor.
383 float device_scale_
; // Current device scale factor.
384 bool printing_enabled_
;
386 // True if the plugin is full-page.
388 // Zooming mode (none, fit to width, fit to height)
391 // If true, this means we told the RenderView that we're starting a network
392 // request so that it can start the throbber. We will tell it again once the
393 // document finishes loading.
394 bool did_call_start_loading_
;
396 // Hold off on painting invalidated requests while this flag is true.
399 // Indicates if plugin is in autoscroll mode.
401 // Rect for autoscroll anchor.
402 pp::Rect autoscroll_rect_
;
403 // Image of the autoscroll anchor and its background.
404 pp::ImageData autoscroll_anchor_
;
405 // Autoscrolling deltas in pixels.
409 // Thickness of a scrollbar.
410 int scrollbar_thickness_
;
412 // Reserved thickness of a scrollbar. This is how much space the scrollbar
413 // takes from the available area. 0 for overlay.
414 int scrollbar_reserved_thickness_
;
416 // Used to remember which toolbar is in use
417 const ToolbarButtonInfo
* current_tb_info_
;
418 size_t current_tb_info_size_
;
420 PaintManager paint_manager_
;
422 struct BackgroundPart
{
426 std::vector
<BackgroundPart
> background_parts_
;
428 struct PrintSettings
{
434 print_pages_called_
= false;
435 memset(&pepper_print_settings
, 0, sizeof(pepper_print_settings
));
437 // This is set to true when PrintBegin is called and false when PrintEnd is
440 // To know whether this was an actual print operation, so we don't double
441 // count UMA logging.
442 bool print_pages_called_
;
443 PP_PrintSettings_Dev pepper_print_settings
;
446 PrintSettings print_settings_
;
448 scoped_ptr
<PDFEngine
> engine_
;
450 // This engine is used to render the individual preview page data. This is
451 // used only in print preview mode. This will use |PreviewModeClient|
452 // interface which has very limited access to the pp::Instance.
453 scoped_ptr
<PDFEngine
> preview_engine_
;
457 scoped_ptr
<FadingControls
> toolbar_
;
458 ThumbnailControl thumbnails_
;
459 ProgressControl progress_bar_
;
460 uint32 delayed_progress_timer_id_
;
461 PageIndicator page_indicator_
;
463 // Used for creating images from numbers.
464 scoped_ptr
<NumberImageGenerator
> number_image_generator_
;
466 // Used for submitting forms.
467 pp::CompletionCallbackFactory
<Instance
> form_factory_
;
468 pp::URLLoader form_loader_
;
470 // Used for generating callbacks.
471 // TODO(raymes): We don't really need other callback factories we can just
472 // fold them into this one.
473 pp::CompletionCallbackFactory
<Instance
> callback_factory_
;
475 // True if we haven't painted the plugin viewport yet.
478 // True when we've painted at least one page from the document.
479 bool painted_first_page_
;
481 // True if we should display page indicator, false otherwise
482 bool show_page_indicator_
;
484 // Callback when the document load completes.
485 pp::Var on_load_callback_
;
486 pp::Var on_scroll_callback_
;
487 pp::Var on_plugin_size_changed_callback_
;
489 DocumentLoadState document_load_state_
;
490 DocumentLoadState preview_document_load_state_
;
492 // JavaScript interface to control this instance.
493 // This wraps a PDFScriptableObject in a pp::Var.
494 pp::VarPrivate instance_object_
;
496 // Used so that we only tell the browser once about an unsupported feature, to
497 // avoid the infobar going up more than once.
498 bool told_browser_about_unsupported_feature_
;
500 // Keeps track of which unsupported features we reported, so we avoid spamming
501 // the stats if a feature shows up many times per document.
502 std::set
<std::string
> unsupported_features_reported_
;
504 // Number of pages in print preview mode, 0 if not in print preview mode.
505 int print_preview_page_count_
;
506 std::vector
<int> print_preview_page_numbers_
;
508 // Used to manage loaded print preview page information. A |PreviewPageInfo|
509 // consists of data source url string and the page index in the destination
511 typedef std::pair
<std::string
, int> PreviewPageInfo
;
512 std::queue
<PreviewPageInfo
> preview_pages_info_
;
514 // Used to signal the browser about focus changes to trigger the OSK.
515 // TODO(abodenha@chromium.org) Implement full IME support in the plugin.
516 // http://crbug.com/132565
517 scoped_ptr
<pp::TextInput_Dev
> text_input_
;
520 // This implements the JavaScript class entrypoint for the plugin instance.
521 // This class is just a thin wrapper. It delegates relevant methods to Instance.
522 class PDFScriptableObject
: public pp::deprecated::ScriptableObject
{
524 explicit PDFScriptableObject(Instance
* instance
);
525 virtual ~PDFScriptableObject();
527 // pp::deprecated::ScriptableObject implementation.
528 virtual bool HasMethod(const pp::Var
& method
, pp::Var
* exception
);
529 virtual pp::Var
Call(const pp::Var
& method
,
530 const std::vector
<pp::Var
>& args
,
537 } // namespace chrome_pdf
539 #endif // PDF_INSTANCE_H_