| 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" | |
| 10 #include "base/logging.h" | 9 #include "base/logging.h" |
| 11 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| 12 #include "base/process_util.h" | |
| 13 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 14 #include "chrome/common/chrome_switches.h" | 12 #include "chrome/common/chrome_switches.h" |
| 15 #include "chrome/common/print_messages.h" | 13 #include "chrome/common/print_messages.h" |
| 16 #include "chrome/common/render_messages.h" | 14 #include "chrome/common/render_messages.h" |
| 17 #include "chrome/common/url_constants.h" | 15 #include "chrome/common/url_constants.h" |
| 18 #include "chrome/renderer/prerender/prerender_helper.h" | 16 #include "chrome/renderer/prerender/prerender_helper.h" |
| 19 #include "content/renderer/render_view.h" | 17 #include "content/renderer/render_view.h" |
| 20 #include "grit/generated_resources.h" | 18 #include "grit/generated_resources.h" |
| 21 #include "printing/metafile_impl.h" | 19 #include "printing/metafile_impl.h" |
| 22 #include "printing/print_job_constants.h" | 20 #include "printing/print_job_constants.h" |
| 23 #include "printing/units.h" | 21 #include "printing/units.h" |
| 24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" | 22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" |
| 25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h" | 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h" |
| 26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" | 25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" |
| 28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" | 27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" |
| 30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" | 28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" |
| 31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h" | 29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h" |
| 32 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 33 #include "ui/base/l10n/l10n_util.h" | 31 #include "ui/base/l10n/l10n_util.h" |
| 34 | 32 |
| 35 #if defined(OS_POSIX) | 33 #if defined(OS_POSIX) |
| 34 #include "base/process_util.h" |
| 36 #include "content/common/view_messages.h" | 35 #include "content/common/view_messages.h" |
| 37 #endif | 36 #endif |
| 38 | 37 |
| 39 #if defined(USE_SKIA) | 38 #if defined(USE_SKIA) |
| 40 #include "base/string_number_conversions.h" | 39 #include "base/string_number_conversions.h" |
| 41 #include "skia/ext/vector_canvas.h" | 40 #include "skia/ext/vector_canvas.h" |
| 42 #include "skia/ext/vector_platform_device_skia.h" | 41 #include "skia/ext/vector_platform_device_skia.h" |
| 43 #include "third_party/skia/include/core/SkTypeface.h" | 42 #include "third_party/skia/include/core/SkTypeface.h" |
| 44 #endif // defined(USE_SKIA) | 43 #endif // defined(USE_SKIA) |
| 45 | 44 |
| (...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 IPC_MESSAGE_HANDLER(PrintMsg_PrintPages, OnPrintPages) | 363 IPC_MESSAGE_HANDLER(PrintMsg_PrintPages, OnPrintPages) |
| 365 IPC_MESSAGE_HANDLER(PrintMsg_PrintForSystemDialog, OnPrintForSystemDialog) | 364 IPC_MESSAGE_HANDLER(PrintMsg_PrintForSystemDialog, OnPrintForSystemDialog) |
| 366 IPC_MESSAGE_HANDLER(PrintMsg_InitiatePrintPreview, OnInitiatePrintPreview) | 365 IPC_MESSAGE_HANDLER(PrintMsg_InitiatePrintPreview, OnInitiatePrintPreview) |
| 367 IPC_MESSAGE_HANDLER(PrintMsg_PrintNodeUnderContextMenu, | 366 IPC_MESSAGE_HANDLER(PrintMsg_PrintNodeUnderContextMenu, |
| 368 OnPrintNodeUnderContextMenu) | 367 OnPrintNodeUnderContextMenu) |
| 369 IPC_MESSAGE_HANDLER(PrintMsg_PrintPreview, OnPrintPreview) | 368 IPC_MESSAGE_HANDLER(PrintMsg_PrintPreview, OnPrintPreview) |
| 370 IPC_MESSAGE_HANDLER(PrintMsg_PrintForPrintPreview, OnPrintForPrintPreview) | 369 IPC_MESSAGE_HANDLER(PrintMsg_PrintForPrintPreview, OnPrintForPrintPreview) |
| 371 IPC_MESSAGE_HANDLER(PrintMsg_PrintingDone, OnPrintingDone) | 370 IPC_MESSAGE_HANDLER(PrintMsg_PrintingDone, OnPrintingDone) |
| 372 IPC_MESSAGE_HANDLER(PrintMsg_ResetScriptedPrintCount, | 371 IPC_MESSAGE_HANDLER(PrintMsg_ResetScriptedPrintCount, |
| 373 ResetScriptedPrintCount) | 372 ResetScriptedPrintCount) |
| 374 IPC_MESSAGE_HANDLER(PrintMsg_ContinuePreview, OnContinuePreview) | |
| 375 IPC_MESSAGE_HANDLER(PrintMsg_AbortPreview, OnAbortPreview) | |
| 376 IPC_MESSAGE_HANDLER(PrintMsg_PreviewPrintingRequestCancelled, | 373 IPC_MESSAGE_HANDLER(PrintMsg_PreviewPrintingRequestCancelled, |
| 377 DisplayPrintJobError) | 374 DisplayPrintJobError) |
| 378 IPC_MESSAGE_UNHANDLED(handled = false) | 375 IPC_MESSAGE_UNHANDLED(handled = false) |
| 379 IPC_END_MESSAGE_MAP() | 376 IPC_END_MESSAGE_MAP() |
| 380 return handled; | 377 return handled; |
| 381 } | 378 } |
| 382 | 379 |
| 383 void PrintWebViewHelper::OnPrintForPrintPreview( | 380 void PrintWebViewHelper::OnPrintForPrintPreview( |
| 384 const DictionaryValue& job_settings) { | 381 const DictionaryValue& job_settings) { |
| 385 DCHECK(is_preview_); | 382 DCHECK(is_preview_); |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 488 print_pages_params_->params.preview_request_id; | 485 print_pages_params_->params.preview_request_id; |
| 489 | 486 |
| 490 Send(new PrintHostMsg_PagesReadyForPreview(routing_id(), preview_params)); | 487 Send(new PrintHostMsg_PagesReadyForPreview(routing_id(), preview_params)); |
| 491 return; | 488 return; |
| 492 } | 489 } |
| 493 // Always clear |old_print_pages_params_| before rendering the pages. | 490 // Always clear |old_print_pages_params_| before rendering the pages. |
| 494 old_print_pages_params_.reset(); | 491 old_print_pages_params_.reset(); |
| 495 | 492 |
| 496 // PDF printer device supports alpha blending. | 493 // PDF printer device supports alpha blending. |
| 497 print_pages_params_->params.supports_alpha_blend = true; | 494 print_pages_params_->params.supports_alpha_blend = true; |
| 498 if (!CreatePreviewDocument()) | 495 if (CreatePreviewDocument()) |
| 496 DidFinishPrinting(OK); |
| 497 else |
| 499 DidFinishPrinting(FAIL_PREVIEW); | 498 DidFinishPrinting(FAIL_PREVIEW); |
| 500 } | 499 } |
| 501 | 500 |
| 502 bool PrintWebViewHelper::CreatePreviewDocument() { | 501 bool PrintWebViewHelper::CreatePreviewDocument() { |
| 503 PrintMsg_Print_Params print_params = print_pages_params_->params; | 502 PrintMsg_Print_Params print_params = print_pages_params_->params; |
| 504 const std::vector<int>& pages = print_pages_params_->pages; | 503 const std::vector<int>& pages = print_pages_params_->pages; |
| 505 if (!print_preview_context_.CreatePreviewDocument(&print_params, pages)) | 504 if (!print_preview_context_.CreatePreviewDocument(&print_params, pages)) |
| 506 return false; | 505 return false; |
| 507 PrintHostMsg_DidGetPreviewPageCount_Params params; | 506 PrintHostMsg_DidGetPreviewPageCount_Params params; |
| 508 params.page_count = print_preview_context_.total_page_count(); | 507 params.page_count = print_preview_context_.total_page_count(); |
| 509 params.is_modifiable = print_preview_context_.IsModifiable(); | 508 params.is_modifiable = print_preview_context_.IsModifiable(); |
| 510 params.document_cookie = print_pages_params_->params.document_cookie; | 509 params.document_cookie = print_pages_params_->params.document_cookie; |
| 511 params.preview_request_id = print_pages_params_->params.preview_request_id; | 510 params.preview_request_id = print_pages_params_->params.preview_request_id; |
| 512 Send(new PrintHostMsg_DidGetPreviewPageCount(routing_id(), params)); | 511 Send(new PrintHostMsg_DidGetPreviewPageCount(routing_id(), params)); |
| 513 PreviewPageRendered(printing::INVALID_PAGE_INDEX, NULL); | 512 if (CheckForCancel()) |
| 514 return true; | 513 return false; |
| 515 } | |
| 516 | 514 |
| 517 void PrintWebViewHelper::OnContinuePreview(int requested_preview_page_index) { | |
| 518 // Spurious message. We already finished/cancelled/aborted the print preview. | |
| 519 if (!print_preview_context_.IsBusy()) | |
| 520 return; | |
| 521 int page_number; | 515 int page_number; |
| 522 #if defined(USE_SKIA) | 516 while ((page_number = print_preview_context_.GetNextPageNumber()) >= 0) { |
| 523 if (requested_preview_page_index >= printing::FIRST_PAGE_INDEX) { | 517 if (!RenderPreviewPage(page_number)) |
| 524 page_number = requested_preview_page_index; | 518 return false; |
| 525 } else | 519 if (CheckForCancel()) |
| 526 #endif | 520 return false; |
| 527 { | 521 }; |
| 528 page_number = print_preview_context_.GetNextPageNumber(); | |
| 529 } | |
| 530 | |
| 531 if (page_number >= printing::FIRST_PAGE_INDEX) { | |
| 532 // Continue generating the print preview. | |
| 533 RenderPreviewPage(page_number); | |
| 534 return; | |
| 535 } | |
| 536 | 522 |
| 537 // Finished generating preview. Finalize the document. | 523 // Finished generating preview. Finalize the document. |
| 538 if (FinalizePreviewDocument()) { | 524 if (!FinalizePreviewDocument()) |
| 539 print_preview_context_.Finished(); | 525 return false; |
| 540 DidFinishPrinting(OK); | 526 print_preview_context_.Finished(); |
| 541 } else { | 527 return true; |
| 542 DidFinishPrinting(FAIL_PREVIEW); | |
| 543 } | |
| 544 } | |
| 545 | |
| 546 void PrintWebViewHelper::OnAbortPreview() { | |
| 547 DidFinishPrinting(ABORT_PREVIEW); | |
| 548 return; | |
| 549 } | 528 } |
| 550 | 529 |
| 551 bool PrintWebViewHelper::FinalizePreviewDocument() { | 530 bool PrintWebViewHelper::FinalizePreviewDocument() { |
| 552 print_preview_context_.FinalizePreviewDocument(); | 531 print_preview_context_.FinalizePreviewDocument(); |
| 553 | 532 |
| 554 // Get the size of the resulting metafile. | 533 // Get the size of the resulting metafile. |
| 555 printing::PreviewMetafile* metafile = print_preview_context_.metafile(); | 534 printing::PreviewMetafile* metafile = print_preview_context_.metafile(); |
| 556 uint32 buf_size = metafile->GetDataSize(); | 535 uint32 buf_size = metafile->GetDataSize(); |
| 557 DCHECK_GT(buf_size, 0u); | 536 DCHECK_GT(buf_size, 0u); |
| 558 | 537 |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 653 DisplayPrintJobError(); | 632 DisplayPrintJobError(); |
| 654 | 633 |
| 655 if (notify_browser_of_print_failure_) { | 634 if (notify_browser_of_print_failure_) { |
| 656 int cookie = print_pages_params_->params.document_cookie; | 635 int cookie = print_pages_params_->params.document_cookie; |
| 657 Send(new PrintHostMsg_PrintingFailed(routing_id(), cookie)); | 636 Send(new PrintHostMsg_PrintingFailed(routing_id(), cookie)); |
| 658 } | 637 } |
| 659 } else if (result == FAIL_PREVIEW) { | 638 } else if (result == FAIL_PREVIEW) { |
| 660 DCHECK(is_preview_); | 639 DCHECK(is_preview_); |
| 661 store_print_pages_params = false; | 640 store_print_pages_params = false; |
| 662 int cookie = print_pages_params_->params.document_cookie; | 641 int cookie = print_pages_params_->params.document_cookie; |
| 663 Send(new PrintHostMsg_PrintPreviewFailed(routing_id(), cookie)); | 642 if (notify_browser_of_print_failure_) |
| 643 Send(new PrintHostMsg_PrintPreviewFailed(routing_id(), cookie)); |
| 644 else |
| 645 Send(new PrintHostMsg_PrintPreviewCancelled(routing_id(), cookie)); |
| 664 print_preview_context_.Failed(); | 646 print_preview_context_.Failed(); |
| 665 } else if (result == ABORT_PREVIEW) { | |
| 666 DCHECK(is_preview_); | |
| 667 store_print_pages_params = false; | |
| 668 print_preview_context_.Abort(); | |
| 669 } | 647 } |
| 670 | 648 |
| 671 if (print_web_view_) { | 649 if (print_web_view_) { |
| 672 print_web_view_->close(); | 650 print_web_view_->close(); |
| 673 print_web_view_ = NULL; | 651 print_web_view_ = NULL; |
| 674 } | 652 } |
| 675 | 653 |
| 676 if (store_print_pages_params) { | 654 if (store_print_pages_params) { |
| 677 old_print_pages_params_.reset(print_pages_params_.release()); | 655 old_print_pages_params_.reset(print_pages_params_.release()); |
| 678 } else { | 656 } else { |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 896 DCHECK(!prepare->get()); | 874 DCHECK(!prepare->get()); |
| 897 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, | 875 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, |
| 898 frame, node)); | 876 frame, node)); |
| 899 UpdatePrintableSizeInPrintParameters(frame, node, prepare->get(), | 877 UpdatePrintableSizeInPrintParameters(frame, node, prepare->get(), |
| 900 &print_pages_params_->params); | 878 &print_pages_params_->params); |
| 901 Send(new PrintHostMsg_DidGetDocumentCookie( | 879 Send(new PrintHostMsg_DidGetDocumentCookie( |
| 902 routing_id(), print_pages_params_->params.document_cookie)); | 880 routing_id(), print_pages_params_->params.document_cookie)); |
| 903 return true; | 881 return true; |
| 904 } | 882 } |
| 905 | 883 |
| 906 bool PrintWebViewHelper::UpdatePrintSettingsRequestId( | |
| 907 const DictionaryValue& job_settings, | |
| 908 PrintMsg_Print_Params* params) { | |
| 909 if (!job_settings.GetInteger(printing::kPreviewRequestID, | |
| 910 &(params->preview_request_id))) { | |
| 911 NOTREACHED(); | |
| 912 return false; | |
| 913 } | |
| 914 return true; | |
| 915 } | |
| 916 | |
| 917 bool PrintWebViewHelper::UpdatePrintSettings( | 884 bool PrintWebViewHelper::UpdatePrintSettings( |
| 918 const DictionaryValue& job_settings) { | 885 const DictionaryValue& job_settings) { |
| 919 PrintMsg_PrintPages_Params settings; | 886 PrintMsg_PrintPages_Params settings; |
| 920 | 887 |
| 921 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), | 888 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), |
| 922 print_pages_params_->params.document_cookie, job_settings, &settings)); | 889 print_pages_params_->params.document_cookie, job_settings, &settings)); |
| 923 | 890 |
| 924 if (settings.params.dpi < kMinDpi || !settings.params.document_cookie) | 891 if (settings.params.dpi < kMinDpi || !settings.params.document_cookie) |
| 925 return false; | 892 return false; |
| 926 | 893 |
| 927 if (!UpdatePrintSettingsRequestId(job_settings, &(settings.params))) | 894 if (!job_settings.GetInteger(printing::kPreviewUIId, |
| 928 return false; | 895 &(settings.params.preview_ui_id)) || |
| 929 | 896 !job_settings.GetInteger(printing::kPreviewRequestID, |
| 930 if (!job_settings.GetBoolean(printing::kIsFirstRequest, | 897 &(settings.params.preview_request_id)) || |
| 898 !job_settings.GetBoolean(printing::kIsFirstRequest, |
| 931 &(settings.params.is_first_request))) { | 899 &(settings.params.is_first_request))) { |
| 932 NOTREACHED(); | 900 NOTREACHED(); |
| 901 return false; |
| 933 } | 902 } |
| 934 | 903 |
| 935 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); | 904 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); |
| 936 | 905 |
| 937 if (print_pages_params_->params.display_header_footer) { | 906 if (print_pages_params_->params.display_header_footer) { |
| 938 header_footer_info_.reset(new DictionaryValue()); | 907 header_footer_info_.reset(new DictionaryValue()); |
| 939 header_footer_info_->SetString(printing::kSettingHeaderFooterDate, | 908 header_footer_info_->SetString(printing::kSettingHeaderFooterDate, |
| 940 print_pages_params_->params.date); | 909 print_pages_params_->params.date); |
| 941 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, | 910 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, |
| 942 print_pages_params_->params.url); | 911 print_pages_params_->params.url); |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1032 min_wait_seconds = std::min( | 1001 min_wait_seconds = std::min( |
| 1033 kMinSecondsToIgnoreJavascriptInitiatedPrint << | 1002 kMinSecondsToIgnoreJavascriptInitiatedPrint << |
| 1034 (user_cancelled_scripted_print_count_ - 3), | 1003 (user_cancelled_scripted_print_count_ - 3), |
| 1035 kMaxSecondsToIgnoreJavascriptInitiatedPrint); | 1004 kMaxSecondsToIgnoreJavascriptInitiatedPrint); |
| 1036 } | 1005 } |
| 1037 if (diff.InSeconds() < min_wait_seconds) { | 1006 if (diff.InSeconds() < min_wait_seconds) { |
| 1038 too_frequent = true; | 1007 too_frequent = true; |
| 1039 } | 1008 } |
| 1040 } | 1009 } |
| 1041 | 1010 |
| 1042 if (!too_frequent && print_preview_context_.IsBusy()) | |
| 1043 too_frequent = true; | |
| 1044 | |
| 1045 if (!too_frequent) | 1011 if (!too_frequent) |
| 1046 return false; | 1012 return false; |
| 1047 | 1013 |
| 1048 WebString message(WebString::fromUTF8( | 1014 WebString message(WebString::fromUTF8( |
| 1049 "Ignoring too frequent calls to print().")); | 1015 "Ignoring too frequent calls to print().")); |
| 1050 frame->addMessageToConsole(WebConsoleMessage(WebConsoleMessage::LevelWarning, | 1016 frame->addMessageToConsole(WebConsoleMessage(WebConsoleMessage::LevelWarning, |
| 1051 message)); | 1017 message)); |
| 1052 return true; | 1018 return true; |
| 1053 } | 1019 } |
| 1054 | 1020 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 1070 render_view()->runModalAlertDialog( | 1036 render_view()->runModalAlertDialog( |
| 1071 web_view->mainFrame(), | 1037 web_view->mainFrame(), |
| 1072 l10n_util::GetStringUTF16(IDS_PRINT_SPOOL_FAILED_ERROR_TEXT)); | 1038 l10n_util::GetStringUTF16(IDS_PRINT_SPOOL_FAILED_ERROR_TEXT)); |
| 1073 } | 1039 } |
| 1074 | 1040 |
| 1075 void PrintWebViewHelper::RequestPrintPreview() { | 1041 void PrintWebViewHelper::RequestPrintPreview() { |
| 1076 old_print_pages_params_.reset(); | 1042 old_print_pages_params_.reset(); |
| 1077 Send(new PrintHostMsg_RequestPrintPreview(routing_id())); | 1043 Send(new PrintHostMsg_RequestPrintPreview(routing_id())); |
| 1078 } | 1044 } |
| 1079 | 1045 |
| 1080 void PrintWebViewHelper::PreviewPageRendered(int page_number, | 1046 bool PrintWebViewHelper::CheckForCancel() { |
| 1047 bool cancel = false; |
| 1048 Send(new PrintHostMsg_CheckForCancel( |
| 1049 routing_id(), |
| 1050 print_pages_params_->params.preview_ui_id, |
| 1051 print_pages_params_->params.preview_request_id, |
| 1052 &cancel)); |
| 1053 if (cancel) { |
| 1054 notify_browser_of_print_failure_ = false; |
| 1055 return true; |
| 1056 } |
| 1057 return false; |
| 1058 } |
| 1059 |
| 1060 bool PrintWebViewHelper::PreviewPageRendered(int page_number, |
| 1081 printing::Metafile* metafile) { | 1061 printing::Metafile* metafile) { |
| 1082 if ((page_number == printing::INVALID_PAGE_INDEX && metafile) || | 1062 if (page_number < printing::FIRST_PAGE_INDEX) { |
| 1083 (page_number >= printing::FIRST_PAGE_INDEX && !metafile && | |
| 1084 print_preview_context_.IsModifiable())) { | |
| 1085 NOTREACHED(); | 1063 NOTREACHED(); |
| 1086 DidFinishPrinting(FAIL_PREVIEW); | 1064 return false; |
| 1087 return; | 1065 } |
| 1066 |
| 1067 // For non-modifiable files, |metafile| should be NULL, so do not bother |
| 1068 // sending a message. |
| 1069 if (!print_preview_context_.IsModifiable()) { |
| 1070 DCHECK(!metafile); |
| 1071 return true; |
| 1072 } |
| 1073 |
| 1074 if (!metafile) { |
| 1075 NOTREACHED(); |
| 1076 return false; |
| 1088 } | 1077 } |
| 1089 | 1078 |
| 1090 uint32 buf_size = 0; | 1079 uint32 buf_size = 0; |
| 1091 PrintHostMsg_DidPreviewPage_Params preview_page_params; | 1080 PrintHostMsg_DidPreviewPage_Params preview_page_params; |
| 1092 // Get the size of the resulting metafile. | 1081 // Get the size of the resulting metafile. |
| 1093 if (metafile) { | 1082 if (metafile) { |
| 1094 buf_size = metafile->GetDataSize(); | 1083 buf_size = metafile->GetDataSize(); |
| 1095 DCHECK_GT(buf_size, 0u); | 1084 DCHECK_GT(buf_size, 0u); |
| 1096 if (!CopyMetafileDataToSharedMem( | 1085 if (!CopyMetafileDataToSharedMem( |
| 1097 metafile, &(preview_page_params.metafile_data_handle))) { | 1086 metafile, &(preview_page_params.metafile_data_handle))) { |
| 1098 DidFinishPrinting(FAIL_PREVIEW); | 1087 return false; |
| 1099 return; | |
| 1100 } | 1088 } |
| 1101 } | 1089 } |
| 1102 preview_page_params.data_size = buf_size; | 1090 preview_page_params.data_size = buf_size; |
| 1103 preview_page_params.page_number = page_number; | 1091 preview_page_params.page_number = page_number; |
| 1104 preview_page_params.preview_request_id = | 1092 preview_page_params.preview_request_id = |
| 1105 print_pages_params_->params.preview_request_id; | 1093 print_pages_params_->params.preview_request_id; |
| 1094 |
| 1106 Send(new PrintHostMsg_DidPreviewPage(routing_id(), preview_page_params)); | 1095 Send(new PrintHostMsg_DidPreviewPage(routing_id(), preview_page_params)); |
| 1096 return true; |
| 1107 } | 1097 } |
| 1108 | 1098 |
| 1109 PrintWebViewHelper::PrintPreviewContext::PrintPreviewContext() | 1099 PrintWebViewHelper::PrintPreviewContext::PrintPreviewContext() |
| 1110 : frame_(NULL), | 1100 : frame_(NULL), |
| 1111 total_page_count_(0), | 1101 total_page_count_(0), |
| 1112 actual_page_count_(0), | 1102 actual_page_count_(0), |
| 1113 current_page_number_(0), | 1103 current_page_index_(0), |
| 1114 state_(UNINITIALIZED) { | 1104 state_(UNINITIALIZED) { |
| 1115 } | 1105 } |
| 1116 | 1106 |
| 1117 PrintWebViewHelper::PrintPreviewContext::~PrintPreviewContext() { | 1107 PrintWebViewHelper::PrintPreviewContext::~PrintPreviewContext() { |
| 1118 } | 1108 } |
| 1119 | 1109 |
| 1120 void PrintWebViewHelper::PrintPreviewContext::InitWithFrame( | 1110 void PrintWebViewHelper::PrintPreviewContext::InitWithFrame( |
| 1121 WebKit::WebFrame* web_frame) { | 1111 WebKit::WebFrame* web_frame) { |
| 1122 DCHECK(web_frame); | 1112 DCHECK(web_frame); |
| 1123 if (IsReadyToRender()) | |
| 1124 return; | |
| 1125 state_ = INITIALIZED; | 1113 state_ = INITIALIZED; |
| 1126 frame_ = web_frame; | 1114 frame_ = web_frame; |
| 1127 node_.reset(); | 1115 node_.reset(); |
| 1128 } | 1116 } |
| 1129 | 1117 |
| 1130 void PrintWebViewHelper::PrintPreviewContext::InitWithNode( | 1118 void PrintWebViewHelper::PrintPreviewContext::InitWithNode( |
| 1131 const WebKit::WebNode& web_node) { | 1119 const WebKit::WebNode& web_node) { |
| 1132 DCHECK(!web_node.isNull()); | 1120 DCHECK(!web_node.isNull()); |
| 1133 if (IsReadyToRender()) | |
| 1134 return; | |
| 1135 state_ = INITIALIZED; | 1121 state_ = INITIALIZED; |
| 1136 frame_ = web_node.document().frame(); | 1122 frame_ = web_node.document().frame(); |
| 1137 node_.reset(new WebNode(web_node)); | 1123 node_.reset(new WebNode(web_node)); |
| 1138 } | 1124 } |
| 1139 | 1125 |
| 1140 void PrintWebViewHelper::PrintPreviewContext::OnPrintPreview() { | 1126 void PrintWebViewHelper::PrintPreviewContext::OnPrintPreview() { |
| 1141 DCHECK(IsReadyToRender()); | 1127 DCHECK(IsReadyToRender()); |
| 1142 ClearContext(); | 1128 ClearContext(); |
| 1143 } | 1129 } |
| 1144 | 1130 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 1157 // Need to make sure old object gets destroyed first. | 1143 // Need to make sure old object gets destroyed first. |
| 1158 prep_frame_view_.reset(new PrepareFrameAndViewForPrint(*print_params, frame(), | 1144 prep_frame_view_.reset(new PrepareFrameAndViewForPrint(*print_params, frame(), |
| 1159 node())); | 1145 node())); |
| 1160 UpdatePrintableSizeInPrintParameters(frame_, node_.get(), | 1146 UpdatePrintableSizeInPrintParameters(frame_, node_.get(), |
| 1161 prep_frame_view_.get(), print_params); | 1147 prep_frame_view_.get(), print_params); |
| 1162 | 1148 |
| 1163 total_page_count_ = prep_frame_view_->GetExpectedPageCount(); | 1149 total_page_count_ = prep_frame_view_->GetExpectedPageCount(); |
| 1164 if (total_page_count_ == 0) | 1150 if (total_page_count_ == 0) |
| 1165 return false; | 1151 return false; |
| 1166 | 1152 |
| 1167 current_page_number_ = 0; | 1153 current_page_index_ = 0; |
| 1168 if (pages.empty()) { | 1154 if (pages.empty()) { |
| 1169 actual_page_count_ = total_page_count_; | 1155 actual_page_count_ = total_page_count_; |
| 1170 rendered_pages_ = std::vector<PreviewPageInfo>(total_page_count_, | 1156 for (int i = 0; i < actual_page_count_; ++i) |
| 1171 std::make_pair(false, -1)); | 1157 pages_to_render_.push_back(i); |
| 1172 } else { | 1158 } else { |
| 1173 actual_page_count_ = pages.size(); | 1159 actual_page_count_ = pages.size(); |
| 1174 rendered_pages_ = std::vector<PreviewPageInfo>(total_page_count_, | |
| 1175 std::make_pair(true, -1)); | |
| 1176 for (int i = 0; i < actual_page_count_; ++i) { | 1160 for (int i = 0; i < actual_page_count_; ++i) { |
| 1177 int page_number = pages[i]; | 1161 int page_number = pages[i]; |
| 1178 if (page_number < printing::FIRST_PAGE_INDEX || | 1162 if (page_number < printing::FIRST_PAGE_INDEX || |
| 1179 page_number >= total_page_count_) { | 1163 page_number >= total_page_count_) { |
| 1180 return false; | 1164 return false; |
| 1181 } | 1165 } |
| 1182 rendered_pages_[page_number].first = false; | 1166 pages_to_render_.push_back(page_number); |
| 1183 rendered_pages_[page_number].second = i; | |
| 1184 } | 1167 } |
| 1185 } | 1168 } |
| 1186 | 1169 |
| 1187 document_render_time_ = base::TimeDelta(); | 1170 document_render_time_ = base::TimeDelta(); |
| 1188 begin_time_ = base::TimeTicks::Now(); | 1171 begin_time_ = base::TimeTicks::Now(); |
| 1189 | 1172 |
| 1190 return true; | 1173 return true; |
| 1191 } | 1174 } |
| 1192 | 1175 |
| 1193 void PrintWebViewHelper::PrintPreviewContext::RenderedPreviewPage( | 1176 void PrintWebViewHelper::PrintPreviewContext::RenderedPreviewPage( |
| (...skipping 26 matching lines...) Expand all Loading... |
| 1220 UMA_HISTOGRAM_MEDIUM_TIMES("PrintPreview.RenderAndGeneratePDFTimeAvgPerPage", | 1203 UMA_HISTOGRAM_MEDIUM_TIMES("PrintPreview.RenderAndGeneratePDFTimeAvgPerPage", |
| 1221 total_time / actual_page_count_); | 1204 total_time / actual_page_count_); |
| 1222 } | 1205 } |
| 1223 | 1206 |
| 1224 void PrintWebViewHelper::PrintPreviewContext::Finished() { | 1207 void PrintWebViewHelper::PrintPreviewContext::Finished() { |
| 1225 DCHECK_EQ(DONE, state_); | 1208 DCHECK_EQ(DONE, state_); |
| 1226 ClearContext(); | 1209 ClearContext(); |
| 1227 } | 1210 } |
| 1228 | 1211 |
| 1229 void PrintWebViewHelper::PrintPreviewContext::Failed() { | 1212 void PrintWebViewHelper::PrintPreviewContext::Failed() { |
| 1230 DCHECK(IsBusy()); | 1213 DCHECK(state_ == INITIALIZED || state_ == RENDERING); |
| 1231 state_ = INITIALIZED; | 1214 state_ = INITIALIZED; |
| 1232 ClearContext(); | 1215 ClearContext(); |
| 1233 } | 1216 } |
| 1234 | 1217 |
| 1235 void PrintWebViewHelper::PrintPreviewContext::Abort() { | |
| 1236 state_ = UNINITIALIZED; | |
| 1237 ClearContext(); | |
| 1238 frame_ = NULL; | |
| 1239 node_.reset(); | |
| 1240 } | |
| 1241 | |
| 1242 int PrintWebViewHelper::PrintPreviewContext::GetNextPageNumber() { | 1218 int PrintWebViewHelper::PrintPreviewContext::GetNextPageNumber() { |
| 1243 DCHECK_EQ(RENDERING, state_); | 1219 DCHECK_EQ(RENDERING, state_); |
| 1244 for (int i = 0; i < total_page_count_; i++) { | 1220 if (current_page_index_ >= actual_page_count_) |
| 1245 if (!rendered_pages_[current_page_number_].first) | 1221 return -1; |
| 1246 break; | 1222 return pages_to_render_[current_page_index_++]; |
| 1247 current_page_number_ = (current_page_number_ + 1) % total_page_count_; | |
| 1248 } | |
| 1249 if (rendered_pages_[current_page_number_].first) | |
| 1250 return printing::INVALID_PAGE_INDEX; | |
| 1251 rendered_pages_[current_page_number_].first = true; | |
| 1252 return current_page_number_; | |
| 1253 } | 1223 } |
| 1254 | 1224 |
| 1255 bool PrintWebViewHelper::PrintPreviewContext::IsReadyToRender() const { | 1225 bool PrintWebViewHelper::PrintPreviewContext::IsReadyToRender() const { |
| 1256 return state_ != UNINITIALIZED; | 1226 return state_ != UNINITIALIZED; |
| 1257 } | 1227 } |
| 1258 | 1228 |
| 1259 bool PrintWebViewHelper::PrintPreviewContext::IsBusy() const { | |
| 1260 return state_ == INITIALIZED || state_ == RENDERING; | |
| 1261 } | |
| 1262 | |
| 1263 bool PrintWebViewHelper::PrintPreviewContext::IsModifiable() const { | 1229 bool PrintWebViewHelper::PrintPreviewContext::IsModifiable() const { |
| 1264 // TODO(vandebo) I think this should only return false if the content is a | 1230 // TODO(vandebo) I think this should only return false if the content is a |
| 1265 // PDF, just because we are printing a particular node does not mean it's | 1231 // PDF, just because we are printing a particular node does not mean it's |
| 1266 // a PDF (right?), we should check the mime type of the node. | 1232 // a PDF (right?), we should check the mime type of the node. |
| 1267 if (node()) | 1233 if (node()) |
| 1268 return false; | 1234 return false; |
| 1269 std::string mime(frame()->dataSource()->response().mimeType().utf8()); | 1235 std::string mime(frame()->dataSource()->response().mimeType().utf8()); |
| 1270 return mime != "application/pdf"; | 1236 return mime != "application/pdf"; |
| 1271 } | 1237 } |
| 1272 | 1238 |
| 1273 WebKit::WebFrame* PrintWebViewHelper::PrintPreviewContext::frame() const { | 1239 WebKit::WebFrame* PrintWebViewHelper::PrintPreviewContext::frame() const { |
| 1274 return frame_; | 1240 return frame_; |
| 1275 } | 1241 } |
| 1276 | 1242 |
| 1277 WebKit::WebNode* PrintWebViewHelper::PrintPreviewContext::node() const { | 1243 WebKit::WebNode* PrintWebViewHelper::PrintPreviewContext::node() const { |
| 1278 return node_.get(); | 1244 return node_.get(); |
| 1279 } | 1245 } |
| 1280 | 1246 |
| 1281 int PrintWebViewHelper::PrintPreviewContext::total_page_count() const { | 1247 int PrintWebViewHelper::PrintPreviewContext::total_page_count() const { |
| 1248 DCHECK(IsReadyToRender()); |
| 1282 return total_page_count_; | 1249 return total_page_count_; |
| 1283 } | 1250 } |
| 1284 | 1251 |
| 1285 printing::PreviewMetafile* | 1252 printing::PreviewMetafile* |
| 1286 PrintWebViewHelper::PrintPreviewContext::metafile() const { | 1253 PrintWebViewHelper::PrintPreviewContext::metafile() const { |
| 1287 return metafile_.get(); | 1254 return metafile_.get(); |
| 1288 } | 1255 } |
| 1289 | 1256 |
| 1290 const PrintMsg_Print_Params& | 1257 const PrintMsg_Print_Params& |
| 1291 PrintWebViewHelper::PrintPreviewContext::print_params() const { | 1258 PrintWebViewHelper::PrintPreviewContext::print_params() const { |
| 1292 return *print_params_; | 1259 return *print_params_; |
| 1293 } | 1260 } |
| 1294 | 1261 |
| 1295 const gfx::Size& | 1262 const gfx::Size& |
| 1296 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { | 1263 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { |
| 1297 return prep_frame_view_->GetPrintCanvasSize(); | 1264 return prep_frame_view_->GetPrintCanvasSize(); |
| 1298 } | 1265 } |
| 1299 | 1266 |
| 1300 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { | 1267 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
| 1301 prep_frame_view_.reset(); | 1268 prep_frame_view_.reset(); |
| 1302 metafile_.reset(); | 1269 metafile_.reset(); |
| 1303 rendered_pages_.clear(); | 1270 pages_to_render_.clear(); |
| 1304 } | 1271 } |
| OLD | NEW |