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