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/dev/selection_dev.h"
24 #include "ppapi/cpp/graphics_2d.h"
25 #include "ppapi/cpp/image_data.h"
26 #include "ppapi/cpp/input_event.h"
27 #include "ppapi/cpp/instance.h"
28 #include "ppapi/cpp/private/find_private.h"
29 #include "ppapi/cpp/private/uma_private.h"
30 #include "ppapi/cpp/url_loader.h"
31 #include "ppapi/utility/completion_callback_factory.h"
37 namespace chrome_pdf
{
39 class OutOfProcessInstance
: public pp::Instance
,
40 public pp::Find_Private
,
41 public pp::Printing_Dev
,
42 public pp::Selection_Dev
,
43 public PaintManager::Client
,
44 public PDFEngine::Client
,
45 public PreviewModeClient::Client
{
47 explicit OutOfProcessInstance(PP_Instance instance
);
48 ~OutOfProcessInstance() override
;
50 // pp::Instance implementation.
51 bool Init(uint32_t argc
, const char* argn
[], const char* argv
[]) override
;
52 void HandleMessage(const pp::Var
& message
) override
;
53 bool HandleInputEvent(const pp::InputEvent
& event
) override
;
54 void DidChangeView(const pp::View
& view
) override
;
56 // pp::Find_Private implementation.
57 bool StartFind(const std::string
& text
, bool case_sensitive
) override
;
58 void SelectFindResult(bool forward
) override
;
59 void StopFind() override
;
61 // pp::PaintManager::Client implementation.
62 void OnPaint(const std::vector
<pp::Rect
>& paint_rects
,
63 std::vector
<PaintManager::ReadyRect
>* ready
,
64 std::vector
<pp::Rect
>* pending
) override
;
66 // pp::Printing_Dev implementation.
67 uint32_t QuerySupportedPrintOutputFormats() override
;
68 int32_t PrintBegin(const PP_PrintSettings_Dev
& print_settings
) override
;
69 pp::Resource
PrintPages(
70 const PP_PrintPageNumberRange_Dev
* page_ranges
,
71 uint32_t page_range_count
) override
;
72 void PrintEnd() override
;
73 bool IsPrintScalingDisabled() override
;
75 // pp::Private implementation.
76 virtual pp::Var
GetLinkAtPosition(const pp::Point
& point
);
77 virtual void GetPrintPresetOptionsFromDocument(
78 PP_PdfPrintPresetOptions_Dev
* options
);
80 // PPP_Selection_Dev implementation.
81 pp::Var
GetSelectedText(bool html
) override
;
83 void FlushCallback(int32_t result
);
84 void DidOpen(int32_t result
);
85 void DidOpenPreview(int32_t result
);
87 // Called when the timer is fired.
88 void OnClientTimerFired(int32_t id
);
90 // Called to print without re-entrancy issues.
91 void OnPrint(int32_t);
93 // PDFEngine::Client implementation.
94 void DocumentSizeUpdated(const pp::Size
& size
) override
;
95 void Invalidate(const pp::Rect
& rect
) override
;
96 void Scroll(const pp::Point
& point
) override
;
97 void ScrollToX(int position
) override
;
98 void ScrollToY(int position
) override
;
99 void ScrollToPage(int page
) override
;
100 void NavigateTo(const std::string
& url
, bool open_in_new_tab
) override
;
101 void UpdateCursor(PP_CursorType_Dev cursor
) override
;
102 void UpdateTickMarks(const std::vector
<pp::Rect
>& tickmarks
) override
;
103 void NotifyNumberOfFindResultsChanged(int total
, bool final_result
) override
;
104 void NotifySelectedFindResultChanged(int current_find_index
) override
;
105 void GetDocumentPassword(
106 pp::CompletionCallbackWithOutput
<pp::Var
> callback
) override
;
107 void Alert(const std::string
& message
) override
;
108 bool Confirm(const std::string
& message
) override
;
109 std::string
Prompt(const std::string
& question
,
110 const std::string
& default_answer
) override
;
111 std::string
GetURL() override
;
112 void Email(const std::string
& to
,
113 const std::string
& cc
,
114 const std::string
& bcc
,
115 const std::string
& subject
,
116 const std::string
& body
) override
;
117 void Print() override
;
118 void SubmitForm(const std::string
& url
,
120 int length
) override
;
121 std::string
ShowFileSelectionDialog() override
;
122 pp::URLLoader
CreateURLLoader() override
;
123 void ScheduleCallback(int id
, int delay_in_ms
) override
;
124 void SearchString(const base::char16
* string
,
125 const base::char16
* term
,
127 std::vector
<SearchStringResult
>* results
) override
;
128 void DocumentPaintOccurred() override
;
129 void DocumentLoadComplete(int page_count
) override
;
130 void DocumentLoadFailed() override
;
131 pp::Instance
* GetPluginInstance() override
;
132 void DocumentHasUnsupportedFeature(const std::string
& feature
) override
;
133 void DocumentLoadProgress(uint32 available
, uint32 doc_size
) override
;
134 void FormTextFieldFocusChange(bool in_focus
) override
;
135 bool IsPrintPreview() override
;
136 uint32
GetBackgroundColor() override
;
137 void IsSelectingChanged(bool is_selecting
) override
;
139 // PreviewModeClient::Client implementation.
140 void PreviewDocumentLoadComplete() override
;
141 void PreviewDocumentLoadFailed() override
;
143 // Helper functions for implementing PPP_PDF.
144 void RotateClockwise();
145 void RotateCounterclockwise();
148 void ResetRecentlySentFindUpdate(int32_t);
150 // Called whenever the plugin geometry changes to update the location of the
151 // background parts, and notifies the pdf engine.
152 void OnGeometryChanged(double old_zoom
, float old_device_scale
);
154 // Figures out the location of any background rectangles (i.e. those that
155 // aren't painted by the PDF engine).
156 void CalculateBackgroundParts();
158 // Computes document width/height in device pixels, based on current zoom and
160 int GetDocumentPixelWidth() const;
161 int GetDocumentPixelHeight() const;
163 // Draws a rectangle with the specified dimensions and color in our buffer.
164 void FillRect(const pp::Rect
& rect
, uint32 color
);
166 void LoadUrl(const std::string
& url
);
167 void LoadPreviewUrl(const std::string
& url
);
168 void LoadUrlInternal(const std::string
& url
, pp::URLLoader
* loader
,
169 void (OutOfProcessInstance::* method
)(int32_t));
171 // Creates a URL loader and allows it to access all urls, i.e. not just the
173 pp::URLLoader
CreateURLLoaderInternal();
175 // Figure out the initial page to display based on #page=N and #nameddest=foo
177 // Returns -1 if there is no valid fragment. The returned value is 0-based,
178 // whereas page=N is 1-based.
179 int GetInitialPage(const std::string
& url
);
181 void FormDidOpen(int32_t result
);
183 std::string
GetLocalizedString(PP_ResourceString id
);
185 void UserMetricsRecordAction(const std::string
& action
);
187 enum DocumentLoadState
{
193 // Set new zoom scale.
194 void SetZoom(double scale
);
196 // Reduces the document to 1 page and appends |print_preview_page_count_|
197 // blank pages to the document for print preview.
198 void AppendBlankPrintPreviewPages();
200 // Process the preview page data information. |src_url| specifies the preview
201 // page data location. The |src_url| is in the format:
202 // chrome://print/id/page_number/print.pdf
203 // |dst_page_index| specifies the blank page index that needs to be replaced
204 // with the new page data.
205 void ProcessPreviewPageInfo(const std::string
& src_url
, int dst_page_index
);
206 // Load the next available preview page into the blank page.
207 void LoadAvailablePreviewPage();
209 // Bound the given scroll offset to the document.
210 pp::FloatPoint
BoundScrollOffsetToDocument(
211 const pp::FloatPoint
& scroll_offset
);
213 pp::ImageData image_data_
;
214 // Used when the plugin is embedded in a page and we have to create the loader
216 pp::CompletionCallbackFactory
<OutOfProcessInstance
> loader_factory_
;
217 pp::URLLoader embed_loader_
;
218 pp::URLLoader embed_preview_loader_
;
220 PP_CursorType_Dev cursor_
; // The current cursor.
222 pp::CompletionCallbackFactory
<OutOfProcessInstance
> timer_factory_
;
224 // Size, in pixels, of plugin rectangle.
225 pp::Size plugin_size_
;
226 // Size, in DIPs, of plugin rectangle.
227 pp::Size plugin_dip_size_
;
228 // Remaining area, in pixels, to render the pdf in after accounting for
229 // horizontal centering.
230 pp::Rect available_area_
;
231 // Size of entire document in pixels (i.e. if each page is 800 pixels high and
232 // there are 10 pages, the height will be 8000).
233 pp::Size document_size_
;
235 double zoom_
; // Current zoom factor.
237 float device_scale_
; // Current device scale factor.
238 // True if the plugin is full-page.
241 PaintManager paint_manager_
;
243 struct BackgroundPart
{
247 std::vector
<BackgroundPart
> background_parts_
;
249 struct PrintSettings
{
255 print_pages_called_
= false;
256 memset(&pepper_print_settings
, 0, sizeof(pepper_print_settings
));
258 // This is set to true when PrintBegin is called and false when PrintEnd is
261 // To know whether this was an actual print operation, so we don't double
262 // count UMA logging.
263 bool print_pages_called_
;
264 PP_PrintSettings_Dev pepper_print_settings
;
267 PrintSettings print_settings_
;
269 scoped_ptr
<PDFEngine
> engine_
;
271 // This engine is used to render the individual preview page data. This is
272 // used only in print preview mode. This will use |PreviewModeClient|
273 // interface which has very limited access to the pp::Instance.
274 scoped_ptr
<PDFEngine
> preview_engine_
;
278 // Used for submitting forms.
279 pp::CompletionCallbackFactory
<OutOfProcessInstance
> form_factory_
;
280 pp::URLLoader form_loader_
;
282 // Used for printing without re-entrancy issues.
283 pp::CompletionCallbackFactory
<OutOfProcessInstance
> print_callback_factory_
;
285 // The callback for receiving the password from the page.
286 scoped_ptr
<pp::CompletionCallbackWithOutput
<pp::Var
> > password_callback_
;
288 // True if we haven't painted the plugin viewport yet.
291 DocumentLoadState document_load_state_
;
292 DocumentLoadState preview_document_load_state_
;
294 // A UMA resource for histogram reporting.
297 // Used so that we only tell the browser once about an unsupported feature, to
298 // avoid the infobar going up more than once.
299 bool told_browser_about_unsupported_feature_
;
301 // Keeps track of which unsupported features we reported, so we avoid spamming
302 // the stats if a feature shows up many times per document.
303 std::set
<std::string
> unsupported_features_reported_
;
305 // Number of pages in print preview mode, 0 if not in print preview mode.
306 int print_preview_page_count_
;
307 std::vector
<int> print_preview_page_numbers_
;
309 // Used to manage loaded print preview page information. A |PreviewPageInfo|
310 // consists of data source url string and the page index in the destination
312 typedef std::pair
<std::string
, int> PreviewPageInfo
;
313 std::queue
<PreviewPageInfo
> preview_pages_info_
;
315 // Used to signal the browser about focus changes to trigger the OSK.
316 // TODO(abodenha@chromium.org) Implement full IME support in the plugin.
317 // http://crbug.com/132565
318 scoped_ptr
<pp::TextInput_Dev
> text_input_
;
320 // The last document load progress value sent to the web page.
321 double last_progress_sent_
;
323 // Whether an update to the number of find results found was sent less than
324 // |kFindResultCooldownMs| milliseconds ago.
325 bool recently_sent_find_update_
;
328 std::vector
<pp::Rect
> tickmarks_
;
330 // Whether the plugin has received a viewport changed message. Nothing should
331 // be painted until this is received.
332 bool received_viewport_message_
;
334 // If true, this means we told the RenderView that we're starting a network
335 // request so that it can start the throbber. We will tell it again once the
336 // document finishes loading.
337 bool did_call_start_loading_
;
339 // If this is true, then don't scroll the plugin in response to DidChangeView
340 // messages. This will be true when the extension page is in the process of
341 // zooming the plugin so that flickering doesn't occur while zooming.
342 bool stop_scrolling_
;
344 // The background color of the PDF viewer.
345 uint32 background_color_
;
348 } // namespace chrome_pdf
350 #endif // PDF_OUT_OF_PROCESS_INSTANCE_H_