OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/renderer/printing/print_web_view_helper.h" | 5 #include "chrome/renderer/printing/print_web_view_helper.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
14 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
15 #include "base/process/process_handle.h" | 15 #include "base/process/process_handle.h" |
16 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
17 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
18 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
19 #include "chrome/common/chrome_switches.h" | 19 #include "chrome/common/chrome_switches.h" |
20 #include "chrome/common/print_messages.h" | 20 #include "chrome/common/print_messages.h" |
21 #include "chrome/common/render_messages.h" | 21 #include "chrome/common/render_messages.h" |
22 #include "chrome/renderer/prerender/prerender_helper.h" | 22 #include "chrome/renderer/prerender/prerender_helper.h" |
| 23 #include "content/public/common/web_preferences.h" |
23 #include "content/public/renderer/render_frame.h" | 24 #include "content/public/renderer/render_frame.h" |
24 #include "content/public/renderer/render_thread.h" | 25 #include "content/public/renderer/render_thread.h" |
25 #include "content/public/renderer/render_view.h" | 26 #include "content/public/renderer/render_view.h" |
26 #include "content/public/renderer/web_preferences.h" | |
27 #include "grit/browser_resources.h" | 27 #include "grit/browser_resources.h" |
28 #include "net/base/escape.h" | 28 #include "net/base/escape.h" |
29 #include "printing/metafile.h" | 29 #include "printing/metafile.h" |
30 #include "printing/metafile_impl.h" | 30 #include "printing/metafile_impl.h" |
31 #include "printing/units.h" | 31 #include "printing/units.h" |
32 #include "skia/ext/vector_platform_device_skia.h" | 32 #include "skia/ext/vector_platform_device_skia.h" |
33 #include "third_party/WebKit/public/platform/WebSize.h" | 33 #include "third_party/WebKit/public/platform/WebSize.h" |
34 #include "third_party/WebKit/public/platform/WebURLRequest.h" | 34 #include "third_party/WebKit/public/platform/WebURLRequest.h" |
35 #include "third_party/WebKit/public/web/WebConsoleMessage.h" | 35 #include "third_party/WebKit/public/web/WebConsoleMessage.h" |
36 #include "third_party/WebKit/public/web/WebDocument.h" | 36 #include "third_party/WebKit/public/web/WebDocument.h" |
37 #include "third_party/WebKit/public/web/WebElement.h" | 37 #include "third_party/WebKit/public/web/WebElement.h" |
38 #include "third_party/WebKit/public/web/WebFrameClient.h" | 38 #include "third_party/WebKit/public/web/WebFrameClient.h" |
39 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 39 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
40 #include "third_party/WebKit/public/web/WebPlugin.h" | 40 #include "third_party/WebKit/public/web/WebPlugin.h" |
41 #include "third_party/WebKit/public/web/WebPluginDocument.h" | 41 #include "third_party/WebKit/public/web/WebPluginDocument.h" |
42 #include "third_party/WebKit/public/web/WebPrintParams.h" | 42 #include "third_party/WebKit/public/web/WebPrintParams.h" |
43 #include "third_party/WebKit/public/web/WebPrintScalingOption.h" | 43 #include "third_party/WebKit/public/web/WebPrintScalingOption.h" |
44 #include "third_party/WebKit/public/web/WebScriptSource.h" | 44 #include "third_party/WebKit/public/web/WebScriptSource.h" |
45 #include "third_party/WebKit/public/web/WebSettings.h" | 45 #include "third_party/WebKit/public/web/WebSettings.h" |
46 #include "third_party/WebKit/public/web/WebView.h" | 46 #include "third_party/WebKit/public/web/WebView.h" |
47 #include "third_party/WebKit/public/web/WebViewClient.h" | 47 #include "third_party/WebKit/public/web/WebViewClient.h" |
48 #include "ui/base/resource/resource_bundle.h" | 48 #include "ui/base/resource/resource_bundle.h" |
49 #include "webkit/common/webpreferences.h" | 49 |
| 50 using content::WebPreferences; |
50 | 51 |
51 namespace printing { | 52 namespace printing { |
52 | 53 |
53 namespace { | 54 namespace { |
54 | 55 |
55 enum PrintPreviewHelperEvents { | 56 enum PrintPreviewHelperEvents { |
56 PREVIEW_EVENT_REQUESTED, | 57 PREVIEW_EVENT_REQUESTED, |
57 PREVIEW_EVENT_CACHE_HIT, // Unused | 58 PREVIEW_EVENT_CACHE_HIT, // Unused |
58 PREVIEW_EVENT_CREATE_DOCUMENT, | 59 PREVIEW_EVENT_CREATE_DOCUMENT, |
59 PREVIEW_EVENT_NEW_SETTINGS, // Unused | 60 PREVIEW_EVENT_NEW_SETTINGS, // Unused |
(...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
525 } | 526 } |
526 return frame->printPage(page_number, canvas); | 527 return frame->printPage(page_number, canvas); |
527 } | 528 } |
528 | 529 |
529 // Class that calls the Begin and End print functions on the frame and changes | 530 // Class that calls the Begin and End print functions on the frame and changes |
530 // the size of the view temporarily to support full page printing.. | 531 // the size of the view temporarily to support full page printing.. |
531 class PrepareFrameAndViewForPrint : public blink::WebViewClient, | 532 class PrepareFrameAndViewForPrint : public blink::WebViewClient, |
532 public blink::WebFrameClient { | 533 public blink::WebFrameClient { |
533 public: | 534 public: |
534 PrepareFrameAndViewForPrint(const PrintMsg_Print_Params& params, | 535 PrepareFrameAndViewForPrint(const PrintMsg_Print_Params& params, |
| 536 content::RenderView* render_view, |
535 blink::WebLocalFrame* frame, | 537 blink::WebLocalFrame* frame, |
536 const blink::WebNode& node, | 538 const blink::WebNode& node, |
537 bool ignore_css_margins); | 539 bool ignore_css_margins); |
538 virtual ~PrepareFrameAndViewForPrint(); | 540 virtual ~PrepareFrameAndViewForPrint(); |
539 | 541 |
540 // Optional. Replaces |frame_| with selection if needed. Will call |on_ready| | 542 // Optional. Replaces |frame_| with selection if needed. Will call |on_ready| |
541 // when completed. | 543 // when completed. |
542 void CopySelectionIfNeeded(const WebPreferences& preferences, | 544 void CopySelectionIfNeeded(const WebPreferences& preferences, |
543 const base::Closure& on_ready); | 545 const base::Closure& on_ready); |
544 | 546 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
581 virtual void frameDetached(blink::WebFrame* frame); | 583 virtual void frameDetached(blink::WebFrame* frame); |
582 | 584 |
583 private: | 585 private: |
584 void CallOnReady(); | 586 void CallOnReady(); |
585 void ResizeForPrinting(); | 587 void ResizeForPrinting(); |
586 void RestoreSize(); | 588 void RestoreSize(); |
587 void CopySelection(const WebPreferences& preferences); | 589 void CopySelection(const WebPreferences& preferences); |
588 | 590 |
589 base::WeakPtrFactory<PrepareFrameAndViewForPrint> weak_ptr_factory_; | 591 base::WeakPtrFactory<PrepareFrameAndViewForPrint> weak_ptr_factory_; |
590 | 592 |
| 593 content::RenderView* render_view_; |
591 FrameReference frame_; | 594 FrameReference frame_; |
592 blink::WebNode node_to_print_; | 595 blink::WebNode node_to_print_; |
593 bool owns_web_view_; | 596 bool owns_web_view_; |
594 blink::WebPrintParams web_print_params_; | 597 blink::WebPrintParams web_print_params_; |
595 gfx::Size prev_view_size_; | 598 gfx::Size prev_view_size_; |
596 gfx::Size prev_scroll_offset_; | 599 gfx::Size prev_scroll_offset_; |
597 int expected_pages_count_; | 600 int expected_pages_count_; |
598 base::Closure on_ready_; | 601 base::Closure on_ready_; |
599 bool should_print_backgrounds_; | 602 bool should_print_backgrounds_; |
600 bool should_print_selection_only_; | 603 bool should_print_selection_only_; |
601 bool is_printing_started_; | 604 bool is_printing_started_; |
602 | 605 |
603 DISALLOW_COPY_AND_ASSIGN(PrepareFrameAndViewForPrint); | 606 DISALLOW_COPY_AND_ASSIGN(PrepareFrameAndViewForPrint); |
604 }; | 607 }; |
605 | 608 |
606 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( | 609 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( |
607 const PrintMsg_Print_Params& params, | 610 const PrintMsg_Print_Params& params, |
| 611 content::RenderView* render_view, |
608 blink::WebLocalFrame* frame, | 612 blink::WebLocalFrame* frame, |
609 const blink::WebNode& node, | 613 const blink::WebNode& node, |
610 bool ignore_css_margins) | 614 bool ignore_css_margins) |
611 : weak_ptr_factory_(this), | 615 : weak_ptr_factory_(this), |
| 616 render_view_(render_view), |
612 frame_(frame), | 617 frame_(frame), |
613 node_to_print_(node), | 618 node_to_print_(node), |
614 owns_web_view_(false), | 619 owns_web_view_(false), |
615 expected_pages_count_(0), | 620 expected_pages_count_(0), |
616 should_print_backgrounds_(params.should_print_backgrounds), | 621 should_print_backgrounds_(params.should_print_backgrounds), |
617 should_print_selection_only_(params.selection_only), | 622 should_print_selection_only_(params.selection_only), |
618 is_printing_started_(false) { | 623 is_printing_started_(false) { |
619 PrintMsg_Print_Params print_params = params; | 624 PrintMsg_Print_Params print_params = params; |
620 if (!should_print_selection_only_ || | 625 if (!should_print_selection_only_ || |
621 !PrintingNodeOrPdfFrame(frame, node_to_print_)) { | 626 !PrintingNodeOrPdfFrame(frame, node_to_print_)) { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
689 RestoreSize(); | 694 RestoreSize(); |
690 // Create a new WebView with the same settings as the current display one. | 695 // Create a new WebView with the same settings as the current display one. |
691 // Except that we disable javascript (don't want any active content running | 696 // Except that we disable javascript (don't want any active content running |
692 // on the page). | 697 // on the page). |
693 WebPreferences prefs = preferences; | 698 WebPreferences prefs = preferences; |
694 prefs.javascript_enabled = false; | 699 prefs.javascript_enabled = false; |
695 prefs.java_enabled = false; | 700 prefs.java_enabled = false; |
696 | 701 |
697 blink::WebView* web_view = blink::WebView::create(this); | 702 blink::WebView* web_view = blink::WebView::create(this); |
698 owns_web_view_ = true; | 703 owns_web_view_ = true; |
699 content::ApplyWebPreferences(prefs, web_view); | 704 render_view_->ApplyWebPreferences(prefs); |
700 web_view->setMainFrame(blink::WebLocalFrame::create(this)); | 705 web_view->setMainFrame(blink::WebLocalFrame::create(this)); |
701 frame_.Reset(web_view->mainFrame()->toWebLocalFrame()); | 706 frame_.Reset(web_view->mainFrame()->toWebLocalFrame()); |
702 node_to_print_.reset(); | 707 node_to_print_.reset(); |
703 | 708 |
704 // When loading is done this will call didStopLoading() and that will do the | 709 // When loading is done this will call didStopLoading() and that will do the |
705 // actual printing. | 710 // actual printing. |
706 frame()->loadRequest(blink::WebURLRequest(GURL(url_str))); | 711 frame()->loadRequest(blink::WebURLRequest(GURL(url_str))); |
707 } | 712 } |
708 | 713 |
709 bool PrepareFrameAndViewForPrint::allowsBrokenNullLayerTreeView() const { | 714 bool PrepareFrameAndViewForPrint::allowsBrokenNullLayerTreeView() const { |
(...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1059 // Don't reset loading frame or WebKit will fail assert. Just retry when | 1064 // Don't reset loading frame or WebKit will fail assert. Just retry when |
1060 // current selection is loaded. | 1065 // current selection is loaded. |
1061 if (prep_frame_view_ && prep_frame_view_->IsLoadingSelection()) { | 1066 if (prep_frame_view_ && prep_frame_view_->IsLoadingSelection()) { |
1062 reset_prep_frame_view_ = true; | 1067 reset_prep_frame_view_ = true; |
1063 return; | 1068 return; |
1064 } | 1069 } |
1065 | 1070 |
1066 const PrintMsg_Print_Params& print_params = print_pages_params_->params; | 1071 const PrintMsg_Print_Params& print_params = print_pages_params_->params; |
1067 prep_frame_view_.reset( | 1072 prep_frame_view_.reset( |
1068 new PrepareFrameAndViewForPrint(print_params, | 1073 new PrepareFrameAndViewForPrint(print_params, |
| 1074 render_view(), |
1069 print_preview_context_.source_frame(), | 1075 print_preview_context_.source_frame(), |
1070 print_preview_context_.source_node(), | 1076 print_preview_context_.source_node(), |
1071 ignore_css_margins_)); | 1077 ignore_css_margins_)); |
1072 prep_frame_view_->CopySelectionIfNeeded( | 1078 prep_frame_view_->CopySelectionIfNeeded( |
1073 render_view()->GetWebkitPreferences(), | 1079 render_view()->GetWebkitPreferences(), |
1074 base::Bind(&PrintWebViewHelper::OnFramePreparedForPreviewDocument, | 1080 base::Bind(&PrintWebViewHelper::OnFramePreparedForPreviewDocument, |
1075 base::Unretained(this))); | 1081 base::Unretained(this))); |
1076 } | 1082 } |
1077 | 1083 |
1078 void PrintWebViewHelper::OnFramePreparedForPreviewDocument() { | 1084 void PrintWebViewHelper::OnFramePreparedForPreviewDocument() { |
(...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1449 int* number_of_pages) { | 1455 int* number_of_pages) { |
1450 DCHECK(frame); | 1456 DCHECK(frame); |
1451 bool fit_to_paper_size = !(PrintingNodeOrPdfFrame(frame, node)); | 1457 bool fit_to_paper_size = !(PrintingNodeOrPdfFrame(frame, node)); |
1452 if (!InitPrintSettings(fit_to_paper_size)) { | 1458 if (!InitPrintSettings(fit_to_paper_size)) { |
1453 notify_browser_of_print_failure_ = false; | 1459 notify_browser_of_print_failure_ = false; |
1454 Send(new PrintHostMsg_ShowInvalidPrinterSettingsError(routing_id())); | 1460 Send(new PrintHostMsg_ShowInvalidPrinterSettingsError(routing_id())); |
1455 return false; | 1461 return false; |
1456 } | 1462 } |
1457 | 1463 |
1458 const PrintMsg_Print_Params& params = print_pages_params_->params; | 1464 const PrintMsg_Print_Params& params = print_pages_params_->params; |
1459 PrepareFrameAndViewForPrint prepare(params, frame, node, ignore_css_margins_); | 1465 PrepareFrameAndViewForPrint prepare( |
| 1466 params, render_view(), frame, node, ignore_css_margins_); |
1460 prepare.StartPrinting(); | 1467 prepare.StartPrinting(); |
1461 | 1468 |
1462 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(), | 1469 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(), |
1463 params.document_cookie)); | 1470 params.document_cookie)); |
1464 *number_of_pages = prepare.GetExpectedPageCount(); | 1471 *number_of_pages = prepare.GetExpectedPageCount(); |
1465 return true; | 1472 return true; |
1466 } | 1473 } |
1467 | 1474 |
1468 bool PrintWebViewHelper::UpdatePrintSettings( | 1475 bool PrintWebViewHelper::UpdatePrintSettings( |
1469 blink::WebLocalFrame* frame, | 1476 blink::WebLocalFrame* frame, |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1589 print_pages_params_->params.print_scaling_option = scaling_option; | 1596 print_pages_params_->params.print_scaling_option = scaling_option; |
1590 return (print_settings.params.dpi && print_settings.params.document_cookie); | 1597 return (print_settings.params.dpi && print_settings.params.document_cookie); |
1591 } | 1598 } |
1592 | 1599 |
1593 bool PrintWebViewHelper::RenderPagesForPrint(blink::WebLocalFrame* frame, | 1600 bool PrintWebViewHelper::RenderPagesForPrint(blink::WebLocalFrame* frame, |
1594 const blink::WebNode& node) { | 1601 const blink::WebNode& node) { |
1595 if (!frame || prep_frame_view_) | 1602 if (!frame || prep_frame_view_) |
1596 return false; | 1603 return false; |
1597 const PrintMsg_PrintPages_Params& params = *print_pages_params_; | 1604 const PrintMsg_PrintPages_Params& params = *print_pages_params_; |
1598 const PrintMsg_Print_Params& print_params = params.params; | 1605 const PrintMsg_Print_Params& print_params = params.params; |
1599 prep_frame_view_.reset( | 1606 prep_frame_view_.reset(new PrepareFrameAndViewForPrint( |
1600 new PrepareFrameAndViewForPrint(print_params, frame, node, | 1607 print_params, render_view(), frame, node, ignore_css_margins_)); |
1601 ignore_css_margins_)); | |
1602 DCHECK(!print_pages_params_->params.selection_only || | 1608 DCHECK(!print_pages_params_->params.selection_only || |
1603 print_pages_params_->pages.empty()); | 1609 print_pages_params_->pages.empty()); |
1604 prep_frame_view_->CopySelectionIfNeeded( | 1610 prep_frame_view_->CopySelectionIfNeeded( |
1605 render_view()->GetWebkitPreferences(), | 1611 render_view()->GetWebkitPreferences(), |
1606 base::Bind(&PrintWebViewHelper::OnFramePreparedForPrintPages, | 1612 base::Bind(&PrintWebViewHelper::OnFramePreparedForPrintPages, |
1607 base::Unretained(this))); | 1613 base::Unretained(this))); |
1608 return true; | 1614 return true; |
1609 } | 1615 } |
1610 | 1616 |
1611 #if defined(OS_POSIX) | 1617 #if defined(OS_POSIX) |
(...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2022 } | 2028 } |
2023 | 2029 |
2024 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { | 2030 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
2025 prep_frame_view_.reset(); | 2031 prep_frame_view_.reset(); |
2026 metafile_.reset(); | 2032 metafile_.reset(); |
2027 pages_to_render_.clear(); | 2033 pages_to_render_.clear(); |
2028 error_ = PREVIEW_ERROR_NONE; | 2034 error_ = PREVIEW_ERROR_NONE; |
2029 } | 2035 } |
2030 | 2036 |
2031 } // namespace printing | 2037 } // namespace printing |
OLD | NEW |