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

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: Using initialisation parameters instead of delegate methods 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"
dgn 2015/01/05 22:45:48 Will move to //components/printing/common/print_me
21 #include "chrome/common/render_messages.h"
22 #include "chrome/grit/browser_resources.h" 19 #include "chrome/grit/browser_resources.h"
dgn 2015/01/05 22:45:48 The used variable will be moved to //components/pr
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
53 static const char kPdfExtensionId[] = "mhjfbmdgcfjbbpaeojofohoefgiehjai";
Lei Zhang 2015/01/06 04:11:32 Can we pass this in, instead of duplicating the va
dgn 2015/01/06 16:35:31 Done.
54
58 namespace { 55 namespace {
59 56
60 enum PrintPreviewHelperEvents { 57 enum PrintPreviewHelperEvents {
61 PREVIEW_EVENT_REQUESTED, 58 PREVIEW_EVENT_REQUESTED,
62 PREVIEW_EVENT_CACHE_HIT, // Unused 59 PREVIEW_EVENT_CACHE_HIT, // Unused
63 PREVIEW_EVENT_CREATE_DOCUMENT, 60 PREVIEW_EVENT_CREATE_DOCUMENT,
64 PREVIEW_EVENT_NEW_SETTINGS, // Unused 61 PREVIEW_EVENT_NEW_SETTINGS, // Unused
65 PREVIEW_EVENT_MAX, 62 PREVIEW_EVENT_MAX,
66 }; 63 };
67 64
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after
408 *scale_factor = factor; 405 *scale_factor = factor;
409 } 406 }
410 return result_params; 407 return result_params;
411 } 408 }
412 409
413 // Return the PDF object element if |frame| is the out of process PDF extension. 410 // Return the PDF object element if |frame| is the out of process PDF extension.
414 blink::WebElement GetPdfElement(blink::WebLocalFrame* frame) { 411 blink::WebElement GetPdfElement(blink::WebLocalFrame* frame) {
415 #if defined(ENABLE_EXTENSIONS) 412 #if defined(ENABLE_EXTENSIONS)
416 GURL url = frame->document().url(); 413 GURL url = frame->document().url();
417 if (url.SchemeIs(extensions::kExtensionScheme) && 414 if (url.SchemeIs(extensions::kExtensionScheme) &&
418 url.host() == extension_misc::kPdfExtensionId) { 415 url.host() == kPdfExtensionId) {
419 // <object> with id="plugin" is created in 416 // <object> with id="plugin" is created in
420 // chrome/browser/resources/pdf/pdf.js. 417 // chrome/browser/resources/pdf/pdf.js.
421 auto plugin_element = frame->document().getElementById("plugin"); 418 auto plugin_element = frame->document().getElementById("plugin");
422 if (!plugin_element.isNull()) { 419 if (!plugin_element.isNull()) {
423 return plugin_element; 420 return plugin_element;
424 } 421 }
425 NOTREACHED(); 422 NOTREACHED();
426 } 423 }
427 #endif // defined(ENABLE_EXTENSIONS) 424 #endif // defined(ENABLE_EXTENSIONS)
428 return blink::WebElement(); 425 return blink::WebElement();
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after
783 if (owns_web_view_) { 780 if (owns_web_view_) {
784 DCHECK(!frame->isLoading()); 781 DCHECK(!frame->isLoading());
785 owns_web_view_ = false; 782 owns_web_view_ = false;
786 web_view->close(); 783 web_view->close();
787 } 784 }
788 } 785 }
789 frame_.Reset(NULL); 786 frame_.Reset(NULL);
790 on_ready_.Reset(); 787 on_ready_.Reset();
791 } 788 }
792 789
793 PrintWebViewHelper::PrintWebViewHelper(content::RenderView* render_view) 790 PrintWebViewHelper::PrintWebViewHelper(
791 content::RenderView* render_view,
792 const bool out_of_process_pdf_enabled,
793 const bool print_preview_disabled,
794 Delegate* delegate)
794 : content::RenderViewObserver(render_view), 795 : content::RenderViewObserver(render_view),
795 content::RenderViewObserverTracker<PrintWebViewHelper>(render_view), 796 content::RenderViewObserverTracker<PrintWebViewHelper>(render_view),
796 reset_prep_frame_view_(false), 797 reset_prep_frame_view_(false),
797 is_print_ready_metafile_sent_(false), 798 is_print_ready_metafile_sent_(false),
798 ignore_css_margins_(false), 799 ignore_css_margins_(false),
799 is_scripted_printing_blocked_(false), 800 is_scripted_printing_blocked_(false),
800 notify_browser_of_print_failure_(true), 801 notify_browser_of_print_failure_(true),
801 print_for_preview_(false), 802 print_for_preview_(false),
803 out_of_process_pdf_enabled_(out_of_process_pdf_enabled),
804 delegate_(delegate),
802 print_node_in_progress_(false), 805 print_node_in_progress_(false),
803 is_loading_(false), 806 is_loading_(false),
804 is_scripted_preview_delayed_(false), 807 is_scripted_preview_delayed_(false),
805 weak_ptr_factory_(this) { 808 weak_ptr_factory_(this) {
806 if (CommandLine::ForCurrentProcess()->HasSwitch( 809 if (print_preview_disabled) {
807 switches::kDisablePrintPreview)) {
808 DisablePreview(); 810 DisablePreview();
809 } 811 }
810 } 812 }
811 813
812 PrintWebViewHelper::~PrintWebViewHelper() {} 814 PrintWebViewHelper::~PrintWebViewHelper() {}
813 815
814 // static 816 // static
815 void PrintWebViewHelper::DisablePreview() { 817 void PrintWebViewHelper::DisablePreview() {
816 g_is_preview_enabled_ = false; 818 g_is_preview_enabled_ = false;
817 } 819 }
(...skipping 20 matching lines...) Expand all
838 on_stop_loading_closure_.Reset(); 840 on_stop_loading_closure_.Reset();
839 } 841 }
840 } 842 }
841 843
842 // Prints |frame| which called window.print(). 844 // Prints |frame| which called window.print().
843 void PrintWebViewHelper::PrintPage(blink::WebLocalFrame* frame, 845 void PrintWebViewHelper::PrintPage(blink::WebLocalFrame* frame,
844 bool user_initiated) { 846 bool user_initiated) {
845 DCHECK(frame); 847 DCHECK(frame);
846 848
847 // Allow Prerendering to cancel this print request if necessary. 849 // Allow Prerendering to cancel this print request if necessary.
848 if (prerender::PrerenderHelper::IsPrerendering( 850 if (delegate_->CancelPrerender(render_view(), routing_id()))
849 render_view()->GetMainRenderFrame())) {
850 Send(new ChromeViewHostMsg_CancelPrerenderForPrinting(routing_id()));
851 return; 851 return;
852 }
853 852
854 if (!IsScriptInitiatedPrintAllowed(frame, user_initiated)) 853 if (!IsScriptInitiatedPrintAllowed(frame, user_initiated))
855 return; 854 return;
856 855
857 if (!g_is_preview_enabled_) { 856 if (!g_is_preview_enabled_) {
858 Print(frame, blink::WebNode(), true); 857 Print(frame, blink::WebNode(), true);
859 } else { 858 } else {
860 print_preview_context_.InitWithFrame(frame); 859 print_preview_context_.InitWithFrame(frame);
861 RequestPrintPreview(PRINT_PREVIEW_SCRIPTED); 860 RequestPrintPreview(PRINT_PREVIEW_SCRIPTED);
862 } 861 }
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
899 if (pdf_element.isNull()) { 898 if (pdf_element.isNull()) {
900 NOTREACHED(); 899 NOTREACHED();
901 return; 900 return;
902 } 901 }
903 902
904 // The out-of-process plugin element is nested within a frame. In tests, there 903 // 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 904 // 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. 905 // the element with ID "pdf-viewer" if it isn't an iframe.
907 blink::WebLocalFrame* plugin_frame = pdf_element.document().frame(); 906 blink::WebLocalFrame* plugin_frame = pdf_element.document().frame();
908 blink::WebElement plugin_element = pdf_element; 907 blink::WebElement plugin_element = pdf_element;
909 if (switches::OutOfProcessPdfEnabled() && 908 if (out_of_process_pdf_enabled_ && pdf_element.hasHTMLTagName("iframe")) {
910 pdf_element.hasHTMLTagName("iframe")) {
911 plugin_frame = blink::WebLocalFrame::fromFrameOwnerElement(pdf_element); 909 plugin_frame = blink::WebLocalFrame::fromFrameOwnerElement(pdf_element);
912 plugin_element = GetPdfElement(plugin_frame); 910 plugin_element = GetPdfElement(plugin_frame);
913 if (plugin_element.isNull()) { 911 if (plugin_element.isNull()) {
914 NOTREACHED(); 912 NOTREACHED();
915 return; 913 return;
916 } 914 }
917 } 915 }
918 916
919 // Set |print_for_preview_| flag and autoreset it to back to original 917 // Set |print_for_preview_| flag and autoreset it to back to original
920 // on return. 918 // on return.
(...skipping 1134 matching lines...) Expand 10 before | Expand all | Expand 10 after
2055 blink::WebConsoleMessage::LevelWarning, message)); 2053 blink::WebConsoleMessage::LevelWarning, message));
2056 return false; 2054 return false;
2057 } 2055 }
2058 2056
2059 void PrintWebViewHelper::ScriptingThrottler::Reset() { 2057 void PrintWebViewHelper::ScriptingThrottler::Reset() {
2060 // Reset counter on successful print. 2058 // Reset counter on successful print.
2061 count_ = 0; 2059 count_ = 0;
2062 } 2060 }
2063 2061
2064 } // namespace printing 2062 } // namespace printing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698