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 virtual bool Init(uint32_t argc
,
66 const char* argv
[]) override
;
67 virtual bool HandleDocumentLoad(const pp::URLLoader
& loader
) override
;
68 virtual bool HandleInputEvent(const pp::InputEvent
& event
) override
;
69 virtual void DidChangeView(const pp::View
& view
) override
;
70 virtual pp::Var
GetInstanceObject() override
;
72 // pp::Find_Private implementation.
73 virtual bool StartFind(const std::string
& text
, bool case_sensitive
) override
;
74 virtual void SelectFindResult(bool forward
) override
;
75 virtual void StopFind() override
;
77 // pp::PaintManager::Client implementation.
78 virtual 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 virtual uint32_t QuerySupportedPrintOutputFormats() override
;
84 virtual int32_t PrintBegin(
85 const PP_PrintSettings_Dev
& print_settings
) override
;
86 virtual pp::Resource
PrintPages(
87 const PP_PrintPageNumberRange_Dev
* page_ranges
,
88 uint32_t page_range_count
) override
;
89 virtual void PrintEnd() override
;
90 virtual 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 virtual pp::Var
GetSelectedText(bool html
) override
;
100 // WidgetClient_Dev implementation.
101 virtual void InvalidateWidget(pp::Widget_Dev widget
,
102 const pp::Rect
& dirty_rect
) override
;
103 virtual void ScrollbarValueChanged(pp::Scrollbar_Dev scrollbar
,
104 uint32_t value
) override
;
105 virtual void ScrollbarOverlayChanged(pp::Scrollbar_Dev scrollbar
,
106 bool overlay
) override
;
108 // pp::Zoom_Dev implementation.
109 virtual 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
;
178 // ControlOwner implementation.
179 void OnEvent(uint32 control_id
, uint32 event_id
, void* data
) override
;
180 void Invalidate(uint32 control_id
, const pp::Rect
& rc
) override
;
181 uint32
ScheduleTimer(uint32 control_id
, uint32 timeout_ms
) override
;
182 void SetEventCapture(uint32 control_id
, bool set_capture
) override
;
183 void SetCursor(uint32 control_id
, PP_CursorType_Dev cursor_type
) override
;
184 pp::Instance
* GetInstance() override
;
186 bool dont_paint() const { return dont_paint_
; }
187 void set_dont_paint(bool dont_paint
) { dont_paint_
= dont_paint
; }
189 // Called by PDFScriptableObject.
190 bool HasScriptableMethod(const pp::Var
& method
, pp::Var
* exception
);
191 pp::Var
CallScriptableMethod(const pp::Var
& method
,
192 const std::vector
<pp::Var
>& args
,
195 // PreviewModeClient::Client implementation.
196 virtual void PreviewDocumentLoadComplete() override
;
197 virtual void PreviewDocumentLoadFailed() override
;
199 // Helper functions for implementing PPP_PDF.
200 void RotateClockwise();
201 void RotateCounterclockwise();
203 // Helper function to inform when mouse pointer is on scrollbar, returns true
204 // when mouse is on vertical or horizaontal scrollbar else returns false.
205 bool IsMouseOnScrollbar(const pp::InputEvent
& event
);
208 // Called whenever the plugin geometry changes to update the location of the
209 // scrollbars, background parts, and notifies the pdf engine.
210 void OnGeometryChanged(double old_zoom
, float old_device_scale
);
212 // Runs the given JS callback given in |callback|.
213 void RunCallback(int32_t, pp::Var callback
);
215 void CreateHorizontalScrollbar();
216 void CreateVerticalScrollbar();
217 void DestroyHorizontalScrollbar();
218 void DestroyVerticalScrollbar();
220 // Returns the thickness of a scrollbar. This returns the thickness when it's
221 // shown, so for overlay scrollbars it'll still be non-zero.
222 int GetScrollbarThickness();
224 // Returns the space we need to reserve for the scrollbar in the plugin area.
225 // If overlay scrollbars are used, this will be 0.
226 int GetScrollbarReservedThickness();
228 // Returns true if overlay scrollbars are in use.
229 bool IsOverlayScrollbar();
231 // Figures out the location of any background rectangles (i.e. those that
232 // aren't painted by the PDF engine).
233 void CalculateBackgroundParts();
235 // Computes document width/height in device pixels, based on current zoom and
237 int GetDocumentPixelWidth() const;
238 int GetDocumentPixelHeight() const;
240 // Draws a rectangle with the specified dimensions and color in our buffer.
241 void FillRect(const pp::Rect
& rect
, uint32 color
);
243 std::vector
<pp::ImageData
> GetThumbnailResources();
244 std::vector
<pp::ImageData
> GetProgressBarResources(pp::ImageData
* background
);
246 void CreateToolbar(const ToolbarButtonInfo
* tb_info
, size_t size
);
247 int GetToolbarRightOffset();
248 int GetToolbarBottomOffset();
249 void CreateProgressBar();
250 void ConfigureProgressBar();
251 void CreateThumbnails();
252 void CreatePageIndicator(bool always_visible
);
253 void ConfigurePageIndicator();
255 void PaintOverlayControl(Control
* ctrl
,
256 pp::ImageData
* image_data
,
257 std::vector
<PaintManager::ReadyRect
>* ready
);
259 void LoadUrl(const std::string
& url
);
260 void LoadPreviewUrl(const std::string
& url
);
261 void LoadUrlInternal(const std::string
& url
, pp::URLLoader
* loader
,
262 void (Instance::* method
)(int32_t));
264 // Creates a URL loader and allows it to access all urls, i.e. not just the
266 pp::URLLoader
CreateURLLoaderInternal();
268 // Figure out the initial page to display based on #page=N and #nameddest=foo
270 // Returns -1 if there is no valid fragment. The returned value is 0-based,
271 // whereas page=N is 1-based.
272 int GetInitialPage(const std::string
& url
);
274 void UpdateToolbarPosition(bool invalidate
);
275 void UpdateProgressBarPosition(bool invalidate
);
276 void UpdatePageIndicatorPosition(bool invalidate
);
278 void FormDidOpen(int32_t result
);
280 std::string
GetLocalizedString(PP_ResourceString id
);
282 void UserMetricsRecordAction(const std::string
& action
);
287 ZOOM_SCALE
, // Standard zooming mode, resize will not affect it.
288 ZOOM_FIT_TO_WIDTH
, // Maintain fit to width on resize.
289 ZOOM_FIT_TO_PAGE
, // Maintain fit to page on resize.
290 ZOOM_AUTO
// Maintain the default auto fitting mode on resize.
293 enum DocumentLoadState
{
299 // Set new zoom mode and scale. Scale will be ignored if mode != ZOOM_SCALE.
300 void SetZoom(ZoomMode zoom_mode
, double scale
);
302 // Updates internal zoom scale based on the plugin/document geometry and
304 void UpdateZoomScale();
306 // Simulates how Chrome "snaps" zooming up/down to the next nearest zoom level
307 // when the previous zoom level wasn't an integer. We do this so that
308 // pressing the zoom buttons has the same effect as the menu buttons, even if
309 // we start from a non-standard zoom level because of fit-width or fit-height.
310 double CalculateZoom(uint32 control_id
) const;
312 pp::ImageData
CreateResourceImage(PP_ResourceImage image_id
);
314 void DrawText(const pp::Point
& top_center
, PP_ResourceString id
);
316 // Set print preview mode, where the current PDF document is reduced to
317 // only one page, and then extended to |page_count| pages with
318 // |page_count| - 1 blank pages.
319 void SetPrintPreviewMode(int page_count
);
321 // Returns the page number to be displayed in the page indicator. If the
322 // plugin is running within print preview, the displayed number might be
323 // different from the index of the displayed page.
324 int GetPageNumberToDisplay();
326 // Process the preview page data information. |src_url| specifies the preview
327 // page data location. The |src_url| is in the format:
328 // chrome://print/id/page_number/print.pdf
329 // |dst_page_index| specifies the blank page index that needs to be replaced
330 // with the new page data.
331 void ProcessPreviewPageInfo(const std::string
& src_url
, int dst_page_index
);
332 // Load the next available preview page into the blank page.
333 void LoadAvailablePreviewPage();
335 // Enables autoscroll using origin as a neutral (center) point.
336 void EnableAutoscroll(const pp::Point
& origin
);
337 // Disables autoscroll and returns to normal functionality.
338 void DisableAutoscroll();
339 // Calculate autoscroll info and return proper mouse pointer and scroll
341 PP_CursorType_Dev
CalculateAutoscroll(const pp::Point
& mouse_pos
);
343 void ConfigureNumberImageGenerator();
345 NumberImageGenerator
* number_image_generator();
347 int GetScaled(int x
) const;
349 pp::ImageData image_data_
;
350 // Used when the plugin is embedded in a page and we have to create the loader
352 pp::CompletionCallbackFactory
<Instance
> loader_factory_
;
353 pp::URLLoader embed_loader_
;
354 pp::URLLoader embed_preview_loader_
;
356 scoped_ptr
<pp::Scrollbar_Dev
> h_scrollbar_
;
357 scoped_ptr
<pp::Scrollbar_Dev
> v_scrollbar_
;
358 int32 valid_v_range_
;
360 PP_CursorType_Dev cursor_
; // The current cursor.
362 // Used when selecting and dragging beyond the visible portion, in which case
363 // we want to scroll the document.
365 pp::MouseInputEvent last_mouse_event_
;
366 pp::CompletionCallbackFactory
<Instance
> timer_factory_
;
367 uint32 current_timer_id_
;
369 // Size, in pixels, of plugin rectangle.
370 pp::Size plugin_size_
;
371 // Size, in DIPs, of plugin rectangle.
372 pp::Size plugin_dip_size_
;
373 // Remaining area, in pixels, to render the pdf in after accounting for
374 // scrollbars/toolbars and horizontal centering.
375 pp::Rect available_area_
;
376 // Size of entire document in pixels (i.e. if each page is 800 pixels high and
377 // there are 10 pages, the height will be 8000).
378 pp::Size document_size_
;
380 double zoom_
; // Current zoom factor.
382 float device_scale_
; // Current device scale factor.
383 bool printing_enabled_
;
385 // True if the plugin is full-page.
387 // Zooming mode (none, fit to width, fit to height)
390 // If true, this means we told the RenderView that we're starting a network
391 // request so that it can start the throbber. We will tell it again once the
392 // document finishes loading.
393 bool did_call_start_loading_
;
395 // Hold off on painting invalidated requests while this flag is true.
398 // Indicates if plugin is in autoscroll mode.
400 // Rect for autoscroll anchor.
401 pp::Rect autoscroll_rect_
;
402 // Image of the autoscroll anchor and its background.
403 pp::ImageData autoscroll_anchor_
;
404 // Autoscrolling deltas in pixels.
408 // Thickness of a scrollbar.
409 int scrollbar_thickness_
;
411 // Reserved thickness of a scrollbar. This is how much space the scrollbar
412 // takes from the available area. 0 for overlay.
413 int scrollbar_reserved_thickness_
;
415 // Used to remember which toolbar is in use
416 const ToolbarButtonInfo
* current_tb_info_
;
417 size_t current_tb_info_size_
;
419 PaintManager paint_manager_
;
421 struct BackgroundPart
{
425 std::vector
<BackgroundPart
> background_parts_
;
427 struct PrintSettings
{
433 print_pages_called_
= false;
434 memset(&pepper_print_settings
, 0, sizeof(pepper_print_settings
));
436 // This is set to true when PrintBegin is called and false when PrintEnd is
439 // To know whether this was an actual print operation, so we don't double
440 // count UMA logging.
441 bool print_pages_called_
;
442 PP_PrintSettings_Dev pepper_print_settings
;
445 PrintSettings print_settings_
;
447 scoped_ptr
<PDFEngine
> engine_
;
449 // This engine is used to render the individual preview page data. This is
450 // used only in print preview mode. This will use |PreviewModeClient|
451 // interface which has very limited access to the pp::Instance.
452 scoped_ptr
<PDFEngine
> preview_engine_
;
456 scoped_ptr
<FadingControls
> toolbar_
;
457 ThumbnailControl thumbnails_
;
458 ProgressControl progress_bar_
;
459 uint32 delayed_progress_timer_id_
;
460 PageIndicator page_indicator_
;
462 // Used for creating images from numbers.
463 scoped_ptr
<NumberImageGenerator
> number_image_generator_
;
465 // Used for submitting forms.
466 pp::CompletionCallbackFactory
<Instance
> form_factory_
;
467 pp::URLLoader form_loader_
;
469 // Used for generating callbacks.
470 // TODO(raymes): We don't really need other callback factories we can just
471 // fold them into this one.
472 pp::CompletionCallbackFactory
<Instance
> callback_factory_
;
474 // True if we haven't painted the plugin viewport yet.
477 // True when we've painted at least one page from the document.
478 bool painted_first_page_
;
480 // True if we should display page indicator, false otherwise
481 bool show_page_indicator_
;
483 // Callback when the document load completes.
484 pp::Var on_load_callback_
;
485 pp::Var on_scroll_callback_
;
486 pp::Var on_plugin_size_changed_callback_
;
488 DocumentLoadState document_load_state_
;
489 DocumentLoadState preview_document_load_state_
;
491 // JavaScript interface to control this instance.
492 // This wraps a PDFScriptableObject in a pp::Var.
493 pp::VarPrivate instance_object_
;
495 // Used so that we only tell the browser once about an unsupported feature, to
496 // avoid the infobar going up more than once.
497 bool told_browser_about_unsupported_feature_
;
499 // Keeps track of which unsupported features we reported, so we avoid spamming
500 // the stats if a feature shows up many times per document.
501 std::set
<std::string
> unsupported_features_reported_
;
503 // Number of pages in print preview mode, 0 if not in print preview mode.
504 int print_preview_page_count_
;
505 std::vector
<int> print_preview_page_numbers_
;
507 // Used to manage loaded print preview page information. A |PreviewPageInfo|
508 // consists of data source url string and the page index in the destination
510 typedef std::pair
<std::string
, int> PreviewPageInfo
;
511 std::queue
<PreviewPageInfo
> preview_pages_info_
;
513 // Used to signal the browser about focus changes to trigger the OSK.
514 // TODO(abodenha@chromium.org) Implement full IME support in the plugin.
515 // http://crbug.com/132565
516 scoped_ptr
<pp::TextInput_Dev
> text_input_
;
519 // This implements the JavaScript class entrypoint for the plugin instance.
520 // This class is just a thin wrapper. It delegates relevant methods to Instance.
521 class PDFScriptableObject
: public pp::deprecated::ScriptableObject
{
523 explicit PDFScriptableObject(Instance
* instance
);
524 virtual ~PDFScriptableObject();
526 // pp::deprecated::ScriptableObject implementation.
527 virtual bool HasMethod(const pp::Var
& method
, pp::Var
* exception
);
528 virtual pp::Var
Call(const pp::Var
& method
,
529 const std::vector
<pp::Var
>& args
,
536 } // namespace chrome_pdf
538 #endif // PDF_INSTANCE_H_