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

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

Issue 981843003: Printing: PDFs should only be fit to page if there is a size mismatch. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@pdf169120
Patch Set: rebase Created 5 years, 9 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
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 <string> 7 #include <string>
8 8
9 #include "base/auto_reset.h" 9 #include "base/auto_reset.h"
10 #include "base/json/json_writer.h" 10 #include "base/json/json_writer.h"
(...skipping 17 matching lines...) Expand all
28 #include "third_party/WebKit/public/platform/WebURLRequest.h" 28 #include "third_party/WebKit/public/platform/WebURLRequest.h"
29 #include "third_party/WebKit/public/web/WebConsoleMessage.h" 29 #include "third_party/WebKit/public/web/WebConsoleMessage.h"
30 #include "third_party/WebKit/public/web/WebDocument.h" 30 #include "third_party/WebKit/public/web/WebDocument.h"
31 #include "third_party/WebKit/public/web/WebElement.h" 31 #include "third_party/WebKit/public/web/WebElement.h"
32 #include "third_party/WebKit/public/web/WebFrameClient.h" 32 #include "third_party/WebKit/public/web/WebFrameClient.h"
33 #include "third_party/WebKit/public/web/WebLocalFrame.h" 33 #include "third_party/WebKit/public/web/WebLocalFrame.h"
34 #include "third_party/WebKit/public/web/WebPlugin.h" 34 #include "third_party/WebKit/public/web/WebPlugin.h"
35 #include "third_party/WebKit/public/web/WebPluginDocument.h" 35 #include "third_party/WebKit/public/web/WebPluginDocument.h"
36 #include "third_party/WebKit/public/web/WebPrintParams.h" 36 #include "third_party/WebKit/public/web/WebPrintParams.h"
37 #include "third_party/WebKit/public/web/WebPrintPresetOptions.h" 37 #include "third_party/WebKit/public/web/WebPrintPresetOptions.h"
38 #include "third_party/WebKit/public/web/WebPrintScalingOption.h"
39 #include "third_party/WebKit/public/web/WebSandboxFlags.h" 38 #include "third_party/WebKit/public/web/WebSandboxFlags.h"
40 #include "third_party/WebKit/public/web/WebScriptSource.h" 39 #include "third_party/WebKit/public/web/WebScriptSource.h"
41 #include "third_party/WebKit/public/web/WebSettings.h" 40 #include "third_party/WebKit/public/web/WebSettings.h"
42 #include "third_party/WebKit/public/web/WebView.h" 41 #include "third_party/WebKit/public/web/WebView.h"
43 #include "third_party/WebKit/public/web/WebViewClient.h" 42 #include "third_party/WebKit/public/web/WebViewClient.h"
44 #include "ui/base/resource/resource_bundle.h" 43 #include "ui/base/resource/resource_bundle.h"
45 44
46 using content::WebPreferences; 45 using content::WebPreferences;
47 46
48 namespace printing { 47 namespace printing {
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 bool frame_has_custom_page_size_style = false; 304 bool frame_has_custom_page_size_style = false;
306 for (int i = 0; i < total_page_count; ++i) { 305 for (int i = 0; i < total_page_count; ++i) {
307 if (frame->hasCustomPageSizeStyle(i)) { 306 if (frame->hasCustomPageSizeStyle(i)) {
308 frame_has_custom_page_size_style = true; 307 frame_has_custom_page_size_style = true;
309 break; 308 break;
310 } 309 }
311 } 310 }
312 return frame_has_custom_page_size_style; 311 return frame_has_custom_page_size_style;
313 } 312 }
314 313
315 MarginType GetMarginsForPdf(blink::WebFrame* frame, 314 // Disable scaling when either:
316 const blink::WebNode& node) { 315 // - The PDF specifies disabling scaling.
317 if (frame->isPrintScalingDisabledForPlugin(node)) 316 // - All the pages in the PDF are the same size, and that size is the same as
318 return NO_MARGINS; 317 // the paper size.
319 else 318 bool PDFShouldDisableScalingBasedOnPreset(
320 return PRINTABLE_AREA_MARGINS; 319 const blink::WebPrintPresetOptions& options,
320 const PrintMsg_Print_Params& params) {
321 if (options.isScalingDisabled)
322 return true;
323
324 if (!options.isPageSizeUniform)
325 return false;
326
327 int dpi = GetDPI(&params);
328 blink::WebSize page_size(
329 ConvertUnit(params.page_size.width(), dpi, kPointsPerInch),
330 ConvertUnit(params.page_size.height(), dpi, kPointsPerInch));
331 return options.uniformPageSize == page_size;
332 }
333
334 bool PDFShouldDisableScaling(blink::WebLocalFrame* frame,
335 const blink::WebNode& node,
336 const PrintMsg_Print_Params& params) {
337 const bool kDefaultPDFShouldDisableScalingSetting = true;
338 blink::WebPrintPresetOptions preset_options;
339 if (!frame->getPrintPresetOptionsForPlugin(node, &preset_options))
340 return kDefaultPDFShouldDisableScalingSetting;
341 return PDFShouldDisableScalingBasedOnPreset(preset_options, params);
342 }
343
344 MarginType GetMarginsForPdf(blink::WebLocalFrame* frame,
345 const blink::WebNode& node,
346 const PrintMsg_Print_Params& params) {
347 return PDFShouldDisableScaling(frame, node, params) ?
348 NO_MARGINS : PRINTABLE_AREA_MARGINS;
321 } 349 }
322 350
323 bool FitToPageEnabled(const base::DictionaryValue& job_settings) { 351 bool FitToPageEnabled(const base::DictionaryValue& job_settings) {
324 bool fit_to_paper_size = false; 352 bool fit_to_paper_size = false;
325 if (!job_settings.GetBoolean(kSettingFitToPageEnabled, &fit_to_paper_size)) { 353 if (!job_settings.GetBoolean(kSettingFitToPageEnabled, &fit_to_paper_size)) {
326 NOTREACHED(); 354 NOTREACHED();
327 } 355 }
328 return fit_to_paper_size; 356 return fit_to_paper_size;
329 } 357 }
330 358
331 // Returns the print scaling option to retain/scale/crop the source page size 359 // Returns the print scaling option to retain/scale/crop the source page size
332 // to fit the printable area of the paper. 360 // to fit the printable area of the paper.
333 // 361 //
334 // We retain the source page size when the current destination printer is 362 // We retain the source page size when the current destination printer is
335 // SAVE_AS_PDF. 363 // SAVE_AS_PDF.
336 // 364 //
337 // We crop the source page size to fit the printable area or we print only the 365 // We crop the source page size to fit the printable area or we print only the
338 // left top page contents when 366 // left top page contents when
339 // (1) Source is PDF and the user has requested not to fit to printable area 367 // (1) Source is PDF and the user has requested not to fit to printable area
340 // via |job_settings|. 368 // via |job_settings|.
341 // (2) Source is PDF. This is the first preview request and print scaling 369 // (2) Source is PDF. This is the first preview request and print scaling
342 // option is disabled for initiator renderer plugin. 370 // option is disabled for initiator renderer plugin.
343 // 371 //
344 // In all other cases, we scale the source page to fit the printable area. 372 // In all other cases, we scale the source page to fit the printable area.
345 blink::WebPrintScalingOption GetPrintScalingOption( 373 blink::WebPrintScalingOption GetPrintScalingOption(
346 blink::WebFrame* frame, 374 blink::WebLocalFrame* frame,
347 const blink::WebNode& node, 375 const blink::WebNode& node,
348 bool source_is_html, 376 bool source_is_html,
349 const base::DictionaryValue& job_settings, 377 const base::DictionaryValue& job_settings,
350 const PrintMsg_Print_Params& params) { 378 const PrintMsg_Print_Params& params) {
351 if (params.print_to_pdf) 379 if (params.print_to_pdf)
352 return blink::WebPrintScalingOptionSourceSize; 380 return blink::WebPrintScalingOptionSourceSize;
353 381
354 if (!source_is_html) { 382 if (!source_is_html) {
355 if (!FitToPageEnabled(job_settings)) 383 if (!FitToPageEnabled(job_settings))
356 return blink::WebPrintScalingOptionNone; 384 return blink::WebPrintScalingOptionNone;
357 385
358 bool no_plugin_scaling = frame->isPrintScalingDisabledForPlugin(node); 386 bool no_plugin_scaling = PDFShouldDisableScaling(frame, node, params);
359
360 if (params.is_first_request && no_plugin_scaling) 387 if (params.is_first_request && no_plugin_scaling)
361 return blink::WebPrintScalingOptionNone; 388 return blink::WebPrintScalingOptionNone;
362 } 389 }
363 return blink::WebPrintScalingOptionFitToPrintableArea; 390 return blink::WebPrintScalingOptionFitToPrintableArea;
364 } 391 }
365 392
366 PrintMsg_Print_Params CalculatePrintParamsForCss( 393 PrintMsg_Print_Params CalculatePrintParamsForCss(
367 blink::WebFrame* frame, 394 blink::WebFrame* frame,
368 int page_index, 395 int page_index,
369 const PrintMsg_Print_Params& page_params, 396 const PrintMsg_Print_Params& page_params,
(...skipping 640 matching lines...) Expand 10 before | Expand all | Expand 10 after
1010 } 1037 }
1011 DidFinishPrinting(FAIL_PREVIEW); 1038 DidFinishPrinting(FAIL_PREVIEW);
1012 return; 1039 return;
1013 } 1040 }
1014 1041
1015 // Set the options from document if we are previewing a pdf and send a 1042 // Set the options from document if we are previewing a pdf and send a
1016 // message to browser. 1043 // message to browser.
1017 if (print_pages_params_->params.is_first_request && 1044 if (print_pages_params_->params.is_first_request &&
1018 !print_preview_context_.IsModifiable()) { 1045 !print_preview_context_.IsModifiable()) {
1019 PrintHostMsg_SetOptionsFromDocument_Params options; 1046 PrintHostMsg_SetOptionsFromDocument_Params options;
1020 SetOptionsFromPdfDocument(&options); 1047 if (SetOptionsFromPdfDocument(&options))
1021 Send(new PrintHostMsg_SetOptionsFromDocument(routing_id(), options)); 1048 Send(new PrintHostMsg_SetOptionsFromDocument(routing_id(), options));
1022 } 1049 }
1023 1050
1024 is_print_ready_metafile_sent_ = false; 1051 is_print_ready_metafile_sent_ = false;
1025 1052
1026 // PDF printer device supports alpha blending. 1053 // PDF printer device supports alpha blending.
1027 print_pages_params_->params.supports_alpha_blend = true; 1054 print_pages_params_->params.supports_alpha_blend = true;
1028 1055
1029 bool generate_draft_pages = false; 1056 bool generate_draft_pages = false;
1030 if (!settings.GetBoolean(kSettingGenerateDraftData, &generate_draft_pages)) { 1057 if (!settings.GetBoolean(kSettingGenerateDraftData, &generate_draft_pages)) {
1031 NOTREACHED(); 1058 NOTREACHED();
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after
1436 } 1463 }
1437 1464
1438 const PrintMsg_Print_Params& params = print_pages_params_->params; 1465 const PrintMsg_Print_Params& params = print_pages_params_->params;
1439 PrepareFrameAndViewForPrint prepare(params, frame, node, ignore_css_margins_); 1466 PrepareFrameAndViewForPrint prepare(params, frame, node, ignore_css_margins_);
1440 prepare.StartPrinting(); 1467 prepare.StartPrinting();
1441 1468
1442 *number_of_pages = prepare.GetExpectedPageCount(); 1469 *number_of_pages = prepare.GetExpectedPageCount();
1443 return true; 1470 return true;
1444 } 1471 }
1445 1472
1446 void PrintWebViewHelper::SetOptionsFromPdfDocument( 1473 bool PrintWebViewHelper::SetOptionsFromPdfDocument(
1447 PrintHostMsg_SetOptionsFromDocument_Params* options) { 1474 PrintHostMsg_SetOptionsFromDocument_Params* options) {
1448 blink::WebLocalFrame* source_frame = print_preview_context_.source_frame(); 1475 blink::WebLocalFrame* source_frame = print_preview_context_.source_frame();
1449 const blink::WebNode& source_node = print_preview_context_.source_node(); 1476 const blink::WebNode& source_node = print_preview_context_.source_node();
1450 1477
1451 blink::WebPrintPresetOptions preset_options; 1478 blink::WebPrintPresetOptions preset_options;
1452 if (!source_frame->getPrintPresetOptionsForPlugin(source_node, 1479 if (!source_frame->getPrintPresetOptionsForPlugin(source_node,
1453 &preset_options)) { 1480 &preset_options)) {
1454 return; 1481 return false;
1455 } 1482 }
1456 1483
1457 options->is_scaling_disabled = preset_options.isScalingDisabled; 1484 options->is_scaling_disabled = PDFShouldDisableScalingBasedOnPreset(
1485 preset_options, print_pages_params_->params);
1458 options->copies = preset_options.copies; 1486 options->copies = preset_options.copies;
1459 1487
1460 // TODO(thestig) This should be a straight pass-through, but print preview 1488 // TODO(thestig) This should be a straight pass-through, but print preview
1461 // does not currently support short-edge printing. 1489 // does not currently support short-edge printing.
1462 switch (preset_options.duplexMode) { 1490 switch (preset_options.duplexMode) {
1463 case blink::WebSimplex: 1491 case blink::WebSimplex:
1464 options->duplex = SIMPLEX; 1492 options->duplex = SIMPLEX;
1465 break; 1493 break;
1466 case blink::WebLongEdge: 1494 case blink::WebLongEdge:
1467 options->duplex = LONG_EDGE; 1495 options->duplex = LONG_EDGE;
1468 break; 1496 break;
1469 default: 1497 default:
1470 options->duplex = UNKNOWN_DUPLEX_MODE; 1498 options->duplex = UNKNOWN_DUPLEX_MODE;
1471 break; 1499 break;
1472 } 1500 }
1501 return true;
1473 } 1502 }
1474 1503
1475 bool PrintWebViewHelper::UpdatePrintSettings( 1504 bool PrintWebViewHelper::UpdatePrintSettings(
1476 blink::WebLocalFrame* frame, 1505 blink::WebLocalFrame* frame,
1477 const blink::WebNode& node, 1506 const blink::WebNode& node,
1478 const base::DictionaryValue& passed_job_settings) { 1507 const base::DictionaryValue& passed_job_settings) {
1479 const base::DictionaryValue* job_settings = &passed_job_settings; 1508 const base::DictionaryValue* job_settings = &passed_job_settings;
1480 base::DictionaryValue modified_job_settings; 1509 base::DictionaryValue modified_job_settings;
1481 if (job_settings->empty()) { 1510 if (job_settings->empty()) {
1482 if (!print_for_preview_) 1511 if (!print_for_preview_)
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
1543 print_preview_context_.set_error(PREVIEW_ERROR_INVALID_PRINTER_SETTINGS); 1572 print_preview_context_.set_error(PREVIEW_ERROR_INVALID_PRINTER_SETTINGS);
1544 else 1573 else
1545 Send(new PrintHostMsg_ShowInvalidPrinterSettingsError(routing_id())); 1574 Send(new PrintHostMsg_ShowInvalidPrinterSettingsError(routing_id()));
1546 1575
1547 return false; 1576 return false;
1548 } 1577 }
1549 1578
1550 return true; 1579 return true;
1551 } 1580 }
1552 1581
1553 bool PrintWebViewHelper::GetPrintSettingsFromUser(blink::WebFrame* frame, 1582 bool PrintWebViewHelper::GetPrintSettingsFromUser(blink::WebLocalFrame* frame,
1554 const blink::WebNode& node, 1583 const blink::WebNode& node,
1555 int expected_pages_count, 1584 int expected_pages_count,
1556 bool is_scripted) { 1585 bool is_scripted) {
1557 PrintHostMsg_ScriptedPrint_Params params; 1586 PrintHostMsg_ScriptedPrint_Params params;
1558 PrintMsg_PrintPages_Params print_settings; 1587 PrintMsg_PrintPages_Params print_settings;
1559 1588
1560 params.cookie = print_pages_params_->params.document_cookie; 1589 params.cookie = print_pages_params_->params.document_cookie;
1561 params.has_selection = frame->hasSelection(); 1590 params.has_selection = frame->hasSelection();
1562 params.expected_pages_count = expected_pages_count; 1591 params.expected_pages_count = expected_pages_count;
1563 MarginType margin_type = DEFAULT_MARGINS; 1592 MarginType margin_type = DEFAULT_MARGINS;
1564 if (PrintingNodeOrPdfFrame(frame, node)) 1593 if (PrintingNodeOrPdfFrame(frame, node)) {
1565 margin_type = GetMarginsForPdf(frame, node); 1594 margin_type =
1595 GetMarginsForPdf(frame, node, print_pages_params_->params);
1596 }
1566 params.margin_type = margin_type; 1597 params.margin_type = margin_type;
1567 params.is_scripted = is_scripted; 1598 params.is_scripted = is_scripted;
1568 1599
1569 Send(new PrintHostMsg_DidShowPrintDialog(routing_id())); 1600 Send(new PrintHostMsg_DidShowPrintDialog(routing_id()));
1570 1601
1571 // PrintHostMsg_ScriptedPrint will reset print_scaling_option, so we save the 1602 // PrintHostMsg_ScriptedPrint will reset print_scaling_option, so we save the
1572 // value before and restore it afterwards. 1603 // value before and restore it afterwards.
1573 blink::WebPrintScalingOption scaling_option = 1604 blink::WebPrintScalingOption scaling_option =
1574 print_pages_params_->params.print_scaling_option; 1605 print_pages_params_->params.print_scaling_option;
1575 1606
(...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after
2034 blink::WebConsoleMessage::LevelWarning, message)); 2065 blink::WebConsoleMessage::LevelWarning, message));
2035 return false; 2066 return false;
2036 } 2067 }
2037 2068
2038 void PrintWebViewHelper::ScriptingThrottler::Reset() { 2069 void PrintWebViewHelper::ScriptingThrottler::Reset() {
2039 // Reset counter on successful print. 2070 // Reset counter on successful print.
2040 count_ = 0; 2071 count_ = 0;
2041 } 2072 }
2042 2073
2043 } // namespace printing 2074 } // namespace printing
OLDNEW
« no previous file with comments | « components/printing/renderer/print_web_view_helper.h ('k') | content/renderer/pepper/pepper_plugin_instance_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698