1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/renderer/printing/chrome_print_web_view_helper_delegate.h"
9 #include "base/command_line.h"
10 #include "chrome/common/chrome_switches.h"
11 #include "chrome/common/render_messages.h"
12 #include "chrome/renderer/prerender/prerender_helper.h"
13 #include "content/public/renderer/render_frame.h"
14 #include "content/public/renderer/render_view.h"
15 #include "ipc/ipc_message.h"
16 #include "third_party/WebKit/public/web/WebDocument.h"
17 #include "third_party/WebKit/public/web/WebElement.h"
18 #include "third_party/WebKit/public/web/WebLocalFrame.h"
20 #if defined(ENABLE_EXTENSIONS)
21 #include "chrome/common/extensions/extension_constants.h"
22 #include "extensions/common/constants.h"
23 #include "extensions/renderer/guest_view/mime_handler_view/mime_handler_view_container.h"
24 #endif // defined(ENABLE_EXTENSIONS)
26 ChromePrintWebViewHelperDelegate::~ChromePrintWebViewHelperDelegate(){
29 bool ChromePrintWebViewHelperDelegate::CancelPrerender(
30 content::RenderView
* render_view
, int routing_id
) {
31 if (!render_view
|| !prerender::PrerenderHelper::IsPrerendering(
32 render_view
->GetMainRenderFrame()))
35 return render_view
->Send(
36 new ChromeViewHostMsg_CancelPrerenderForPrinting(routing_id
));
39 // Return the PDF object element if |frame| is the out of process PDF extension.
40 blink::WebElement
ChromePrintWebViewHelperDelegate::GetPdfElement(
41 blink::WebLocalFrame
* frame
) {
42 #if defined(ENABLE_EXTENSIONS)
43 GURL url
= frame
->document().url();
44 if (url
.SchemeIs(extensions::kExtensionScheme
) &&
45 url
.host() == extension_misc::kPdfExtensionId
) {
46 // <object> with id="plugin" is created in
47 // chrome/browser/resources/pdf/pdf.js.
48 auto plugin_element
= frame
->document().getElementById("plugin");
49 if (!plugin_element
.isNull()) {
50 return plugin_element
;
54 #endif // defined(ENABLE_EXTENSIONS)
55 return blink::WebElement();
58 bool ChromePrintWebViewHelperDelegate::IsOutOfProcessPdfEnabled() {
59 return switches::OutOfProcessPdfEnabled();
62 bool ChromePrintWebViewHelperDelegate::IsPrintPreviewEnabled() {
63 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
64 return !command_line
->HasSwitch(switches::kDisablePrintPreview
);
67 bool ChromePrintWebViewHelperDelegate::OverridePrint(
68 blink::WebLocalFrame
* frame
) {
69 #if defined(ENABLE_EXTENSIONS)
70 if (!frame
->document().isPluginDocument())
73 std::vector
<extensions::MimeHandlerViewContainer
*> mime_handlers
=
74 extensions::MimeHandlerViewContainer::FromRenderFrame(
75 content::RenderFrame::FromWebFrame(frame
));
76 if (!mime_handlers
.empty()) {
77 // This message is handled in chrome/browser/resources/pdf/pdf.js and
78 // instructs the PDF plugin to print. This is to make window.print() on a
79 // PDF plugin document correctly print the PDF. See
80 // https://crbug.com/448720.
81 base::DictionaryValue message
;
82 message
.SetString("type", "print");
83 mime_handlers
.front()->PostMessageFromValue(message
);
86 #endif // defined(ENABLE_EXTENSIONS)