Fix telemetry_unittests and telemetry_perf_unittests so they are 2 steps.
[chromium-blink-merge.git] / pdf / instance.h
blobfe794723d1dc79aa465f04eabe4ed834529fc617
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_
8 #include <queue>
9 #include <set>
10 #include <string>
11 #include <utility>
12 #include <vector>
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"
41 namespace pp {
42 class TextInput_Dev;
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,
54 public pp::Zoom_Dev,
55 public PaintManager::Client,
56 public PDFEngine::Client,
57 public PreviewModeClient::Client,
58 public ControlOwner {
59 public:
60 explicit Instance(PP_Instance instance);
61 virtual ~Instance();
63 // pp::Instance implementation.
64 virtual bool Init(uint32_t argc,
65 const char* argn[],
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
116 // rect to ready.
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,
160 const void* data,
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,
167 bool case_sensitive,
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,
193 pp::Var* exception);
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 private:
204 // Called whenever the plugin geometry changes to update the location of the
205 // scrollbars, background parts, and notifies the pdf engine.
206 void OnGeometryChanged(double old_zoom, float old_device_scale);
208 // Runs the given JS callback given in |callback|.
209 void RunCallback(int32_t, pp::Var callback);
211 void CreateHorizontalScrollbar();
212 void CreateVerticalScrollbar();
213 void DestroyHorizontalScrollbar();
214 void DestroyVerticalScrollbar();
216 // Returns the thickness of a scrollbar. This returns the thickness when it's
217 // shown, so for overlay scrollbars it'll still be non-zero.
218 int GetScrollbarThickness();
220 // Returns the space we need to reserve for the scrollbar in the plugin area.
221 // If overlay scrollbars are used, this will be 0.
222 int GetScrollbarReservedThickness();
224 // Returns true if overlay scrollbars are in use.
225 bool IsOverlayScrollbar();
227 // Figures out the location of any background rectangles (i.e. those that
228 // aren't painted by the PDF engine).
229 void CalculateBackgroundParts();
231 // Computes document width/height in device pixels, based on current zoom and
232 // device scale
233 int GetDocumentPixelWidth() const;
234 int GetDocumentPixelHeight() const;
236 // Draws a rectangle with the specified dimensions and color in our buffer.
237 void FillRect(const pp::Rect& rect, uint32 color);
239 std::vector<pp::ImageData> GetThumbnailResources();
240 std::vector<pp::ImageData> GetProgressBarResources(pp::ImageData* background);
242 void CreateToolbar(const ToolbarButtonInfo* tb_info, size_t size);
243 int GetToolbarRightOffset();
244 int GetToolbarBottomOffset();
245 void CreateProgressBar();
246 void ConfigureProgressBar();
247 void CreateThumbnails();
248 void CreatePageIndicator(bool always_visible);
249 void ConfigurePageIndicator();
251 void PaintOverlayControl(Control* ctrl,
252 pp::ImageData* image_data,
253 std::vector<PaintManager::ReadyRect>* ready);
255 void LoadUrl(const std::string& url);
256 void LoadPreviewUrl(const std::string& url);
257 void LoadUrlInternal(const std::string& url, pp::URLLoader* loader,
258 void (Instance::* method)(int32_t));
260 // Creates a URL loader and allows it to access all urls, i.e. not just the
261 // frame's origin.
262 pp::URLLoader CreateURLLoaderInternal();
264 // Figure out the initial page to display based on #page=N and #nameddest=foo
265 // in the |url_|.
266 // Returns -1 if there is no valid fragment. The returned value is 0-based,
267 // whereas page=N is 1-based.
268 int GetInitialPage(const std::string& url);
270 void UpdateToolbarPosition(bool invalidate);
271 void UpdateProgressBarPosition(bool invalidate);
272 void UpdatePageIndicatorPosition(bool invalidate);
274 void FormDidOpen(int32_t result);
276 std::string GetLocalizedString(PP_ResourceString id);
278 void UserMetricsRecordAction(const std::string& action);
280 void SaveAs();
282 enum ZoomMode {
283 ZOOM_SCALE, // Standard zooming mode, resize will not affect it.
284 ZOOM_FIT_TO_WIDTH, // Maintain fit to width on resize.
285 ZOOM_FIT_TO_PAGE, // Maintain fit to page on resize.
286 ZOOM_AUTO // Maintain the default auto fitting mode on resize.
289 enum DocumentLoadState {
290 LOAD_STATE_LOADING,
291 LOAD_STATE_COMPLETE,
292 LOAD_STATE_FAILED,
295 // Set new zoom mode and scale. Scale will be ignored if mode != ZOOM_SCALE.
296 void SetZoom(ZoomMode zoom_mode, double scale);
298 // Updates internal zoom scale based on the plugin/document geometry and
299 // current mode.
300 void UpdateZoomScale();
302 // Simulates how Chrome "snaps" zooming up/down to the next nearest zoom level
303 // when the previous zoom level wasn't an integer. We do this so that
304 // pressing the zoom buttons has the same effect as the menu buttons, even if
305 // we start from a non-standard zoom level because of fit-width or fit-height.
306 double CalculateZoom(uint32 control_id) const;
308 pp::ImageData CreateResourceImage(PP_ResourceImage image_id);
310 void DrawText(const pp::Point& top_center, PP_ResourceString id);
312 // Set print preview mode, where the current PDF document is reduced to
313 // only one page, and then extended to |page_count| pages with
314 // |page_count| - 1 blank pages.
315 void SetPrintPreviewMode(int page_count);
317 // Returns the page number to be displayed in the page indicator. If the
318 // plugin is running within print preview, the displayed number might be
319 // different from the index of the displayed page.
320 int GetPageNumberToDisplay();
322 // Process the preview page data information. |src_url| specifies the preview
323 // page data location. The |src_url| is in the format:
324 // chrome://print/id/page_number/print.pdf
325 // |dst_page_index| specifies the blank page index that needs to be replaced
326 // with the new page data.
327 void ProcessPreviewPageInfo(const std::string& src_url, int dst_page_index);
328 // Load the next available preview page into the blank page.
329 void LoadAvailablePreviewPage();
331 // Enables autoscroll using origin as a neutral (center) point.
332 void EnableAutoscroll(const pp::Point& origin);
333 // Disables autoscroll and returns to normal functionality.
334 void DisableAutoscroll();
335 // Calculate autoscroll info and return proper mouse pointer and scroll
336 // andjustments.
337 PP_CursorType_Dev CalculateAutoscroll(const pp::Point& mouse_pos);
339 void ConfigureNumberImageGenerator();
341 NumberImageGenerator* number_image_generator();
343 int GetScaled(int x) const;
345 pp::ImageData image_data_;
346 // Used when the plugin is embedded in a page and we have to create the loader
347 // ourself.
348 pp::CompletionCallbackFactory<Instance> loader_factory_;
349 pp::URLLoader embed_loader_;
350 pp::URLLoader embed_preview_loader_;
352 scoped_ptr<pp::Scrollbar_Dev> h_scrollbar_;
353 scoped_ptr<pp::Scrollbar_Dev> v_scrollbar_;
354 int32 valid_v_range_;
356 PP_CursorType_Dev cursor_; // The current cursor.
358 // Used when selecting and dragging beyond the visible portion, in which case
359 // we want to scroll the document.
360 bool timer_pending_;
361 pp::MouseInputEvent last_mouse_event_;
362 pp::CompletionCallbackFactory<Instance> timer_factory_;
363 uint32 current_timer_id_;
365 // Size, in pixels, of plugin rectangle.
366 pp::Size plugin_size_;
367 // Size, in DIPs, of plugin rectangle.
368 pp::Size plugin_dip_size_;
369 // Remaining area, in pixels, to render the pdf in after accounting for
370 // scrollbars/toolbars and horizontal centering.
371 pp::Rect available_area_;
372 // Size of entire document in pixels (i.e. if each page is 800 pixels high and
373 // there are 10 pages, the height will be 8000).
374 pp::Size document_size_;
376 double zoom_; // Current zoom factor.
378 float device_scale_; // Current device scale factor.
379 bool printing_enabled_;
380 bool hidpi_enabled_;
381 // True if the plugin is full-page.
382 bool full_;
383 // Zooming mode (none, fit to width, fit to height)
384 ZoomMode zoom_mode_;
386 // If true, this means we told the RenderView that we're starting a network
387 // request so that it can start the throbber. We will tell it again once the
388 // document finishes loading.
389 bool did_call_start_loading_;
391 // Hold off on painting invalidated requests while this flag is true.
392 bool dont_paint_;
394 // Indicates if plugin is in autoscroll mode.
395 bool is_autoscroll_;
396 // Rect for autoscroll anchor.
397 pp::Rect autoscroll_rect_;
398 // Image of the autoscroll anchor and its background.
399 pp::ImageData autoscroll_anchor_;
400 // Autoscrolling deltas in pixels.
401 int autoscroll_x_;
402 int autoscroll_y_;
404 // Thickness of a scrollbar.
405 int scrollbar_thickness_;
407 // Reserved thickness of a scrollbar. This is how much space the scrollbar
408 // takes from the available area. 0 for overlay.
409 int scrollbar_reserved_thickness_;
411 // Used to remember which toolbar is in use
412 const ToolbarButtonInfo* current_tb_info_;
413 size_t current_tb_info_size_;
415 PaintManager paint_manager_;
417 struct BackgroundPart {
418 pp::Rect location;
419 uint32 color;
421 std::vector<BackgroundPart> background_parts_;
423 struct PrintSettings {
424 PrintSettings() {
425 Clear();
427 void Clear() {
428 is_printing = false;
429 print_pages_called_ = false;
430 memset(&pepper_print_settings, 0, sizeof(pepper_print_settings));
432 // This is set to true when PrintBegin is called and false when PrintEnd is
433 // called.
434 bool is_printing;
435 // To know whether this was an actual print operation, so we don't double
436 // count UMA logging.
437 bool print_pages_called_;
438 PP_PrintSettings_Dev pepper_print_settings;
441 PrintSettings print_settings_;
443 scoped_ptr<PDFEngine> engine_;
445 // This engine is used to render the individual preview page data. This is
446 // used only in print preview mode. This will use |PreviewModeClient|
447 // interface which has very limited access to the pp::Instance.
448 scoped_ptr<PDFEngine> preview_engine_;
450 std::string url_;
452 scoped_ptr<FadingControls> toolbar_;
453 ThumbnailControl thumbnails_;
454 ProgressControl progress_bar_;
455 uint32 delayed_progress_timer_id_;
456 PageIndicator page_indicator_;
458 // Used for creating images from numbers.
459 scoped_ptr<NumberImageGenerator> number_image_generator_;
461 // Used for submitting forms.
462 pp::CompletionCallbackFactory<Instance> form_factory_;
463 pp::URLLoader form_loader_;
465 // Used for generating callbacks.
466 // TODO(raymes): We don't really need other callback factories we can just
467 // fold them into this one.
468 pp::CompletionCallbackFactory<Instance> callback_factory_;
470 // True if we haven't painted the plugin viewport yet.
471 bool first_paint_;
473 // True when we've painted at least one page from the document.
474 bool painted_first_page_;
476 // True if we should display page indicator, false otherwise
477 bool show_page_indicator_;
479 // Callback when the document load completes.
480 pp::Var on_load_callback_;
481 pp::Var on_scroll_callback_;
482 pp::Var on_plugin_size_changed_callback_;
484 DocumentLoadState document_load_state_;
485 DocumentLoadState preview_document_load_state_;
487 // JavaScript interface to control this instance.
488 // This wraps a PDFScriptableObject in a pp::Var.
489 pp::VarPrivate instance_object_;
491 // Used so that we only tell the browser once about an unsupported feature, to
492 // avoid the infobar going up more than once.
493 bool told_browser_about_unsupported_feature_;
495 // Keeps track of which unsupported features we reported, so we avoid spamming
496 // the stats if a feature shows up many times per document.
497 std::set<std::string> unsupported_features_reported_;
499 // Number of pages in print preview mode, 0 if not in print preview mode.
500 int print_preview_page_count_;
501 std::vector<int> print_preview_page_numbers_;
503 // Used to manage loaded print preview page information. A |PreviewPageInfo|
504 // consists of data source url string and the page index in the destination
505 // document.
506 typedef std::pair<std::string, int> PreviewPageInfo;
507 std::queue<PreviewPageInfo> preview_pages_info_;
509 // Used to signal the browser about focus changes to trigger the OSK.
510 // TODO(abodenha@chromium.org) Implement full IME support in the plugin.
511 // http://crbug.com/132565
512 scoped_ptr<pp::TextInput_Dev> text_input_;
515 // This implements the JavaScript class entrypoint for the plugin instance.
516 // This class is just a thin wrapper. It delegates relevant methods to Instance.
517 class PDFScriptableObject : public pp::deprecated::ScriptableObject {
518 public:
519 explicit PDFScriptableObject(Instance* instance);
520 virtual ~PDFScriptableObject();
522 // pp::deprecated::ScriptableObject implementation.
523 virtual bool HasMethod(const pp::Var& method, pp::Var* exception);
524 virtual pp::Var Call(const pp::Var& method,
525 const std::vector<pp::Var>& args,
526 pp::Var* exception);
528 private:
529 Instance* instance_;
532 } // namespace chrome_pdf
534 #endif // PDF_INSTANCE_H_