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

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

Issue 7993005: Reuse PrintContext to excessively triggering matchMedia('print') listeners. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Adds a unit test and updates DPI on change. Created 9 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 | Annotate | Revision Log
OLDNEW
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 354 matching lines...) Expand 10 before | Expand all | Expand 10 after
365 #if defined(USE_SKIA) 365 #if defined(USE_SKIA)
366 device->setDrawingArea(SkPDFDevice::kContent_DrawingArea); 366 device->setDrawingArea(SkPDFDevice::kContent_DrawingArea);
367 #endif 367 #endif
368 } 368 }
369 369
370 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( 370 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint(
371 const PrintMsg_Print_Params& print_params, 371 const PrintMsg_Print_Params& print_params,
372 WebFrame* frame, 372 WebFrame* frame,
373 const WebNode& node) 373 const WebNode& node)
374 : frame_(frame), 374 : frame_(frame),
375 node_to_print_(node),
376 web_view_(frame->view()), 375 web_view_(frame->view()),
377 dpi_(static_cast<int>(print_params.dpi)),
378 expected_pages_count_(0), 376 expected_pages_count_(0),
379 use_browser_overlays_(true),
380 finished_(false) { 377 finished_(false) {
381 gfx::Size canvas_size; 378 gfx::Size canvas_size;
382 CalculatePrintCanvasSize(print_params, &canvas_size); 379 CalculatePrintCanvasSize(print_params, &canvas_size);
383 380
384 if (WebFrame* web_frame = web_view_->mainFrame()) 381 if (WebFrame* web_frame = web_view_->mainFrame())
385 prev_scroll_offset_ = web_frame->scrollOffset(); 382 prev_scroll_offset_ = web_frame->scrollOffset();
386 prev_view_size_ = web_view_->size(); 383 prev_view_size_ = web_view_->size();
387 384
388 StartPrinting(canvas_size); 385 frame_->printBegin(node);
386 StartPrinting(canvas_size, static_cast<int>(print_params.dpi));
389 } 387 }
390 388
391 PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() { 389 PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() {
392 FinishPrinting(); 390 FinishPrinting();
393 } 391 }
394 392
395 void PrepareFrameAndViewForPrint::UpdatePrintParams( 393 void PrepareFrameAndViewForPrint::UpdatePrintParams(
396 const PrintMsg_Print_Params& print_params) { 394 const PrintMsg_Print_Params& print_params) {
397 DCHECK(!finished_); 395 DCHECK(!finished_);
398 gfx::Size canvas_size; 396 gfx::Size canvas_size;
399 CalculatePrintCanvasSize(print_params, &canvas_size); 397 CalculatePrintCanvasSize(print_params, &canvas_size);
400 if (canvas_size == print_canvas_size_) 398 int dpi = static_cast<int>(print_params.dpi);
399 if (canvas_size == print_canvas_size_ && dpi == dpi_)
401 return; 400 return;
402 401
403 frame_->printEnd(); 402 StartPrinting(canvas_size, dpi);
404 dpi_ = static_cast<int>(print_params.dpi); 403 }
405 StartPrinting(canvas_size); 404
405 bool PrepareFrameAndViewForPrint::ShouldUseBrowserOverlays() const {
406 return frame_->shouldUseBrowserOverlays();
406 } 407 }
407 408
408 void PrepareFrameAndViewForPrint::StartPrinting( 409 void PrepareFrameAndViewForPrint::StartPrinting(
409 const gfx::Size& print_canvas_size) { 410 const gfx::Size& print_canvas_size,
411 int dpi) {
410 print_canvas_size_ = print_canvas_size; 412 print_canvas_size_ = print_canvas_size;
413 dpi_ = dpi;
411 414
412 // Layout page according to printer page size. Since WebKit shrinks the 415 // Layout page according to printer page size. Since WebKit shrinks the
413 // size of the page automatically (from 125% to 200%) we trick it to 416 // size of the page automatically (from 125% to 200%) we trick it to
414 // think the page is 125% larger so the size of the page is correct for 417 // think the page is 125% larger so the size of the page is correct for
415 // minimum (default) scaling. 418 // minimum (default) scaling.
416 // This is important for sites that try to fill the page. 419 // This is important for sites that try to fill the page.
417 gfx::Size print_layout_size(print_canvas_size_); 420 gfx::Size print_layout_size(print_canvas_size_);
418 print_layout_size.set_height(static_cast<int>( 421 print_layout_size.set_height(static_cast<int>(
419 static_cast<double>(print_layout_size.height()) * 1.25)); 422 static_cast<double>(print_layout_size.height()) * 1.25));
420 423
421 web_view_->resize(print_layout_size); 424 web_view_->resize(print_layout_size);
422 425
423 expected_pages_count_ = frame_->printBegin(print_canvas_size_, node_to_print_, 426 expected_pages_count_ =
424 dpi_, &use_browser_overlays_); 427 frame_->setPageSizeResolution(print_canvas_size_, dpi);
425 } 428 }
426 429
427 void PrepareFrameAndViewForPrint::FinishPrinting() { 430 void PrepareFrameAndViewForPrint::FinishPrinting() {
428 if (!finished_) { 431 if (!finished_) {
429 finished_ = true; 432 finished_ = true;
430 frame_->printEnd(); 433 frame_->printEnd();
431 web_view_->resize(prev_view_size_); 434 web_view_->resize(prev_view_size_);
432 if (WebFrame* web_frame = web_view_->mainFrame()) 435 if (WebFrame* web_frame = web_view_->mainFrame())
433 web_frame->setScrollOffset(prev_scroll_offset_); 436 web_frame->setScrollOffset(prev_scroll_offset_);
434 } 437 }
(...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after
741 return; // Failed to init print page settings. 744 return; // Failed to init print page settings.
742 } 745 }
743 746
744 int expected_page_count = 0; 747 int expected_page_count = 0;
745 bool use_browser_overlays = true; 748 bool use_browser_overlays = true;
746 749
747 expected_page_count = prepare->GetExpectedPageCount(); 750 expected_page_count = prepare->GetExpectedPageCount();
748 if (expected_page_count) 751 if (expected_page_count)
749 use_browser_overlays = prepare->ShouldUseBrowserOverlays(); 752 use_browser_overlays = prepare->ShouldUseBrowserOverlays();
750 753
751 // Release the prepare before going any further, since we are going to
752 // show UI and wait for the user.
753 prepare.reset();
754
755 // Some full screen plugins can say they don't want to print. 754 // Some full screen plugins can say they don't want to print.
756 if (!expected_page_count) { 755 if (!expected_page_count) {
757 DidFinishPrinting(OK); // Release resources and fail silently. 756 DidFinishPrinting(OK); // Release resources and fail silently.
758 return; 757 return;
759 } 758 }
760 759
761 // Ask the browser to show UI to retrieve the final print settings. 760 // Ask the browser to show UI to retrieve the final print settings.
762 if (!GetPrintSettingsFromUser(frame, node, expected_page_count, 761 if (!GetPrintSettingsFromUser(frame, node, expected_page_count,
763 use_browser_overlays)) { 762 use_browser_overlays)) {
764 DidFinishPrinting(OK); // Release resources and fail silently. 763 DidFinishPrinting(OK); // Release resources and fail silently.
765 return; 764 return;
766 } 765 }
767 766
768 // Render Pages for printing. 767 // Render Pages for printing.
769 if (!RenderPagesForPrint(frame, node, NULL)) { 768 if (!RenderPagesForPrint(frame, node, prepare.get())) {
770 LOG(ERROR) << "RenderPagesForPrint failed"; 769 LOG(ERROR) << "RenderPagesForPrint failed";
771 DidFinishPrinting(FAIL_PRINT); 770 DidFinishPrinting(FAIL_PRINT);
772 } 771 }
773 ResetScriptedPrintCount(); 772 ResetScriptedPrintCount();
774 } 773 }
775 774
776 void PrintWebViewHelper::DidFinishPrinting(PrintingResult result) { 775 void PrintWebViewHelper::DidFinishPrinting(PrintingResult result) {
777 bool store_print_pages_params = true; 776 bool store_print_pages_params = true;
778 if (result == FAIL_PRINT) { 777 if (result == FAIL_PRINT) {
779 DisplayPrintJobError(); 778 DisplayPrintJobError();
(...skipping 786 matching lines...) Expand 10 before | Expand all | Expand 10 after
1566 DCHECK(IsRendering()); 1565 DCHECK(IsRendering());
1567 return prep_frame_view_->GetPrintCanvasSize(); 1566 return prep_frame_view_->GetPrintCanvasSize();
1568 } 1567 }
1569 1568
1570 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { 1569 void PrintWebViewHelper::PrintPreviewContext::ClearContext() {
1571 prep_frame_view_.reset(); 1570 prep_frame_view_.reset();
1572 metafile_.reset(); 1571 metafile_.reset();
1573 pages_to_render_.clear(); 1572 pages_to_render_.clear();
1574 error_ = PREVIEW_ERROR_NONE; 1573 error_ = PREVIEW_ERROR_NONE;
1575 } 1574 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698