OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/renderer/printing/print_web_view_helper.h" | 5 #include "chrome/renderer/printing/print_web_view_helper.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 | 436 |
437 blink::WebView* FrameReference::view() { | 437 blink::WebView* FrameReference::view() { |
438 return view_; | 438 return view_; |
439 } | 439 } |
440 | 440 |
441 // static - Not anonymous so that platform implementations can use it. | 441 // static - Not anonymous so that platform implementations can use it. |
442 void PrintWebViewHelper::PrintHeaderAndFooter( | 442 void PrintWebViewHelper::PrintHeaderAndFooter( |
443 blink::WebCanvas* canvas, | 443 blink::WebCanvas* canvas, |
444 int page_number, | 444 int page_number, |
445 int total_pages, | 445 int total_pages, |
| 446 const blink::WebFrame& source_frame, |
446 float webkit_scale_factor, | 447 float webkit_scale_factor, |
447 const PageSizeMargins& page_layout, | 448 const PageSizeMargins& page_layout, |
448 const base::DictionaryValue& header_footer_info, | |
449 const PrintMsg_Print_Params& params) { | 449 const PrintMsg_Print_Params& params) { |
450 skia::VectorPlatformDeviceSkia* device = | 450 skia::VectorPlatformDeviceSkia* device = |
451 static_cast<skia::VectorPlatformDeviceSkia*>(canvas->getTopDevice()); | 451 static_cast<skia::VectorPlatformDeviceSkia*>(canvas->getTopDevice()); |
452 device->setDrawingArea(SkPDFDevice::kMargin_DrawingArea); | 452 device->setDrawingArea(SkPDFDevice::kMargin_DrawingArea); |
453 | 453 |
454 SkAutoCanvasRestore auto_restore(canvas, true); | 454 SkAutoCanvasRestore auto_restore(canvas, true); |
455 canvas->scale(1 / webkit_scale_factor, 1 / webkit_scale_factor); | 455 canvas->scale(1 / webkit_scale_factor, 1 / webkit_scale_factor); |
456 | 456 |
457 blink::WebSize page_size(page_layout.margin_left + page_layout.margin_right + | 457 blink::WebSize page_size(page_layout.margin_left + page_layout.margin_right + |
458 page_layout.content_width, | 458 page_layout.content_width, |
459 page_layout.margin_top + page_layout.margin_bottom + | 459 page_layout.margin_top + page_layout.margin_bottom + |
460 page_layout.content_height); | 460 page_layout.content_height); |
461 | 461 |
462 blink::WebView* web_view = blink::WebView::create(NULL); | 462 blink::WebView* web_view = blink::WebView::create(NULL); |
463 web_view->settings()->setJavaScriptEnabled(true); | 463 web_view->settings()->setJavaScriptEnabled(true); |
464 | 464 |
465 blink::WebLocalFrame* frame = blink::WebLocalFrame::create(NULL); | 465 blink::WebLocalFrame* frame = blink::WebLocalFrame::create(NULL); |
466 web_view->setMainFrame(frame); | 466 web_view->setMainFrame(frame); |
467 | 467 |
468 base::StringValue html( | 468 base::StringValue html(ResourceBundle::GetSharedInstance().GetLocalizedString( |
469 ResourceBundle::GetSharedInstance().GetLocalizedString( | 469 IDR_PRINT_PREVIEW_PAGE)); |
470 IDR_PRINT_PREVIEW_PAGE)); | |
471 // Load page with script to avoid async operations. | 470 // Load page with script to avoid async operations. |
472 ExecuteScript(frame, kPageLoadScriptFormat, html); | 471 ExecuteScript(frame, kPageLoadScriptFormat, html); |
473 | 472 |
474 scoped_ptr<base::DictionaryValue> options(header_footer_info.DeepCopy()); | 473 scoped_ptr<base::DictionaryValue> options(new base::DictionaryValue()); |
| 474 options.reset(new base::DictionaryValue()); |
| 475 options->SetDouble(kSettingHeaderFooterDate, base::Time::Now().ToJsTime()); |
475 options->SetDouble("width", page_size.width); | 476 options->SetDouble("width", page_size.width); |
476 options->SetDouble("height", page_size.height); | 477 options->SetDouble("height", page_size.height); |
477 options->SetDouble("topMargin", page_layout.margin_top); | 478 options->SetDouble("topMargin", page_layout.margin_top); |
478 options->SetDouble("bottomMargin", page_layout.margin_bottom); | 479 options->SetDouble("bottomMargin", page_layout.margin_bottom); |
479 options->SetString("pageNumber", | 480 options->SetString("pageNumber", |
480 base::StringPrintf("%d/%d", page_number, total_pages)); | 481 base::StringPrintf("%d/%d", page_number, total_pages)); |
481 | 482 |
| 483 // Fallback to initiator URL and title if it's empty for printed frame. |
| 484 base::string16 url = source_frame.document().url().string(); |
| 485 options->SetString("url", url.empty() ? params.url : url); |
| 486 base::string16 title = source_frame.document().title(); |
| 487 options->SetString("title", title.empty() ? params.title : title); |
| 488 |
482 ExecuteScript(frame, kPageSetupScriptFormat, *options); | 489 ExecuteScript(frame, kPageSetupScriptFormat, *options); |
483 | 490 |
484 blink::WebPrintParams webkit_params(page_size); | 491 blink::WebPrintParams webkit_params(page_size); |
485 webkit_params.printerDPI = GetDPI(¶ms); | 492 webkit_params.printerDPI = GetDPI(¶ms); |
486 | 493 |
487 frame->printBegin(webkit_params); | 494 frame->printBegin(webkit_params); |
488 frame->printPage(0, canvas); | 495 frame->printPage(0, canvas); |
489 frame->printEnd(); | 496 frame->printEnd(); |
490 | 497 |
491 web_view->close(); | 498 web_view->close(); |
(...skipping 994 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1486 &settings.params.is_first_request)) { | 1493 &settings.params.is_first_request)) { |
1487 NOTREACHED(); | 1494 NOTREACHED(); |
1488 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); | 1495 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); |
1489 return false; | 1496 return false; |
1490 } | 1497 } |
1491 | 1498 |
1492 settings.params.print_to_pdf = IsPrintToPdfRequested(*job_settings); | 1499 settings.params.print_to_pdf = IsPrintToPdfRequested(*job_settings); |
1493 UpdateFrameMarginsCssInfo(*job_settings); | 1500 UpdateFrameMarginsCssInfo(*job_settings); |
1494 settings.params.print_scaling_option = GetPrintScalingOption( | 1501 settings.params.print_scaling_option = GetPrintScalingOption( |
1495 frame, node, source_is_html, *job_settings, settings.params); | 1502 frame, node, source_is_html, *job_settings, settings.params); |
1496 | |
1497 // Header/Footer: Set |header_footer_info_|. | |
1498 if (settings.params.display_header_footer) { | |
1499 header_footer_info_.reset(new base::DictionaryValue()); | |
1500 header_footer_info_->SetDouble(kSettingHeaderFooterDate, | |
1501 base::Time::Now().ToJsTime()); | |
1502 header_footer_info_->SetString(kSettingHeaderFooterURL, | |
1503 settings.params.url); | |
1504 header_footer_info_->SetString(kSettingHeaderFooterTitle, | |
1505 settings.params.title); | |
1506 } | |
1507 } | 1503 } |
1508 | 1504 |
1509 SetPrintPagesParams(settings); | 1505 SetPrintPagesParams(settings); |
1510 | 1506 |
1511 if (!PrintMsg_Print_Params_IsValid(settings.params)) { | 1507 if (!PrintMsg_Print_Params_IsValid(settings.params)) { |
1512 if (!print_for_preview_) | 1508 if (!print_for_preview_) |
1513 print_preview_context_.set_error(PREVIEW_ERROR_INVALID_PRINTER_SETTINGS); | 1509 print_preview_context_.set_error(PREVIEW_ERROR_INVALID_PRINTER_SETTINGS); |
1514 else | 1510 else |
1515 Send(new PrintHostMsg_ShowInvalidPrinterSettingsError(routing_id())); | 1511 Send(new PrintHostMsg_ShowInvalidPrinterSettingsError(routing_id())); |
1516 | 1512 |
(...skipping 447 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1964 } | 1960 } |
1965 | 1961 |
1966 void PrintWebViewHelper::SetPrintPagesParams( | 1962 void PrintWebViewHelper::SetPrintPagesParams( |
1967 const PrintMsg_PrintPages_Params& settings) { | 1963 const PrintMsg_PrintPages_Params& settings) { |
1968 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); | 1964 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); |
1969 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(), | 1965 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(), |
1970 settings.params.document_cookie)); | 1966 settings.params.document_cookie)); |
1971 } | 1967 } |
1972 | 1968 |
1973 } // namespace printing | 1969 } // namespace printing |
OLD | NEW |