Chromium Code Reviews| 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" | |
| 11 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
| 12 #include "base/logging.h" | 11 #include "base/logging.h" |
| 13 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 14 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 15 #include "base/process/process_handle.h" | 14 #include "base/process/process_handle.h" |
| 16 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
| 18 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 19 #include "chrome/common/chrome_switches.h" | |
| 20 #include "chrome/common/print_messages.h" | 18 #include "chrome/common/print_messages.h" |
| 21 #include "chrome/common/render_messages.h" | 19 #include "chrome/renderer/printing/print_web_view_helper_delegate.h" |
| 22 #include "chrome/grit/browser_resources.h" | |
| 23 #include "chrome/renderer/prerender/prerender_helper.h" | |
| 24 #include "content/public/common/web_preferences.h" | 20 #include "content/public/common/web_preferences.h" |
| 25 #include "content/public/renderer/render_frame.h" | 21 #include "content/public/renderer/render_frame.h" |
| 26 #include "content/public/renderer/render_thread.h" | 22 #include "content/public/renderer/render_thread.h" |
| 27 #include "content/public/renderer/render_view.h" | 23 #include "content/public/renderer/render_view.h" |
| 28 #include "net/base/escape.h" | 24 #include "net/base/escape.h" |
| 29 #include "printing/pdf_metafile_skia.h" | 25 #include "printing/pdf_metafile_skia.h" |
| 30 #include "printing/units.h" | 26 #include "printing/units.h" |
| 31 #include "third_party/WebKit/public/platform/WebSize.h" | 27 #include "third_party/WebKit/public/platform/WebSize.h" |
| 32 #include "third_party/WebKit/public/platform/WebURLRequest.h" | 28 #include "third_party/WebKit/public/platform/WebURLRequest.h" |
| 33 #include "third_party/WebKit/public/web/WebConsoleMessage.h" | 29 #include "third_party/WebKit/public/web/WebConsoleMessage.h" |
| 34 #include "third_party/WebKit/public/web/WebDocument.h" | 30 #include "third_party/WebKit/public/web/WebDocument.h" |
| 35 #include "third_party/WebKit/public/web/WebElement.h" | 31 #include "third_party/WebKit/public/web/WebElement.h" |
| 36 #include "third_party/WebKit/public/web/WebFrameClient.h" | 32 #include "third_party/WebKit/public/web/WebFrameClient.h" |
| 37 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 33 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 38 #include "third_party/WebKit/public/web/WebPlugin.h" | 34 #include "third_party/WebKit/public/web/WebPlugin.h" |
| 39 #include "third_party/WebKit/public/web/WebPluginDocument.h" | 35 #include "third_party/WebKit/public/web/WebPluginDocument.h" |
| 40 #include "third_party/WebKit/public/web/WebPrintParams.h" | 36 #include "third_party/WebKit/public/web/WebPrintParams.h" |
| 41 #include "third_party/WebKit/public/web/WebPrintPresetOptions.h" | 37 #include "third_party/WebKit/public/web/WebPrintPresetOptions.h" |
| 42 #include "third_party/WebKit/public/web/WebPrintScalingOption.h" | 38 #include "third_party/WebKit/public/web/WebPrintScalingOption.h" |
| 43 #include "third_party/WebKit/public/web/WebScriptSource.h" | 39 #include "third_party/WebKit/public/web/WebScriptSource.h" |
| 44 #include "third_party/WebKit/public/web/WebSettings.h" | 40 #include "third_party/WebKit/public/web/WebSettings.h" |
| 45 #include "third_party/WebKit/public/web/WebView.h" | 41 #include "third_party/WebKit/public/web/WebView.h" |
| 46 #include "third_party/WebKit/public/web/WebViewClient.h" | 42 #include "third_party/WebKit/public/web/WebViewClient.h" |
| 47 #include "ui/base/resource/resource_bundle.h" | |
| 48 | |
| 49 #if defined(ENABLE_EXTENSIONS) | |
| 50 #include "chrome/common/extensions/extension_constants.h" | |
| 51 #include "extensions/common/constants.h" | |
| 52 #endif // defined(ENABLE_EXTENSIONS) | |
| 53 | 43 |
| 54 using content::WebPreferences; | 44 using content::WebPreferences; |
| 55 | 45 |
| 56 namespace printing { | 46 namespace printing { |
| 57 | 47 |
| 58 namespace { | 48 namespace { |
| 59 | 49 |
| 60 enum PrintPreviewHelperEvents { | 50 enum PrintPreviewHelperEvents { |
| 61 PREVIEW_EVENT_REQUESTED, | 51 PREVIEW_EVENT_REQUESTED, |
| 62 PREVIEW_EVENT_CACHE_HIT, // Unused | 52 PREVIEW_EVENT_CACHE_HIT, // Unused |
| (...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 403 } | 393 } |
| 404 | 394 |
| 405 if (fit_to_page) { | 395 if (fit_to_page) { |
| 406 double factor = FitPrintParamsToPage(params, &result_params); | 396 double factor = FitPrintParamsToPage(params, &result_params); |
| 407 if (scale_factor) | 397 if (scale_factor) |
| 408 *scale_factor = factor; | 398 *scale_factor = factor; |
| 409 } | 399 } |
| 410 return result_params; | 400 return result_params; |
| 411 } | 401 } |
| 412 | 402 |
| 413 // Return the PDF object element if |frame| is the out of process PDF extension. | |
| 414 blink::WebElement GetPdfElement(blink::WebLocalFrame* frame) { | |
| 415 #if defined(ENABLE_EXTENSIONS) | |
| 416 GURL url = frame->document().url(); | |
| 417 if (url.SchemeIs(extensions::kExtensionScheme) && | |
|
Vitaly Buka (NO REVIEWS)
2015/01/05 19:57:03
Is this because of extensions?
Seems components ar
dgn
2015/01/05 22:45:47
That was about the extensionId but apparently it's
| |
| 418 url.host() == extension_misc::kPdfExtensionId) { | |
| 419 // <object> with id="plugin" is created in | |
| 420 // chrome/browser/resources/pdf/pdf.js. | |
| 421 auto plugin_element = frame->document().getElementById("plugin"); | |
| 422 if (!plugin_element.isNull()) { | |
| 423 return plugin_element; | |
| 424 } | |
| 425 NOTREACHED(); | |
| 426 } | |
| 427 #endif // defined(ENABLE_EXTENSIONS) | |
| 428 return blink::WebElement(); | |
| 429 } | |
| 430 | |
| 431 } // namespace | 403 } // namespace |
| 432 | 404 |
| 433 FrameReference::FrameReference(blink::WebLocalFrame* frame) { | 405 FrameReference::FrameReference(blink::WebLocalFrame* frame) { |
| 434 Reset(frame); | 406 Reset(frame); |
| 435 } | 407 } |
| 436 | 408 |
| 437 FrameReference::FrameReference() { | 409 FrameReference::FrameReference() { |
| 438 Reset(NULL); | 410 Reset(NULL); |
| 439 } | 411 } |
| 440 | 412 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 483 page_layout.content_width, | 455 page_layout.content_width, |
| 484 page_layout.margin_top + page_layout.margin_bottom + | 456 page_layout.margin_top + page_layout.margin_bottom + |
| 485 page_layout.content_height); | 457 page_layout.content_height); |
| 486 | 458 |
| 487 blink::WebView* web_view = blink::WebView::create(NULL); | 459 blink::WebView* web_view = blink::WebView::create(NULL); |
| 488 web_view->settings()->setJavaScriptEnabled(true); | 460 web_view->settings()->setJavaScriptEnabled(true); |
| 489 | 461 |
| 490 blink::WebLocalFrame* frame = blink::WebLocalFrame::create(NULL); | 462 blink::WebLocalFrame* frame = blink::WebLocalFrame::create(NULL); |
| 491 web_view->setMainFrame(frame); | 463 web_view->setMainFrame(frame); |
| 492 | 464 |
| 493 base::StringValue html(ResourceBundle::GetSharedInstance().GetLocalizedString( | |
| 494 IDR_PRINT_PREVIEW_PAGE)); | |
| 495 // Load page with script to avoid async operations. | 465 // Load page with script to avoid async operations. |
|
Vitaly Buka (NO REVIEWS)
2015/01/05 19:57:03
this resource has nothing chrome specific so it sh
dgn
2015/01/05 22:45:47
Acknowledged.
| |
| 496 ExecuteScript(frame, kPageLoadScriptFormat, html); | 466 ExecuteScript(frame, kPageLoadScriptFormat, delegate_->GetPrintPreviewHtml()); |
| 497 | 467 |
| 498 scoped_ptr<base::DictionaryValue> options(new base::DictionaryValue()); | 468 scoped_ptr<base::DictionaryValue> options(new base::DictionaryValue()); |
| 499 options.reset(new base::DictionaryValue()); | 469 options.reset(new base::DictionaryValue()); |
| 500 options->SetDouble(kSettingHeaderFooterDate, base::Time::Now().ToJsTime()); | 470 options->SetDouble(kSettingHeaderFooterDate, base::Time::Now().ToJsTime()); |
| 501 options->SetDouble("width", page_size.width); | 471 options->SetDouble("width", page_size.width); |
| 502 options->SetDouble("height", page_size.height); | 472 options->SetDouble("height", page_size.height); |
| 503 options->SetDouble("topMargin", page_layout.margin_top); | 473 options->SetDouble("topMargin", page_layout.margin_top); |
| 504 options->SetDouble("bottomMargin", page_layout.margin_bottom); | 474 options->SetDouble("bottomMargin", page_layout.margin_bottom); |
| 505 options->SetString("pageNumber", | 475 options->SetString("pageNumber", |
| 506 base::StringPrintf("%d/%d", page_number, total_pages)); | 476 base::StringPrintf("%d/%d", page_number, total_pages)); |
| (...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 783 if (owns_web_view_) { | 753 if (owns_web_view_) { |
| 784 DCHECK(!frame->isLoading()); | 754 DCHECK(!frame->isLoading()); |
| 785 owns_web_view_ = false; | 755 owns_web_view_ = false; |
| 786 web_view->close(); | 756 web_view->close(); |
| 787 } | 757 } |
| 788 } | 758 } |
| 789 frame_.Reset(NULL); | 759 frame_.Reset(NULL); |
| 790 on_ready_.Reset(); | 760 on_ready_.Reset(); |
| 791 } | 761 } |
| 792 | 762 |
| 793 PrintWebViewHelper::PrintWebViewHelper(content::RenderView* render_view) | 763 PrintWebViewHelper::PrintWebViewHelper( |
| 764 content::RenderView* render_view, | |
| 765 scoped_ptr<PrintWebViewHelperDelegate> delegate) | |
| 794 : content::RenderViewObserver(render_view), | 766 : content::RenderViewObserver(render_view), |
| 795 content::RenderViewObserverTracker<PrintWebViewHelper>(render_view), | 767 content::RenderViewObserverTracker<PrintWebViewHelper>(render_view), |
| 796 reset_prep_frame_view_(false), | 768 reset_prep_frame_view_(false), |
| 797 is_print_ready_metafile_sent_(false), | 769 is_print_ready_metafile_sent_(false), |
| 798 ignore_css_margins_(false), | 770 ignore_css_margins_(false), |
| 799 is_scripted_printing_blocked_(false), | 771 is_scripted_printing_blocked_(false), |
| 800 notify_browser_of_print_failure_(true), | 772 notify_browser_of_print_failure_(true), |
| 801 print_for_preview_(false), | 773 print_for_preview_(false), |
| 774 delegate_(delegate.Pass()), | |
| 802 print_node_in_progress_(false), | 775 print_node_in_progress_(false), |
| 803 is_loading_(false), | 776 is_loading_(false), |
| 804 is_scripted_preview_delayed_(false), | 777 is_scripted_preview_delayed_(false), |
| 805 weak_ptr_factory_(this) { | 778 weak_ptr_factory_(this) { |
| 806 if (CommandLine::ForCurrentProcess()->HasSwitch( | 779 if (delegate_->PrintPreviewDisabled()) { |
|
Vitaly Buka (NO REVIEWS)
2015/01/05 19:57:03
this switch could go to components
not sure yet if
dgn
2015/01/05 22:45:47
Replaced with a constructor argument
| |
| 807 switches::kDisablePrintPreview)) { | |
| 808 DisablePreview(); | 780 DisablePreview(); |
| 809 } | 781 } |
| 810 } | 782 } |
| 811 | 783 |
| 812 PrintWebViewHelper::~PrintWebViewHelper() {} | 784 PrintWebViewHelper::~PrintWebViewHelper() {} |
| 813 | 785 |
| 814 // static | 786 // static |
| 815 void PrintWebViewHelper::DisablePreview() { | 787 void PrintWebViewHelper::DisablePreview() { |
| 816 g_is_preview_enabled_ = false; | 788 g_is_preview_enabled_ = false; |
| 817 } | 789 } |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 837 on_stop_loading_closure_.Run(); | 809 on_stop_loading_closure_.Run(); |
| 838 on_stop_loading_closure_.Reset(); | 810 on_stop_loading_closure_.Reset(); |
| 839 } | 811 } |
| 840 } | 812 } |
| 841 | 813 |
| 842 // Prints |frame| which called window.print(). | 814 // Prints |frame| which called window.print(). |
| 843 void PrintWebViewHelper::PrintPage(blink::WebLocalFrame* frame, | 815 void PrintWebViewHelper::PrintPage(blink::WebLocalFrame* frame, |
| 844 bool user_initiated) { | 816 bool user_initiated) { |
| 845 DCHECK(frame); | 817 DCHECK(frame); |
| 846 | 818 |
| 847 // Allow Prerendering to cancel this print request if necessary. | 819 // In Chrome, allows prerendering to cancel this print request if necessary. |
| 848 if (prerender::PrerenderHelper::IsPrerendering( | 820 IPC::Message* cancel_message = delegate_->GetScriptedPrintCancelMessage( |
| 849 render_view()->GetMainRenderFrame())) { | 821 render_view(), routing_id()); |
| 850 Send(new ChromeViewHostMsg_CancelPrerenderForPrinting(routing_id())); | 822 if (cancel_message) { |
| 823 Send(cancel_message); | |
| 851 return; | 824 return; |
| 852 } | 825 } |
| 853 | 826 |
| 854 if (!IsScriptInitiatedPrintAllowed(frame, user_initiated)) | 827 if (!IsScriptInitiatedPrintAllowed(frame, user_initiated)) |
| 855 return; | 828 return; |
| 856 | 829 |
| 857 if (!g_is_preview_enabled_) { | 830 if (!g_is_preview_enabled_) { |
| 858 Print(frame, blink::WebNode(), true); | 831 Print(frame, blink::WebNode(), true); |
| 859 } else { | 832 } else { |
| 860 print_preview_context_.InitWithFrame(frame); | 833 print_preview_context_.InitWithFrame(frame); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 899 if (pdf_element.isNull()) { | 872 if (pdf_element.isNull()) { |
| 900 NOTREACHED(); | 873 NOTREACHED(); |
| 901 return; | 874 return; |
| 902 } | 875 } |
| 903 | 876 |
| 904 // The out-of-process plugin element is nested within a frame. In tests, there | 877 // The out-of-process plugin element is nested within a frame. In tests, there |
| 905 // may not be an iframe containing the out-of-process plugin, so continue with | 878 // may not be an iframe containing the out-of-process plugin, so continue with |
| 906 // the element with ID "pdf-viewer" if it isn't an iframe. | 879 // the element with ID "pdf-viewer" if it isn't an iframe. |
| 907 blink::WebLocalFrame* plugin_frame = pdf_element.document().frame(); | 880 blink::WebLocalFrame* plugin_frame = pdf_element.document().frame(); |
| 908 blink::WebElement plugin_element = pdf_element; | 881 blink::WebElement plugin_element = pdf_element; |
| 909 if (switches::OutOfProcessPdfEnabled() && | 882 if (delegate_->OutOfProcessPdfEnabled() && |
| 910 pdf_element.hasHTMLTagName("iframe")) { | 883 pdf_element.hasHTMLTagName("iframe")) { |
| 911 plugin_frame = blink::WebLocalFrame::fromFrameOwnerElement(pdf_element); | 884 plugin_frame = blink::WebLocalFrame::fromFrameOwnerElement(pdf_element); |
| 912 plugin_element = GetPdfElement(plugin_frame); | 885 plugin_element = delegate_->GetPdfElement(plugin_frame); |
| 913 if (plugin_element.isNull()) { | 886 if (plugin_element.isNull()) { |
| 914 NOTREACHED(); | 887 NOTREACHED(); |
| 915 return; | 888 return; |
| 916 } | 889 } |
| 917 } | 890 } |
| 918 | 891 |
| 919 // Set |print_for_preview_| flag and autoreset it to back to original | 892 // Set |print_for_preview_| flag and autoreset it to back to original |
| 920 // on return. | 893 // on return. |
| 921 base::AutoReset<bool> set_printing_flag(&print_for_preview_, true); | 894 base::AutoReset<bool> set_printing_flag(&print_for_preview_, true); |
| 922 | 895 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 962 return true; | 935 return true; |
| 963 } | 936 } |
| 964 | 937 |
| 965 #if defined(ENABLE_BASIC_PRINTING) | 938 #if defined(ENABLE_BASIC_PRINTING) |
| 966 void PrintWebViewHelper::OnPrintPages() { | 939 void PrintWebViewHelper::OnPrintPages() { |
| 967 blink::WebLocalFrame* frame; | 940 blink::WebLocalFrame* frame; |
| 968 if (!GetPrintFrame(&frame)) | 941 if (!GetPrintFrame(&frame)) |
| 969 return; | 942 return; |
| 970 // If we are printing a PDF extension frame, find the plugin node and print | 943 // If we are printing a PDF extension frame, find the plugin node and print |
| 971 // that instead. | 944 // that instead. |
| 972 auto plugin = GetPdfElement(frame); | 945 auto plugin = delegate_->GetPdfElement(frame); |
| 973 Print(frame, plugin, false); | 946 Print(frame, plugin, false); |
| 974 } | 947 } |
| 975 | 948 |
| 976 void PrintWebViewHelper::OnPrintForSystemDialog() { | 949 void PrintWebViewHelper::OnPrintForSystemDialog() { |
| 977 blink::WebLocalFrame* frame = print_preview_context_.source_frame(); | 950 blink::WebLocalFrame* frame = print_preview_context_.source_frame(); |
| 978 if (!frame) { | 951 if (!frame) { |
| 979 NOTREACHED(); | 952 NOTREACHED(); |
| 980 return; | 953 return; |
| 981 } | 954 } |
| 982 Print(frame, print_preview_context_.source_node(), false); | 955 Print(frame, print_preview_context_.source_node(), false); |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1217 void PrintWebViewHelper::SetScriptedPrintBlocked(bool blocked) { | 1190 void PrintWebViewHelper::SetScriptedPrintBlocked(bool blocked) { |
| 1218 is_scripted_printing_blocked_ = blocked; | 1191 is_scripted_printing_blocked_ = blocked; |
| 1219 } | 1192 } |
| 1220 | 1193 |
| 1221 void PrintWebViewHelper::OnInitiatePrintPreview(bool selection_only) { | 1194 void PrintWebViewHelper::OnInitiatePrintPreview(bool selection_only) { |
| 1222 blink::WebLocalFrame* frame = NULL; | 1195 blink::WebLocalFrame* frame = NULL; |
| 1223 GetPrintFrame(&frame); | 1196 GetPrintFrame(&frame); |
| 1224 DCHECK(frame); | 1197 DCHECK(frame); |
| 1225 // If we are printing a PDF extension frame, find the plugin node and print | 1198 // If we are printing a PDF extension frame, find the plugin node and print |
| 1226 // that instead. | 1199 // that instead. |
| 1227 auto plugin = GetPdfElement(frame); | 1200 auto plugin = delegate_->GetPdfElement(frame); |
| 1228 if (!plugin.isNull()) { | 1201 if (!plugin.isNull()) { |
| 1229 PrintNode(plugin); | 1202 PrintNode(plugin); |
| 1230 return; | 1203 return; |
| 1231 } | 1204 } |
| 1232 print_preview_context_.InitWithFrame(frame); | 1205 print_preview_context_.InitWithFrame(frame); |
| 1233 RequestPrintPreview(selection_only ? | 1206 RequestPrintPreview(selection_only ? |
| 1234 PRINT_PREVIEW_USER_INITIATED_SELECTION : | 1207 PRINT_PREVIEW_USER_INITIATED_SELECTION : |
| 1235 PRINT_PREVIEW_USER_INITIATED_ENTIRE_FRAME); | 1208 PRINT_PREVIEW_USER_INITIATED_ENTIRE_FRAME); |
| 1236 } | 1209 } |
| 1237 | 1210 |
| (...skipping 817 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2055 blink::WebConsoleMessage::LevelWarning, message)); | 2028 blink::WebConsoleMessage::LevelWarning, message)); |
| 2056 return false; | 2029 return false; |
| 2057 } | 2030 } |
| 2058 | 2031 |
| 2059 void PrintWebViewHelper::ScriptingThrottler::Reset() { | 2032 void PrintWebViewHelper::ScriptingThrottler::Reset() { |
| 2060 // Reset counter on successful print. | 2033 // Reset counter on successful print. |
| 2061 count_ = 0; | 2034 count_ = 0; |
| 2062 } | 2035 } |
| 2063 | 2036 |
| 2064 } // namespace printing | 2037 } // namespace printing |
| OLD | NEW |