OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef CHROME_RENDERER_PRINTING_PRINT_WEB_VIEW_HELPER_H_ | |
6 #define CHROME_RENDERER_PRINTING_PRINT_WEB_VIEW_HELPER_H_ | |
7 | |
8 #include <vector> | |
9 | |
10 #include "base/callback.h" | |
11 #include "base/gtest_prod_util.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/memory/shared_memory.h" | |
14 #include "base/memory/weak_ptr.h" | |
15 #include "base/time/time.h" | |
16 #include "content/public/renderer/render_view_observer.h" | |
17 #include "content/public/renderer/render_view_observer_tracker.h" | |
18 #include "printing/pdf_metafile_skia.h" | |
19 #include "third_party/WebKit/public/platform/WebCanvas.h" | |
20 #include "third_party/WebKit/public/web/WebNode.h" | |
21 #include "third_party/WebKit/public/web/WebPrintParams.h" | |
22 #include "ui/gfx/geometry/size.h" | |
23 | |
24 struct PrintMsg_Print_Params; | |
25 struct PrintMsg_PrintPage_Params; | |
26 struct PrintMsg_PrintPages_Params; | |
27 struct PrintHostMsg_SetOptionsFromDocument_Params; | |
28 | |
29 namespace base { | |
30 class DictionaryValue; | |
31 } | |
32 | |
33 namespace blink { | |
34 class WebFrame; | |
35 class WebView; | |
36 } | |
37 | |
38 namespace printing { | |
39 | |
40 struct PageSizeMargins; | |
41 class PrepareFrameAndViewForPrint; | |
42 | |
43 // Stores reference to frame using WebVew and unique name. | |
44 // Workaround to modal dialog issue on Linux. crbug.com/236147. | |
45 // If WebFrame someday supports WeakPtr, we should use it here. | |
46 class FrameReference { | |
47 public: | |
48 explicit FrameReference(blink::WebLocalFrame* frame); | |
49 FrameReference(); | |
50 ~FrameReference(); | |
51 | |
52 void Reset(blink::WebLocalFrame* frame); | |
53 | |
54 blink::WebLocalFrame* GetFrame(); | |
55 blink::WebView* view(); | |
56 | |
57 private: | |
58 blink::WebView* view_; | |
59 blink::WebLocalFrame* frame_; | |
60 }; | |
61 | |
62 // PrintWebViewHelper handles most of the printing grunt work for RenderView. | |
63 // We plan on making print asynchronous and that will require copying the DOM | |
64 // of the document and creating a new WebView with the contents. | |
65 class PrintWebViewHelper | |
66 : public content::RenderViewObserver, | |
67 public content::RenderViewObserverTracker<PrintWebViewHelper> { | |
68 public: | |
69 | |
70 class Delegate { | |
71 public: | |
72 virtual ~Delegate() {} | |
73 | |
74 // Cancels prerender if it's currently in progress and returns |true| if | |
75 // the cancellation was done with success. | |
76 virtual bool CancelPrerender(content::RenderView* render_view, | |
77 int routing_id) = 0; | |
78 | |
79 // Returns the element to be printed. Returns a null WebElement if | |
80 // a pdf plugin element can't be extracted from the frame. | |
81 virtual blink::WebElement GetPdfElement(blink::WebLocalFrame* frame) = 0; | |
82 }; | |
83 | |
84 PrintWebViewHelper(content::RenderView* render_view, | |
85 bool out_of_process_pdf_enabled, | |
86 bool print_preview_disabled, | |
87 scoped_ptr<Delegate> delegate); | |
88 ~PrintWebViewHelper() override; | |
89 | |
90 // Disable print preview and switch to system dialog printing even if full | |
91 // printing is build-in. This method is used by CEF. | |
92 static void DisablePreview(); | |
93 | |
94 bool IsPrintingEnabled(); | |
95 | |
96 void PrintNode(const blink::WebNode& node); | |
97 | |
98 private: | |
99 friend class PrintWebViewHelperTestBase; | |
100 FRIEND_TEST_ALL_PREFIXES(PrintWebViewHelperPreviewTest, | |
101 BlockScriptInitiatedPrinting); | |
102 FRIEND_TEST_ALL_PREFIXES(PrintWebViewHelperTest, OnPrintPages); | |
103 FRIEND_TEST_ALL_PREFIXES(PrintWebViewHelperTest, | |
104 BlockScriptInitiatedPrinting); | |
105 FRIEND_TEST_ALL_PREFIXES(PrintWebViewHelperTest, | |
106 BlockScriptInitiatedPrintingFromPopup); | |
107 #if defined(OS_WIN) || defined(OS_MACOSX) | |
108 FRIEND_TEST_ALL_PREFIXES(PrintWebViewHelperTest, PrintLayoutTest); | |
109 FRIEND_TEST_ALL_PREFIXES(PrintWebViewHelperTest, PrintWithIframe); | |
110 #endif // defined(OS_WIN) || defined(OS_MACOSX) | |
111 | |
112 enum PrintingResult { | |
113 OK, | |
114 FAIL_PRINT_INIT, | |
115 FAIL_PRINT, | |
116 FAIL_PREVIEW, | |
117 }; | |
118 | |
119 enum PrintPreviewErrorBuckets { | |
120 PREVIEW_ERROR_NONE, // Always first. | |
121 PREVIEW_ERROR_BAD_SETTING, | |
122 PREVIEW_ERROR_METAFILE_COPY_FAILED, | |
123 PREVIEW_ERROR_METAFILE_INIT_FAILED, | |
124 PREVIEW_ERROR_ZERO_PAGES, | |
125 PREVIEW_ERROR_MAC_DRAFT_METAFILE_INIT_FAILED, | |
126 PREVIEW_ERROR_PAGE_RENDERED_WITHOUT_METAFILE, | |
127 PREVIEW_ERROR_INVALID_PRINTER_SETTINGS, | |
128 PREVIEW_ERROR_LAST_ENUM // Always last. | |
129 }; | |
130 | |
131 enum PrintPreviewRequestType { | |
132 PRINT_PREVIEW_USER_INITIATED_ENTIRE_FRAME, | |
133 PRINT_PREVIEW_USER_INITIATED_SELECTION, | |
134 PRINT_PREVIEW_USER_INITIATED_CONTEXT_NODE, | |
135 PRINT_PREVIEW_SCRIPTED // triggered by window.print(). | |
136 }; | |
137 | |
138 // RenderViewObserver implementation. | |
139 bool OnMessageReceived(const IPC::Message& message) override; | |
140 void PrintPage(blink::WebLocalFrame* frame, bool user_initiated) override; | |
141 void DidStartLoading() override; | |
142 void DidStopLoading() override; | |
143 | |
144 // Message handlers --------------------------------------------------------- | |
145 #if defined(ENABLE_BASIC_PRINTING) | |
146 void OnPrintPages(); | |
147 void OnPrintForSystemDialog(); | |
148 #endif // ENABLE_BASIC_PRINTING | |
149 void OnInitiatePrintPreview(bool selection_only); | |
150 void OnPrintPreview(const base::DictionaryValue& settings); | |
151 void OnPrintForPrintPreview(const base::DictionaryValue& job_settings); | |
152 void OnPrintingDone(bool success); | |
153 | |
154 // Get |page_size| and |content_area| information from | |
155 // |page_layout_in_points|. | |
156 void GetPageSizeAndContentAreaFromPageLayout( | |
157 const PageSizeMargins& page_layout_in_points, | |
158 gfx::Size* page_size, | |
159 gfx::Rect* content_area); | |
160 | |
161 // Update |ignore_css_margins_| based on settings. | |
162 void UpdateFrameMarginsCssInfo(const base::DictionaryValue& settings); | |
163 | |
164 // Returns true if the current destination printer is PRINT_TO_PDF. | |
165 bool IsPrintToPdfRequested(const base::DictionaryValue& settings); | |
166 | |
167 // Prepare frame for creating preview document. | |
168 void PrepareFrameForPreviewDocument(); | |
169 | |
170 // Continue creating preview document. | |
171 void OnFramePreparedForPreviewDocument(); | |
172 | |
173 // Initialize the print preview document. | |
174 bool CreatePreviewDocument(); | |
175 | |
176 // Renders a print preview page. |page_number| is 0-based. | |
177 // Returns true if print preview should continue, false on failure. | |
178 bool RenderPreviewPage(int page_number, | |
179 const PrintMsg_Print_Params& print_params); | |
180 | |
181 // Finalize the print ready preview document. | |
182 bool FinalizePrintReadyDocument(); | |
183 | |
184 // Enable/Disable window.print calls. If |blocked| is true window.print | |
185 // calls will silently fail. Call with |blocked| set to false to reenable. | |
186 void SetScriptedPrintBlocked(bool blocked); | |
187 | |
188 // Main printing code ------------------------------------------------------- | |
189 | |
190 // |is_scripted| should be true when the call is coming from window.print() | |
191 void Print(blink::WebLocalFrame* frame, | |
192 const blink::WebNode& node, | |
193 bool is_scripted); | |
194 | |
195 // Notification when printing is done - signal tear-down/free resources. | |
196 void DidFinishPrinting(PrintingResult result); | |
197 | |
198 // Print Settings ----------------------------------------------------------- | |
199 | |
200 // Initialize print page settings with default settings. | |
201 // Used only for native printing workflow. | |
202 bool InitPrintSettings(bool fit_to_paper_size); | |
203 | |
204 // Calculate number of pages in source document. | |
205 bool CalculateNumberOfPages(blink::WebLocalFrame* frame, | |
206 const blink::WebNode& node, | |
207 int* number_of_pages); | |
208 | |
209 // Set options for print preset from source PDF document. | |
210 void SetOptionsFromDocument( | |
211 PrintHostMsg_SetOptionsFromDocument_Params& params); | |
212 | |
213 // Update the current print settings with new |passed_job_settings|. | |
214 // |passed_job_settings| dictionary contains print job details such as printer | |
215 // name, number of copies, page range, etc. | |
216 bool UpdatePrintSettings(blink::WebLocalFrame* frame, | |
217 const blink::WebNode& node, | |
218 const base::DictionaryValue& passed_job_settings); | |
219 | |
220 // Get final print settings from the user. | |
221 // Return false if the user cancels or on error. | |
222 bool GetPrintSettingsFromUser(blink::WebFrame* frame, | |
223 const blink::WebNode& node, | |
224 int expected_pages_count, | |
225 bool is_scripted); | |
226 | |
227 // Page Printing / Rendering ------------------------------------------------ | |
228 | |
229 void OnFramePreparedForPrintPages(); | |
230 void PrintPages(); | |
231 bool PrintPagesNative(blink::WebFrame* frame, int page_count); | |
232 void FinishFramePrinting(); | |
233 | |
234 // Prints the page listed in |params|. | |
235 #if defined(OS_LINUX) || defined(OS_ANDROID) | |
236 void PrintPageInternal(const PrintMsg_PrintPage_Params& params, | |
237 blink::WebFrame* frame, | |
238 PdfMetafileSkia* metafile); | |
239 #elif defined(OS_WIN) | |
240 void PrintPageInternal(const PrintMsg_PrintPage_Params& params, | |
241 blink::WebFrame* frame, | |
242 PdfMetafileSkia* metafile, | |
243 gfx::Size* page_size_in_dpi, | |
244 gfx::Rect* content_area_in_dpi); | |
245 #else | |
246 void PrintPageInternal(const PrintMsg_PrintPage_Params& params, | |
247 blink::WebFrame* frame); | |
248 #endif | |
249 | |
250 // Render the frame for printing. | |
251 bool RenderPagesForPrint(blink::WebLocalFrame* frame, | |
252 const blink::WebNode& node); | |
253 | |
254 // Platform specific helper function for rendering page(s) to |metafile|. | |
255 #if defined(OS_MACOSX) | |
256 void RenderPage(const PrintMsg_Print_Params& params, | |
257 int page_number, | |
258 blink::WebFrame* frame, | |
259 bool is_preview, | |
260 PdfMetafileSkia* metafile, | |
261 gfx::Size* page_size, | |
262 gfx::Rect* content_rect); | |
263 #endif // defined(OS_MACOSX) | |
264 | |
265 // Renders page contents from |frame| to |content_area| of |canvas|. | |
266 // |page_number| is zero-based. | |
267 // When method is called, canvas should be setup to draw to |canvas_area| | |
268 // with |scale_factor|. | |
269 static float RenderPageContent(blink::WebFrame* frame, | |
270 int page_number, | |
271 const gfx::Rect& canvas_area, | |
272 const gfx::Rect& content_area, | |
273 double scale_factor, | |
274 blink::WebCanvas* canvas); | |
275 | |
276 // Helper methods ----------------------------------------------------------- | |
277 | |
278 bool CopyMetafileDataToSharedMem(PdfMetafileSkia* metafile, | |
279 base::SharedMemoryHandle* shared_mem_handle); | |
280 | |
281 // Helper method to get page layout in points and fit to page if needed. | |
282 static void ComputePageLayoutInPointsForCss( | |
283 blink::WebFrame* frame, | |
284 int page_index, | |
285 const PrintMsg_Print_Params& default_params, | |
286 bool ignore_css_margins, | |
287 double* scale_factor, | |
288 PageSizeMargins* page_layout_in_points); | |
289 | |
290 #if defined(ENABLE_PRINT_PREVIEW) | |
291 // Given the |device| and |canvas| to draw on, prints the appropriate headers | |
292 // and footers using strings from |header_footer_info| on to the canvas. | |
293 static void PrintHeaderAndFooter(blink::WebCanvas* canvas, | |
294 int page_number, | |
295 int total_pages, | |
296 const blink::WebFrame& source_frame, | |
297 float webkit_scale_factor, | |
298 const PageSizeMargins& page_layout_in_points, | |
299 const PrintMsg_Print_Params& params); | |
300 #endif // defined(ENABLE_PRINT_PREVIEW) | |
301 | |
302 bool GetPrintFrame(blink::WebLocalFrame** frame); | |
303 | |
304 // Script Initiated Printing ------------------------------------------------ | |
305 | |
306 // Return true if script initiated printing is currently | |
307 // allowed. |user_initiated| should be true when a user event triggered the | |
308 // script, most likely by pressing a print button on the page. | |
309 bool IsScriptInitiatedPrintAllowed(blink::WebFrame* frame, | |
310 bool user_initiated); | |
311 | |
312 // Shows scripted print preview when options from plugin are available. | |
313 void ShowScriptedPrintPreview(); | |
314 | |
315 void RequestPrintPreview(PrintPreviewRequestType type); | |
316 | |
317 // Checks whether print preview should continue or not. | |
318 // Returns true if canceling, false if continuing. | |
319 bool CheckForCancel(); | |
320 | |
321 // Notifies the browser a print preview page has been rendered. | |
322 // |page_number| is 0-based. | |
323 // For a valid |page_number| with modifiable content, | |
324 // |metafile| is the rendered page. Otherwise |metafile| is NULL. | |
325 // Returns true if print preview should continue, false on failure. | |
326 bool PreviewPageRendered(int page_number, PdfMetafileSkia* metafile); | |
327 | |
328 void SetPrintPagesParams(const PrintMsg_PrintPages_Params& settings); | |
329 | |
330 // WebView used only to print the selection. | |
331 scoped_ptr<PrepareFrameAndViewForPrint> prep_frame_view_; | |
332 bool reset_prep_frame_view_; | |
333 | |
334 scoped_ptr<PrintMsg_PrintPages_Params> print_pages_params_; | |
335 bool is_print_ready_metafile_sent_; | |
336 bool ignore_css_margins_; | |
337 | |
338 // Used for scripted initiated printing blocking. | |
339 bool is_scripted_printing_blocked_; | |
340 | |
341 // Let the browser process know of a printing failure. Only set to false when | |
342 // the failure came from the browser in the first place. | |
343 bool notify_browser_of_print_failure_; | |
344 | |
345 // True, when printing from print preview. | |
346 bool print_for_preview_; | |
347 | |
348 // Whether the content to print could be nested in an iframe. | |
349 const bool out_of_process_pdf_enabled_; | |
350 | |
351 // Used to check the prerendering status. | |
352 const scoped_ptr<Delegate> delegate_; | |
353 | |
354 // Keeps track of the state of print preview between messages. | |
355 // TODO(vitalybuka): Create PrintPreviewContext when needed and delete after | |
356 // use. Now it's interaction with various messages is confusing. | |
357 class PrintPreviewContext { | |
358 public: | |
359 PrintPreviewContext(); | |
360 ~PrintPreviewContext(); | |
361 | |
362 // Initializes the print preview context. Need to be called to set | |
363 // the |web_frame| / |web_node| to generate the print preview for. | |
364 void InitWithFrame(blink::WebLocalFrame* web_frame); | |
365 void InitWithNode(const blink::WebNode& web_node); | |
366 | |
367 // Does bookkeeping at the beginning of print preview. | |
368 void OnPrintPreview(); | |
369 | |
370 // Create the print preview document. |pages| is empty to print all pages. | |
371 // Takes ownership of |prepared_frame|. | |
372 bool CreatePreviewDocument(PrepareFrameAndViewForPrint* prepared_frame, | |
373 const std::vector<int>& pages); | |
374 | |
375 // Called after a page gets rendered. |page_time| is how long the | |
376 // rendering took. | |
377 void RenderedPreviewPage(const base::TimeDelta& page_time); | |
378 | |
379 // Updates the print preview context when the required pages are rendered. | |
380 void AllPagesRendered(); | |
381 | |
382 // Finalizes the print ready preview document. | |
383 void FinalizePrintReadyDocument(); | |
384 | |
385 // Cleanup after print preview finishes. | |
386 void Finished(); | |
387 | |
388 // Cleanup after print preview fails. | |
389 void Failed(bool report_error); | |
390 | |
391 // Helper functions | |
392 int GetNextPageNumber(); | |
393 bool IsRendering() const; | |
394 bool IsModifiable(); | |
395 bool HasSelection(); | |
396 bool IsLastPageOfPrintReadyMetafile() const; | |
397 bool IsFinalPageRendered() const; | |
398 | |
399 // Setters | |
400 void set_generate_draft_pages(bool generate_draft_pages); | |
401 void set_error(enum PrintPreviewErrorBuckets error); | |
402 | |
403 // Getters | |
404 // Original frame for which preview was requested. | |
405 blink::WebLocalFrame* source_frame(); | |
406 // Original node for which preview was requested. | |
407 const blink::WebNode& source_node() const; | |
408 | |
409 // Frame to be use to render preview. May be the same as source_frame(), or | |
410 // generated from it, e.g. copy of selected block. | |
411 blink::WebLocalFrame* prepared_frame(); | |
412 // Node to be use to render preview. May be the same as source_node(), or | |
413 // generated from it, e.g. copy of selected block. | |
414 const blink::WebNode& prepared_node() const; | |
415 | |
416 int total_page_count() const; | |
417 bool generate_draft_pages() const; | |
418 PdfMetafileSkia* metafile(); | |
419 int last_error() const; | |
420 | |
421 private: | |
422 enum State { | |
423 UNINITIALIZED, // Not ready to render. | |
424 INITIALIZED, // Ready to render. | |
425 RENDERING, // Rendering. | |
426 DONE // Finished rendering. | |
427 }; | |
428 | |
429 // Reset some of the internal rendering context. | |
430 void ClearContext(); | |
431 | |
432 // Specifies what to render for print preview. | |
433 FrameReference source_frame_; | |
434 blink::WebNode source_node_; | |
435 | |
436 scoped_ptr<PrepareFrameAndViewForPrint> prep_frame_view_; | |
437 scoped_ptr<PdfMetafileSkia> metafile_; | |
438 | |
439 // Total page count in the renderer. | |
440 int total_page_count_; | |
441 | |
442 // The current page to render. | |
443 int current_page_index_; | |
444 | |
445 // List of page indices that need to be rendered. | |
446 std::vector<int> pages_to_render_; | |
447 | |
448 // True, when draft pages needs to be generated. | |
449 bool generate_draft_pages_; | |
450 | |
451 // Specifies the total number of pages in the print ready metafile. | |
452 int print_ready_metafile_page_count_; | |
453 | |
454 base::TimeDelta document_render_time_; | |
455 base::TimeTicks begin_time_; | |
456 | |
457 enum PrintPreviewErrorBuckets error_; | |
458 | |
459 State state_; | |
460 }; | |
461 | |
462 class ScriptingThrottler { | |
463 public: | |
464 ScriptingThrottler(); | |
465 | |
466 // Returns false if script initiated printing occurs too often. | |
467 bool IsAllowed(blink::WebFrame* frame); | |
468 | |
469 // Reset the counter for script initiated printing. | |
470 // Scripted printing will be allowed to continue. | |
471 void Reset(); | |
472 | |
473 private: | |
474 base::Time last_print_; | |
475 int count_ = 0; | |
476 DISALLOW_COPY_AND_ASSIGN(ScriptingThrottler); | |
477 }; | |
478 | |
479 ScriptingThrottler scripting_throttler_; | |
480 | |
481 bool print_node_in_progress_; | |
482 PrintPreviewContext print_preview_context_; | |
483 bool is_loading_; | |
484 bool is_scripted_preview_delayed_; | |
485 | |
486 // Used to fix a race condition where the source is a PDF and print preview | |
487 // hangs because RequestPrintPreview is called before DidStopLoading() is | |
488 // called. This is a store for the RequestPrintPreview() call and its | |
489 // parameters so that it can be invoked after DidStopLoading. | |
490 base::Closure on_stop_loading_closure_; | |
491 | |
492 base::WeakPtrFactory<PrintWebViewHelper> weak_ptr_factory_; | |
493 | |
494 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelper); | |
495 }; | |
496 | |
497 } // namespace printing | |
498 | |
499 #endif // CHROME_RENDERER_PRINTING_PRINT_WEB_VIEW_HELPER_H_ | |
OLD | NEW |