OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
421 device->setDrawingArea(SkPDFDevice::kContent_DrawingArea); | 421 device->setDrawingArea(SkPDFDevice::kContent_DrawingArea); |
422 #endif | 422 #endif |
423 } | 423 } |
424 | 424 |
425 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( | 425 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( |
426 const PrintMsg_Print_Params& print_params, | 426 const PrintMsg_Print_Params& print_params, |
427 WebFrame* frame, | 427 WebFrame* frame, |
428 WebNode* node) | 428 WebNode* node) |
429 : frame_(frame), | 429 : frame_(frame), |
430 web_view_(frame->view()), | 430 web_view_(frame->view()), |
431 dpi_(static_cast<int>(print_params.dpi)), | |
432 expected_pages_count_(0), | 431 expected_pages_count_(0), |
433 use_browser_overlays_(true), | |
434 finished_(false) { | 432 finished_(false) { |
435 gfx::Size canvas_size; | 433 gfx::Size canvas_size; |
436 CalculatePrintCanvasSize(print_params, &canvas_size); | 434 CalculatePrintCanvasSize(print_params, &canvas_size); |
437 | 435 |
438 if (WebFrame* web_frame = web_view_->mainFrame()) | 436 if (WebFrame* web_frame = web_view_->mainFrame()) |
439 prev_scroll_offset_ = web_frame->scrollOffset(); | 437 prev_scroll_offset_ = web_frame->scrollOffset(); |
440 prev_view_size_ = web_view_->size(); | 438 prev_view_size_ = web_view_->size(); |
441 | 439 |
440 WebKit::WebNode node_to_print; | |
442 if (node) | 441 if (node) |
443 node_to_print_ = *node; | 442 node_to_print = *node; |
444 | 443 |
445 StartPrinting(canvas_size); | 444 frame_->printBegin(node_to_print); |
445 StartPrinting(canvas_size, static_cast<int>(print_params.dpi)); | |
446 } | 446 } |
447 | 447 |
448 PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() { | 448 PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() { |
449 FinishPrinting(); | 449 FinishPrinting(); |
450 } | 450 } |
451 | 451 |
452 void PrepareFrameAndViewForPrint::UpdatePrintParams( | 452 void PrepareFrameAndViewForPrint::UpdatePrintParams( |
453 const PrintMsg_Print_Params& print_params) { | 453 const PrintMsg_Print_Params& print_params) { |
454 DCHECK(!finished_); | 454 DCHECK(!finished_); |
455 gfx::Size canvas_size; | 455 gfx::Size canvas_size; |
456 CalculatePrintCanvasSize(print_params, &canvas_size); | 456 CalculatePrintCanvasSize(print_params, &canvas_size); |
457 if (canvas_size == print_canvas_size_) | 457 if (canvas_size == print_canvas_size_) |
458 return; | 458 return; |
459 | 459 |
460 frame_->printEnd(); | 460 StartPrinting(canvas_size, static_cast<int>(print_params.dpi)); |
461 dpi_ = static_cast<int>(print_params.dpi); | 461 } |
462 StartPrinting(canvas_size); | 462 |
463 bool PrepareFrameAndViewForPrint::ShouldUseBrowserOverlays() const | |
464 { | |
465 return frame_->shouldUseBrowserOverlays(); | |
463 } | 466 } |
464 | 467 |
465 void PrepareFrameAndViewForPrint::StartPrinting( | 468 void PrepareFrameAndViewForPrint::StartPrinting( |
466 const gfx::Size& print_canvas_size) { | 469 const gfx::Size& print_canvas_size, |
470 int dpi) { | |
467 print_canvas_size_ = print_canvas_size; | 471 print_canvas_size_ = print_canvas_size; |
468 | 472 |
469 // Layout page according to printer page size. Since WebKit shrinks the | 473 // Layout page according to printer page size. Since WebKit shrinks the |
470 // size of the page automatically (from 125% to 200%) we trick it to | 474 // size of the page automatically (from 125% to 200%) we trick it to |
471 // think the page is 125% larger so the size of the page is correct for | 475 // think the page is 125% larger so the size of the page is correct for |
472 // minimum (default) scaling. | 476 // minimum (default) scaling. |
473 // This is important for sites that try to fill the page. | 477 // This is important for sites that try to fill the page. |
474 gfx::Size print_layout_size(print_canvas_size_); | 478 gfx::Size print_layout_size(print_canvas_size_); |
475 print_layout_size.set_height(static_cast<int>( | 479 print_layout_size.set_height(static_cast<int>( |
476 static_cast<double>(print_layout_size.height()) * 1.25)); | 480 static_cast<double>(print_layout_size.height()) * 1.25)); |
477 | 481 |
478 web_view_->resize(print_layout_size); | 482 web_view_->resize(print_layout_size); |
479 | 483 |
480 expected_pages_count_ = frame_->printBegin(print_canvas_size_, node_to_print_, | 484 expected_pages_count_ = |
481 dpi_, &use_browser_overlays_); | 485 frame_->setPageSizeResolution(print_canvas_size_, dpi); |
482 } | 486 } |
483 | 487 |
484 void PrepareFrameAndViewForPrint::FinishPrinting() { | 488 void PrepareFrameAndViewForPrint::FinishPrinting() { |
485 if (!finished_) { | 489 if (!finished_) { |
486 finished_ = true; | 490 finished_ = true; |
487 frame_->printEnd(); | 491 frame_->printEnd(); |
488 web_view_->resize(prev_view_size_); | 492 web_view_->resize(prev_view_size_); |
489 if (WebFrame* web_frame = web_view_->mainFrame()) | 493 if (WebFrame* web_frame = web_view_->mainFrame()) |
490 web_frame->setScrollOffset(prev_scroll_offset_); | 494 web_frame->setScrollOffset(prev_scroll_offset_); |
491 } | 495 } |
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
807 if (!InitPrintSettingsAndPrepareFrame(frame, node, &prepare)) | 811 if (!InitPrintSettingsAndPrepareFrame(frame, node, &prepare)) |
808 return; // Failed to init print page settings. | 812 return; // Failed to init print page settings. |
809 | 813 |
810 int expected_page_count = 0; | 814 int expected_page_count = 0; |
811 bool use_browser_overlays = true; | 815 bool use_browser_overlays = true; |
812 | 816 |
813 expected_page_count = prepare->GetExpectedPageCount(); | 817 expected_page_count = prepare->GetExpectedPageCount(); |
814 if (expected_page_count) | 818 if (expected_page_count) |
815 use_browser_overlays = prepare->ShouldUseBrowserOverlays(); | 819 use_browser_overlays = prepare->ShouldUseBrowserOverlays(); |
816 | 820 |
817 // Release the prepare before going any further, since we are going to | |
818 // show UI and wait for the user. | |
819 prepare.reset(); | |
vandebo (ex-Chrome)
2011/10/24 20:53:47
I think the reason this is done is not to make sur
dominicc (has gone to gerrit)
2011/10/25 04:44:39
Is that platform-specific, or specific to the new
Lei Zhang
2011/10/25 06:40:22
Unless you changed things in about:flags, in M15+
vandebo (ex-Chrome)
2011/10/25 17:03:11
As Lei noted, this code path is only used for the
| |
820 | |
821 // Some full screen plugins can say they don't want to print. | 821 // Some full screen plugins can say they don't want to print. |
822 if (!expected_page_count) { | 822 if (!expected_page_count) { |
823 DidFinishPrinting(OK); // Release resources and fail silently. | 823 DidFinishPrinting(OK); // Release resources and fail silently. |
824 return; | 824 return; |
825 } | 825 } |
826 | 826 |
827 // Ask the browser to show UI to retrieve the final print settings. | 827 // Ask the browser to show UI to retrieve the final print settings. |
828 if (!GetPrintSettingsFromUser(frame, expected_page_count, | 828 if (!GetPrintSettingsFromUser(frame, expected_page_count, |
829 use_browser_overlays)) { | 829 use_browser_overlays)) { |
830 DidFinishPrinting(OK); // Release resources and fail silently. | 830 DidFinishPrinting(OK); // Release resources and fail silently. |
831 return; | 831 return; |
832 } | 832 } |
833 | 833 |
834 // Render Pages for printing. | 834 // Render Pages for printing. |
835 if (!RenderPagesForPrint(frame, node, NULL)) { | 835 if (!RenderPagesForPrint(frame, node, prepare.get())) { |
Lei Zhang
2011/09/22 21:48:30
Not sure if this is right. If you use the |prepare
dominicc (has gone to gerrit)
2011/10/24 08:45:13
This code is quite twisty, but don’t the settings
| |
836 LOG(ERROR) << "RenderPagesForPrint failed"; | 836 LOG(ERROR) << "RenderPagesForPrint failed"; |
837 DidFinishPrinting(FAIL_PRINT); | 837 DidFinishPrinting(FAIL_PRINT); |
838 } | 838 } |
839 ResetScriptedPrintCount(); | 839 ResetScriptedPrintCount(); |
840 } | 840 } |
841 | 841 |
842 void PrintWebViewHelper::DidFinishPrinting(PrintingResult result) { | 842 void PrintWebViewHelper::DidFinishPrinting(PrintingResult result) { |
843 bool store_print_pages_params = true; | 843 bool store_print_pages_params = true; |
844 if (result == FAIL_PRINT) { | 844 if (result == FAIL_PRINT) { |
845 DisplayPrintJobError(); | 845 DisplayPrintJobError(); |
(...skipping 703 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1549 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { | 1549 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { |
1550 return prep_frame_view_->GetPrintCanvasSize(); | 1550 return prep_frame_view_->GetPrintCanvasSize(); |
1551 } | 1551 } |
1552 | 1552 |
1553 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { | 1553 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
1554 prep_frame_view_.reset(); | 1554 prep_frame_view_.reset(); |
1555 metafile_.reset(); | 1555 metafile_.reset(); |
1556 pages_to_render_.clear(); | 1556 pages_to_render_.clear(); |
1557 error_ = PREVIEW_ERROR_NONE; | 1557 error_ = PREVIEW_ERROR_NONE; |
1558 } | 1558 } |
OLD | NEW |