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 684 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
695 PrintHostMsg_DidGetPreviewPageCount_Params params; | 695 PrintHostMsg_DidGetPreviewPageCount_Params params; |
696 params.page_count = print_preview_context_.total_page_count(); | 696 params.page_count = print_preview_context_.total_page_count(); |
697 params.is_modifiable = print_preview_context_.IsModifiable(); | 697 params.is_modifiable = print_preview_context_.IsModifiable(); |
698 params.document_cookie = print_pages_params_->params.document_cookie; | 698 params.document_cookie = print_pages_params_->params.document_cookie; |
699 params.preview_request_id = print_pages_params_->params.preview_request_id; | 699 params.preview_request_id = print_pages_params_->params.preview_request_id; |
700 params.clear_preview_data = print_preview_context_.generate_draft_pages(); | 700 params.clear_preview_data = print_preview_context_.generate_draft_pages(); |
701 Send(new PrintHostMsg_DidGetPreviewPageCount(routing_id(), params)); | 701 Send(new PrintHostMsg_DidGetPreviewPageCount(routing_id(), params)); |
702 if (CheckForCancel()) | 702 if (CheckForCancel()) |
703 return false; | 703 return false; |
704 | 704 |
705 int page_number; | 705 while (!print_preview_context_.IsFinalPageRendered()) { |
706 while ((page_number = print_preview_context_.GetNextPageNumber()) >= 0) { | 706 int page_number = print_preview_context_.GetNextPageNumber(); |
| 707 DCHECK(page_number >= 0); |
707 if (!RenderPreviewPage(page_number)) | 708 if (!RenderPreviewPage(page_number)) |
708 return false; | 709 return false; |
| 710 |
709 if (CheckForCancel()) | 711 if (CheckForCancel()) |
710 return false; | 712 return false; |
| 713 |
| 714 // We must call PrepareFrameAndViewForPrint::FinishPrinting() (by way of |
| 715 // print_preview_context_.AllPagesRendered()) before calling |
| 716 // FinalizePrintReadyDocument() when printing a PDF because the plugin |
| 717 // code does not generate output until we call FinishPrinting(). We do not |
| 718 // generate draft pages for PDFs, so IsFinalPageRendered() and |
| 719 // IsLastPageOfPrintReadyMetafile() will be true in the same iteration of |
| 720 // the loop. |
| 721 if (print_preview_context_.IsFinalPageRendered()) |
| 722 print_preview_context_.AllPagesRendered(); |
| 723 |
711 if (print_preview_context_.IsLastPageOfPrintReadyMetafile()) { | 724 if (print_preview_context_.IsLastPageOfPrintReadyMetafile()) { |
712 // Finished generating preview. Finalize the document. | 725 DCHECK(print_preview_context_.IsModifiable() || |
| 726 print_preview_context_.IsFinalPageRendered()); |
713 if (!FinalizePrintReadyDocument()) | 727 if (!FinalizePrintReadyDocument()) |
714 return false; | 728 return false; |
715 } | 729 } |
716 } | 730 } |
717 print_preview_context_.AllPagesRendered(); | |
718 print_preview_context_.Finished(); | 731 print_preview_context_.Finished(); |
719 return true; | 732 return true; |
720 } | 733 } |
721 | 734 |
722 bool PrintWebViewHelper::FinalizePrintReadyDocument() { | 735 bool PrintWebViewHelper::FinalizePrintReadyDocument() { |
723 DCHECK(!is_print_ready_metafile_sent_); | 736 DCHECK(!is_print_ready_metafile_sent_); |
724 print_preview_context_.FinalizePrintReadyDocument(); | 737 print_preview_context_.FinalizePrintReadyDocument(); |
725 | 738 |
726 // Get the size of the resulting metafile. | 739 // Get the size of the resulting metafile. |
727 printing::PreviewMetafile* metafile = print_preview_context_.metafile(); | 740 printing::PreviewMetafile* metafile = print_preview_context_.metafile(); |
(...skipping 644 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1372 } | 1385 } |
1373 | 1386 |
1374 int selected_page_count = pages.size(); | 1387 int selected_page_count = pages.size(); |
1375 current_page_index_ = 0; | 1388 current_page_index_ = 0; |
1376 print_ready_metafile_page_count_ = selected_page_count; | 1389 print_ready_metafile_page_count_ = selected_page_count; |
1377 pages_to_render_ = pages; | 1390 pages_to_render_ = pages; |
1378 | 1391 |
1379 if (selected_page_count == 0) { | 1392 if (selected_page_count == 0) { |
1380 print_ready_metafile_page_count_ = total_page_count_; | 1393 print_ready_metafile_page_count_ = total_page_count_; |
1381 // Render all pages. | 1394 // Render all pages. |
1382 for (int i = 0; i < total_page_count_; i++) | 1395 for (int i = 0; i < total_page_count_; ++i) |
1383 pages_to_render_.push_back(i); | 1396 pages_to_render_.push_back(i); |
1384 } else if (generate_draft_pages_) { | 1397 } else if (generate_draft_pages_) { |
1385 int pages_index = 0; | 1398 int pages_index = 0; |
1386 for (int i = 0; i < total_page_count_; i++) { | 1399 for (int i = 0; i < total_page_count_; ++i) { |
1387 if (pages_index < selected_page_count && i == pages[pages_index]) { | 1400 if (pages_index < selected_page_count && i == pages[pages_index]) { |
1388 pages_index++; | 1401 pages_index++; |
1389 continue; | 1402 continue; |
1390 } | 1403 } |
1391 pages_to_render_.push_back(i); | 1404 pages_to_render_.push_back(i); |
1392 } | 1405 } |
1393 } | 1406 } |
1394 | 1407 |
1395 document_render_time_ = base::TimeDelta(); | 1408 document_render_time_ = base::TimeDelta(); |
1396 begin_time_ = base::TimeTicks::Now(); | 1409 begin_time_ = base::TimeTicks::Now(); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1439 } | 1452 } |
1440 | 1453 |
1441 void PrintWebViewHelper::PrintPreviewContext::Failed() { | 1454 void PrintWebViewHelper::PrintPreviewContext::Failed() { |
1442 DCHECK(state_ == INITIALIZED || state_ == RENDERING); | 1455 DCHECK(state_ == INITIALIZED || state_ == RENDERING); |
1443 state_ = INITIALIZED; | 1456 state_ = INITIALIZED; |
1444 ClearContext(); | 1457 ClearContext(); |
1445 } | 1458 } |
1446 | 1459 |
1447 int PrintWebViewHelper::PrintPreviewContext::GetNextPageNumber() { | 1460 int PrintWebViewHelper::PrintPreviewContext::GetNextPageNumber() { |
1448 DCHECK_EQ(RENDERING, state_); | 1461 DCHECK_EQ(RENDERING, state_); |
1449 if ((size_t)(current_page_index_) == pages_to_render_.size()) | 1462 if (IsFinalPageRendered()) |
1450 return -1; | 1463 return -1; |
1451 return pages_to_render_[current_page_index_++]; | 1464 return pages_to_render_[current_page_index_++]; |
1452 } | 1465 } |
1453 | 1466 |
1454 bool PrintWebViewHelper::PrintPreviewContext::IsReadyToRender() const { | 1467 bool PrintWebViewHelper::PrintPreviewContext::IsReadyToRender() const { |
1455 return state_ != UNINITIALIZED; | 1468 return state_ != UNINITIALIZED; |
1456 } | 1469 } |
1457 | 1470 |
1458 bool PrintWebViewHelper::PrintPreviewContext::IsModifiable() const { | 1471 bool PrintWebViewHelper::PrintPreviewContext::IsModifiable() const { |
1459 // TODO(vandebo) I think this should only return false if the content is a | 1472 // TODO(vandebo) I think this should only return false if the content is a |
1460 // PDF, just because we are printing a particular node does not mean it's | 1473 // PDF, just because we are printing a particular node does not mean it's |
1461 // a PDF (right?), we should check the mime type of the node. | 1474 // a PDF (right?), we should check the mime type of the node. |
1462 if (node()) | 1475 if (node()) |
1463 return false; | 1476 return false; |
1464 std::string mime(frame()->dataSource()->response().mimeType().utf8()); | 1477 std::string mime(frame()->dataSource()->response().mimeType().utf8()); |
1465 return mime != "application/pdf"; | 1478 return mime != "application/pdf"; |
1466 } | 1479 } |
1467 | 1480 |
1468 bool PrintWebViewHelper::PrintPreviewContext::IsLastPageOfPrintReadyMetafile() | 1481 bool PrintWebViewHelper::PrintPreviewContext::IsLastPageOfPrintReadyMetafile() |
1469 const { | 1482 const { |
1470 return current_page_index_ == print_ready_metafile_page_count_; | 1483 return current_page_index_ == print_ready_metafile_page_count_; |
1471 } | 1484 } |
1472 | 1485 |
| 1486 bool PrintWebViewHelper::PrintPreviewContext::IsFinalPageRendered() const { |
| 1487 return static_cast<size_t>(current_page_index_) == pages_to_render_.size(); |
| 1488 } |
| 1489 |
1473 void PrintWebViewHelper::PrintPreviewContext::set_generate_draft_pages( | 1490 void PrintWebViewHelper::PrintPreviewContext::set_generate_draft_pages( |
1474 bool generate_draft_pages) { | 1491 bool generate_draft_pages) { |
1475 generate_draft_pages_ = generate_draft_pages; | 1492 generate_draft_pages_ = generate_draft_pages; |
1476 } | 1493 } |
1477 | 1494 |
1478 WebKit::WebFrame* PrintWebViewHelper::PrintPreviewContext::frame() const { | 1495 WebKit::WebFrame* PrintWebViewHelper::PrintPreviewContext::frame() const { |
1479 return frame_; | 1496 return frame_; |
1480 } | 1497 } |
1481 | 1498 |
1482 WebKit::WebNode* PrintWebViewHelper::PrintPreviewContext::node() const { | 1499 WebKit::WebNode* PrintWebViewHelper::PrintPreviewContext::node() const { |
(...skipping 22 matching lines...) Expand all Loading... |
1505 const gfx::Size& | 1522 const gfx::Size& |
1506 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { | 1523 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { |
1507 return prep_frame_view_->GetPrintCanvasSize(); | 1524 return prep_frame_view_->GetPrintCanvasSize(); |
1508 } | 1525 } |
1509 | 1526 |
1510 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { | 1527 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
1511 prep_frame_view_.reset(); | 1528 prep_frame_view_.reset(); |
1512 metafile_.reset(); | 1529 metafile_.reset(); |
1513 pages_to_render_.clear(); | 1530 pages_to_render_.clear(); |
1514 } | 1531 } |
OLD | NEW |