| 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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 PREVIEW_EVENT_MAX, | 61 PREVIEW_EVENT_MAX, |
| 62 }; | 62 }; |
| 63 | 63 |
| 64 const double kMinDpi = 1.0; | 64 const double kMinDpi = 1.0; |
| 65 | 65 |
| 66 const char kPageLoadScriptFormat[] = | 66 const char kPageLoadScriptFormat[] = |
| 67 "document.open(); document.write(%s); document.close();"; | 67 "document.open(); document.write(%s); document.close();"; |
| 68 | 68 |
| 69 const char kPageSetupScriptFormat[] = "setup(%s);"; | 69 const char kPageSetupScriptFormat[] = "setup(%s);"; |
| 70 | 70 |
| 71 void ExecuteScript(WebKit::WebFrame* frame, | 71 void ExecuteScript(blink::WebFrame* frame, |
| 72 const char* script_format, | 72 const char* script_format, |
| 73 const base::Value& parameters) { | 73 const base::Value& parameters) { |
| 74 std::string json; | 74 std::string json; |
| 75 base::JSONWriter::Write(¶meters, &json); | 75 base::JSONWriter::Write(¶meters, &json); |
| 76 std::string script = base::StringPrintf(script_format, json.c_str()); | 76 std::string script = base::StringPrintf(script_format, json.c_str()); |
| 77 frame->executeScript(WebKit::WebString(UTF8ToUTF16(script))); | 77 frame->executeScript(blink::WebString(UTF8ToUTF16(script))); |
| 78 } | 78 } |
| 79 | 79 |
| 80 int GetDPI(const PrintMsg_Print_Params* print_params) { | 80 int GetDPI(const PrintMsg_Print_Params* print_params) { |
| 81 #if defined(OS_MACOSX) | 81 #if defined(OS_MACOSX) |
| 82 // On the Mac, the printable area is in points, don't do any scaling based | 82 // On the Mac, the printable area is in points, don't do any scaling based |
| 83 // on dpi. | 83 // on dpi. |
| 84 return kPointsPerInch; | 84 return kPointsPerInch; |
| 85 #else | 85 #else |
| 86 return static_cast<int>(print_params->dpi); | 86 return static_cast<int>(print_params->dpi); |
| 87 #endif // defined(OS_MACOSX) | 87 #endif // defined(OS_MACOSX) |
| 88 } | 88 } |
| 89 | 89 |
| 90 bool PrintMsg_Print_Params_IsValid(const PrintMsg_Print_Params& params) { | 90 bool PrintMsg_Print_Params_IsValid(const PrintMsg_Print_Params& params) { |
| 91 return !params.content_size.IsEmpty() && !params.page_size.IsEmpty() && | 91 return !params.content_size.IsEmpty() && !params.page_size.IsEmpty() && |
| 92 !params.printable_area.IsEmpty() && params.document_cookie && | 92 !params.printable_area.IsEmpty() && params.document_cookie && |
| 93 params.desired_dpi && params.max_shrink && params.min_shrink && | 93 params.desired_dpi && params.max_shrink && params.min_shrink && |
| 94 params.dpi && (params.margin_top >= 0) && (params.margin_left >= 0); | 94 params.dpi && (params.margin_top >= 0) && (params.margin_left >= 0); |
| 95 } | 95 } |
| 96 | 96 |
| 97 PrintMsg_Print_Params GetCssPrintParams( | 97 PrintMsg_Print_Params GetCssPrintParams( |
| 98 WebKit::WebFrame* frame, | 98 blink::WebFrame* frame, |
| 99 int page_index, | 99 int page_index, |
| 100 const PrintMsg_Print_Params& page_params) { | 100 const PrintMsg_Print_Params& page_params) { |
| 101 PrintMsg_Print_Params page_css_params = page_params; | 101 PrintMsg_Print_Params page_css_params = page_params; |
| 102 int dpi = GetDPI(&page_params); | 102 int dpi = GetDPI(&page_params); |
| 103 | 103 |
| 104 WebKit::WebSize page_size_in_pixels( | 104 blink::WebSize page_size_in_pixels( |
| 105 ConvertUnit(page_params.page_size.width(), dpi, kPixelsPerInch), | 105 ConvertUnit(page_params.page_size.width(), dpi, kPixelsPerInch), |
| 106 ConvertUnit(page_params.page_size.height(), dpi, kPixelsPerInch)); | 106 ConvertUnit(page_params.page_size.height(), dpi, kPixelsPerInch)); |
| 107 int margin_top_in_pixels = | 107 int margin_top_in_pixels = |
| 108 ConvertUnit(page_params.margin_top, dpi, kPixelsPerInch); | 108 ConvertUnit(page_params.margin_top, dpi, kPixelsPerInch); |
| 109 int margin_right_in_pixels = ConvertUnit( | 109 int margin_right_in_pixels = ConvertUnit( |
| 110 page_params.page_size.width() - | 110 page_params.page_size.width() - |
| 111 page_params.content_size.width() - page_params.margin_left, | 111 page_params.content_size.width() - page_params.margin_left, |
| 112 dpi, kPixelsPerInch); | 112 dpi, kPixelsPerInch); |
| 113 int margin_bottom_in_pixels = ConvertUnit( | 113 int margin_bottom_in_pixels = ConvertUnit( |
| 114 page_params.page_size.height() - | 114 page_params.page_size.height() - |
| 115 page_params.content_size.height() - page_params.margin_top, | 115 page_params.content_size.height() - page_params.margin_top, |
| 116 dpi, kPixelsPerInch); | 116 dpi, kPixelsPerInch); |
| 117 int margin_left_in_pixels = ConvertUnit( | 117 int margin_left_in_pixels = ConvertUnit( |
| 118 page_params.margin_left, | 118 page_params.margin_left, |
| 119 dpi, kPixelsPerInch); | 119 dpi, kPixelsPerInch); |
| 120 | 120 |
| 121 WebKit::WebSize original_page_size_in_pixels = page_size_in_pixels; | 121 blink::WebSize original_page_size_in_pixels = page_size_in_pixels; |
| 122 | 122 |
| 123 if (frame) { | 123 if (frame) { |
| 124 frame->pageSizeAndMarginsInPixels(page_index, | 124 frame->pageSizeAndMarginsInPixels(page_index, |
| 125 page_size_in_pixels, | 125 page_size_in_pixels, |
| 126 margin_top_in_pixels, | 126 margin_top_in_pixels, |
| 127 margin_right_in_pixels, | 127 margin_right_in_pixels, |
| 128 margin_bottom_in_pixels, | 128 margin_bottom_in_pixels, |
| 129 margin_left_in_pixels); | 129 margin_left_in_pixels); |
| 130 } | 130 } |
| 131 | 131 |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 page_params->page_size.width()); | 238 page_params->page_size.width()); |
| 239 page_params->content_size.SetSize(page_params->content_size.height(), | 239 page_params->content_size.SetSize(page_params->content_size.height(), |
| 240 page_params->content_size.width()); | 240 page_params->content_size.width()); |
| 241 page_params->printable_area.set_size( | 241 page_params->printable_area.set_size( |
| 242 gfx::Size(page_params->printable_area.height(), | 242 gfx::Size(page_params->printable_area.height(), |
| 243 page_params->printable_area.width())); | 243 page_params->printable_area.width())); |
| 244 } | 244 } |
| 245 | 245 |
| 246 void ComputeWebKitPrintParamsInDesiredDpi( | 246 void ComputeWebKitPrintParamsInDesiredDpi( |
| 247 const PrintMsg_Print_Params& print_params, | 247 const PrintMsg_Print_Params& print_params, |
| 248 WebKit::WebPrintParams* webkit_print_params) { | 248 blink::WebPrintParams* webkit_print_params) { |
| 249 int dpi = GetDPI(&print_params); | 249 int dpi = GetDPI(&print_params); |
| 250 webkit_print_params->printerDPI = dpi; | 250 webkit_print_params->printerDPI = dpi; |
| 251 webkit_print_params->printScalingOption = print_params.print_scaling_option; | 251 webkit_print_params->printScalingOption = print_params.print_scaling_option; |
| 252 | 252 |
| 253 webkit_print_params->printContentArea.width = | 253 webkit_print_params->printContentArea.width = |
| 254 ConvertUnit(print_params.content_size.width(), dpi, | 254 ConvertUnit(print_params.content_size.width(), dpi, |
| 255 print_params.desired_dpi); | 255 print_params.desired_dpi); |
| 256 webkit_print_params->printContentArea.height = | 256 webkit_print_params->printContentArea.height = |
| 257 ConvertUnit(print_params.content_size.height(), dpi, | 257 ConvertUnit(print_params.content_size.height(), dpi, |
| 258 print_params.desired_dpi); | 258 print_params.desired_dpi); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 271 dpi, print_params.desired_dpi); | 271 dpi, print_params.desired_dpi); |
| 272 | 272 |
| 273 webkit_print_params->paperSize.width = | 273 webkit_print_params->paperSize.width = |
| 274 ConvertUnit(print_params.page_size.width(), dpi, | 274 ConvertUnit(print_params.page_size.width(), dpi, |
| 275 print_params.desired_dpi); | 275 print_params.desired_dpi); |
| 276 webkit_print_params->paperSize.height = | 276 webkit_print_params->paperSize.height = |
| 277 ConvertUnit(print_params.page_size.height(), dpi, | 277 ConvertUnit(print_params.page_size.height(), dpi, |
| 278 print_params.desired_dpi); | 278 print_params.desired_dpi); |
| 279 } | 279 } |
| 280 | 280 |
| 281 WebKit::WebPlugin* GetPlugin(const WebKit::WebFrame* frame) { | 281 blink::WebPlugin* GetPlugin(const blink::WebFrame* frame) { |
| 282 return frame->document().isPluginDocument() ? | 282 return frame->document().isPluginDocument() ? |
| 283 frame->document().to<WebKit::WebPluginDocument>().plugin() : NULL; | 283 frame->document().to<blink::WebPluginDocument>().plugin() : NULL; |
| 284 } | 284 } |
| 285 | 285 |
| 286 bool PrintingNodeOrPdfFrame(const WebKit::WebFrame* frame, | 286 bool PrintingNodeOrPdfFrame(const blink::WebFrame* frame, |
| 287 const WebKit::WebNode& node) { | 287 const blink::WebNode& node) { |
| 288 if (!node.isNull()) | 288 if (!node.isNull()) |
| 289 return true; | 289 return true; |
| 290 WebKit::WebPlugin* plugin = GetPlugin(frame); | 290 blink::WebPlugin* plugin = GetPlugin(frame); |
| 291 return plugin && plugin->supportsPaginatedPrint(); | 291 return plugin && plugin->supportsPaginatedPrint(); |
| 292 } | 292 } |
| 293 | 293 |
| 294 bool PrintingFrameHasPageSizeStyle(WebKit::WebFrame* frame, | 294 bool PrintingFrameHasPageSizeStyle(blink::WebFrame* frame, |
| 295 int total_page_count) { | 295 int total_page_count) { |
| 296 if (!frame) | 296 if (!frame) |
| 297 return false; | 297 return false; |
| 298 bool frame_has_custom_page_size_style = false; | 298 bool frame_has_custom_page_size_style = false; |
| 299 for (int i = 0; i < total_page_count; ++i) { | 299 for (int i = 0; i < total_page_count; ++i) { |
| 300 if (frame->hasCustomPageSizeStyle(i)) { | 300 if (frame->hasCustomPageSizeStyle(i)) { |
| 301 frame_has_custom_page_size_style = true; | 301 frame_has_custom_page_size_style = true; |
| 302 break; | 302 break; |
| 303 } | 303 } |
| 304 } | 304 } |
| 305 return frame_has_custom_page_size_style; | 305 return frame_has_custom_page_size_style; |
| 306 } | 306 } |
| 307 | 307 |
| 308 MarginType GetMarginsForPdf(WebKit::WebFrame* frame, | 308 MarginType GetMarginsForPdf(blink::WebFrame* frame, |
| 309 const WebKit::WebNode& node) { | 309 const blink::WebNode& node) { |
| 310 if (frame->isPrintScalingDisabledForPlugin(node)) | 310 if (frame->isPrintScalingDisabledForPlugin(node)) |
| 311 return NO_MARGINS; | 311 return NO_MARGINS; |
| 312 else | 312 else |
| 313 return PRINTABLE_AREA_MARGINS; | 313 return PRINTABLE_AREA_MARGINS; |
| 314 } | 314 } |
| 315 | 315 |
| 316 bool FitToPageEnabled(const base::DictionaryValue& job_settings) { | 316 bool FitToPageEnabled(const base::DictionaryValue& job_settings) { |
| 317 bool fit_to_paper_size = false; | 317 bool fit_to_paper_size = false; |
| 318 if (!job_settings.GetBoolean(kSettingFitToPageEnabled, &fit_to_paper_size)) { | 318 if (!job_settings.GetBoolean(kSettingFitToPageEnabled, &fit_to_paper_size)) { |
| 319 NOTREACHED(); | 319 NOTREACHED(); |
| 320 } | 320 } |
| 321 return fit_to_paper_size; | 321 return fit_to_paper_size; |
| 322 } | 322 } |
| 323 | 323 |
| 324 PrintMsg_Print_Params CalculatePrintParamsForCss( | 324 PrintMsg_Print_Params CalculatePrintParamsForCss( |
| 325 WebKit::WebFrame* frame, | 325 blink::WebFrame* frame, |
| 326 int page_index, | 326 int page_index, |
| 327 const PrintMsg_Print_Params& page_params, | 327 const PrintMsg_Print_Params& page_params, |
| 328 bool ignore_css_margins, | 328 bool ignore_css_margins, |
| 329 bool fit_to_page, | 329 bool fit_to_page, |
| 330 double* scale_factor) { | 330 double* scale_factor) { |
| 331 PrintMsg_Print_Params css_params = GetCssPrintParams(frame, page_index, | 331 PrintMsg_Print_Params css_params = GetCssPrintParams(frame, page_index, |
| 332 page_params); | 332 page_params); |
| 333 | 333 |
| 334 PrintMsg_Print_Params params = page_params; | 334 PrintMsg_Print_Params params = page_params; |
| 335 EnsureOrientationMatches(css_params, ¶ms); | 335 EnsureOrientationMatches(css_params, ¶ms); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 369 switches::kRendererPrintPreview); | 369 switches::kRendererPrintPreview); |
| 370 } | 370 } |
| 371 | 371 |
| 372 bool IsPrintThrottlingDisabled() { | 372 bool IsPrintThrottlingDisabled() { |
| 373 return CommandLine::ForCurrentProcess()->HasSwitch( | 373 return CommandLine::ForCurrentProcess()->HasSwitch( |
| 374 switches::kDisableScriptedPrintThrottling); | 374 switches::kDisableScriptedPrintThrottling); |
| 375 } | 375 } |
| 376 | 376 |
| 377 } // namespace | 377 } // namespace |
| 378 | 378 |
| 379 FrameReference::FrameReference(const WebKit::WebFrame* frame) { | 379 FrameReference::FrameReference(const blink::WebFrame* frame) { |
| 380 Reset(frame); | 380 Reset(frame); |
| 381 } | 381 } |
| 382 | 382 |
| 383 FrameReference::FrameReference() { | 383 FrameReference::FrameReference() { |
| 384 Reset(NULL); | 384 Reset(NULL); |
| 385 } | 385 } |
| 386 | 386 |
| 387 FrameReference::~FrameReference() { | 387 FrameReference::~FrameReference() { |
| 388 } | 388 } |
| 389 | 389 |
| 390 void FrameReference::Reset(const WebKit::WebFrame* frame) { | 390 void FrameReference::Reset(const blink::WebFrame* frame) { |
| 391 if (frame) { | 391 if (frame) { |
| 392 view_ = frame->view(); | 392 view_ = frame->view(); |
| 393 frame_name_ = frame->uniqueName(); | 393 frame_name_ = frame->uniqueName(); |
| 394 } else { | 394 } else { |
| 395 view_ = NULL; | 395 view_ = NULL; |
| 396 frame_name_.reset(); | 396 frame_name_.reset(); |
| 397 } | 397 } |
| 398 } | 398 } |
| 399 | 399 |
| 400 WebKit::WebFrame* FrameReference::GetFrame() { | 400 blink::WebFrame* FrameReference::GetFrame() { |
| 401 return view_ ? view_->findFrameByName(frame_name_) : NULL; | 401 return view_ ? view_->findFrameByName(frame_name_) : NULL; |
| 402 } | 402 } |
| 403 | 403 |
| 404 WebKit::WebView* FrameReference::view() { | 404 blink::WebView* FrameReference::view() { |
| 405 return view_; | 405 return view_; |
| 406 } | 406 } |
| 407 | 407 |
| 408 // static - Not anonymous so that platform implementations can use it. | 408 // static - Not anonymous so that platform implementations can use it. |
| 409 void PrintWebViewHelper::PrintHeaderAndFooter( | 409 void PrintWebViewHelper::PrintHeaderAndFooter( |
| 410 WebKit::WebCanvas* canvas, | 410 blink::WebCanvas* canvas, |
| 411 int page_number, | 411 int page_number, |
| 412 int total_pages, | 412 int total_pages, |
| 413 float webkit_scale_factor, | 413 float webkit_scale_factor, |
| 414 const PageSizeMargins& page_layout, | 414 const PageSizeMargins& page_layout, |
| 415 const base::DictionaryValue& header_footer_info, | 415 const base::DictionaryValue& header_footer_info, |
| 416 const PrintMsg_Print_Params& params) { | 416 const PrintMsg_Print_Params& params) { |
| 417 skia::VectorPlatformDeviceSkia* device = | 417 skia::VectorPlatformDeviceSkia* device = |
| 418 static_cast<skia::VectorPlatformDeviceSkia*>(canvas->getTopDevice()); | 418 static_cast<skia::VectorPlatformDeviceSkia*>(canvas->getTopDevice()); |
| 419 device->setDrawingArea(SkPDFDevice::kMargin_DrawingArea); | 419 device->setDrawingArea(SkPDFDevice::kMargin_DrawingArea); |
| 420 | 420 |
| 421 SkAutoCanvasRestore auto_restore(canvas, true); | 421 SkAutoCanvasRestore auto_restore(canvas, true); |
| 422 canvas->scale(1 / webkit_scale_factor, 1 / webkit_scale_factor); | 422 canvas->scale(1 / webkit_scale_factor, 1 / webkit_scale_factor); |
| 423 | 423 |
| 424 WebKit::WebSize page_size(page_layout.margin_left + page_layout.margin_right + | 424 blink::WebSize page_size(page_layout.margin_left + page_layout.margin_right + |
| 425 page_layout.content_width, | 425 page_layout.content_width, |
| 426 page_layout.margin_top + page_layout.margin_bottom + | 426 page_layout.margin_top + page_layout.margin_bottom + |
| 427 page_layout.content_height); | 427 page_layout.content_height); |
| 428 | 428 |
| 429 WebKit::WebView* web_view = WebKit::WebView::create(NULL); | 429 blink::WebView* web_view = blink::WebView::create(NULL); |
| 430 web_view->settings()->setJavaScriptEnabled(true); | 430 web_view->settings()->setJavaScriptEnabled(true); |
| 431 web_view->initializeMainFrame(NULL); | 431 web_view->initializeMainFrame(NULL); |
| 432 | 432 |
| 433 WebKit::WebFrame* frame = web_view->mainFrame(); | 433 blink::WebFrame* frame = web_view->mainFrame(); |
| 434 | 434 |
| 435 base::StringValue html( | 435 base::StringValue html( |
| 436 ResourceBundle::GetSharedInstance().GetLocalizedString( | 436 ResourceBundle::GetSharedInstance().GetLocalizedString( |
| 437 IDR_PRINT_PREVIEW_PAGE)); | 437 IDR_PRINT_PREVIEW_PAGE)); |
| 438 // Load page with script to avoid async operations. | 438 // Load page with script to avoid async operations. |
| 439 ExecuteScript(frame, kPageLoadScriptFormat, html); | 439 ExecuteScript(frame, kPageLoadScriptFormat, html); |
| 440 | 440 |
| 441 scoped_ptr<base::DictionaryValue> options(header_footer_info.DeepCopy()); | 441 scoped_ptr<base::DictionaryValue> options(header_footer_info.DeepCopy()); |
| 442 options->SetDouble("width", page_size.width); | 442 options->SetDouble("width", page_size.width); |
| 443 options->SetDouble("height", page_size.height); | 443 options->SetDouble("height", page_size.height); |
| 444 options->SetDouble("topMargin", page_layout.margin_top); | 444 options->SetDouble("topMargin", page_layout.margin_top); |
| 445 options->SetDouble("bottomMargin", page_layout.margin_bottom); | 445 options->SetDouble("bottomMargin", page_layout.margin_bottom); |
| 446 options->SetString("pageNumber", | 446 options->SetString("pageNumber", |
| 447 base::StringPrintf("%d/%d", page_number, total_pages)); | 447 base::StringPrintf("%d/%d", page_number, total_pages)); |
| 448 | 448 |
| 449 ExecuteScript(frame, kPageSetupScriptFormat, *options); | 449 ExecuteScript(frame, kPageSetupScriptFormat, *options); |
| 450 | 450 |
| 451 WebKit::WebPrintParams webkit_params(page_size); | 451 blink::WebPrintParams webkit_params(page_size); |
| 452 webkit_params.printerDPI = GetDPI(¶ms); | 452 webkit_params.printerDPI = GetDPI(¶ms); |
| 453 | 453 |
| 454 frame->printBegin(webkit_params); | 454 frame->printBegin(webkit_params); |
| 455 frame->printPage(0, canvas); | 455 frame->printPage(0, canvas); |
| 456 frame->printEnd(); | 456 frame->printEnd(); |
| 457 | 457 |
| 458 web_view->close(); | 458 web_view->close(); |
| 459 | 459 |
| 460 device->setDrawingArea(SkPDFDevice::kContent_DrawingArea); | 460 device->setDrawingArea(SkPDFDevice::kContent_DrawingArea); |
| 461 } | 461 } |
| 462 | 462 |
| 463 // static - Not anonymous so that platform implementations can use it. | 463 // static - Not anonymous so that platform implementations can use it. |
| 464 float PrintWebViewHelper::RenderPageContent(WebKit::WebFrame* frame, | 464 float PrintWebViewHelper::RenderPageContent(blink::WebFrame* frame, |
| 465 int page_number, | 465 int page_number, |
| 466 const gfx::Rect& canvas_area, | 466 const gfx::Rect& canvas_area, |
| 467 const gfx::Rect& content_area, | 467 const gfx::Rect& content_area, |
| 468 double scale_factor, | 468 double scale_factor, |
| 469 WebKit::WebCanvas* canvas) { | 469 blink::WebCanvas* canvas) { |
| 470 SkAutoCanvasRestore auto_restore(canvas, true); | 470 SkAutoCanvasRestore auto_restore(canvas, true); |
| 471 if (content_area != canvas_area) { | 471 if (content_area != canvas_area) { |
| 472 canvas->translate((content_area.x() - canvas_area.x()) / scale_factor, | 472 canvas->translate((content_area.x() - canvas_area.x()) / scale_factor, |
| 473 (content_area.y() - canvas_area.y()) / scale_factor); | 473 (content_area.y() - canvas_area.y()) / scale_factor); |
| 474 SkRect clip_rect( | 474 SkRect clip_rect( |
| 475 SkRect::MakeXYWH(content_area.origin().x() / scale_factor, | 475 SkRect::MakeXYWH(content_area.origin().x() / scale_factor, |
| 476 content_area.origin().y() / scale_factor, | 476 content_area.origin().y() / scale_factor, |
| 477 content_area.size().width() / scale_factor, | 477 content_area.size().width() / scale_factor, |
| 478 content_area.size().height() / scale_factor)); | 478 content_area.size().height() / scale_factor)); |
| 479 SkIRect clip_int_rect; | 479 SkIRect clip_int_rect; |
| 480 clip_rect.roundOut(&clip_int_rect); | 480 clip_rect.roundOut(&clip_int_rect); |
| 481 SkRegion clip_region(clip_int_rect); | 481 SkRegion clip_region(clip_int_rect); |
| 482 canvas->setClipRegion(clip_region); | 482 canvas->setClipRegion(clip_region); |
| 483 } | 483 } |
| 484 return frame->printPage(page_number, canvas); | 484 return frame->printPage(page_number, canvas); |
| 485 } | 485 } |
| 486 | 486 |
| 487 // Class that calls the Begin and End print functions on the frame and changes | 487 // Class that calls the Begin and End print functions on the frame and changes |
| 488 // the size of the view temporarily to support full page printing.. | 488 // the size of the view temporarily to support full page printing.. |
| 489 class PrepareFrameAndViewForPrint : public WebKit::WebViewClient, | 489 class PrepareFrameAndViewForPrint : public blink::WebViewClient, |
| 490 public WebKit::WebFrameClient { | 490 public blink::WebFrameClient { |
| 491 public: | 491 public: |
| 492 PrepareFrameAndViewForPrint(const PrintMsg_Print_Params& params, | 492 PrepareFrameAndViewForPrint(const PrintMsg_Print_Params& params, |
| 493 WebKit::WebFrame* frame, | 493 blink::WebFrame* frame, |
| 494 const WebKit::WebNode& node, | 494 const blink::WebNode& node, |
| 495 bool ignore_css_margins); | 495 bool ignore_css_margins); |
| 496 virtual ~PrepareFrameAndViewForPrint(); | 496 virtual ~PrepareFrameAndViewForPrint(); |
| 497 | 497 |
| 498 // Optional. Replaces |frame_| with selection if needed. Will call |on_ready| | 498 // Optional. Replaces |frame_| with selection if needed. Will call |on_ready| |
| 499 // when completed. | 499 // when completed. |
| 500 void CopySelectionIfNeeded(const WebPreferences& preferences, | 500 void CopySelectionIfNeeded(const WebPreferences& preferences, |
| 501 const base::Closure& on_ready); | 501 const base::Closure& on_ready); |
| 502 | 502 |
| 503 // Prepares frame for printing. | 503 // Prepares frame for printing. |
| 504 void StartPrinting(); | 504 void StartPrinting(); |
| 505 | 505 |
| 506 WebKit::WebFrame* frame() { | 506 blink::WebFrame* frame() { |
| 507 return frame_.GetFrame(); | 507 return frame_.GetFrame(); |
| 508 } | 508 } |
| 509 | 509 |
| 510 const WebKit::WebNode& node() const { | 510 const blink::WebNode& node() const { |
| 511 return node_to_print_; | 511 return node_to_print_; |
| 512 } | 512 } |
| 513 | 513 |
| 514 int GetExpectedPageCount() const { | 514 int GetExpectedPageCount() const { |
| 515 return expected_pages_count_; | 515 return expected_pages_count_; |
| 516 } | 516 } |
| 517 | 517 |
| 518 gfx::Size GetPrintCanvasSize() const; | 518 gfx::Size GetPrintCanvasSize() const; |
| 519 | 519 |
| 520 void FinishPrinting(); | 520 void FinishPrinting(); |
| 521 | 521 |
| 522 bool IsLoadingSelection() { | 522 bool IsLoadingSelection() { |
| 523 // It's not selection if not |owns_web_view_|. | 523 // It's not selection if not |owns_web_view_|. |
| 524 return owns_web_view_ && frame() && frame()->isLoading(); | 524 return owns_web_view_ && frame() && frame()->isLoading(); |
| 525 } | 525 } |
| 526 | 526 |
| 527 protected: | 527 protected: |
| 528 // WebKit::WebViewClient override: | 528 // blink::WebViewClient override: |
| 529 virtual void didStopLoading(); | 529 virtual void didStopLoading(); |
| 530 | 530 |
| 531 virtual void CallOnReady(); | 531 virtual void CallOnReady(); |
| 532 | 532 |
| 533 private: | 533 private: |
| 534 void ResizeForPrinting(); | 534 void ResizeForPrinting(); |
| 535 void RestoreSize(); | 535 void RestoreSize(); |
| 536 void CopySelection(const WebPreferences& preferences); | 536 void CopySelection(const WebPreferences& preferences); |
| 537 | 537 |
| 538 base::WeakPtrFactory<PrepareFrameAndViewForPrint> weak_ptr_factory_; | 538 base::WeakPtrFactory<PrepareFrameAndViewForPrint> weak_ptr_factory_; |
| 539 | 539 |
| 540 FrameReference frame_; | 540 FrameReference frame_; |
| 541 WebKit::WebNode node_to_print_; | 541 blink::WebNode node_to_print_; |
| 542 bool owns_web_view_; | 542 bool owns_web_view_; |
| 543 WebKit::WebPrintParams web_print_params_; | 543 blink::WebPrintParams web_print_params_; |
| 544 gfx::Size prev_view_size_; | 544 gfx::Size prev_view_size_; |
| 545 gfx::Size prev_scroll_offset_; | 545 gfx::Size prev_scroll_offset_; |
| 546 int expected_pages_count_; | 546 int expected_pages_count_; |
| 547 base::Closure on_ready_; | 547 base::Closure on_ready_; |
| 548 bool should_print_backgrounds_; | 548 bool should_print_backgrounds_; |
| 549 bool should_print_selection_only_; | 549 bool should_print_selection_only_; |
| 550 bool is_printing_started_; | 550 bool is_printing_started_; |
| 551 | 551 |
| 552 DISALLOW_COPY_AND_ASSIGN(PrepareFrameAndViewForPrint); | 552 DISALLOW_COPY_AND_ASSIGN(PrepareFrameAndViewForPrint); |
| 553 }; | 553 }; |
| 554 | 554 |
| 555 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( | 555 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( |
| 556 const PrintMsg_Print_Params& params, | 556 const PrintMsg_Print_Params& params, |
| 557 WebKit::WebFrame* frame, | 557 blink::WebFrame* frame, |
| 558 const WebKit::WebNode& node, | 558 const blink::WebNode& node, |
| 559 bool ignore_css_margins) | 559 bool ignore_css_margins) |
| 560 : weak_ptr_factory_(this), | 560 : weak_ptr_factory_(this), |
| 561 frame_(frame), | 561 frame_(frame), |
| 562 node_to_print_(node), | 562 node_to_print_(node), |
| 563 owns_web_view_(false), | 563 owns_web_view_(false), |
| 564 expected_pages_count_(0), | 564 expected_pages_count_(0), |
| 565 should_print_backgrounds_(params.should_print_backgrounds), | 565 should_print_backgrounds_(params.should_print_backgrounds), |
| 566 should_print_selection_only_(params.selection_only), | 566 should_print_selection_only_(params.selection_only), |
| 567 is_printing_started_(false) { | 567 is_printing_started_(false) { |
| 568 PrintMsg_Print_Params print_params = params; | 568 PrintMsg_Print_Params print_params = params; |
| 569 if (!should_print_selection_only_ || | 569 if (!should_print_selection_only_ || |
| 570 !PrintingNodeOrPdfFrame(frame, node_to_print_)) { | 570 !PrintingNodeOrPdfFrame(frame, node_to_print_)) { |
| 571 bool fit_to_page = ignore_css_margins && | 571 bool fit_to_page = ignore_css_margins && |
| 572 print_params.print_scaling_option == | 572 print_params.print_scaling_option == |
| 573 WebKit::WebPrintScalingOptionFitToPrintableArea; | 573 blink::WebPrintScalingOptionFitToPrintableArea; |
| 574 ComputeWebKitPrintParamsInDesiredDpi(params, &web_print_params_); | 574 ComputeWebKitPrintParamsInDesiredDpi(params, &web_print_params_); |
| 575 frame->printBegin(web_print_params_, node_to_print_); | 575 frame->printBegin(web_print_params_, node_to_print_); |
| 576 print_params = CalculatePrintParamsForCss(frame, 0, print_params, | 576 print_params = CalculatePrintParamsForCss(frame, 0, print_params, |
| 577 ignore_css_margins, fit_to_page, | 577 ignore_css_margins, fit_to_page, |
| 578 NULL); | 578 NULL); |
| 579 frame->printEnd(); | 579 frame->printEnd(); |
| 580 } | 580 } |
| 581 ComputeWebKitPrintParamsInDesiredDpi(print_params, &web_print_params_); | 581 ComputeWebKitPrintParamsInDesiredDpi(print_params, &web_print_params_); |
| 582 } | 582 } |
| 583 | 583 |
| 584 PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() { | 584 PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() { |
| 585 FinishPrinting(); | 585 FinishPrinting(); |
| 586 } | 586 } |
| 587 | 587 |
| 588 void PrepareFrameAndViewForPrint::ResizeForPrinting() { | 588 void PrepareFrameAndViewForPrint::ResizeForPrinting() { |
| 589 // Layout page according to printer page size. Since WebKit shrinks the | 589 // Layout page according to printer page size. Since WebKit shrinks the |
| 590 // size of the page automatically (from 125% to 200%) we trick it to | 590 // size of the page automatically (from 125% to 200%) we trick it to |
| 591 // think the page is 125% larger so the size of the page is correct for | 591 // think the page is 125% larger so the size of the page is correct for |
| 592 // minimum (default) scaling. | 592 // minimum (default) scaling. |
| 593 // This is important for sites that try to fill the page. | 593 // This is important for sites that try to fill the page. |
| 594 gfx::Size print_layout_size(web_print_params_.printContentArea.width, | 594 gfx::Size print_layout_size(web_print_params_.printContentArea.width, |
| 595 web_print_params_.printContentArea.height); | 595 web_print_params_.printContentArea.height); |
| 596 print_layout_size.set_height( | 596 print_layout_size.set_height( |
| 597 static_cast<int>(static_cast<double>(print_layout_size.height()) * 1.25)); | 597 static_cast<int>(static_cast<double>(print_layout_size.height()) * 1.25)); |
| 598 | 598 |
| 599 if (!frame()) | 599 if (!frame()) |
| 600 return; | 600 return; |
| 601 WebKit::WebView* web_view = frame_.view(); | 601 blink::WebView* web_view = frame_.view(); |
| 602 // Backup size and offset. | 602 // Backup size and offset. |
| 603 if (WebKit::WebFrame* web_frame = web_view->mainFrame()) | 603 if (blink::WebFrame* web_frame = web_view->mainFrame()) |
| 604 prev_scroll_offset_ = web_frame->scrollOffset(); | 604 prev_scroll_offset_ = web_frame->scrollOffset(); |
| 605 prev_view_size_ = web_view->size(); | 605 prev_view_size_ = web_view->size(); |
| 606 | 606 |
| 607 web_view->resize(print_layout_size); | 607 web_view->resize(print_layout_size); |
| 608 } | 608 } |
| 609 | 609 |
| 610 | 610 |
| 611 void PrepareFrameAndViewForPrint::StartPrinting() { | 611 void PrepareFrameAndViewForPrint::StartPrinting() { |
| 612 ResizeForPrinting(); | 612 ResizeForPrinting(); |
| 613 WebKit::WebView* web_view = frame_.view(); | 613 blink::WebView* web_view = frame_.view(); |
| 614 web_view->settings()->setShouldPrintBackgrounds(should_print_backgrounds_); | 614 web_view->settings()->setShouldPrintBackgrounds(should_print_backgrounds_); |
| 615 expected_pages_count_ = | 615 expected_pages_count_ = |
| 616 frame()->printBegin(web_print_params_, node_to_print_); | 616 frame()->printBegin(web_print_params_, node_to_print_); |
| 617 is_printing_started_ = true; | 617 is_printing_started_ = true; |
| 618 } | 618 } |
| 619 | 619 |
| 620 void PrepareFrameAndViewForPrint::CopySelectionIfNeeded( | 620 void PrepareFrameAndViewForPrint::CopySelectionIfNeeded( |
| 621 const WebPreferences& preferences, | 621 const WebPreferences& preferences, |
| 622 const base::Closure& on_ready) { | 622 const base::Closure& on_ready) { |
| 623 on_ready_ = on_ready; | 623 on_ready_ = on_ready; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 634 url_str.append( | 634 url_str.append( |
| 635 net::EscapeQueryParamValue(frame()->selectionAsMarkup().utf8(), false)); | 635 net::EscapeQueryParamValue(frame()->selectionAsMarkup().utf8(), false)); |
| 636 RestoreSize(); | 636 RestoreSize(); |
| 637 // Create a new WebView with the same settings as the current display one. | 637 // Create a new WebView with the same settings as the current display one. |
| 638 // Except that we disable javascript (don't want any active content running | 638 // Except that we disable javascript (don't want any active content running |
| 639 // on the page). | 639 // on the page). |
| 640 WebPreferences prefs = preferences; | 640 WebPreferences prefs = preferences; |
| 641 prefs.javascript_enabled = false; | 641 prefs.javascript_enabled = false; |
| 642 prefs.java_enabled = false; | 642 prefs.java_enabled = false; |
| 643 | 643 |
| 644 WebKit::WebView* web_view = WebKit::WebView::create(this); | 644 blink::WebView* web_view = blink::WebView::create(this); |
| 645 owns_web_view_ = true; | 645 owns_web_view_ = true; |
| 646 content::ApplyWebPreferences(prefs, web_view); | 646 content::ApplyWebPreferences(prefs, web_view); |
| 647 web_view->initializeMainFrame(this); | 647 web_view->initializeMainFrame(this); |
| 648 frame_.Reset(web_view->mainFrame()); | 648 frame_.Reset(web_view->mainFrame()); |
| 649 node_to_print_.reset(); | 649 node_to_print_.reset(); |
| 650 | 650 |
| 651 // When loading is done this will call didStopLoading() and that will do the | 651 // When loading is done this will call didStopLoading() and that will do the |
| 652 // actual printing. | 652 // actual printing. |
| 653 frame()->loadRequest(WebKit::WebURLRequest(GURL(url_str))); | 653 frame()->loadRequest(blink::WebURLRequest(GURL(url_str))); |
| 654 } | 654 } |
| 655 | 655 |
| 656 void PrepareFrameAndViewForPrint::didStopLoading() { | 656 void PrepareFrameAndViewForPrint::didStopLoading() { |
| 657 DCHECK(!on_ready_.is_null()); | 657 DCHECK(!on_ready_.is_null()); |
| 658 // Don't call callback here, because it can delete |this| and WebView that is | 658 // Don't call callback here, because it can delete |this| and WebView that is |
| 659 // called didStopLoading. | 659 // called didStopLoading. |
| 660 base::MessageLoop::current()->PostTask( | 660 base::MessageLoop::current()->PostTask( |
| 661 FROM_HERE, | 661 FROM_HERE, |
| 662 base::Bind(&PrepareFrameAndViewForPrint::CallOnReady, | 662 base::Bind(&PrepareFrameAndViewForPrint::CallOnReady, |
| 663 weak_ptr_factory_.GetWeakPtr())); | 663 weak_ptr_factory_.GetWeakPtr())); |
| 664 } | 664 } |
| 665 | 665 |
| 666 void PrepareFrameAndViewForPrint::CallOnReady() { | 666 void PrepareFrameAndViewForPrint::CallOnReady() { |
| 667 return on_ready_.Run(); // Can delete |this|. | 667 return on_ready_.Run(); // Can delete |this|. |
| 668 } | 668 } |
| 669 | 669 |
| 670 gfx::Size PrepareFrameAndViewForPrint::GetPrintCanvasSize() const { | 670 gfx::Size PrepareFrameAndViewForPrint::GetPrintCanvasSize() const { |
| 671 DCHECK(is_printing_started_); | 671 DCHECK(is_printing_started_); |
| 672 return gfx::Size(web_print_params_.printContentArea.width, | 672 return gfx::Size(web_print_params_.printContentArea.width, |
| 673 web_print_params_.printContentArea.height); | 673 web_print_params_.printContentArea.height); |
| 674 } | 674 } |
| 675 | 675 |
| 676 void PrepareFrameAndViewForPrint::RestoreSize() { | 676 void PrepareFrameAndViewForPrint::RestoreSize() { |
| 677 if (frame()) { | 677 if (frame()) { |
| 678 WebKit::WebView* web_view = frame_.GetFrame()->view(); | 678 blink::WebView* web_view = frame_.GetFrame()->view(); |
| 679 web_view->resize(prev_view_size_); | 679 web_view->resize(prev_view_size_); |
| 680 if (WebKit::WebFrame* web_frame = web_view->mainFrame()) | 680 if (blink::WebFrame* web_frame = web_view->mainFrame()) |
| 681 web_frame->setScrollOffset(prev_scroll_offset_); | 681 web_frame->setScrollOffset(prev_scroll_offset_); |
| 682 } | 682 } |
| 683 } | 683 } |
| 684 | 684 |
| 685 void PrepareFrameAndViewForPrint::FinishPrinting() { | 685 void PrepareFrameAndViewForPrint::FinishPrinting() { |
| 686 WebKit::WebFrame* frame = frame_.GetFrame(); | 686 blink::WebFrame* frame = frame_.GetFrame(); |
| 687 if (frame) { | 687 if (frame) { |
| 688 WebKit::WebView* web_view = frame->view(); | 688 blink::WebView* web_view = frame->view(); |
| 689 if (is_printing_started_) { | 689 if (is_printing_started_) { |
| 690 is_printing_started_ = false; | 690 is_printing_started_ = false; |
| 691 frame->printEnd(); | 691 frame->printEnd(); |
| 692 if (!owns_web_view_) { | 692 if (!owns_web_view_) { |
| 693 web_view->settings()->setShouldPrintBackgrounds(false); | 693 web_view->settings()->setShouldPrintBackgrounds(false); |
| 694 RestoreSize(); | 694 RestoreSize(); |
| 695 } | 695 } |
| 696 } | 696 } |
| 697 if (owns_web_view_) { | 697 if (owns_web_view_) { |
| 698 DCHECK(!frame->isLoading()); | 698 DCHECK(!frame->isLoading()); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 718 print_for_preview_(false), | 718 print_for_preview_(false), |
| 719 print_node_in_progress_(false), | 719 print_node_in_progress_(false), |
| 720 is_loading_(false), | 720 is_loading_(false), |
| 721 is_scripted_preview_delayed_(false), | 721 is_scripted_preview_delayed_(false), |
| 722 weak_ptr_factory_(this) { | 722 weak_ptr_factory_(this) { |
| 723 } | 723 } |
| 724 | 724 |
| 725 PrintWebViewHelper::~PrintWebViewHelper() {} | 725 PrintWebViewHelper::~PrintWebViewHelper() {} |
| 726 | 726 |
| 727 bool PrintWebViewHelper::IsScriptInitiatedPrintAllowed( | 727 bool PrintWebViewHelper::IsScriptInitiatedPrintAllowed( |
| 728 WebKit::WebFrame* frame, bool user_initiated) { | 728 blink::WebFrame* frame, bool user_initiated) { |
| 729 #if defined(OS_ANDROID) | 729 #if defined(OS_ANDROID) |
| 730 return false; | 730 return false; |
| 731 #endif // defined(OS_ANDROID) | 731 #endif // defined(OS_ANDROID) |
| 732 if (is_scripted_printing_blocked_) | 732 if (is_scripted_printing_blocked_) |
| 733 return false; | 733 return false; |
| 734 // If preview is enabled, then the print dialog is tab modal, and the user | 734 // If preview is enabled, then the print dialog is tab modal, and the user |
| 735 // can always close the tab on a mis-behaving page (the system print dialog | 735 // can always close the tab on a mis-behaving page (the system print dialog |
| 736 // is app modal). If the print was initiated through user action, don't | 736 // is app modal). If the print was initiated through user action, don't |
| 737 // throttle. Or, if the command line flag to skip throttling has been set. | 737 // throttle. Or, if the command line flag to skip throttling has been set. |
| 738 if (!is_scripted_print_throttling_disabled_ && | 738 if (!is_scripted_print_throttling_disabled_ && |
| 739 !is_preview_enabled_ && | 739 !is_preview_enabled_ && |
| 740 !user_initiated) | 740 !user_initiated) |
| 741 return !IsScriptInitiatedPrintTooFrequent(frame); | 741 return !IsScriptInitiatedPrintTooFrequent(frame); |
| 742 return true; | 742 return true; |
| 743 } | 743 } |
| 744 | 744 |
| 745 void PrintWebViewHelper::DidStartLoading() { | 745 void PrintWebViewHelper::DidStartLoading() { |
| 746 is_loading_ = true; | 746 is_loading_ = true; |
| 747 } | 747 } |
| 748 | 748 |
| 749 void PrintWebViewHelper::DidStopLoading() { | 749 void PrintWebViewHelper::DidStopLoading() { |
| 750 is_loading_ = false; | 750 is_loading_ = false; |
| 751 ShowScriptedPrintPreview(); | 751 ShowScriptedPrintPreview(); |
| 752 } | 752 } |
| 753 | 753 |
| 754 // Prints |frame| which called window.print(). | 754 // Prints |frame| which called window.print(). |
| 755 void PrintWebViewHelper::PrintPage(WebKit::WebFrame* frame, | 755 void PrintWebViewHelper::PrintPage(blink::WebFrame* frame, |
| 756 bool user_initiated) { | 756 bool user_initiated) { |
| 757 DCHECK(frame); | 757 DCHECK(frame); |
| 758 | 758 |
| 759 // Allow Prerendering to cancel this print request if necessary. | 759 // Allow Prerendering to cancel this print request if necessary. |
| 760 if (prerender::PrerenderHelper::IsPrerendering(render_view())) { | 760 if (prerender::PrerenderHelper::IsPrerendering(render_view())) { |
| 761 Send(new ChromeViewHostMsg_CancelPrerenderForPrinting(routing_id())); | 761 Send(new ChromeViewHostMsg_CancelPrerenderForPrinting(routing_id())); |
| 762 return; | 762 return; |
| 763 } | 763 } |
| 764 | 764 |
| 765 if (!IsScriptInitiatedPrintAllowed(frame, user_initiated)) | 765 if (!IsScriptInitiatedPrintAllowed(frame, user_initiated)) |
| 766 return; | 766 return; |
| 767 IncrementScriptedPrintCount(); | 767 IncrementScriptedPrintCount(); |
| 768 | 768 |
| 769 if (is_preview_enabled_) { | 769 if (is_preview_enabled_) { |
| 770 print_preview_context_.InitWithFrame(frame); | 770 print_preview_context_.InitWithFrame(frame); |
| 771 RequestPrintPreview(PRINT_PREVIEW_SCRIPTED); | 771 RequestPrintPreview(PRINT_PREVIEW_SCRIPTED); |
| 772 } else { | 772 } else { |
| 773 Print(frame, WebKit::WebNode()); | 773 Print(frame, blink::WebNode()); |
| 774 } | 774 } |
| 775 } | 775 } |
| 776 | 776 |
| 777 bool PrintWebViewHelper::OnMessageReceived(const IPC::Message& message) { | 777 bool PrintWebViewHelper::OnMessageReceived(const IPC::Message& message) { |
| 778 bool handled = true; | 778 bool handled = true; |
| 779 IPC_BEGIN_MESSAGE_MAP(PrintWebViewHelper, message) | 779 IPC_BEGIN_MESSAGE_MAP(PrintWebViewHelper, message) |
| 780 IPC_MESSAGE_HANDLER(PrintMsg_PrintPages, OnPrintPages) | 780 IPC_MESSAGE_HANDLER(PrintMsg_PrintPages, OnPrintPages) |
| 781 IPC_MESSAGE_HANDLER(PrintMsg_PrintForSystemDialog, OnPrintForSystemDialog) | 781 IPC_MESSAGE_HANDLER(PrintMsg_PrintForSystemDialog, OnPrintForSystemDialog) |
| 782 IPC_MESSAGE_HANDLER(PrintMsg_InitiatePrintPreview, OnInitiatePrintPreview) | 782 IPC_MESSAGE_HANDLER(PrintMsg_InitiatePrintPreview, OnInitiatePrintPreview) |
| 783 IPC_MESSAGE_HANDLER(PrintMsg_PrintNodeUnderContextMenu, | 783 IPC_MESSAGE_HANDLER(PrintMsg_PrintNodeUnderContextMenu, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 796 | 796 |
| 797 void PrintWebViewHelper::OnPrintForPrintPreview( | 797 void PrintWebViewHelper::OnPrintForPrintPreview( |
| 798 const DictionaryValue& job_settings) { | 798 const DictionaryValue& job_settings) { |
| 799 DCHECK(is_preview_enabled_); | 799 DCHECK(is_preview_enabled_); |
| 800 // If still not finished with earlier print request simply ignore. | 800 // If still not finished with earlier print request simply ignore. |
| 801 if (prep_frame_view_) | 801 if (prep_frame_view_) |
| 802 return; | 802 return; |
| 803 | 803 |
| 804 if (!render_view()->GetWebView()) | 804 if (!render_view()->GetWebView()) |
| 805 return; | 805 return; |
| 806 WebKit::WebFrame* main_frame = render_view()->GetWebView()->mainFrame(); | 806 blink::WebFrame* main_frame = render_view()->GetWebView()->mainFrame(); |
| 807 if (!main_frame) | 807 if (!main_frame) |
| 808 return; | 808 return; |
| 809 | 809 |
| 810 WebKit::WebDocument document = main_frame->document(); | 810 blink::WebDocument document = main_frame->document(); |
| 811 // <object> with id="pdf-viewer" is created in | 811 // <object> with id="pdf-viewer" is created in |
| 812 // chrome/browser/resources/print_preview/print_preview.js | 812 // chrome/browser/resources/print_preview/print_preview.js |
| 813 WebKit::WebElement pdf_element = document.getElementById("pdf-viewer"); | 813 blink::WebElement pdf_element = document.getElementById("pdf-viewer"); |
| 814 if (pdf_element.isNull()) { | 814 if (pdf_element.isNull()) { |
| 815 NOTREACHED(); | 815 NOTREACHED(); |
| 816 return; | 816 return; |
| 817 } | 817 } |
| 818 | 818 |
| 819 // Set |print_for_preview_| flag and autoreset it to back to original | 819 // Set |print_for_preview_| flag and autoreset it to back to original |
| 820 // on return. | 820 // on return. |
| 821 base::AutoReset<bool> set_printing_flag(&print_for_preview_, true); | 821 base::AutoReset<bool> set_printing_flag(&print_for_preview_, true); |
| 822 | 822 |
| 823 WebKit::WebFrame* pdf_frame = pdf_element.document().frame(); | 823 blink::WebFrame* pdf_frame = pdf_element.document().frame(); |
| 824 if (!UpdatePrintSettings(pdf_frame, pdf_element, job_settings)) { | 824 if (!UpdatePrintSettings(pdf_frame, pdf_element, job_settings)) { |
| 825 LOG(ERROR) << "UpdatePrintSettings failed"; | 825 LOG(ERROR) << "UpdatePrintSettings failed"; |
| 826 DidFinishPrinting(FAIL_PRINT); | 826 DidFinishPrinting(FAIL_PRINT); |
| 827 return; | 827 return; |
| 828 } | 828 } |
| 829 | 829 |
| 830 // Print page onto entire page not just printable area. Preview PDF already | 830 // Print page onto entire page not just printable area. Preview PDF already |
| 831 // has content in correct position taking into account page size and printable | 831 // has content in correct position taking into account page size and printable |
| 832 // area. | 832 // area. |
| 833 // TODO(vitalybuka) : Make this consistent on all platform. This change | 833 // TODO(vitalybuka) : Make this consistent on all platform. This change |
| 834 // affects Windows only. On Linux and OSX RenderPagesForPrint does not use | 834 // affects Windows only. On Linux and OSX RenderPagesForPrint does not use |
| 835 // printable_area. Also we can't change printable_area deeper inside | 835 // printable_area. Also we can't change printable_area deeper inside |
| 836 // RenderPagesForPrint for Windows, because it's used also by native | 836 // RenderPagesForPrint for Windows, because it's used also by native |
| 837 // printing and it expects real printable_area value. | 837 // printing and it expects real printable_area value. |
| 838 // See http://crbug.com/123408 | 838 // See http://crbug.com/123408 |
| 839 PrintMsg_Print_Params& print_params = print_pages_params_->params; | 839 PrintMsg_Print_Params& print_params = print_pages_params_->params; |
| 840 print_params.printable_area = gfx::Rect(print_params.page_size); | 840 print_params.printable_area = gfx::Rect(print_params.page_size); |
| 841 | 841 |
| 842 // Render Pages for printing. | 842 // Render Pages for printing. |
| 843 if (!RenderPagesForPrint(pdf_frame, pdf_element)) { | 843 if (!RenderPagesForPrint(pdf_frame, pdf_element)) { |
| 844 LOG(ERROR) << "RenderPagesForPrint failed"; | 844 LOG(ERROR) << "RenderPagesForPrint failed"; |
| 845 DidFinishPrinting(FAIL_PRINT); | 845 DidFinishPrinting(FAIL_PRINT); |
| 846 } | 846 } |
| 847 } | 847 } |
| 848 | 848 |
| 849 bool PrintWebViewHelper::GetPrintFrame(WebKit::WebFrame** frame) { | 849 bool PrintWebViewHelper::GetPrintFrame(blink::WebFrame** frame) { |
| 850 DCHECK(frame); | 850 DCHECK(frame); |
| 851 WebKit::WebView* webView = render_view()->GetWebView(); | 851 blink::WebView* webView = render_view()->GetWebView(); |
| 852 DCHECK(webView); | 852 DCHECK(webView); |
| 853 if (!webView) | 853 if (!webView) |
| 854 return false; | 854 return false; |
| 855 | 855 |
| 856 // If the user has selected text in the currently focused frame we print | 856 // If the user has selected text in the currently focused frame we print |
| 857 // only that frame (this makes print selection work for multiple frames). | 857 // only that frame (this makes print selection work for multiple frames). |
| 858 WebKit::WebFrame* focusedFrame = webView->focusedFrame(); | 858 blink::WebFrame* focusedFrame = webView->focusedFrame(); |
| 859 *frame = focusedFrame->hasSelection() ? focusedFrame : webView->mainFrame(); | 859 *frame = focusedFrame->hasSelection() ? focusedFrame : webView->mainFrame(); |
| 860 return true; | 860 return true; |
| 861 } | 861 } |
| 862 | 862 |
| 863 void PrintWebViewHelper::OnPrintPages() { | 863 void PrintWebViewHelper::OnPrintPages() { |
| 864 WebKit::WebFrame* frame; | 864 blink::WebFrame* frame; |
| 865 if (GetPrintFrame(&frame)) | 865 if (GetPrintFrame(&frame)) |
| 866 Print(frame, WebKit::WebNode()); | 866 Print(frame, blink::WebNode()); |
| 867 } | 867 } |
| 868 | 868 |
| 869 void PrintWebViewHelper::OnPrintForSystemDialog() { | 869 void PrintWebViewHelper::OnPrintForSystemDialog() { |
| 870 WebKit::WebFrame* frame = print_preview_context_.source_frame(); | 870 blink::WebFrame* frame = print_preview_context_.source_frame(); |
| 871 if (!frame) { | 871 if (!frame) { |
| 872 NOTREACHED(); | 872 NOTREACHED(); |
| 873 return; | 873 return; |
| 874 } | 874 } |
| 875 | 875 |
| 876 Print(frame, print_preview_context_.source_node()); | 876 Print(frame, print_preview_context_.source_node()); |
| 877 } | 877 } |
| 878 | 878 |
| 879 void PrintWebViewHelper::GetPageSizeAndContentAreaFromPageLayout( | 879 void PrintWebViewHelper::GetPageSizeAndContentAreaFromPageLayout( |
| 880 const PageSizeMargins& page_layout_in_points, | 880 const PageSizeMargins& page_layout_in_points, |
| (...skipping 21 matching lines...) Expand all Loading... |
| 902 } | 902 } |
| 903 | 903 |
| 904 bool PrintWebViewHelper::IsPrintToPdfRequested( | 904 bool PrintWebViewHelper::IsPrintToPdfRequested( |
| 905 const base::DictionaryValue& job_settings) { | 905 const base::DictionaryValue& job_settings) { |
| 906 bool print_to_pdf = false; | 906 bool print_to_pdf = false; |
| 907 if (!job_settings.GetBoolean(kSettingPrintToPDF, &print_to_pdf)) | 907 if (!job_settings.GetBoolean(kSettingPrintToPDF, &print_to_pdf)) |
| 908 NOTREACHED(); | 908 NOTREACHED(); |
| 909 return print_to_pdf; | 909 return print_to_pdf; |
| 910 } | 910 } |
| 911 | 911 |
| 912 WebKit::WebPrintScalingOption PrintWebViewHelper::GetPrintScalingOption( | 912 blink::WebPrintScalingOption PrintWebViewHelper::GetPrintScalingOption( |
| 913 bool source_is_html, const base::DictionaryValue& job_settings, | 913 bool source_is_html, const base::DictionaryValue& job_settings, |
| 914 const PrintMsg_Print_Params& params) { | 914 const PrintMsg_Print_Params& params) { |
| 915 DCHECK(!print_for_preview_); | 915 DCHECK(!print_for_preview_); |
| 916 | 916 |
| 917 if (params.print_to_pdf) | 917 if (params.print_to_pdf) |
| 918 return WebKit::WebPrintScalingOptionSourceSize; | 918 return blink::WebPrintScalingOptionSourceSize; |
| 919 | 919 |
| 920 if (!source_is_html) { | 920 if (!source_is_html) { |
| 921 if (!FitToPageEnabled(job_settings)) | 921 if (!FitToPageEnabled(job_settings)) |
| 922 return WebKit::WebPrintScalingOptionNone; | 922 return blink::WebPrintScalingOptionNone; |
| 923 | 923 |
| 924 bool no_plugin_scaling = | 924 bool no_plugin_scaling = |
| 925 print_preview_context_.source_frame()->isPrintScalingDisabledForPlugin( | 925 print_preview_context_.source_frame()->isPrintScalingDisabledForPlugin( |
| 926 print_preview_context_.source_node()); | 926 print_preview_context_.source_node()); |
| 927 | 927 |
| 928 if (params.is_first_request && no_plugin_scaling) | 928 if (params.is_first_request && no_plugin_scaling) |
| 929 return WebKit::WebPrintScalingOptionNone; | 929 return blink::WebPrintScalingOptionNone; |
| 930 } | 930 } |
| 931 return WebKit::WebPrintScalingOptionFitToPrintableArea; | 931 return blink::WebPrintScalingOptionFitToPrintableArea; |
| 932 } | 932 } |
| 933 | 933 |
| 934 void PrintWebViewHelper::OnPrintPreview(const base::DictionaryValue& settings) { | 934 void PrintWebViewHelper::OnPrintPreview(const base::DictionaryValue& settings) { |
| 935 DCHECK(is_preview_enabled_); | 935 DCHECK(is_preview_enabled_); |
| 936 print_preview_context_.OnPrintPreview(); | 936 print_preview_context_.OnPrintPreview(); |
| 937 | 937 |
| 938 UMA_HISTOGRAM_ENUMERATION("PrintPreview.PreviewEvent", | 938 UMA_HISTOGRAM_ENUMERATION("PrintPreview.PreviewEvent", |
| 939 PREVIEW_EVENT_REQUESTED, PREVIEW_EVENT_MAX); | 939 PREVIEW_EVENT_REQUESTED, PREVIEW_EVENT_MAX); |
| 940 | 940 |
| 941 if (!UpdatePrintSettings(print_preview_context_.source_frame(), | 941 if (!UpdatePrintSettings(print_preview_context_.source_frame(), |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1127 void PrintWebViewHelper::SetScriptedPrintBlocked(bool blocked) { | 1127 void PrintWebViewHelper::SetScriptedPrintBlocked(bool blocked) { |
| 1128 is_scripted_printing_blocked_ = blocked; | 1128 is_scripted_printing_blocked_ = blocked; |
| 1129 } | 1129 } |
| 1130 | 1130 |
| 1131 void PrintWebViewHelper::OnPrintNodeUnderContextMenu() { | 1131 void PrintWebViewHelper::OnPrintNodeUnderContextMenu() { |
| 1132 PrintNode(render_view()->GetContextMenuNode()); | 1132 PrintNode(render_view()->GetContextMenuNode()); |
| 1133 } | 1133 } |
| 1134 | 1134 |
| 1135 void PrintWebViewHelper::OnInitiatePrintPreview(bool selection_only) { | 1135 void PrintWebViewHelper::OnInitiatePrintPreview(bool selection_only) { |
| 1136 DCHECK(is_preview_enabled_); | 1136 DCHECK(is_preview_enabled_); |
| 1137 WebKit::WebFrame* frame = NULL; | 1137 blink::WebFrame* frame = NULL; |
| 1138 GetPrintFrame(&frame); | 1138 GetPrintFrame(&frame); |
| 1139 DCHECK(frame); | 1139 DCHECK(frame); |
| 1140 print_preview_context_.InitWithFrame(frame); | 1140 print_preview_context_.InitWithFrame(frame); |
| 1141 RequestPrintPreview(selection_only ? | 1141 RequestPrintPreview(selection_only ? |
| 1142 PRINT_PREVIEW_USER_INITIATED_SELECTION : | 1142 PRINT_PREVIEW_USER_INITIATED_SELECTION : |
| 1143 PRINT_PREVIEW_USER_INITIATED_ENTIRE_FRAME); | 1143 PRINT_PREVIEW_USER_INITIATED_ENTIRE_FRAME); |
| 1144 } | 1144 } |
| 1145 | 1145 |
| 1146 bool PrintWebViewHelper::IsPrintingEnabled() { | 1146 bool PrintWebViewHelper::IsPrintingEnabled() { |
| 1147 bool result = false; | 1147 bool result = false; |
| 1148 Send(new PrintHostMsg_IsPrintingEnabled(routing_id(), &result)); | 1148 Send(new PrintHostMsg_IsPrintingEnabled(routing_id(), &result)); |
| 1149 return result; | 1149 return result; |
| 1150 } | 1150 } |
| 1151 | 1151 |
| 1152 void PrintWebViewHelper::PrintNode(const WebKit::WebNode& node) { | 1152 void PrintWebViewHelper::PrintNode(const blink::WebNode& node) { |
| 1153 if (node.isNull() || !node.document().frame()) { | 1153 if (node.isNull() || !node.document().frame()) { |
| 1154 // This can occur when the context menu refers to an invalid WebNode. | 1154 // This can occur when the context menu refers to an invalid WebNode. |
| 1155 // See http://crbug.com/100890#c17 for a repro case. | 1155 // See http://crbug.com/100890#c17 for a repro case. |
| 1156 return; | 1156 return; |
| 1157 } | 1157 } |
| 1158 | 1158 |
| 1159 if (print_node_in_progress_) { | 1159 if (print_node_in_progress_) { |
| 1160 // This can happen as a result of processing sync messages when printing | 1160 // This can happen as a result of processing sync messages when printing |
| 1161 // from ppapi plugins. It's a rare case, so its OK to just fail here. | 1161 // from ppapi plugins. It's a rare case, so its OK to just fail here. |
| 1162 // See http://crbug.com/159165. | 1162 // See http://crbug.com/159165. |
| 1163 return; | 1163 return; |
| 1164 } | 1164 } |
| 1165 | 1165 |
| 1166 print_node_in_progress_ = true; | 1166 print_node_in_progress_ = true; |
| 1167 | 1167 |
| 1168 // Make a copy of the node, in case RenderView::OnContextMenuClosed resets | 1168 // Make a copy of the node, in case RenderView::OnContextMenuClosed resets |
| 1169 // its |context_menu_node_|. | 1169 // its |context_menu_node_|. |
| 1170 if (is_preview_enabled_) { | 1170 if (is_preview_enabled_) { |
| 1171 print_preview_context_.InitWithNode(node); | 1171 print_preview_context_.InitWithNode(node); |
| 1172 RequestPrintPreview(PRINT_PREVIEW_USER_INITIATED_CONTEXT_NODE); | 1172 RequestPrintPreview(PRINT_PREVIEW_USER_INITIATED_CONTEXT_NODE); |
| 1173 } else { | 1173 } else { |
| 1174 WebKit::WebNode duplicate_node(node); | 1174 blink::WebNode duplicate_node(node); |
| 1175 Print(duplicate_node.document().frame(), duplicate_node); | 1175 Print(duplicate_node.document().frame(), duplicate_node); |
| 1176 } | 1176 } |
| 1177 | 1177 |
| 1178 print_node_in_progress_ = false; | 1178 print_node_in_progress_ = false; |
| 1179 } | 1179 } |
| 1180 | 1180 |
| 1181 void PrintWebViewHelper::Print(WebKit::WebFrame* frame, | 1181 void PrintWebViewHelper::Print(blink::WebFrame* frame, |
| 1182 const WebKit::WebNode& node) { | 1182 const blink::WebNode& node) { |
| 1183 // If still not finished with earlier print request simply ignore. | 1183 // If still not finished with earlier print request simply ignore. |
| 1184 if (prep_frame_view_) | 1184 if (prep_frame_view_) |
| 1185 return; | 1185 return; |
| 1186 | 1186 |
| 1187 FrameReference frame_ref(frame); | 1187 FrameReference frame_ref(frame); |
| 1188 | 1188 |
| 1189 int expected_page_count = 0; | 1189 int expected_page_count = 0; |
| 1190 if (!CalculateNumberOfPages(frame, node, &expected_page_count)) { | 1190 if (!CalculateNumberOfPages(frame, node, &expected_page_count)) { |
| 1191 DidFinishPrinting(FAIL_PRINT_INIT); | 1191 DidFinishPrinting(FAIL_PRINT_INIT); |
| 1192 return; // Failed to init print page settings. | 1192 return; // Failed to init print page settings. |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1291 LOG(ERROR) << "Printing failed."; | 1291 LOG(ERROR) << "Printing failed."; |
| 1292 return DidFinishPrinting(FAIL_PRINT); | 1292 return DidFinishPrinting(FAIL_PRINT); |
| 1293 } | 1293 } |
| 1294 } | 1294 } |
| 1295 | 1295 |
| 1296 void PrintWebViewHelper::FinishFramePrinting() { | 1296 void PrintWebViewHelper::FinishFramePrinting() { |
| 1297 prep_frame_view_.reset(); | 1297 prep_frame_view_.reset(); |
| 1298 } | 1298 } |
| 1299 | 1299 |
| 1300 #if defined(OS_MACOSX) || defined(OS_WIN) | 1300 #if defined(OS_MACOSX) || defined(OS_WIN) |
| 1301 bool PrintWebViewHelper::PrintPagesNative(WebKit::WebFrame* frame, | 1301 bool PrintWebViewHelper::PrintPagesNative(blink::WebFrame* frame, |
| 1302 int page_count, | 1302 int page_count, |
| 1303 const gfx::Size& canvas_size) { | 1303 const gfx::Size& canvas_size) { |
| 1304 const PrintMsg_PrintPages_Params& params = *print_pages_params_; | 1304 const PrintMsg_PrintPages_Params& params = *print_pages_params_; |
| 1305 const PrintMsg_Print_Params& print_params = params.params; | 1305 const PrintMsg_Print_Params& print_params = params.params; |
| 1306 | 1306 |
| 1307 PrintMsg_PrintPage_Params page_params; | 1307 PrintMsg_PrintPage_Params page_params; |
| 1308 page_params.params = print_params; | 1308 page_params.params = print_params; |
| 1309 if (params.pages.empty()) { | 1309 if (params.pages.empty()) { |
| 1310 for (int i = 0; i < page_count; ++i) { | 1310 for (int i = 0; i < page_count; ++i) { |
| 1311 page_params.page_number = i; | 1311 page_params.page_number = i; |
| 1312 PrintPageInternal(page_params, canvas_size, frame); | 1312 PrintPageInternal(page_params, canvas_size, frame); |
| 1313 } | 1313 } |
| 1314 } else { | 1314 } else { |
| 1315 for (size_t i = 0; i < params.pages.size(); ++i) { | 1315 for (size_t i = 0; i < params.pages.size(); ++i) { |
| 1316 if (params.pages[i] >= page_count) | 1316 if (params.pages[i] >= page_count) |
| 1317 break; | 1317 break; |
| 1318 page_params.page_number = params.pages[i]; | 1318 page_params.page_number = params.pages[i]; |
| 1319 PrintPageInternal(page_params, canvas_size, frame); | 1319 PrintPageInternal(page_params, canvas_size, frame); |
| 1320 } | 1320 } |
| 1321 } | 1321 } |
| 1322 return true; | 1322 return true; |
| 1323 } | 1323 } |
| 1324 | 1324 |
| 1325 #endif // OS_MACOSX || OS_WIN | 1325 #endif // OS_MACOSX || OS_WIN |
| 1326 | 1326 |
| 1327 // static - Not anonymous so that platform implementations can use it. | 1327 // static - Not anonymous so that platform implementations can use it. |
| 1328 void PrintWebViewHelper::ComputePageLayoutInPointsForCss( | 1328 void PrintWebViewHelper::ComputePageLayoutInPointsForCss( |
| 1329 WebKit::WebFrame* frame, | 1329 blink::WebFrame* frame, |
| 1330 int page_index, | 1330 int page_index, |
| 1331 const PrintMsg_Print_Params& page_params, | 1331 const PrintMsg_Print_Params& page_params, |
| 1332 bool ignore_css_margins, | 1332 bool ignore_css_margins, |
| 1333 double* scale_factor, | 1333 double* scale_factor, |
| 1334 PageSizeMargins* page_layout_in_points) { | 1334 PageSizeMargins* page_layout_in_points) { |
| 1335 PrintMsg_Print_Params params = CalculatePrintParamsForCss( | 1335 PrintMsg_Print_Params params = CalculatePrintParamsForCss( |
| 1336 frame, page_index, page_params, ignore_css_margins, | 1336 frame, page_index, page_params, ignore_css_margins, |
| 1337 page_params.print_scaling_option == | 1337 page_params.print_scaling_option == |
| 1338 WebKit::WebPrintScalingOptionFitToPrintableArea, | 1338 blink::WebPrintScalingOptionFitToPrintableArea, |
| 1339 scale_factor); | 1339 scale_factor); |
| 1340 CalculatePageLayoutFromPrintParams(params, page_layout_in_points); | 1340 CalculatePageLayoutFromPrintParams(params, page_layout_in_points); |
| 1341 } | 1341 } |
| 1342 | 1342 |
| 1343 bool PrintWebViewHelper::InitPrintSettings(bool fit_to_paper_size) { | 1343 bool PrintWebViewHelper::InitPrintSettings(bool fit_to_paper_size) { |
| 1344 PrintMsg_PrintPages_Params settings; | 1344 PrintMsg_PrintPages_Params settings; |
| 1345 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(), | 1345 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(), |
| 1346 &settings.params)); | 1346 &settings.params)); |
| 1347 // Check if the printer returned any settings, if the settings is empty, we | 1347 // Check if the printer returned any settings, if the settings is empty, we |
| 1348 // can safely assume there are no printer drivers configured. So we safely | 1348 // can safely assume there are no printer drivers configured. So we safely |
| 1349 // terminate. | 1349 // terminate. |
| 1350 bool result = true; | 1350 bool result = true; |
| 1351 if (!PrintMsg_Print_Params_IsValid(settings.params)) | 1351 if (!PrintMsg_Print_Params_IsValid(settings.params)) |
| 1352 result = false; | 1352 result = false; |
| 1353 | 1353 |
| 1354 if (result && | 1354 if (result && |
| 1355 (settings.params.dpi < kMinDpi || settings.params.document_cookie == 0)) { | 1355 (settings.params.dpi < kMinDpi || settings.params.document_cookie == 0)) { |
| 1356 // Invalid print page settings. | 1356 // Invalid print page settings. |
| 1357 NOTREACHED(); | 1357 NOTREACHED(); |
| 1358 result = false; | 1358 result = false; |
| 1359 } | 1359 } |
| 1360 | 1360 |
| 1361 // Reset to default values. | 1361 // Reset to default values. |
| 1362 ignore_css_margins_ = false; | 1362 ignore_css_margins_ = false; |
| 1363 settings.pages.clear(); | 1363 settings.pages.clear(); |
| 1364 | 1364 |
| 1365 settings.params.print_scaling_option = | 1365 settings.params.print_scaling_option = |
| 1366 WebKit::WebPrintScalingOptionSourceSize; | 1366 blink::WebPrintScalingOptionSourceSize; |
| 1367 if (fit_to_paper_size) { | 1367 if (fit_to_paper_size) { |
| 1368 settings.params.print_scaling_option = | 1368 settings.params.print_scaling_option = |
| 1369 WebKit::WebPrintScalingOptionFitToPrintableArea; | 1369 blink::WebPrintScalingOptionFitToPrintableArea; |
| 1370 } | 1370 } |
| 1371 | 1371 |
| 1372 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); | 1372 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); |
| 1373 return result; | 1373 return result; |
| 1374 } | 1374 } |
| 1375 | 1375 |
| 1376 bool PrintWebViewHelper::CalculateNumberOfPages(WebKit::WebFrame* frame, | 1376 bool PrintWebViewHelper::CalculateNumberOfPages(blink::WebFrame* frame, |
| 1377 const WebKit::WebNode& node, | 1377 const blink::WebNode& node, |
| 1378 int* number_of_pages) { | 1378 int* number_of_pages) { |
| 1379 DCHECK(frame); | 1379 DCHECK(frame); |
| 1380 bool fit_to_paper_size = !(PrintingNodeOrPdfFrame(frame, node)); | 1380 bool fit_to_paper_size = !(PrintingNodeOrPdfFrame(frame, node)); |
| 1381 if (!InitPrintSettings(fit_to_paper_size)) { | 1381 if (!InitPrintSettings(fit_to_paper_size)) { |
| 1382 notify_browser_of_print_failure_ = false; | 1382 notify_browser_of_print_failure_ = false; |
| 1383 render_view()->RunModalAlertDialog( | 1383 render_view()->RunModalAlertDialog( |
| 1384 frame, | 1384 frame, |
| 1385 l10n_util::GetStringUTF16(IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS)); | 1385 l10n_util::GetStringUTF16(IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS)); |
| 1386 return false; | 1386 return false; |
| 1387 } | 1387 } |
| 1388 | 1388 |
| 1389 const PrintMsg_Print_Params& params = print_pages_params_->params; | 1389 const PrintMsg_Print_Params& params = print_pages_params_->params; |
| 1390 PrepareFrameAndViewForPrint prepare(params, frame, node, ignore_css_margins_); | 1390 PrepareFrameAndViewForPrint prepare(params, frame, node, ignore_css_margins_); |
| 1391 prepare.StartPrinting(); | 1391 prepare.StartPrinting(); |
| 1392 | 1392 |
| 1393 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(), | 1393 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(), |
| 1394 params.document_cookie)); | 1394 params.document_cookie)); |
| 1395 *number_of_pages = prepare.GetExpectedPageCount(); | 1395 *number_of_pages = prepare.GetExpectedPageCount(); |
| 1396 return true; | 1396 return true; |
| 1397 } | 1397 } |
| 1398 | 1398 |
| 1399 bool PrintWebViewHelper::UpdatePrintSettings( | 1399 bool PrintWebViewHelper::UpdatePrintSettings( |
| 1400 WebKit::WebFrame* frame, | 1400 blink::WebFrame* frame, |
| 1401 const WebKit::WebNode& node, | 1401 const blink::WebNode& node, |
| 1402 const base::DictionaryValue& passed_job_settings) { | 1402 const base::DictionaryValue& passed_job_settings) { |
| 1403 DCHECK(is_preview_enabled_); | 1403 DCHECK(is_preview_enabled_); |
| 1404 const base::DictionaryValue* job_settings = &passed_job_settings; | 1404 const base::DictionaryValue* job_settings = &passed_job_settings; |
| 1405 base::DictionaryValue modified_job_settings; | 1405 base::DictionaryValue modified_job_settings; |
| 1406 if (job_settings->empty()) { | 1406 if (job_settings->empty()) { |
| 1407 if (!print_for_preview_) | 1407 if (!print_for_preview_) |
| 1408 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); | 1408 print_preview_context_.set_error(PREVIEW_ERROR_BAD_SETTING); |
| 1409 return false; | 1409 return false; |
| 1410 } | 1410 } |
| 1411 | 1411 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 1432 PrintMsg_PrintPages_Params settings; | 1432 PrintMsg_PrintPages_Params settings; |
| 1433 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), cookie, *job_settings, | 1433 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), cookie, *job_settings, |
| 1434 &settings)); | 1434 &settings)); |
| 1435 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); | 1435 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); |
| 1436 | 1436 |
| 1437 if (!PrintMsg_Print_Params_IsValid(settings.params)) { | 1437 if (!PrintMsg_Print_Params_IsValid(settings.params)) { |
| 1438 if (!print_for_preview_) { | 1438 if (!print_for_preview_) { |
| 1439 print_preview_context_.set_error(PREVIEW_ERROR_INVALID_PRINTER_SETTINGS); | 1439 print_preview_context_.set_error(PREVIEW_ERROR_INVALID_PRINTER_SETTINGS); |
| 1440 } else { | 1440 } else { |
| 1441 // PrintForPrintPreview | 1441 // PrintForPrintPreview |
| 1442 WebKit::WebFrame* print_frame = NULL; | 1442 blink::WebFrame* print_frame = NULL; |
| 1443 // This may not be the right frame, but the alert will be modal, | 1443 // This may not be the right frame, but the alert will be modal, |
| 1444 // therefore it works well enough. | 1444 // therefore it works well enough. |
| 1445 GetPrintFrame(&print_frame); | 1445 GetPrintFrame(&print_frame); |
| 1446 if (print_frame) { | 1446 if (print_frame) { |
| 1447 render_view()->RunModalAlertDialog( | 1447 render_view()->RunModalAlertDialog( |
| 1448 print_frame, | 1448 print_frame, |
| 1449 l10n_util::GetStringUTF16( | 1449 l10n_util::GetStringUTF16( |
| 1450 IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS)); | 1450 IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS)); |
| 1451 } | 1451 } |
| 1452 } | 1452 } |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1492 } | 1492 } |
| 1493 } | 1493 } |
| 1494 | 1494 |
| 1495 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); | 1495 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); |
| 1496 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(), | 1496 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(), |
| 1497 settings.params.document_cookie)); | 1497 settings.params.document_cookie)); |
| 1498 | 1498 |
| 1499 return true; | 1499 return true; |
| 1500 } | 1500 } |
| 1501 | 1501 |
| 1502 bool PrintWebViewHelper::GetPrintSettingsFromUser(WebKit::WebFrame* frame, | 1502 bool PrintWebViewHelper::GetPrintSettingsFromUser(blink::WebFrame* frame, |
| 1503 const WebKit::WebNode& node, | 1503 const blink::WebNode& node, |
| 1504 int expected_pages_count) { | 1504 int expected_pages_count) { |
| 1505 PrintHostMsg_ScriptedPrint_Params params; | 1505 PrintHostMsg_ScriptedPrint_Params params; |
| 1506 PrintMsg_PrintPages_Params print_settings; | 1506 PrintMsg_PrintPages_Params print_settings; |
| 1507 | 1507 |
| 1508 params.cookie = print_pages_params_->params.document_cookie; | 1508 params.cookie = print_pages_params_->params.document_cookie; |
| 1509 params.has_selection = frame->hasSelection(); | 1509 params.has_selection = frame->hasSelection(); |
| 1510 params.expected_pages_count = expected_pages_count; | 1510 params.expected_pages_count = expected_pages_count; |
| 1511 MarginType margin_type = DEFAULT_MARGINS; | 1511 MarginType margin_type = DEFAULT_MARGINS; |
| 1512 if (PrintingNodeOrPdfFrame(frame, node)) | 1512 if (PrintingNodeOrPdfFrame(frame, node)) |
| 1513 margin_type = GetMarginsForPdf(frame, node); | 1513 margin_type = GetMarginsForPdf(frame, node); |
| 1514 params.margin_type = margin_type; | 1514 params.margin_type = margin_type; |
| 1515 | 1515 |
| 1516 Send(new PrintHostMsg_DidShowPrintDialog(routing_id())); | 1516 Send(new PrintHostMsg_DidShowPrintDialog(routing_id())); |
| 1517 | 1517 |
| 1518 // PrintHostMsg_ScriptedPrint will reset print_scaling_option, so we save the | 1518 // PrintHostMsg_ScriptedPrint will reset print_scaling_option, so we save the |
| 1519 // value before and restore it afterwards. | 1519 // value before and restore it afterwards. |
| 1520 WebKit::WebPrintScalingOption scaling_option = | 1520 blink::WebPrintScalingOption scaling_option = |
| 1521 print_pages_params_->params.print_scaling_option; | 1521 print_pages_params_->params.print_scaling_option; |
| 1522 | 1522 |
| 1523 print_pages_params_.reset(); | 1523 print_pages_params_.reset(); |
| 1524 IPC::SyncMessage* msg = | 1524 IPC::SyncMessage* msg = |
| 1525 new PrintHostMsg_ScriptedPrint(routing_id(), params, &print_settings); | 1525 new PrintHostMsg_ScriptedPrint(routing_id(), params, &print_settings); |
| 1526 msg->EnableMessagePumping(); | 1526 msg->EnableMessagePumping(); |
| 1527 Send(msg); | 1527 Send(msg); |
| 1528 print_pages_params_.reset(new PrintMsg_PrintPages_Params(print_settings)); | 1528 print_pages_params_.reset(new PrintMsg_PrintPages_Params(print_settings)); |
| 1529 | 1529 |
| 1530 print_pages_params_->params.print_scaling_option = scaling_option; | 1530 print_pages_params_->params.print_scaling_option = scaling_option; |
| 1531 return (print_settings.params.dpi && print_settings.params.document_cookie); | 1531 return (print_settings.params.dpi && print_settings.params.document_cookie); |
| 1532 } | 1532 } |
| 1533 | 1533 |
| 1534 bool PrintWebViewHelper::RenderPagesForPrint(WebKit::WebFrame* frame, | 1534 bool PrintWebViewHelper::RenderPagesForPrint(blink::WebFrame* frame, |
| 1535 const WebKit::WebNode& node) { | 1535 const blink::WebNode& node) { |
| 1536 if (!frame || prep_frame_view_) | 1536 if (!frame || prep_frame_view_) |
| 1537 return false; | 1537 return false; |
| 1538 const PrintMsg_PrintPages_Params& params = *print_pages_params_; | 1538 const PrintMsg_PrintPages_Params& params = *print_pages_params_; |
| 1539 const PrintMsg_Print_Params& print_params = params.params; | 1539 const PrintMsg_Print_Params& print_params = params.params; |
| 1540 prep_frame_view_.reset( | 1540 prep_frame_view_.reset( |
| 1541 new PrepareFrameAndViewForPrint(print_params, frame, node, | 1541 new PrepareFrameAndViewForPrint(print_params, frame, node, |
| 1542 ignore_css_margins_)); | 1542 ignore_css_margins_)); |
| 1543 DCHECK(!print_pages_params_->params.selection_only || | 1543 DCHECK(!print_pages_params_->params.selection_only || |
| 1544 print_pages_params_->pages.empty()); | 1544 print_pages_params_->pages.empty()); |
| 1545 prep_frame_view_->CopySelectionIfNeeded( | 1545 prep_frame_view_->CopySelectionIfNeeded( |
| (...skipping 19 matching lines...) Expand all Loading... |
| 1565 shared_mem_handle); | 1565 shared_mem_handle); |
| 1566 return true; | 1566 return true; |
| 1567 } | 1567 } |
| 1568 } | 1568 } |
| 1569 NOTREACHED(); | 1569 NOTREACHED(); |
| 1570 return false; | 1570 return false; |
| 1571 } | 1571 } |
| 1572 #endif // defined(OS_POSIX) | 1572 #endif // defined(OS_POSIX) |
| 1573 | 1573 |
| 1574 bool PrintWebViewHelper::IsScriptInitiatedPrintTooFrequent( | 1574 bool PrintWebViewHelper::IsScriptInitiatedPrintTooFrequent( |
| 1575 WebKit::WebFrame* frame) { | 1575 blink::WebFrame* frame) { |
| 1576 const int kMinSecondsToIgnoreJavascriptInitiatedPrint = 2; | 1576 const int kMinSecondsToIgnoreJavascriptInitiatedPrint = 2; |
| 1577 const int kMaxSecondsToIgnoreJavascriptInitiatedPrint = 32; | 1577 const int kMaxSecondsToIgnoreJavascriptInitiatedPrint = 32; |
| 1578 bool too_frequent = false; | 1578 bool too_frequent = false; |
| 1579 | 1579 |
| 1580 // Check if there is script repeatedly trying to print and ignore it if too | 1580 // Check if there is script repeatedly trying to print and ignore it if too |
| 1581 // frequent. The first 3 times, we use a constant wait time, but if this | 1581 // frequent. The first 3 times, we use a constant wait time, but if this |
| 1582 // gets excessive, we switch to exponential wait time. So for a page that | 1582 // gets excessive, we switch to exponential wait time. So for a page that |
| 1583 // calls print() in a loop the user will need to cancel the print dialog | 1583 // calls print() in a loop the user will need to cancel the print dialog |
| 1584 // after: [2, 2, 2, 4, 8, 16, 32, 32, ...] seconds. | 1584 // after: [2, 2, 2, 4, 8, 16, 32, 32, ...] seconds. |
| 1585 // This gives the user time to navigate from the page. | 1585 // This gives the user time to navigate from the page. |
| 1586 if (user_cancelled_scripted_print_count_ > 0) { | 1586 if (user_cancelled_scripted_print_count_ > 0) { |
| 1587 base::TimeDelta diff = base::Time::Now() - last_cancelled_script_print_; | 1587 base::TimeDelta diff = base::Time::Now() - last_cancelled_script_print_; |
| 1588 int min_wait_seconds = kMinSecondsToIgnoreJavascriptInitiatedPrint; | 1588 int min_wait_seconds = kMinSecondsToIgnoreJavascriptInitiatedPrint; |
| 1589 if (user_cancelled_scripted_print_count_ > 3) { | 1589 if (user_cancelled_scripted_print_count_ > 3) { |
| 1590 min_wait_seconds = std::min( | 1590 min_wait_seconds = std::min( |
| 1591 kMinSecondsToIgnoreJavascriptInitiatedPrint << | 1591 kMinSecondsToIgnoreJavascriptInitiatedPrint << |
| 1592 (user_cancelled_scripted_print_count_ - 3), | 1592 (user_cancelled_scripted_print_count_ - 3), |
| 1593 kMaxSecondsToIgnoreJavascriptInitiatedPrint); | 1593 kMaxSecondsToIgnoreJavascriptInitiatedPrint); |
| 1594 } | 1594 } |
| 1595 if (diff.InSeconds() < min_wait_seconds) { | 1595 if (diff.InSeconds() < min_wait_seconds) { |
| 1596 too_frequent = true; | 1596 too_frequent = true; |
| 1597 } | 1597 } |
| 1598 } | 1598 } |
| 1599 | 1599 |
| 1600 if (!too_frequent) | 1600 if (!too_frequent) |
| 1601 return false; | 1601 return false; |
| 1602 | 1602 |
| 1603 WebKit::WebString message( | 1603 blink::WebString message( |
| 1604 WebKit::WebString::fromUTF8("Ignoring too frequent calls to print().")); | 1604 blink::WebString::fromUTF8("Ignoring too frequent calls to print().")); |
| 1605 frame->addMessageToConsole( | 1605 frame->addMessageToConsole( |
| 1606 WebKit::WebConsoleMessage( | 1606 blink::WebConsoleMessage( |
| 1607 WebKit::WebConsoleMessage::LevelWarning, message)); | 1607 blink::WebConsoleMessage::LevelWarning, message)); |
| 1608 return true; | 1608 return true; |
| 1609 } | 1609 } |
| 1610 | 1610 |
| 1611 void PrintWebViewHelper::ResetScriptedPrintCount() { | 1611 void PrintWebViewHelper::ResetScriptedPrintCount() { |
| 1612 // Reset cancel counter on successful print. | 1612 // Reset cancel counter on successful print. |
| 1613 user_cancelled_scripted_print_count_ = 0; | 1613 user_cancelled_scripted_print_count_ = 0; |
| 1614 } | 1614 } |
| 1615 | 1615 |
| 1616 void PrintWebViewHelper::IncrementScriptedPrintCount() { | 1616 void PrintWebViewHelper::IncrementScriptedPrintCount() { |
| 1617 ++user_cancelled_scripted_print_count_; | 1617 ++user_cancelled_scripted_print_count_; |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1735 generate_draft_pages_(true), | 1735 generate_draft_pages_(true), |
| 1736 print_ready_metafile_page_count_(0), | 1736 print_ready_metafile_page_count_(0), |
| 1737 error_(PREVIEW_ERROR_NONE), | 1737 error_(PREVIEW_ERROR_NONE), |
| 1738 state_(UNINITIALIZED) { | 1738 state_(UNINITIALIZED) { |
| 1739 } | 1739 } |
| 1740 | 1740 |
| 1741 PrintWebViewHelper::PrintPreviewContext::~PrintPreviewContext() { | 1741 PrintWebViewHelper::PrintPreviewContext::~PrintPreviewContext() { |
| 1742 } | 1742 } |
| 1743 | 1743 |
| 1744 void PrintWebViewHelper::PrintPreviewContext::InitWithFrame( | 1744 void PrintWebViewHelper::PrintPreviewContext::InitWithFrame( |
| 1745 WebKit::WebFrame* web_frame) { | 1745 blink::WebFrame* web_frame) { |
| 1746 DCHECK(web_frame); | 1746 DCHECK(web_frame); |
| 1747 DCHECK(!IsRendering()); | 1747 DCHECK(!IsRendering()); |
| 1748 state_ = INITIALIZED; | 1748 state_ = INITIALIZED; |
| 1749 source_frame_.Reset(web_frame); | 1749 source_frame_.Reset(web_frame); |
| 1750 source_node_.reset(); | 1750 source_node_.reset(); |
| 1751 } | 1751 } |
| 1752 | 1752 |
| 1753 void PrintWebViewHelper::PrintPreviewContext::InitWithNode( | 1753 void PrintWebViewHelper::PrintPreviewContext::InitWithNode( |
| 1754 const WebKit::WebNode& web_node) { | 1754 const blink::WebNode& web_node) { |
| 1755 DCHECK(!web_node.isNull()); | 1755 DCHECK(!web_node.isNull()); |
| 1756 DCHECK(web_node.document().frame()); | 1756 DCHECK(web_node.document().frame()); |
| 1757 DCHECK(!IsRendering()); | 1757 DCHECK(!IsRendering()); |
| 1758 state_ = INITIALIZED; | 1758 state_ = INITIALIZED; |
| 1759 source_frame_.Reset(web_node.document().frame()); | 1759 source_frame_.Reset(web_node.document().frame()); |
| 1760 source_node_ = web_node; | 1760 source_node_ = web_node; |
| 1761 } | 1761 } |
| 1762 | 1762 |
| 1763 void PrintWebViewHelper::PrintPreviewContext::OnPrintPreview() { | 1763 void PrintWebViewHelper::PrintPreviewContext::OnPrintPreview() { |
| 1764 DCHECK_EQ(INITIALIZED, state_); | 1764 DCHECK_EQ(INITIALIZED, state_); |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1911 bool generate_draft_pages) { | 1911 bool generate_draft_pages) { |
| 1912 DCHECK_EQ(INITIALIZED, state_); | 1912 DCHECK_EQ(INITIALIZED, state_); |
| 1913 generate_draft_pages_ = generate_draft_pages; | 1913 generate_draft_pages_ = generate_draft_pages; |
| 1914 } | 1914 } |
| 1915 | 1915 |
| 1916 void PrintWebViewHelper::PrintPreviewContext::set_error( | 1916 void PrintWebViewHelper::PrintPreviewContext::set_error( |
| 1917 enum PrintPreviewErrorBuckets error) { | 1917 enum PrintPreviewErrorBuckets error) { |
| 1918 error_ = error; | 1918 error_ = error; |
| 1919 } | 1919 } |
| 1920 | 1920 |
| 1921 WebKit::WebFrame* PrintWebViewHelper::PrintPreviewContext::source_frame() { | 1921 blink::WebFrame* PrintWebViewHelper::PrintPreviewContext::source_frame() { |
| 1922 DCHECK(state_ != UNINITIALIZED); | 1922 DCHECK(state_ != UNINITIALIZED); |
| 1923 return source_frame_.GetFrame(); | 1923 return source_frame_.GetFrame(); |
| 1924 } | 1924 } |
| 1925 | 1925 |
| 1926 const WebKit::WebNode& | 1926 const blink::WebNode& |
| 1927 PrintWebViewHelper::PrintPreviewContext::source_node() const { | 1927 PrintWebViewHelper::PrintPreviewContext::source_node() const { |
| 1928 DCHECK(state_ != UNINITIALIZED); | 1928 DCHECK(state_ != UNINITIALIZED); |
| 1929 return source_node_; | 1929 return source_node_; |
| 1930 } | 1930 } |
| 1931 | 1931 |
| 1932 WebKit::WebFrame* PrintWebViewHelper::PrintPreviewContext::prepared_frame() { | 1932 blink::WebFrame* PrintWebViewHelper::PrintPreviewContext::prepared_frame() { |
| 1933 DCHECK(state_ != UNINITIALIZED); | 1933 DCHECK(state_ != UNINITIALIZED); |
| 1934 return prep_frame_view_->frame(); | 1934 return prep_frame_view_->frame(); |
| 1935 } | 1935 } |
| 1936 | 1936 |
| 1937 const WebKit::WebNode& | 1937 const blink::WebNode& |
| 1938 PrintWebViewHelper::PrintPreviewContext::prepared_node() const { | 1938 PrintWebViewHelper::PrintPreviewContext::prepared_node() const { |
| 1939 DCHECK(state_ != UNINITIALIZED); | 1939 DCHECK(state_ != UNINITIALIZED); |
| 1940 return prep_frame_view_->node(); | 1940 return prep_frame_view_->node(); |
| 1941 } | 1941 } |
| 1942 | 1942 |
| 1943 int PrintWebViewHelper::PrintPreviewContext::total_page_count() const { | 1943 int PrintWebViewHelper::PrintPreviewContext::total_page_count() const { |
| 1944 DCHECK(state_ != UNINITIALIZED); | 1944 DCHECK(state_ != UNINITIALIZED); |
| 1945 return total_page_count_; | 1945 return total_page_count_; |
| 1946 } | 1946 } |
| 1947 | 1947 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 1964 } | 1964 } |
| 1965 | 1965 |
| 1966 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { | 1966 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
| 1967 prep_frame_view_.reset(); | 1967 prep_frame_view_.reset(); |
| 1968 metafile_.reset(); | 1968 metafile_.reset(); |
| 1969 pages_to_render_.clear(); | 1969 pages_to_render_.clear(); |
| 1970 error_ = PREVIEW_ERROR_NONE; | 1970 error_ = PREVIEW_ERROR_NONE; |
| 1971 } | 1971 } |
| 1972 | 1972 |
| 1973 } // namespace printing | 1973 } // namespace printing |
| OLD | NEW |