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" | |
22 #include "chrome/grit/browser_resources.h" | 19 #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" | 43 #include "ui/base/resource/resource_bundle.h" |
48 | 44 |
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 | |
54 using content::WebPreferences; | 45 using content::WebPreferences; |
55 | 46 |
56 namespace printing { | 47 namespace printing { |
57 | 48 |
58 namespace { | 49 namespace { |
59 | 50 |
60 enum PrintPreviewHelperEvents { | 51 enum PrintPreviewHelperEvents { |
61 PREVIEW_EVENT_REQUESTED, | 52 PREVIEW_EVENT_REQUESTED, |
62 PREVIEW_EVENT_CACHE_HIT, // Unused | 53 PREVIEW_EVENT_CACHE_HIT, // Unused |
63 PREVIEW_EVENT_CREATE_DOCUMENT, | 54 PREVIEW_EVENT_CREATE_DOCUMENT, |
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 } | 394 } |
404 | 395 |
405 if (fit_to_page) { | 396 if (fit_to_page) { |
406 double factor = FitPrintParamsToPage(params, &result_params); | 397 double factor = FitPrintParamsToPage(params, &result_params); |
407 if (scale_factor) | 398 if (scale_factor) |
408 *scale_factor = factor; | 399 *scale_factor = factor; |
409 } | 400 } |
410 return result_params; | 401 return result_params; |
411 } | 402 } |
412 | 403 |
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) && | |
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 | 404 } // namespace |
432 | 405 |
433 FrameReference::FrameReference(blink::WebLocalFrame* frame) { | 406 FrameReference::FrameReference(blink::WebLocalFrame* frame) { |
434 Reset(frame); | 407 Reset(frame); |
435 } | 408 } |
436 | 409 |
437 FrameReference::FrameReference() { | 410 FrameReference::FrameReference() { |
438 Reset(NULL); | 411 Reset(NULL); |
439 } | 412 } |
440 | 413 |
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
772 if (owns_web_view_) { | 745 if (owns_web_view_) { |
773 DCHECK(!frame->isLoading()); | 746 DCHECK(!frame->isLoading()); |
774 owns_web_view_ = false; | 747 owns_web_view_ = false; |
775 web_view->close(); | 748 web_view->close(); |
776 } | 749 } |
777 } | 750 } |
778 frame_.Reset(NULL); | 751 frame_.Reset(NULL); |
779 on_ready_.Reset(); | 752 on_ready_.Reset(); |
780 } | 753 } |
781 | 754 |
782 PrintWebViewHelper::PrintWebViewHelper(content::RenderView* render_view) | 755 PrintWebViewHelper::PrintWebViewHelper( |
| 756 content::RenderView* render_view, |
| 757 bool out_of_process_pdf_enabled, |
| 758 bool print_preview_disabled, |
| 759 scoped_ptr<Delegate> delegate) |
783 : content::RenderViewObserver(render_view), | 760 : content::RenderViewObserver(render_view), |
784 content::RenderViewObserverTracker<PrintWebViewHelper>(render_view), | 761 content::RenderViewObserverTracker<PrintWebViewHelper>(render_view), |
785 reset_prep_frame_view_(false), | 762 reset_prep_frame_view_(false), |
786 is_print_ready_metafile_sent_(false), | 763 is_print_ready_metafile_sent_(false), |
787 ignore_css_margins_(false), | 764 ignore_css_margins_(false), |
788 is_scripted_printing_blocked_(false), | 765 is_scripted_printing_blocked_(false), |
789 notify_browser_of_print_failure_(true), | 766 notify_browser_of_print_failure_(true), |
790 print_for_preview_(false), | 767 print_for_preview_(false), |
| 768 out_of_process_pdf_enabled_(out_of_process_pdf_enabled), |
| 769 delegate_(delegate.Pass()), |
791 print_node_in_progress_(false), | 770 print_node_in_progress_(false), |
792 is_loading_(false), | 771 is_loading_(false), |
793 is_scripted_preview_delayed_(false), | 772 is_scripted_preview_delayed_(false), |
794 weak_ptr_factory_(this) { | 773 weak_ptr_factory_(this) { |
795 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 774 if (print_preview_disabled) |
796 switches::kDisablePrintPreview)) { | |
797 DisablePreview(); | 775 DisablePreview(); |
798 } | |
799 } | 776 } |
800 | 777 |
801 PrintWebViewHelper::~PrintWebViewHelper() {} | 778 PrintWebViewHelper::~PrintWebViewHelper() {} |
802 | 779 |
803 // static | 780 // static |
804 void PrintWebViewHelper::DisablePreview() { | 781 void PrintWebViewHelper::DisablePreview() { |
805 g_is_preview_enabled_ = false; | 782 g_is_preview_enabled_ = false; |
806 } | 783 } |
807 | 784 |
808 bool PrintWebViewHelper::IsScriptInitiatedPrintAllowed( | 785 bool PrintWebViewHelper::IsScriptInitiatedPrintAllowed( |
(...skipping 18 matching lines...) Expand all Loading... |
827 on_stop_loading_closure_.Reset(); | 804 on_stop_loading_closure_.Reset(); |
828 } | 805 } |
829 } | 806 } |
830 | 807 |
831 // Prints |frame| which called window.print(). | 808 // Prints |frame| which called window.print(). |
832 void PrintWebViewHelper::PrintPage(blink::WebLocalFrame* frame, | 809 void PrintWebViewHelper::PrintPage(blink::WebLocalFrame* frame, |
833 bool user_initiated) { | 810 bool user_initiated) { |
834 DCHECK(frame); | 811 DCHECK(frame); |
835 | 812 |
836 // Allow Prerendering to cancel this print request if necessary. | 813 // Allow Prerendering to cancel this print request if necessary. |
837 if (prerender::PrerenderHelper::IsPrerendering( | 814 if (delegate_->CancelPrerender(render_view(), routing_id())) |
838 render_view()->GetMainRenderFrame())) { | |
839 Send(new ChromeViewHostMsg_CancelPrerenderForPrinting(routing_id())); | |
840 return; | 815 return; |
841 } | |
842 | 816 |
843 if (!IsScriptInitiatedPrintAllowed(frame, user_initiated)) | 817 if (!IsScriptInitiatedPrintAllowed(frame, user_initiated)) |
844 return; | 818 return; |
845 | 819 |
846 if (!g_is_preview_enabled_) { | 820 if (!g_is_preview_enabled_) { |
847 Print(frame, blink::WebNode(), true); | 821 Print(frame, blink::WebNode(), true); |
848 } else { | 822 } else { |
849 print_preview_context_.InitWithFrame(frame); | 823 print_preview_context_.InitWithFrame(frame); |
850 RequestPrintPreview(PRINT_PREVIEW_SCRIPTED); | 824 RequestPrintPreview(PRINT_PREVIEW_SCRIPTED); |
851 } | 825 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
888 if (pdf_element.isNull()) { | 862 if (pdf_element.isNull()) { |
889 NOTREACHED(); | 863 NOTREACHED(); |
890 return; | 864 return; |
891 } | 865 } |
892 | 866 |
893 // The out-of-process plugin element is nested within a frame. In tests, there | 867 // The out-of-process plugin element is nested within a frame. In tests, there |
894 // may not be an iframe containing the out-of-process plugin, so continue with | 868 // may not be an iframe containing the out-of-process plugin, so continue with |
895 // the element with ID "pdf-viewer" if it isn't an iframe. | 869 // the element with ID "pdf-viewer" if it isn't an iframe. |
896 blink::WebLocalFrame* plugin_frame = pdf_element.document().frame(); | 870 blink::WebLocalFrame* plugin_frame = pdf_element.document().frame(); |
897 blink::WebElement plugin_element = pdf_element; | 871 blink::WebElement plugin_element = pdf_element; |
898 if (switches::OutOfProcessPdfEnabled() && | 872 if (out_of_process_pdf_enabled_ && pdf_element.hasHTMLTagName("iframe")) { |
899 pdf_element.hasHTMLTagName("iframe")) { | |
900 plugin_frame = blink::WebLocalFrame::fromFrameOwnerElement(pdf_element); | 873 plugin_frame = blink::WebLocalFrame::fromFrameOwnerElement(pdf_element); |
901 plugin_element = GetPdfElement(plugin_frame); | 874 plugin_element = delegate_->GetPdfElement(plugin_frame); |
902 if (plugin_element.isNull()) { | 875 if (plugin_element.isNull()) { |
903 NOTREACHED(); | 876 NOTREACHED(); |
904 return; | 877 return; |
905 } | 878 } |
906 } | 879 } |
907 | 880 |
908 // Set |print_for_preview_| flag and autoreset it to back to original | 881 // Set |print_for_preview_| flag and autoreset it to back to original |
909 // on return. | 882 // on return. |
910 base::AutoReset<bool> set_printing_flag(&print_for_preview_, true); | 883 base::AutoReset<bool> set_printing_flag(&print_for_preview_, true); |
911 | 884 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
951 return true; | 924 return true; |
952 } | 925 } |
953 | 926 |
954 #if defined(ENABLE_BASIC_PRINTING) | 927 #if defined(ENABLE_BASIC_PRINTING) |
955 void PrintWebViewHelper::OnPrintPages() { | 928 void PrintWebViewHelper::OnPrintPages() { |
956 blink::WebLocalFrame* frame; | 929 blink::WebLocalFrame* frame; |
957 if (!GetPrintFrame(&frame)) | 930 if (!GetPrintFrame(&frame)) |
958 return; | 931 return; |
959 // If we are printing a PDF extension frame, find the plugin node and print | 932 // If we are printing a PDF extension frame, find the plugin node and print |
960 // that instead. | 933 // that instead. |
961 auto plugin = GetPdfElement(frame); | 934 auto plugin = delegate_->GetPdfElement(frame); |
962 Print(frame, plugin, false); | 935 Print(frame, plugin, false); |
963 } | 936 } |
964 | 937 |
965 void PrintWebViewHelper::OnPrintForSystemDialog() { | 938 void PrintWebViewHelper::OnPrintForSystemDialog() { |
966 blink::WebLocalFrame* frame = print_preview_context_.source_frame(); | 939 blink::WebLocalFrame* frame = print_preview_context_.source_frame(); |
967 if (!frame) { | 940 if (!frame) { |
968 NOTREACHED(); | 941 NOTREACHED(); |
969 return; | 942 return; |
970 } | 943 } |
971 Print(frame, print_preview_context_.source_node(), false); | 944 Print(frame, print_preview_context_.source_node(), false); |
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1206 void PrintWebViewHelper::SetScriptedPrintBlocked(bool blocked) { | 1179 void PrintWebViewHelper::SetScriptedPrintBlocked(bool blocked) { |
1207 is_scripted_printing_blocked_ = blocked; | 1180 is_scripted_printing_blocked_ = blocked; |
1208 } | 1181 } |
1209 | 1182 |
1210 void PrintWebViewHelper::OnInitiatePrintPreview(bool selection_only) { | 1183 void PrintWebViewHelper::OnInitiatePrintPreview(bool selection_only) { |
1211 blink::WebLocalFrame* frame = NULL; | 1184 blink::WebLocalFrame* frame = NULL; |
1212 GetPrintFrame(&frame); | 1185 GetPrintFrame(&frame); |
1213 DCHECK(frame); | 1186 DCHECK(frame); |
1214 // If we are printing a PDF extension frame, find the plugin node and print | 1187 // If we are printing a PDF extension frame, find the plugin node and print |
1215 // that instead. | 1188 // that instead. |
1216 auto plugin = GetPdfElement(frame); | 1189 auto plugin = delegate_->GetPdfElement(frame); |
1217 if (!plugin.isNull()) { | 1190 if (!plugin.isNull()) { |
1218 PrintNode(plugin); | 1191 PrintNode(plugin); |
1219 return; | 1192 return; |
1220 } | 1193 } |
1221 print_preview_context_.InitWithFrame(frame); | 1194 print_preview_context_.InitWithFrame(frame); |
1222 RequestPrintPreview(selection_only ? | 1195 RequestPrintPreview(selection_only ? |
1223 PRINT_PREVIEW_USER_INITIATED_SELECTION : | 1196 PRINT_PREVIEW_USER_INITIATED_SELECTION : |
1224 PRINT_PREVIEW_USER_INITIATED_ENTIRE_FRAME); | 1197 PRINT_PREVIEW_USER_INITIATED_ENTIRE_FRAME); |
1225 } | 1198 } |
1226 | 1199 |
(...skipping 817 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2044 blink::WebConsoleMessage::LevelWarning, message)); | 2017 blink::WebConsoleMessage::LevelWarning, message)); |
2045 return false; | 2018 return false; |
2046 } | 2019 } |
2047 | 2020 |
2048 void PrintWebViewHelper::ScriptingThrottler::Reset() { | 2021 void PrintWebViewHelper::ScriptingThrottler::Reset() { |
2049 // Reset counter on successful print. | 2022 // Reset counter on successful print. |
2050 count_ = 0; | 2023 count_ = 0; |
2051 } | 2024 } |
2052 | 2025 |
2053 } // namespace printing | 2026 } // namespace printing |
OLD | NEW |