Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(249)

Side by Side Diff: components/printing/renderer/print_web_view_helper.cc

Issue 2454293004: Printing: Fix undefined behavior for near 0 scaling (Closed)
Patch Set: Move declaration Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | components/printing/renderer/print_web_view_helper_mac.mm » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "components/printing/renderer/print_web_view_helper.h" 5 #include "components/printing/renderer/print_web_view_helper.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 #include <string> 9 #include <string>
10 #include <utility> 10 #include <utility>
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 int GetDPI(const PrintMsg_Print_Params* print_params) { 107 int GetDPI(const PrintMsg_Print_Params* print_params) {
108 #if defined(OS_MACOSX) 108 #if defined(OS_MACOSX)
109 // On the Mac, the printable area is in points, don't do any scaling based 109 // On the Mac, the printable area is in points, don't do any scaling based
110 // on dpi. 110 // on dpi.
111 return kPointsPerInch; 111 return kPointsPerInch;
112 #else 112 #else
113 return static_cast<int>(print_params->dpi); 113 return static_cast<int>(print_params->dpi);
114 #endif // defined(OS_MACOSX) 114 #endif // defined(OS_MACOSX)
115 } 115 }
116 116
117 constexpr double kEpsilon = 0.01f;
Lei Zhang 2016/10/29 01:19:24 Sorry, I meant line 115 in the header file, that w
rbpotter 2016/10/31 16:30:24 Done.
118
117 bool PrintMsg_Print_Params_IsValid(const PrintMsg_Print_Params& params) { 119 bool PrintMsg_Print_Params_IsValid(const PrintMsg_Print_Params& params) {
118 return !params.content_size.IsEmpty() && !params.page_size.IsEmpty() && 120 return !params.content_size.IsEmpty() && !params.page_size.IsEmpty() &&
119 !params.printable_area.IsEmpty() && params.document_cookie && 121 !params.printable_area.IsEmpty() && params.document_cookie &&
120 params.desired_dpi && params.dpi && params.margin_top >= 0 && 122 params.desired_dpi && params.dpi && params.margin_top >= 0 &&
121 params.margin_left >= 0 && params.dpi > kMinDpi && 123 params.margin_left >= 0 && params.dpi > kMinDpi &&
122 params.document_cookie != 0; 124 params.document_cookie != 0;
123 } 125 }
124 126
125 // Helper function to check for fit to page 127 // Helper function to check for fit to page
126 bool IsWebPrintScalingOptionFitToPage(const PrintMsg_Print_Params& params) { 128 bool IsWebPrintScalingOptionFitToPage(const PrintMsg_Print_Params& params) {
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 void CalculatePageLayoutFromPrintParams( 229 void CalculatePageLayoutFromPrintParams(
228 const PrintMsg_Print_Params& params, 230 const PrintMsg_Print_Params& params,
229 double scale_factor, 231 double scale_factor,
230 PageSizeMargins* page_layout_in_points) { 232 PageSizeMargins* page_layout_in_points) {
231 bool fit_to_page = IsWebPrintScalingOptionFitToPage(params); 233 bool fit_to_page = IsWebPrintScalingOptionFitToPage(params);
232 int dpi = GetDPI(&params); 234 int dpi = GetDPI(&params);
233 int content_width = params.content_size.width(); 235 int content_width = params.content_size.width();
234 int content_height = params.content_size.height(); 236 int content_height = params.content_size.height();
235 // Scale the content to its normal size for purpose of computing page layout. 237 // Scale the content to its normal size for purpose of computing page layout.
236 // Otherwise we will get negative margins. 238 // Otherwise we will get negative margins.
237 if (scale_factor > 0 && (fit_to_page || params.print_to_pdf)) { 239 if (scale_factor >= kEpsilon && (fit_to_page || params.print_to_pdf)) {
238 content_width = 240 content_width =
239 static_cast<int>(static_cast<double>(content_width) * scale_factor); 241 static_cast<int>(static_cast<double>(content_width) * scale_factor);
240 content_height = 242 content_height =
241 static_cast<int>(static_cast<double>(content_height) * scale_factor); 243 static_cast<int>(static_cast<double>(content_height) * scale_factor);
242 } 244 }
243 245
244 int margin_bottom = 246 int margin_bottom =
245 params.page_size.height() - content_height - params.margin_top; 247 params.page_size.height() - content_height - params.margin_top;
246 int margin_right = 248 int margin_right =
247 params.page_size.width() - content_width - params.margin_left; 249 params.page_size.width() - content_width - params.margin_left;
(...skipping 466 matching lines...) Expand 10 before | Expand all | Expand 10 after
714 weak_ptr_factory_(this) { 716 weak_ptr_factory_(this) {
715 PrintMsg_Print_Params print_params = params; 717 PrintMsg_Print_Params print_params = params;
716 if (!should_print_selection_only_ || 718 if (!should_print_selection_only_ ||
717 !PrintingNodeOrPdfFrame(frame, node_to_print_)) { 719 !PrintingNodeOrPdfFrame(frame, node_to_print_)) {
718 bool fit_to_page = 720 bool fit_to_page =
719 ignore_css_margins && IsWebPrintScalingOptionFitToPage(print_params); 721 ignore_css_margins && IsWebPrintScalingOptionFitToPage(print_params);
720 ComputeWebKitPrintParamsInDesiredDpi(params, &web_print_params_); 722 ComputeWebKitPrintParamsInDesiredDpi(params, &web_print_params_);
721 frame->printBegin(web_print_params_, node_to_print_); 723 frame->printBegin(web_print_params_, node_to_print_);
722 double scale_factor = 1.0f; 724 double scale_factor = 1.0f;
723 #if defined(ENABLE_PRINT_PREVIEW) 725 #if defined(ENABLE_PRINT_PREVIEW)
724 if (print_params.scale_factor > 0) 726 if (print_params.scale_factor >= kEpsilon)
725 scale_factor = print_params.scale_factor; 727 scale_factor = print_params.scale_factor;
726 #endif 728 #endif
727 print_params = CalculatePrintParamsForCss( 729 print_params = CalculatePrintParamsForCss(
728 frame, 0, print_params, ignore_css_margins, fit_to_page, &scale_factor); 730 frame, 0, print_params, ignore_css_margins, fit_to_page, &scale_factor);
729 frame->printEnd(); 731 frame->printEnd();
730 } 732 }
731 ComputeWebKitPrintParamsInDesiredDpi(print_params, &web_print_params_); 733 ComputeWebKitPrintParamsInDesiredDpi(print_params, &web_print_params_);
732 } 734 }
733 735
734 PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() { 736 PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() {
(...skipping 490 matching lines...) Expand 10 before | Expand all | Expand 10 after
1225 const PrintMsg_Print_Params& print_params = print_pages_params_->params; 1227 const PrintMsg_Print_Params& print_params = print_pages_params_->params;
1226 const std::vector<int>& pages = print_pages_params_->pages; 1228 const std::vector<int>& pages = print_pages_params_->pages;
1227 1229
1228 if (!print_preview_context_.CreatePreviewDocument(prep_frame_view_.release(), 1230 if (!print_preview_context_.CreatePreviewDocument(prep_frame_view_.release(),
1229 pages)) { 1231 pages)) {
1230 return false; 1232 return false;
1231 } 1233 }
1232 1234
1233 PageSizeMargins default_page_layout; 1235 PageSizeMargins default_page_layout;
1234 double scale_factor = 1236 double scale_factor =
1235 print_params.scale_factor > 0 ? print_params.scale_factor : 1.0f; 1237 print_params.scale_factor >= kEpsilon ? print_params.scale_factor : 1.0f;
1236 1238
1237 ComputePageLayoutInPointsForCss(print_preview_context_.prepared_frame(), 0, 1239 ComputePageLayoutInPointsForCss(print_preview_context_.prepared_frame(), 0,
1238 print_params, ignore_css_margins_, 1240 print_params, ignore_css_margins_,
1239 &scale_factor, &default_page_layout); 1241 &scale_factor, &default_page_layout);
1240 bool has_page_size_style = 1242 bool has_page_size_style =
1241 PrintingFrameHasPageSizeStyle(print_preview_context_.prepared_frame(), 1243 PrintingFrameHasPageSizeStyle(print_preview_context_.prepared_frame(),
1242 print_preview_context_.total_page_count()); 1244 print_preview_context_.total_page_count());
1243 int dpi = GetDPI(&print_params); 1245 int dpi = GetDPI(&print_params);
1244 1246
1245 gfx::Rect printable_area_in_points( 1247 gfx::Rect printable_area_in_points(
(...skipping 575 matching lines...) Expand 10 before | Expand all | Expand 10 after
1821 void PrintWebViewHelper::PrintPageInternal( 1823 void PrintWebViewHelper::PrintPageInternal(
1822 const PrintMsg_PrintPage_Params& params, 1824 const PrintMsg_PrintPage_Params& params,
1823 blink::WebLocalFrame* frame, 1825 blink::WebLocalFrame* frame,
1824 PdfMetafileSkia* metafile, 1826 PdfMetafileSkia* metafile,
1825 gfx::Size* page_size_in_dpi, 1827 gfx::Size* page_size_in_dpi,
1826 gfx::Rect* content_area_in_dpi) { 1828 gfx::Rect* content_area_in_dpi) {
1827 PageSizeMargins page_layout_in_points; 1829 PageSizeMargins page_layout_in_points;
1828 1830
1829 double css_scale_factor = 1.0f; 1831 double css_scale_factor = 1.0f;
1830 #if defined(ENABLE_PRINT_PREVIEW) 1832 #if defined(ENABLE_PRINT_PREVIEW)
1831 if (params.params.scale_factor > 0) 1833 if (params.params.scale_factor >= kEpsilon)
1832 css_scale_factor = params.params.scale_factor; 1834 css_scale_factor = params.params.scale_factor;
1833 #endif 1835 #endif
1834 ComputePageLayoutInPointsForCss(frame, params.page_number, params.params, 1836 ComputePageLayoutInPointsForCss(frame, params.page_number, params.params,
1835 ignore_css_margins_, &css_scale_factor, 1837 ignore_css_margins_, &css_scale_factor,
1836 &page_layout_in_points); 1838 &page_layout_in_points);
1837 gfx::Size page_size; 1839 gfx::Size page_size;
1838 gfx::Rect content_area; 1840 gfx::Rect content_area;
1839 GetPageSizeAndContentAreaFromPageLayout(page_layout_in_points, &page_size, 1841 GetPageSizeAndContentAreaFromPageLayout(page_layout_in_points, &page_size,
1840 &content_area); 1842 &content_area);
1841 int dpi = static_cast<int>(params.params.dpi); 1843 int dpi = static_cast<int>(params.params.dpi);
(...skipping 491 matching lines...) Expand 10 before | Expand all | Expand 10 after
2333 blink::WebConsoleMessage::LevelWarning, message)); 2335 blink::WebConsoleMessage::LevelWarning, message));
2334 return false; 2336 return false;
2335 } 2337 }
2336 2338
2337 void PrintWebViewHelper::ScriptingThrottler::Reset() { 2339 void PrintWebViewHelper::ScriptingThrottler::Reset() {
2338 // Reset counter on successful print. 2340 // Reset counter on successful print.
2339 count_ = 0; 2341 count_ = 0;
2340 } 2342 }
2341 2343
2342 } // namespace printing 2344 } // namespace printing
OLDNEW
« no previous file with comments | « no previous file | components/printing/renderer/print_web_view_helper_mac.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698