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()) { |
kmadhusu
2011/08/19 19:51:03
nit: Remove {}
Lei Zhang
2011/08/19 22:50:28
Done.
| |
496 DidFinishPrinting(OK); | |
497 } else { | |
499 DidFinishPrinting(FAIL_PREVIEW); | 498 DidFinishPrinting(FAIL_PREVIEW); |
499 } | |
500 } | 500 } |
501 | 501 |
502 bool PrintWebViewHelper::CreatePreviewDocument() { | 502 bool PrintWebViewHelper::CreatePreviewDocument() { |
503 PrintMsg_Print_Params print_params = print_pages_params_->params; | 503 PrintMsg_Print_Params print_params = print_pages_params_->params; |
504 const std::vector<int>& pages = print_pages_params_->pages; | 504 const std::vector<int>& pages = print_pages_params_->pages; |
505 if (!print_preview_context_.CreatePreviewDocument(&print_params, pages)) | 505 if (!print_preview_context_.CreatePreviewDocument(&print_params, pages)) |
506 return false; | 506 return false; |
507 PrintHostMsg_DidGetPreviewPageCount_Params params; | 507 PrintHostMsg_DidGetPreviewPageCount_Params params; |
508 params.page_count = print_preview_context_.total_page_count(); | 508 params.page_count = print_preview_context_.total_page_count(); |
509 params.is_modifiable = print_preview_context_.IsModifiable(); | 509 params.is_modifiable = print_preview_context_.IsModifiable(); |
510 params.document_cookie = print_pages_params_->params.document_cookie; | 510 params.document_cookie = print_pages_params_->params.document_cookie; |
511 params.preview_request_id = print_pages_params_->params.preview_request_id; | 511 params.preview_request_id = print_pages_params_->params.preview_request_id; |
512 Send(new PrintHostMsg_DidGetPreviewPageCount(routing_id(), params)); | 512 Send(new PrintHostMsg_DidGetPreviewPageCount(routing_id(), params)); |
513 PreviewPageRendered(printing::INVALID_PAGE_INDEX, NULL); | 513 if (!PreviewPageRendered(printing::INVALID_PAGE_INDEX, NULL)) |
514 return true; | 514 return false; |
515 } | |
516 | 515 |
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; | 516 int page_number; |
522 #if defined(USE_SKIA) | 517 while ((page_number = print_preview_context_.GetNextPageNumber()) >= 0) { |
523 if (requested_preview_page_index >= printing::FIRST_PAGE_INDEX) { | 518 if (!RenderPreviewPage(page_number)) |
524 page_number = requested_preview_page_index; | 519 return false; |
525 } else | 520 }; |
526 #endif | |
527 { | |
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 | 521 |
537 // Finished generating preview. Finalize the document. | 522 // Finished generating preview. Finalize the document. |
538 if (FinalizePreviewDocument()) { | 523 if (!FinalizePreviewDocument()) |
539 print_preview_context_.Finished(); | 524 return false; |
540 DidFinishPrinting(OK); | 525 print_preview_context_.Finished(); |
541 } else { | 526 return true; |
542 DidFinishPrinting(FAIL_PREVIEW); | |
543 } | |
544 } | |
545 | |
546 void PrintWebViewHelper::OnAbortPreview() { | |
547 DidFinishPrinting(ABORT_PREVIEW); | |
548 return; | |
549 } | 527 } |
550 | 528 |
551 bool PrintWebViewHelper::FinalizePreviewDocument() { | 529 bool PrintWebViewHelper::FinalizePreviewDocument() { |
552 print_preview_context_.FinalizePreviewDocument(); | 530 print_preview_context_.FinalizePreviewDocument(); |
553 | 531 |
554 // Get the size of the resulting metafile. | 532 // Get the size of the resulting metafile. |
555 printing::PreviewMetafile* metafile = print_preview_context_.metafile(); | 533 printing::PreviewMetafile* metafile = print_preview_context_.metafile(); |
556 uint32 buf_size = metafile->GetDataSize(); | 534 uint32 buf_size = metafile->GetDataSize(); |
557 DCHECK_GT(buf_size, 0u); | 535 DCHECK_GT(buf_size, 0u); |
558 | 536 |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
653 DisplayPrintJobError(); | 631 DisplayPrintJobError(); |
654 | 632 |
655 if (notify_browser_of_print_failure_) { | 633 if (notify_browser_of_print_failure_) { |
656 int cookie = print_pages_params_->params.document_cookie; | 634 int cookie = print_pages_params_->params.document_cookie; |
657 Send(new PrintHostMsg_PrintingFailed(routing_id(), cookie)); | 635 Send(new PrintHostMsg_PrintingFailed(routing_id(), cookie)); |
658 } | 636 } |
659 } else if (result == FAIL_PREVIEW) { | 637 } else if (result == FAIL_PREVIEW) { |
660 DCHECK(is_preview_); | 638 DCHECK(is_preview_); |
661 store_print_pages_params = false; | 639 store_print_pages_params = false; |
662 int cookie = print_pages_params_->params.document_cookie; | 640 int cookie = print_pages_params_->params.document_cookie; |
663 Send(new PrintHostMsg_PrintPreviewFailed(routing_id(), cookie)); | 641 if (notify_browser_of_print_failure_) { |
kmadhusu
2011/08/19 19:51:03
nit: Remove {}
Lei Zhang
2011/08/19 22:50:28
Done.
| |
642 Send(new PrintHostMsg_PrintPreviewFailed(routing_id(), cookie)); | |
643 } else { | |
644 Send(new PrintHostMsg_PrintPreviewCancelled(routing_id(), cookie)); | |
645 } | |
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.GetString(printing::kPreviewUIAddr, |
kmadhusu
2011/08/19 19:51:03
Can you merge these 3 if conditions (line #894, 90
Lei Zhang
2011/08/19 22:50:28
Done.
| |
895 &(settings.params.preview_ui_addr))) { | |
896 NOTREACHED(); | |
928 return false; | 897 return false; |
898 } | |
899 | |
900 if (!job_settings.GetInteger(printing::kPreviewRequestID, | |
901 &(settings.params.preview_request_id))) { | |
902 NOTREACHED(); | |
903 return false; | |
904 } | |
929 | 905 |
930 if (!job_settings.GetBoolean(printing::kIsFirstRequest, | 906 if (!job_settings.GetBoolean(printing::kIsFirstRequest, |
931 &(settings.params.is_first_request))) { | 907 &(settings.params.is_first_request))) { |
932 NOTREACHED(); | 908 NOTREACHED(); |
909 return false; | |
933 } | 910 } |
934 | 911 |
935 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); | 912 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); |
936 | 913 |
937 if (print_pages_params_->params.display_header_footer) { | 914 if (print_pages_params_->params.display_header_footer) { |
938 header_footer_info_.reset(new DictionaryValue()); | 915 header_footer_info_.reset(new DictionaryValue()); |
939 header_footer_info_->SetString(printing::kSettingHeaderFooterDate, | 916 header_footer_info_->SetString(printing::kSettingHeaderFooterDate, |
940 print_pages_params_->params.date); | 917 print_pages_params_->params.date); |
941 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, | 918 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, |
942 print_pages_params_->params.url); | 919 print_pages_params_->params.url); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1032 min_wait_seconds = std::min( | 1009 min_wait_seconds = std::min( |
1033 kMinSecondsToIgnoreJavascriptInitiatedPrint << | 1010 kMinSecondsToIgnoreJavascriptInitiatedPrint << |
1034 (user_cancelled_scripted_print_count_ - 3), | 1011 (user_cancelled_scripted_print_count_ - 3), |
1035 kMaxSecondsToIgnoreJavascriptInitiatedPrint); | 1012 kMaxSecondsToIgnoreJavascriptInitiatedPrint); |
1036 } | 1013 } |
1037 if (diff.InSeconds() < min_wait_seconds) { | 1014 if (diff.InSeconds() < min_wait_seconds) { |
1038 too_frequent = true; | 1015 too_frequent = true; |
1039 } | 1016 } |
1040 } | 1017 } |
1041 | 1018 |
1042 if (!too_frequent && print_preview_context_.IsBusy()) | |
1043 too_frequent = true; | |
1044 | |
1045 if (!too_frequent) | 1019 if (!too_frequent) |
1046 return false; | 1020 return false; |
1047 | 1021 |
1048 WebString message(WebString::fromUTF8( | 1022 WebString message(WebString::fromUTF8( |
1049 "Ignoring too frequent calls to print().")); | 1023 "Ignoring too frequent calls to print().")); |
1050 frame->addMessageToConsole(WebConsoleMessage(WebConsoleMessage::LevelWarning, | 1024 frame->addMessageToConsole(WebConsoleMessage(WebConsoleMessage::LevelWarning, |
1051 message)); | 1025 message)); |
1052 return true; | 1026 return true; |
1053 } | 1027 } |
1054 | 1028 |
(...skipping 15 matching lines...) Expand all Loading... | |
1070 render_view()->runModalAlertDialog( | 1044 render_view()->runModalAlertDialog( |
1071 web_view->mainFrame(), | 1045 web_view->mainFrame(), |
1072 l10n_util::GetStringUTF16(IDS_PRINT_SPOOL_FAILED_ERROR_TEXT)); | 1046 l10n_util::GetStringUTF16(IDS_PRINT_SPOOL_FAILED_ERROR_TEXT)); |
1073 } | 1047 } |
1074 | 1048 |
1075 void PrintWebViewHelper::RequestPrintPreview() { | 1049 void PrintWebViewHelper::RequestPrintPreview() { |
1076 old_print_pages_params_.reset(); | 1050 old_print_pages_params_.reset(); |
1077 Send(new PrintHostMsg_RequestPrintPreview(routing_id())); | 1051 Send(new PrintHostMsg_RequestPrintPreview(routing_id())); |
1078 } | 1052 } |
1079 | 1053 |
1080 void PrintWebViewHelper::PreviewPageRendered(int page_number, | 1054 bool PrintWebViewHelper::PreviewPageRendered(int page_number, |
kmadhusu
2011/08/19 19:51:03
PreviewPageRendered() not only sends the rendered
Lei Zhang
2011/08/19 22:50:28
Done.
| |
1081 printing::Metafile* metafile) { | 1055 printing::Metafile* metafile) { |
1082 if ((page_number == printing::INVALID_PAGE_INDEX && metafile) || | 1056 if ((page_number == printing::INVALID_PAGE_INDEX && metafile) || |
1083 (page_number >= printing::FIRST_PAGE_INDEX && !metafile && | 1057 (page_number >= printing::FIRST_PAGE_INDEX && !metafile && |
1084 print_preview_context_.IsModifiable())) { | 1058 print_preview_context_.IsModifiable())) { |
1085 NOTREACHED(); | 1059 NOTREACHED(); |
1086 DidFinishPrinting(FAIL_PREVIEW); | 1060 return false; |
1087 return; | |
1088 } | 1061 } |
1089 | 1062 |
1063 // First check whether to cancel or not. | |
1064 bool cancel = false; | |
1065 Send(new PrintHostMsg_CheckForCancel( | |
1066 routing_id(), | |
1067 print_pages_params_->params.preview_ui_addr, | |
1068 print_pages_params_->params.preview_request_id, | |
1069 &cancel)); | |
1070 if (cancel) { | |
1071 notify_browser_of_print_failure_ = false; | |
1072 return false; | |
1073 } | |
1074 | |
1075 // Not cancelling, send the page data. | |
1090 uint32 buf_size = 0; | 1076 uint32 buf_size = 0; |
1091 PrintHostMsg_DidPreviewPage_Params preview_page_params; | 1077 PrintHostMsg_DidPreviewPage_Params preview_page_params; |
1092 // Get the size of the resulting metafile. | 1078 // Get the size of the resulting metafile. |
1093 if (metafile) { | 1079 if (metafile) { |
1094 buf_size = metafile->GetDataSize(); | 1080 buf_size = metafile->GetDataSize(); |
1095 DCHECK_GT(buf_size, 0u); | 1081 DCHECK_GT(buf_size, 0u); |
1096 if (!CopyMetafileDataToSharedMem( | 1082 if (!CopyMetafileDataToSharedMem( |
1097 metafile, &(preview_page_params.metafile_data_handle))) { | 1083 metafile, &(preview_page_params.metafile_data_handle))) { |
1098 DidFinishPrinting(FAIL_PREVIEW); | 1084 return false; |
1099 return; | |
1100 } | 1085 } |
1101 } | 1086 } |
1102 preview_page_params.data_size = buf_size; | 1087 preview_page_params.data_size = buf_size; |
1103 preview_page_params.page_number = page_number; | 1088 preview_page_params.page_number = page_number; |
1104 preview_page_params.preview_request_id = | 1089 preview_page_params.preview_request_id = |
1105 print_pages_params_->params.preview_request_id; | 1090 print_pages_params_->params.preview_request_id; |
1091 | |
1106 Send(new PrintHostMsg_DidPreviewPage(routing_id(), preview_page_params)); | 1092 Send(new PrintHostMsg_DidPreviewPage(routing_id(), preview_page_params)); |
1093 return true; | |
kmadhusu
2011/08/19 19:51:03
Previously, We were sending an empty draft metafil
Lei Zhang
2011/08/19 22:50:28
Done.
| |
1107 } | 1094 } |
1108 | 1095 |
1109 PrintWebViewHelper::PrintPreviewContext::PrintPreviewContext() | 1096 PrintWebViewHelper::PrintPreviewContext::PrintPreviewContext() |
1110 : frame_(NULL), | 1097 : frame_(NULL), |
1111 total_page_count_(0), | 1098 total_page_count_(0), |
1112 actual_page_count_(0), | 1099 actual_page_count_(0), |
1113 current_page_number_(0), | 1100 current_page_index_(0), |
1114 state_(UNINITIALIZED) { | 1101 state_(UNINITIALIZED) { |
1115 } | 1102 } |
1116 | 1103 |
1117 PrintWebViewHelper::PrintPreviewContext::~PrintPreviewContext() { | 1104 PrintWebViewHelper::PrintPreviewContext::~PrintPreviewContext() { |
1118 } | 1105 } |
1119 | 1106 |
1120 void PrintWebViewHelper::PrintPreviewContext::InitWithFrame( | 1107 void PrintWebViewHelper::PrintPreviewContext::InitWithFrame( |
1121 WebKit::WebFrame* web_frame) { | 1108 WebKit::WebFrame* web_frame) { |
1122 DCHECK(web_frame); | 1109 DCHECK(web_frame); |
1123 if (IsReadyToRender()) | |
1124 return; | |
1125 state_ = INITIALIZED; | 1110 state_ = INITIALIZED; |
1126 frame_ = web_frame; | 1111 frame_ = web_frame; |
1127 node_.reset(); | 1112 node_.reset(); |
1128 } | 1113 } |
1129 | 1114 |
1130 void PrintWebViewHelper::PrintPreviewContext::InitWithNode( | 1115 void PrintWebViewHelper::PrintPreviewContext::InitWithNode( |
1131 const WebKit::WebNode& web_node) { | 1116 const WebKit::WebNode& web_node) { |
1132 DCHECK(!web_node.isNull()); | 1117 DCHECK(!web_node.isNull()); |
1133 if (IsReadyToRender()) | |
1134 return; | |
1135 state_ = INITIALIZED; | 1118 state_ = INITIALIZED; |
1136 frame_ = web_node.document().frame(); | 1119 frame_ = web_node.document().frame(); |
1137 node_.reset(new WebNode(web_node)); | 1120 node_.reset(new WebNode(web_node)); |
1138 } | 1121 } |
1139 | 1122 |
1140 void PrintWebViewHelper::PrintPreviewContext::OnPrintPreview() { | 1123 void PrintWebViewHelper::PrintPreviewContext::OnPrintPreview() { |
1141 DCHECK(IsReadyToRender()); | 1124 DCHECK(IsReadyToRender()); |
1142 ClearContext(); | 1125 ClearContext(); |
1143 } | 1126 } |
1144 | 1127 |
(...skipping 12 matching lines...) Expand all Loading... | |
1157 // Need to make sure old object gets destroyed first. | 1140 // Need to make sure old object gets destroyed first. |
1158 prep_frame_view_.reset(new PrepareFrameAndViewForPrint(*print_params, frame(), | 1141 prep_frame_view_.reset(new PrepareFrameAndViewForPrint(*print_params, frame(), |
1159 node())); | 1142 node())); |
1160 UpdatePrintableSizeInPrintParameters(frame_, node_.get(), | 1143 UpdatePrintableSizeInPrintParameters(frame_, node_.get(), |
1161 prep_frame_view_.get(), print_params); | 1144 prep_frame_view_.get(), print_params); |
1162 | 1145 |
1163 total_page_count_ = prep_frame_view_->GetExpectedPageCount(); | 1146 total_page_count_ = prep_frame_view_->GetExpectedPageCount(); |
1164 if (total_page_count_ == 0) | 1147 if (total_page_count_ == 0) |
1165 return false; | 1148 return false; |
1166 | 1149 |
1167 current_page_number_ = 0; | 1150 current_page_index_ = 0; |
1168 if (pages.empty()) { | 1151 if (pages.empty()) { |
1169 actual_page_count_ = total_page_count_; | 1152 actual_page_count_ = total_page_count_; |
1170 rendered_pages_ = std::vector<PreviewPageInfo>(total_page_count_, | 1153 for (int i = 0; i < actual_page_count_; ++i) { |
kmadhusu
2011/08/19 19:51:03
nit: Remove {}
Lei Zhang
2011/08/19 22:50:28
Done.
| |
1171 std::make_pair(false, -1)); | 1154 pages_to_render_.push_back(i); |
1155 } | |
1172 } else { | 1156 } else { |
1173 actual_page_count_ = pages.size(); | 1157 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) { | 1158 for (int i = 0; i < actual_page_count_; ++i) { |
1177 int page_number = pages[i]; | 1159 int page_number = pages[i]; |
1178 if (page_number < printing::FIRST_PAGE_INDEX || | 1160 if (page_number < printing::FIRST_PAGE_INDEX || |
1179 page_number >= total_page_count_) { | 1161 page_number >= total_page_count_) { |
1180 return false; | 1162 return false; |
1181 } | 1163 } |
1182 rendered_pages_[page_number].first = false; | 1164 pages_to_render_.push_back(page_number); |
1183 rendered_pages_[page_number].second = i; | |
1184 } | 1165 } |
1185 } | 1166 } |
1186 | 1167 |
1187 document_render_time_ = base::TimeDelta(); | 1168 document_render_time_ = base::TimeDelta(); |
1188 begin_time_ = base::TimeTicks::Now(); | 1169 begin_time_ = base::TimeTicks::Now(); |
1189 | 1170 |
1190 return true; | 1171 return true; |
1191 } | 1172 } |
1192 | 1173 |
1193 void PrintWebViewHelper::PrintPreviewContext::RenderedPreviewPage( | 1174 void PrintWebViewHelper::PrintPreviewContext::RenderedPreviewPage( |
(...skipping 26 matching lines...) Expand all Loading... | |
1220 UMA_HISTOGRAM_MEDIUM_TIMES("PrintPreview.RenderAndGeneratePDFTimeAvgPerPage", | 1201 UMA_HISTOGRAM_MEDIUM_TIMES("PrintPreview.RenderAndGeneratePDFTimeAvgPerPage", |
1221 total_time / actual_page_count_); | 1202 total_time / actual_page_count_); |
1222 } | 1203 } |
1223 | 1204 |
1224 void PrintWebViewHelper::PrintPreviewContext::Finished() { | 1205 void PrintWebViewHelper::PrintPreviewContext::Finished() { |
1225 DCHECK_EQ(DONE, state_); | 1206 DCHECK_EQ(DONE, state_); |
1226 ClearContext(); | 1207 ClearContext(); |
1227 } | 1208 } |
1228 | 1209 |
1229 void PrintWebViewHelper::PrintPreviewContext::Failed() { | 1210 void PrintWebViewHelper::PrintPreviewContext::Failed() { |
1230 DCHECK(IsBusy()); | 1211 DCHECK(state_ == INITIALIZED || state_ == RENDERING); |
1231 state_ = INITIALIZED; | 1212 state_ = INITIALIZED; |
1232 ClearContext(); | 1213 ClearContext(); |
1233 } | 1214 } |
1234 | 1215 |
1235 void PrintWebViewHelper::PrintPreviewContext::Abort() { | |
1236 state_ = UNINITIALIZED; | |
1237 ClearContext(); | |
1238 frame_ = NULL; | |
1239 node_.reset(); | |
1240 } | |
1241 | |
1242 int PrintWebViewHelper::PrintPreviewContext::GetNextPageNumber() { | 1216 int PrintWebViewHelper::PrintPreviewContext::GetNextPageNumber() { |
1243 DCHECK_EQ(RENDERING, state_); | 1217 DCHECK_EQ(RENDERING, state_); |
1244 for (int i = 0; i < total_page_count_; i++) { | 1218 if (current_page_index_ >= actual_page_count_) |
1245 if (!rendered_pages_[current_page_number_].first) | 1219 return -1; |
1246 break; | 1220 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 } | 1221 } |
1254 | 1222 |
1255 bool PrintWebViewHelper::PrintPreviewContext::IsReadyToRender() const { | 1223 bool PrintWebViewHelper::PrintPreviewContext::IsReadyToRender() const { |
1256 return state_ != UNINITIALIZED; | 1224 return state_ != UNINITIALIZED; |
1257 } | 1225 } |
1258 | 1226 |
1259 bool PrintWebViewHelper::PrintPreviewContext::IsBusy() const { | |
1260 return state_ == INITIALIZED || state_ == RENDERING; | |
1261 } | |
1262 | |
1263 bool PrintWebViewHelper::PrintPreviewContext::IsModifiable() const { | 1227 bool PrintWebViewHelper::PrintPreviewContext::IsModifiable() const { |
1264 // TODO(vandebo) I think this should only return false if the content is a | 1228 // 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 | 1229 // 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. | 1230 // a PDF (right?), we should check the mime type of the node. |
1267 if (node()) | 1231 if (node()) |
1268 return false; | 1232 return false; |
1269 std::string mime(frame()->dataSource()->response().mimeType().utf8()); | 1233 std::string mime(frame()->dataSource()->response().mimeType().utf8()); |
1270 return mime != "application/pdf"; | 1234 return mime != "application/pdf"; |
1271 } | 1235 } |
1272 | 1236 |
(...skipping 20 matching lines...) Expand all Loading... | |
1293 } | 1257 } |
1294 | 1258 |
1295 const gfx::Size& | 1259 const gfx::Size& |
1296 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { | 1260 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { |
1297 return prep_frame_view_->GetPrintCanvasSize(); | 1261 return prep_frame_view_->GetPrintCanvasSize(); |
1298 } | 1262 } |
1299 | 1263 |
1300 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { | 1264 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
1301 prep_frame_view_.reset(); | 1265 prep_frame_view_.reset(); |
1302 metafile_.reset(); | 1266 metafile_.reset(); |
1303 rendered_pages_.clear(); | 1267 pages_to_render_.clear(); |
1268 total_page_count_ = 0; | |
1269 actual_page_count_ = 0; | |
1270 current_page_index_ = 0; | |
1304 } | 1271 } |
OLD | NEW |