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

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

Issue 791133006: Delegates for the printing component (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/renderer/printing/print_web_view_helper.h" 5 #include "chrome/renderer/printing/print_web_view_helper.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/auto_reset.h" 9 #include "base/auto_reset.h"
10 #include "base/command_line.h"
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) 45 #if defined(ENABLE_EXTENSIONS)
50 #include "chrome/common/extensions/extension_constants.h"
51 #include "extensions/common/constants.h" 46 #include "extensions/common/constants.h"
52 #endif // defined(ENABLE_EXTENSIONS) 47 #endif // defined(ENABLE_EXTENSIONS)
53 48
54 using content::WebPreferences; 49 using content::WebPreferences;
55 50
56 namespace printing { 51 namespace printing {
57 52
58 namespace { 53 namespace {
59 54
60 enum PrintPreviewHelperEvents { 55 enum PrintPreviewHelperEvents {
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 399
405 if (fit_to_page) { 400 if (fit_to_page) {
406 double factor = FitPrintParamsToPage(params, &result_params); 401 double factor = FitPrintParamsToPage(params, &result_params);
407 if (scale_factor) 402 if (scale_factor)
408 *scale_factor = factor; 403 *scale_factor = factor;
409 } 404 }
410 return result_params; 405 return result_params;
411 } 406 }
412 407
413 // Return the PDF object element if |frame| is the out of process PDF extension. 408 // Return the PDF object element if |frame| is the out of process PDF extension.
414 blink::WebElement GetPdfElement(blink::WebLocalFrame* frame) { 409 blink::WebElement GetPdfElement(blink::WebLocalFrame* frame,
410 const char* pdf_extension_id) {
dgn 2015/01/06 16:35:31 Would it be better to make this function a method
Vitaly Buka (NO REVIEWS) 2015/01/06 19:29:24 It's fine to keep as is On 2015/01/06 16:35:31, d
dgn 2015/01/06 21:01:45 Done.
415 #if defined(ENABLE_EXTENSIONS) 411 #if defined(ENABLE_EXTENSIONS)
416 GURL url = frame->document().url(); 412 GURL url = frame->document().url();
417 if (url.SchemeIs(extensions::kExtensionScheme) && 413 if (url.SchemeIs(extensions::kExtensionScheme) &&
418 url.host() == extension_misc::kPdfExtensionId) { 414 url.host() == pdf_extension_id) {
419 // <object> with id="plugin" is created in 415 // <object> with id="plugin" is created in
420 // chrome/browser/resources/pdf/pdf.js. 416 // chrome/browser/resources/pdf/pdf.js.
421 auto plugin_element = frame->document().getElementById("plugin"); 417 auto plugin_element = frame->document().getElementById("plugin");
422 if (!plugin_element.isNull()) { 418 if (!plugin_element.isNull()) {
423 return plugin_element; 419 return plugin_element;
424 } 420 }
425 NOTREACHED(); 421 NOTREACHED();
426 } 422 }
427 #endif // defined(ENABLE_EXTENSIONS) 423 #endif // defined(ENABLE_EXTENSIONS)
428 return blink::WebElement(); 424 return blink::WebElement();
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after
772 if (owns_web_view_) { 768 if (owns_web_view_) {
773 DCHECK(!frame->isLoading()); 769 DCHECK(!frame->isLoading());
774 owns_web_view_ = false; 770 owns_web_view_ = false;
775 web_view->close(); 771 web_view->close();
776 } 772 }
777 } 773 }
778 frame_.Reset(NULL); 774 frame_.Reset(NULL);
779 on_ready_.Reset(); 775 on_ready_.Reset();
780 } 776 }
781 777
782 PrintWebViewHelper::PrintWebViewHelper(content::RenderView* render_view) 778 PrintWebViewHelper::PrintWebViewHelper(
779 content::RenderView* render_view,
780 bool out_of_process_pdf_enabled,
781 bool print_preview_disabled,
782 const char* pdf_extension_id,
783 scoped_ptr<Delegate> delegate)
783 : content::RenderViewObserver(render_view), 784 : content::RenderViewObserver(render_view),
784 content::RenderViewObserverTracker<PrintWebViewHelper>(render_view), 785 content::RenderViewObserverTracker<PrintWebViewHelper>(render_view),
785 reset_prep_frame_view_(false), 786 reset_prep_frame_view_(false),
786 is_print_ready_metafile_sent_(false), 787 is_print_ready_metafile_sent_(false),
787 ignore_css_margins_(false), 788 ignore_css_margins_(false),
788 is_scripted_printing_blocked_(false), 789 is_scripted_printing_blocked_(false),
789 notify_browser_of_print_failure_(true), 790 notify_browser_of_print_failure_(true),
790 print_for_preview_(false), 791 print_for_preview_(false),
792 out_of_process_pdf_enabled_(out_of_process_pdf_enabled),
793 pdf_extension_id_(pdf_extension_id),
794 delegate_(delegate.Pass()),
791 print_node_in_progress_(false), 795 print_node_in_progress_(false),
792 is_loading_(false), 796 is_loading_(false),
793 is_scripted_preview_delayed_(false), 797 is_scripted_preview_delayed_(false),
794 weak_ptr_factory_(this) { 798 weak_ptr_factory_(this) {
795 if (base::CommandLine::ForCurrentProcess()->HasSwitch( 799 if (print_preview_disabled)
796 switches::kDisablePrintPreview)) {
797 DisablePreview(); 800 DisablePreview();
798 }
799 } 801 }
800 802
801 PrintWebViewHelper::~PrintWebViewHelper() {} 803 PrintWebViewHelper::~PrintWebViewHelper() {}
802 804
803 // static 805 // static
804 void PrintWebViewHelper::DisablePreview() { 806 void PrintWebViewHelper::DisablePreview() {
805 g_is_preview_enabled_ = false; 807 g_is_preview_enabled_ = false;
806 } 808 }
807 809
808 bool PrintWebViewHelper::IsScriptInitiatedPrintAllowed( 810 bool PrintWebViewHelper::IsScriptInitiatedPrintAllowed(
(...skipping 18 matching lines...) Expand all
827 on_stop_loading_closure_.Reset(); 829 on_stop_loading_closure_.Reset();
828 } 830 }
829 } 831 }
830 832
831 // Prints |frame| which called window.print(). 833 // Prints |frame| which called window.print().
832 void PrintWebViewHelper::PrintPage(blink::WebLocalFrame* frame, 834 void PrintWebViewHelper::PrintPage(blink::WebLocalFrame* frame,
833 bool user_initiated) { 835 bool user_initiated) {
834 DCHECK(frame); 836 DCHECK(frame);
835 837
836 // Allow Prerendering to cancel this print request if necessary. 838 // Allow Prerendering to cancel this print request if necessary.
837 if (prerender::PrerenderHelper::IsPrerendering( 839 if (delegate_->CancelPrerender(render_view(), routing_id()))
838 render_view()->GetMainRenderFrame())) {
839 Send(new ChromeViewHostMsg_CancelPrerenderForPrinting(routing_id()));
840 return; 840 return;
841 }
842 841
843 if (!IsScriptInitiatedPrintAllowed(frame, user_initiated)) 842 if (!IsScriptInitiatedPrintAllowed(frame, user_initiated))
844 return; 843 return;
845 844
846 if (!g_is_preview_enabled_) { 845 if (!g_is_preview_enabled_) {
847 Print(frame, blink::WebNode(), true); 846 Print(frame, blink::WebNode(), true);
848 } else { 847 } else {
849 print_preview_context_.InitWithFrame(frame); 848 print_preview_context_.InitWithFrame(frame);
850 RequestPrintPreview(PRINT_PREVIEW_SCRIPTED); 849 RequestPrintPreview(PRINT_PREVIEW_SCRIPTED);
851 } 850 }
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
888 if (pdf_element.isNull()) { 887 if (pdf_element.isNull()) {
889 NOTREACHED(); 888 NOTREACHED();
890 return; 889 return;
891 } 890 }
892 891
893 // The out-of-process plugin element is nested within a frame. In tests, there 892 // 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 893 // 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. 894 // the element with ID "pdf-viewer" if it isn't an iframe.
896 blink::WebLocalFrame* plugin_frame = pdf_element.document().frame(); 895 blink::WebLocalFrame* plugin_frame = pdf_element.document().frame();
897 blink::WebElement plugin_element = pdf_element; 896 blink::WebElement plugin_element = pdf_element;
898 if (switches::OutOfProcessPdfEnabled() && 897 if (out_of_process_pdf_enabled_ && pdf_element.hasHTMLTagName("iframe")) {
899 pdf_element.hasHTMLTagName("iframe")) {
900 plugin_frame = blink::WebLocalFrame::fromFrameOwnerElement(pdf_element); 898 plugin_frame = blink::WebLocalFrame::fromFrameOwnerElement(pdf_element);
901 plugin_element = GetPdfElement(plugin_frame); 899 plugin_element = GetPdfElement(plugin_frame, pdf_extension_id_);
902 if (plugin_element.isNull()) { 900 if (plugin_element.isNull()) {
903 NOTREACHED(); 901 NOTREACHED();
904 return; 902 return;
905 } 903 }
906 } 904 }
907 905
908 // Set |print_for_preview_| flag and autoreset it to back to original 906 // Set |print_for_preview_| flag and autoreset it to back to original
909 // on return. 907 // on return.
910 base::AutoReset<bool> set_printing_flag(&print_for_preview_, true); 908 base::AutoReset<bool> set_printing_flag(&print_for_preview_, true);
911 909
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
951 return true; 949 return true;
952 } 950 }
953 951
954 #if defined(ENABLE_BASIC_PRINTING) 952 #if defined(ENABLE_BASIC_PRINTING)
955 void PrintWebViewHelper::OnPrintPages() { 953 void PrintWebViewHelper::OnPrintPages() {
956 blink::WebLocalFrame* frame; 954 blink::WebLocalFrame* frame;
957 if (!GetPrintFrame(&frame)) 955 if (!GetPrintFrame(&frame))
958 return; 956 return;
959 // If we are printing a PDF extension frame, find the plugin node and print 957 // If we are printing a PDF extension frame, find the plugin node and print
960 // that instead. 958 // that instead.
961 auto plugin = GetPdfElement(frame); 959 auto plugin = GetPdfElement(frame, pdf_extension_id_);
962 Print(frame, plugin, false); 960 Print(frame, plugin, false);
963 } 961 }
964 962
965 void PrintWebViewHelper::OnPrintForSystemDialog() { 963 void PrintWebViewHelper::OnPrintForSystemDialog() {
966 blink::WebLocalFrame* frame = print_preview_context_.source_frame(); 964 blink::WebLocalFrame* frame = print_preview_context_.source_frame();
967 if (!frame) { 965 if (!frame) {
968 NOTREACHED(); 966 NOTREACHED();
969 return; 967 return;
970 } 968 }
971 Print(frame, print_preview_context_.source_node(), false); 969 Print(frame, print_preview_context_.source_node(), false);
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
1206 void PrintWebViewHelper::SetScriptedPrintBlocked(bool blocked) { 1204 void PrintWebViewHelper::SetScriptedPrintBlocked(bool blocked) {
1207 is_scripted_printing_blocked_ = blocked; 1205 is_scripted_printing_blocked_ = blocked;
1208 } 1206 }
1209 1207
1210 void PrintWebViewHelper::OnInitiatePrintPreview(bool selection_only) { 1208 void PrintWebViewHelper::OnInitiatePrintPreview(bool selection_only) {
1211 blink::WebLocalFrame* frame = NULL; 1209 blink::WebLocalFrame* frame = NULL;
1212 GetPrintFrame(&frame); 1210 GetPrintFrame(&frame);
1213 DCHECK(frame); 1211 DCHECK(frame);
1214 // If we are printing a PDF extension frame, find the plugin node and print 1212 // If we are printing a PDF extension frame, find the plugin node and print
1215 // that instead. 1213 // that instead.
1216 auto plugin = GetPdfElement(frame); 1214 auto plugin = GetPdfElement(frame, pdf_extension_id_);
1217 if (!plugin.isNull()) { 1215 if (!plugin.isNull()) {
1218 PrintNode(plugin); 1216 PrintNode(plugin);
1219 return; 1217 return;
1220 } 1218 }
1221 print_preview_context_.InitWithFrame(frame); 1219 print_preview_context_.InitWithFrame(frame);
1222 RequestPrintPreview(selection_only ? 1220 RequestPrintPreview(selection_only ?
1223 PRINT_PREVIEW_USER_INITIATED_SELECTION : 1221 PRINT_PREVIEW_USER_INITIATED_SELECTION :
1224 PRINT_PREVIEW_USER_INITIATED_ENTIRE_FRAME); 1222 PRINT_PREVIEW_USER_INITIATED_ENTIRE_FRAME);
1225 } 1223 }
1226 1224
(...skipping 817 matching lines...) Expand 10 before | Expand all | Expand 10 after
2044 blink::WebConsoleMessage::LevelWarning, message)); 2042 blink::WebConsoleMessage::LevelWarning, message));
2045 return false; 2043 return false;
2046 } 2044 }
2047 2045
2048 void PrintWebViewHelper::ScriptingThrottler::Reset() { 2046 void PrintWebViewHelper::ScriptingThrottler::Reset() {
2049 // Reset counter on successful print. 2047 // Reset counter on successful print.
2050 count_ = 0; 2048 count_ = 0;
2051 } 2049 }
2052 2050
2053 } // namespace printing 2051 } // namespace printing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698