OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/printing/print_web_view_helper.h" | 5 #include "chrome/renderer/printing/print_web_view_helper.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
468 content_area.size().width() / scale_factor, | 468 content_area.size().width() / scale_factor, |
469 content_area.size().height() / scale_factor)); | 469 content_area.size().height() / scale_factor)); |
470 SkIRect clip_int_rect; | 470 SkIRect clip_int_rect; |
471 clip_rect.roundOut(&clip_int_rect); | 471 clip_rect.roundOut(&clip_int_rect); |
472 SkRegion clip_region(clip_int_rect); | 472 SkRegion clip_region(clip_int_rect); |
473 canvas->setClipRegion(clip_region); | 473 canvas->setClipRegion(clip_region); |
474 } | 474 } |
475 return frame->printPage(page_number, canvas); | 475 return frame->printPage(page_number, canvas); |
476 } | 476 } |
477 | 477 |
| 478 // Class that calls the Begin and End print functions on the frame and changes |
| 479 // the size of the view temporarily to support full page printing.. |
| 480 // Do not serve any events in the time between construction and destruction of |
| 481 // this class because it will cause flicker. |
| 482 class PrepareFrameAndViewForPrint { |
| 483 public: |
| 484 // Prints |frame|. If |node| is not NULL, then only that node will be |
| 485 // printed. |
| 486 PrepareFrameAndViewForPrint(const PrintMsg_Print_Params& params, |
| 487 WebKit::WebFrame* frame, |
| 488 const WebKit::WebNode& node, |
| 489 bool ignore_css_margins); |
| 490 ~PrepareFrameAndViewForPrint(); |
| 491 |
| 492 void StartPrinting(); |
| 493 |
| 494 int GetExpectedPageCount() const { |
| 495 return expected_pages_count_; |
| 496 } |
| 497 |
| 498 gfx::Size GetPrintCanvasSize() const; |
| 499 |
| 500 void FinishPrinting(); |
| 501 |
| 502 private: |
| 503 WebKit::WebFrame* frame_; |
| 504 WebKit::WebNode node_to_print_; |
| 505 WebKit::WebPrintParams web_print_params_; |
| 506 gfx::Size prev_view_size_; |
| 507 gfx::Size prev_scroll_offset_; |
| 508 int expected_pages_count_; |
| 509 bool should_print_backgrounds_; |
| 510 |
| 511 DISALLOW_COPY_AND_ASSIGN(PrepareFrameAndViewForPrint); |
| 512 }; |
| 513 |
| 514 |
478 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( | 515 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( |
479 const PrintMsg_Print_Params& print_params, | 516 const PrintMsg_Print_Params& params, |
480 WebKit::WebFrame* frame, | 517 WebKit::WebFrame* frame, |
481 const WebKit::WebNode& node) | 518 const WebKit::WebNode& node, |
482 : frame_(frame), | 519 bool ignore_css_margins) |
483 node_to_print_(node), | 520 : frame_(frame), |
484 web_view_(frame->view()), | 521 node_to_print_(node), |
485 expected_pages_count_(0), | 522 expected_pages_count_(0), |
486 use_browser_overlays_(true), | 523 should_print_backgrounds_(params.should_print_backgrounds) { |
487 finished_(false), | 524 PrintMsg_Print_Params print_params = params; |
488 should_print_backgrounds_(print_params.should_print_backgrounds) { | |
489 WebKit::WebPrintParams webkit_print_params; | |
490 ComputeWebKitPrintParamsInDesiredDpi(print_params, &webkit_print_params); | |
491 | 525 |
492 if (WebKit::WebFrame* web_frame = web_view_->mainFrame()) | 526 if (WebKit::WebFrame* web_frame = frame->view()->mainFrame()) |
493 prev_scroll_offset_ = web_frame->scrollOffset(); | 527 prev_scroll_offset_ = web_frame->scrollOffset(); |
494 prev_view_size_ = web_view_->size(); | 528 prev_view_size_ = frame->view()->size(); |
495 | 529 |
496 StartPrinting(webkit_print_params); | 530 if (!PrintingNodeOrPdfFrame(frame_, node_to_print_)) { |
| 531 bool fit_to_page = ignore_css_margins && |
| 532 print_params.print_scaling_option == |
| 533 WebKit::WebPrintScalingOptionFitToPrintableArea; |
| 534 print_params = CalculatePrintParamsForCss(frame_, 0, print_params, |
| 535 ignore_css_margins, fit_to_page, |
| 536 NULL); |
| 537 } |
| 538 |
| 539 ComputeWebKitPrintParamsInDesiredDpi(print_params, &web_print_params_); |
497 } | 540 } |
498 | 541 |
499 PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() { | 542 PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() { |
500 FinishPrinting(); | 543 FinishPrinting(); |
501 } | 544 } |
502 | 545 |
503 void PrepareFrameAndViewForPrint::UpdatePrintParams( | 546 void PrepareFrameAndViewForPrint::StartPrinting() { |
504 const PrintMsg_Print_Params& print_params) { | |
505 DCHECK(!finished_); | |
506 WebKit::WebPrintParams webkit_print_params; | |
507 ComputeWebKitPrintParamsInDesiredDpi(print_params, &webkit_print_params); | |
508 | |
509 should_print_backgrounds_ = print_params.should_print_backgrounds; | |
510 | |
511 if (webkit_print_params.printContentArea == | |
512 web_print_params_.printContentArea && | |
513 webkit_print_params.printableArea == web_print_params_.printableArea && | |
514 webkit_print_params.paperSize == web_print_params_.paperSize && | |
515 webkit_print_params.printScalingOption == | |
516 web_print_params_.printScalingOption) { | |
517 return; | |
518 } | |
519 | |
520 frame_->printEnd(); | |
521 StartPrinting(webkit_print_params); | |
522 } | |
523 | |
524 gfx::Size PrepareFrameAndViewForPrint::GetPrintCanvasSize() const { | |
525 return gfx::Size(web_print_params_.printContentArea.width, | |
526 web_print_params_.printContentArea.height); | |
527 } | |
528 | |
529 void PrepareFrameAndViewForPrint::StartPrinting( | |
530 const WebKit::WebPrintParams& webkit_print_params) { | |
531 web_print_params_ = webkit_print_params; | |
532 | |
533 // Layout page according to printer page size. Since WebKit shrinks the | 547 // Layout page according to printer page size. Since WebKit shrinks the |
534 // size of the page automatically (from 125% to 200%) we trick it to | 548 // size of the page automatically (from 125% to 200%) we trick it to |
535 // think the page is 125% larger so the size of the page is correct for | 549 // think the page is 125% larger so the size of the page is correct for |
536 // minimum (default) scaling. | 550 // minimum (default) scaling. |
537 // This is important for sites that try to fill the page. | 551 // This is important for sites that try to fill the page. |
538 gfx::Size print_layout_size(web_print_params_.printContentArea.width, | 552 gfx::Size print_layout_size(web_print_params_.printContentArea.width, |
539 web_print_params_.printContentArea.height); | 553 web_print_params_.printContentArea.height); |
540 print_layout_size.set_height(static_cast<int>( | 554 print_layout_size.set_height(static_cast<int>( |
541 static_cast<double>(print_layout_size.height()) * 1.25)); | 555 static_cast<double>(print_layout_size.height()) * 1.25)); |
542 | 556 |
543 web_view_->resize(print_layout_size); | 557 WebKit::WebView* web_view = frame_->view(); |
| 558 web_view->resize(print_layout_size); |
| 559 web_view->settings()->setShouldPrintBackgrounds(should_print_backgrounds_); |
544 | 560 |
545 expected_pages_count_ = frame_->printBegin(web_print_params_, | 561 // TODO(vitalybuka): Update call after |
546 node_to_print_, | 562 // https://bugs.webkit.org/show_bug.cgi?id=107718 is fixed. |
547 &use_browser_overlays_); | 563 expected_pages_count_ = frame_->printBegin(web_print_params_, node_to_print_, |
| 564 NULL); |
| 565 } |
548 | 566 |
549 web_view_->settings()->setShouldPrintBackgrounds(should_print_backgrounds_); | 567 gfx::Size PrepareFrameAndViewForPrint::GetPrintCanvasSize() const { |
| 568 return gfx::Size(web_print_params_.printContentArea.width, |
| 569 web_print_params_.printContentArea.height); |
550 } | 570 } |
551 | 571 |
552 void PrepareFrameAndViewForPrint::FinishPrinting() { | 572 void PrepareFrameAndViewForPrint::FinishPrinting() { |
553 if (!finished_) { | 573 if (frame_) { |
554 finished_ = true; | |
555 frame_->printEnd(); | 574 frame_->printEnd(); |
556 web_view_->resize(prev_view_size_); | 575 WebKit::WebView* web_view = frame_->view(); |
557 if (WebKit::WebFrame* web_frame = web_view_->mainFrame()) | 576 web_view->resize(prev_view_size_); |
| 577 if (WebKit::WebFrame* web_frame = web_view->mainFrame()) |
558 web_frame->setScrollOffset(prev_scroll_offset_); | 578 web_frame->setScrollOffset(prev_scroll_offset_); |
559 web_view_->settings()->setShouldPrintBackgrounds(false); | 579 web_view->settings()->setShouldPrintBackgrounds(false); |
560 } | 580 } |
| 581 frame_ = NULL; |
561 } | 582 } |
562 | 583 |
563 PrintWebViewHelper::PrintWebViewHelper(content::RenderView* render_view) | 584 PrintWebViewHelper::PrintWebViewHelper(content::RenderView* render_view) |
564 : content::RenderViewObserver(render_view), | 585 : content::RenderViewObserver(render_view), |
565 content::RenderViewObserverTracker<PrintWebViewHelper>(render_view), | 586 content::RenderViewObserverTracker<PrintWebViewHelper>(render_view), |
566 print_web_view_(NULL), | 587 print_web_view_(NULL), |
567 is_preview_enabled_(IsPrintPreviewEnabled()), | 588 is_preview_enabled_(IsPrintPreviewEnabled()), |
568 is_scripted_print_throttling_disabled_(IsPrintThrottlingDisabled()), | 589 is_scripted_print_throttling_disabled_(IsPrintThrottlingDisabled()), |
569 is_print_ready_metafile_sent_(false), | 590 is_print_ready_metafile_sent_(false), |
570 ignore_css_margins_(false), | 591 ignore_css_margins_(false), |
(...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1002 | 1023 |
1003 print_node_in_progress_ = false; | 1024 print_node_in_progress_ = false; |
1004 } | 1025 } |
1005 | 1026 |
1006 void PrintWebViewHelper::Print(WebKit::WebFrame* frame, | 1027 void PrintWebViewHelper::Print(WebKit::WebFrame* frame, |
1007 const WebKit::WebNode& node) { | 1028 const WebKit::WebNode& node) { |
1008 // If still not finished with earlier print request simply ignore. | 1029 // If still not finished with earlier print request simply ignore. |
1009 if (print_web_view_) | 1030 if (print_web_view_) |
1010 return; | 1031 return; |
1011 | 1032 |
1012 // Initialize print settings. | 1033 int expected_page_count = 0; |
1013 scoped_ptr<PrepareFrameAndViewForPrint> prepare; | 1034 if (!CalculateNumberOfPages(frame, node, &expected_page_count)) { |
1014 if (!InitPrintSettingsAndPrepareFrame(frame, node, &prepare)) { | |
1015 DidFinishPrinting(FAIL_PRINT_INIT); | 1035 DidFinishPrinting(FAIL_PRINT_INIT); |
1016 return; // Failed to init print page settings. | 1036 return; // Failed to init print page settings. |
1017 } | 1037 } |
1018 | 1038 |
1019 int expected_page_count = 0; | |
1020 bool use_browser_overlays = true; | |
1021 | |
1022 expected_page_count = prepare->GetExpectedPageCount(); | |
1023 if (expected_page_count) | |
1024 use_browser_overlays = prepare->ShouldUseBrowserOverlays(); | |
1025 | |
1026 // Release the prepare before going any further, since we are going to | |
1027 // show UI and wait for the user. | |
1028 prepare.reset(); | |
1029 | |
1030 // Some full screen plugins can say they don't want to print. | 1039 // Some full screen plugins can say they don't want to print. |
1031 if (!expected_page_count) { | 1040 if (!expected_page_count) { |
1032 DidFinishPrinting(FAIL_PRINT); | 1041 DidFinishPrinting(FAIL_PRINT); |
1033 return; | 1042 return; |
1034 } | 1043 } |
1035 | 1044 |
1036 // Ask the browser to show UI to retrieve the final print settings. | 1045 // Ask the browser to show UI to retrieve the final print settings. |
1037 if (!GetPrintSettingsFromUser(frame, node, expected_page_count, | 1046 if (!GetPrintSettingsFromUser(frame, node, expected_page_count)) { |
1038 use_browser_overlays)) { | |
1039 DidFinishPrinting(OK); // Release resources and fail silently. | 1047 DidFinishPrinting(OK); // Release resources and fail silently. |
1040 return; | 1048 return; |
1041 } | 1049 } |
1042 | 1050 |
1043 // Render Pages for printing. | 1051 // Render Pages for printing. |
1044 if (!RenderPagesForPrint(frame, node)) { | 1052 if (!RenderPagesForPrint(frame, node)) { |
1045 LOG(ERROR) << "RenderPagesForPrint failed"; | 1053 LOG(ERROR) << "RenderPagesForPrint failed"; |
1046 DidFinishPrinting(FAIL_PRINT); | 1054 DidFinishPrinting(FAIL_PRINT); |
1047 } | 1055 } |
1048 ResetScriptedPrintCount(); | 1056 ResetScriptedPrintCount(); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1112 url_str.append(html); | 1120 url_str.append(html); |
1113 GURL url(url_str); | 1121 GURL url(url_str); |
1114 | 1122 |
1115 // When loading is done this will call didStopLoading() and that will do the | 1123 // When loading is done this will call didStopLoading() and that will do the |
1116 // actual printing. | 1124 // actual printing. |
1117 print_web_view_->mainFrame()->loadRequest(WebKit::WebURLRequest(url)); | 1125 print_web_view_->mainFrame()->loadRequest(WebKit::WebURLRequest(url)); |
1118 | 1126 |
1119 return true; | 1127 return true; |
1120 } | 1128 } |
1121 | 1129 |
1122 #if defined(OS_MACOSX) || defined(OS_WIN) | |
1123 bool PrintWebViewHelper::PrintPages(WebKit::WebFrame* frame, | 1130 bool PrintWebViewHelper::PrintPages(WebKit::WebFrame* frame, |
1124 const WebKit::WebNode& node) { | 1131 const WebKit::WebNode& node) { |
1125 const PrintMsg_PrintPages_Params& params = *print_pages_params_; | 1132 const PrintMsg_PrintPages_Params& params = *print_pages_params_; |
1126 const PrintMsg_Print_Params& print_params = params.params; | 1133 const PrintMsg_Print_Params& print_params = params.params; |
1127 PrepareFrameAndViewForPrint prep_frame_view(print_params, frame, node); | 1134 PrepareFrameAndViewForPrint prep_frame_view(print_params, frame, node, |
1128 UpdateFrameAndViewFromCssPageLayout(frame, node, &prep_frame_view, | 1135 ignore_css_margins_); |
1129 print_params, ignore_css_margins_); | 1136 prep_frame_view.StartPrinting(); |
1130 | 1137 |
1131 int page_count = prep_frame_view.GetExpectedPageCount(); | 1138 int page_count = prep_frame_view.GetExpectedPageCount(); |
1132 if (!page_count) | 1139 if (!page_count) |
1133 return false; | 1140 return false; |
| 1141 |
| 1142 #if !defined(OS_CHROMEOS) |
1134 // TODO(vitalybuka): should be page_count or valid pages from params.pages. | 1143 // TODO(vitalybuka): should be page_count or valid pages from params.pages. |
1135 // See http://crbug.com/161576 | 1144 // See http://crbug.com/161576 |
1136 Send(new PrintHostMsg_DidGetPrintedPagesCount(routing_id(), | 1145 Send(new PrintHostMsg_DidGetPrintedPagesCount(routing_id(), |
1137 print_params.document_cookie, | 1146 print_params.document_cookie, |
1138 page_count)); | 1147 page_count)); |
| 1148 #endif |
1139 | 1149 |
1140 const gfx::Size& canvas_size = prep_frame_view.GetPrintCanvasSize(); | 1150 return PrintPagesNative(frame, node, page_count, |
| 1151 prep_frame_view.GetPrintCanvasSize()); |
| 1152 } |
| 1153 |
| 1154 #if defined(OS_MACOSX) || defined(OS_WIN) |
| 1155 bool PrintWebViewHelper::PrintPagesNative(WebKit::WebFrame* frame, |
| 1156 const WebKit::WebNode& node, |
| 1157 int page_count, |
| 1158 const gfx::Size& canvas_size) { |
| 1159 const PrintMsg_PrintPages_Params& params = *print_pages_params_; |
| 1160 const PrintMsg_Print_Params& print_params = params.params; |
| 1161 |
1141 PrintMsg_PrintPage_Params page_params; | 1162 PrintMsg_PrintPage_Params page_params; |
1142 page_params.params = print_params; | 1163 page_params.params = print_params; |
1143 if (params.pages.empty()) { | 1164 if (params.pages.empty()) { |
1144 for (int i = 0; i < page_count; ++i) { | 1165 for (int i = 0; i < page_count; ++i) { |
1145 page_params.page_number = i; | 1166 page_params.page_number = i; |
1146 PrintPageInternal(page_params, canvas_size, frame); | 1167 PrintPageInternal(page_params, canvas_size, frame); |
1147 } | 1168 } |
1148 } else { | 1169 } else { |
1149 for (size_t i = 0; i < params.pages.size(); ++i) { | 1170 for (size_t i = 0; i < params.pages.size(); ++i) { |
1150 if (params.pages[i] >= page_count) | 1171 if (params.pages[i] >= page_count) |
1151 break; | 1172 break; |
1152 page_params.page_number = params.pages[i]; | 1173 page_params.page_number = params.pages[i]; |
1153 PrintPageInternal(page_params, canvas_size, frame); | 1174 PrintPageInternal(page_params, canvas_size, frame); |
1154 } | 1175 } |
1155 } | 1176 } |
1156 return true; | 1177 return true; |
1157 } | 1178 } |
| 1179 |
1158 #endif // OS_MACOSX || OS_WIN | 1180 #endif // OS_MACOSX || OS_WIN |
1159 | 1181 |
1160 void PrintWebViewHelper::didStopLoading() { | 1182 void PrintWebViewHelper::didStopLoading() { |
1161 PrintPages(print_web_view_->mainFrame(), WebKit::WebNode()); | 1183 PrintPages(print_web_view_->mainFrame(), WebKit::WebNode()); |
1162 } | 1184 } |
1163 | 1185 |
1164 // static - Not anonymous so that platform implementations can use it. | 1186 // static - Not anonymous so that platform implementations can use it. |
1165 void PrintWebViewHelper::ComputePageLayoutInPointsForCss( | 1187 void PrintWebViewHelper::ComputePageLayoutInPointsForCss( |
1166 WebKit::WebFrame* frame, | 1188 WebKit::WebFrame* frame, |
1167 int page_index, | 1189 int page_index, |
1168 const PrintMsg_Print_Params& page_params, | 1190 const PrintMsg_Print_Params& page_params, |
1169 bool ignore_css_margins, | 1191 bool ignore_css_margins, |
1170 double* scale_factor, | 1192 double* scale_factor, |
1171 PageSizeMargins* page_layout_in_points) { | 1193 PageSizeMargins* page_layout_in_points) { |
1172 PrintMsg_Print_Params params = CalculatePrintParamsForCss( | 1194 PrintMsg_Print_Params params = CalculatePrintParamsForCss( |
1173 frame, page_index, page_params, ignore_css_margins, | 1195 frame, page_index, page_params, ignore_css_margins, |
1174 page_params.print_scaling_option == | 1196 page_params.print_scaling_option == |
1175 WebKit::WebPrintScalingOptionFitToPrintableArea, | 1197 WebKit::WebPrintScalingOptionFitToPrintableArea, |
1176 scale_factor); | 1198 scale_factor); |
1177 CalculatePageLayoutFromPrintParams(params, page_layout_in_points); | 1199 CalculatePageLayoutFromPrintParams(params, page_layout_in_points); |
1178 } | 1200 } |
1179 | 1201 |
1180 // static - Not anonymous so that platform implementations can use it. | |
1181 void PrintWebViewHelper::UpdateFrameAndViewFromCssPageLayout( | |
1182 WebKit::WebFrame* frame, | |
1183 const WebKit::WebNode& node, | |
1184 PrepareFrameAndViewForPrint* prepare, | |
1185 const PrintMsg_Print_Params& params, | |
1186 bool ignore_css_margins) { | |
1187 if (PrintingNodeOrPdfFrame(frame, node)) | |
1188 return; | |
1189 bool fit_to_page = ignore_css_margins && | |
1190 params.print_scaling_option == | |
1191 WebKit::WebPrintScalingOptionFitToPrintableArea; | |
1192 PrintMsg_Print_Params print_params = CalculatePrintParamsForCss( | |
1193 frame, 0, params, ignore_css_margins, fit_to_page, NULL); | |
1194 prepare->UpdatePrintParams(print_params); | |
1195 } | |
1196 | |
1197 bool PrintWebViewHelper::InitPrintSettings(bool fit_to_paper_size) { | 1202 bool PrintWebViewHelper::InitPrintSettings(bool fit_to_paper_size) { |
1198 PrintMsg_PrintPages_Params settings; | 1203 PrintMsg_PrintPages_Params settings; |
1199 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(), | 1204 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(), |
1200 &settings.params)); | 1205 &settings.params)); |
1201 // Check if the printer returned any settings, if the settings is empty, we | 1206 // Check if the printer returned any settings, if the settings is empty, we |
1202 // can safely assume there are no printer drivers configured. So we safely | 1207 // can safely assume there are no printer drivers configured. So we safely |
1203 // terminate. | 1208 // terminate. |
1204 bool result = true; | 1209 bool result = true; |
1205 if (!PrintMsg_Print_Params_IsValid(settings.params)) | 1210 if (!PrintMsg_Print_Params_IsValid(settings.params)) |
1206 result = false; | 1211 result = false; |
(...skipping 13 matching lines...) Expand all Loading... |
1220 WebKit::WebPrintScalingOptionSourceSize; | 1225 WebKit::WebPrintScalingOptionSourceSize; |
1221 if (fit_to_paper_size) { | 1226 if (fit_to_paper_size) { |
1222 settings.params.print_scaling_option = | 1227 settings.params.print_scaling_option = |
1223 WebKit::WebPrintScalingOptionFitToPrintableArea; | 1228 WebKit::WebPrintScalingOptionFitToPrintableArea; |
1224 } | 1229 } |
1225 | 1230 |
1226 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); | 1231 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); |
1227 return result; | 1232 return result; |
1228 } | 1233 } |
1229 | 1234 |
1230 bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame( | 1235 bool PrintWebViewHelper::CalculateNumberOfPages(WebKit::WebFrame* frame, |
1231 WebKit::WebFrame* frame, | 1236 const WebKit::WebNode& node, |
1232 const WebKit::WebNode& node, | 1237 int* number_of_pages) { |
1233 scoped_ptr<PrepareFrameAndViewForPrint>* prepare) { | |
1234 DCHECK(frame); | 1238 DCHECK(frame); |
1235 | |
1236 bool fit_to_paper_size = !(PrintingNodeOrPdfFrame(frame, node)); | 1239 bool fit_to_paper_size = !(PrintingNodeOrPdfFrame(frame, node)); |
1237 if (!InitPrintSettings(fit_to_paper_size)) { | 1240 if (!InitPrintSettings(fit_to_paper_size)) { |
1238 notify_browser_of_print_failure_ = false; | 1241 notify_browser_of_print_failure_ = false; |
1239 render_view()->RunModalAlertDialog( | 1242 render_view()->RunModalAlertDialog( |
1240 frame, | 1243 frame, |
1241 l10n_util::GetStringUTF16(IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS)); | 1244 l10n_util::GetStringUTF16(IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS)); |
1242 return false; | 1245 return false; |
1243 } | 1246 } |
1244 | 1247 |
1245 DCHECK(!prepare->get()); | 1248 const PrintMsg_Print_Params& params = print_pages_params_->params; |
1246 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, | 1249 PrepareFrameAndViewForPrint prepare(params, frame, node, ignore_css_margins_); |
1247 frame, node)); | 1250 prepare.StartPrinting(); |
1248 UpdateFrameAndViewFromCssPageLayout(frame, node, prepare->get(), | 1251 |
1249 print_pages_params_->params, | 1252 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(), |
1250 ignore_css_margins_); | 1253 params.document_cookie)); |
1251 Send(new PrintHostMsg_DidGetDocumentCookie( | 1254 *number_of_pages = prepare.GetExpectedPageCount(); |
1252 routing_id(), print_pages_params_->params.document_cookie)); | |
1253 return true; | 1255 return true; |
1254 } | 1256 } |
1255 | 1257 |
1256 bool PrintWebViewHelper::UpdatePrintSettings( | 1258 bool PrintWebViewHelper::UpdatePrintSettings( |
1257 WebKit::WebFrame* frame, | 1259 WebKit::WebFrame* frame, |
1258 const WebKit::WebNode& node, | 1260 const WebKit::WebNode& node, |
1259 const DictionaryValue& passed_job_settings) { | 1261 const DictionaryValue& passed_job_settings) { |
1260 DCHECK(is_preview_enabled_); | 1262 DCHECK(is_preview_enabled_); |
1261 const DictionaryValue* job_settings = &passed_job_settings; | 1263 const DictionaryValue* job_settings = &passed_job_settings; |
1262 DictionaryValue modified_job_settings; | 1264 DictionaryValue modified_job_settings; |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1347 | 1349 |
1348 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); | 1350 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); |
1349 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(), | 1351 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(), |
1350 settings.params.document_cookie)); | 1352 settings.params.document_cookie)); |
1351 | 1353 |
1352 return true; | 1354 return true; |
1353 } | 1355 } |
1354 | 1356 |
1355 bool PrintWebViewHelper::GetPrintSettingsFromUser(WebKit::WebFrame* frame, | 1357 bool PrintWebViewHelper::GetPrintSettingsFromUser(WebKit::WebFrame* frame, |
1356 const WebKit::WebNode& node, | 1358 const WebKit::WebNode& node, |
1357 int expected_pages_count, | 1359 int expected_pages_count) { |
1358 bool use_browser_overlays) { | |
1359 PrintHostMsg_ScriptedPrint_Params params; | 1360 PrintHostMsg_ScriptedPrint_Params params; |
1360 PrintMsg_PrintPages_Params print_settings; | 1361 PrintMsg_PrintPages_Params print_settings; |
1361 | 1362 |
1362 params.cookie = print_pages_params_->params.document_cookie; | 1363 params.cookie = print_pages_params_->params.document_cookie; |
1363 params.has_selection = frame->hasSelection(); | 1364 params.has_selection = frame->hasSelection(); |
1364 params.expected_pages_count = expected_pages_count; | 1365 params.expected_pages_count = expected_pages_count; |
1365 MarginType margin_type = DEFAULT_MARGINS; | 1366 MarginType margin_type = DEFAULT_MARGINS; |
1366 if (PrintingNodeOrPdfFrame(frame, node)) | 1367 if (PrintingNodeOrPdfFrame(frame, node)) |
1367 margin_type = GetMarginsForPdf(frame, node); | 1368 margin_type = GetMarginsForPdf(frame, node); |
1368 params.margin_type = margin_type; | 1369 params.margin_type = margin_type; |
(...skipping 12 matching lines...) Expand all Loading... |
1381 Send(msg); | 1382 Send(msg); |
1382 print_pages_params_.reset(new PrintMsg_PrintPages_Params(print_settings)); | 1383 print_pages_params_.reset(new PrintMsg_PrintPages_Params(print_settings)); |
1383 | 1384 |
1384 print_pages_params_->params.print_scaling_option = scaling_option; | 1385 print_pages_params_->params.print_scaling_option = scaling_option; |
1385 return (print_settings.params.dpi && print_settings.params.document_cookie); | 1386 return (print_settings.params.dpi && print_settings.params.document_cookie); |
1386 } | 1387 } |
1387 | 1388 |
1388 bool PrintWebViewHelper::RenderPagesForPrint( | 1389 bool PrintWebViewHelper::RenderPagesForPrint( |
1389 WebKit::WebFrame* frame, | 1390 WebKit::WebFrame* frame, |
1390 const WebKit::WebNode& node) { | 1391 const WebKit::WebNode& node) { |
1391 if (print_pages_params_->params.selection_only) | 1392 if (print_pages_params_->params.selection_only) { |
| 1393 DCHECK(print_pages_params_->pages.empty()); |
1392 return CopyAndPrint(frame); | 1394 return CopyAndPrint(frame); |
| 1395 } |
1393 return PrintPages(frame, node); | 1396 return PrintPages(frame, node); |
1394 } | 1397 } |
1395 | 1398 |
1396 #if defined(OS_POSIX) | 1399 #if defined(OS_POSIX) |
1397 bool PrintWebViewHelper::CopyMetafileDataToSharedMem( | 1400 bool PrintWebViewHelper::CopyMetafileDataToSharedMem( |
1398 Metafile* metafile, | 1401 Metafile* metafile, |
1399 base::SharedMemoryHandle* shared_mem_handle) { | 1402 base::SharedMemoryHandle* shared_mem_handle) { |
1400 uint32 buf_size = metafile->GetDataSize(); | 1403 uint32 buf_size = metafile->GetDataSize(); |
1401 scoped_ptr<base::SharedMemory> shared_buf( | 1404 scoped_ptr<base::SharedMemory> shared_buf( |
1402 content::RenderThread::Get()->HostAllocateSharedMemoryBuffer( | 1405 content::RenderThread::Get()->HostAllocateSharedMemoryBuffer( |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1585 DCHECK_EQ(INITIALIZED, state_); | 1588 DCHECK_EQ(INITIALIZED, state_); |
1586 state_ = RENDERING; | 1589 state_ = RENDERING; |
1587 | 1590 |
1588 metafile_.reset(new PreviewMetafile); | 1591 metafile_.reset(new PreviewMetafile); |
1589 if (!metafile_->Init()) { | 1592 if (!metafile_->Init()) { |
1590 set_error(PREVIEW_ERROR_METAFILE_INIT_FAILED); | 1593 set_error(PREVIEW_ERROR_METAFILE_INIT_FAILED); |
1591 LOG(ERROR) << "PreviewMetafile Init failed"; | 1594 LOG(ERROR) << "PreviewMetafile Init failed"; |
1592 return false; | 1595 return false; |
1593 } | 1596 } |
1594 | 1597 |
1595 // Need to make sure old object gets destroyed first. | 1598 // Need to make sure old object gets destroyed first. |
1596 prep_frame_view_.reset(new PrepareFrameAndViewForPrint(print_params, frame(), | 1599 prep_frame_view_.reset( |
1597 node())); | 1600 new PrepareFrameAndViewForPrint(print_params, frame(), node(), |
1598 UpdateFrameAndViewFromCssPageLayout(frame_, node_, prep_frame_view_.get(), | 1601 ignore_css_margins)); |
1599 print_params, ignore_css_margins); | 1602 prep_frame_view_->StartPrinting(); |
1600 | 1603 |
1601 total_page_count_ = prep_frame_view_->GetExpectedPageCount(); | 1604 total_page_count_ = prep_frame_view_->GetExpectedPageCount(); |
1602 if (total_page_count_ == 0) { | 1605 if (total_page_count_ == 0) { |
1603 LOG(ERROR) << "CreatePreviewDocument got 0 page count"; | 1606 LOG(ERROR) << "CreatePreviewDocument got 0 page count"; |
1604 set_error(PREVIEW_ERROR_ZERO_PAGES); | 1607 set_error(PREVIEW_ERROR_ZERO_PAGES); |
1605 return false; | 1608 return false; |
1606 } | 1609 } |
1607 | 1610 |
1608 int selected_page_count = pages.size(); | 1611 int selected_page_count = pages.size(); |
1609 current_page_index_ = 0; | 1612 current_page_index_ = 0; |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1757 } | 1760 } |
1758 | 1761 |
1759 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { | 1762 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
1760 prep_frame_view_.reset(); | 1763 prep_frame_view_.reset(); |
1761 metafile_.reset(); | 1764 metafile_.reset(); |
1762 pages_to_render_.clear(); | 1765 pages_to_render_.clear(); |
1763 error_ = PREVIEW_ERROR_NONE; | 1766 error_ = PREVIEW_ERROR_NONE; |
1764 } | 1767 } |
1765 | 1768 |
1766 } // namespace printing | 1769 } // namespace printing |
OLD | NEW |