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 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 return frame_has_custom_page_size_style; | 334 return frame_has_custom_page_size_style; |
335 } | 335 } |
336 | 336 |
337 printing::MarginType GetMarginsForPdf(WebFrame* frame, const WebNode& node) { | 337 printing::MarginType GetMarginsForPdf(WebFrame* frame, const WebNode& node) { |
338 if (frame->isPrintScalingDisabledForPlugin(node)) | 338 if (frame->isPrintScalingDisabledForPlugin(node)) |
339 return printing::NO_MARGINS; | 339 return printing::NO_MARGINS; |
340 else | 340 else |
341 return printing::PRINTABLE_AREA_MARGINS; | 341 return printing::PRINTABLE_AREA_MARGINS; |
342 } | 342 } |
343 | 343 |
| 344 bool FitToPageEnabled(const DictionaryValue& job_settings) { |
| 345 bool fit_to_paper_size = false; |
| 346 if (!job_settings.GetBoolean(printing::kSettingFitToPageEnabled, |
| 347 &fit_to_paper_size)) { |
| 348 NOTREACHED(); |
| 349 } |
| 350 return fit_to_paper_size; |
| 351 } |
| 352 |
344 // Get the (x, y) coordinate from where printing of the current text should | 353 // Get the (x, y) coordinate from where printing of the current text should |
345 // start depending on the horizontal alignment (LEFT, RIGHT, CENTER) and | 354 // start depending on the horizontal alignment (LEFT, RIGHT, CENTER) and |
346 // vertical alignment (TOP, BOTTOM). | 355 // vertical alignment (TOP, BOTTOM). |
347 SkPoint GetHeaderFooterPosition( | 356 SkPoint GetHeaderFooterPosition( |
348 float webkit_scale_factor, | 357 float webkit_scale_factor, |
349 const PageSizeMargins& page_layout, | 358 const PageSizeMargins& page_layout, |
350 printing::HorizontalHeaderFooterPosition horizontal_position, | 359 printing::HorizontalHeaderFooterPosition horizontal_position, |
351 printing::VerticalHeaderFooterPosition vertical_position, | 360 printing::VerticalHeaderFooterPosition vertical_position, |
352 double offset_to_baseline, | 361 double offset_to_baseline, |
353 double text_width_in_points) { | 362 double text_width_in_points) { |
(...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
826 } | 835 } |
827 | 836 |
828 bool PrintWebViewHelper::IsPrintToPdfRequested( | 837 bool PrintWebViewHelper::IsPrintToPdfRequested( |
829 const DictionaryValue& job_settings) { | 838 const DictionaryValue& job_settings) { |
830 bool print_to_pdf = false; | 839 bool print_to_pdf = false; |
831 if (!job_settings.GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf)) | 840 if (!job_settings.GetBoolean(printing::kSettingPrintToPDF, &print_to_pdf)) |
832 NOTREACHED(); | 841 NOTREACHED(); |
833 return print_to_pdf; | 842 return print_to_pdf; |
834 } | 843 } |
835 | 844 |
| 845 void PrintWebViewHelper::UpdateFitToPageInfo( |
| 846 WebKit::WebFrame* frame, const WebKit::WebNode& node, |
| 847 const DictionaryValue& job_settings, PrintMsg_PrintPages_Params* settings) { |
| 848 bool source_is_html = true; |
| 849 if (print_for_preview_) { |
| 850 if (!job_settings.GetBoolean(printing::kSettingPreviewModifiable, |
| 851 &source_is_html)) { |
| 852 NOTREACHED(); |
| 853 } |
| 854 } else { |
| 855 source_is_html = !PrintingNodeOrFrame(frame, node); |
| 856 } |
| 857 |
| 858 settings->params.fit_to_paper_size = true; |
| 859 if (print_for_preview_ || settings->params.print_to_pdf) { |
| 860 // Do not fit to paper size when the user is printing the preview data or |
| 861 // saving the print contents as pdf. |
| 862 settings->params.fit_to_paper_size = false; |
| 863 } else if (!source_is_html) { |
| 864 // PDF in initiator renderer. Get the print scaling option for the initiator |
| 865 // renderer pdf. |
| 866 bool print_scaling_disabled = frame->isPrintScalingDisabledForPlugin(node); |
| 867 |
| 868 // If this is the first preview request, UI doesn't know about the print |
| 869 // scaling option of the plugin. Therefore, check the print scaling option |
| 870 // and update the print params accordingly. |
| 871 // |
| 872 // If this is not the first preview request, update print params based on |
| 873 // preview job settings. |
| 874 if ((settings.params.is_first_request && |
| 875 print_scaling_disabled_for_plugin) || |
| 876 !FitToPageEnabled(job_settings)) { |
| 877 settings->params.fit_to_paper_size = false; |
| 878 } |
| 879 } |
| 880 } |
| 881 |
836 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { | 882 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { |
837 DCHECK(is_preview_enabled_); | 883 DCHECK(is_preview_enabled_); |
838 print_preview_context_.OnPrintPreview(); | 884 print_preview_context_.OnPrintPreview(); |
839 | 885 |
840 if (!UpdatePrintSettings(print_preview_context_.frame(), | 886 if (!UpdatePrintSettings(print_preview_context_.frame(), |
841 print_preview_context_.node(), settings)) { | 887 print_preview_context_.node(), settings)) { |
842 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) { | 888 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) { |
843 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings( | 889 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings( |
844 routing_id(), print_pages_params_->params.document_cookie)); | 890 routing_id(), print_pages_params_->params.document_cookie)); |
845 notify_browser_of_print_failure_ = false; // Already sent. | 891 notify_browser_of_print_failure_ = false; // Already sent. |
(...skipping 14 matching lines...) Expand all Loading... |
860 preview_params.expected_pages_count = | 906 preview_params.expected_pages_count = |
861 print_preview_context_.total_page_count(); | 907 print_preview_context_.total_page_count(); |
862 preview_params.modifiable = print_preview_context_.IsModifiable(); | 908 preview_params.modifiable = print_preview_context_.IsModifiable(); |
863 preview_params.preview_request_id = | 909 preview_params.preview_request_id = |
864 print_pages_params_->params.preview_request_id; | 910 print_pages_params_->params.preview_request_id; |
865 | 911 |
866 Send(new PrintHostMsg_MetafileReadyForPrinting(routing_id(), | 912 Send(new PrintHostMsg_MetafileReadyForPrinting(routing_id(), |
867 preview_params)); | 913 preview_params)); |
868 return; | 914 return; |
869 } | 915 } |
| 916 |
| 917 // If we are previewing a pdf and the print scaling is disabled, send a |
| 918 // message to browser. |
| 919 if (!print_preview_context_.IsModifiable() && |
| 920 print_pages_params_->params.is_first_request && |
| 921 print_preview_context_.frame()->isPrintScalingDisabledForPlugin( |
| 922 print_preview_context_.node())) { |
| 923 Send(new PrintHostMsg_PrintPreviewScalingDisabled(routing_id())); |
| 924 } |
| 925 |
870 // Always clear |old_print_pages_params_| before rendering the pages. | 926 // Always clear |old_print_pages_params_| before rendering the pages. |
871 old_print_pages_params_.reset(); | 927 old_print_pages_params_.reset(); |
872 is_print_ready_metafile_sent_ = false; | 928 is_print_ready_metafile_sent_ = false; |
873 | 929 |
874 // PDF printer device supports alpha blending. | 930 // PDF printer device supports alpha blending. |
875 print_pages_params_->params.supports_alpha_blend = true; | 931 print_pages_params_->params.supports_alpha_blend = true; |
876 | 932 |
877 bool generate_draft_pages = false; | 933 bool generate_draft_pages = false; |
878 if (!settings.GetBoolean(printing::kSettingGenerateDraftData, | 934 if (!settings.GetBoolean(printing::kSettingGenerateDraftData, |
879 &generate_draft_pages)) { | 935 &generate_draft_pages)) { |
(...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1303 } | 1359 } |
1304 } else { | 1360 } else { |
1305 source_is_html = !PrintingNodeOrPdfFrame(frame, node); | 1361 source_is_html = !PrintingNodeOrPdfFrame(frame, node); |
1306 } | 1362 } |
1307 | 1363 |
1308 if (print_for_preview_ || !source_is_html) { | 1364 if (print_for_preview_ || !source_is_html) { |
1309 modified_job_settings.MergeDictionary(job_settings); | 1365 modified_job_settings.MergeDictionary(job_settings); |
1310 modified_job_settings.SetBoolean(printing::kSettingHeaderFooterEnabled, | 1366 modified_job_settings.SetBoolean(printing::kSettingHeaderFooterEnabled, |
1311 false); | 1367 false); |
1312 | 1368 |
1313 // - On Windows, we don't add a margin until we turn it into an EMF when | |
1314 // printing for print preview (We could add it in the plugin). | |
1315 // - On Mac with Skia, we don't add a margin until we send it to the printer | |
1316 // using the CG PDF class (We could add it in the plugin). | |
1317 // - On Mac with CG, we can add a margin when generating the preview. | |
1318 // - On Linux, we never add a margin (We Could add it in the plugin). | |
1319 #if defined(OS_MACOSX) && !defined(USE_SKIA) | |
1320 bool get_margins_from_pdf = !source_is_html && !print_for_preview_; | |
1321 #elif defined(OS_WIN) || defined(OS_MACOSX) | |
1322 bool get_margins_from_pdf = !source_is_html && print_for_preview_; | |
1323 #else | |
1324 bool get_margins_from_pdf = false; | |
1325 #endif | |
1326 | |
1327 printing::MarginType margin_type = printing::NO_MARGINS; | 1369 printing::MarginType margin_type = printing::NO_MARGINS; |
1328 if (get_margins_from_pdf) | |
1329 margin_type = GetMarginsForPdf(frame, node); | |
1330 modified_job_settings.SetInteger(printing::kSettingMarginsType, | 1370 modified_job_settings.SetInteger(printing::kSettingMarginsType, |
1331 margin_type); | 1371 margin_type); |
1332 job_settings = &modified_job_settings; | 1372 job_settings = &modified_job_settings; |
1333 } | 1373 } |
1334 | 1374 |
1335 // Send the cookie so that UpdatePrintSettings can reuse PrinterQuery when | 1375 // Send the cookie so that UpdatePrintSettings can reuse PrinterQuery when |
1336 // possible. | 1376 // possible. |
1337 int cookie = print_pages_params_.get() ? | 1377 int cookie = print_pages_params_.get() ? |
1338 print_pages_params_->params.document_cookie : 0; | 1378 print_pages_params_->params.document_cookie : 0; |
1339 PrintMsg_PrintPages_Params settings; | 1379 PrintMsg_PrintPages_Params settings; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1373 &(settings.params.preview_request_id)) || | 1413 &(settings.params.preview_request_id)) || |
1374 !job_settings->GetBoolean(printing::kIsFirstRequest, | 1414 !job_settings->GetBoolean(printing::kIsFirstRequest, |
1375 &(settings.params.is_first_request))) { | 1415 &(settings.params.is_first_request))) { |
1376 NOTREACHED(); | 1416 NOTREACHED(); |
1377 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); | 1417 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); |
1378 return false; | 1418 return false; |
1379 } | 1419 } |
1380 | 1420 |
1381 settings.params.print_to_pdf = IsPrintToPdfRequested(*job_settings); | 1421 settings.params.print_to_pdf = IsPrintToPdfRequested(*job_settings); |
1382 UpdateFrameMarginsCssInfo(*job_settings); | 1422 UpdateFrameMarginsCssInfo(*job_settings); |
1383 | 1423 UpdateFitToPageInfo(frame, node, job_settings, &settings); |
1384 // Fit to paper size. | |
1385 settings.params.fit_to_paper_size = source_is_html && | |
1386 !IsPrintToPdfRequested(*job_settings); | |
1387 | 1424 |
1388 // Header/Footer: Set |header_footer_info_|. | 1425 // Header/Footer: Set |header_footer_info_|. |
1389 if (settings.params.display_header_footer) { | 1426 if (settings.params.display_header_footer) { |
1390 header_footer_info_.reset(new DictionaryValue()); | 1427 header_footer_info_.reset(new DictionaryValue()); |
1391 header_footer_info_->SetString(printing::kSettingHeaderFooterDate, | 1428 header_footer_info_->SetString(printing::kSettingHeaderFooterDate, |
1392 settings.params.date); | 1429 settings.params.date); |
1393 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, | 1430 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, |
1394 settings.params.url); | 1431 settings.params.url); |
1395 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle, | 1432 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle, |
1396 settings.params.title); | 1433 settings.params.title); |
(...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1820 DCHECK(IsRendering()); | 1857 DCHECK(IsRendering()); |
1821 return prep_frame_view_->GetPrintCanvasSize(); | 1858 return prep_frame_view_->GetPrintCanvasSize(); |
1822 } | 1859 } |
1823 | 1860 |
1824 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { | 1861 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
1825 prep_frame_view_.reset(); | 1862 prep_frame_view_.reset(); |
1826 metafile_.reset(); | 1863 metafile_.reset(); |
1827 pages_to_render_.clear(); | 1864 pages_to_render_.clear(); |
1828 error_ = PREVIEW_ERROR_NONE; | 1865 error_ = PREVIEW_ERROR_NONE; |
1829 } | 1866 } |
OLD | NEW |