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 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
338 return frame_has_custom_page_size_style; | 338 return frame_has_custom_page_size_style; |
339 } | 339 } |
340 | 340 |
341 printing::MarginType GetMarginsForPdf(WebFrame* frame, const WebNode& node) { | 341 printing::MarginType GetMarginsForPdf(WebFrame* frame, const WebNode& node) { |
342 if (frame->isPrintScalingDisabledForPlugin(node)) | 342 if (frame->isPrintScalingDisabledForPlugin(node)) |
343 return printing::NO_MARGINS; | 343 return printing::NO_MARGINS; |
344 else | 344 else |
345 return printing::PRINTABLE_AREA_MARGINS; | 345 return printing::PRINTABLE_AREA_MARGINS; |
346 } | 346 } |
347 | 347 |
| 348 bool FitToPageEnabled(const DictionaryValue& job_settings) { |
| 349 bool fit_to_paper_size = false; |
| 350 if (!job_settings.GetBoolean(printing::kSettingFitToPageEnabled, |
| 351 &fit_to_paper_size)) { |
| 352 NOTREACHED(); |
| 353 } |
| 354 return fit_to_paper_size; |
| 355 } |
| 356 |
348 // Get the (x, y) coordinate from where printing of the current text should | 357 // Get the (x, y) coordinate from where printing of the current text should |
349 // start depending on the horizontal alignment (LEFT, RIGHT, CENTER) and | 358 // start depending on the horizontal alignment (LEFT, RIGHT, CENTER) and |
350 // vertical alignment (TOP, BOTTOM). | 359 // vertical alignment (TOP, BOTTOM). |
351 SkPoint GetHeaderFooterPosition( | 360 SkPoint GetHeaderFooterPosition( |
352 float webkit_scale_factor, | 361 float webkit_scale_factor, |
353 const PageSizeMargins& page_layout, | 362 const PageSizeMargins& page_layout, |
354 printing::HorizontalHeaderFooterPosition horizontal_position, | 363 printing::HorizontalHeaderFooterPosition horizontal_position, |
355 printing::VerticalHeaderFooterPosition vertical_position, | 364 printing::VerticalHeaderFooterPosition vertical_position, |
356 double offset_to_baseline, | 365 double offset_to_baseline, |
357 double text_width_in_points) { | 366 double text_width_in_points) { |
(...skipping 534 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
892 } | 901 } |
893 | 902 |
894 bool PrintWebViewHelper::IsPrintToPdfRequested( | 903 bool PrintWebViewHelper::IsPrintToPdfRequested( |
895 const DictionaryValue& job_settings) { | 904 const DictionaryValue& job_settings) { |
896 bool print_to_pdf = false; | 905 bool print_to_pdf = false; |
897 if (!job_settings.GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf)) | 906 if (!job_settings.GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf)) |
898 NOTREACHED(); | 907 NOTREACHED(); |
899 return print_to_pdf; | 908 return print_to_pdf; |
900 } | 909 } |
901 | 910 |
| 911 bool PrintWebViewHelper::IsFitToPaperSizeRequested( |
| 912 bool source_is_html, const DictionaryValue& job_settings, |
| 913 const PrintMsg_Print_Params& params) { |
| 914 DCHECK(!print_for_preview_); |
| 915 |
| 916 // Do not fit to paper size when the user is saving the print contents as pdf. |
| 917 if (params.print_to_pdf) |
| 918 return false; |
| 919 |
| 920 if (!source_is_html) { |
| 921 if (!FitToPageEnabled(job_settings)) |
| 922 return false; |
| 923 |
| 924 // Get the print scaling option for the initiator renderer pdf. |
| 925 bool print_scaling_disabled_for_plugin = |
| 926 print_preview_context_.frame()->isPrintScalingDisabledForPlugin( |
| 927 print_preview_context_.node()); |
| 928 |
| 929 // If this is the first preview request, UI doesn't know about the print |
| 930 // scaling option of the plugin. Therefore, check the print scaling option |
| 931 // and update the print params accordingly. |
| 932 // |
| 933 // If this is not the first preview request, update print params based on |
| 934 // preview job settings. |
| 935 if (params.is_first_request && print_scaling_disabled_for_plugin) |
| 936 return false; |
| 937 } |
| 938 return true; |
| 939 } |
| 940 |
902 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { | 941 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { |
903 DCHECK(is_preview_enabled_); | 942 DCHECK(is_preview_enabled_); |
904 print_preview_context_.OnPrintPreview(); | 943 print_preview_context_.OnPrintPreview(); |
905 | 944 |
906 if (!UpdatePrintSettings(print_preview_context_.frame(), | 945 if (!UpdatePrintSettings(print_preview_context_.frame(), |
907 print_preview_context_.node(), settings)) { | 946 print_preview_context_.node(), settings)) { |
908 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) { | 947 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) { |
909 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings( | 948 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings( |
910 routing_id(), print_pages_params_->params.document_cookie)); | 949 routing_id(), print_pages_params_->params.document_cookie)); |
911 notify_browser_of_print_failure_ = false; // Already sent. | 950 notify_browser_of_print_failure_ = false; // Already sent. |
(...skipping 14 matching lines...) Expand all Loading... |
926 preview_params.expected_pages_count = | 965 preview_params.expected_pages_count = |
927 print_preview_context_.total_page_count(); | 966 print_preview_context_.total_page_count(); |
928 preview_params.modifiable = print_preview_context_.IsModifiable(); | 967 preview_params.modifiable = print_preview_context_.IsModifiable(); |
929 preview_params.preview_request_id = | 968 preview_params.preview_request_id = |
930 print_pages_params_->params.preview_request_id; | 969 print_pages_params_->params.preview_request_id; |
931 | 970 |
932 Send(new PrintHostMsg_MetafileReadyForPrinting(routing_id(), | 971 Send(new PrintHostMsg_MetafileReadyForPrinting(routing_id(), |
933 preview_params)); | 972 preview_params)); |
934 return; | 973 return; |
935 } | 974 } |
| 975 |
| 976 // If we are previewing a pdf and the print scaling is disabled, send a |
| 977 // message to browser. |
| 978 if (print_pages_params_->params.is_first_request && |
| 979 !print_preview_context_.IsModifiable() && |
| 980 print_preview_context_.frame()->isPrintScalingDisabledForPlugin( |
| 981 print_preview_context_.node())) { |
| 982 Send(new PrintHostMsg_PrintPreviewScalingDisabled(routing_id())); |
| 983 } |
| 984 |
936 // Always clear |old_print_pages_params_| before rendering the pages. | 985 // Always clear |old_print_pages_params_| before rendering the pages. |
937 old_print_pages_params_.reset(); | 986 old_print_pages_params_.reset(); |
938 is_print_ready_metafile_sent_ = false; | 987 is_print_ready_metafile_sent_ = false; |
939 | 988 |
940 // PDF printer device supports alpha blending. | 989 // PDF printer device supports alpha blending. |
941 print_pages_params_->params.supports_alpha_blend = true; | 990 print_pages_params_->params.supports_alpha_blend = true; |
942 | 991 |
943 bool generate_draft_pages = false; | 992 bool generate_draft_pages = false; |
944 if (!settings.GetBoolean(printing::kSettingGenerateDraftData, | 993 if (!settings.GetBoolean(printing::kSettingGenerateDraftData, |
945 &generate_draft_pages)) { | 994 &generate_draft_pages)) { |
(...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1368 NOTREACHED(); | 1417 NOTREACHED(); |
1369 } | 1418 } |
1370 } else { | 1419 } else { |
1371 source_is_html = !PrintingNodeOrPdfFrame(frame, node); | 1420 source_is_html = !PrintingNodeOrPdfFrame(frame, node); |
1372 } | 1421 } |
1373 | 1422 |
1374 if (print_for_preview_ || !source_is_html) { | 1423 if (print_for_preview_ || !source_is_html) { |
1375 modified_job_settings.MergeDictionary(job_settings); | 1424 modified_job_settings.MergeDictionary(job_settings); |
1376 modified_job_settings.SetBoolean(printing::kSettingHeaderFooterEnabled, | 1425 modified_job_settings.SetBoolean(printing::kSettingHeaderFooterEnabled, |
1377 false); | 1426 false); |
1378 | |
1379 // - On Windows, we don't add a margin until we turn it into an EMF when | |
1380 // printing for print preview (We could add it in the plugin). | |
1381 // - On Mac with Skia, we don't add a margin until we send it to the printer | |
1382 // using the CG PDF class (We could add it in the plugin). | |
1383 // - On Mac with CG, we can add a margin when generating the preview. | |
1384 // - On Linux, we never add a margin (We Could add it in the plugin). | |
1385 #if defined(OS_MACOSX) && !defined(USE_SKIA) | |
1386 bool get_margins_from_pdf = !source_is_html && !print_for_preview_; | |
1387 #elif defined(OS_WIN) || defined(OS_MACOSX) | |
1388 bool get_margins_from_pdf = !source_is_html && print_for_preview_; | |
1389 #else | |
1390 bool get_margins_from_pdf = false; | |
1391 #endif | |
1392 | |
1393 printing::MarginType margin_type = printing::NO_MARGINS; | |
1394 if (get_margins_from_pdf) | |
1395 margin_type = GetMarginsForPdf(frame, node); | |
1396 modified_job_settings.SetInteger(printing::kSettingMarginsType, | 1427 modified_job_settings.SetInteger(printing::kSettingMarginsType, |
1397 margin_type); | 1428 printing::NO_MARGINS); |
1398 job_settings = &modified_job_settings; | 1429 job_settings = &modified_job_settings; |
1399 } | 1430 } |
1400 | 1431 |
1401 // Send the cookie so that UpdatePrintSettings can reuse PrinterQuery when | 1432 // Send the cookie so that UpdatePrintSettings can reuse PrinterQuery when |
1402 // possible. | 1433 // possible. |
1403 int cookie = print_pages_params_.get() ? | 1434 int cookie = print_pages_params_.get() ? |
1404 print_pages_params_->params.document_cookie : 0; | 1435 print_pages_params_->params.document_cookie : 0; |
1405 PrintMsg_PrintPages_Params settings; | 1436 PrintMsg_PrintPages_Params settings; |
1406 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), | 1437 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), |
1407 cookie, *job_settings, &settings)); | 1438 cookie, *job_settings, &settings)); |
(...skipping 30 matching lines...) Expand all Loading... |
1438 !job_settings->GetInteger(printing::kPreviewRequestID, | 1469 !job_settings->GetInteger(printing::kPreviewRequestID, |
1439 &(settings.params.preview_request_id)) || | 1470 &(settings.params.preview_request_id)) || |
1440 !job_settings->GetBoolean(printing::kIsFirstRequest, | 1471 !job_settings->GetBoolean(printing::kIsFirstRequest, |
1441 &(settings.params.is_first_request))) { | 1472 &(settings.params.is_first_request))) { |
1442 NOTREACHED(); | 1473 NOTREACHED(); |
1443 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); | 1474 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); |
1444 return false; | 1475 return false; |
1445 } | 1476 } |
1446 | 1477 |
1447 settings.params.print_to_pdf = IsPrintToPdfRequested(*job_settings); | 1478 settings.params.print_to_pdf = IsPrintToPdfRequested(*job_settings); |
| 1479 settings.params.fit_to_paper_size = IsFitToPaperSizeRequested( |
| 1480 source_is_html, *job_settings, settings.params); |
1448 UpdateFrameMarginsCssInfo(*job_settings); | 1481 UpdateFrameMarginsCssInfo(*job_settings); |
1449 | 1482 |
1450 // Fit to paper size. | |
1451 settings.params.fit_to_paper_size = source_is_html && | |
1452 !IsPrintToPdfRequested(*job_settings); | |
1453 | |
1454 // Header/Footer: Set |header_footer_info_|. | 1483 // Header/Footer: Set |header_footer_info_|. |
1455 if (settings.params.display_header_footer) { | 1484 if (settings.params.display_header_footer) { |
1456 header_footer_info_.reset(new DictionaryValue()); | 1485 header_footer_info_.reset(new DictionaryValue()); |
1457 header_footer_info_->SetString(printing::kSettingHeaderFooterDate, | 1486 header_footer_info_->SetString(printing::kSettingHeaderFooterDate, |
1458 settings.params.date); | 1487 settings.params.date); |
1459 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, | 1488 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, |
1460 settings.params.url); | 1489 settings.params.url); |
1461 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle, | 1490 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle, |
1462 settings.params.title); | 1491 settings.params.title); |
1463 } | 1492 } |
(...skipping 19 matching lines...) Expand all Loading... |
1483 params.cookie = print_pages_params_->params.document_cookie; | 1512 params.cookie = print_pages_params_->params.document_cookie; |
1484 params.has_selection = frame->hasSelection(); | 1513 params.has_selection = frame->hasSelection(); |
1485 params.expected_pages_count = expected_pages_count; | 1514 params.expected_pages_count = expected_pages_count; |
1486 printing::MarginType margin_type = printing::DEFAULT_MARGINS; | 1515 printing::MarginType margin_type = printing::DEFAULT_MARGINS; |
1487 if (PrintingNodeOrPdfFrame(frame, node)) | 1516 if (PrintingNodeOrPdfFrame(frame, node)) |
1488 margin_type = GetMarginsForPdf(frame, node); | 1517 margin_type = GetMarginsForPdf(frame, node); |
1489 params.margin_type = margin_type; | 1518 params.margin_type = margin_type; |
1490 | 1519 |
1491 Send(new PrintHostMsg_DidShowPrintDialog(routing_id())); | 1520 Send(new PrintHostMsg_DidShowPrintDialog(routing_id())); |
1492 | 1521 |
| 1522 // PrintHostMsg_ScriptedPrint will reset print_scaling_option, so we save the |
| 1523 // value before and restore it afterwards. |
| 1524 bool fit_to_paper_size = print_pages_params_->params.fit_to_paper_size; |
| 1525 |
1493 print_pages_params_.reset(); | 1526 print_pages_params_.reset(); |
1494 IPC::SyncMessage* msg = | 1527 IPC::SyncMessage* msg = |
1495 new PrintHostMsg_ScriptedPrint(routing_id(), params, &print_settings); | 1528 new PrintHostMsg_ScriptedPrint(routing_id(), params, &print_settings); |
1496 msg->EnableMessagePumping(); | 1529 msg->EnableMessagePumping(); |
1497 Send(msg); | 1530 Send(msg); |
1498 print_pages_params_.reset(new PrintMsg_PrintPages_Params(print_settings)); | 1531 print_pages_params_.reset(new PrintMsg_PrintPages_Params(print_settings)); |
| 1532 |
| 1533 print_pages_params_->params.fit_to_paper_size = fit_to_paper_size; |
1499 return (print_settings.params.dpi && print_settings.params.document_cookie); | 1534 return (print_settings.params.dpi && print_settings.params.document_cookie); |
1500 } | 1535 } |
1501 | 1536 |
1502 bool PrintWebViewHelper::RenderPagesForPrint( | 1537 bool PrintWebViewHelper::RenderPagesForPrint( |
1503 WebKit::WebFrame* frame, | 1538 WebKit::WebFrame* frame, |
1504 const WebKit::WebNode& node) { | 1539 const WebKit::WebNode& node) { |
1505 if (print_pages_params_->params.selection_only) | 1540 if (print_pages_params_->params.selection_only) |
1506 return CopyAndPrint(frame); | 1541 return CopyAndPrint(frame); |
1507 return PrintPages(frame, node); | 1542 return PrintPages(frame, node); |
1508 } | 1543 } |
(...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1886 DCHECK(IsRendering()); | 1921 DCHECK(IsRendering()); |
1887 return prep_frame_view_->GetPrintCanvasSize(); | 1922 return prep_frame_view_->GetPrintCanvasSize(); |
1888 } | 1923 } |
1889 | 1924 |
1890 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { | 1925 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
1891 prep_frame_view_.reset(); | 1926 prep_frame_view_.reset(); |
1892 metafile_.reset(); | 1927 metafile_.reset(); |
1893 pages_to_render_.clear(); | 1928 pages_to_render_.clear(); |
1894 error_ = PREVIEW_ERROR_NONE; | 1929 error_ = PREVIEW_ERROR_NONE; |
1895 } | 1930 } |
OLD | NEW |