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_OUT_OF_PROCESS_INSTANCE_H_
6 #define PDF_OUT_OF_PROCESS_INSTANCE_H_
14 #include "base/memory/scoped_ptr.h"
15 #include "pdf/paint_manager.h"
16 #include "pdf/pdf_engine.h"
17 #include "pdf/preview_mode_client.h"
19 #include "ppapi/c/private/ppb_pdf.h"
20 #include "ppapi/c/private/ppp_pdf.h"
21 #include "ppapi/cpp/dev/printing_dev.h"
22 #include "ppapi/cpp/dev/scriptable_object_deprecated.h"
23 #include "ppapi/cpp/graphics_2d.h"
24 #include "ppapi/cpp/image_data.h"
25 #include "ppapi/cpp/input_event.h"
26 #include "ppapi/cpp/instance.h"
27 #include "ppapi/cpp/private/find_private.h"
28 #include "ppapi/cpp/private/uma_private.h"
29 #include "ppapi/cpp/url_loader.h"
30 #include "ppapi/utility/completion_callback_factory.h"
36 namespace chrome_pdf
{
38 class OutOfProcessInstance
: public pp::Instance
,
39 public pp::Find_Private
,
40 public pp::Printing_Dev
,
41 public PaintManager::Client
,
42 public PDFEngine::Client
,
43 public PreviewModeClient::Client
{
45 explicit OutOfProcessInstance(PP_Instance instance
);
46 ~OutOfProcessInstance() override
;
48 // pp::Instance implementation.
49 bool Init(uint32_t argc
, const char* argn
[], const char* argv
[]) override
;
50 void HandleMessage(const pp::Var
& message
) override
;
51 bool HandleInputEvent(const pp::InputEvent
& event
) override
;
52 void DidChangeView(const pp::View
& view
) override
;
54 // pp::Find_Private implementation.
55 bool StartFind(const std::string
& text
, bool case_sensitive
) override
;
56 void SelectFindResult(bool forward
) override
;
57 void StopFind() override
;
59 // pp::PaintManager::Client implementation.
60 void OnPaint(const std::vector
<pp::Rect
>& paint_rects
,
61 std::vector
<PaintManager::ReadyRect
>* ready
,
62 std::vector
<pp::Rect
>* pending
) override
;
64 // pp::Printing_Dev implementation.
65 uint32_t QuerySupportedPrintOutputFormats() override
;
66 int32_t PrintBegin(const PP_PrintSettings_Dev
& print_settings
) override
;
67 pp::Resource
PrintPages(
68 const PP_PrintPageNumberRange_Dev
* page_ranges
,
69 uint32_t page_range_count
) override
;
70 void PrintEnd() override
;
71 bool IsPrintScalingDisabled() override
;
73 // pp::Private implementation.
74 virtual pp::Var
GetLinkAtPosition(const pp::Point
& point
);
75 virtual void GetPrintPresetOptionsFromDocument(
76 PP_PdfPrintPresetOptions_Dev
* options
);
78 void FlushCallback(int32_t result
);
79 void DidOpen(int32_t result
);
80 void DidOpenPreview(int32_t result
);
82 // Called when the timer is fired.
83 void OnClientTimerFired(int32_t id
);
85 // Called to print without re-entrancy issues.
86 void OnPrint(int32_t);
88 // PDFEngine::Client implementation.
89 void DocumentSizeUpdated(const pp::Size
& size
) override
;
90 void Invalidate(const pp::Rect
& rect
) override
;
91 void Scroll(const pp::Point
& point
) override
;
92 void ScrollToX(int position
) override
;
93 void ScrollToY(int position
) override
;
94 void ScrollToPage(int page
) override
;
95 void NavigateTo(const std::string
& url
, bool open_in_new_tab
) override
;
96 void UpdateCursor(PP_CursorType_Dev cursor
) override
;
97 void UpdateTickMarks(const std::vector
<pp::Rect
>& tickmarks
) override
;
98 void NotifyNumberOfFindResultsChanged(int total
, bool final_result
) override
;
99 void NotifySelectedFindResultChanged(int current_find_index
) override
;
100 void GetDocumentPassword(
101 pp::CompletionCallbackWithOutput
<pp::Var
> callback
) override
;
102 void Alert(const std::string
& message
) override
;
103 bool Confirm(const std::string
& message
) override
;
104 std::string
Prompt(const std::string
& question
,
105 const std::string
& default_answer
) override
;
106 std::string
GetURL() override
;
107 void Email(const std::string
& to
,
108 const std::string
& cc
,
109 const std::string
& bcc
,
110 const std::string
& subject
,
111 const std::string
& body
) override
;
112 void Print() override
;
113 void SubmitForm(const std::string
& url
,
115 int length
) override
;
116 std::string
ShowFileSelectionDialog() override
;
117 pp::URLLoader
CreateURLLoader() override
;
118 void ScheduleCallback(int id
, int delay_in_ms
) override
;
119 void SearchString(const base::char16
* string
,
120 const base::char16
* term
,
122 std::vector
<SearchStringResult
>* results
) override
;
123 void DocumentPaintOccurred() override
;
124 void DocumentLoadComplete(int page_count
) override
;
125 void DocumentLoadFailed() override
;
126 pp::Instance
* GetPluginInstance() override
;
127 void DocumentHasUnsupportedFeature(const std::string
& feature
) override
;
128 void DocumentLoadProgress(uint32 available
, uint32 doc_size
) override
;
129 void FormTextFieldFocusChange(bool in_focus
) override
;
130 bool IsPrintPreview() override
;
131 uint32
GetBackgroundColor() override
;
132 void IsSelectingChanged(bool is_selecting
) override
;
134 // PreviewModeClient::Client implementation.
135 void PreviewDocumentLoadComplete() override
;
136 void PreviewDocumentLoadFailed() override
;
138 // Helper functions for implementing PPP_PDF.
139 void RotateClockwise();
140 void RotateCounterclockwise();
143 void ResetRecentlySentFindUpdate(int32_t);
145 // Called whenever the plugin geometry changes to update the location of the
146 // background parts, and notifies the pdf engine.
147 void OnGeometryChanged(double old_zoom
, float old_device_scale
);
149 // Figures out the location of any background rectangles (i.e. those that
150 // aren't painted by the PDF engine).
151 void CalculateBackgroundParts();
153 // Computes document width/height in device pixels, based on current zoom and
155 int GetDocumentPixelWidth() const;
156 int GetDocumentPixelHeight() const;
158 // Draws a rectangle with the specified dimensions and color in our buffer.
159 void FillRect(const pp::Rect
& rect
, uint32 color
);
161 void LoadUrl(const std::string
& url
);
162 void LoadPreviewUrl(const std::string
& url
);
163 void LoadUrlInternal(const std::string
& url
, pp::URLLoader
* loader
,
164 void (OutOfProcessInstance::* method
)(int32_t));
166 // Creates a URL loader and allows it to access all urls, i.e. not just the
168 pp::URLLoader
CreateURLLoaderInternal();
170 void FormDidOpen(int32_t result
);
172 void UserMetricsRecordAction(const std::string
& action
);
174 enum DocumentLoadState
{
180 // Set new zoom scale.
181 void SetZoom(double scale
);
183 // Reduces the document to 1 page and appends |print_preview_page_count_|
184 // blank pages to the document for print preview.
185 void AppendBlankPrintPreviewPages();
187 // Process the preview page data information. |src_url| specifies the preview
188 // page data location. The |src_url| is in the format:
189 // chrome://print/id/page_number/print.pdf
190 // |dst_page_index| specifies the blank page index that needs to be replaced
191 // with the new page data.
192 void ProcessPreviewPageInfo(const std::string
& src_url
, int dst_page_index
);
193 // Load the next available preview page into the blank page.
194 void LoadAvailablePreviewPage();
196 // Bound the given scroll offset to the document.
197 pp::FloatPoint
BoundScrollOffsetToDocument(
198 const pp::FloatPoint
& scroll_offset
);
200 pp::ImageData image_data_
;
201 // Used when the plugin is embedded in a page and we have to create the loader
203 pp::CompletionCallbackFactory
<OutOfProcessInstance
> loader_factory_
;
204 pp::URLLoader embed_loader_
;
205 pp::URLLoader embed_preview_loader_
;
207 PP_CursorType_Dev cursor_
; // The current cursor.
209 pp::CompletionCallbackFactory
<OutOfProcessInstance
> timer_factory_
;
211 // Size, in pixels, of plugin rectangle.
212 pp::Size plugin_size_
;
213 // Size, in DIPs, of plugin rectangle.
214 pp::Size plugin_dip_size_
;
215 // Remaining area, in pixels, to render the pdf in after accounting for
216 // horizontal centering.
217 pp::Rect available_area_
;
218 // Size of entire document in pixels (i.e. if each page is 800 pixels high and
219 // there are 10 pages, the height will be 8000).
220 pp::Size document_size_
;
222 double zoom_
; // Current zoom factor.
224 float device_scale_
; // Current device scale factor.
225 // True if the plugin is full-page.
228 PaintManager paint_manager_
;
230 struct BackgroundPart
{
234 std::vector
<BackgroundPart
> background_parts_
;
236 struct PrintSettings
{
242 print_pages_called_
= false;
243 memset(&pepper_print_settings
, 0, sizeof(pepper_print_settings
));
245 // This is set to true when PrintBegin is called and false when PrintEnd is
248 // To know whether this was an actual print operation, so we don't double
249 // count UMA logging.
250 bool print_pages_called_
;
251 PP_PrintSettings_Dev pepper_print_settings
;
254 PrintSettings print_settings_
;
256 scoped_ptr
<PDFEngine
> engine_
;
258 // The PreviewModeClient used for print preview. Will be passed to
259 // |preview_engine_|.
260 scoped_ptr
<PreviewModeClient
> preview_client_
;
262 // This engine is used to render the individual preview page data. This is
263 // used only in print preview mode. This will use |PreviewModeClient|
264 // interface which has very limited access to the pp::Instance.
265 scoped_ptr
<PDFEngine
> preview_engine_
;
269 // Used for submitting forms.
270 pp::CompletionCallbackFactory
<OutOfProcessInstance
> form_factory_
;
271 pp::URLLoader form_loader_
;
273 // Used for printing without re-entrancy issues.
274 pp::CompletionCallbackFactory
<OutOfProcessInstance
> print_callback_factory_
;
276 // The callback for receiving the password from the page.
277 scoped_ptr
<pp::CompletionCallbackWithOutput
<pp::Var
> > password_callback_
;
279 // True if we haven't painted the plugin viewport yet.
282 DocumentLoadState document_load_state_
;
283 DocumentLoadState preview_document_load_state_
;
285 // A UMA resource for histogram reporting.
288 // Used so that we only tell the browser once about an unsupported feature, to
289 // avoid the infobar going up more than once.
290 bool told_browser_about_unsupported_feature_
;
292 // Keeps track of which unsupported features we reported, so we avoid spamming
293 // the stats if a feature shows up many times per document.
294 std::set
<std::string
> unsupported_features_reported_
;
296 // Number of pages in print preview mode, 0 if not in print preview mode.
297 int print_preview_page_count_
;
298 std::vector
<int> print_preview_page_numbers_
;
300 // Used to manage loaded print preview page information. A |PreviewPageInfo|
301 // consists of data source url string and the page index in the destination
303 typedef std::pair
<std::string
, int> PreviewPageInfo
;
304 std::queue
<PreviewPageInfo
> preview_pages_info_
;
306 // Used to signal the browser about focus changes to trigger the OSK.
307 // TODO(abodenha@chromium.org) Implement full IME support in the plugin.
308 // http://crbug.com/132565
309 scoped_ptr
<pp::TextInput_Dev
> text_input_
;
311 // The last document load progress value sent to the web page.
312 double last_progress_sent_
;
314 // Whether an update to the number of find results found was sent less than
315 // |kFindResultCooldownMs| milliseconds ago.
316 bool recently_sent_find_update_
;
319 std::vector
<pp::Rect
> tickmarks_
;
321 // Whether the plugin has received a viewport changed message. Nothing should
322 // be painted until this is received.
323 bool received_viewport_message_
;
325 // If true, this means we told the RenderView that we're starting a network
326 // request so that it can start the throbber. We will tell it again once the
327 // document finishes loading.
328 bool did_call_start_loading_
;
330 // If this is true, then don't scroll the plugin in response to DidChangeView
331 // messages. This will be true when the extension page is in the process of
332 // zooming the plugin so that flickering doesn't occur while zooming.
333 bool stop_scrolling_
;
335 // The background color of the PDF viewer.
336 uint32 background_color_
;
338 // The blank space above the first page of the document reserved for the
340 int top_toolbar_height_
;
342 DISALLOW_COPY_AND_ASSIGN(OutOfProcessInstance
);
345 } // namespace chrome_pdf
347 #endif // PDF_OUT_OF_PROCESS_INSTANCE_H_