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" |
(...skipping 483 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
494 void CopySelectionIfNeeded(const webkit_glue::WebPreferences& preferences, | 494 void CopySelectionIfNeeded(const webkit_glue::WebPreferences& preferences, |
495 const base::Closure& on_ready); | 495 const base::Closure& on_ready); |
496 | 496 |
497 // Prepares frame for printing. | 497 // Prepares frame for printing. |
498 void StartPrinting(); | 498 void StartPrinting(); |
499 | 499 |
500 WebKit::WebFrame* frame() const { | 500 WebKit::WebFrame* frame() const { |
501 return frame_; | 501 return frame_; |
502 } | 502 } |
503 | 503 |
504 WebKit::WebNode node() const { | 504 const WebKit::WebNode& node() const { |
505 return node_to_print_; | 505 return node_to_print_; |
506 } | 506 } |
507 | 507 |
508 int GetExpectedPageCount() const { | 508 int GetExpectedPageCount() const { |
509 return expected_pages_count_; | 509 return expected_pages_count_; |
510 } | 510 } |
511 | 511 |
512 gfx::Size GetPrintCanvasSize() const; | 512 gfx::Size GetPrintCanvasSize() const; |
513 | 513 |
514 void FinishPrinting(); | 514 void FinishPrinting(); |
(...skipping 12 matching lines...) Expand all Loading... |
527 WebKit::WebFrame* frame_; | 527 WebKit::WebFrame* frame_; |
528 WebKit::WebNode node_to_print_; | 528 WebKit::WebNode node_to_print_; |
529 bool should_close_web_view_; | 529 bool should_close_web_view_; |
530 WebKit::WebPrintParams web_print_params_; | 530 WebKit::WebPrintParams web_print_params_; |
531 gfx::Size prev_view_size_; | 531 gfx::Size prev_view_size_; |
532 gfx::Size prev_scroll_offset_; | 532 gfx::Size prev_scroll_offset_; |
533 int expected_pages_count_; | 533 int expected_pages_count_; |
534 base::Closure on_ready_; | 534 base::Closure on_ready_; |
535 bool should_print_backgrounds_; | 535 bool should_print_backgrounds_; |
536 bool should_print_selection_only_; | 536 bool should_print_selection_only_; |
| 537 bool is_printing_started_; |
537 | 538 |
538 DISALLOW_COPY_AND_ASSIGN(PrepareFrameAndViewForPrint); | 539 DISALLOW_COPY_AND_ASSIGN(PrepareFrameAndViewForPrint); |
539 }; | 540 }; |
540 | 541 |
541 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( | 542 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( |
542 const PrintMsg_Print_Params& params, | 543 const PrintMsg_Print_Params& params, |
543 WebKit::WebFrame* frame, | 544 WebKit::WebFrame* frame, |
544 const WebKit::WebNode& node, | 545 const WebKit::WebNode& node, |
545 bool ignore_css_margins) | 546 bool ignore_css_margins) |
546 : weak_ptr_factory_(this), | 547 : weak_ptr_factory_(this), |
547 frame_(frame), | 548 frame_(frame), |
548 node_to_print_(node), | 549 node_to_print_(node), |
549 should_close_web_view_(false), | 550 should_close_web_view_(false), |
550 expected_pages_count_(0), | 551 expected_pages_count_(0), |
551 should_print_backgrounds_(params.should_print_backgrounds), | 552 should_print_backgrounds_(params.should_print_backgrounds), |
552 should_print_selection_only_(params.selection_only) { | 553 should_print_selection_only_(params.selection_only), |
| 554 is_printing_started_(false) { |
553 PrintMsg_Print_Params print_params = params; | 555 PrintMsg_Print_Params print_params = params; |
554 if (!should_print_selection_only_ || | 556 if (!should_print_selection_only_ || |
555 !PrintingNodeOrPdfFrame(frame_, node_to_print_)) { | 557 !PrintingNodeOrPdfFrame(frame_, node_to_print_)) { |
556 bool fit_to_page = ignore_css_margins && | 558 bool fit_to_page = ignore_css_margins && |
557 print_params.print_scaling_option == | 559 print_params.print_scaling_option == |
558 WebKit::WebPrintScalingOptionFitToPrintableArea; | 560 WebKit::WebPrintScalingOptionFitToPrintableArea; |
559 print_params = CalculatePrintParamsForCss(frame_, 0, print_params, | 561 print_params = CalculatePrintParamsForCss(frame_, 0, print_params, |
560 ignore_css_margins, fit_to_page, | 562 ignore_css_margins, fit_to_page, |
561 NULL); | 563 NULL); |
562 } | 564 } |
(...skipping 25 matching lines...) Expand all Loading... |
588 if (WebKit::WebFrame* web_frame = web_view->mainFrame()) | 590 if (WebKit::WebFrame* web_frame = web_view->mainFrame()) |
589 prev_scroll_offset_ = web_frame->scrollOffset(); | 591 prev_scroll_offset_ = web_frame->scrollOffset(); |
590 prev_view_size_ = web_view->size(); | 592 prev_view_size_ = web_view->size(); |
591 | 593 |
592 web_view->resize(print_layout_size); | 594 web_view->resize(print_layout_size); |
593 web_view->settings()->setShouldPrintBackgrounds(should_print_backgrounds_); | 595 web_view->settings()->setShouldPrintBackgrounds(should_print_backgrounds_); |
594 // TODO(vitalybuka): Update call after | 596 // TODO(vitalybuka): Update call after |
595 // https://bugs.webkit.org/show_bug.cgi?id=107718 is fixed. | 597 // https://bugs.webkit.org/show_bug.cgi?id=107718 is fixed. |
596 expected_pages_count_ = frame_->printBegin(web_print_params_, node_to_print_, | 598 expected_pages_count_ = frame_->printBegin(web_print_params_, node_to_print_, |
597 NULL); | 599 NULL); |
| 600 is_printing_started_ = true; |
598 } | 601 } |
599 | 602 |
600 void PrepareFrameAndViewForPrint::CopySelectionIfNeeded( | 603 void PrepareFrameAndViewForPrint::CopySelectionIfNeeded( |
601 const webkit_glue::WebPreferences& preferences, | 604 const webkit_glue::WebPreferences& preferences, |
602 const base::Closure& on_ready) { | 605 const base::Closure& on_ready) { |
603 on_ready_ = on_ready; | 606 on_ready_ = on_ready; |
604 if (should_print_selection_only_) | 607 if (should_print_selection_only_) |
605 CopySelection(preferences); | 608 CopySelection(preferences); |
606 else | 609 else |
607 didStopLoading(); | 610 didStopLoading(); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
646 return on_ready_.Run(); // Can delete |this|. | 649 return on_ready_.Run(); // Can delete |this|. |
647 } | 650 } |
648 | 651 |
649 gfx::Size PrepareFrameAndViewForPrint::GetPrintCanvasSize() const { | 652 gfx::Size PrepareFrameAndViewForPrint::GetPrintCanvasSize() const { |
650 return gfx::Size(web_print_params_.printContentArea.width, | 653 return gfx::Size(web_print_params_.printContentArea.width, |
651 web_print_params_.printContentArea.height); | 654 web_print_params_.printContentArea.height); |
652 } | 655 } |
653 | 656 |
654 void PrepareFrameAndViewForPrint::FinishPrinting() { | 657 void PrepareFrameAndViewForPrint::FinishPrinting() { |
655 if (frame_) { | 658 if (frame_) { |
656 frame_->printEnd(); | 659 if (is_printing_started_) |
| 660 frame_->printEnd(); |
657 WebKit::WebView* web_view = frame_->view(); | 661 WebKit::WebView* web_view = frame_->view(); |
658 web_view->resize(prev_view_size_); | 662 web_view->resize(prev_view_size_); |
659 if (WebKit::WebFrame* web_frame = web_view->mainFrame()) | 663 if (WebKit::WebFrame* web_frame = web_view->mainFrame()) |
660 web_frame->setScrollOffset(prev_scroll_offset_); | 664 web_frame->setScrollOffset(prev_scroll_offset_); |
661 web_view->settings()->setShouldPrintBackgrounds(false); | 665 web_view->settings()->setShouldPrintBackgrounds(false); |
662 if (should_close_web_view_) | 666 if (should_close_web_view_) |
663 web_view->close(); | 667 web_view->close(); |
664 } | 668 } |
665 frame_ = NULL; | 669 frame_ = NULL; |
666 on_ready_.Reset(); | 670 on_ready_.Reset(); |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
806 return true; | 810 return true; |
807 } | 811 } |
808 | 812 |
809 void PrintWebViewHelper::OnPrintPages() { | 813 void PrintWebViewHelper::OnPrintPages() { |
810 WebKit::WebFrame* frame; | 814 WebKit::WebFrame* frame; |
811 if (GetPrintFrame(&frame)) | 815 if (GetPrintFrame(&frame)) |
812 Print(frame, WebKit::WebNode()); | 816 Print(frame, WebKit::WebNode()); |
813 } | 817 } |
814 | 818 |
815 void PrintWebViewHelper::OnPrintForSystemDialog() { | 819 void PrintWebViewHelper::OnPrintForSystemDialog() { |
816 WebKit::WebFrame* frame = print_preview_context_.frame(); | 820 WebKit::WebFrame* frame = print_preview_context_.source_frame(); |
817 if (!frame) { | 821 if (!frame) { |
818 NOTREACHED(); | 822 NOTREACHED(); |
819 return; | 823 return; |
820 } | 824 } |
821 | 825 |
822 Print(frame, print_preview_context_.node()); | 826 Print(frame, print_preview_context_.source_node()); |
823 } | 827 } |
824 | 828 |
825 void PrintWebViewHelper::GetPageSizeAndContentAreaFromPageLayout( | 829 void PrintWebViewHelper::GetPageSizeAndContentAreaFromPageLayout( |
826 const PageSizeMargins& page_layout_in_points, | 830 const PageSizeMargins& page_layout_in_points, |
827 gfx::Size* page_size, | 831 gfx::Size* page_size, |
828 gfx::Rect* content_area) { | 832 gfx::Rect* content_area) { |
829 *page_size = gfx::Size( | 833 *page_size = gfx::Size( |
830 page_layout_in_points.content_width + | 834 page_layout_in_points.content_width + |
831 page_layout_in_points.margin_right + | 835 page_layout_in_points.margin_right + |
832 page_layout_in_points.margin_left, | 836 page_layout_in_points.margin_left, |
(...skipping 28 matching lines...) Expand all Loading... |
861 DCHECK(!print_for_preview_); | 865 DCHECK(!print_for_preview_); |
862 | 866 |
863 if (params.print_to_pdf) | 867 if (params.print_to_pdf) |
864 return WebKit::WebPrintScalingOptionSourceSize; | 868 return WebKit::WebPrintScalingOptionSourceSize; |
865 | 869 |
866 if (!source_is_html) { | 870 if (!source_is_html) { |
867 if (!FitToPageEnabled(job_settings)) | 871 if (!FitToPageEnabled(job_settings)) |
868 return WebKit::WebPrintScalingOptionNone; | 872 return WebKit::WebPrintScalingOptionNone; |
869 | 873 |
870 bool no_plugin_scaling = | 874 bool no_plugin_scaling = |
871 print_preview_context_.frame()->isPrintScalingDisabledForPlugin( | 875 print_preview_context_.source_frame()->isPrintScalingDisabledForPlugin( |
872 print_preview_context_.node()); | 876 print_preview_context_.source_node()); |
873 | 877 |
874 if (params.is_first_request && no_plugin_scaling) | 878 if (params.is_first_request && no_plugin_scaling) |
875 return WebKit::WebPrintScalingOptionNone; | 879 return WebKit::WebPrintScalingOptionNone; |
876 } | 880 } |
877 return WebKit::WebPrintScalingOptionFitToPrintableArea; | 881 return WebKit::WebPrintScalingOptionFitToPrintableArea; |
878 } | 882 } |
879 | 883 |
880 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { | 884 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { |
881 DCHECK(is_preview_enabled_); | 885 DCHECK(is_preview_enabled_); |
882 print_preview_context_.OnPrintPreview(); | 886 print_preview_context_.OnPrintPreview(); |
883 | 887 |
884 if (!UpdatePrintSettings(print_preview_context_.frame(), | 888 if (!UpdatePrintSettings(print_preview_context_.source_frame(), |
885 print_preview_context_.node(), settings)) { | 889 print_preview_context_.source_node(), settings)) { |
886 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) { | 890 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) { |
887 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings( | 891 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings( |
888 routing_id(), print_pages_params_->params.document_cookie)); | 892 routing_id(), print_pages_params_->params.document_cookie)); |
889 notify_browser_of_print_failure_ = false; // Already sent. | 893 notify_browser_of_print_failure_ = false; // Already sent. |
890 } | 894 } |
891 DidFinishPrinting(FAIL_PREVIEW); | 895 DidFinishPrinting(FAIL_PREVIEW); |
892 return; | 896 return; |
893 } | 897 } |
894 | 898 |
895 if (!print_pages_params_->params.is_first_request && | 899 if (!print_pages_params_->params.is_first_request && |
(...skipping 13 matching lines...) Expand all Loading... |
909 | 913 |
910 Send(new PrintHostMsg_MetafileReadyForPrinting(routing_id(), | 914 Send(new PrintHostMsg_MetafileReadyForPrinting(routing_id(), |
911 preview_params)); | 915 preview_params)); |
912 return; | 916 return; |
913 } | 917 } |
914 | 918 |
915 // If we are previewing a pdf and the print scaling is disabled, send a | 919 // If we are previewing a pdf and the print scaling is disabled, send a |
916 // message to browser. | 920 // message to browser. |
917 if (print_pages_params_->params.is_first_request && | 921 if (print_pages_params_->params.is_first_request && |
918 !print_preview_context_.IsModifiable() && | 922 !print_preview_context_.IsModifiable() && |
919 print_preview_context_.frame()->isPrintScalingDisabledForPlugin( | 923 print_preview_context_.source_frame()->isPrintScalingDisabledForPlugin( |
920 print_preview_context_.node())) { | 924 print_preview_context_.source_node())) { |
921 Send(new PrintHostMsg_PrintPreviewScalingDisabled(routing_id())); | 925 Send(new PrintHostMsg_PrintPreviewScalingDisabled(routing_id())); |
922 } | 926 } |
923 | 927 |
924 // Always clear |old_print_pages_params_| before rendering the pages. | 928 // Always clear |old_print_pages_params_| before rendering the pages. |
925 old_print_pages_params_.reset(); | 929 old_print_pages_params_.reset(); |
926 is_print_ready_metafile_sent_ = false; | 930 is_print_ready_metafile_sent_ = false; |
927 | 931 |
928 // PDF printer device supports alpha blending. | 932 // PDF printer device supports alpha blending. |
929 print_pages_params_->params.supports_alpha_blend = true; | 933 print_pages_params_->params.supports_alpha_blend = true; |
930 | 934 |
931 bool generate_draft_pages = false; | 935 bool generate_draft_pages = false; |
932 if (!settings.GetBoolean(kSettingGenerateDraftData, | 936 if (!settings.GetBoolean(kSettingGenerateDraftData, |
933 &generate_draft_pages)) { | 937 &generate_draft_pages)) { |
934 NOTREACHED(); | 938 NOTREACHED(); |
935 } | 939 } |
936 print_preview_context_.set_generate_draft_pages(generate_draft_pages); | 940 print_preview_context_.set_generate_draft_pages(generate_draft_pages); |
937 | 941 |
| 942 PrepareFrameForPreviewDocument(); |
| 943 } |
| 944 |
| 945 void PrintWebViewHelper::PrepareFrameForPreviewDocument() { |
| 946 const PrintMsg_Print_Params& print_params = print_pages_params_->params; |
| 947 |
| 948 prep_frame_view_.reset( |
| 949 new PrepareFrameAndViewForPrint(print_params, |
| 950 print_preview_context_.source_frame(), |
| 951 print_preview_context_.source_node(), |
| 952 ignore_css_margins_)); |
| 953 prep_frame_view_->CopySelectionIfNeeded( |
| 954 render_view()->GetWebkitPreferences(), |
| 955 base::Bind(&PrintWebViewHelper::OnFramePreparedForPreviewDocument, |
| 956 base::Unretained(this))); |
| 957 } |
| 958 |
| 959 void PrintWebViewHelper::OnFramePreparedForPreviewDocument() { |
938 if (CreatePreviewDocument()) { | 960 if (CreatePreviewDocument()) { |
939 DidFinishPrinting(OK); | 961 DidFinishPrinting(OK); |
940 } else { | 962 } else { |
941 if (notify_browser_of_print_failure_) | 963 if (notify_browser_of_print_failure_) |
942 LOG(ERROR) << "CreatePreviewDocument failed"; | 964 LOG(ERROR) << "CreatePreviewDocument failed"; |
943 DidFinishPrinting(FAIL_PREVIEW); | 965 DidFinishPrinting(FAIL_PREVIEW); |
944 } | 966 } |
945 } | 967 } |
946 | 968 |
947 bool PrintWebViewHelper::CreatePreviewDocument() { | 969 bool PrintWebViewHelper::CreatePreviewDocument() { |
| 970 if (CheckForCancel()) |
| 971 return false; |
| 972 |
948 const PrintMsg_Print_Params& print_params = print_pages_params_->params; | 973 const PrintMsg_Print_Params& print_params = print_pages_params_->params; |
949 const std::vector<int>& pages = print_pages_params_->pages; | 974 const std::vector<int>& pages = print_pages_params_->pages; |
950 if (!print_preview_context_.CreatePreviewDocument(print_params, pages, | 975 |
951 ignore_css_margins_)) { | 976 if (!print_preview_context_.CreatePreviewDocument(prep_frame_view_.release(), |
| 977 pages)) { |
952 return false; | 978 return false; |
953 } | 979 } |
954 | 980 |
955 PageSizeMargins default_page_layout; | 981 PageSizeMargins default_page_layout; |
956 ComputePageLayoutInPointsForCss(print_preview_context_.frame(), 0, | 982 ComputePageLayoutInPointsForCss(print_preview_context_.prepared_frame(), 0, |
957 print_params, ignore_css_margins_, NULL, | 983 print_params, ignore_css_margins_, NULL, |
958 &default_page_layout); | 984 &default_page_layout); |
959 | 985 |
960 if (!old_print_pages_params_.get() || | 986 if (!old_print_pages_params_.get() || |
961 !PageLayoutIsEqual(*old_print_pages_params_, *print_pages_params_)) { | 987 !PageLayoutIsEqual(*old_print_pages_params_, *print_pages_params_)) { |
962 bool has_page_size_style = PrintingFrameHasPageSizeStyle( | 988 bool has_page_size_style = PrintingFrameHasPageSizeStyle( |
963 print_preview_context_.frame(), | 989 print_preview_context_.prepared_frame(), |
964 print_preview_context_.total_page_count()); | 990 print_preview_context_.total_page_count()); |
965 int dpi = GetDPI(&print_params); | 991 int dpi = GetDPI(&print_params); |
966 | 992 |
967 gfx::Rect printable_area_in_points( | 993 gfx::Rect printable_area_in_points( |
968 ConvertUnit(print_params.printable_area.x(), dpi, kPointsPerInch), | 994 ConvertUnit(print_params.printable_area.x(), dpi, kPointsPerInch), |
969 ConvertUnit(print_params.printable_area.y(), dpi, kPointsPerInch), | 995 ConvertUnit(print_params.printable_area.y(), dpi, kPointsPerInch), |
970 ConvertUnit(print_params.printable_area.width(), dpi, kPointsPerInch), | 996 ConvertUnit(print_params.printable_area.width(), dpi, kPointsPerInch), |
971 ConvertUnit(print_params.printable_area.height(), dpi, kPointsPerInch)); | 997 ConvertUnit(print_params.printable_area.height(), dpi, kPointsPerInch)); |
972 | 998 |
973 // Margins: Send default page layout to browser process. | 999 // Margins: Send default page layout to browser process. |
(...skipping 630 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1604 preview_page_params.data_size = buf_size; | 1630 preview_page_params.data_size = buf_size; |
1605 preview_page_params.page_number = page_number; | 1631 preview_page_params.page_number = page_number; |
1606 preview_page_params.preview_request_id = | 1632 preview_page_params.preview_request_id = |
1607 print_pages_params_->params.preview_request_id; | 1633 print_pages_params_->params.preview_request_id; |
1608 | 1634 |
1609 Send(new PrintHostMsg_DidPreviewPage(routing_id(), preview_page_params)); | 1635 Send(new PrintHostMsg_DidPreviewPage(routing_id(), preview_page_params)); |
1610 return true; | 1636 return true; |
1611 } | 1637 } |
1612 | 1638 |
1613 PrintWebViewHelper::PrintPreviewContext::PrintPreviewContext() | 1639 PrintWebViewHelper::PrintPreviewContext::PrintPreviewContext() |
1614 : frame_(NULL), | 1640 : source_frame_(NULL), |
1615 total_page_count_(0), | 1641 total_page_count_(0), |
1616 current_page_index_(0), | 1642 current_page_index_(0), |
1617 generate_draft_pages_(true), | 1643 generate_draft_pages_(true), |
1618 print_ready_metafile_page_count_(0), | 1644 print_ready_metafile_page_count_(0), |
1619 error_(PREVIEW_ERROR_NONE), | 1645 error_(PREVIEW_ERROR_NONE), |
1620 state_(UNINITIALIZED) { | 1646 state_(UNINITIALIZED) { |
1621 } | 1647 } |
1622 | 1648 |
1623 PrintWebViewHelper::PrintPreviewContext::~PrintPreviewContext() { | 1649 PrintWebViewHelper::PrintPreviewContext::~PrintPreviewContext() { |
1624 } | 1650 } |
1625 | 1651 |
1626 void PrintWebViewHelper::PrintPreviewContext::InitWithFrame( | 1652 void PrintWebViewHelper::PrintPreviewContext::InitWithFrame( |
1627 WebKit::WebFrame* web_frame) { | 1653 WebKit::WebFrame* web_frame) { |
1628 DCHECK(web_frame); | 1654 DCHECK(web_frame); |
1629 DCHECK(!IsRendering()); | 1655 DCHECK(!IsRendering()); |
1630 state_ = INITIALIZED; | 1656 state_ = INITIALIZED; |
1631 frame_ = web_frame; | 1657 source_frame_ = web_frame; |
1632 node_.reset(); | 1658 source_node_.reset(); |
1633 } | 1659 } |
1634 | 1660 |
1635 void PrintWebViewHelper::PrintPreviewContext::InitWithNode( | 1661 void PrintWebViewHelper::PrintPreviewContext::InitWithNode( |
1636 const WebKit::WebNode& web_node) { | 1662 const WebKit::WebNode& web_node) { |
1637 DCHECK(!web_node.isNull()); | 1663 DCHECK(!web_node.isNull()); |
1638 DCHECK(web_node.document().frame()); | 1664 DCHECK(web_node.document().frame()); |
1639 DCHECK(!IsRendering()); | 1665 DCHECK(!IsRendering()); |
1640 state_ = INITIALIZED; | 1666 state_ = INITIALIZED; |
1641 frame_ = web_node.document().frame(); | 1667 source_frame_ = web_node.document().frame(); |
1642 node_ = web_node; | 1668 source_node_ = web_node; |
1643 } | 1669 } |
1644 | 1670 |
1645 void PrintWebViewHelper::PrintPreviewContext::OnPrintPreview() { | 1671 void PrintWebViewHelper::PrintPreviewContext::OnPrintPreview() { |
1646 DCHECK_EQ(INITIALIZED, state_); | 1672 DCHECK_EQ(INITIALIZED, state_); |
1647 ClearContext(); | 1673 ClearContext(); |
1648 } | 1674 } |
1649 | 1675 |
1650 bool PrintWebViewHelper::PrintPreviewContext::CreatePreviewDocument( | 1676 bool PrintWebViewHelper::PrintPreviewContext::CreatePreviewDocument( |
1651 const PrintMsg_Print_Params& print_params, | 1677 PrepareFrameAndViewForPrint* prepared_frame, |
1652 const std::vector<int>& pages, | 1678 const std::vector<int>& pages) { |
1653 bool ignore_css_margins) { | |
1654 DCHECK_EQ(INITIALIZED, state_); | 1679 DCHECK_EQ(INITIALIZED, state_); |
1655 state_ = RENDERING; | 1680 state_ = RENDERING; |
1656 | 1681 |
1657 metafile_.reset(new PreviewMetafile); | |
1658 if (!metafile_->Init()) { | |
1659 set_error(PREVIEW_ERROR_METAFILE_INIT_FAILED); | |
1660 LOG(ERROR) << "PreviewMetafile Init failed"; | |
1661 return false; | |
1662 } | |
1663 | |
1664 // Need to make sure old object gets destroyed first. | 1682 // Need to make sure old object gets destroyed first. |
1665 prep_frame_view_.reset( | 1683 prep_frame_view_.reset(prepared_frame); |
1666 new PrepareFrameAndViewForPrint(print_params, frame(), node(), | |
1667 ignore_css_margins)); | |
1668 prep_frame_view_->StartPrinting(); | 1684 prep_frame_view_->StartPrinting(); |
1669 | 1685 |
1670 total_page_count_ = prep_frame_view_->GetExpectedPageCount(); | 1686 total_page_count_ = prep_frame_view_->GetExpectedPageCount(); |
1671 if (total_page_count_ == 0) { | 1687 if (total_page_count_ == 0) { |
1672 LOG(ERROR) << "CreatePreviewDocument got 0 page count"; | 1688 LOG(ERROR) << "CreatePreviewDocument got 0 page count"; |
1673 set_error(PREVIEW_ERROR_ZERO_PAGES); | 1689 set_error(PREVIEW_ERROR_ZERO_PAGES); |
1674 return false; | 1690 return false; |
1675 } | 1691 } |
1676 | 1692 |
| 1693 metafile_.reset(new PreviewMetafile); |
| 1694 if (!metafile_->Init()) { |
| 1695 set_error(PREVIEW_ERROR_METAFILE_INIT_FAILED); |
| 1696 LOG(ERROR) << "PreviewMetafile Init failed"; |
| 1697 return false; |
| 1698 } |
| 1699 |
1677 int selected_page_count = pages.size(); | 1700 int selected_page_count = pages.size(); |
1678 current_page_index_ = 0; | 1701 current_page_index_ = 0; |
1679 print_ready_metafile_page_count_ = selected_page_count; | 1702 print_ready_metafile_page_count_ = selected_page_count; |
1680 pages_to_render_ = pages; | 1703 pages_to_render_ = pages; |
1681 | 1704 |
1682 if (selected_page_count == 0) { | 1705 if (selected_page_count == 0) { |
1683 print_ready_metafile_page_count_ = total_page_count_; | 1706 print_ready_metafile_page_count_ = total_page_count_; |
1684 // Render all pages. | 1707 // Render all pages. |
1685 for (int i = 0; i < total_page_count_; ++i) | 1708 for (int i = 0; i < total_page_count_; ++i) |
1686 pages_to_render_.push_back(i); | 1709 pages_to_render_.push_back(i); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1758 return -1; | 1781 return -1; |
1759 return pages_to_render_[current_page_index_++]; | 1782 return pages_to_render_[current_page_index_++]; |
1760 } | 1783 } |
1761 | 1784 |
1762 bool PrintWebViewHelper::PrintPreviewContext::IsRendering() const { | 1785 bool PrintWebViewHelper::PrintPreviewContext::IsRendering() const { |
1763 return state_ == RENDERING || state_ == DONE; | 1786 return state_ == RENDERING || state_ == DONE; |
1764 } | 1787 } |
1765 | 1788 |
1766 bool PrintWebViewHelper::PrintPreviewContext::IsModifiable() const { | 1789 bool PrintWebViewHelper::PrintPreviewContext::IsModifiable() const { |
1767 // The only kind of node we can print right now is a PDF node. | 1790 // The only kind of node we can print right now is a PDF node. |
1768 return !PrintingNodeOrPdfFrame(frame_, node_); | 1791 return !PrintingNodeOrPdfFrame(source_frame_, source_node_); |
1769 } | 1792 } |
1770 | 1793 |
1771 bool PrintWebViewHelper::PrintPreviewContext::IsLastPageOfPrintReadyMetafile() | 1794 bool PrintWebViewHelper::PrintPreviewContext::IsLastPageOfPrintReadyMetafile() |
1772 const { | 1795 const { |
1773 DCHECK(IsRendering()); | 1796 DCHECK(IsRendering()); |
1774 return current_page_index_ == print_ready_metafile_page_count_; | 1797 return current_page_index_ == print_ready_metafile_page_count_; |
1775 } | 1798 } |
1776 | 1799 |
1777 bool PrintWebViewHelper::PrintPreviewContext::IsFinalPageRendered() const { | 1800 bool PrintWebViewHelper::PrintPreviewContext::IsFinalPageRendered() const { |
1778 DCHECK(IsRendering()); | 1801 DCHECK(IsRendering()); |
1779 return static_cast<size_t>(current_page_index_) == pages_to_render_.size(); | 1802 return static_cast<size_t>(current_page_index_) == pages_to_render_.size(); |
1780 } | 1803 } |
1781 | 1804 |
1782 void PrintWebViewHelper::PrintPreviewContext::set_generate_draft_pages( | 1805 void PrintWebViewHelper::PrintPreviewContext::set_generate_draft_pages( |
1783 bool generate_draft_pages) { | 1806 bool generate_draft_pages) { |
1784 DCHECK_EQ(INITIALIZED, state_); | 1807 DCHECK_EQ(INITIALIZED, state_); |
1785 generate_draft_pages_ = generate_draft_pages; | 1808 generate_draft_pages_ = generate_draft_pages; |
1786 } | 1809 } |
1787 | 1810 |
1788 void PrintWebViewHelper::PrintPreviewContext::set_error( | 1811 void PrintWebViewHelper::PrintPreviewContext::set_error( |
1789 enum PrintPreviewErrorBuckets error) { | 1812 enum PrintPreviewErrorBuckets error) { |
1790 error_ = error; | 1813 error_ = error; |
1791 } | 1814 } |
1792 | 1815 |
1793 WebKit::WebFrame* PrintWebViewHelper::PrintPreviewContext::frame() { | 1816 WebKit::WebFrame* PrintWebViewHelper::PrintPreviewContext::source_frame() { |
1794 // TODO(thestig) turn this back into a DCHECK when http://crbug.com/118303 is | 1817 // TODO(thestig) turn this back into a DCHECK when http://crbug.com/118303 is |
1795 // resolved. | 1818 // resolved. |
1796 CHECK(state_ != UNINITIALIZED); | 1819 CHECK(state_ != UNINITIALIZED); |
1797 return frame_; | 1820 return source_frame_; |
1798 } | 1821 } |
1799 | 1822 |
1800 const WebKit::WebNode& PrintWebViewHelper::PrintPreviewContext::node() const { | 1823 const WebKit::WebNode& |
| 1824 PrintWebViewHelper::PrintPreviewContext::source_node() const { |
1801 DCHECK(state_ != UNINITIALIZED); | 1825 DCHECK(state_ != UNINITIALIZED); |
1802 return node_; | 1826 return source_node_; |
| 1827 } |
| 1828 |
| 1829 WebKit::WebFrame* PrintWebViewHelper::PrintPreviewContext::prepared_frame() { |
| 1830 // TODO(thestig) turn this back into a DCHECK when http://crbug.com/118303 is |
| 1831 // resolved. |
| 1832 CHECK(state_ != UNINITIALIZED); |
| 1833 return prep_frame_view_->frame(); |
| 1834 } |
| 1835 |
| 1836 const WebKit::WebNode& |
| 1837 PrintWebViewHelper::PrintPreviewContext::prepared_node() const { |
| 1838 DCHECK(state_ != UNINITIALIZED); |
| 1839 return prep_frame_view_->node(); |
1803 } | 1840 } |
1804 | 1841 |
1805 int PrintWebViewHelper::PrintPreviewContext::total_page_count() const { | 1842 int PrintWebViewHelper::PrintPreviewContext::total_page_count() const { |
1806 DCHECK(state_ != UNINITIALIZED); | 1843 DCHECK(state_ != UNINITIALIZED); |
1807 return total_page_count_; | 1844 return total_page_count_; |
1808 } | 1845 } |
1809 | 1846 |
1810 bool PrintWebViewHelper::PrintPreviewContext::generate_draft_pages() const { | 1847 bool PrintWebViewHelper::PrintPreviewContext::generate_draft_pages() const { |
1811 return generate_draft_pages_; | 1848 return generate_draft_pages_; |
1812 } | 1849 } |
(...skipping 13 matching lines...) Expand all Loading... |
1826 } | 1863 } |
1827 | 1864 |
1828 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { | 1865 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
1829 prep_frame_view_.reset(); | 1866 prep_frame_view_.reset(); |
1830 metafile_.reset(); | 1867 metafile_.reset(); |
1831 pages_to_render_.clear(); | 1868 pages_to_render_.clear(); |
1832 error_ = PREVIEW_ERROR_NONE; | 1869 error_ = PREVIEW_ERROR_NONE; |
1833 } | 1870 } |
1834 | 1871 |
1835 } // namespace printing | 1872 } // namespace printing |
OLD | NEW |