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 #if defined(OS_MACOSX) && !defined(USE_SKIA) | 7 #if defined(OS_MACOSX) && !defined(USE_SKIA) |
8 #include <CoreGraphics/CGContext.h> | 8 #include <CoreGraphics/CGContext.h> |
9 #endif | 9 #endif |
10 | 10 |
11 #include <string> | 11 #include <string> |
12 | 12 |
13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
16 #include "base/process_util.h" | |
17 #include "base/string_number_conversions.h" | 16 #include "base/string_number_conversions.h" |
18 #include "base/utf_string_conversions.h" | 17 #include "base/utf_string_conversions.h" |
19 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
20 #include "chrome/common/print_messages.h" | 19 #include "chrome/common/print_messages.h" |
21 #include "chrome/common/render_messages.h" | 20 #include "chrome/common/render_messages.h" |
22 #include "chrome/common/url_constants.h" | 21 #include "chrome/common/url_constants.h" |
23 #include "chrome/renderer/prerender/prerender_helper.h" | 22 #include "chrome/renderer/prerender/prerender_helper.h" |
24 #include "content/renderer/render_view.h" | 23 #include "content/renderer/render_view.h" |
25 #include "grit/generated_resources.h" | 24 #include "grit/generated_resources.h" |
26 #include "printing/metafile_impl.h" | 25 #include "printing/metafile_impl.h" |
27 #include "printing/print_job_constants.h" | 26 #include "printing/print_job_constants.h" |
28 #include "printing/units.h" | 27 #include "printing/units.h" |
29 #include "third_party/skia/include/core/SkRect.h" | 28 #include "third_party/skia/include/core/SkRect.h" |
30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebCanvas.h" | 29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebCanvas.h" |
31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" | 30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" |
32 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h" | 31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h" |
33 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 32 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
34 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" | 33 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" |
35 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 34 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
36 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" | 35 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" |
37 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" | 36 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" |
38 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h" | 37 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h" |
39 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 38 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
40 #include "ui/base/l10n/l10n_util.h" | 39 #include "ui/base/l10n/l10n_util.h" |
41 | 40 |
42 #if defined(OS_POSIX) | 41 #if defined(OS_POSIX) |
| 42 #include "base/process_util.h" |
43 #include "content/common/view_messages.h" | 43 #include "content/common/view_messages.h" |
44 #endif | 44 #endif |
45 | 45 |
46 #if defined(USE_SKIA) | 46 #if defined(USE_SKIA) |
47 #include "skia/ext/vector_canvas.h" | 47 #include "skia/ext/vector_canvas.h" |
48 #include "skia/ext/vector_platform_device_skia.h" | 48 #include "skia/ext/vector_platform_device_skia.h" |
49 #include "third_party/skia/include/core/SkTypeface.h" | 49 #include "third_party/skia/include/core/SkTypeface.h" |
50 #elif defined(OS_MACOSX) | 50 #elif defined(OS_MACOSX) |
51 #include "base/mac/scoped_cftyperef.h" | 51 #include "base/mac/scoped_cftyperef.h" |
52 #include "base/sys_string_conversions.h" | 52 #include "base/sys_string_conversions.h" |
(...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
454 IPC_MESSAGE_HANDLER(PrintMsg_PrintPages, OnPrintPages) | 454 IPC_MESSAGE_HANDLER(PrintMsg_PrintPages, OnPrintPages) |
455 IPC_MESSAGE_HANDLER(PrintMsg_PrintForSystemDialog, OnPrintForSystemDialog) | 455 IPC_MESSAGE_HANDLER(PrintMsg_PrintForSystemDialog, OnPrintForSystemDialog) |
456 IPC_MESSAGE_HANDLER(PrintMsg_InitiatePrintPreview, OnInitiatePrintPreview) | 456 IPC_MESSAGE_HANDLER(PrintMsg_InitiatePrintPreview, OnInitiatePrintPreview) |
457 IPC_MESSAGE_HANDLER(PrintMsg_PrintNodeUnderContextMenu, | 457 IPC_MESSAGE_HANDLER(PrintMsg_PrintNodeUnderContextMenu, |
458 OnPrintNodeUnderContextMenu) | 458 OnPrintNodeUnderContextMenu) |
459 IPC_MESSAGE_HANDLER(PrintMsg_PrintPreview, OnPrintPreview) | 459 IPC_MESSAGE_HANDLER(PrintMsg_PrintPreview, OnPrintPreview) |
460 IPC_MESSAGE_HANDLER(PrintMsg_PrintForPrintPreview, OnPrintForPrintPreview) | 460 IPC_MESSAGE_HANDLER(PrintMsg_PrintForPrintPreview, OnPrintForPrintPreview) |
461 IPC_MESSAGE_HANDLER(PrintMsg_PrintingDone, OnPrintingDone) | 461 IPC_MESSAGE_HANDLER(PrintMsg_PrintingDone, OnPrintingDone) |
462 IPC_MESSAGE_HANDLER(PrintMsg_ResetScriptedPrintCount, | 462 IPC_MESSAGE_HANDLER(PrintMsg_ResetScriptedPrintCount, |
463 ResetScriptedPrintCount) | 463 ResetScriptedPrintCount) |
464 IPC_MESSAGE_HANDLER(PrintMsg_ContinuePreview, OnContinuePreview) | |
465 IPC_MESSAGE_HANDLER(PrintMsg_AbortPreview, OnAbortPreview) | |
466 IPC_MESSAGE_HANDLER(PrintMsg_PreviewPrintingRequestCancelled, | 464 IPC_MESSAGE_HANDLER(PrintMsg_PreviewPrintingRequestCancelled, |
467 DisplayPrintJobError) | 465 DisplayPrintJobError) |
468 IPC_MESSAGE_UNHANDLED(handled = false) | 466 IPC_MESSAGE_UNHANDLED(handled = false) |
469 IPC_END_MESSAGE_MAP() | 467 IPC_END_MESSAGE_MAP() |
470 return handled; | 468 return handled; |
471 } | 469 } |
472 | 470 |
473 void PrintWebViewHelper::OnPrintForPrintPreview( | 471 void PrintWebViewHelper::OnPrintForPrintPreview( |
474 const DictionaryValue& job_settings) { | 472 const DictionaryValue& job_settings) { |
475 DCHECK(is_preview_); | 473 DCHECK(is_preview_); |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
578 print_pages_params_->params.preview_request_id; | 576 print_pages_params_->params.preview_request_id; |
579 | 577 |
580 Send(new PrintHostMsg_PagesReadyForPreview(routing_id(), preview_params)); | 578 Send(new PrintHostMsg_PagesReadyForPreview(routing_id(), preview_params)); |
581 return; | 579 return; |
582 } | 580 } |
583 // Always clear |old_print_pages_params_| before rendering the pages. | 581 // Always clear |old_print_pages_params_| before rendering the pages. |
584 old_print_pages_params_.reset(); | 582 old_print_pages_params_.reset(); |
585 | 583 |
586 // PDF printer device supports alpha blending. | 584 // PDF printer device supports alpha blending. |
587 print_pages_params_->params.supports_alpha_blend = true; | 585 print_pages_params_->params.supports_alpha_blend = true; |
588 if (!CreatePreviewDocument()) | 586 if (CreatePreviewDocument()) |
| 587 DidFinishPrinting(OK); |
| 588 else |
589 DidFinishPrinting(FAIL_PREVIEW); | 589 DidFinishPrinting(FAIL_PREVIEW); |
590 } | 590 } |
591 | 591 |
592 bool PrintWebViewHelper::CreatePreviewDocument() { | 592 bool PrintWebViewHelper::CreatePreviewDocument() { |
593 PrintMsg_Print_Params print_params = print_pages_params_->params; | 593 PrintMsg_Print_Params print_params = print_pages_params_->params; |
594 const std::vector<int>& pages = print_pages_params_->pages; | 594 const std::vector<int>& pages = print_pages_params_->pages; |
595 if (!print_preview_context_.CreatePreviewDocument(&print_params, pages)) | 595 if (!print_preview_context_.CreatePreviewDocument(&print_params, pages)) |
596 return false; | 596 return false; |
597 PrintHostMsg_DidGetPreviewPageCount_Params params; | 597 PrintHostMsg_DidGetPreviewPageCount_Params params; |
598 params.page_count = print_preview_context_.total_page_count(); | 598 params.page_count = print_preview_context_.total_page_count(); |
599 params.is_modifiable = print_preview_context_.IsModifiable(); | 599 params.is_modifiable = print_preview_context_.IsModifiable(); |
600 params.document_cookie = print_pages_params_->params.document_cookie; | 600 params.document_cookie = print_pages_params_->params.document_cookie; |
601 params.preview_request_id = print_pages_params_->params.preview_request_id; | 601 params.preview_request_id = print_pages_params_->params.preview_request_id; |
602 Send(new PrintHostMsg_DidGetPreviewPageCount(routing_id(), params)); | 602 Send(new PrintHostMsg_DidGetPreviewPageCount(routing_id(), params)); |
603 PreviewPageRendered(printing::INVALID_PAGE_INDEX, NULL); | 603 if (CheckForCancel()) |
604 return true; | 604 return false; |
605 } | |
606 | 605 |
607 void PrintWebViewHelper::OnContinuePreview(int requested_preview_page_index) { | |
608 // Spurious message. We already finished/cancelled/aborted the print preview. | |
609 if (!print_preview_context_.IsBusy()) | |
610 return; | |
611 int page_number; | 606 int page_number; |
612 #if defined(USE_SKIA) | 607 while ((page_number = print_preview_context_.GetNextPageNumber()) >= 0) { |
613 if (requested_preview_page_index >= printing::FIRST_PAGE_INDEX) { | 608 if (!RenderPreviewPage(page_number)) |
614 page_number = requested_preview_page_index; | 609 return false; |
615 } else | 610 if (CheckForCancel()) |
616 #endif | 611 return false; |
617 { | 612 }; |
618 page_number = print_preview_context_.GetNextPageNumber(); | |
619 } | |
620 | |
621 if (page_number >= printing::FIRST_PAGE_INDEX) { | |
622 // Continue generating the print preview. | |
623 RenderPreviewPage(page_number); | |
624 return; | |
625 } | |
626 | 613 |
627 // Finished generating preview. Finalize the document. | 614 // Finished generating preview. Finalize the document. |
628 if (FinalizePreviewDocument()) { | 615 if (!FinalizePreviewDocument()) |
629 print_preview_context_.Finished(); | 616 return false; |
630 DidFinishPrinting(OK); | 617 print_preview_context_.Finished(); |
631 } else { | 618 return true; |
632 DidFinishPrinting(FAIL_PREVIEW); | |
633 } | |
634 } | |
635 | |
636 void PrintWebViewHelper::OnAbortPreview() { | |
637 DidFinishPrinting(ABORT_PREVIEW); | |
638 return; | |
639 } | 619 } |
640 | 620 |
641 bool PrintWebViewHelper::FinalizePreviewDocument() { | 621 bool PrintWebViewHelper::FinalizePreviewDocument() { |
642 print_preview_context_.FinalizePreviewDocument(); | 622 print_preview_context_.FinalizePreviewDocument(); |
643 | 623 |
644 // Get the size of the resulting metafile. | 624 // Get the size of the resulting metafile. |
645 printing::PreviewMetafile* metafile = print_preview_context_.metafile(); | 625 printing::PreviewMetafile* metafile = print_preview_context_.metafile(); |
646 uint32 buf_size = metafile->GetDataSize(); | 626 uint32 buf_size = metafile->GetDataSize(); |
647 DCHECK_GT(buf_size, 0u); | 627 DCHECK_GT(buf_size, 0u); |
648 | 628 |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
743 DisplayPrintJobError(); | 723 DisplayPrintJobError(); |
744 | 724 |
745 if (notify_browser_of_print_failure_) { | 725 if (notify_browser_of_print_failure_) { |
746 int cookie = print_pages_params_->params.document_cookie; | 726 int cookie = print_pages_params_->params.document_cookie; |
747 Send(new PrintHostMsg_PrintingFailed(routing_id(), cookie)); | 727 Send(new PrintHostMsg_PrintingFailed(routing_id(), cookie)); |
748 } | 728 } |
749 } else if (result == FAIL_PREVIEW) { | 729 } else if (result == FAIL_PREVIEW) { |
750 DCHECK(is_preview_); | 730 DCHECK(is_preview_); |
751 store_print_pages_params = false; | 731 store_print_pages_params = false; |
752 int cookie = print_pages_params_->params.document_cookie; | 732 int cookie = print_pages_params_->params.document_cookie; |
753 Send(new PrintHostMsg_PrintPreviewFailed(routing_id(), cookie)); | 733 if (notify_browser_of_print_failure_) |
| 734 Send(new PrintHostMsg_PrintPreviewFailed(routing_id(), cookie)); |
| 735 else |
| 736 Send(new PrintHostMsg_PrintPreviewCancelled(routing_id(), cookie)); |
754 print_preview_context_.Failed(); | 737 print_preview_context_.Failed(); |
755 } else if (result == ABORT_PREVIEW) { | |
756 DCHECK(is_preview_); | |
757 store_print_pages_params = false; | |
758 print_preview_context_.Abort(); | |
759 } | 738 } |
760 | 739 |
761 if (print_web_view_) { | 740 if (print_web_view_) { |
762 print_web_view_->close(); | 741 print_web_view_->close(); |
763 print_web_view_ = NULL; | 742 print_web_view_ = NULL; |
764 } | 743 } |
765 | 744 |
766 if (store_print_pages_params) { | 745 if (store_print_pages_params) { |
767 old_print_pages_params_.reset(print_pages_params_.release()); | 746 old_print_pages_params_.reset(print_pages_params_.release()); |
768 } else { | 747 } else { |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
986 DCHECK(!prepare->get()); | 965 DCHECK(!prepare->get()); |
987 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, | 966 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, |
988 frame, node)); | 967 frame, node)); |
989 UpdatePrintableSizeInPrintParameters(frame, node, prepare->get(), | 968 UpdatePrintableSizeInPrintParameters(frame, node, prepare->get(), |
990 &print_pages_params_->params); | 969 &print_pages_params_->params); |
991 Send(new PrintHostMsg_DidGetDocumentCookie( | 970 Send(new PrintHostMsg_DidGetDocumentCookie( |
992 routing_id(), print_pages_params_->params.document_cookie)); | 971 routing_id(), print_pages_params_->params.document_cookie)); |
993 return true; | 972 return true; |
994 } | 973 } |
995 | 974 |
996 bool PrintWebViewHelper::UpdatePrintSettingsRequestId( | |
997 const DictionaryValue& job_settings, | |
998 PrintMsg_Print_Params* params) { | |
999 if (!job_settings.GetInteger(printing::kPreviewRequestID, | |
1000 &(params->preview_request_id))) { | |
1001 NOTREACHED(); | |
1002 return false; | |
1003 } | |
1004 return true; | |
1005 } | |
1006 | |
1007 bool PrintWebViewHelper::UpdatePrintSettings( | 975 bool PrintWebViewHelper::UpdatePrintSettings( |
1008 const DictionaryValue& job_settings) { | 976 const DictionaryValue& job_settings) { |
1009 PrintMsg_PrintPages_Params settings; | 977 PrintMsg_PrintPages_Params settings; |
1010 | 978 |
1011 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), | 979 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), |
1012 print_pages_params_->params.document_cookie, job_settings, &settings)); | 980 print_pages_params_->params.document_cookie, job_settings, &settings)); |
1013 | 981 |
1014 if (settings.params.dpi < kMinDpi || !settings.params.document_cookie) | 982 if (settings.params.dpi < kMinDpi || !settings.params.document_cookie) |
1015 return false; | 983 return false; |
1016 | 984 |
1017 if (!UpdatePrintSettingsRequestId(job_settings, &(settings.params))) | 985 if (!job_settings.GetString(printing::kPreviewUIAddr, |
1018 return false; | 986 &(settings.params.preview_ui_addr)) || |
1019 | 987 !job_settings.GetInteger(printing::kPreviewRequestID, |
1020 if (!job_settings.GetBoolean(printing::kIsFirstRequest, | 988 &(settings.params.preview_request_id)) || |
| 989 !job_settings.GetBoolean(printing::kIsFirstRequest, |
1021 &(settings.params.is_first_request))) { | 990 &(settings.params.is_first_request))) { |
1022 NOTREACHED(); | 991 NOTREACHED(); |
| 992 return false; |
1023 } | 993 } |
1024 | 994 |
1025 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); | 995 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); |
1026 | 996 |
1027 if (print_pages_params_->params.display_header_footer) { | 997 if (print_pages_params_->params.display_header_footer) { |
1028 header_footer_info_.reset(new DictionaryValue()); | 998 header_footer_info_.reset(new DictionaryValue()); |
1029 header_footer_info_->SetString(printing::kSettingHeaderFooterDate, | 999 header_footer_info_->SetString(printing::kSettingHeaderFooterDate, |
1030 print_pages_params_->params.date); | 1000 print_pages_params_->params.date); |
1031 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, | 1001 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, |
1032 print_pages_params_->params.url); | 1002 print_pages_params_->params.url); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1122 min_wait_seconds = std::min( | 1092 min_wait_seconds = std::min( |
1123 kMinSecondsToIgnoreJavascriptInitiatedPrint << | 1093 kMinSecondsToIgnoreJavascriptInitiatedPrint << |
1124 (user_cancelled_scripted_print_count_ - 3), | 1094 (user_cancelled_scripted_print_count_ - 3), |
1125 kMaxSecondsToIgnoreJavascriptInitiatedPrint); | 1095 kMaxSecondsToIgnoreJavascriptInitiatedPrint); |
1126 } | 1096 } |
1127 if (diff.InSeconds() < min_wait_seconds) { | 1097 if (diff.InSeconds() < min_wait_seconds) { |
1128 too_frequent = true; | 1098 too_frequent = true; |
1129 } | 1099 } |
1130 } | 1100 } |
1131 | 1101 |
1132 if (!too_frequent && print_preview_context_.IsBusy()) | |
1133 too_frequent = true; | |
1134 | |
1135 if (!too_frequent) | 1102 if (!too_frequent) |
1136 return false; | 1103 return false; |
1137 | 1104 |
1138 WebString message(WebString::fromUTF8( | 1105 WebString message(WebString::fromUTF8( |
1139 "Ignoring too frequent calls to print().")); | 1106 "Ignoring too frequent calls to print().")); |
1140 frame->addMessageToConsole(WebConsoleMessage(WebConsoleMessage::LevelWarning, | 1107 frame->addMessageToConsole(WebConsoleMessage(WebConsoleMessage::LevelWarning, |
1141 message)); | 1108 message)); |
1142 return true; | 1109 return true; |
1143 } | 1110 } |
1144 | 1111 |
(...skipping 15 matching lines...) Expand all Loading... |
1160 render_view()->runModalAlertDialog( | 1127 render_view()->runModalAlertDialog( |
1161 web_view->mainFrame(), | 1128 web_view->mainFrame(), |
1162 l10n_util::GetStringUTF16(IDS_PRINT_SPOOL_FAILED_ERROR_TEXT)); | 1129 l10n_util::GetStringUTF16(IDS_PRINT_SPOOL_FAILED_ERROR_TEXT)); |
1163 } | 1130 } |
1164 | 1131 |
1165 void PrintWebViewHelper::RequestPrintPreview() { | 1132 void PrintWebViewHelper::RequestPrintPreview() { |
1166 old_print_pages_params_.reset(); | 1133 old_print_pages_params_.reset(); |
1167 Send(new PrintHostMsg_RequestPrintPreview(routing_id())); | 1134 Send(new PrintHostMsg_RequestPrintPreview(routing_id())); |
1168 } | 1135 } |
1169 | 1136 |
1170 void PrintWebViewHelper::PreviewPageRendered(int page_number, | 1137 bool PrintWebViewHelper::CheckForCancel() { |
| 1138 bool cancel = false; |
| 1139 Send(new PrintHostMsg_CheckForCancel( |
| 1140 routing_id(), |
| 1141 print_pages_params_->params.preview_ui_addr, |
| 1142 print_pages_params_->params.preview_request_id, |
| 1143 &cancel)); |
| 1144 if (cancel) |
| 1145 notify_browser_of_print_failure_ = false; |
| 1146 return cancel; |
| 1147 } |
| 1148 |
| 1149 bool PrintWebViewHelper::PreviewPageRendered(int page_number, |
1171 printing::Metafile* metafile) { | 1150 printing::Metafile* metafile) { |
1172 if ((page_number == printing::INVALID_PAGE_INDEX && metafile) || | 1151 DCHECK_GE(page_number, printing::FIRST_PAGE_INDEX); |
1173 (page_number >= printing::FIRST_PAGE_INDEX && !metafile && | 1152 |
1174 print_preview_context_.IsModifiable())) { | 1153 // For non-modifiable files, |metafile| should be NULL, so do not bother |
1175 NOTREACHED(); | 1154 // sending a message. |
1176 DidFinishPrinting(FAIL_PREVIEW); | 1155 if (!print_preview_context_.IsModifiable()) { |
1177 return; | 1156 DCHECK(!metafile); |
| 1157 return true; |
1178 } | 1158 } |
1179 | 1159 |
1180 uint32 buf_size = 0; | 1160 if (!metafile) { |
| 1161 NOTREACHED(); |
| 1162 return false; |
| 1163 } |
| 1164 |
1181 PrintHostMsg_DidPreviewPage_Params preview_page_params; | 1165 PrintHostMsg_DidPreviewPage_Params preview_page_params; |
1182 // Get the size of the resulting metafile. | 1166 // Get the size of the resulting metafile. |
1183 if (metafile) { | 1167 uint32 buf_size = metafile->GetDataSize(); |
1184 buf_size = metafile->GetDataSize(); | 1168 DCHECK_GT(buf_size, 0u); |
1185 DCHECK_GT(buf_size, 0u); | 1169 if (!CopyMetafileDataToSharedMem( |
1186 if (!CopyMetafileDataToSharedMem( | 1170 metafile, &(preview_page_params.metafile_data_handle))) { |
1187 metafile, &(preview_page_params.metafile_data_handle))) { | 1171 return false; |
1188 DidFinishPrinting(FAIL_PREVIEW); | |
1189 return; | |
1190 } | |
1191 } | 1172 } |
1192 preview_page_params.data_size = buf_size; | 1173 preview_page_params.data_size = buf_size; |
1193 preview_page_params.page_number = page_number; | 1174 preview_page_params.page_number = page_number; |
1194 preview_page_params.preview_request_id = | 1175 preview_page_params.preview_request_id = |
1195 print_pages_params_->params.preview_request_id; | 1176 print_pages_params_->params.preview_request_id; |
| 1177 |
1196 Send(new PrintHostMsg_DidPreviewPage(routing_id(), preview_page_params)); | 1178 Send(new PrintHostMsg_DidPreviewPage(routing_id(), preview_page_params)); |
| 1179 return true; |
1197 } | 1180 } |
1198 | 1181 |
1199 PrintWebViewHelper::PrintPreviewContext::PrintPreviewContext() | 1182 PrintWebViewHelper::PrintPreviewContext::PrintPreviewContext() |
1200 : frame_(NULL), | 1183 : frame_(NULL), |
1201 total_page_count_(0), | 1184 total_page_count_(0), |
1202 actual_page_count_(0), | 1185 actual_page_count_(0), |
1203 current_page_number_(0), | 1186 current_page_index_(0), |
1204 state_(UNINITIALIZED) { | 1187 state_(UNINITIALIZED) { |
1205 } | 1188 } |
1206 | 1189 |
1207 PrintWebViewHelper::PrintPreviewContext::~PrintPreviewContext() { | 1190 PrintWebViewHelper::PrintPreviewContext::~PrintPreviewContext() { |
1208 } | 1191 } |
1209 | 1192 |
1210 void PrintWebViewHelper::PrintPreviewContext::InitWithFrame( | 1193 void PrintWebViewHelper::PrintPreviewContext::InitWithFrame( |
1211 WebKit::WebFrame* web_frame) { | 1194 WebKit::WebFrame* web_frame) { |
1212 DCHECK(web_frame); | 1195 DCHECK(web_frame); |
1213 if (IsReadyToRender()) | |
1214 return; | |
1215 state_ = INITIALIZED; | 1196 state_ = INITIALIZED; |
1216 frame_ = web_frame; | 1197 frame_ = web_frame; |
1217 node_.reset(); | 1198 node_.reset(); |
1218 } | 1199 } |
1219 | 1200 |
1220 void PrintWebViewHelper::PrintPreviewContext::InitWithNode( | 1201 void PrintWebViewHelper::PrintPreviewContext::InitWithNode( |
1221 const WebKit::WebNode& web_node) { | 1202 const WebKit::WebNode& web_node) { |
1222 DCHECK(!web_node.isNull()); | 1203 DCHECK(!web_node.isNull()); |
1223 if (IsReadyToRender()) | |
1224 return; | |
1225 state_ = INITIALIZED; | 1204 state_ = INITIALIZED; |
1226 frame_ = web_node.document().frame(); | 1205 frame_ = web_node.document().frame(); |
1227 node_.reset(new WebNode(web_node)); | 1206 node_.reset(new WebNode(web_node)); |
1228 } | 1207 } |
1229 | 1208 |
1230 void PrintWebViewHelper::PrintPreviewContext::OnPrintPreview() { | 1209 void PrintWebViewHelper::PrintPreviewContext::OnPrintPreview() { |
1231 DCHECK(IsReadyToRender()); | 1210 DCHECK(IsReadyToRender()); |
1232 ClearContext(); | 1211 ClearContext(); |
1233 } | 1212 } |
1234 | 1213 |
(...skipping 12 matching lines...) Expand all Loading... |
1247 // Need to make sure old object gets destroyed first. | 1226 // Need to make sure old object gets destroyed first. |
1248 prep_frame_view_.reset(new PrepareFrameAndViewForPrint(*print_params, frame(), | 1227 prep_frame_view_.reset(new PrepareFrameAndViewForPrint(*print_params, frame(), |
1249 node())); | 1228 node())); |
1250 UpdatePrintableSizeInPrintParameters(frame_, node_.get(), | 1229 UpdatePrintableSizeInPrintParameters(frame_, node_.get(), |
1251 prep_frame_view_.get(), print_params); | 1230 prep_frame_view_.get(), print_params); |
1252 | 1231 |
1253 total_page_count_ = prep_frame_view_->GetExpectedPageCount(); | 1232 total_page_count_ = prep_frame_view_->GetExpectedPageCount(); |
1254 if (total_page_count_ == 0) | 1233 if (total_page_count_ == 0) |
1255 return false; | 1234 return false; |
1256 | 1235 |
1257 current_page_number_ = 0; | 1236 current_page_index_ = 0; |
1258 if (pages.empty()) { | 1237 if (pages.empty()) { |
1259 actual_page_count_ = total_page_count_; | 1238 actual_page_count_ = total_page_count_; |
1260 rendered_pages_ = std::vector<PreviewPageInfo>(total_page_count_, | 1239 for (int i = 0; i < actual_page_count_; ++i) |
1261 std::make_pair(false, -1)); | 1240 pages_to_render_.push_back(i); |
1262 } else { | 1241 } else { |
1263 actual_page_count_ = pages.size(); | 1242 actual_page_count_ = pages.size(); |
1264 rendered_pages_ = std::vector<PreviewPageInfo>(total_page_count_, | |
1265 std::make_pair(true, -1)); | |
1266 for (int i = 0; i < actual_page_count_; ++i) { | 1243 for (int i = 0; i < actual_page_count_; ++i) { |
1267 int page_number = pages[i]; | 1244 int page_number = pages[i]; |
1268 if (page_number < printing::FIRST_PAGE_INDEX || | 1245 if (page_number < printing::FIRST_PAGE_INDEX || |
1269 page_number >= total_page_count_) { | 1246 page_number >= total_page_count_) { |
1270 return false; | 1247 return false; |
1271 } | 1248 } |
1272 rendered_pages_[page_number].first = false; | 1249 pages_to_render_.push_back(page_number); |
1273 rendered_pages_[page_number].second = i; | |
1274 } | 1250 } |
1275 } | 1251 } |
1276 | 1252 |
1277 document_render_time_ = base::TimeDelta(); | 1253 document_render_time_ = base::TimeDelta(); |
1278 begin_time_ = base::TimeTicks::Now(); | 1254 begin_time_ = base::TimeTicks::Now(); |
1279 | 1255 |
1280 return true; | 1256 return true; |
1281 } | 1257 } |
1282 | 1258 |
1283 void PrintWebViewHelper::PrintPreviewContext::RenderedPreviewPage( | 1259 void PrintWebViewHelper::PrintPreviewContext::RenderedPreviewPage( |
(...skipping 26 matching lines...) Expand all Loading... |
1310 UMA_HISTOGRAM_MEDIUM_TIMES("PrintPreview.RenderAndGeneratePDFTimeAvgPerPage", | 1286 UMA_HISTOGRAM_MEDIUM_TIMES("PrintPreview.RenderAndGeneratePDFTimeAvgPerPage", |
1311 total_time / actual_page_count_); | 1287 total_time / actual_page_count_); |
1312 } | 1288 } |
1313 | 1289 |
1314 void PrintWebViewHelper::PrintPreviewContext::Finished() { | 1290 void PrintWebViewHelper::PrintPreviewContext::Finished() { |
1315 DCHECK_EQ(DONE, state_); | 1291 DCHECK_EQ(DONE, state_); |
1316 ClearContext(); | 1292 ClearContext(); |
1317 } | 1293 } |
1318 | 1294 |
1319 void PrintWebViewHelper::PrintPreviewContext::Failed() { | 1295 void PrintWebViewHelper::PrintPreviewContext::Failed() { |
1320 DCHECK(IsBusy()); | 1296 DCHECK(state_ == INITIALIZED || state_ == RENDERING); |
1321 state_ = INITIALIZED; | 1297 state_ = INITIALIZED; |
1322 ClearContext(); | 1298 ClearContext(); |
1323 } | 1299 } |
1324 | 1300 |
1325 void PrintWebViewHelper::PrintPreviewContext::Abort() { | |
1326 state_ = UNINITIALIZED; | |
1327 ClearContext(); | |
1328 frame_ = NULL; | |
1329 node_.reset(); | |
1330 } | |
1331 | |
1332 int PrintWebViewHelper::PrintPreviewContext::GetNextPageNumber() { | 1301 int PrintWebViewHelper::PrintPreviewContext::GetNextPageNumber() { |
1333 DCHECK_EQ(RENDERING, state_); | 1302 DCHECK_EQ(RENDERING, state_); |
1334 for (int i = 0; i < total_page_count_; i++) { | 1303 if (current_page_index_ >= actual_page_count_) |
1335 if (!rendered_pages_[current_page_number_].first) | 1304 return -1; |
1336 break; | 1305 return pages_to_render_[current_page_index_++]; |
1337 current_page_number_ = (current_page_number_ + 1) % total_page_count_; | |
1338 } | |
1339 if (rendered_pages_[current_page_number_].first) | |
1340 return printing::INVALID_PAGE_INDEX; | |
1341 rendered_pages_[current_page_number_].first = true; | |
1342 return current_page_number_; | |
1343 } | 1306 } |
1344 | 1307 |
1345 bool PrintWebViewHelper::PrintPreviewContext::IsReadyToRender() const { | 1308 bool PrintWebViewHelper::PrintPreviewContext::IsReadyToRender() const { |
1346 return state_ != UNINITIALIZED; | 1309 return state_ != UNINITIALIZED; |
1347 } | 1310 } |
1348 | 1311 |
1349 bool PrintWebViewHelper::PrintPreviewContext::IsBusy() const { | |
1350 return state_ == INITIALIZED || state_ == RENDERING; | |
1351 } | |
1352 | |
1353 bool PrintWebViewHelper::PrintPreviewContext::IsModifiable() const { | 1312 bool PrintWebViewHelper::PrintPreviewContext::IsModifiable() const { |
1354 // TODO(vandebo) I think this should only return false if the content is a | 1313 // TODO(vandebo) I think this should only return false if the content is a |
1355 // PDF, just because we are printing a particular node does not mean it's | 1314 // PDF, just because we are printing a particular node does not mean it's |
1356 // a PDF (right?), we should check the mime type of the node. | 1315 // a PDF (right?), we should check the mime type of the node. |
1357 if (node()) | 1316 if (node()) |
1358 return false; | 1317 return false; |
1359 std::string mime(frame()->dataSource()->response().mimeType().utf8()); | 1318 std::string mime(frame()->dataSource()->response().mimeType().utf8()); |
1360 return mime != "application/pdf"; | 1319 return mime != "application/pdf"; |
1361 } | 1320 } |
1362 | 1321 |
1363 WebKit::WebFrame* PrintWebViewHelper::PrintPreviewContext::frame() const { | 1322 WebKit::WebFrame* PrintWebViewHelper::PrintPreviewContext::frame() const { |
1364 return frame_; | 1323 return frame_; |
1365 } | 1324 } |
1366 | 1325 |
1367 WebKit::WebNode* PrintWebViewHelper::PrintPreviewContext::node() const { | 1326 WebKit::WebNode* PrintWebViewHelper::PrintPreviewContext::node() const { |
1368 return node_.get(); | 1327 return node_.get(); |
1369 } | 1328 } |
1370 | 1329 |
1371 int PrintWebViewHelper::PrintPreviewContext::total_page_count() const { | 1330 int PrintWebViewHelper::PrintPreviewContext::total_page_count() const { |
| 1331 DCHECK(IsReadyToRender()); |
1372 return total_page_count_; | 1332 return total_page_count_; |
1373 } | 1333 } |
1374 | 1334 |
1375 printing::PreviewMetafile* | 1335 printing::PreviewMetafile* |
1376 PrintWebViewHelper::PrintPreviewContext::metafile() const { | 1336 PrintWebViewHelper::PrintPreviewContext::metafile() const { |
1377 return metafile_.get(); | 1337 return metafile_.get(); |
1378 } | 1338 } |
1379 | 1339 |
1380 const PrintMsg_Print_Params& | 1340 const PrintMsg_Print_Params& |
1381 PrintWebViewHelper::PrintPreviewContext::print_params() const { | 1341 PrintWebViewHelper::PrintPreviewContext::print_params() const { |
1382 return *print_params_; | 1342 return *print_params_; |
1383 } | 1343 } |
1384 | 1344 |
1385 const gfx::Size& | 1345 const gfx::Size& |
1386 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { | 1346 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { |
1387 return prep_frame_view_->GetPrintCanvasSize(); | 1347 return prep_frame_view_->GetPrintCanvasSize(); |
1388 } | 1348 } |
1389 | 1349 |
1390 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { | 1350 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
1391 prep_frame_view_.reset(); | 1351 prep_frame_view_.reset(); |
1392 metafile_.reset(); | 1352 metafile_.reset(); |
1393 rendered_pages_.clear(); | 1353 pages_to_render_.clear(); |
1394 } | 1354 } |
OLD | NEW |