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

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
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 549 matching lines...) Expand 10 before | Expand all | Expand 10 after
560 560
561 WebDocument document = main_frame->document(); 561 WebDocument document = main_frame->document();
562 // <object> with id="pdf-viewer" is created in 562 // <object> with id="pdf-viewer" is created in
563 // chrome/browser/resources/print_preview/print_preview.js 563 // chrome/browser/resources/print_preview/print_preview.js
564 WebElement pdf_element = document.getElementById("pdf-viewer"); 564 WebElement pdf_element = document.getElementById("pdf-viewer");
565 if (pdf_element.isNull()) { 565 if (pdf_element.isNull()) {
566 NOTREACHED(); 566 NOTREACHED();
567 return; 567 return;
568 } 568 }
569 569
570 WebFrame* pdf_frame = pdf_element.document().frame();
571 scoped_ptr<PrepareFrameAndViewForPrint> prepare;
572 if (!InitPrintSettingsAndPrepareFrame(pdf_frame, &pdf_element, &prepare)) {
573 LOG(ERROR) << "Failed to initialize print page settings";
574 return;
575 }
576
577 if (!UpdatePrintSettings(job_settings, false)) { 570 if (!UpdatePrintSettings(job_settings, false)) {
578 LOG(ERROR) << "UpdatePrintSettings failed"; 571 LOG(ERROR) << "UpdatePrintSettings failed";
579 DidFinishPrinting(FAIL_PRINT); 572 DidFinishPrinting(FAIL_PRINT);
580 return; 573 return;
581 } 574 }
582 575
576 WebFrame* pdf_frame = pdf_element.document().frame();
577 scoped_ptr<PrepareFrameAndViewForPrint> prepare;
578 prepare.reset(new PrepareFrameAndViewForPrint(print_pages_params_->params,
579 pdf_frame, &pdf_element));
580 UpdatePrintableSizeInPrintParameters(pdf_frame, &pdf_element, prepare.get(),
581 &print_pages_params_->params);
582
583 // Render Pages for printing. 583 // Render Pages for printing.
584 if (!RenderPagesForPrint(pdf_frame, &pdf_element, prepare.get())) { 584 if (!RenderPagesForPrint(pdf_frame, &pdf_element, prepare.get())) {
585 LOG(ERROR) << "RenderPagesForPrint failed"; 585 LOG(ERROR) << "RenderPagesForPrint failed";
586 DidFinishPrinting(FAIL_PRINT); 586 DidFinishPrinting(FAIL_PRINT);
587 } 587 }
588 } 588 }
589 589
590 bool PrintWebViewHelper::GetPrintFrame(WebKit::WebFrame** frame) { 590 bool PrintWebViewHelper::GetPrintFrame(WebKit::WebFrame** frame) {
591 DCHECK(frame); 591 DCHECK(frame);
592 DCHECK(render_view()->webview()); 592 DCHECK(render_view()->webview());
(...skipping 29 matching lines...) Expand all
622 // copy when the print preview tab closes. 622 // copy when the print preview tab closes.
623 WebNode duplicate_node(*node); 623 WebNode duplicate_node(*node);
624 Print(frame, &duplicate_node); 624 Print(frame, &duplicate_node);
625 } 625 }
626 } 626 }
627 627
628 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { 628 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) {
629 DCHECK(is_preview_); 629 DCHECK(is_preview_);
630 print_preview_context_.OnPrintPreview(); 630 print_preview_context_.OnPrintPreview();
631 631
632 if (!InitPrintSettings(print_preview_context_.frame(),
633 print_preview_context_.node(),
634 true)) {
635 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings(
636 routing_id(),
637 print_pages_params_->params.document_cookie));
638 return;
639 }
640
641 if (!UpdatePrintSettings(settings, true)) { 632 if (!UpdatePrintSettings(settings, true)) {
642 LOG(ERROR) << "UpdatePrintSettings failed"; 633 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) {
634 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings(
635 routing_id(), print_pages_params_->params.document_cookie));
636 notify_browser_of_print_failure_ = false; // Already sent.
637 }
643 DidFinishPrinting(FAIL_PREVIEW); 638 DidFinishPrinting(FAIL_PREVIEW);
644 return; 639 return;
645 } 640 }
646 641
647 if (!print_pages_params_->params.is_first_request && 642 if (!print_pages_params_->params.is_first_request &&
648 old_print_pages_params_.get() && 643 old_print_pages_params_.get() &&
649 PrintMsg_Print_Params_IsEqual(*old_print_pages_params_, 644 PrintMsg_Print_Params_IsEqual(*old_print_pages_params_,
650 *print_pages_params_)) { 645 *print_pages_params_)) {
651 PrintHostMsg_DidPreviewDocument_Params preview_params; 646 PrintHostMsg_DidPreviewDocument_Params preview_params;
652 preview_params.reuse_existing_data = true; 647 preview_params.reuse_existing_data = true;
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
797 } 792 }
798 } 793 }
799 794
800 void PrintWebViewHelper::Print(WebKit::WebFrame* frame, WebKit::WebNode* node) { 795 void PrintWebViewHelper::Print(WebKit::WebFrame* frame, WebKit::WebNode* node) {
801 // If still not finished with earlier print request simply ignore. 796 // If still not finished with earlier print request simply ignore.
802 if (print_web_view_) 797 if (print_web_view_)
803 return; 798 return;
804 799
805 // Initialize print settings. 800 // Initialize print settings.
806 scoped_ptr<PrepareFrameAndViewForPrint> prepare; 801 scoped_ptr<PrepareFrameAndViewForPrint> prepare;
807 if (!InitPrintSettingsAndPrepareFrame(frame, node, &prepare)) 802 if (!InitPrintSettingsAndPrepareFrame(frame, node, &prepare)) {
803 DidFinishPrinting(FAIL_PRINT);
808 return; // Failed to init print page settings. 804 return; // Failed to init print page settings.
805 }
809 806
810 int expected_page_count = 0; 807 int expected_page_count = 0;
811 bool use_browser_overlays = true; 808 bool use_browser_overlays = true;
812 809
813 expected_page_count = prepare->GetExpectedPageCount(); 810 expected_page_count = prepare->GetExpectedPageCount();
814 if (expected_page_count) 811 if (expected_page_count)
815 use_browser_overlays = prepare->ShouldUseBrowserOverlays(); 812 use_browser_overlays = prepare->ShouldUseBrowserOverlays();
816 813
817 // Release the prepare before going any further, since we are going to 814 // Release the prepare before going any further, since we are going to
818 // show UI and wait for the user. 815 // show UI and wait for the user.
(...skipping 25 matching lines...) Expand all
844 if (result == FAIL_PRINT) { 841 if (result == FAIL_PRINT) {
845 DisplayPrintJobError(); 842 DisplayPrintJobError();
846 843
847 if (notify_browser_of_print_failure_ && print_pages_params_.get()) { 844 if (notify_browser_of_print_failure_ && print_pages_params_.get()) {
848 int cookie = print_pages_params_->params.document_cookie; 845 int cookie = print_pages_params_->params.document_cookie;
849 Send(new PrintHostMsg_PrintingFailed(routing_id(), cookie)); 846 Send(new PrintHostMsg_PrintingFailed(routing_id(), cookie));
850 } 847 }
851 } else if (result == FAIL_PREVIEW) { 848 } else if (result == FAIL_PREVIEW) {
852 DCHECK(is_preview_); 849 DCHECK(is_preview_);
853 store_print_pages_params = false; 850 store_print_pages_params = false;
854 int cookie = print_pages_params_->params.document_cookie; 851 int cookie = print_pages_params_.get() ?
852 print_pages_params_->params.document_cookie : 0;
855 if (notify_browser_of_print_failure_) 853 if (notify_browser_of_print_failure_)
856 Send(new PrintHostMsg_PrintPreviewFailed(routing_id(), cookie)); 854 Send(new PrintHostMsg_PrintPreviewFailed(routing_id(), cookie));
857 else 855 else
858 Send(new PrintHostMsg_PrintPreviewCancelled(routing_id(), cookie)); 856 Send(new PrintHostMsg_PrintPreviewCancelled(routing_id(), cookie));
859 print_preview_context_.Failed(notify_browser_of_print_failure_); 857 print_preview_context_.Failed(notify_browser_of_print_failure_);
860 } 858 }
861 859
862 if (print_web_view_) { 860 if (print_web_view_) {
863 print_web_view_->close(); 861 print_web_view_->close();
864 print_web_view_ = NULL; 862 print_web_view_ = NULL;
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
1047 page_height_in_points, printing::kPointsPerInch, dpi))); 1045 page_height_in_points, printing::kPointsPerInch, dpi)));
1048 1046
1049 params->margin_top = static_cast<int>(ConvertUnitDouble( 1047 params->margin_top = static_cast<int>(ConvertUnitDouble(
1050 page_layout_in_points.margin_top, printing::kPointsPerInch, dpi)); 1048 page_layout_in_points.margin_top, printing::kPointsPerInch, dpi));
1051 params->margin_left = static_cast<int>(ConvertUnitDouble( 1049 params->margin_left = static_cast<int>(ConvertUnitDouble(
1052 page_layout_in_points.margin_left, printing::kPointsPerInch, dpi)); 1050 page_layout_in_points.margin_left, printing::kPointsPerInch, dpi));
1053 1051
1054 prepare->UpdatePrintParams(*params); 1052 prepare->UpdatePrintParams(*params);
1055 } 1053 }
1056 1054
1057 bool PrintWebViewHelper::InitPrintSettings(WebKit::WebFrame* frame, 1055 bool PrintWebViewHelper::InitPrintSettings(WebKit::WebFrame* frame) {
1058 WebKit::WebNode* node,
1059 bool is_preview) {
1060 DCHECK(frame); 1056 DCHECK(frame);
1061 PrintMsg_PrintPages_Params settings; 1057 PrintMsg_PrintPages_Params settings;
1062 1058
1063 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(), 1059 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(),
1064 &settings.params)); 1060 &settings.params));
1065 // Check if the printer returned any settings, if the settings is empty, we 1061 // Check if the printer returned any settings, if the settings is empty, we
1066 // can safely assume there are no printer drivers configured. So we safely 1062 // can safely assume there are no printer drivers configured. So we safely
1067 // terminate. 1063 // terminate.
1068 bool result = true; 1064 bool result = true;
1069 if (PrintMsg_Print_Params_IsEmpty(settings.params)) { 1065 if (PrintMsg_Print_Params_IsEmpty(settings.params)) {
1070 if (!is_preview) { 1066 render_view()->runModalAlertDialog(
1071 render_view()->runModalAlertDialog( 1067 frame,
1072 frame, 1068 l10n_util::GetStringUTF16(
1073 l10n_util::GetStringUTF16( 1069 IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS));
1074 IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS));
1075 }
1076 result = false; 1070 result = false;
1077 } 1071 }
1078 1072
1079 if (result && 1073 if (result &&
1080 (settings.params.dpi < kMinDpi || settings.params.document_cookie == 0)) { 1074 (settings.params.dpi < kMinDpi || settings.params.document_cookie == 0)) {
1081 // Invalid print page settings. 1075 // Invalid print page settings.
1082 NOTREACHED(); 1076 NOTREACHED();
1083 result = false; 1077 result = false;
1084 } 1078 }
1085 1079
1086 settings.pages.clear(); 1080 settings.pages.clear();
1087 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); 1081 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings));
1088 return result; 1082 return result;
1089 } 1083 }
1090 1084
1091 bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame( 1085 bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame(
1092 WebKit::WebFrame* frame, WebKit::WebNode* node, 1086 WebKit::WebFrame* frame, WebKit::WebNode* node,
1093 scoped_ptr<PrepareFrameAndViewForPrint>* prepare) { 1087 scoped_ptr<PrepareFrameAndViewForPrint>* prepare) {
1094 if (!InitPrintSettings(frame, node, false)) 1088 if (!InitPrintSettings(frame))
1095 return false; 1089 return false;
1096 1090
1097 DCHECK(!prepare->get()); 1091 DCHECK(!prepare->get());
1098 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, 1092 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params,
1099 frame, node)); 1093 frame, node));
1100 UpdatePrintableSizeInPrintParameters(frame, node, prepare->get(), 1094 UpdatePrintableSizeInPrintParameters(frame, node, prepare->get(),
1101 &print_pages_params_->params); 1095 &print_pages_params_->params);
1102 Send(new PrintHostMsg_DidGetDocumentCookie( 1096 Send(new PrintHostMsg_DidGetDocumentCookie(
1103 routing_id(), print_pages_params_->params.document_cookie)); 1097 routing_id(), print_pages_params_->params.document_cookie));
1104 return true; 1098 return true;
1105 } 1099 }
1106 1100
1107 bool PrintWebViewHelper::UpdatePrintSettings( 1101 bool PrintWebViewHelper::UpdatePrintSettings(
1108 const DictionaryValue& job_settings, bool is_preview) { 1102 const DictionaryValue& job_settings, bool is_preview) {
1103 if (job_settings.empty()) {
1104 if (is_preview)
1105 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING);
1106 return false;
1107 }
1108
1109 // Send the cookie so that UpdatePrintSettings can reuse PrinterQuery when
1110 // possible.
1111 int cookie = print_pages_params_.get() ?
1112 print_pages_params_->params.document_cookie : 0;
1109 PrintMsg_PrintPages_Params settings; 1113 PrintMsg_PrintPages_Params settings;
1114 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(),
1115 cookie, job_settings, &settings));
1116 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings));
1110 1117
1111 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), 1118 if (PrintMsg_Print_Params_IsEmpty(settings.params)) {
1112 print_pages_params_->params.document_cookie, job_settings, &settings)); 1119 if (is_preview) {
1120 print_preview_context_.set_error(PREVIEW_ERROR_INVALID_PRINTER_SETTINGS);
1121 } else {
1122 WebKit::WebFrame* frame = print_preview_context_.frame();
1123 if (!frame) {
1124 GetPrintFrame(&frame);
1125 }
1126 if (frame) {
1127 render_view()->runModalAlertDialog(
1128 frame,
1129 l10n_util::GetStringUTF16(
1130 IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS));
1131 }
1132 }
1133 return false;
1134 }
1135
1113 if (settings.params.dpi < kMinDpi || !settings.params.document_cookie) { 1136 if (settings.params.dpi < kMinDpi || !settings.params.document_cookie) {
1114 print_preview_context_.set_error(PREVIEW_ERROR_UPDATING_PRINT_SETTINGS); 1137 print_preview_context_.set_error(PREVIEW_ERROR_UPDATING_PRINT_SETTINGS);
1115 return false; 1138 return false;
1116 } 1139 }
1117 1140
1118 if (is_preview) { 1141 if (is_preview) {
1119 // Validate expected print preview settings. 1142 // Validate expected print preview settings.
1120 if (!job_settings.GetString(printing::kPreviewUIAddr, 1143 if (!job_settings.GetString(printing::kPreviewUIAddr,
1121 &(settings.params.preview_ui_addr)) || 1144 &(settings.params.preview_ui_addr)) ||
1122 !job_settings.GetInteger(printing::kPreviewRequestID, 1145 !job_settings.GetInteger(printing::kPreviewRequestID,
(...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after
1538 printing::PreviewMetafile* 1561 printing::PreviewMetafile*
1539 PrintWebViewHelper::PrintPreviewContext::metafile() const { 1562 PrintWebViewHelper::PrintPreviewContext::metafile() const {
1540 return metafile_.get(); 1563 return metafile_.get();
1541 } 1564 }
1542 1565
1543 const PrintMsg_Print_Params& 1566 const PrintMsg_Print_Params&
1544 PrintWebViewHelper::PrintPreviewContext::print_params() const { 1567 PrintWebViewHelper::PrintPreviewContext::print_params() const {
1545 return *print_params_; 1568 return *print_params_;
1546 } 1569 }
1547 1570
1571 int PrintWebViewHelper::PrintPreviewContext::last_error() const {
1572 return error_;
1573 }
1574
1548 const gfx::Size& 1575 const gfx::Size&
1549 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { 1576 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const {
1550 return prep_frame_view_->GetPrintCanvasSize(); 1577 return prep_frame_view_->GetPrintCanvasSize();
1551 } 1578 }
1552 1579
1553 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { 1580 void PrintWebViewHelper::PrintPreviewContext::ClearContext() {
1554 prep_frame_view_.reset(); 1581 prep_frame_view_.reset();
1555 metafile_.reset(); 1582 metafile_.reset();
1556 pages_to_render_.clear(); 1583 pages_to_render_.clear();
1557 error_ = PREVIEW_ERROR_NONE; 1584 error_ = PREVIEW_ERROR_NONE;
1558 } 1585 }
OLDNEW
« no previous file with comments | « chrome/renderer/print_web_view_helper.h ('k') | chrome/renderer/print_web_view_helper_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698