Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(213)

Side by Side Diff: chrome/renderer/print_web_view_helper.cc

Issue 7831041: Fix print preview workflow to reflect settings of selected printer. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Update per code review Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/renderer/print_web_view_helper.h ('k') | printing/print_settings_initializer_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/print_web_view_helper.h" 5 #include "chrome/renderer/print_web_view_helper.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 550 matching lines...) Expand 10 before | Expand all | Expand 10 after
561 561
562 WebDocument document = main_frame->document(); 562 WebDocument document = main_frame->document();
563 // <object> with id="pdf-viewer" is created in 563 // <object> with id="pdf-viewer" is created in
564 // chrome/browser/resources/print_preview/print_preview.js 564 // chrome/browser/resources/print_preview/print_preview.js
565 WebElement pdf_element = document.getElementById("pdf-viewer"); 565 WebElement pdf_element = document.getElementById("pdf-viewer");
566 if (pdf_element.isNull()) { 566 if (pdf_element.isNull()) {
567 NOTREACHED(); 567 NOTREACHED();
568 return; 568 return;
569 } 569 }
570 570
571 WebFrame* pdf_frame = pdf_element.document().frame();
572 scoped_ptr<PrepareFrameAndViewForPrint> prepare;
573 if (!InitPrintSettingsAndPrepareFrame(pdf_frame, &pdf_element, &prepare)) {
574 LOG(ERROR) << "Failed to initialize print page settings";
575 return;
576 }
577
578 if (!UpdatePrintSettings(job_settings, false)) { 571 if (!UpdatePrintSettings(job_settings, false)) {
579 LOG(ERROR) << "UpdatePrintSettings failed"; 572 LOG(ERROR) << "UpdatePrintSettings failed";
580 DidFinishPrinting(FAIL_PRINT); 573 DidFinishPrinting(FAIL_PRINT);
581 return; 574 return;
582 } 575 }
583 576
577 WebFrame* pdf_frame = pdf_element.document().frame();
578 scoped_ptr<PrepareFrameAndViewForPrint> prepare;
579 prepare.reset(new PrepareFrameAndViewForPrint(print_pages_params_->params,
580 pdf_frame, &pdf_element));
581 UpdatePrintableSizeInPrintParameters(pdf_frame, &pdf_element, prepare.get(),
582 &print_pages_params_->params);
583
584 // Render Pages for printing. 584 // Render Pages for printing.
585 if (!RenderPagesForPrint(pdf_frame, &pdf_element, prepare.get())) { 585 if (!RenderPagesForPrint(pdf_frame, &pdf_element, prepare.get())) {
586 LOG(ERROR) << "RenderPagesForPrint failed"; 586 LOG(ERROR) << "RenderPagesForPrint failed";
587 DidFinishPrinting(FAIL_PRINT); 587 DidFinishPrinting(FAIL_PRINT);
588 } 588 }
589 } 589 }
590 590
591 bool PrintWebViewHelper::GetPrintFrame(WebKit::WebFrame** frame) { 591 bool PrintWebViewHelper::GetPrintFrame(WebKit::WebFrame** frame) {
592 DCHECK(frame); 592 DCHECK(frame);
593 DCHECK(render_view()->webview()); 593 DCHECK(render_view()->webview());
(...skipping 29 matching lines...) Expand all
623 // copy when the print preview tab closes. 623 // copy when the print preview tab closes.
624 WebNode duplicate_node(*node); 624 WebNode duplicate_node(*node);
625 Print(frame, &duplicate_node); 625 Print(frame, &duplicate_node);
626 } 626 }
627 } 627 }
628 628
629 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { 629 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) {
630 DCHECK(is_preview_); 630 DCHECK(is_preview_);
631 print_preview_context_.OnPrintPreview(); 631 print_preview_context_.OnPrintPreview();
632 632
633 if (!InitPrintSettings(print_preview_context_.frame(),
634 print_preview_context_.node(),
635 true)) {
636 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings(
637 routing_id(),
638 print_pages_params_->params.document_cookie));
639 return;
640 }
641
642 if (!UpdatePrintSettings(settings, true)) { 633 if (!UpdatePrintSettings(settings, true)) {
643 LOG(ERROR) << "UpdatePrintSettings failed"; 634 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) {
635 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings(
636 routing_id(), print_pages_params_->params.document_cookie));
637 notify_browser_of_print_failure_ = false; // Already sent.
638 }
644 DidFinishPrinting(FAIL_PREVIEW); 639 DidFinishPrinting(FAIL_PREVIEW);
645 return; 640 return;
646 } 641 }
647 642
648 if (!print_pages_params_->params.is_first_request && 643 if (!print_pages_params_->params.is_first_request &&
649 old_print_pages_params_.get() && 644 old_print_pages_params_.get() &&
650 PrintMsg_Print_Params_IsEqual(*old_print_pages_params_, 645 PrintMsg_Print_Params_IsEqual(*old_print_pages_params_,
651 *print_pages_params_)) { 646 *print_pages_params_)) {
652 PrintHostMsg_DidPreviewDocument_Params preview_params; 647 PrintHostMsg_DidPreviewDocument_Params preview_params;
653 preview_params.reuse_existing_data = true; 648 preview_params.reuse_existing_data = true;
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
798 } 793 }
799 } 794 }
800 795
801 void PrintWebViewHelper::Print(WebKit::WebFrame* frame, WebKit::WebNode* node) { 796 void PrintWebViewHelper::Print(WebKit::WebFrame* frame, WebKit::WebNode* node) {
802 // If still not finished with earlier print request simply ignore. 797 // If still not finished with earlier print request simply ignore.
803 if (print_web_view_) 798 if (print_web_view_)
804 return; 799 return;
805 800
806 // Initialize print settings. 801 // Initialize print settings.
807 scoped_ptr<PrepareFrameAndViewForPrint> prepare; 802 scoped_ptr<PrepareFrameAndViewForPrint> prepare;
808 if (!InitPrintSettingsAndPrepareFrame(frame, node, &prepare)) 803 if (!InitPrintSettingsAndPrepareFrame(frame, node, &prepare)) {
804 DidFinishPrinting(FAIL_PRINT);
809 return; // Failed to init print page settings. 805 return; // Failed to init print page settings.
806 }
810 807
811 int expected_page_count = 0; 808 int expected_page_count = 0;
812 bool use_browser_overlays = true; 809 bool use_browser_overlays = true;
813 810
814 expected_page_count = prepare->GetExpectedPageCount(); 811 expected_page_count = prepare->GetExpectedPageCount();
815 if (expected_page_count) 812 if (expected_page_count)
816 use_browser_overlays = prepare->ShouldUseBrowserOverlays(); 813 use_browser_overlays = prepare->ShouldUseBrowserOverlays();
817 814
818 // Release the prepare before going any further, since we are going to 815 // Release the prepare before going any further, since we are going to
819 // show UI and wait for the user. 816 // show UI and wait for the user.
(...skipping 25 matching lines...) Expand all
845 if (result == FAIL_PRINT) { 842 if (result == FAIL_PRINT) {
846 DisplayPrintJobError(); 843 DisplayPrintJobError();
847 844
848 if (notify_browser_of_print_failure_ && print_pages_params_.get()) { 845 if (notify_browser_of_print_failure_ && print_pages_params_.get()) {
849 int cookie = print_pages_params_->params.document_cookie; 846 int cookie = print_pages_params_->params.document_cookie;
850 Send(new PrintHostMsg_PrintingFailed(routing_id(), cookie)); 847 Send(new PrintHostMsg_PrintingFailed(routing_id(), cookie));
851 } 848 }
852 } else if (result == FAIL_PREVIEW) { 849 } else if (result == FAIL_PREVIEW) {
853 DCHECK(is_preview_); 850 DCHECK(is_preview_);
854 store_print_pages_params = false; 851 store_print_pages_params = false;
855 int cookie = print_pages_params_->params.document_cookie; 852 int cookie = print_pages_params_.get() ?
853 print_pages_params_->params.document_cookie : 0;
856 if (notify_browser_of_print_failure_) 854 if (notify_browser_of_print_failure_)
857 Send(new PrintHostMsg_PrintPreviewFailed(routing_id(), cookie)); 855 Send(new PrintHostMsg_PrintPreviewFailed(routing_id(), cookie));
858 else 856 else
859 Send(new PrintHostMsg_PrintPreviewCancelled(routing_id(), cookie)); 857 Send(new PrintHostMsg_PrintPreviewCancelled(routing_id(), cookie));
860 print_preview_context_.Failed(notify_browser_of_print_failure_); 858 print_preview_context_.Failed(notify_browser_of_print_failure_);
861 } 859 }
862 860
863 if (print_web_view_) { 861 if (print_web_view_) {
864 print_web_view_->close(); 862 print_web_view_->close();
865 print_web_view_ = NULL; 863 print_web_view_ = NULL;
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
1048 page_height_in_points, printing::kPointsPerInch, dpi))); 1046 page_height_in_points, printing::kPointsPerInch, dpi)));
1049 1047
1050 params->margin_top = static_cast<int>(ConvertUnitDouble( 1048 params->margin_top = static_cast<int>(ConvertUnitDouble(
1051 page_layout_in_points.margin_top, printing::kPointsPerInch, dpi)); 1049 page_layout_in_points.margin_top, printing::kPointsPerInch, dpi));
1052 params->margin_left = static_cast<int>(ConvertUnitDouble( 1050 params->margin_left = static_cast<int>(ConvertUnitDouble(
1053 page_layout_in_points.margin_left, printing::kPointsPerInch, dpi)); 1051 page_layout_in_points.margin_left, printing::kPointsPerInch, dpi));
1054 1052
1055 prepare->UpdatePrintParams(*params); 1053 prepare->UpdatePrintParams(*params);
1056 } 1054 }
1057 1055
1058 bool PrintWebViewHelper::InitPrintSettings(WebKit::WebFrame* frame, 1056 bool PrintWebViewHelper::InitPrintSettings(WebKit::WebFrame* frame) {
1059 WebKit::WebNode* node,
1060 bool is_preview) {
1061 DCHECK(frame); 1057 DCHECK(frame);
1062 PrintMsg_PrintPages_Params settings; 1058 PrintMsg_PrintPages_Params settings;
1063 1059
1064 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(), 1060 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(),
1065 &settings.params)); 1061 &settings.params));
1066 // Check if the printer returned any settings, if the settings is empty, we 1062 // Check if the printer returned any settings, if the settings is empty, we
1067 // can safely assume there are no printer drivers configured. So we safely 1063 // can safely assume there are no printer drivers configured. So we safely
1068 // terminate. 1064 // terminate.
1069 bool result = true; 1065 bool result = true;
1070 if (PrintMsg_Print_Params_IsEmpty(settings.params)) { 1066 if (PrintMsg_Print_Params_IsEmpty(settings.params)) {
1071 if (!is_preview) { 1067 render_view()->runModalAlertDialog(
1072 render_view()->runModalAlertDialog( 1068 frame,
1073 frame, 1069 l10n_util::GetStringUTF16(
1074 l10n_util::GetStringUTF16( 1070 IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS));
1075 IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS));
1076 }
1077 result = false; 1071 result = false;
1078 } 1072 }
1079 1073
1080 if (result && 1074 if (result &&
1081 (settings.params.dpi < kMinDpi || settings.params.document_cookie == 0)) { 1075 (settings.params.dpi < kMinDpi || settings.params.document_cookie == 0)) {
1082 // Invalid print page settings. 1076 // Invalid print page settings.
1083 NOTREACHED(); 1077 NOTREACHED();
1084 result = false; 1078 result = false;
1085 } 1079 }
1086 1080
1087 settings.pages.clear(); 1081 settings.pages.clear();
1088 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); 1082 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings));
1089 return result; 1083 return result;
1090 } 1084 }
1091 1085
1092 bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame( 1086 bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame(
1093 WebKit::WebFrame* frame, WebKit::WebNode* node, 1087 WebKit::WebFrame* frame, WebKit::WebNode* node,
1094 scoped_ptr<PrepareFrameAndViewForPrint>* prepare) { 1088 scoped_ptr<PrepareFrameAndViewForPrint>* prepare) {
1095 if (!InitPrintSettings(frame, node, false)) 1089 if (!InitPrintSettings(frame))
1096 return false; 1090 return false;
1097 1091
1098 DCHECK(!prepare->get()); 1092 DCHECK(!prepare->get());
1099 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, 1093 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params,
1100 frame, node)); 1094 frame, node));
1101 UpdatePrintableSizeInPrintParameters(frame, node, prepare->get(), 1095 UpdatePrintableSizeInPrintParameters(frame, node, prepare->get(),
1102 &print_pages_params_->params); 1096 &print_pages_params_->params);
1103 Send(new PrintHostMsg_DidGetDocumentCookie( 1097 Send(new PrintHostMsg_DidGetDocumentCookie(
1104 routing_id(), print_pages_params_->params.document_cookie)); 1098 routing_id(), print_pages_params_->params.document_cookie));
1105 return true; 1099 return true;
1106 } 1100 }
1107 1101
1108 bool PrintWebViewHelper::UpdatePrintSettings( 1102 bool PrintWebViewHelper::UpdatePrintSettings(
1109 const DictionaryValue& job_settings, bool is_preview) { 1103 const DictionaryValue& job_settings, bool is_preview) {
1104 if (is_preview && job_settings.empty()) {
kmadhusu 2011/09/20 00:42:41 In the preview printing workflow, we still want to
arthurhsu 2011/09/20 18:41:19 Yes, but it shall send back BAD_SETTINGS instead o
1105 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING);
1106 return false;
1107 }
1108
1109 // Send the cookie so that UpdatePrintSettings can reuse PrintQuery when
1110 // possible.
1111 int cookie = print_pages_params_.get() ?
1112 print_pages_params_->params.document_cookie : 0;
1110 PrintMsg_PrintPages_Params settings; 1113 PrintMsg_PrintPages_Params settings;
1114 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(),
1115 cookie, job_settings, &settings));
1111 1116
1112 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), 1117 // Setup an empty default param so that document cookie can be propagated
1113 print_pages_params_->params.document_cookie, job_settings, &settings)); 1118 // in case of error.
1119 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings));
kmadhusu 2011/09/20 00:42:41 Do you still need the code in line 1119?
arthurhsu 2011/09/20 18:41:19 Removed misleading comments. Basically the page p
1120
1121 if (PrintMsg_Print_Params_IsEmpty(settings.params)) {
1122 if (!is_preview) {
1123 WebKit::WebFrame* frame = print_preview_context_.frame();
1124 if (!frame)
1125 GetPrintFrame(&frame);
1126 DCHECK(frame);
1127 render_view()->runModalAlertDialog(
1128 frame,
1129 l10n_util::GetStringUTF16(
1130 IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS));
1131 } else {
1132 print_preview_context_.set_error(PREVIEW_ERROR_INVALID_PRINTER_SETTINGS);
1133 }
1134 return false;
1135 }
1136
1114 if (settings.params.dpi < kMinDpi || !settings.params.document_cookie) { 1137 if (settings.params.dpi < kMinDpi || !settings.params.document_cookie) {
1115 print_preview_context_.set_error(PREVIEW_ERROR_UPDATING_PRINT_SETTINGS); 1138 print_preview_context_.set_error(PREVIEW_ERROR_UPDATING_PRINT_SETTINGS);
1116 return false; 1139 return false;
1117 } 1140 }
1118 1141
1119 if (is_preview) { 1142 if (is_preview) {
1120 // Validate expected print preview settings. 1143 // Validate expected print preview settings.
1121 if (!job_settings.GetString(printing::kPreviewUIAddr, 1144 if (!job_settings.GetString(printing::kPreviewUIAddr,
1122 &(settings.params.preview_ui_addr)) || 1145 &(settings.params.preview_ui_addr)) ||
1123 !job_settings.GetInteger(printing::kPreviewRequestID, 1146 !job_settings.GetInteger(printing::kPreviewRequestID,
(...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after
1539 printing::PreviewMetafile* 1562 printing::PreviewMetafile*
1540 PrintWebViewHelper::PrintPreviewContext::metafile() const { 1563 PrintWebViewHelper::PrintPreviewContext::metafile() const {
1541 return metafile_.get(); 1564 return metafile_.get();
1542 } 1565 }
1543 1566
1544 const PrintMsg_Print_Params& 1567 const PrintMsg_Print_Params&
1545 PrintWebViewHelper::PrintPreviewContext::print_params() const { 1568 PrintWebViewHelper::PrintPreviewContext::print_params() const {
1546 return *print_params_; 1569 return *print_params_;
1547 } 1570 }
1548 1571
1572 int PrintWebViewHelper::PrintPreviewContext::last_error() const {
1573 return error_;
1574 }
1575
1549 const gfx::Size& 1576 const gfx::Size&
1550 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { 1577 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const {
1551 return prep_frame_view_->GetPrintCanvasSize(); 1578 return prep_frame_view_->GetPrintCanvasSize();
1552 } 1579 }
1553 1580
1554 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { 1581 void PrintWebViewHelper::PrintPreviewContext::ClearContext() {
1555 prep_frame_view_.reset(); 1582 prep_frame_view_.reset();
1556 metafile_.reset(); 1583 metafile_.reset();
1557 pages_to_render_.clear(); 1584 pages_to_render_.clear();
1558 error_ = PREVIEW_ERROR_NONE; 1585 error_ = PREVIEW_ERROR_NONE;
1559 } 1586 }
OLDNEW
« no previous file with comments | « chrome/renderer/print_web_view_helper.h ('k') | printing/print_settings_initializer_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698