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/printing/print_web_view_helper.h" | 5 #include "chrome/renderer/printing/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 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 } | 315 } |
316 | 316 |
317 bool FitToPageEnabled(const base::DictionaryValue& job_settings) { | 317 bool FitToPageEnabled(const base::DictionaryValue& job_settings) { |
318 bool fit_to_paper_size = false; | 318 bool fit_to_paper_size = false; |
319 if (!job_settings.GetBoolean(kSettingFitToPageEnabled, &fit_to_paper_size)) { | 319 if (!job_settings.GetBoolean(kSettingFitToPageEnabled, &fit_to_paper_size)) { |
320 NOTREACHED(); | 320 NOTREACHED(); |
321 } | 321 } |
322 return fit_to_paper_size; | 322 return fit_to_paper_size; |
323 } | 323 } |
324 | 324 |
| 325 // Returns the print scaling option to retain/scale/crop the source page size |
| 326 // to fit the printable area of the paper. |
| 327 // |
| 328 // We retain the source page size when the current destination printer is |
| 329 // SAVE_AS_PDF. |
| 330 // |
| 331 // We crop the source page size to fit the printable area or we print only the |
| 332 // left top page contents when |
| 333 // (1) Source is PDF and the user has requested not to fit to printable area |
| 334 // via |job_settings|. |
| 335 // (2) Source is PDF. This is the first preview request and print scaling |
| 336 // option is disabled for initiator renderer plugin. |
| 337 // |
| 338 // In all other cases, we scale the source page to fit the printable area. |
| 339 blink::WebPrintScalingOption GetPrintScalingOption( |
| 340 blink::WebFrame* frame, |
| 341 const blink::WebNode& node, |
| 342 bool source_is_html, |
| 343 const base::DictionaryValue& job_settings, |
| 344 const PrintMsg_Print_Params& params) { |
| 345 if (params.print_to_pdf) |
| 346 return blink::WebPrintScalingOptionSourceSize; |
| 347 |
| 348 if (!source_is_html) { |
| 349 if (!FitToPageEnabled(job_settings)) |
| 350 return blink::WebPrintScalingOptionNone; |
| 351 |
| 352 bool no_plugin_scaling = frame->isPrintScalingDisabledForPlugin(node); |
| 353 |
| 354 if (params.is_first_request && no_plugin_scaling) |
| 355 return blink::WebPrintScalingOptionNone; |
| 356 } |
| 357 return blink::WebPrintScalingOptionFitToPrintableArea; |
| 358 } |
| 359 |
325 PrintMsg_Print_Params CalculatePrintParamsForCss( | 360 PrintMsg_Print_Params CalculatePrintParamsForCss( |
326 blink::WebFrame* frame, | 361 blink::WebFrame* frame, |
327 int page_index, | 362 int page_index, |
328 const PrintMsg_Print_Params& page_params, | 363 const PrintMsg_Print_Params& page_params, |
329 bool ignore_css_margins, | 364 bool ignore_css_margins, |
330 bool fit_to_page, | 365 bool fit_to_page, |
331 double* scale_factor) { | 366 double* scale_factor) { |
332 PrintMsg_Print_Params css_params = GetCssPrintParams(frame, page_index, | 367 PrintMsg_Print_Params css_params = GetCssPrintParams(frame, page_index, |
333 page_params); | 368 page_params); |
334 | 369 |
(...skipping 583 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
918 } | 953 } |
919 | 954 |
920 bool PrintWebViewHelper::IsPrintToPdfRequested( | 955 bool PrintWebViewHelper::IsPrintToPdfRequested( |
921 const base::DictionaryValue& job_settings) { | 956 const base::DictionaryValue& job_settings) { |
922 bool print_to_pdf = false; | 957 bool print_to_pdf = false; |
923 if (!job_settings.GetBoolean(kSettingPrintToPDF, &print_to_pdf)) | 958 if (!job_settings.GetBoolean(kSettingPrintToPDF, &print_to_pdf)) |
924 NOTREACHED(); | 959 NOTREACHED(); |
925 return print_to_pdf; | 960 return print_to_pdf; |
926 } | 961 } |
927 | 962 |
928 blink::WebPrintScalingOption PrintWebViewHelper::GetPrintScalingOption( | |
929 bool source_is_html, const base::DictionaryValue& job_settings, | |
930 const PrintMsg_Print_Params& params) { | |
931 DCHECK(!print_for_preview_); | |
932 | |
933 if (params.print_to_pdf) | |
934 return blink::WebPrintScalingOptionSourceSize; | |
935 | |
936 if (!source_is_html) { | |
937 if (!FitToPageEnabled(job_settings)) | |
938 return blink::WebPrintScalingOptionNone; | |
939 | |
940 bool no_plugin_scaling = | |
941 print_preview_context_.source_frame()->isPrintScalingDisabledForPlugin( | |
942 print_preview_context_.source_node()); | |
943 | |
944 if (params.is_first_request && no_plugin_scaling) | |
945 return blink::WebPrintScalingOptionNone; | |
946 } | |
947 return blink::WebPrintScalingOptionFitToPrintableArea; | |
948 } | |
949 | |
950 void PrintWebViewHelper::OnPrintPreview(const base::DictionaryValue& settings) { | 963 void PrintWebViewHelper::OnPrintPreview(const base::DictionaryValue& settings) { |
951 DCHECK(is_preview_enabled_); | 964 DCHECK(is_preview_enabled_); |
952 print_preview_context_.OnPrintPreview(); | 965 print_preview_context_.OnPrintPreview(); |
953 | 966 |
954 UMA_HISTOGRAM_ENUMERATION("PrintPreview.PreviewEvent", | 967 UMA_HISTOGRAM_ENUMERATION("PrintPreview.PreviewEvent", |
955 PREVIEW_EVENT_REQUESTED, PREVIEW_EVENT_MAX); | 968 PREVIEW_EVENT_REQUESTED, PREVIEW_EVENT_MAX); |
956 | 969 if (!print_preview_context_.source_frame() || |
957 if (!UpdatePrintSettings(print_preview_context_.source_frame(), | 970 !UpdatePrintSettings(print_preview_context_.source_frame(), |
958 print_preview_context_.source_node(), settings)) { | 971 print_preview_context_.source_node(), settings)) { |
959 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) { | 972 if (print_preview_context_.last_error() != PREVIEW_ERROR_BAD_SETTING) { |
960 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings( | 973 Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings( |
961 routing_id(), print_pages_params_->params.document_cookie)); | 974 routing_id(), print_pages_params_->params.document_cookie)); |
962 notify_browser_of_print_failure_ = false; // Already sent. | 975 notify_browser_of_print_failure_ = false; // Already sent. |
963 } | 976 } |
964 DidFinishPrinting(FAIL_PREVIEW); | 977 DidFinishPrinting(FAIL_PREVIEW); |
965 return; | 978 return; |
966 } | 979 } |
967 | 980 |
(...skipping 518 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1486 !job_settings->GetBoolean(kIsFirstRequest, | 1499 !job_settings->GetBoolean(kIsFirstRequest, |
1487 &settings.params.is_first_request)) { | 1500 &settings.params.is_first_request)) { |
1488 NOTREACHED(); | 1501 NOTREACHED(); |
1489 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); | 1502 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); |
1490 return false; | 1503 return false; |
1491 } | 1504 } |
1492 | 1505 |
1493 settings.params.print_to_pdf = IsPrintToPdfRequested(*job_settings); | 1506 settings.params.print_to_pdf = IsPrintToPdfRequested(*job_settings); |
1494 UpdateFrameMarginsCssInfo(*job_settings); | 1507 UpdateFrameMarginsCssInfo(*job_settings); |
1495 settings.params.print_scaling_option = GetPrintScalingOption( | 1508 settings.params.print_scaling_option = GetPrintScalingOption( |
1496 source_is_html, *job_settings, settings.params); | 1509 frame, node, source_is_html, *job_settings, settings.params); |
1497 | 1510 |
1498 // Header/Footer: Set |header_footer_info_|. | 1511 // Header/Footer: Set |header_footer_info_|. |
1499 if (settings.params.display_header_footer) { | 1512 if (settings.params.display_header_footer) { |
1500 header_footer_info_.reset(new base::DictionaryValue()); | 1513 header_footer_info_.reset(new base::DictionaryValue()); |
1501 header_footer_info_->SetDouble(kSettingHeaderFooterDate, | 1514 header_footer_info_->SetDouble(kSettingHeaderFooterDate, |
1502 base::Time::Now().ToJsTime()); | 1515 base::Time::Now().ToJsTime()); |
1503 header_footer_info_->SetString(kSettingHeaderFooterURL, | 1516 header_footer_info_->SetString(kSettingHeaderFooterURL, |
1504 settings.params.url); | 1517 settings.params.url); |
1505 header_footer_info_->SetString(kSettingHeaderFooterTitle, | 1518 header_footer_info_->SetString(kSettingHeaderFooterTitle, |
1506 settings.params.title); | 1519 settings.params.title); |
(...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1979 } | 1992 } |
1980 | 1993 |
1981 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { | 1994 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
1982 prep_frame_view_.reset(); | 1995 prep_frame_view_.reset(); |
1983 metafile_.reset(); | 1996 metafile_.reset(); |
1984 pages_to_render_.clear(); | 1997 pages_to_render_.clear(); |
1985 error_ = PREVIEW_ERROR_NONE; | 1998 error_ = PREVIEW_ERROR_NONE; |
1986 } | 1999 } |
1987 | 2000 |
1988 } // namespace printing | 2001 } // namespace printing |
OLD | NEW |