Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(70)

Side by Side Diff: chrome/renderer/printing/print_web_view_helper.cc

Issue 11953010: Cleanup of PrepareFrameAndViewForPrint code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 bool StartPrinting();
Lei Zhang 2013/01/23 08:31:38 Why return a bool if you never check it?
Vitaly Buka (NO REVIEWS) 2013/01/23 09:32:31 Done.
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::WebView* web_view_;
506 WebKit::WebPrintParams web_print_params_;
507 gfx::Size prev_view_size_;
508 gfx::Size prev_scroll_offset_;
509 int expected_pages_count_;
510 bool should_print_backgrounds_;
511
512 DISALLOW_COPY_AND_ASSIGN(PrepareFrameAndViewForPrint);
513 };
514
515
478 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( 516 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint(
479 const PrintMsg_Print_Params& print_params, 517 const PrintMsg_Print_Params& params,
480 WebKit::WebFrame* frame, 518 WebKit::WebFrame* frame,
481 const WebKit::WebNode& node) 519 const WebKit::WebNode& node,
482 : frame_(frame), 520 bool ignore_css_margins)
483 node_to_print_(node), 521 : frame_(frame),
484 web_view_(frame->view()), 522 node_to_print_(node),
485 expected_pages_count_(0), 523 web_view_(frame->view()),
486 use_browser_overlays_(true), 524 expected_pages_count_(0),
487 finished_(false), 525 should_print_backgrounds_(params.should_print_backgrounds) {
488 should_print_backgrounds_(print_params.should_print_backgrounds) { 526 PrintMsg_Print_Params print_params = params;
489 WebKit::WebPrintParams webkit_print_params;
490 ComputeWebKitPrintParamsInDesiredDpi(print_params, &webkit_print_params);
491 527
492 if (WebKit::WebFrame* web_frame = web_view_->mainFrame()) 528 if (WebKit::WebFrame* web_frame = web_view_->mainFrame())
493 prev_scroll_offset_ = web_frame->scrollOffset(); 529 prev_scroll_offset_ = web_frame->scrollOffset();
494 prev_view_size_ = web_view_->size(); 530 prev_view_size_ = web_view_->size();
495 531
496 StartPrinting(webkit_print_params); 532 if (!PrintingNodeOrPdfFrame(frame_, node_to_print_)) {
533 bool fit_to_page = ignore_css_margins &&
534 print_params.print_scaling_option ==
535 WebKit::WebPrintScalingOptionFitToPrintableArea;
536 print_params = CalculatePrintParamsForCss(frame_, 0, print_params,
537 ignore_css_margins, fit_to_page,
538 NULL);
539 }
540
541 ComputeWebKitPrintParamsInDesiredDpi(print_params, &web_print_params_);
497 } 542 }
498 543
499 PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() { 544 PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() {
500 FinishPrinting(); 545 FinishPrinting();
501 } 546 }
502 547
503 void PrepareFrameAndViewForPrint::UpdatePrintParams( 548 bool 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 549 // 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 550 // 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 551 // think the page is 125% larger so the size of the page is correct for
536 // minimum (default) scaling. 552 // minimum (default) scaling.
537 // This is important for sites that try to fill the page. 553 // This is important for sites that try to fill the page.
538 gfx::Size print_layout_size(web_print_params_.printContentArea.width, 554 gfx::Size print_layout_size(web_print_params_.printContentArea.width,
539 web_print_params_.printContentArea.height); 555 web_print_params_.printContentArea.height);
540 print_layout_size.set_height(static_cast<int>( 556 print_layout_size.set_height(static_cast<int>(
541 static_cast<double>(print_layout_size.height()) * 1.25)); 557 static_cast<double>(print_layout_size.height()) * 1.25));
542 558
543 web_view_->resize(print_layout_size); 559 web_view_->resize(print_layout_size);
560 web_view_->settings()->setShouldPrintBackgrounds(should_print_backgrounds_);
544 561
545 expected_pages_count_ = frame_->printBegin(web_print_params_, 562 expected_pages_count_ = frame_->printBegin(web_print_params_, node_to_print_,
546 node_to_print_, 563 NULL);
Lei Zhang 2013/01/23 08:31:38 |use_browser_overlays_| has been disconnected for
Vitaly Buka (NO REVIEWS) 2013/01/23 09:32:31 I am not assuming, just see that it's not used any
Lei Zhang 2013/01/23 19:50:24 Thanks. Please do that and add a TODO here referri
Vitaly Buka (NO REVIEWS) 2013/01/23 20:25:58 Done.
547 &use_browser_overlays_); 564 return expected_pages_count_ > 0;
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_)
Lei Zhang 2013/01/23 08:31:38 If you get rid of |finished_|, you may want to doc
Vitaly Buka (NO REVIEWS) 2013/01/23 09:32:31 Actually only frame is necessary. On 2013/01/23 0
554 finished_ = true;
555 frame_->printEnd(); 574 frame_->printEnd();
575 if (web_view_) {
556 web_view_->resize(prev_view_size_); 576 web_view_->resize(prev_view_size_);
557 if (WebKit::WebFrame* web_frame = web_view_->mainFrame()) 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;
582 web_view_ = NULL;
561 } 583 }
562 584
563 PrintWebViewHelper::PrintWebViewHelper(content::RenderView* render_view) 585 PrintWebViewHelper::PrintWebViewHelper(content::RenderView* render_view)
564 : content::RenderViewObserver(render_view), 586 : content::RenderViewObserver(render_view),
565 content::RenderViewObserverTracker<PrintWebViewHelper>(render_view), 587 content::RenderViewObserverTracker<PrintWebViewHelper>(render_view),
566 print_web_view_(NULL), 588 print_web_view_(NULL),
567 is_preview_enabled_(IsPrintPreviewEnabled()), 589 is_preview_enabled_(IsPrintPreviewEnabled()),
568 is_scripted_print_throttling_disabled_(IsPrintThrottlingDisabled()), 590 is_scripted_print_throttling_disabled_(IsPrintThrottlingDisabled()),
569 is_print_ready_metafile_sent_(false), 591 is_print_ready_metafile_sent_(false),
570 ignore_css_margins_(false), 592 ignore_css_margins_(false),
(...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after
1002 1024
1003 print_node_in_progress_ = false; 1025 print_node_in_progress_ = false;
1004 } 1026 }
1005 1027
1006 void PrintWebViewHelper::Print(WebKit::WebFrame* frame, 1028 void PrintWebViewHelper::Print(WebKit::WebFrame* frame,
1007 const WebKit::WebNode& node) { 1029 const WebKit::WebNode& node) {
1008 // If still not finished with earlier print request simply ignore. 1030 // If still not finished with earlier print request simply ignore.
1009 if (print_web_view_) 1031 if (print_web_view_)
1010 return; 1032 return;
1011 1033
1012 // Initialize print settings. 1034 int expected_page_count = 0;
1013 scoped_ptr<PrepareFrameAndViewForPrint> prepare; 1035 if (!CalculateNumberOfPages(frame, node, &expected_page_count)) {
1014 if (!InitPrintSettingsAndPrepareFrame(frame, node, &prepare)) {
1015 DidFinishPrinting(FAIL_PRINT_INIT); 1036 DidFinishPrinting(FAIL_PRINT_INIT);
1016 return; // Failed to init print page settings. 1037 return; // Failed to init print page settings.
1017 } 1038 }
1018 1039
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. 1040 // Some full screen plugins can say they don't want to print.
1031 if (!expected_page_count) { 1041 if (!expected_page_count) {
1032 DidFinishPrinting(FAIL_PRINT); 1042 DidFinishPrinting(FAIL_PRINT);
1033 return; 1043 return;
1034 } 1044 }
1035 1045
1036 // Ask the browser to show UI to retrieve the final print settings. 1046 // Ask the browser to show UI to retrieve the final print settings.
1037 if (!GetPrintSettingsFromUser(frame, node, expected_page_count, 1047 if (!GetPrintSettingsFromUser(frame, node, expected_page_count)) {
1038 use_browser_overlays)) {
1039 DidFinishPrinting(OK); // Release resources and fail silently. 1048 DidFinishPrinting(OK); // Release resources and fail silently.
1040 return; 1049 return;
1041 } 1050 }
1042 1051
1043 // Render Pages for printing. 1052 // Render Pages for printing.
1044 if (!RenderPagesForPrint(frame, node)) { 1053 if (!RenderPagesForPrint(frame, node)) {
1045 LOG(ERROR) << "RenderPagesForPrint failed"; 1054 LOG(ERROR) << "RenderPagesForPrint failed";
1046 DidFinishPrinting(FAIL_PRINT); 1055 DidFinishPrinting(FAIL_PRINT);
1047 } 1056 }
1048 ResetScriptedPrintCount(); 1057 ResetScriptedPrintCount();
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
1112 url_str.append(html); 1121 url_str.append(html);
1113 GURL url(url_str); 1122 GURL url(url_str);
1114 1123
1115 // When loading is done this will call didStopLoading() and that will do the 1124 // When loading is done this will call didStopLoading() and that will do the
1116 // actual printing. 1125 // actual printing.
1117 print_web_view_->mainFrame()->loadRequest(WebKit::WebURLRequest(url)); 1126 print_web_view_->mainFrame()->loadRequest(WebKit::WebURLRequest(url));
1118 1127
1119 return true; 1128 return true;
1120 } 1129 }
1121 1130
1122 #if defined(OS_MACOSX) || defined(OS_WIN)
1123 bool PrintWebViewHelper::PrintPages(WebKit::WebFrame* frame, 1131 bool PrintWebViewHelper::PrintPages(WebKit::WebFrame* frame,
1124 const WebKit::WebNode& node) { 1132 const WebKit::WebNode& node) {
1125 const PrintMsg_PrintPages_Params& params = *print_pages_params_; 1133 const PrintMsg_PrintPages_Params& params = *print_pages_params_;
1126 const PrintMsg_Print_Params& print_params = params.params; 1134 const PrintMsg_Print_Params& print_params = params.params;
1127 PrepareFrameAndViewForPrint prep_frame_view(print_params, frame, node); 1135 PrepareFrameAndViewForPrint prep_frame_view(print_params, frame, node,
1128 UpdateFrameAndViewFromCssPageLayout(frame, node, &prep_frame_view, 1136 ignore_css_margins_);
1129 print_params, ignore_css_margins_); 1137 prep_frame_view.StartPrinting();
1130 1138
1131 int page_count = prep_frame_view.GetExpectedPageCount(); 1139 int page_count = prep_frame_view.GetExpectedPageCount();
1132 if (!page_count) 1140 if (!page_count)
1133 return false; 1141 return false;
1142
1143 #if !defined(OS_CHROMEOS)
1134 // TODO(vitalybuka): should be page_count or valid pages from params.pages. 1144 // TODO(vitalybuka): should be page_count or valid pages from params.pages.
1135 // See http://crbug.com/161576 1145 // See http://crbug.com/161576
1136 Send(new PrintHostMsg_DidGetPrintedPagesCount(routing_id(), 1146 Send(new PrintHostMsg_DidGetPrintedPagesCount(routing_id(),
1137 print_params.document_cookie, 1147 print_params.document_cookie,
1138 page_count)); 1148 page_count));
1149 #endif
1139 1150
1140 const gfx::Size& canvas_size = prep_frame_view.GetPrintCanvasSize(); 1151 return PrintPagesNative(frame, node, page_count,
1152 prep_frame_view.GetPrintCanvasSize());
1153 }
1154
1155 #if defined(OS_MACOSX) || defined(OS_WIN)
1156 bool PrintWebViewHelper::PrintPagesNative(WebKit::WebFrame* frame,
1157 const WebKit::WebNode& node,
1158 int page_count,
1159 const gfx::Size& canvas_size) {
1160 const PrintMsg_PrintPages_Params& params = *print_pages_params_;
1161 const PrintMsg_Print_Params& print_params = params.params;
1162
1141 PrintMsg_PrintPage_Params page_params; 1163 PrintMsg_PrintPage_Params page_params;
1142 page_params.params = print_params; 1164 page_params.params = print_params;
1143 if (params.pages.empty()) { 1165 if (params.pages.empty()) {
1144 for (int i = 0; i < page_count; ++i) { 1166 for (int i = 0; i < page_count; ++i) {
1145 page_params.page_number = i; 1167 page_params.page_number = i;
1146 PrintPageInternal(page_params, canvas_size, frame); 1168 PrintPageInternal(page_params, canvas_size, frame);
1147 } 1169 }
1148 } else { 1170 } else {
1149 for (size_t i = 0; i < params.pages.size(); ++i) { 1171 for (size_t i = 0; i < params.pages.size(); ++i) {
1150 if (params.pages[i] >= page_count) 1172 if (params.pages[i] >= page_count)
1151 break; 1173 break;
1152 page_params.page_number = params.pages[i]; 1174 page_params.page_number = params.pages[i];
1153 PrintPageInternal(page_params, canvas_size, frame); 1175 PrintPageInternal(page_params, canvas_size, frame);
1154 } 1176 }
1155 } 1177 }
1156 return true; 1178 return true;
1157 } 1179 }
1180
1158 #endif // OS_MACOSX || OS_WIN 1181 #endif // OS_MACOSX || OS_WIN
1159 1182
1160 void PrintWebViewHelper::didStopLoading() { 1183 void PrintWebViewHelper::didStopLoading() {
1161 PrintPages(print_web_view_->mainFrame(), WebKit::WebNode()); 1184 PrintPages(print_web_view_->mainFrame(), WebKit::WebNode());
1162 } 1185 }
1163 1186
1164 // static - Not anonymous so that platform implementations can use it. 1187 // static - Not anonymous so that platform implementations can use it.
1165 void PrintWebViewHelper::ComputePageLayoutInPointsForCss( 1188 void PrintWebViewHelper::ComputePageLayoutInPointsForCss(
1166 WebKit::WebFrame* frame, 1189 WebKit::WebFrame* frame,
1167 int page_index, 1190 int page_index,
1168 const PrintMsg_Print_Params& page_params, 1191 const PrintMsg_Print_Params& page_params,
1169 bool ignore_css_margins, 1192 bool ignore_css_margins,
1170 double* scale_factor, 1193 double* scale_factor,
1171 PageSizeMargins* page_layout_in_points) { 1194 PageSizeMargins* page_layout_in_points) {
1172 PrintMsg_Print_Params params = CalculatePrintParamsForCss( 1195 PrintMsg_Print_Params params = CalculatePrintParamsForCss(
1173 frame, page_index, page_params, ignore_css_margins, 1196 frame, page_index, page_params, ignore_css_margins,
1174 page_params.print_scaling_option == 1197 page_params.print_scaling_option ==
1175 WebKit::WebPrintScalingOptionFitToPrintableArea, 1198 WebKit::WebPrintScalingOptionFitToPrintableArea,
1176 scale_factor); 1199 scale_factor);
1177 CalculatePageLayoutFromPrintParams(params, page_layout_in_points); 1200 CalculatePageLayoutFromPrintParams(params, page_layout_in_points);
1178 } 1201 }
1179 1202
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) { 1203 bool PrintWebViewHelper::InitPrintSettings(bool fit_to_paper_size) {
1198 PrintMsg_PrintPages_Params settings; 1204 PrintMsg_PrintPages_Params settings;
1199 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(), 1205 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(),
1200 &settings.params)); 1206 &settings.params));
1201 // Check if the printer returned any settings, if the settings is empty, we 1207 // 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 1208 // can safely assume there are no printer drivers configured. So we safely
1203 // terminate. 1209 // terminate.
1204 bool result = true; 1210 bool result = true;
1205 if (!PrintMsg_Print_Params_IsValid(settings.params)) 1211 if (!PrintMsg_Print_Params_IsValid(settings.params))
1206 result = false; 1212 result = false;
(...skipping 13 matching lines...) Expand all
1220 WebKit::WebPrintScalingOptionSourceSize; 1226 WebKit::WebPrintScalingOptionSourceSize;
1221 if (fit_to_paper_size) { 1227 if (fit_to_paper_size) {
1222 settings.params.print_scaling_option = 1228 settings.params.print_scaling_option =
1223 WebKit::WebPrintScalingOptionFitToPrintableArea; 1229 WebKit::WebPrintScalingOptionFitToPrintableArea;
1224 } 1230 }
1225 1231
1226 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); 1232 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings));
1227 return result; 1233 return result;
1228 } 1234 }
1229 1235
1230 bool PrintWebViewHelper::InitPrintSettingsAndPrepareFrame( 1236 bool PrintWebViewHelper::CalculateNumberOfPages(WebKit::WebFrame* frame,
1231 WebKit::WebFrame* frame, 1237 const WebKit::WebNode& node,
1232 const WebKit::WebNode& node, 1238 int* number_of_pages) {
1233 scoped_ptr<PrepareFrameAndViewForPrint>* prepare) {
1234 DCHECK(frame); 1239 DCHECK(frame);
1235 1240
1236 bool fit_to_paper_size = !(PrintingNodeOrPdfFrame(frame, node)); 1241 bool fit_to_paper_size = !(PrintingNodeOrPdfFrame(frame, node));
1237 if (!InitPrintSettings(fit_to_paper_size)) { 1242 if (!InitPrintSettings(fit_to_paper_size)) {
1238 notify_browser_of_print_failure_ = false; 1243 notify_browser_of_print_failure_ = false;
1239 render_view()->RunModalAlertDialog( 1244 render_view()->RunModalAlertDialog(
1240 frame, 1245 frame,
1241 l10n_util::GetStringUTF16(IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS)); 1246 l10n_util::GetStringUTF16(IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS));
1242 return false; 1247 return false;
1243 } 1248 }
1244 1249
1245 DCHECK(!prepare->get()); 1250 PrepareFrameAndViewForPrint prepare(print_pages_params_->params, frame, node,
1246 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params,
1247 frame, node));
1248 UpdateFrameAndViewFromCssPageLayout(frame, node, prepare->get(),
1249 print_pages_params_->params,
1250 ignore_css_margins_); 1251 ignore_css_margins_);
1252 prepare.StartPrinting();
1251 Send(new PrintHostMsg_DidGetDocumentCookie( 1253 Send(new PrintHostMsg_DidGetDocumentCookie(
1252 routing_id(), print_pages_params_->params.document_cookie)); 1254 routing_id(), print_pages_params_->params.document_cookie));
1255 *number_of_pages = prepare.GetExpectedPageCount();
1253 return true; 1256 return true;
1254 } 1257 }
1255 1258
1256 bool PrintWebViewHelper::UpdatePrintSettings( 1259 bool PrintWebViewHelper::UpdatePrintSettings(
1257 WebKit::WebFrame* frame, 1260 WebKit::WebFrame* frame,
1258 const WebKit::WebNode& node, 1261 const WebKit::WebNode& node,
1259 const DictionaryValue& passed_job_settings) { 1262 const DictionaryValue& passed_job_settings) {
1260 DCHECK(is_preview_enabled_); 1263 DCHECK(is_preview_enabled_);
1261 const DictionaryValue* job_settings = &passed_job_settings; 1264 const DictionaryValue* job_settings = &passed_job_settings;
1262 DictionaryValue modified_job_settings; 1265 DictionaryValue modified_job_settings;
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
1347 1350
1348 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); 1351 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings));
1349 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(), 1352 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(),
1350 settings.params.document_cookie)); 1353 settings.params.document_cookie));
1351 1354
1352 return true; 1355 return true;
1353 } 1356 }
1354 1357
1355 bool PrintWebViewHelper::GetPrintSettingsFromUser(WebKit::WebFrame* frame, 1358 bool PrintWebViewHelper::GetPrintSettingsFromUser(WebKit::WebFrame* frame,
1356 const WebKit::WebNode& node, 1359 const WebKit::WebNode& node,
1357 int expected_pages_count, 1360 int expected_pages_count) {
1358 bool use_browser_overlays) {
1359 PrintHostMsg_ScriptedPrint_Params params; 1361 PrintHostMsg_ScriptedPrint_Params params;
1360 PrintMsg_PrintPages_Params print_settings; 1362 PrintMsg_PrintPages_Params print_settings;
1361 1363
1362 params.cookie = print_pages_params_->params.document_cookie; 1364 params.cookie = print_pages_params_->params.document_cookie;
1363 params.has_selection = frame->hasSelection(); 1365 params.has_selection = frame->hasSelection();
1364 params.expected_pages_count = expected_pages_count; 1366 params.expected_pages_count = expected_pages_count;
1365 MarginType margin_type = DEFAULT_MARGINS; 1367 MarginType margin_type = DEFAULT_MARGINS;
1366 if (PrintingNodeOrPdfFrame(frame, node)) 1368 if (PrintingNodeOrPdfFrame(frame, node))
1367 margin_type = GetMarginsForPdf(frame, node); 1369 margin_type = GetMarginsForPdf(frame, node);
1368 params.margin_type = margin_type; 1370 params.margin_type = margin_type;
(...skipping 12 matching lines...) Expand all
1381 Send(msg); 1383 Send(msg);
1382 print_pages_params_.reset(new PrintMsg_PrintPages_Params(print_settings)); 1384 print_pages_params_.reset(new PrintMsg_PrintPages_Params(print_settings));
1383 1385
1384 print_pages_params_->params.print_scaling_option = scaling_option; 1386 print_pages_params_->params.print_scaling_option = scaling_option;
1385 return (print_settings.params.dpi && print_settings.params.document_cookie); 1387 return (print_settings.params.dpi && print_settings.params.document_cookie);
1386 } 1388 }
1387 1389
1388 bool PrintWebViewHelper::RenderPagesForPrint( 1390 bool PrintWebViewHelper::RenderPagesForPrint(
1389 WebKit::WebFrame* frame, 1391 WebKit::WebFrame* frame,
1390 const WebKit::WebNode& node) { 1392 const WebKit::WebNode& node) {
1391 if (print_pages_params_->params.selection_only) 1393 if (print_pages_params_->params.selection_only) {
1394 DCHECK(print_pages_params_->pages.empty());
1392 return CopyAndPrint(frame); 1395 return CopyAndPrint(frame);
1396 }
1393 return PrintPages(frame, node); 1397 return PrintPages(frame, node);
1394 } 1398 }
1395 1399
1396 #if defined(OS_POSIX) 1400 #if defined(OS_POSIX)
1397 bool PrintWebViewHelper::CopyMetafileDataToSharedMem( 1401 bool PrintWebViewHelper::CopyMetafileDataToSharedMem(
1398 Metafile* metafile, 1402 Metafile* metafile,
1399 base::SharedMemoryHandle* shared_mem_handle) { 1403 base::SharedMemoryHandle* shared_mem_handle) {
1400 uint32 buf_size = metafile->GetDataSize(); 1404 uint32 buf_size = metafile->GetDataSize();
1401 scoped_ptr<base::SharedMemory> shared_buf( 1405 scoped_ptr<base::SharedMemory> shared_buf(
1402 content::RenderThread::Get()->HostAllocateSharedMemoryBuffer( 1406 content::RenderThread::Get()->HostAllocateSharedMemoryBuffer(
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
1585 DCHECK_EQ(INITIALIZED, state_); 1589 DCHECK_EQ(INITIALIZED, state_);
1586 state_ = RENDERING; 1590 state_ = RENDERING;
1587 1591
1588 metafile_.reset(new PreviewMetafile); 1592 metafile_.reset(new PreviewMetafile);
1589 if (!metafile_->Init()) { 1593 if (!metafile_->Init()) {
1590 set_error(PREVIEW_ERROR_METAFILE_INIT_FAILED); 1594 set_error(PREVIEW_ERROR_METAFILE_INIT_FAILED);
1591 LOG(ERROR) << "PreviewMetafile Init failed"; 1595 LOG(ERROR) << "PreviewMetafile Init failed";
1592 return false; 1596 return false;
1593 } 1597 }
1594 1598
1595 // Need to make sure old object gets destroyed first. 1599 // Need to make sure old object gets destroyed first.
1596 prep_frame_view_.reset(new PrepareFrameAndViewForPrint(print_params, frame(), 1600 prep_frame_view_.reset(
1597 node())); 1601 new PrepareFrameAndViewForPrint(print_params, frame(), node(),
1598 UpdateFrameAndViewFromCssPageLayout(frame_, node_, prep_frame_view_.get(), 1602 ignore_css_margins));
1599 print_params, ignore_css_margins); 1603 prep_frame_view_->StartPrinting();
1600 1604
1601 total_page_count_ = prep_frame_view_->GetExpectedPageCount(); 1605 total_page_count_ = prep_frame_view_->GetExpectedPageCount();
1602 if (total_page_count_ == 0) { 1606 if (total_page_count_ == 0) {
1603 LOG(ERROR) << "CreatePreviewDocument got 0 page count"; 1607 LOG(ERROR) << "CreatePreviewDocument got 0 page count";
1604 set_error(PREVIEW_ERROR_ZERO_PAGES); 1608 set_error(PREVIEW_ERROR_ZERO_PAGES);
1605 return false; 1609 return false;
1606 } 1610 }
1607 1611
1608 int selected_page_count = pages.size(); 1612 int selected_page_count = pages.size();
1609 current_page_index_ = 0; 1613 current_page_index_ = 0;
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
1757 } 1761 }
1758 1762
1759 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { 1763 void PrintWebViewHelper::PrintPreviewContext::ClearContext() {
1760 prep_frame_view_.reset(); 1764 prep_frame_view_.reset();
1761 metafile_.reset(); 1765 metafile_.reset();
1762 pages_to_render_.clear(); 1766 pages_to_render_.clear();
1763 error_ = PREVIEW_ERROR_NONE; 1767 error_ = PREVIEW_ERROR_NONE;
1764 } 1768 }
1765 1769
1766 } // namespace printing 1770 } // namespace printing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698