Chromium Code Reviews| 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 bool PrintWebViewHelper::IsFitToPaperSizeRequested( | |
| 846 bool source_is_html, const DictionaryValue& job_settings, | |
| 847 const PrintMsg_Print_Params& params) { | |
| 848 DCHECK(!print_for_preview_); | |
| 849 | |
| 850 // Do not fit to paper size when the user is saving the print contents as pdf. | |
| 851 if (params.print_to_pdf) | |
| 852 return false; | |
| 853 | |
| 854 if (!source_is_html) { | |
| 855 if (!FitToPageEnabled(job_settings)) | |
| 856 return false; | |
| 857 | |
| 858 // Get the print scaling option for the initiator renderer pdf. | |
| 859 bool print_scaling_disabled_for_plugin = | |
| 860 print_preview_context_.frame()->isPrintScalingDisabledForPlugin( | |
| 861 print_preview_context_.node()); | |
| 862 | |
| 863 // If this is the first preview request, UI doesn't know about the print | |
| 864 // scaling option of the plugin. Therefore, check the print scaling option | |
| 865 // and update the print params accordingly. | |
| 866 // | |
| 867 // If this is not the first preview request, update print params based on | |
| 868 // preview job settings. | |
| 869 if (params.is_first_request && print_scaling_disabled_for_plugin) | |
| 870 return false; | |
| 871 } | |
| 872 return true; | |
| 873 } | |
| 874 | |
| 836 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { | 875 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { |
| 837 DCHECK(is_preview_enabled_); | 876 DCHECK(is_preview_enabled_); |
| 838 print_preview_context_.OnPrintPreview(); | 877 print_preview_context_.OnPrintPreview(); |
| 839 | 878 |
| 840 if (!UpdatePrintSettings(print_preview_context_.frame(), | 879 if (!UpdatePrintSettings(print_preview_context_.frame(), |
| 841 print_preview_context_.node(), settings)) { | 880 print_preview_context_.node(), settings)) { |
| 842 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) { | 881 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) { |
| 843 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings( | 882 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings( |
| 844 routing_id(), print_pages_params_->params.document_cookie)); | 883 routing_id(), print_pages_params_->params.document_cookie)); |
| 845 notify_browser_of_print_failure_ = false; // Already sent. | 884 notify_browser_of_print_failure_ = false; // Already sent. |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 860 preview_params.expected_pages_count = | 899 preview_params.expected_pages_count = |
| 861 print_preview_context_.total_page_count(); | 900 print_preview_context_.total_page_count(); |
| 862 preview_params.modifiable = print_preview_context_.IsModifiable(); | 901 preview_params.modifiable = print_preview_context_.IsModifiable(); |
| 863 preview_params.preview_request_id = | 902 preview_params.preview_request_id = |
| 864 print_pages_params_->params.preview_request_id; | 903 print_pages_params_->params.preview_request_id; |
| 865 | 904 |
| 866 Send(new PrintHostMsg_MetafileReadyForPrinting(routing_id(), | 905 Send(new PrintHostMsg_MetafileReadyForPrinting(routing_id(), |
| 867 preview_params)); | 906 preview_params)); |
| 868 return; | 907 return; |
| 869 } | 908 } |
| 909 | |
| 910 // If we are previewing a pdf and the print scaling is disabled, send a | |
| 911 // message to browser. | |
| 912 if (!print_preview_context_.IsModifiable() && | |
| 913 print_pages_params_->params.is_first_request && | |
|
Lei Zhang
2012/05/02 06:42:36
nit: if you check this first, it's a much cheaper
kmadhusu
2012/05/06 20:10:15
Done.
| |
| 914 print_preview_context_.frame()->isPrintScalingDisabledForPlugin( | |
| 915 print_preview_context_.node())) { | |
| 916 Send(new PrintHostMsg_PrintPreviewScalingDisabled(routing_id())); | |
| 917 } | |
| 918 | |
| 870 // Always clear |old_print_pages_params_| before rendering the pages. | 919 // Always clear |old_print_pages_params_| before rendering the pages. |
| 871 old_print_pages_params_.reset(); | 920 old_print_pages_params_.reset(); |
| 872 is_print_ready_metafile_sent_ = false; | 921 is_print_ready_metafile_sent_ = false; |
| 873 | 922 |
| 874 // PDF printer device supports alpha blending. | 923 // PDF printer device supports alpha blending. |
| 875 print_pages_params_->params.supports_alpha_blend = true; | 924 print_pages_params_->params.supports_alpha_blend = true; |
| 876 | 925 |
| 877 bool generate_draft_pages = false; | 926 bool generate_draft_pages = false; |
| 878 if (!settings.GetBoolean(printing::kSettingGenerateDraftData, | 927 if (!settings.GetBoolean(printing::kSettingGenerateDraftData, |
| 879 &generate_draft_pages)) { | 928 &generate_draft_pages)) { |
| (...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1302 NOTREACHED(); | 1351 NOTREACHED(); |
| 1303 } | 1352 } |
| 1304 } else { | 1353 } else { |
| 1305 source_is_html = !PrintingNodeOrPdfFrame(frame, node); | 1354 source_is_html = !PrintingNodeOrPdfFrame(frame, node); |
| 1306 } | 1355 } |
| 1307 | 1356 |
| 1308 if (print_for_preview_ || !source_is_html) { | 1357 if (print_for_preview_ || !source_is_html) { |
| 1309 modified_job_settings.MergeDictionary(job_settings); | 1358 modified_job_settings.MergeDictionary(job_settings); |
| 1310 modified_job_settings.SetBoolean(printing::kSettingHeaderFooterEnabled, | 1359 modified_job_settings.SetBoolean(printing::kSettingHeaderFooterEnabled, |
| 1311 false); | 1360 false); |
| 1312 | |
| 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; | |
| 1328 if (get_margins_from_pdf) | |
| 1329 margin_type = GetMarginsForPdf(frame, node); | |
| 1330 modified_job_settings.SetInteger(printing::kSettingMarginsType, | 1361 modified_job_settings.SetInteger(printing::kSettingMarginsType, |
| 1331 margin_type); | 1362 printing::NO_MARGINS); |
| 1332 job_settings = &modified_job_settings; | 1363 job_settings = &modified_job_settings; |
| 1333 } | 1364 } |
| 1334 | 1365 |
| 1335 // Send the cookie so that UpdatePrintSettings can reuse PrinterQuery when | 1366 // Send the cookie so that UpdatePrintSettings can reuse PrinterQuery when |
| 1336 // possible. | 1367 // possible. |
| 1337 int cookie = print_pages_params_.get() ? | 1368 int cookie = print_pages_params_.get() ? |
| 1338 print_pages_params_->params.document_cookie : 0; | 1369 print_pages_params_->params.document_cookie : 0; |
| 1339 PrintMsg_PrintPages_Params settings; | 1370 PrintMsg_PrintPages_Params settings; |
| 1340 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), | 1371 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), |
| 1341 cookie, *job_settings, &settings)); | 1372 cookie, *job_settings, &settings)); |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 1372 !job_settings->GetInteger(printing::kPreviewRequestID, | 1403 !job_settings->GetInteger(printing::kPreviewRequestID, |
| 1373 &(settings.params.preview_request_id)) || | 1404 &(settings.params.preview_request_id)) || |
| 1374 !job_settings->GetBoolean(printing::kIsFirstRequest, | 1405 !job_settings->GetBoolean(printing::kIsFirstRequest, |
| 1375 &(settings.params.is_first_request))) { | 1406 &(settings.params.is_first_request))) { |
| 1376 NOTREACHED(); | 1407 NOTREACHED(); |
| 1377 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); | 1408 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); |
| 1378 return false; | 1409 return false; |
| 1379 } | 1410 } |
| 1380 | 1411 |
| 1381 settings.params.print_to_pdf = IsPrintToPdfRequested(*job_settings); | 1412 settings.params.print_to_pdf = IsPrintToPdfRequested(*job_settings); |
| 1413 settings.params.fit_to_paper_size = IsFitToPaperSizeRequested( | |
| 1414 source_is_html, *job_settings, settings.params); | |
| 1382 UpdateFrameMarginsCssInfo(*job_settings); | 1415 UpdateFrameMarginsCssInfo(*job_settings); |
| 1383 | 1416 |
| 1384 // Fit to paper size. | |
| 1385 settings.params.fit_to_paper_size = source_is_html && | |
| 1386 !IsPrintToPdfRequested(*job_settings); | |
| 1387 | |
| 1388 // Header/Footer: Set |header_footer_info_|. | 1417 // Header/Footer: Set |header_footer_info_|. |
| 1389 if (settings.params.display_header_footer) { | 1418 if (settings.params.display_header_footer) { |
| 1390 header_footer_info_.reset(new DictionaryValue()); | 1419 header_footer_info_.reset(new DictionaryValue()); |
| 1391 header_footer_info_->SetString(printing::kSettingHeaderFooterDate, | 1420 header_footer_info_->SetString(printing::kSettingHeaderFooterDate, |
| 1392 settings.params.date); | 1421 settings.params.date); |
| 1393 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, | 1422 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, |
| 1394 settings.params.url); | 1423 settings.params.url); |
| 1395 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle, | 1424 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle, |
| 1396 settings.params.title); | 1425 settings.params.title); |
| 1397 } | 1426 } |
| (...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1820 DCHECK(IsRendering()); | 1849 DCHECK(IsRendering()); |
| 1821 return prep_frame_view_->GetPrintCanvasSize(); | 1850 return prep_frame_view_->GetPrintCanvasSize(); |
| 1822 } | 1851 } |
| 1823 | 1852 |
| 1824 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { | 1853 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
| 1825 prep_frame_view_.reset(); | 1854 prep_frame_view_.reset(); |
| 1826 metafile_.reset(); | 1855 metafile_.reset(); |
| 1827 pages_to_render_.clear(); | 1856 pages_to_render_.clear(); |
| 1828 error_ = PREVIEW_ERROR_NONE; | 1857 error_ = PREVIEW_ERROR_NONE; |
| 1829 } | 1858 } |
| OLD | NEW |