• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "libcef/renderer/extensions/print_render_frame_helper_delegate.h"
6 
7 #include <vector>
8 
9 #include "libcef/common/extensions/extensions_util.h"
10 
11 #include "base/command_line.h"
12 #include "base/strings/string_util.h"
13 #include "chrome/common/chrome_switches.h"
14 #include "chrome/common/extensions/extension_constants.h"
15 #include "chrome/common/pdf_util.h"
16 #include "chrome/common/url_constants.h"
17 #include "content/public/renderer/render_frame.h"
18 #include "extensions/common/constants.h"
19 #include "extensions/renderer/guest_view/mime_handler_view/post_message_support.h"
20 #include "pdf/pdf_features.h"
21 #include "services/service_manager/public/cpp/interface_provider.h"
22 #include "third_party/blink/public/web/web_document.h"
23 #include "third_party/blink/public/web/web_element.h"
24 #include "third_party/blink/public/web/web_local_frame.h"
25 
26 namespace extensions {
27 
CefPrintRenderFrameHelperDelegate(bool is_windowless)28 CefPrintRenderFrameHelperDelegate::CefPrintRenderFrameHelperDelegate(
29     bool is_windowless)
30     : is_windowless_(is_windowless) {}
31 
32 CefPrintRenderFrameHelperDelegate::~CefPrintRenderFrameHelperDelegate() =
33     default;
34 
35 // Return the PDF object element if |frame| is the out of process PDF extension.
GetPdfElement(blink::WebLocalFrame * frame)36 blink::WebElement CefPrintRenderFrameHelperDelegate::GetPdfElement(
37     blink::WebLocalFrame* frame) {
38   if (IsPdfInternalPluginAllowedOrigin(frame->GetSecurityOrigin())) {
39     DCHECK(!base::FeatureList::IsEnabled(chrome_pdf::features::kPdfUnseasoned));
40     // <object> with id="plugin" is created in
41     // chrome/browser/resources/pdf/pdf_viewer_base.js.
42     auto viewer_element = frame->GetDocument().GetElementById("viewer");
43     if (!viewer_element.IsNull() && !viewer_element.ShadowRoot().IsNull()) {
44       auto plugin_element =
45           viewer_element.ShadowRoot().QuerySelector("#plugin");
46       if (!plugin_element.IsNull()) {
47         return plugin_element;
48       }
49     }
50     NOTREACHED();
51     return blink::WebElement();
52   }
53 
54   if (frame->Parent() &&
55       IsPdfInternalPluginAllowedOrigin(frame->Parent()->GetSecurityOrigin())) {
56     DCHECK(base::FeatureList::IsEnabled(chrome_pdf::features::kPdfUnseasoned));
57     auto plugin_element = frame->GetDocument().QuerySelector("embed");
58     DCHECK(!plugin_element.IsNull());
59     return plugin_element;
60   }
61 
62   return blink::WebElement();
63 }
64 
IsPrintPreviewEnabled()65 bool CefPrintRenderFrameHelperDelegate::IsPrintPreviewEnabled() {
66   return !is_windowless_ && PrintPreviewEnabled();
67 }
68 
OverridePrint(blink::WebLocalFrame * frame)69 bool CefPrintRenderFrameHelperDelegate::OverridePrint(
70     blink::WebLocalFrame* frame) {
71   auto* post_message_support =
72       extensions::PostMessageSupport::FromWebLocalFrame(frame);
73   if (post_message_support) {
74     // This message is handled in chrome/browser/resources/pdf/pdf.js and
75     // instructs the PDF plugin to print. This is to make window.print() on a
76     // PDF plugin document correctly print the PDF. See
77     // https://crbug.com/448720.
78     base::DictionaryValue message;
79     message.SetString("type", "print");
80     post_message_support->PostMessageFromValue(message);
81     return true;
82   }
83   return false;
84 }
85 
86 }  // namespace extensions
87