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

Side by Side Diff: chrome/renderer/print_web_view_helper.cc

Issue 10168013: [Print Preview] Modified PrepareFrameAndViewPrint class to call the new printBegin function to supp… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: '' Created 8 years, 8 months 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 | Annotate | Revision Log
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 "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 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 const PrintMsg_PrintPages_Params& oldParams, 126 const PrintMsg_PrintPages_Params& oldParams,
127 const PrintMsg_PrintPages_Params& newParams) { 127 const PrintMsg_PrintPages_Params& newParams) {
128 return PageLayoutIsEqual(oldParams, newParams) && 128 return PageLayoutIsEqual(oldParams, newParams) &&
129 oldParams.params.max_shrink == newParams.params.max_shrink && 129 oldParams.params.max_shrink == newParams.params.max_shrink &&
130 oldParams.params.min_shrink == newParams.params.min_shrink && 130 oldParams.params.min_shrink == newParams.params.min_shrink &&
131 oldParams.params.selection_only == newParams.params.selection_only && 131 oldParams.params.selection_only == newParams.params.selection_only &&
132 oldParams.params.supports_alpha_blend == 132 oldParams.params.supports_alpha_blend ==
133 newParams.params.supports_alpha_blend && 133 newParams.params.supports_alpha_blend &&
134 oldParams.pages.size() == newParams.pages.size() && 134 oldParams.pages.size() == newParams.pages.size() &&
135 oldParams.params.print_to_pdf == newParams.params.print_to_pdf && 135 oldParams.params.print_to_pdf == newParams.params.print_to_pdf &&
136 oldParams.params.fit_to_paper_size == 136 oldParams.params.print_scaling_option ==
137 newParams.params.fit_to_paper_size && 137 newParams.params.print_scaling_option &&
138 oldParams.params.display_header_footer == 138 oldParams.params.display_header_footer ==
139 newParams.params.display_header_footer && 139 newParams.params.display_header_footer &&
140 oldParams.params.date == newParams.params.date && 140 oldParams.params.date == newParams.params.date &&
141 oldParams.params.title == newParams.params.title && 141 oldParams.params.title == newParams.params.title &&
142 oldParams.params.url == newParams.params.url && 142 oldParams.params.url == newParams.params.url &&
143 std::equal(oldParams.pages.begin(), oldParams.pages.end(), 143 std::equal(oldParams.pages.begin(), oldParams.pages.end(),
144 newParams.pages.begin()); 144 newParams.pages.begin());
145 } 145 }
146 146
147 PrintMsg_Print_Params GetCssPrintParams( 147 PrintMsg_Print_Params GetCssPrintParams(
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 // Swap the |width| and |height| values. 295 // Swap the |width| and |height| values.
296 page_params->page_size.SetSize(page_params->page_size.height(), 296 page_params->page_size.SetSize(page_params->page_size.height(),
297 page_params->page_size.width()); 297 page_params->page_size.width());
298 page_params->content_size.SetSize(page_params->content_size.height(), 298 page_params->content_size.SetSize(page_params->content_size.height(),
299 page_params->content_size.width()); 299 page_params->content_size.width());
300 page_params->printable_area.set_size( 300 page_params->printable_area.set_size(
301 gfx::Size(page_params->printable_area.height(), 301 gfx::Size(page_params->printable_area.height(),
302 page_params->printable_area.width())); 302 page_params->printable_area.width()));
303 } 303 }
304 304
305 void CalculatePrintCanvasSize(const PrintMsg_Print_Params& print_params, 305 void ComputePrintParamsInDesiredDpi(const PrintMsg_Print_Params& print_params,
306 gfx::Size* result) { 306 gfx::Size* content_size,
307 gfx::Rect* printable_area,
308 gfx::Size* paper_size) {
307 int dpi = GetDPI(&print_params); 309 int dpi = GetDPI(&print_params);
308 result->set_width(ConvertUnit(print_params.content_size.width(), dpi, 310 content_size->set_width(ConvertUnit(print_params.content_size.width(), dpi,
309 print_params.desired_dpi)); 311 print_params.desired_dpi));
310 312
311 result->set_height(ConvertUnit(print_params.content_size.height(), dpi, 313 content_size->set_height(ConvertUnit(print_params.content_size.height(), dpi,
312 print_params.desired_dpi)); 314 print_params.desired_dpi));
315
316 printable_area->SetRect(ConvertUnit(print_params.printable_area.x(), dpi,
317 print_params.desired_dpi),
318 ConvertUnit(print_params.printable_area.y(), dpi,
319 print_params.desired_dpi),
320 ConvertUnit(print_params.printable_area.width(), dpi,
321 print_params.desired_dpi),
322 ConvertUnit(print_params.printable_area.height(), dpi,
323 print_params.desired_dpi));
324 paper_size->SetSize(ConvertUnit(print_params.page_size.width(), dpi,
325 print_params.desired_dpi),
326 ConvertUnit(print_params.page_size.height(), dpi,
327 print_params.desired_dpi));
313 } 328 }
314 329
315 bool PrintingNodeOrPdfFrame(const WebFrame* frame, const WebNode& node) { 330 bool PrintingNodeOrPdfFrame(const WebFrame* frame, const WebNode& node) {
316 if (!node.isNull()) 331 if (!node.isNull())
317 return true; 332 return true;
318 if (!frame->document().isPluginDocument()) 333 if (!frame->document().isPluginDocument())
319 return false; 334 return false;
320 WebPlugin* plugin = frame->document().to<WebPluginDocument>().plugin(); 335 WebPlugin* plugin = frame->document().to<WebPluginDocument>().plugin();
321 return plugin && plugin->supportsPaginatedPrint(); 336 return plugin && plugin->supportsPaginatedPrint();
322 } 337 }
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after
600 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( 615 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint(
601 const PrintMsg_Print_Params& print_params, 616 const PrintMsg_Print_Params& print_params,
602 WebFrame* frame, 617 WebFrame* frame,
603 const WebNode& node) 618 const WebNode& node)
604 : frame_(frame), 619 : frame_(frame),
605 node_to_print_(node), 620 node_to_print_(node),
606 web_view_(frame->view()), 621 web_view_(frame->view()),
607 dpi_(static_cast<int>(print_params.dpi)), 622 dpi_(static_cast<int>(print_params.dpi)),
608 expected_pages_count_(0), 623 expected_pages_count_(0),
609 use_browser_overlays_(true), 624 use_browser_overlays_(true),
625 print_scaling_option_(print_params.print_scaling_option),
610 finished_(false) { 626 finished_(false) {
611 gfx::Size canvas_size; 627 gfx::Size canvas_size;
612 CalculatePrintCanvasSize(print_params, &canvas_size); 628 gfx::Rect printable_area;
629 gfx::Size paper_size;
630 ComputePrintParamsInDesiredDpi(print_params, &canvas_size, &printable_area,
631 &paper_size);
613 632
614 if (WebFrame* web_frame = web_view_->mainFrame()) 633 if (WebFrame* web_frame = web_view_->mainFrame())
615 prev_scroll_offset_ = web_frame->scrollOffset(); 634 prev_scroll_offset_ = web_frame->scrollOffset();
616 prev_view_size_ = web_view_->size(); 635 prev_view_size_ = web_view_->size();
617 636
618 StartPrinting(canvas_size); 637 StartPrinting(canvas_size, printable_area, paper_size);
619 } 638 }
620 639
621 PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() { 640 PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() {
622 FinishPrinting(); 641 FinishPrinting();
623 } 642 }
624 643
625 void PrepareFrameAndViewForPrint::UpdatePrintParams( 644 void PrepareFrameAndViewForPrint::UpdatePrintParams(
626 const PrintMsg_Print_Params& print_params) { 645 const PrintMsg_Print_Params& print_params) {
627 DCHECK(!finished_); 646 DCHECK(!finished_);
628 gfx::Size canvas_size; 647 gfx::Size canvas_size;
629 CalculatePrintCanvasSize(print_params, &canvas_size); 648 gfx::Rect printable_area;
630 if (canvas_size == print_canvas_size_) 649 gfx::Size paper_size;
650 ComputePrintParamsInDesiredDpi(print_params, &canvas_size, &printable_area,
651 &paper_size);
652 if (canvas_size == print_canvas_size_ &&
653 printable_area == printable_area_ &&
654 paper_size == paper_size_ &&
655 print_params.print_scaling_option == print_scaling_option_) {
631 return; 656 return;
657 }
632 658
633 frame_->printEnd(); 659 frame_->printEnd();
634 dpi_ = static_cast<int>(print_params.dpi); 660 dpi_ = static_cast<int>(print_params.dpi);
635 StartPrinting(canvas_size); 661 print_scaling_option_ = print_params.print_scaling_option;
662 StartPrinting(canvas_size, printable_area, paper_size);
636 } 663 }
637 664
638 void PrepareFrameAndViewForPrint::StartPrinting( 665 void PrepareFrameAndViewForPrint::StartPrinting(
639 const gfx::Size& print_canvas_size) { 666 const gfx::Size& print_canvas_size,
667 const gfx::Rect& printable_area,
668 const gfx::Size& paper_size) {
640 print_canvas_size_ = print_canvas_size; 669 print_canvas_size_ = print_canvas_size;
670 printable_area_ = printable_area;
671 paper_size_ = paper_size;
641 672
642 // Layout page according to printer page size. Since WebKit shrinks the 673 // Layout page according to printer page size. Since WebKit shrinks the
643 // size of the page automatically (from 125% to 200%) we trick it to 674 // size of the page automatically (from 125% to 200%) we trick it to
644 // think the page is 125% larger so the size of the page is correct for 675 // think the page is 125% larger so the size of the page is correct for
645 // minimum (default) scaling. 676 // minimum (default) scaling.
646 // This is important for sites that try to fill the page. 677 // This is important for sites that try to fill the page.
647 gfx::Size print_layout_size(print_canvas_size_); 678 gfx::Size print_layout_size(print_canvas_size_);
648 print_layout_size.set_height(static_cast<int>( 679 print_layout_size.set_height(static_cast<int>(
649 static_cast<double>(print_layout_size.height()) * 1.25)); 680 static_cast<double>(print_layout_size.height()) * 1.25));
650 681
651 web_view_->resize(print_layout_size); 682 web_view_->resize(print_layout_size);
652 683
653 expected_pages_count_ = frame_->printBegin(print_canvas_size_, node_to_print_, 684 expected_pages_count_ = frame_->printBegin(print_canvas_size_,
654 dpi_, &use_browser_overlays_); 685 printable_area_,
686 paper_size_,
687 node_to_print_,
688 dpi_,
689 print_scaling_option_,
690 &use_browser_overlays_);
655 } 691 }
656 692
657 void PrepareFrameAndViewForPrint::FinishPrinting() { 693 void PrepareFrameAndViewForPrint::FinishPrinting() {
658 if (!finished_) { 694 if (!finished_) {
659 finished_ = true; 695 finished_ = true;
660 frame_->printEnd(); 696 frame_->printEnd();
661 web_view_->resize(prev_view_size_); 697 web_view_->resize(prev_view_size_);
662 if (WebFrame* web_frame = web_view_->mainFrame()) 698 if (WebFrame* web_frame = web_view_->mainFrame())
663 web_frame->setScrollOffset(prev_scroll_offset_); 699 web_frame->setScrollOffset(prev_scroll_offset_);
664 } 700 }
(...skipping 542 matching lines...) Expand 10 before | Expand all | Expand 10 after
1207 // static - Not anonymous so that platform implementations can use it. 1243 // static - Not anonymous so that platform implementations can use it.
1208 void PrintWebViewHelper::ComputePageLayoutInPointsForCss( 1244 void PrintWebViewHelper::ComputePageLayoutInPointsForCss(
1209 WebFrame* frame, 1245 WebFrame* frame,
1210 int page_index, 1246 int page_index,
1211 const PrintMsg_Print_Params& page_params, 1247 const PrintMsg_Print_Params& page_params,
1212 bool ignore_css_margins, 1248 bool ignore_css_margins,
1213 double* scale_factor, 1249 double* scale_factor,
1214 PageSizeMargins* page_layout_in_points) { 1250 PageSizeMargins* page_layout_in_points) {
1215 PrintMsg_Print_Params params = CalculatePrintParamsForCss( 1251 PrintMsg_Print_Params params = CalculatePrintParamsForCss(
1216 frame, page_index, page_params, ignore_css_margins, 1252 frame, page_index, page_params, ignore_css_margins,
1217 page_params.fit_to_paper_size, scale_factor); 1253 page_params.print_scaling_option == WebKit::WebPrintFitToPrintableArea,
1254 scale_factor);
1218 CalculatePageLayoutFromPrintParams(params, page_layout_in_points); 1255 CalculatePageLayoutFromPrintParams(params, page_layout_in_points);
1219 } 1256 }
1220 1257
1221 // static - Not anonymous so that platform implementations can use it. 1258 // static - Not anonymous so that platform implementations can use it.
1222 void PrintWebViewHelper::UpdateFrameAndViewFromCssPageLayout( 1259 void PrintWebViewHelper::UpdateFrameAndViewFromCssPageLayout(
1223 WebFrame* frame, 1260 WebFrame* frame,
1224 const WebNode& node, 1261 const WebNode& node,
1225 PrepareFrameAndViewForPrint* prepare, 1262 PrepareFrameAndViewForPrint* prepare,
1226 const PrintMsg_Print_Params& params, 1263 const PrintMsg_Print_Params& params,
1227 bool ignore_css_margins) { 1264 bool ignore_css_margins) {
1228 if (PrintingNodeOrPdfFrame(frame, node)) 1265 if (PrintingNodeOrPdfFrame(frame, node))
1229 return; 1266 return;
1230 PrintMsg_Print_Params print_params = CalculatePrintParamsForCss( 1267 PrintMsg_Print_Params print_params = CalculatePrintParamsForCss(
1231 frame, 0, params, ignore_css_margins, 1268 frame, 0, params, ignore_css_margins,
1232 ignore_css_margins && params.fit_to_paper_size, NULL); 1269 ignore_css_margins &&
1270 params.print_scaling_option == WebKit::WebPrintFitToPrintableArea,
1271 NULL);
1233 prepare->UpdatePrintParams(print_params); 1272 prepare->UpdatePrintParams(print_params);
1234 } 1273 }
1235 1274
1236 bool PrintWebViewHelper::InitPrintSettings() { 1275 bool PrintWebViewHelper::InitPrintSettings(bool fit_to_paper_size) {
1237 PrintMsg_PrintPages_Params settings; 1276 PrintMsg_PrintPages_Params settings;
1238 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(), 1277 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(),
1239 &settings.params)); 1278 &settings.params));
1240 // Check if the printer returned any settings, if the settings is empty, we 1279 // Check if the printer returned any settings, if the settings is empty, we
1241 // can safely assume there are no printer drivers configured. So we safely 1280 // can safely assume there are no printer drivers configured. So we safely
1242 // terminate. 1281 // terminate.
1243 bool result = true; 1282 bool result = true;
1244 if (!PrintMsg_Print_Params_IsValid(settings.params)) 1283 if (!PrintMsg_Print_Params_IsValid(settings.params))
1245 result = false; 1284 result = false;
1246 1285
1247 if (result && 1286 if (result &&
1248 (settings.params.dpi < kMinDpi || settings.params.document_cookie == 0)) { 1287 (settings.params.dpi < kMinDpi || settings.params.document_cookie == 0)) {
1249 // Invalid print page settings. 1288 // Invalid print page settings.
1250 NOTREACHED(); 1289 NOTREACHED();
1251 result = false; 1290 result = false;
1252 } 1291 }
1253 1292
1254 // Reset to default values. 1293 // Reset to default values.
1255 ignore_css_margins_ = false; 1294 ignore_css_margins_ = false;
1256 settings.params.fit_to_paper_size = true;
1257 settings.pages.clear(); 1295 settings.pages.clear();
1296 settings.params.print_scaling_option = fit_to_paper_size ?
1297 WebKit::WebPrintFitToPrintableArea : WebKit::WebPrintSourceSize;
1258 1298
1259 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); 1299 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings));
1260 return result; 1300 return result;
1261 } 1301 }
1262 1302
1263 bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame( 1303 bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame(
1264 WebKit::WebFrame* frame, const WebKit::WebNode& node, 1304 WebKit::WebFrame* frame, const WebKit::WebNode& node,
1265 scoped_ptr<PrepareFrameAndViewForPrint>* prepare) { 1305 scoped_ptr<PrepareFrameAndViewForPrint>* prepare) {
1266 DCHECK(frame); 1306 DCHECK(frame);
1267 if (!InitPrintSettings()) { 1307
1308 // If the source is html, fit to paper size else print the source pdf size.
1309 bool fit_to_paper_size = !(PrintingNodeOrPdfFrame(frame, node));
1310 if (!InitPrintSettings(fit_to_paper_size)) {
1268 notify_browser_of_print_failure_ = false; 1311 notify_browser_of_print_failure_ = false;
1269 render_view()->RunModalAlertDialog( 1312 render_view()->RunModalAlertDialog(
1270 frame, 1313 frame,
1271 l10n_util::GetStringUTF16(IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS)); 1314 l10n_util::GetStringUTF16(IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS));
1272 return false; 1315 return false;
1273 } 1316 }
1274 1317
1275 DCHECK(!prepare->get()); 1318 DCHECK(!prepare->get());
1276 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, 1319 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params,
1277 frame, node)); 1320 frame, node));
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
1374 !job_settings->GetBoolean(printing::kIsFirstRequest, 1417 !job_settings->GetBoolean(printing::kIsFirstRequest,
1375 &(settings.params.is_first_request))) { 1418 &(settings.params.is_first_request))) {
1376 NOTREACHED(); 1419 NOTREACHED();
1377 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); 1420 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING);
1378 return false; 1421 return false;
1379 } 1422 }
1380 1423
1381 settings.params.print_to_pdf = IsPrintToPdfRequested(*job_settings); 1424 settings.params.print_to_pdf = IsPrintToPdfRequested(*job_settings);
1382 UpdateFrameMarginsCssInfo(*job_settings); 1425 UpdateFrameMarginsCssInfo(*job_settings);
1383 1426
1384 // Fit to paper size. 1427 // Fit to Paper Size.
1385 settings.params.fit_to_paper_size = source_is_html && 1428 WebKit::WebPrintScalingOption scaling_option =
vandebo (ex-Chrome) 2012/04/23 21:42:19 nit: No need for a local variable, just set the on
kmadhusu 2012/05/11 21:36:02 I have added a new function GetPrintScalingOption(
1386 !IsPrintToPdfRequested(*job_settings); 1429 WebKit::WebPrintFitToPrintableArea;
1430 if (print_for_preview_ || settings.params.print_to_pdf) {
1431 scaling_option = WebKit::WebPrintSourceSize;
1432 } else if (!source_is_html) {
1433 bool fit_to_paper_size = false;
1434 job_settings->GetBoolean(printing::kSettingFitToPageEnabled,
kmadhusu 2012/04/23 17:39:10 (We will send FitToPage option along with JobSetti
vandebo (ex-Chrome) 2012/04/23 21:42:19 nit: Do you want to create another method like IsP
kmadhusu 2012/05/11 21:36:02 Added FitToPageEnabled() and GetPrintScalingOption
1435 &(fit_to_paper_size));
1436 if ((settings.params.is_first_request &&
1437 frame->isPrintScalingDisabledForPlugin(
1438 print_preview_context_.node())) ||
1439 !(fit_to_paper_size)) {
1440 scaling_option = WebKit::WebPrintScaleNone;
1441 }
1442 settings.params.print_scaling_option = scaling_option;
1387 1443
1388 // Header/Footer: Set |header_footer_info_|. 1444 // Header/Footer: Set |header_footer_info_|.
1389 if (settings.params.display_header_footer) { 1445 if (settings.params.display_header_footer) {
1390 header_footer_info_.reset(new DictionaryValue()); 1446 header_footer_info_.reset(new DictionaryValue());
1391 header_footer_info_->SetString(printing::kSettingHeaderFooterDate, 1447 header_footer_info_->SetString(printing::kSettingHeaderFooterDate,
1392 settings.params.date); 1448 settings.params.date);
1393 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, 1449 header_footer_info_->SetString(printing::kSettingHeaderFooterURL,
1394 settings.params.url); 1450 settings.params.url);
1395 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle, 1451 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle,
1396 settings.params.title); 1452 settings.params.title);
(...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after
1820 DCHECK(IsRendering()); 1876 DCHECK(IsRendering());
1821 return prep_frame_view_->GetPrintCanvasSize(); 1877 return prep_frame_view_->GetPrintCanvasSize();
1822 } 1878 }
1823 1879
1824 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { 1880 void PrintWebViewHelper::PrintPreviewContext::ClearContext() {
1825 prep_frame_view_.reset(); 1881 prep_frame_view_.reset();
1826 metafile_.reset(); 1882 metafile_.reset();
1827 pages_to_render_.clear(); 1883 pages_to_render_.clear();
1828 error_ = PREVIEW_ERROR_NONE; 1884 error_ = PREVIEW_ERROR_NONE;
1829 } 1885 }
OLDNEW
« chrome/renderer/mock_printer.h ('K') | « chrome/renderer/print_web_view_helper.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698