Chromium Code Reviews| 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 } |
| 576 | |
| 577 WebFrame* pdf_frame = pdf_element.document().frame(); | |
| 578 scoped_ptr<PrepareFrameAndViewForPrint> prepare; | |
| 579 if (!PrepareFrame(pdf_frame, &pdf_element, &prepare)) { | |
| 580 LOG(ERROR) << "Failed to initialize print page settings"; | |
| 581 DidFinishPrinting(FAIL_PRINT); | |
| 582 return; | |
| 583 } | |
| 583 | 584 |
| 584 // Render Pages for printing. | 585 // Render Pages for printing. |
| 585 if (!RenderPagesForPrint(pdf_frame, &pdf_element, prepare.get())) { | 586 if (!RenderPagesForPrint(pdf_frame, &pdf_element, prepare.get())) { |
| 586 LOG(ERROR) << "RenderPagesForPrint failed"; | 587 LOG(ERROR) << "RenderPagesForPrint failed"; |
| 587 DidFinishPrinting(FAIL_PRINT); | 588 DidFinishPrinting(FAIL_PRINT); |
| 588 } | 589 } |
| 589 } | 590 } |
| 590 | 591 |
| 591 bool PrintWebViewHelper::GetPrintFrame(WebKit::WebFrame** frame) { | 592 bool PrintWebViewHelper::GetPrintFrame(WebKit::WebFrame** frame) { |
| 592 DCHECK(frame); | 593 DCHECK(frame); |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 623 // copy when the print preview tab closes. | 624 // copy when the print preview tab closes. |
| 624 WebNode duplicate_node(*node); | 625 WebNode duplicate_node(*node); |
| 625 Print(frame, &duplicate_node); | 626 Print(frame, &duplicate_node); |
| 626 } | 627 } |
| 627 } | 628 } |
| 628 | 629 |
| 629 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { | 630 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { |
| 630 DCHECK(is_preview_); | 631 DCHECK(is_preview_); |
| 631 print_preview_context_.OnPrintPreview(); | 632 print_preview_context_.OnPrintPreview(); |
| 632 | 633 |
| 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)) { | 634 if (!UpdatePrintSettings(settings, true)) { |
| 643 LOG(ERROR) << "UpdatePrintSettings failed"; | 635 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) { |
|
kmadhusu
2011/09/16 20:31:14
How about?
print_preview_context_.last_error() ==
arthurhsu
2011/09/16 23:39:34
There are three possible values, BAD_SETTING, INVA
| |
| 636 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings( | |
| 637 routing_id(), print_pages_params_->params.document_cookie)); | |
| 638 notify_browser_of_print_failure_ = false; // Already sent. | |
| 639 } | |
| 644 DidFinishPrinting(FAIL_PREVIEW); | 640 DidFinishPrinting(FAIL_PREVIEW); |
| 645 return; | 641 return; |
| 646 } | 642 } |
| 647 | 643 |
| 648 if (!print_pages_params_->params.is_first_request && | 644 if (!print_pages_params_->params.is_first_request && |
| 649 old_print_pages_params_.get() && | 645 old_print_pages_params_.get() && |
| 650 PrintMsg_Print_Params_IsEqual(*old_print_pages_params_, | 646 PrintMsg_Print_Params_IsEqual(*old_print_pages_params_, |
| 651 *print_pages_params_)) { | 647 *print_pages_params_)) { |
| 652 PrintHostMsg_DidPreviewDocument_Params preview_params; | 648 PrintHostMsg_DidPreviewDocument_Params preview_params; |
| 653 preview_params.reuse_existing_data = true; | 649 preview_params.reuse_existing_data = true; |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 798 } | 794 } |
| 799 } | 795 } |
| 800 | 796 |
| 801 void PrintWebViewHelper::Print(WebKit::WebFrame* frame, WebKit::WebNode* node) { | 797 void PrintWebViewHelper::Print(WebKit::WebFrame* frame, WebKit::WebNode* node) { |
| 802 // If still not finished with earlier print request simply ignore. | 798 // If still not finished with earlier print request simply ignore. |
| 803 if (print_web_view_) | 799 if (print_web_view_) |
| 804 return; | 800 return; |
| 805 | 801 |
| 806 // Initialize print settings. | 802 // Initialize print settings. |
| 807 scoped_ptr<PrepareFrameAndViewForPrint> prepare; | 803 scoped_ptr<PrepareFrameAndViewForPrint> prepare; |
| 808 if (!InitPrintSettingsAndPrepareFrame(frame, node, &prepare)) | 804 if (!InitPrintSettings(frame) || |
| 805 !PrepareFrame(frame, node, &prepare)) { | |
| 806 DidFinishPrinting(FAIL_PRINT); | |
| 809 return; // Failed to init print page settings. | 807 return; // Failed to init print page settings. |
| 808 } | |
| 810 | 809 |
| 811 int expected_page_count = 0; | 810 int expected_page_count = 0; |
| 812 bool use_browser_overlays = true; | 811 bool use_browser_overlays = true; |
| 813 | 812 |
| 814 expected_page_count = prepare->GetExpectedPageCount(); | 813 expected_page_count = prepare->GetExpectedPageCount(); |
| 815 if (expected_page_count) | 814 if (expected_page_count) |
| 816 use_browser_overlays = prepare->ShouldUseBrowserOverlays(); | 815 use_browser_overlays = prepare->ShouldUseBrowserOverlays(); |
| 817 | 816 |
| 818 // Release the prepare before going any further, since we are going to | 817 // Release the prepare before going any further, since we are going to |
| 819 // show UI and wait for the user. | 818 // 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))); | 1047 page_height_in_points, printing::kPointsPerInch, dpi))); |
| 1049 | 1048 |
| 1050 params->margin_top = static_cast<int>(ConvertUnitDouble( | 1049 params->margin_top = static_cast<int>(ConvertUnitDouble( |
| 1051 page_layout_in_points.margin_top, printing::kPointsPerInch, dpi)); | 1050 page_layout_in_points.margin_top, printing::kPointsPerInch, dpi)); |
| 1052 params->margin_left = static_cast<int>(ConvertUnitDouble( | 1051 params->margin_left = static_cast<int>(ConvertUnitDouble( |
| 1053 page_layout_in_points.margin_left, printing::kPointsPerInch, dpi)); | 1052 page_layout_in_points.margin_left, printing::kPointsPerInch, dpi)); |
| 1054 | 1053 |
| 1055 prepare->UpdatePrintParams(*params); | 1054 prepare->UpdatePrintParams(*params); |
| 1056 } | 1055 } |
| 1057 | 1056 |
| 1058 bool PrintWebViewHelper::InitPrintSettings(WebKit::WebFrame* frame, | 1057 bool PrintWebViewHelper::InitPrintSettings(WebKit::WebFrame* frame) { |
| 1059 WebKit::WebNode* node, | |
| 1060 bool is_preview) { | |
| 1061 DCHECK(frame); | 1058 DCHECK(frame); |
| 1062 PrintMsg_PrintPages_Params settings; | 1059 PrintMsg_PrintPages_Params settings; |
| 1063 | 1060 |
| 1064 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(), | 1061 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(), |
| 1065 &settings.params)); | 1062 &settings.params)); |
| 1066 // Check if the printer returned any settings, if the settings is empty, we | 1063 // 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 | 1064 // can safely assume there are no printer drivers configured. So we safely |
| 1068 // terminate. | 1065 // terminate. |
| 1069 bool result = true; | 1066 bool result = true; |
| 1070 if (PrintMsg_Print_Params_IsEmpty(settings.params)) { | 1067 if (PrintMsg_Print_Params_IsEmpty(settings.params)) { |
| 1071 if (!is_preview) { | 1068 render_view()->runModalAlertDialog( |
| 1072 render_view()->runModalAlertDialog( | 1069 frame, |
| 1073 frame, | 1070 l10n_util::GetStringUTF16( |
| 1074 l10n_util::GetStringUTF16( | 1071 IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS)); |
| 1075 IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS)); | |
| 1076 } | |
| 1077 result = false; | 1072 result = false; |
| 1078 } | 1073 } |
| 1079 | 1074 |
| 1080 if (result && | 1075 if (result && |
| 1081 (settings.params.dpi < kMinDpi || settings.params.document_cookie == 0)) { | 1076 (settings.params.dpi < kMinDpi || settings.params.document_cookie == 0)) { |
| 1082 // Invalid print page settings. | 1077 // Invalid print page settings. |
| 1083 NOTREACHED(); | 1078 NOTREACHED(); |
| 1084 result = false; | 1079 result = false; |
| 1085 } | 1080 } |
| 1086 | 1081 |
| 1087 settings.pages.clear(); | 1082 settings.pages.clear(); |
| 1088 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); | 1083 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); |
| 1089 return result; | 1084 return result; |
| 1090 } | 1085 } |
| 1091 | 1086 |
| 1092 bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame( | 1087 bool PrintWebViewHelper::PrepareFrame( |
| 1093 WebKit::WebFrame* frame, WebKit::WebNode* node, | 1088 WebKit::WebFrame* frame, WebKit::WebNode* node, |
| 1094 scoped_ptr<PrepareFrameAndViewForPrint>* prepare) { | 1089 scoped_ptr<PrepareFrameAndViewForPrint>* prepare) { |
| 1095 if (!InitPrintSettings(frame, node, false)) | |
| 1096 return false; | |
| 1097 | |
| 1098 DCHECK(!prepare->get()); | 1090 DCHECK(!prepare->get()); |
| 1099 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, | 1091 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, |
| 1100 frame, node)); | 1092 frame, node)); |
| 1101 UpdatePrintableSizeInPrintParameters(frame, node, prepare->get(), | 1093 UpdatePrintableSizeInPrintParameters(frame, node, prepare->get(), |
| 1102 &print_pages_params_->params); | 1094 &print_pages_params_->params); |
| 1103 Send(new PrintHostMsg_DidGetDocumentCookie( | 1095 Send(new PrintHostMsg_DidGetDocumentCookie( |
| 1104 routing_id(), print_pages_params_->params.document_cookie)); | 1096 routing_id(), print_pages_params_->params.document_cookie)); |
| 1105 return true; | 1097 return true; |
| 1106 } | 1098 } |
| 1107 | 1099 |
| 1108 bool PrintWebViewHelper::UpdatePrintSettings( | 1100 bool PrintWebViewHelper::UpdatePrintSettings( |
| 1109 const DictionaryValue& job_settings, bool is_preview) { | 1101 const DictionaryValue& job_settings, bool is_preview) { |
| 1102 if (job_settings.empty()) { | |
| 1103 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); | |
| 1104 print_pages_params_.reset(new PrintMsg_PrintPages_Params()); | |
| 1105 return false; | |
| 1106 } | |
| 1107 | |
| 1110 PrintMsg_PrintPages_Params settings; | 1108 PrintMsg_PrintPages_Params settings; |
| 1109 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), | |
| 1110 job_settings, &settings)); | |
| 1111 | 1111 |
| 1112 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), | 1112 // Setup an empty default param so that document cookie can be propagated |
| 1113 print_pages_params_->params.document_cookie, job_settings, &settings)); | 1113 // in case of error. |
| 1114 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); | |
| 1115 | |
| 1116 if (PrintMsg_Print_Params_IsEmpty(settings.params)) { | |
| 1117 if (!is_preview) { | |
| 1118 WebKit::WebFrame* frame = print_preview_context_.frame(); | |
| 1119 if (!frame) | |
| 1120 GetPrintFrame(&frame); | |
| 1121 DCHECK(frame); | |
| 1122 render_view()->runModalAlertDialog( | |
| 1123 frame, | |
| 1124 l10n_util::GetStringUTF16( | |
| 1125 IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS)); | |
| 1126 } else { | |
| 1127 print_preview_context_.set_error(PREVIEW_ERROR_INVALID_PRINTER_SETTINGS); | |
| 1128 } | |
| 1129 return false; | |
| 1130 } | |
| 1131 | |
| 1114 if (settings.params.dpi < kMinDpi || !settings.params.document_cookie) { | 1132 if (settings.params.dpi < kMinDpi || !settings.params.document_cookie) { |
| 1115 print_preview_context_.set_error(PREVIEW_ERROR_UPDATING_PRINT_SETTINGS); | 1133 print_preview_context_.set_error(PREVIEW_ERROR_UPDATING_PRINT_SETTINGS); |
| 1116 return false; | 1134 return false; |
| 1117 } | 1135 } |
| 1118 | 1136 |
| 1119 if (is_preview) { | 1137 if (is_preview) { |
| 1120 // Validate expected print preview settings. | 1138 // Validate expected print preview settings. |
| 1121 if (!job_settings.GetString(printing::kPreviewUIAddr, | 1139 if (!job_settings.GetString(printing::kPreviewUIAddr, |
| 1122 &(settings.params.preview_ui_addr)) || | 1140 &(settings.params.preview_ui_addr)) || |
| 1123 !job_settings.GetInteger(printing::kPreviewRequestID, | 1141 !job_settings.GetInteger(printing::kPreviewRequestID, |
| (...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1539 printing::PreviewMetafile* | 1557 printing::PreviewMetafile* |
| 1540 PrintWebViewHelper::PrintPreviewContext::metafile() const { | 1558 PrintWebViewHelper::PrintPreviewContext::metafile() const { |
| 1541 return metafile_.get(); | 1559 return metafile_.get(); |
| 1542 } | 1560 } |
| 1543 | 1561 |
| 1544 const PrintMsg_Print_Params& | 1562 const PrintMsg_Print_Params& |
| 1545 PrintWebViewHelper::PrintPreviewContext::print_params() const { | 1563 PrintWebViewHelper::PrintPreviewContext::print_params() const { |
| 1546 return *print_params_; | 1564 return *print_params_; |
| 1547 } | 1565 } |
| 1548 | 1566 |
| 1567 int PrintWebViewHelper::PrintPreviewContext::last_error() const { | |
| 1568 return error_; | |
| 1569 } | |
| 1570 | |
| 1549 const gfx::Size& | 1571 const gfx::Size& |
| 1550 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { | 1572 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { |
| 1551 return prep_frame_view_->GetPrintCanvasSize(); | 1573 return prep_frame_view_->GetPrintCanvasSize(); |
| 1552 } | 1574 } |
| 1553 | 1575 |
| 1554 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { | 1576 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
| 1555 prep_frame_view_.reset(); | 1577 prep_frame_view_.reset(); |
| 1556 metafile_.reset(); | 1578 metafile_.reset(); |
| 1557 pages_to_render_.clear(); | 1579 pages_to_render_.clear(); |
| 1558 error_ = PREVIEW_ERROR_NONE; | 1580 error_ = PREVIEW_ERROR_NONE; |
| 1559 } | 1581 } |
| OLD | NEW |