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/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/auto_reset.h" | 9 #include "base/auto_reset.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 732 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
743 if (pdf_element.isNull()) { | 743 if (pdf_element.isNull()) { |
744 NOTREACHED(); | 744 NOTREACHED(); |
745 return; | 745 return; |
746 } | 746 } |
747 | 747 |
748 // Set |print_for_preview_| flag and autoreset it to back to original | 748 // Set |print_for_preview_| flag and autoreset it to back to original |
749 // on return. | 749 // on return. |
750 AutoReset<bool> set_printing_flag(&print_for_preview_, true); | 750 AutoReset<bool> set_printing_flag(&print_for_preview_, true); |
751 | 751 |
752 WebFrame* pdf_frame = pdf_element.document().frame(); | 752 WebFrame* pdf_frame = pdf_element.document().frame(); |
753 // TODO(gene): Consider using |print_for_preview_| inside UpdatePrintSettings | 753 if (!UpdatePrintSettings(pdf_frame, pdf_element, job_settings)) { |
754 // instead of passing this as parameter. | |
755 if (!UpdatePrintSettings(pdf_frame, pdf_element, | |
756 job_settings, print_for_preview_)) { | |
757 LOG(ERROR) << "UpdatePrintSettings failed"; | 754 LOG(ERROR) << "UpdatePrintSettings failed"; |
758 DidFinishPrinting(FAIL_PRINT); | 755 DidFinishPrinting(FAIL_PRINT); |
759 return; | 756 return; |
760 } | 757 } |
761 | 758 |
762 // Render Pages for printing. | 759 // Render Pages for printing. |
763 if (!RenderPagesForPrint(pdf_frame, pdf_element)) { | 760 if (!RenderPagesForPrint(pdf_frame, pdf_element)) { |
764 LOG(ERROR) << "RenderPagesForPrint failed"; | 761 LOG(ERROR) << "RenderPagesForPrint failed"; |
765 DidFinishPrinting(FAIL_PRINT); | 762 DidFinishPrinting(FAIL_PRINT); |
766 } | 763 } |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
827 if (!job_settings.GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf)) | 824 if (!job_settings.GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf)) |
828 NOTREACHED(); | 825 NOTREACHED(); |
829 return print_to_pdf; | 826 return print_to_pdf; |
830 } | 827 } |
831 | 828 |
832 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { | 829 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { |
833 DCHECK(is_preview_enabled_); | 830 DCHECK(is_preview_enabled_); |
834 print_preview_context_.OnPrintPreview(); | 831 print_preview_context_.OnPrintPreview(); |
835 | 832 |
836 if (!UpdatePrintSettings(print_preview_context_.frame(), | 833 if (!UpdatePrintSettings(print_preview_context_.frame(), |
837 print_preview_context_.node(), settings, false)) { | 834 print_preview_context_.node(), settings)) { |
838 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) { | 835 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) { |
839 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings( | 836 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings( |
840 routing_id(), print_pages_params_->params.document_cookie)); | 837 routing_id(), print_pages_params_->params.document_cookie)); |
841 notify_browser_of_print_failure_ = false; // Already sent. | 838 notify_browser_of_print_failure_ = false; // Already sent. |
842 } | 839 } |
843 DidFinishPrinting(FAIL_PREVIEW); | 840 DidFinishPrinting(FAIL_PREVIEW); |
844 return; | 841 return; |
845 } | 842 } |
846 | 843 |
847 if (!print_pages_params_->params.is_first_request && | 844 if (!print_pages_params_->params.is_first_request && |
(...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1280 UpdateFrameAndViewFromCssPageLayout(frame, node, prepare->get(), | 1277 UpdateFrameAndViewFromCssPageLayout(frame, node, prepare->get(), |
1281 print_pages_params_->params, | 1278 print_pages_params_->params, |
1282 ignore_css_margins_, fit_to_page_); | 1279 ignore_css_margins_, fit_to_page_); |
1283 Send(new PrintHostMsg_DidGetDocumentCookie( | 1280 Send(new PrintHostMsg_DidGetDocumentCookie( |
1284 routing_id(), print_pages_params_->params.document_cookie)); | 1281 routing_id(), print_pages_params_->params.document_cookie)); |
1285 return true; | 1282 return true; |
1286 } | 1283 } |
1287 | 1284 |
1288 bool PrintWebViewHelper::UpdatePrintSettings( | 1285 bool PrintWebViewHelper::UpdatePrintSettings( |
1289 WebKit::WebFrame* frame, const WebKit::WebNode& node, | 1286 WebKit::WebFrame* frame, const WebKit::WebNode& node, |
1290 const DictionaryValue& passed_job_settings, bool print_for_preview) { | 1287 const DictionaryValue& passed_job_settings) { |
1291 DCHECK(is_preview_enabled_); | 1288 DCHECK(is_preview_enabled_); |
1292 const DictionaryValue* job_settings = &passed_job_settings; | 1289 const DictionaryValue* job_settings = &passed_job_settings; |
1293 DictionaryValue modified_job_settings; | 1290 DictionaryValue modified_job_settings; |
1294 if (job_settings->empty()) { | 1291 if (job_settings->empty()) { |
1295 if (!print_for_preview) | 1292 if (!print_for_preview_) |
1296 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); | 1293 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); |
1297 return false; | 1294 return false; |
1298 } | 1295 } |
1299 | 1296 |
1300 bool source_is_html = true; | 1297 bool source_is_html = true; |
1301 if (print_for_preview) { | 1298 if (print_for_preview_) { |
1302 if (!job_settings->GetBoolean(printing::kSettingPreviewModifiable, | 1299 if (!job_settings->GetBoolean(printing::kSettingPreviewModifiable, |
1303 &source_is_html)) { | 1300 &source_is_html)) { |
1304 NOTREACHED(); | 1301 NOTREACHED(); |
1305 } | 1302 } |
1306 } else { | 1303 } else { |
1307 source_is_html = !PrintingNodeOrPdfFrame(frame, node); | 1304 source_is_html = !PrintingNodeOrPdfFrame(frame, node); |
1308 } | 1305 } |
1309 | 1306 |
1310 if (print_for_preview || !source_is_html) { | 1307 if (print_for_preview_ || !source_is_html) { |
1311 modified_job_settings.MergeDictionary(job_settings); | 1308 modified_job_settings.MergeDictionary(job_settings); |
1312 modified_job_settings.SetBoolean(printing::kSettingHeaderFooterEnabled, | 1309 modified_job_settings.SetBoolean(printing::kSettingHeaderFooterEnabled, |
1313 false); | 1310 false); |
1314 | 1311 |
1315 // - On Windows, we don't add a margin until we turn it into an EMF when | 1312 // - On Windows, we don't add a margin until we turn it into an EMF when |
1316 // printing for print preview (We could add it in the plugin). | 1313 // printing for print preview (We could add it in the plugin). |
1317 // - On Mac with Skia, we don't add a margin until we send it to the printer | 1314 // - On Mac with Skia, we don't add a margin until we send it to the printer |
1318 // using the CG PDF class (We could add it in the plugin). | 1315 // using the CG PDF class (We could add it in the plugin). |
1319 // - On Mac with CG, we can add a margin when generating the preview. | 1316 // - On Mac with CG, we can add a margin when generating the preview. |
1320 // - On Linux, we never add a margin (We Could add it in the plugin). | 1317 // - On Linux, we never add a margin (We Could add it in the plugin). |
1321 #if defined(OS_MACOSX) && !defined(USE_SKIA) | 1318 #if defined(OS_MACOSX) && !defined(USE_SKIA) |
1322 bool get_margins_from_pdf = !source_is_html && !print_for_preview; | 1319 bool get_margins_from_pdf = !source_is_html && !print_for_preview_; |
1323 #elif defined(OS_WIN) || defined(OS_MACOSX) | 1320 #elif defined(OS_WIN) || defined(OS_MACOSX) |
1324 bool get_margins_from_pdf = !source_is_html && print_for_preview; | 1321 bool get_margins_from_pdf = !source_is_html && print_for_preview_; |
1325 #else | 1322 #else |
1326 bool get_margins_from_pdf = false; | 1323 bool get_margins_from_pdf = false; |
1327 #endif | 1324 #endif |
1328 | 1325 |
1329 printing::MarginType margin_type = printing::NO_MARGINS; | 1326 printing::MarginType margin_type = printing::NO_MARGINS; |
1330 if (get_margins_from_pdf) | 1327 if (get_margins_from_pdf) |
1331 margin_type = GetMarginsForPdf(frame, node); | 1328 margin_type = GetMarginsForPdf(frame, node); |
1332 modified_job_settings.SetInteger(printing::kSettingMarginsType, | 1329 modified_job_settings.SetInteger(printing::kSettingMarginsType, |
1333 margin_type); | 1330 margin_type); |
1334 job_settings = &modified_job_settings; | 1331 job_settings = &modified_job_settings; |
1335 } | 1332 } |
1336 | 1333 |
1337 // Send the cookie so that UpdatePrintSettings can reuse PrinterQuery when | 1334 // Send the cookie so that UpdatePrintSettings can reuse PrinterQuery when |
1338 // possible. | 1335 // possible. |
1339 int cookie = print_pages_params_.get() ? | 1336 int cookie = print_pages_params_.get() ? |
1340 print_pages_params_->params.document_cookie : 0; | 1337 print_pages_params_->params.document_cookie : 0; |
1341 PrintMsg_PrintPages_Params settings; | 1338 PrintMsg_PrintPages_Params settings; |
1342 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), | 1339 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), |
1343 cookie, *job_settings, &settings)); | 1340 cookie, *job_settings, &settings)); |
1344 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); | 1341 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); |
1345 | 1342 |
1346 if (!PrintMsg_Print_Params_IsValid(settings.params)) { | 1343 if (!PrintMsg_Print_Params_IsValid(settings.params)) { |
1347 if (!print_for_preview) { | 1344 if (!print_for_preview_) { |
1348 print_preview_context_.set_error(PREVIEW_ERROR_INVALID_PRINTER_SETTINGS); | 1345 print_preview_context_.set_error(PREVIEW_ERROR_INVALID_PRINTER_SETTINGS); |
1349 } else { | 1346 } else { |
1350 // PrintForPrintPreview | 1347 // PrintForPrintPreview |
1351 WebKit::WebFrame* print_frame = NULL; | 1348 WebKit::WebFrame* print_frame = NULL; |
1352 // This may not be the right frame, but the alert will be modal, | 1349 // This may not be the right frame, but the alert will be modal, |
1353 // therefore it works well enough. | 1350 // therefore it works well enough. |
1354 GetPrintFrame(&print_frame); | 1351 GetPrintFrame(&print_frame); |
1355 if (print_frame) { | 1352 if (print_frame) { |
1356 render_view()->RunModalAlertDialog( | 1353 render_view()->RunModalAlertDialog( |
1357 print_frame, | 1354 print_frame, |
1358 l10n_util::GetStringUTF16( | 1355 l10n_util::GetStringUTF16( |
1359 IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS)); | 1356 IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS)); |
1360 } | 1357 } |
1361 } | 1358 } |
1362 return false; | 1359 return false; |
1363 } | 1360 } |
1364 | 1361 |
1365 if (settings.params.dpi < kMinDpi || !settings.params.document_cookie) { | 1362 if (settings.params.dpi < kMinDpi || !settings.params.document_cookie) { |
1366 print_preview_context_.set_error(PREVIEW_ERROR_UPDATING_PRINT_SETTINGS); | 1363 print_preview_context_.set_error(PREVIEW_ERROR_UPDATING_PRINT_SETTINGS); |
1367 return false; | 1364 return false; |
1368 } | 1365 } |
1369 | 1366 |
1370 if (!print_for_preview) { | 1367 if (!print_for_preview_) { |
1371 // Validate expected print preview settings. | 1368 // Validate expected print preview settings. |
1372 if (!job_settings->GetString(printing::kPreviewUIAddr, | 1369 if (!job_settings->GetString(printing::kPreviewUIAddr, |
1373 &(settings.params.preview_ui_addr)) || | 1370 &(settings.params.preview_ui_addr)) || |
1374 !job_settings->GetInteger(printing::kPreviewRequestID, | 1371 !job_settings->GetInteger(printing::kPreviewRequestID, |
1375 &(settings.params.preview_request_id)) || | 1372 &(settings.params.preview_request_id)) || |
1376 !job_settings->GetBoolean(printing::kIsFirstRequest, | 1373 !job_settings->GetBoolean(printing::kIsFirstRequest, |
1377 &(settings.params.is_first_request))) { | 1374 &(settings.params.is_first_request))) { |
1378 NOTREACHED(); | 1375 NOTREACHED(); |
1379 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); | 1376 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); |
1380 return false; | 1377 return false; |
(...skipping 440 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1821 DCHECK(IsRendering()); | 1818 DCHECK(IsRendering()); |
1822 return prep_frame_view_->GetPrintCanvasSize(); | 1819 return prep_frame_view_->GetPrintCanvasSize(); |
1823 } | 1820 } |
1824 | 1821 |
1825 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { | 1822 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
1826 prep_frame_view_.reset(); | 1823 prep_frame_view_.reset(); |
1827 metafile_.reset(); | 1824 metafile_.reset(); |
1828 pages_to_render_.clear(); | 1825 pages_to_render_.clear(); |
1829 error_ = PREVIEW_ERROR_NONE; | 1826 error_ = PREVIEW_ERROR_NONE; |
1830 } | 1827 } |
OLD | NEW |