Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / renderer / printing / chrome_print_web_view_helper_delegate.cc
blobc451c805f2aedb4ec5f3a854e2d9a4cad9d53a09
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"
7 #include <vector>
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()))
33 return false;
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;
52 NOTREACHED();
54 #endif // defined(ENABLE_EXTENSIONS)
55 return blink::WebElement();
58 bool ChromePrintWebViewHelperDelegate::IsPrintPreviewEnabled() {
59 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
60 return !command_line->HasSwitch(switches::kDisablePrintPreview);
63 bool ChromePrintWebViewHelperDelegate::OverridePrint(
64 blink::WebLocalFrame* frame) {
65 #if defined(ENABLE_EXTENSIONS)
66 if (!frame->document().isPluginDocument())
67 return false;
69 std::vector<extensions::MimeHandlerViewContainer*> mime_handlers =
70 extensions::MimeHandlerViewContainer::FromRenderFrame(
71 content::RenderFrame::FromWebFrame(frame));
72 if (!mime_handlers.empty()) {
73 // This message is handled in chrome/browser/resources/pdf/pdf.js and
74 // instructs the PDF plugin to print. This is to make window.print() on a
75 // PDF plugin document correctly print the PDF. See
76 // https://crbug.com/448720.
77 base::DictionaryValue message;
78 message.SetString("type", "print");
79 mime_handlers.front()->PostMessageFromValue(message);
80 return true;
82 #endif // defined(ENABLE_EXTENSIONS)
83 return false;