OLD | NEW |
---|---|
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 Loading... | |
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 Loading... | |
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 Loading... | |
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 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1048 page_height_in_points, printing::kPointsPerInch, dpi))); | 1045 page_height_in_points, printing::kPointsPerInch, dpi))); |
1049 | 1046 |
1050 params->margin_top = static_cast<int>(ConvertUnitDouble( | 1047 params->margin_top = static_cast<int>(ConvertUnitDouble( |
1051 page_layout_in_points.margin_top, printing::kPointsPerInch, dpi)); | 1048 page_layout_in_points.margin_top, printing::kPointsPerInch, dpi)); |
1052 params->margin_left = static_cast<int>(ConvertUnitDouble( | 1049 params->margin_left = static_cast<int>(ConvertUnitDouble( |
1053 page_layout_in_points.margin_left, printing::kPointsPerInch, dpi)); | 1050 page_layout_in_points.margin_left, printing::kPointsPerInch, dpi)); |
1054 | 1051 |
1055 prepare->UpdatePrintParams(*params); | 1052 prepare->UpdatePrintParams(*params); |
1056 } | 1053 } |
1057 | 1054 |
1058 bool PrintWebViewHelper::InitPrintSettings(WebKit::WebFrame* frame, | 1055 bool PrintWebViewHelper::InitPrintSettings(WebKit::WebFrame* frame) { |
1059 WebKit::WebNode* node, | |
1060 bool is_preview) { | |
1061 DCHECK(frame); | 1056 DCHECK(frame); |
1062 PrintMsg_PrintPages_Params settings; | 1057 PrintMsg_PrintPages_Params settings; |
1063 | 1058 |
1064 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(), | 1059 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(), |
1065 &settings.params)); | 1060 &settings.params)); |
1066 // 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 |
1067 // 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 |
1068 // terminate. | 1063 // terminate. |
1069 bool result = true; | 1064 bool result = true; |
1070 if (PrintMsg_Print_Params_IsEmpty(settings.params)) { | 1065 if (PrintMsg_Print_Params_IsEmpty(settings.params)) { |
1071 if (!is_preview) { | 1066 render_view()->runModalAlertDialog( |
1072 render_view()->runModalAlertDialog( | 1067 frame, |
1073 frame, | 1068 l10n_util::GetStringUTF16( |
1074 l10n_util::GetStringUTF16( | 1069 IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS)); |
1075 IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS)); | |
1076 } | |
1077 result = false; | 1070 result = false; |
1078 } | 1071 } |
1079 | 1072 |
1080 if (result && | 1073 if (result && |
1081 (settings.params.dpi < kMinDpi || settings.params.document_cookie == 0)) { | 1074 (settings.params.dpi < kMinDpi || settings.params.document_cookie == 0)) { |
1082 // Invalid print page settings. | 1075 // Invalid print page settings. |
1083 NOTREACHED(); | 1076 NOTREACHED(); |
1084 result = false; | 1077 result = false; |
1085 } | 1078 } |
1086 | 1079 |
1087 settings.pages.clear(); | 1080 settings.pages.clear(); |
1088 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); | 1081 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); |
1089 return result; | 1082 return result; |
1090 } | 1083 } |
1091 | 1084 |
1092 bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame( | 1085 bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame( |
1093 WebKit::WebFrame* frame, WebKit::WebNode* node, | 1086 WebKit::WebFrame* frame, WebKit::WebNode* node, |
1094 scoped_ptr<PrepareFrameAndViewForPrint>* prepare) { | 1087 scoped_ptr<PrepareFrameAndViewForPrint>* prepare) { |
1095 if (!InitPrintSettings(frame, node, false)) | 1088 if (!InitPrintSettings(frame)) |
1096 return false; | 1089 return false; |
1097 | 1090 |
1098 DCHECK(!prepare->get()); | 1091 DCHECK(!prepare->get()); |
1099 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, | 1092 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, |
1100 frame, node)); | 1093 frame, node)); |
1101 UpdatePrintableSizeInPrintParameters(frame, node, prepare->get(), | 1094 UpdatePrintableSizeInPrintParameters(frame, node, prepare->get(), |
1102 &print_pages_params_->params); | 1095 &print_pages_params_->params); |
1103 Send(new PrintHostMsg_DidGetDocumentCookie( | 1096 Send(new PrintHostMsg_DidGetDocumentCookie( |
1104 routing_id(), print_pages_params_->params.document_cookie)); | 1097 routing_id(), print_pages_params_->params.document_cookie)); |
1105 return true; | 1098 return true; |
1106 } | 1099 } |
1107 | 1100 |
1108 bool PrintWebViewHelper::UpdatePrintSettings( | 1101 bool PrintWebViewHelper::UpdatePrintSettings( |
1109 const DictionaryValue& job_settings, bool is_preview) { | 1102 const DictionaryValue& job_settings, bool is_preview) { |
1103 if (job_settings.empty()) { | |
1104 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); | |
1105 print_pages_params_.reset(new PrintMsg_PrintPages_Params()); | |
kmadhusu
2011/09/19 19:09:41
Why do you want to reset print_pages_params_ here?
arthurhsu
2011/09/19 22:24:49
Done.
| |
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)); | |
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 Loading... | |
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 } |
OLD | NEW |