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

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

Issue 2458833005: Move printing defines to buildflag system. (Closed)
Patch Set: Missing file 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
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 11 matching lines...) Expand all
22 #include "base/strings/utf_string_conversions.h" 22 #include "base/strings/utf_string_conversions.h"
23 #include "base/threading/thread_task_runner_handle.h" 23 #include "base/threading/thread_task_runner_handle.h"
24 #include "build/build_config.h" 24 #include "build/build_config.h"
25 #include "components/printing/common/print_messages.h" 25 #include "components/printing/common/print_messages.h"
26 #include "content/public/common/web_preferences.h" 26 #include "content/public/common/web_preferences.h"
27 #include "content/public/renderer/render_frame.h" 27 #include "content/public/renderer/render_frame.h"
28 #include "content/public/renderer/render_thread.h" 28 #include "content/public/renderer/render_thread.h"
29 #include "content/public/renderer/render_view.h" 29 #include "content/public/renderer/render_view.h"
30 #include "grit/components_resources.h" 30 #include "grit/components_resources.h"
31 #include "net/base/escape.h" 31 #include "net/base/escape.h"
32 #include "printing/features/features.h"
32 #include "printing/metafile_skia_wrapper.h" 33 #include "printing/metafile_skia_wrapper.h"
33 #include "printing/pdf_metafile_skia.h" 34 #include "printing/pdf_metafile_skia.h"
34 #include "printing/units.h" 35 #include "printing/units.h"
35 #include "third_party/WebKit/public/platform/WebDoubleSize.h" 36 #include "third_party/WebKit/public/platform/WebDoubleSize.h"
36 #include "third_party/WebKit/public/platform/WebSecurityOrigin.h" 37 #include "third_party/WebKit/public/platform/WebSecurityOrigin.h"
37 #include "third_party/WebKit/public/platform/WebSize.h" 38 #include "third_party/WebKit/public/platform/WebSize.h"
38 #include "third_party/WebKit/public/platform/WebURLRequest.h" 39 #include "third_party/WebKit/public/platform/WebURLRequest.h"
39 #include "third_party/WebKit/public/web/WebConsoleMessage.h" 40 #include "third_party/WebKit/public/web/WebConsoleMessage.h"
40 #include "third_party/WebKit/public/web/WebDocument.h" 41 #include "third_party/WebKit/public/web/WebDocument.h"
41 #include "third_party/WebKit/public/web/WebElement.h" 42 #include "third_party/WebKit/public/web/WebElement.h"
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 PREVIEW_EVENT_CREATE_DOCUMENT, 78 PREVIEW_EVENT_CREATE_DOCUMENT,
78 PREVIEW_EVENT_NEW_SETTINGS, // Unused 79 PREVIEW_EVENT_NEW_SETTINGS, // Unused
79 PREVIEW_EVENT_MAX, 80 PREVIEW_EVENT_MAX,
80 }; 81 };
81 82
82 const double kMinDpi = 1.0; 83 const double kMinDpi = 1.0;
83 84
84 // Also set in third_party/WebKit/Source/core/page/PrintContext.cpp 85 // Also set in third_party/WebKit/Source/core/page/PrintContext.cpp
85 const float kPrintingMinimumShrinkFactor = 1.333f; 86 const float kPrintingMinimumShrinkFactor = 1.333f;
86 87
87 #if defined(ENABLE_PRINT_PREVIEW) 88 #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
88 bool g_is_preview_enabled = true; 89 bool g_is_preview_enabled = true;
89 90
90 const char kPageLoadScriptFormat[] = 91 const char kPageLoadScriptFormat[] =
91 "document.open(); document.write(%s); document.close();"; 92 "document.open(); document.write(%s); document.close();";
92 93
93 const char kPageSetupScriptFormat[] = "setup(%s);"; 94 const char kPageSetupScriptFormat[] = "setup(%s);";
94 95
95 void ExecuteScript(blink::WebFrame* frame, 96 void ExecuteScript(blink::WebFrame* frame,
96 const char* script_format, 97 const char* script_format,
97 const base::Value& parameters) { 98 const base::Value& parameters) {
98 std::string json; 99 std::string json;
99 base::JSONWriter::Write(parameters, &json); 100 base::JSONWriter::Write(parameters, &json);
100 std::string script = base::StringPrintf(script_format, json.c_str()); 101 std::string script = base::StringPrintf(script_format, json.c_str());
101 frame->executeScript(blink::WebString(base::UTF8ToUTF16(script))); 102 frame->executeScript(blink::WebString(base::UTF8ToUTF16(script)));
102 } 103 }
103 #else 104 #else
104 bool g_is_preview_enabled = false; 105 bool g_is_preview_enabled = false;
105 #endif // defined(ENABLE_PRINT_PREVIEW) 106 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
106 107
107 int GetDPI(const PrintMsg_Print_Params* print_params) { 108 int GetDPI(const PrintMsg_Print_Params* print_params) {
108 #if defined(OS_MACOSX) 109 #if defined(OS_MACOSX)
109 // On the Mac, the printable area is in points, don't do any scaling based 110 // On the Mac, the printable area is in points, don't do any scaling based
110 // on dpi. 111 // on dpi.
111 return kPointsPerInch; 112 return kPointsPerInch;
112 #else 113 #else
113 return static_cast<int>(print_params->dpi); 114 return static_cast<int>(print_params->dpi);
114 #endif // defined(OS_MACOSX) 115 #endif // defined(OS_MACOSX)
115 } 116 }
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
311 } 312 }
312 313
313 bool PrintingNodeOrPdfFrame(const blink::WebLocalFrame* frame, 314 bool PrintingNodeOrPdfFrame(const blink::WebLocalFrame* frame,
314 const blink::WebNode& node) { 315 const blink::WebNode& node) {
315 if (!node.isNull()) 316 if (!node.isNull())
316 return true; 317 return true;
317 blink::WebPlugin* plugin = GetPlugin(frame); 318 blink::WebPlugin* plugin = GetPlugin(frame);
318 return plugin && plugin->supportsPaginatedPrint(); 319 return plugin && plugin->supportsPaginatedPrint();
319 } 320 }
320 321
321 #if defined(ENABLE_PRINT_PREVIEW) 322 #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
322 // Returns true if the current destination printer is PRINT_TO_PDF. 323 // Returns true if the current destination printer is PRINT_TO_PDF.
323 bool IsPrintToPdfRequested(const base::DictionaryValue& job_settings) { 324 bool IsPrintToPdfRequested(const base::DictionaryValue& job_settings) {
324 bool print_to_pdf = false; 325 bool print_to_pdf = false;
325 if (!job_settings.GetBoolean(kSettingPrintToPDF, &print_to_pdf)) 326 if (!job_settings.GetBoolean(kSettingPrintToPDF, &print_to_pdf))
326 NOTREACHED(); 327 NOTREACHED();
327 return print_to_pdf; 328 return print_to_pdf;
328 } 329 }
329 330
330 bool PrintingFrameHasPageSizeStyle(blink::WebLocalFrame* frame, 331 bool PrintingFrameHasPageSizeStyle(blink::WebLocalFrame* frame,
331 int total_page_count) { 332 int total_page_count) {
332 if (!frame) 333 if (!frame)
333 return false; 334 return false;
334 bool frame_has_custom_page_size_style = false; 335 bool frame_has_custom_page_size_style = false;
335 for (int i = 0; i < total_page_count; ++i) { 336 for (int i = 0; i < total_page_count; ++i) {
336 if (frame->hasCustomPageSizeStyle(i)) { 337 if (frame->hasCustomPageSizeStyle(i)) {
337 frame_has_custom_page_size_style = true; 338 frame_has_custom_page_size_style = true;
338 break; 339 break;
339 } 340 }
340 } 341 }
341 return frame_has_custom_page_size_style; 342 return frame_has_custom_page_size_style;
342 } 343 }
343 #endif // defined(ENABLE_PRINT_PREVIEW) 344 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
344 345
345 // Disable scaling when either: 346 // Disable scaling when either:
346 // - The PDF specifies disabling scaling. 347 // - The PDF specifies disabling scaling.
347 // - All the pages in the PDF are the same size, 348 // - All the pages in the PDF are the same size,
348 // - |ignore_page_size| is false and the uniform size is the same as the paper 349 // - |ignore_page_size| is false and the uniform size is the same as the paper
349 // size. 350 // size.
350 bool PDFShouldDisableScalingBasedOnPreset( 351 bool PDFShouldDisableScalingBasedOnPreset(
351 const blink::WebPrintPresetOptions& options, 352 const blink::WebPrintPresetOptions& options,
352 const PrintMsg_Print_Params& params, 353 const PrintMsg_Print_Params& params,
353 bool ignore_page_size) { 354 bool ignore_page_size) {
(...skipping 24 matching lines...) Expand all
378 const PrintMsg_Print_Params& params, 379 const PrintMsg_Print_Params& params,
379 bool ignore_page_size) { 380 bool ignore_page_size) {
380 const bool kDefaultPDFShouldDisableScalingSetting = true; 381 const bool kDefaultPDFShouldDisableScalingSetting = true;
381 blink::WebPrintPresetOptions preset_options; 382 blink::WebPrintPresetOptions preset_options;
382 if (!frame->getPrintPresetOptionsForPlugin(node, &preset_options)) 383 if (!frame->getPrintPresetOptionsForPlugin(node, &preset_options))
383 return kDefaultPDFShouldDisableScalingSetting; 384 return kDefaultPDFShouldDisableScalingSetting;
384 return PDFShouldDisableScalingBasedOnPreset(preset_options, params, 385 return PDFShouldDisableScalingBasedOnPreset(preset_options, params,
385 ignore_page_size); 386 ignore_page_size);
386 } 387 }
387 388
388 #if defined(ENABLE_BASIC_PRINTING) 389 #if BUILDFLAG(ENABLE_BASIC_PRINTING)
389 MarginType GetMarginsForPdf(blink::WebLocalFrame* frame, 390 MarginType GetMarginsForPdf(blink::WebLocalFrame* frame,
390 const blink::WebNode& node, 391 const blink::WebNode& node,
391 const PrintMsg_Print_Params& params) { 392 const PrintMsg_Print_Params& params) {
392 return PDFShouldDisableScaling(frame, node, params, false) ? 393 return PDFShouldDisableScaling(frame, node, params, false) ?
393 NO_MARGINS : PRINTABLE_AREA_MARGINS; 394 NO_MARGINS : PRINTABLE_AREA_MARGINS;
394 } 395 }
395 #endif 396 #endif
396 397
397 #if defined(ENABLE_PRINT_PREVIEW) 398 #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
398 bool FitToPageEnabled(const base::DictionaryValue& job_settings) { 399 bool FitToPageEnabled(const base::DictionaryValue& job_settings) {
399 bool fit_to_paper_size = false; 400 bool fit_to_paper_size = false;
400 if (!job_settings.GetBoolean(kSettingFitToPageEnabled, &fit_to_paper_size)) { 401 if (!job_settings.GetBoolean(kSettingFitToPageEnabled, &fit_to_paper_size)) {
401 NOTREACHED(); 402 NOTREACHED();
402 } 403 }
403 return fit_to_paper_size; 404 return fit_to_paper_size;
404 } 405 }
405 406
406 // Returns the print scaling option to retain/scale/crop the source page size 407 // Returns the print scaling option to retain/scale/crop the source page size
407 // to fit the printable area of the paper. 408 // to fit the printable area of the paper.
(...skipping 22 matching lines...) Expand all
430 if (!FitToPageEnabled(job_settings)) 431 if (!FitToPageEnabled(job_settings))
431 return blink::WebPrintScalingOptionNone; 432 return blink::WebPrintScalingOptionNone;
432 433
433 bool no_plugin_scaling = PDFShouldDisableScaling(frame, node, params, 434 bool no_plugin_scaling = PDFShouldDisableScaling(frame, node, params,
434 true); 435 true);
435 if (params.is_first_request && no_plugin_scaling) 436 if (params.is_first_request && no_plugin_scaling)
436 return blink::WebPrintScalingOptionNone; 437 return blink::WebPrintScalingOptionNone;
437 } 438 }
438 return blink::WebPrintScalingOptionFitToPrintableArea; 439 return blink::WebPrintScalingOptionFitToPrintableArea;
439 } 440 }
440 #endif // defined(ENABLE_PRINT_PREVIEW) 441 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
441 442
442 // Helper function to scale and round an integer value with a double valued 443 // Helper function to scale and round an integer value with a double valued
443 // scaling. 444 // scaling.
444 int ScaleAndRound(int value, double scaling) { 445 int ScaleAndRound(int value, double scaling) {
445 return static_cast<int>(static_cast<double>(value) / scaling); 446 return static_cast<int>(static_cast<double>(value) / scaling);
446 } 447 }
447 448
448 // Helper function to scale the width and height of a gfx::Size by scaling. 449 // Helper function to scale the width and height of a gfx::Size by scaling.
449 gfx::Size ScaleAndRoundSize(gfx::Size original, double scaling) { 450 gfx::Size ScaleAndRoundSize(gfx::Size original, double scaling) {
450 return gfx::Size(ScaleAndRound(original.width(), scaling), 451 return gfx::Size(ScaleAndRound(original.width(), scaling),
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
549 if (frame == frame_) 550 if (frame == frame_)
550 return frame_; 551 return frame_;
551 } 552 }
552 return NULL; 553 return NULL;
553 } 554 }
554 555
555 blink::WebView* FrameReference::view() { 556 blink::WebView* FrameReference::view() {
556 return view_; 557 return view_;
557 } 558 }
558 559
559 #if defined(ENABLE_PRINT_PREVIEW) 560 #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
560 // static - Not anonymous so that platform implementations can use it. 561 // static - Not anonymous so that platform implementations can use it.
561 void PrintWebViewHelper::PrintHeaderAndFooter( 562 void PrintWebViewHelper::PrintHeaderAndFooter(
562 blink::WebCanvas* canvas, 563 blink::WebCanvas* canvas,
563 int page_number, 564 int page_number,
564 int total_pages, 565 int total_pages,
565 const blink::WebFrame& source_frame, 566 const blink::WebFrame& source_frame,
566 float webkit_scale_factor, 567 float webkit_scale_factor,
567 const PageSizeMargins& page_layout, 568 const PageSizeMargins& page_layout,
568 const PrintMsg_Print_Params& params) { 569 const PrintMsg_Print_Params& params) {
569 SkAutoCanvasRestore auto_restore(canvas, true); 570 SkAutoCanvasRestore auto_restore(canvas, true);
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
607 608
608 blink::WebPrintParams webkit_params(page_size); 609 blink::WebPrintParams webkit_params(page_size);
609 webkit_params.printerDPI = GetDPI(&params); 610 webkit_params.printerDPI = GetDPI(&params);
610 611
611 frame->printBegin(webkit_params); 612 frame->printBegin(webkit_params);
612 frame->printPage(0, canvas); 613 frame->printPage(0, canvas);
613 frame->printEnd(); 614 frame->printEnd();
614 615
615 web_view->close(); 616 web_view->close();
616 } 617 }
617 #endif // defined(ENABLE_PRINT_PREVIEW) 618 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
618 619
619 // static - Not anonymous so that platform implementations can use it. 620 // static - Not anonymous so that platform implementations can use it.
620 float PrintWebViewHelper::RenderPageContent(blink::WebFrame* frame, 621 float PrintWebViewHelper::RenderPageContent(blink::WebFrame* frame,
621 int page_number, 622 int page_number,
622 const gfx::Rect& canvas_area, 623 const gfx::Rect& canvas_area,
623 const gfx::Rect& content_area, 624 const gfx::Rect& content_area,
624 double scale_factor, 625 double scale_factor,
625 blink::WebCanvas* canvas) { 626 blink::WebCanvas* canvas) {
626 SkAutoCanvasRestore auto_restore(canvas, true); 627 SkAutoCanvasRestore auto_restore(canvas, true);
627 canvas->translate((content_area.x() - canvas_area.x()) / scale_factor, 628 canvas->translate((content_area.x() - canvas_area.x()) / scale_factor,
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
713 is_printing_started_(false), 714 is_printing_started_(false),
714 weak_ptr_factory_(this) { 715 weak_ptr_factory_(this) {
715 PrintMsg_Print_Params print_params = params; 716 PrintMsg_Print_Params print_params = params;
716 if (!should_print_selection_only_ || 717 if (!should_print_selection_only_ ||
717 !PrintingNodeOrPdfFrame(frame, node_to_print_)) { 718 !PrintingNodeOrPdfFrame(frame, node_to_print_)) {
718 bool fit_to_page = 719 bool fit_to_page =
719 ignore_css_margins && IsWebPrintScalingOptionFitToPage(print_params); 720 ignore_css_margins && IsWebPrintScalingOptionFitToPage(print_params);
720 ComputeWebKitPrintParamsInDesiredDpi(params, &web_print_params_); 721 ComputeWebKitPrintParamsInDesiredDpi(params, &web_print_params_);
721 frame->printBegin(web_print_params_, node_to_print_); 722 frame->printBegin(web_print_params_, node_to_print_);
722 double scale_factor = 1.0f; 723 double scale_factor = 1.0f;
723 #if defined(ENABLE_PRINT_PREVIEW) 724 #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
724 if (print_params.scale_factor > 0) 725 if (print_params.scale_factor > 0)
725 scale_factor = print_params.scale_factor; 726 scale_factor = print_params.scale_factor;
726 #endif 727 #endif
727 print_params = CalculatePrintParamsForCss( 728 print_params = CalculatePrintParamsForCss(
728 frame, 0, print_params, ignore_css_margins, fit_to_page, &scale_factor); 729 frame, 0, print_params, ignore_css_margins, fit_to_page, &scale_factor);
729 frame->printEnd(); 730 frame->printEnd();
730 } 731 }
731 ComputeWebKitPrintParamsInDesiredDpi(print_params, &web_print_params_); 732 ComputeWebKitPrintParamsInDesiredDpi(print_params, &web_print_params_);
732 } 733 }
733 734
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after
945 if (delegate_->CancelPrerender(render_view(), routing_id())) 946 if (delegate_->CancelPrerender(render_view(), routing_id()))
946 return; 947 return;
947 948
948 if (!IsScriptInitiatedPrintAllowed(frame, user_initiated)) 949 if (!IsScriptInitiatedPrintAllowed(frame, user_initiated))
949 return; 950 return;
950 951
951 if (delegate_->OverridePrint(frame)) 952 if (delegate_->OverridePrint(frame))
952 return; 953 return;
953 954
954 if (g_is_preview_enabled) { 955 if (g_is_preview_enabled) {
955 #if defined(ENABLE_PRINT_PREVIEW) 956 #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
956 print_preview_context_.InitWithFrame(frame); 957 print_preview_context_.InitWithFrame(frame);
957 RequestPrintPreview(PRINT_PREVIEW_SCRIPTED); 958 RequestPrintPreview(PRINT_PREVIEW_SCRIPTED);
958 #endif 959 #endif
959 } else { 960 } else {
960 #if defined(ENABLE_BASIC_PRINTING) 961 #if BUILDFLAG(ENABLE_BASIC_PRINTING)
961 Print(frame, blink::WebNode(), true); 962 Print(frame, blink::WebNode(), true);
962 #endif 963 #endif
963 } 964 }
964 } 965 }
965 966
966 bool PrintWebViewHelper::OnMessageReceived(const IPC::Message& message) { 967 bool PrintWebViewHelper::OnMessageReceived(const IPC::Message& message) {
967 // The class is not designed to handle recursive messages. This is not 968 // The class is not designed to handle recursive messages. This is not
968 // expected during regular flow. However, during rendering of content for 969 // expected during regular flow. However, during rendering of content for
969 // printing, lower level code may run nested message loop. E.g. PDF may has 970 // printing, lower level code may run nested message loop. E.g. PDF may has
970 // script to show message box http://crbug.com/502562. In that moment browser 971 // script to show message box http://crbug.com/502562. In that moment browser
971 // may receive updated printer capabilities and decide to restart print 972 // may receive updated printer capabilities and decide to restart print
972 // preview generation. When this happened message handling function may 973 // preview generation. When this happened message handling function may
973 // choose to ignore message or safely crash process. 974 // choose to ignore message or safely crash process.
974 ++ipc_nesting_level_; 975 ++ipc_nesting_level_;
975 976
976 bool handled = true; 977 bool handled = true;
977 IPC_BEGIN_MESSAGE_MAP(PrintWebViewHelper, message) 978 IPC_BEGIN_MESSAGE_MAP(PrintWebViewHelper, message)
978 #if defined(ENABLE_BASIC_PRINTING) 979 #if BUILDFLAG(ENABLE_BASIC_PRINTING)
979 IPC_MESSAGE_HANDLER(PrintMsg_PrintPages, OnPrintPages) 980 IPC_MESSAGE_HANDLER(PrintMsg_PrintPages, OnPrintPages)
980 IPC_MESSAGE_HANDLER(PrintMsg_PrintForSystemDialog, OnPrintForSystemDialog) 981 IPC_MESSAGE_HANDLER(PrintMsg_PrintForSystemDialog, OnPrintForSystemDialog)
981 #endif // defined(ENABLE_BASIC_PRINTING) 982 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING)
982 #if defined(ENABLE_BASIC_PRINTING) && defined(ENABLE_PRINT_PREVIEW) 983 #if BUILDFLAG(ENABLE_BASIC_PRINTING) && BUILDFLAG(ENABLE_PRINT_PREVIEW)
983 IPC_MESSAGE_HANDLER(PrintMsg_PrintForPrintPreview, OnPrintForPrintPreview) 984 IPC_MESSAGE_HANDLER(PrintMsg_PrintForPrintPreview, OnPrintForPrintPreview)
984 #endif 985 #endif
985 #if defined(ENABLE_PRINT_PREVIEW) 986 #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
986 IPC_MESSAGE_HANDLER(PrintMsg_InitiatePrintPreview, OnInitiatePrintPreview) 987 IPC_MESSAGE_HANDLER(PrintMsg_InitiatePrintPreview, OnInitiatePrintPreview)
987 IPC_MESSAGE_HANDLER(PrintMsg_PrintPreview, OnPrintPreview) 988 IPC_MESSAGE_HANDLER(PrintMsg_PrintPreview, OnPrintPreview)
988 IPC_MESSAGE_HANDLER(PrintMsg_PrintingDone, OnPrintingDone) 989 IPC_MESSAGE_HANDLER(PrintMsg_PrintingDone, OnPrintingDone)
989 #endif // defined(ENABLE_PRINT_PREVIEW) 990 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
990 IPC_MESSAGE_HANDLER(PrintMsg_SetScriptedPrintingBlocked, 991 IPC_MESSAGE_HANDLER(PrintMsg_SetScriptedPrintingBlocked,
991 SetScriptedPrintBlocked) 992 SetScriptedPrintBlocked)
992 IPC_MESSAGE_UNHANDLED(handled = false) 993 IPC_MESSAGE_UNHANDLED(handled = false)
993 IPC_END_MESSAGE_MAP() 994 IPC_END_MESSAGE_MAP()
994 995
995 --ipc_nesting_level_; 996 --ipc_nesting_level_;
996 return handled; 997 return handled;
997 } 998 }
998 999
999 void PrintWebViewHelper::OnDestruct() { 1000 void PrintWebViewHelper::OnDestruct() {
1000 delete this; 1001 delete this;
1001 } 1002 }
1002 1003
1003 bool PrintWebViewHelper::GetPrintFrame(blink::WebLocalFrame** frame) { 1004 bool PrintWebViewHelper::GetPrintFrame(blink::WebLocalFrame** frame) {
1004 DCHECK(frame); 1005 DCHECK(frame);
1005 blink::WebView* webView = render_view()->GetWebView(); 1006 blink::WebView* webView = render_view()->GetWebView();
1006 DCHECK(webView); 1007 DCHECK(webView);
1007 if (!webView) 1008 if (!webView)
1008 return false; 1009 return false;
1009 1010
1010 // If the user has selected text in the currently focused frame we print 1011 // If the user has selected text in the currently focused frame we print
1011 // only that frame (this makes print selection work for multiple frames). 1012 // only that frame (this makes print selection work for multiple frames).
1012 blink::WebLocalFrame* focusedFrame = webView->focusedFrame(); 1013 blink::WebLocalFrame* focusedFrame = webView->focusedFrame();
1013 *frame = focusedFrame->hasSelection() 1014 *frame = focusedFrame->hasSelection()
1014 ? focusedFrame 1015 ? focusedFrame
1015 : webView->mainFrame()->toWebLocalFrame(); 1016 : webView->mainFrame()->toWebLocalFrame();
1016 return true; 1017 return true;
1017 } 1018 }
1018 1019
1019 #if defined(ENABLE_BASIC_PRINTING) 1020 #if BUILDFLAG(ENABLE_BASIC_PRINTING)
1020 void PrintWebViewHelper::OnPrintPages() { 1021 void PrintWebViewHelper::OnPrintPages() {
1021 if (ipc_nesting_level_> 1) 1022 if (ipc_nesting_level_> 1)
1022 return; 1023 return;
1023 blink::WebLocalFrame* frame; 1024 blink::WebLocalFrame* frame;
1024 if (!GetPrintFrame(&frame)) 1025 if (!GetPrintFrame(&frame))
1025 return; 1026 return;
1026 // If we are printing a PDF extension frame, find the plugin node and print 1027 // If we are printing a PDF extension frame, find the plugin node and print
1027 // that instead. 1028 // that instead.
1028 auto plugin = delegate_->GetPdfElement(frame); 1029 auto plugin = delegate_->GetPdfElement(frame);
1029 Print(frame, plugin, false); 1030 Print(frame, plugin, false);
1030 } 1031 }
1031 1032
1032 void PrintWebViewHelper::OnPrintForSystemDialog() { 1033 void PrintWebViewHelper::OnPrintForSystemDialog() {
1033 if (ipc_nesting_level_> 1) 1034 if (ipc_nesting_level_> 1)
1034 return; 1035 return;
1035 blink::WebLocalFrame* frame = print_preview_context_.source_frame(); 1036 blink::WebLocalFrame* frame = print_preview_context_.source_frame();
1036 if (!frame) { 1037 if (!frame) {
1037 NOTREACHED(); 1038 NOTREACHED();
1038 return; 1039 return;
1039 } 1040 }
1040 Print(frame, print_preview_context_.source_node(), false); 1041 Print(frame, print_preview_context_.source_node(), false);
1041 } 1042 }
1042 #endif // defined(ENABLE_BASIC_PRINTING) 1043 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING)
1043 1044
1044 #if defined(ENABLE_BASIC_PRINTING) && defined(ENABLE_PRINT_PREVIEW) 1045 #if BUILDFLAG(ENABLE_BASIC_PRINTING) && BUILDFLAG(ENABLE_PRINT_PREVIEW)
1045 void PrintWebViewHelper::OnPrintForPrintPreview( 1046 void PrintWebViewHelper::OnPrintForPrintPreview(
1046 const base::DictionaryValue& job_settings) { 1047 const base::DictionaryValue& job_settings) {
1047 CHECK_LE(ipc_nesting_level_, 1); 1048 CHECK_LE(ipc_nesting_level_, 1);
1048 // If still not finished with earlier print request simply ignore. 1049 // If still not finished with earlier print request simply ignore.
1049 if (prep_frame_view_) 1050 if (prep_frame_view_)
1050 return; 1051 return;
1051 1052
1052 if (!render_view()->GetWebView()) 1053 if (!render_view()->GetWebView())
1053 return; 1054 return;
1054 blink::WebFrame* main_frame = render_view()->GetWebView()->mainFrame(); 1055 blink::WebFrame* main_frame = render_view()->GetWebView()->mainFrame();
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1099 // See http://crbug.com/123408 1100 // See http://crbug.com/123408
1100 PrintMsg_Print_Params& print_params = print_pages_params_->params; 1101 PrintMsg_Print_Params& print_params = print_pages_params_->params;
1101 print_params.printable_area = gfx::Rect(print_params.page_size); 1102 print_params.printable_area = gfx::Rect(print_params.page_size);
1102 1103
1103 // Render Pages for printing. 1104 // Render Pages for printing.
1104 if (!RenderPagesForPrint(plugin_frame, plugin_element)) { 1105 if (!RenderPagesForPrint(plugin_frame, plugin_element)) {
1105 LOG(ERROR) << "RenderPagesForPrint failed"; 1106 LOG(ERROR) << "RenderPagesForPrint failed";
1106 DidFinishPrinting(FAIL_PRINT); 1107 DidFinishPrinting(FAIL_PRINT);
1107 } 1108 }
1108 } 1109 }
1109 #endif // defined(ENABLE_BASIC_PRINTING) && defined(ENABLE_PRINT_PREVIEW) 1110 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING) && BUILDFLAG(ENABLE_PRINT_PREVIEW)
1110 1111
1111 void PrintWebViewHelper::GetPageSizeAndContentAreaFromPageLayout( 1112 void PrintWebViewHelper::GetPageSizeAndContentAreaFromPageLayout(
1112 const PageSizeMargins& page_layout_in_points, 1113 const PageSizeMargins& page_layout_in_points,
1113 gfx::Size* page_size, 1114 gfx::Size* page_size,
1114 gfx::Rect* content_area) { 1115 gfx::Rect* content_area) {
1115 *page_size = gfx::Size( 1116 *page_size = gfx::Size(
1116 page_layout_in_points.content_width + page_layout_in_points.margin_right + 1117 page_layout_in_points.content_width + page_layout_in_points.margin_right +
1117 page_layout_in_points.margin_left, 1118 page_layout_in_points.margin_left,
1118 page_layout_in_points.content_height + page_layout_in_points.margin_top + 1119 page_layout_in_points.content_height + page_layout_in_points.margin_top +
1119 page_layout_in_points.margin_bottom); 1120 page_layout_in_points.margin_bottom);
1120 *content_area = gfx::Rect(page_layout_in_points.margin_left, 1121 *content_area = gfx::Rect(page_layout_in_points.margin_left,
1121 page_layout_in_points.margin_top, 1122 page_layout_in_points.margin_top,
1122 page_layout_in_points.content_width, 1123 page_layout_in_points.content_width,
1123 page_layout_in_points.content_height); 1124 page_layout_in_points.content_height);
1124 } 1125 }
1125 1126
1126 void PrintWebViewHelper::UpdateFrameMarginsCssInfo( 1127 void PrintWebViewHelper::UpdateFrameMarginsCssInfo(
1127 const base::DictionaryValue& settings) { 1128 const base::DictionaryValue& settings) {
1128 int margins_type = 0; 1129 int margins_type = 0;
1129 if (!settings.GetInteger(kSettingMarginsType, &margins_type)) 1130 if (!settings.GetInteger(kSettingMarginsType, &margins_type))
1130 margins_type = DEFAULT_MARGINS; 1131 margins_type = DEFAULT_MARGINS;
1131 ignore_css_margins_ = (margins_type != DEFAULT_MARGINS); 1132 ignore_css_margins_ = (margins_type != DEFAULT_MARGINS);
1132 } 1133 }
1133 1134
1134 #if defined(ENABLE_PRINT_PREVIEW) 1135 #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
1135 void PrintWebViewHelper::OnPrintPreview(const base::DictionaryValue& settings) { 1136 void PrintWebViewHelper::OnPrintPreview(const base::DictionaryValue& settings) {
1136 if (ipc_nesting_level_ > 1) 1137 if (ipc_nesting_level_ > 1)
1137 return; 1138 return;
1138 1139
1139 print_preview_context_.OnPrintPreview(); 1140 print_preview_context_.OnPrintPreview();
1140 1141
1141 UMA_HISTOGRAM_ENUMERATION("PrintPreview.PreviewEvent", 1142 UMA_HISTOGRAM_ENUMERATION("PrintPreview.PreviewEvent",
1142 PREVIEW_EVENT_REQUESTED, PREVIEW_EVENT_MAX); 1143 PREVIEW_EVENT_REQUESTED, PREVIEW_EVENT_MAX);
1143 1144
1144 if (!print_preview_context_.source_frame()) { 1145 if (!print_preview_context_.source_frame()) {
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
1309 DCHECK(print_preview_context_.IsModifiable() || 1310 DCHECK(print_preview_context_.IsModifiable() ||
1310 print_preview_context_.IsFinalPageRendered()); 1311 print_preview_context_.IsFinalPageRendered());
1311 if (!FinalizePrintReadyDocument()) 1312 if (!FinalizePrintReadyDocument())
1312 return false; 1313 return false;
1313 } 1314 }
1314 } 1315 }
1315 print_preview_context_.Finished(); 1316 print_preview_context_.Finished();
1316 return true; 1317 return true;
1317 } 1318 }
1318 1319
1319 #if !defined(OS_MACOSX) && defined(ENABLE_PRINT_PREVIEW) 1320 #if !defined(OS_MACOSX) && BUILDFLAG(ENABLE_PRINT_PREVIEW)
1320 bool PrintWebViewHelper::RenderPreviewPage( 1321 bool PrintWebViewHelper::RenderPreviewPage(
1321 int page_number, 1322 int page_number,
1322 const PrintMsg_Print_Params& print_params) { 1323 const PrintMsg_Print_Params& print_params) {
1323 PrintMsg_PrintPage_Params page_params; 1324 PrintMsg_PrintPage_Params page_params;
1324 page_params.params = print_params; 1325 page_params.params = print_params;
1325 page_params.page_number = page_number; 1326 page_params.page_number = page_number;
1326 std::unique_ptr<PdfMetafileSkia> draft_metafile; 1327 std::unique_ptr<PdfMetafileSkia> draft_metafile;
1327 PdfMetafileSkia* initial_render_metafile = print_preview_context_.metafile(); 1328 PdfMetafileSkia* initial_render_metafile = print_preview_context_.metafile();
1328 if (print_preview_context_.IsModifiable() && is_print_ready_metafile_sent_) { 1329 if (print_preview_context_.IsModifiable() && is_print_ready_metafile_sent_) {
1329 draft_metafile.reset(new PdfMetafileSkia(PDF_SKIA_DOCUMENT_TYPE)); 1330 draft_metafile.reset(new PdfMetafileSkia(PDF_SKIA_DOCUMENT_TYPE));
1330 initial_render_metafile = draft_metafile.get(); 1331 initial_render_metafile = draft_metafile.get();
1331 } 1332 }
1332 1333
1333 base::TimeTicks begin_time = base::TimeTicks::Now(); 1334 base::TimeTicks begin_time = base::TimeTicks::Now();
1334 PrintPageInternal(page_params, print_preview_context_.prepared_frame(), 1335 PrintPageInternal(page_params, print_preview_context_.prepared_frame(),
1335 initial_render_metafile, nullptr, nullptr); 1336 initial_render_metafile, nullptr, nullptr);
1336 print_preview_context_.RenderedPreviewPage( 1337 print_preview_context_.RenderedPreviewPage(
1337 base::TimeTicks::Now() - begin_time); 1338 base::TimeTicks::Now() - begin_time);
1338 if (draft_metafile.get()) { 1339 if (draft_metafile.get()) {
1339 draft_metafile->FinishDocument(); 1340 draft_metafile->FinishDocument();
1340 } else if (print_preview_context_.IsModifiable() && 1341 } else if (print_preview_context_.IsModifiable() &&
1341 print_preview_context_.generate_draft_pages()) { 1342 print_preview_context_.generate_draft_pages()) {
1342 DCHECK(!draft_metafile.get()); 1343 DCHECK(!draft_metafile.get());
1343 draft_metafile = 1344 draft_metafile =
1344 print_preview_context_.metafile()->GetMetafileForCurrentPage( 1345 print_preview_context_.metafile()->GetMetafileForCurrentPage(
1345 PDF_SKIA_DOCUMENT_TYPE); 1346 PDF_SKIA_DOCUMENT_TYPE);
1346 } 1347 }
1347 return PreviewPageRendered(page_number, draft_metafile.get()); 1348 return PreviewPageRendered(page_number, draft_metafile.get());
1348 } 1349 }
1349 #endif // !defined(OS_MACOSX) && defined(ENABLE_PRINT_PREVIEW) 1350 #endif // !defined(OS_MACOSX) && BUILDFLAG(ENABLE_PRINT_PREVIEW)
1350 1351
1351 bool PrintWebViewHelper::FinalizePrintReadyDocument() { 1352 bool PrintWebViewHelper::FinalizePrintReadyDocument() {
1352 DCHECK(!is_print_ready_metafile_sent_); 1353 DCHECK(!is_print_ready_metafile_sent_);
1353 print_preview_context_.FinalizePrintReadyDocument(); 1354 print_preview_context_.FinalizePrintReadyDocument();
1354 1355
1355 PdfMetafileSkia* metafile = print_preview_context_.metafile(); 1356 PdfMetafileSkia* metafile = print_preview_context_.metafile();
1356 PrintHostMsg_DidPreviewDocument_Params preview_params; 1357 PrintHostMsg_DidPreviewDocument_Params preview_params;
1357 1358
1358 // Ask the browser to create the shared memory for us. 1359 // Ask the browser to create the shared memory for us.
1359 if (!CopyMetafileDataToSharedMem(*metafile, 1360 if (!CopyMetafileDataToSharedMem(*metafile,
1360 &(preview_params.metafile_data_handle))) { 1361 &(preview_params.metafile_data_handle))) {
1361 LOG(ERROR) << "CopyMetafileDataToSharedMem failed"; 1362 LOG(ERROR) << "CopyMetafileDataToSharedMem failed";
1362 print_preview_context_.set_error(PREVIEW_ERROR_METAFILE_COPY_FAILED); 1363 print_preview_context_.set_error(PREVIEW_ERROR_METAFILE_COPY_FAILED);
1363 return false; 1364 return false;
1364 } 1365 }
1365 1366
1366 preview_params.data_size = metafile->GetDataSize(); 1367 preview_params.data_size = metafile->GetDataSize();
1367 preview_params.document_cookie = print_pages_params_->params.document_cookie; 1368 preview_params.document_cookie = print_pages_params_->params.document_cookie;
1368 preview_params.expected_pages_count = 1369 preview_params.expected_pages_count =
1369 print_preview_context_.total_page_count(); 1370 print_preview_context_.total_page_count();
1370 preview_params.modifiable = print_preview_context_.IsModifiable(); 1371 preview_params.modifiable = print_preview_context_.IsModifiable();
1371 preview_params.preview_request_id = 1372 preview_params.preview_request_id =
1372 print_pages_params_->params.preview_request_id; 1373 print_pages_params_->params.preview_request_id;
1373 1374
1374 is_print_ready_metafile_sent_ = true; 1375 is_print_ready_metafile_sent_ = true;
1375 1376
1376 Send(new PrintHostMsg_MetafileReadyForPrinting(routing_id(), preview_params)); 1377 Send(new PrintHostMsg_MetafileReadyForPrinting(routing_id(), preview_params));
1377 return true; 1378 return true;
1378 } 1379 }
1379 #endif // defined(ENABLE_PRINT_PREVIEW) 1380 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
1380 1381
1381 void PrintWebViewHelper::OnPrintingDone(bool success) { 1382 void PrintWebViewHelper::OnPrintingDone(bool success) {
1382 if (ipc_nesting_level_ > 1) 1383 if (ipc_nesting_level_ > 1)
1383 return; 1384 return;
1384 notify_browser_of_print_failure_ = false; 1385 notify_browser_of_print_failure_ = false;
1385 if (!success) 1386 if (!success)
1386 LOG(ERROR) << "Failure in OnPrintingDone"; 1387 LOG(ERROR) << "Failure in OnPrintingDone";
1387 DidFinishPrinting(success ? OK : FAIL_PRINT); 1388 DidFinishPrinting(success ? OK : FAIL_PRINT);
1388 } 1389 }
1389 1390
1390 void PrintWebViewHelper::SetScriptedPrintBlocked(bool blocked) { 1391 void PrintWebViewHelper::SetScriptedPrintBlocked(bool blocked) {
1391 is_scripted_printing_blocked_ = blocked; 1392 is_scripted_printing_blocked_ = blocked;
1392 } 1393 }
1393 1394
1394 #if defined(ENABLE_PRINT_PREVIEW) 1395 #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
1395 void PrintWebViewHelper::OnInitiatePrintPreview(bool selection_only) { 1396 void PrintWebViewHelper::OnInitiatePrintPreview(bool selection_only) {
1396 if (ipc_nesting_level_ > 1) 1397 if (ipc_nesting_level_ > 1)
1397 return; 1398 return;
1398 blink::WebLocalFrame* frame = NULL; 1399 blink::WebLocalFrame* frame = NULL;
1399 GetPrintFrame(&frame); 1400 GetPrintFrame(&frame);
1400 DCHECK(frame); 1401 DCHECK(frame);
1401 // If we are printing a PDF extension frame, find the plugin node and print 1402 // If we are printing a PDF extension frame, find the plugin node and print
1402 // that instead. 1403 // that instead.
1403 auto plugin = delegate_->GetPdfElement(frame); 1404 auto plugin = delegate_->GetPdfElement(frame);
1404 if (!plugin.isNull()) { 1405 if (!plugin.isNull()) {
(...skipping 25 matching lines...) Expand all
1430 // from ppapi plugins. It's a rare case, so its OK to just fail here. 1431 // from ppapi plugins. It's a rare case, so its OK to just fail here.
1431 // See http://crbug.com/159165. 1432 // See http://crbug.com/159165.
1432 return; 1433 return;
1433 } 1434 }
1434 1435
1435 print_node_in_progress_ = true; 1436 print_node_in_progress_ = true;
1436 1437
1437 // Make a copy of the node, in case RenderView::OnContextMenuClosed resets 1438 // Make a copy of the node, in case RenderView::OnContextMenuClosed resets
1438 // its |context_menu_node_|. 1439 // its |context_menu_node_|.
1439 if (g_is_preview_enabled) { 1440 if (g_is_preview_enabled) {
1440 #if defined(ENABLE_PRINT_PREVIEW) 1441 #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
1441 print_preview_context_.InitWithNode(node); 1442 print_preview_context_.InitWithNode(node);
1442 RequestPrintPreview(PRINT_PREVIEW_USER_INITIATED_CONTEXT_NODE); 1443 RequestPrintPreview(PRINT_PREVIEW_USER_INITIATED_CONTEXT_NODE);
1443 #endif 1444 #endif
1444 } else { 1445 } else {
1445 #if defined(ENABLE_BASIC_PRINTING) 1446 #if BUILDFLAG(ENABLE_BASIC_PRINTING)
1446 blink::WebNode duplicate_node(node); 1447 blink::WebNode duplicate_node(node);
1447 Print(duplicate_node.document().frame(), duplicate_node, false); 1448 Print(duplicate_node.document().frame(), duplicate_node, false);
1448 #endif 1449 #endif
1449 } 1450 }
1450 1451
1451 print_node_in_progress_ = false; 1452 print_node_in_progress_ = false;
1452 } 1453 }
1453 1454
1454 #if defined(ENABLE_BASIC_PRINTING) 1455 #if BUILDFLAG(ENABLE_BASIC_PRINTING)
1455 void PrintWebViewHelper::Print(blink::WebLocalFrame* frame, 1456 void PrintWebViewHelper::Print(blink::WebLocalFrame* frame,
1456 const blink::WebNode& node, 1457 const blink::WebNode& node,
1457 bool is_scripted) { 1458 bool is_scripted) {
1458 // If still not finished with earlier print request simply ignore. 1459 // If still not finished with earlier print request simply ignore.
1459 if (prep_frame_view_) 1460 if (prep_frame_view_)
1460 return; 1461 return;
1461 1462
1462 FrameReference frame_ref(frame); 1463 FrameReference frame_ref(frame);
1463 1464
1464 int expected_page_count = 0; 1465 int expected_page_count = 0;
(...skipping 16 matching lines...) Expand all
1481 return; 1482 return;
1482 } 1483 }
1483 1484
1484 // Render Pages for printing. 1485 // Render Pages for printing.
1485 if (!RenderPagesForPrint(frame_ref.GetFrame(), node)) { 1486 if (!RenderPagesForPrint(frame_ref.GetFrame(), node)) {
1486 LOG(ERROR) << "RenderPagesForPrint failed"; 1487 LOG(ERROR) << "RenderPagesForPrint failed";
1487 DidFinishPrinting(FAIL_PRINT); 1488 DidFinishPrinting(FAIL_PRINT);
1488 } 1489 }
1489 scripting_throttler_.Reset(); 1490 scripting_throttler_.Reset();
1490 } 1491 }
1491 #endif // defined(ENABLE_BASIC_PRINTING) 1492 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING)
1492 1493
1493 void PrintWebViewHelper::DidFinishPrinting(PrintingResult result) { 1494 void PrintWebViewHelper::DidFinishPrinting(PrintingResult result) {
1494 switch (result) { 1495 switch (result) {
1495 case OK: 1496 case OK:
1496 break; 1497 break;
1497 1498
1498 case FAIL_PRINT_INIT: 1499 case FAIL_PRINT_INIT:
1499 DCHECK(!notify_browser_of_print_failure_); 1500 DCHECK(!notify_browser_of_print_failure_);
1500 break; 1501 break;
1501 1502
1502 case FAIL_PRINT: 1503 case FAIL_PRINT:
1503 if (notify_browser_of_print_failure_ && print_pages_params_) { 1504 if (notify_browser_of_print_failure_ && print_pages_params_) {
1504 int cookie = print_pages_params_->params.document_cookie; 1505 int cookie = print_pages_params_->params.document_cookie;
1505 Send(new PrintHostMsg_PrintingFailed(routing_id(), cookie)); 1506 Send(new PrintHostMsg_PrintingFailed(routing_id(), cookie));
1506 } 1507 }
1507 break; 1508 break;
1508 1509
1509 #if defined(ENABLE_PRINT_PREVIEW) 1510 #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
1510 case FAIL_PREVIEW: 1511 case FAIL_PREVIEW:
1511 int cookie = 1512 int cookie =
1512 print_pages_params_ ? print_pages_params_->params.document_cookie : 0; 1513 print_pages_params_ ? print_pages_params_->params.document_cookie : 0;
1513 if (notify_browser_of_print_failure_) { 1514 if (notify_browser_of_print_failure_) {
1514 LOG(ERROR) << "CreatePreviewDocument failed"; 1515 LOG(ERROR) << "CreatePreviewDocument failed";
1515 Send(new PrintHostMsg_PrintPreviewFailed(routing_id(), cookie)); 1516 Send(new PrintHostMsg_PrintPreviewFailed(routing_id(), cookie));
1516 } else { 1517 } else {
1517 Send(new PrintHostMsg_PrintPreviewCancelled(routing_id(), cookie)); 1518 Send(new PrintHostMsg_PrintPreviewCancelled(routing_id(), cookie));
1518 } 1519 }
1519 print_preview_context_.Failed(notify_browser_of_print_failure_); 1520 print_preview_context_.Failed(notify_browser_of_print_failure_);
1520 break; 1521 break;
1521 #endif // defined(ENABLE_PRINT_PREVIEW) 1522 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
1522 } 1523 }
1523 prep_frame_view_.reset(); 1524 prep_frame_view_.reset();
1524 print_pages_params_.reset(); 1525 print_pages_params_.reset();
1525 notify_browser_of_print_failure_ = true; 1526 notify_browser_of_print_failure_ = true;
1526 } 1527 }
1527 1528
1528 #if defined(ENABLE_BASIC_PRINTING) 1529 #if BUILDFLAG(ENABLE_BASIC_PRINTING)
1529 void PrintWebViewHelper::OnFramePreparedForPrintPages() { 1530 void PrintWebViewHelper::OnFramePreparedForPrintPages() {
1530 PrintPages(); 1531 PrintPages();
1531 FinishFramePrinting(); 1532 FinishFramePrinting();
1532 } 1533 }
1533 1534
1534 void PrintWebViewHelper::PrintPages() { 1535 void PrintWebViewHelper::PrintPages() {
1535 if (!prep_frame_view_) // Printing is already canceled or failed. 1536 if (!prep_frame_view_) // Printing is already canceled or failed.
1536 return; 1537 return;
1537 1538
1538 prep_frame_view_->StartPrinting(); 1539 prep_frame_view_->StartPrinting();
(...skipping 23 matching lines...) Expand all
1562 1563
1563 if (!PrintPagesNative(prep_frame_view_->frame(), page_count)) { 1564 if (!PrintPagesNative(prep_frame_view_->frame(), page_count)) {
1564 LOG(ERROR) << "Printing failed."; 1565 LOG(ERROR) << "Printing failed.";
1565 return DidFinishPrinting(FAIL_PRINT); 1566 return DidFinishPrinting(FAIL_PRINT);
1566 } 1567 }
1567 } 1568 }
1568 1569
1569 void PrintWebViewHelper::FinishFramePrinting() { 1570 void PrintWebViewHelper::FinishFramePrinting() {
1570 prep_frame_view_.reset(); 1571 prep_frame_view_.reset();
1571 } 1572 }
1572 #endif // defined(ENABLE_BASIC_PRINTING) 1573 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING)
1573 1574
1574 // static - Not anonymous so that platform implementations can use it. 1575 // static - Not anonymous so that platform implementations can use it.
1575 void PrintWebViewHelper::ComputePageLayoutInPointsForCss( 1576 void PrintWebViewHelper::ComputePageLayoutInPointsForCss(
1576 blink::WebLocalFrame* frame, 1577 blink::WebLocalFrame* frame,
1577 int page_index, 1578 int page_index,
1578 const PrintMsg_Print_Params& page_params, 1579 const PrintMsg_Print_Params& page_params,
1579 bool ignore_css_margins, 1580 bool ignore_css_margins,
1580 double* scale_factor, 1581 double* scale_factor,
1581 PageSizeMargins* page_layout_in_points) { 1582 PageSizeMargins* page_layout_in_points) {
1582 double input_scale_factor = *scale_factor; 1583 double input_scale_factor = *scale_factor;
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
1643 } 1644 }
1644 1645
1645 const PrintMsg_Print_Params& params = print_pages_params_->params; 1646 const PrintMsg_Print_Params& params = print_pages_params_->params;
1646 PrepareFrameAndViewForPrint prepare(params, frame, node, ignore_css_margins_); 1647 PrepareFrameAndViewForPrint prepare(params, frame, node, ignore_css_margins_);
1647 prepare.StartPrinting(); 1648 prepare.StartPrinting();
1648 1649
1649 *number_of_pages = prepare.GetExpectedPageCount(); 1650 *number_of_pages = prepare.GetExpectedPageCount();
1650 return true; 1651 return true;
1651 } 1652 }
1652 1653
1653 #if defined(ENABLE_PRINT_PREVIEW) 1654 #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
1654 bool PrintWebViewHelper::SetOptionsFromPdfDocument( 1655 bool PrintWebViewHelper::SetOptionsFromPdfDocument(
1655 PrintHostMsg_SetOptionsFromDocument_Params* options) { 1656 PrintHostMsg_SetOptionsFromDocument_Params* options) {
1656 blink::WebLocalFrame* source_frame = print_preview_context_.source_frame(); 1657 blink::WebLocalFrame* source_frame = print_preview_context_.source_frame();
1657 const blink::WebNode& source_node = print_preview_context_.source_node(); 1658 const blink::WebNode& source_node = print_preview_context_.source_node();
1658 1659
1659 blink::WebPrintPresetOptions preset_options; 1660 blink::WebPrintPresetOptions preset_options;
1660 if (!source_frame->getPrintPresetOptionsForPlugin(source_node, 1661 if (!source_frame->getPrintPresetOptionsForPlugin(source_node,
1661 &preset_options)) { 1662 &preset_options)) {
1662 return false; 1663 return false;
1663 } 1664 }
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
1752 if (!print_for_preview_) 1753 if (!print_for_preview_)
1753 print_preview_context_.set_error(PREVIEW_ERROR_INVALID_PRINTER_SETTINGS); 1754 print_preview_context_.set_error(PREVIEW_ERROR_INVALID_PRINTER_SETTINGS);
1754 else 1755 else
1755 Send(new PrintHostMsg_ShowInvalidPrinterSettingsError(routing_id())); 1756 Send(new PrintHostMsg_ShowInvalidPrinterSettingsError(routing_id()));
1756 1757
1757 return false; 1758 return false;
1758 } 1759 }
1759 1760
1760 return true; 1761 return true;
1761 } 1762 }
1762 #endif // defined(ENABLE_PRINT_PREVIEW) 1763 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
1763 1764
1764 #if defined(ENABLE_BASIC_PRINTING) 1765 #if BUILDFLAG(ENABLE_BASIC_PRINTING)
1765 bool PrintWebViewHelper::GetPrintSettingsFromUser(blink::WebLocalFrame* frame, 1766 bool PrintWebViewHelper::GetPrintSettingsFromUser(blink::WebLocalFrame* frame,
1766 const blink::WebNode& node, 1767 const blink::WebNode& node,
1767 int expected_pages_count, 1768 int expected_pages_count,
1768 bool is_scripted) { 1769 bool is_scripted) {
1769 PrintHostMsg_ScriptedPrint_Params params; 1770 PrintHostMsg_ScriptedPrint_Params params;
1770 PrintMsg_PrintPages_Params print_settings; 1771 PrintMsg_PrintPages_Params print_settings;
1771 1772
1772 params.cookie = print_pages_params_->params.document_cookie; 1773 params.cookie = print_pages_params_->params.document_cookie;
1773 params.has_selection = frame->hasSelection(); 1774 params.has_selection = frame->hasSelection();
1774 params.expected_pages_count = expected_pages_count; 1775 params.expected_pages_count = expected_pages_count;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
1808 prep_frame_view_.reset(new PrepareFrameAndViewForPrint( 1809 prep_frame_view_.reset(new PrepareFrameAndViewForPrint(
1809 print_params, frame, node, ignore_css_margins_)); 1810 print_params, frame, node, ignore_css_margins_));
1810 DCHECK(!print_pages_params_->params.selection_only || 1811 DCHECK(!print_pages_params_->params.selection_only ||
1811 print_pages_params_->pages.empty()); 1812 print_pages_params_->pages.empty());
1812 prep_frame_view_->CopySelectionIfNeeded( 1813 prep_frame_view_->CopySelectionIfNeeded(
1813 render_view()->GetWebkitPreferences(), 1814 render_view()->GetWebkitPreferences(),
1814 base::Bind(&PrintWebViewHelper::OnFramePreparedForPrintPages, 1815 base::Bind(&PrintWebViewHelper::OnFramePreparedForPrintPages,
1815 base::Unretained(this))); 1816 base::Unretained(this)));
1816 return true; 1817 return true;
1817 } 1818 }
1818 #endif // defined(ENABLE_BASIC_PRINTING) 1819 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING)
1819 1820
1820 #if !defined(OS_MACOSX) 1821 #if !defined(OS_MACOSX)
1821 void PrintWebViewHelper::PrintPageInternal( 1822 void PrintWebViewHelper::PrintPageInternal(
1822 const PrintMsg_PrintPage_Params& params, 1823 const PrintMsg_PrintPage_Params& params,
1823 blink::WebLocalFrame* frame, 1824 blink::WebLocalFrame* frame,
1824 PdfMetafileSkia* metafile, 1825 PdfMetafileSkia* metafile,
1825 gfx::Size* page_size_in_dpi, 1826 gfx::Size* page_size_in_dpi,
1826 gfx::Rect* content_area_in_dpi) { 1827 gfx::Rect* content_area_in_dpi) {
1827 PageSizeMargins page_layout_in_points; 1828 PageSizeMargins page_layout_in_points;
1828 1829
1829 double css_scale_factor = 1.0f; 1830 double css_scale_factor = 1.0f;
1830 #if defined(ENABLE_PRINT_PREVIEW) 1831 #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
1831 if (params.params.scale_factor > 0) 1832 if (params.params.scale_factor > 0)
1832 css_scale_factor = params.params.scale_factor; 1833 css_scale_factor = params.params.scale_factor;
1833 #endif 1834 #endif
1834 ComputePageLayoutInPointsForCss(frame, params.page_number, params.params, 1835 ComputePageLayoutInPointsForCss(frame, params.page_number, params.params,
1835 ignore_css_margins_, &css_scale_factor, 1836 ignore_css_margins_, &css_scale_factor,
1836 &page_layout_in_points); 1837 &page_layout_in_points);
1837 gfx::Size page_size; 1838 gfx::Size page_size;
1838 gfx::Rect content_area; 1839 gfx::Rect content_area;
1839 GetPageSizeAndContentAreaFromPageLayout(page_layout_in_points, &page_size, 1840 GetPageSizeAndContentAreaFromPageLayout(page_layout_in_points, &page_size,
1840 &content_area); 1841 &content_area);
(...skipping 25 matching lines...) Expand all
1866 float scale_factor = css_scale_factor; 1867 float scale_factor = css_scale_factor;
1867 #endif 1868 #endif
1868 1869
1869 SkCanvas* canvas = metafile->GetVectorCanvasForNewPage( 1870 SkCanvas* canvas = metafile->GetVectorCanvasForNewPage(
1870 page_size, canvas_area, scale_factor); 1871 page_size, canvas_area, scale_factor);
1871 if (!canvas) 1872 if (!canvas)
1872 return; 1873 return;
1873 1874
1874 MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas, metafile); 1875 MetafileSkiaWrapper::SetMetafileOnCanvas(*canvas, metafile);
1875 1876
1876 #if defined(ENABLE_PRINT_PREVIEW) 1877 #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
1877 if (params.params.display_header_footer) { 1878 if (params.params.display_header_footer) {
1878 // TODO(thestig): Figure out why Linux needs this. It is almost certainly 1879 // TODO(thestig): Figure out why Linux needs this. It is almost certainly
1879 // |printingMinimumShrinkFactor| from Blink. 1880 // |printingMinimumShrinkFactor| from Blink.
1880 #if defined(OS_WIN) 1881 #if defined(OS_WIN)
1881 const float fudge_factor = 1; 1882 const float fudge_factor = 1;
1882 #else 1883 #else
1883 const float fudge_factor = kPrintingMinimumShrinkFactor; 1884 const float fudge_factor = kPrintingMinimumShrinkFactor;
1884 #endif 1885 #endif
1885 // |page_number| is 0-based, so 1 is added. 1886 // |page_number| is 0-based, so 1 is added.
1886 PrintHeaderAndFooter(canvas, params.page_number + 1, 1887 PrintHeaderAndFooter(canvas, params.page_number + 1,
1887 print_preview_context_.total_page_count(), *frame, 1888 print_preview_context_.total_page_count(), *frame,
1888 scale_factor / fudge_factor, page_layout_in_points, 1889 scale_factor / fudge_factor, page_layout_in_points,
1889 params.params); 1890 params.params);
1890 } 1891 }
1891 #endif // defined(ENABLE_PRINT_PREVIEW) 1892 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
1892 1893
1893 float webkit_scale_factor = 1894 float webkit_scale_factor =
1894 RenderPageContent(frame, params.page_number, canvas_area, content_area, 1895 RenderPageContent(frame, params.page_number, canvas_area, content_area,
1895 scale_factor, canvas); 1896 scale_factor, canvas);
1896 DCHECK_GT(webkit_scale_factor, 0.0f); 1897 DCHECK_GT(webkit_scale_factor, 0.0f);
1897 1898
1898 // Done printing. Close the canvas to retrieve the compiled metafile. 1899 // Done printing. Close the canvas to retrieve the compiled metafile.
1899 if (!metafile->FinishPage()) 1900 if (!metafile->FinishPage())
1900 NOTREACHED() << "metafile failed"; 1901 NOTREACHED() << "metafile failed";
1901 } 1902 }
(...skipping 15 matching lines...) Expand all
1917 return false; 1918 return false;
1918 1919
1919 if (!metafile.GetData(shared_buf->memory(), buf_size)) 1920 if (!metafile.GetData(shared_buf->memory(), buf_size))
1920 return false; 1921 return false;
1921 1922
1922 *shared_mem_handle = 1923 *shared_mem_handle =
1923 base::SharedMemory::DuplicateHandle(shared_buf->handle()); 1924 base::SharedMemory::DuplicateHandle(shared_buf->handle());
1924 return true; 1925 return true;
1925 } 1926 }
1926 1927
1927 #if defined(ENABLE_PRINT_PREVIEW) 1928 #if BUILDFLAG(ENABLE_PRINT_PREVIEW)
1928 void PrintWebViewHelper::ShowScriptedPrintPreview() { 1929 void PrintWebViewHelper::ShowScriptedPrintPreview() {
1929 if (is_scripted_preview_delayed_) { 1930 if (is_scripted_preview_delayed_) {
1930 is_scripted_preview_delayed_ = false; 1931 is_scripted_preview_delayed_ = false;
1931 Send(new PrintHostMsg_ShowScriptedPrintPreview( 1932 Send(new PrintHostMsg_ShowScriptedPrintPreview(
1932 routing_id(), print_preview_context_.IsModifiable())); 1933 routing_id(), print_preview_context_.IsModifiable()));
1933 } 1934 }
1934 } 1935 }
1935 1936
1936 void PrintWebViewHelper::RequestPrintPreview(PrintPreviewRequestType type) { 1937 void PrintWebViewHelper::RequestPrintPreview(PrintPreviewRequestType type) {
1937 const bool is_modifiable = print_preview_context_.IsModifiable(); 1938 const bool is_modifiable = print_preview_context_.IsModifiable();
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
2045 return false; 2046 return false;
2046 } 2047 }
2047 preview_page_params.data_size = metafile->GetDataSize(); 2048 preview_page_params.data_size = metafile->GetDataSize();
2048 preview_page_params.page_number = page_number; 2049 preview_page_params.page_number = page_number;
2049 preview_page_params.preview_request_id = 2050 preview_page_params.preview_request_id =
2050 print_pages_params_->params.preview_request_id; 2051 print_pages_params_->params.preview_request_id;
2051 2052
2052 Send(new PrintHostMsg_DidPreviewPage(routing_id(), preview_page_params)); 2053 Send(new PrintHostMsg_DidPreviewPage(routing_id(), preview_page_params));
2053 return true; 2054 return true;
2054 } 2055 }
2055 #endif // defined(ENABLE_PRINT_PREVIEW) 2056 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
2056 2057
2057 PrintWebViewHelper::PrintPreviewContext::PrintPreviewContext() 2058 PrintWebViewHelper::PrintPreviewContext::PrintPreviewContext()
2058 : total_page_count_(0), 2059 : total_page_count_(0),
2059 current_page_index_(0), 2060 current_page_index_(0),
2060 generate_draft_pages_(true), 2061 generate_draft_pages_(true),
2061 print_ready_metafile_page_count_(0), 2062 print_ready_metafile_page_count_(0),
2062 error_(PREVIEW_ERROR_NONE), 2063 error_(PREVIEW_ERROR_NONE),
2063 state_(UNINITIALIZED) { 2064 state_(UNINITIALIZED) {
2064 } 2065 }
2065 2066
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after
2333 blink::WebConsoleMessage::LevelWarning, message)); 2334 blink::WebConsoleMessage::LevelWarning, message));
2334 return false; 2335 return false;
2335 } 2336 }
2336 2337
2337 void PrintWebViewHelper::ScriptingThrottler::Reset() { 2338 void PrintWebViewHelper::ScriptingThrottler::Reset() {
2338 // Reset counter on successful print. 2339 // Reset counter on successful print.
2339 count_ = 0; 2340 count_ = 0;
2340 } 2341 }
2341 2342
2342 } // namespace printing 2343 } // namespace printing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698