2015-07-16 23:40:01 +02:00
|
|
|
// Copyright 2015 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2017-09-06 23:40:58 +02:00
|
|
|
#include "libcef/renderer/extensions/print_render_frame_helper_delegate.h"
|
2015-07-16 23:40:01 +02:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
2019-07-17 20:47:27 +02:00
|
|
|
#include "libcef/common/extensions/extensions_util.h"
|
|
|
|
|
|
|
|
#include "base/command_line.h"
|
|
|
|
#include "base/strings/string_util.h"
|
2015-07-16 23:40:01 +02:00
|
|
|
#include "chrome/common/chrome_switches.h"
|
|
|
|
#include "chrome/common/extensions/extension_constants.h"
|
2019-07-17 20:47:27 +02:00
|
|
|
#include "chrome/common/url_constants.h"
|
2015-07-16 23:40:01 +02:00
|
|
|
#include "content/public/renderer/render_frame.h"
|
|
|
|
#include "extensions/common/constants.h"
|
2021-03-04 23:36:57 +01:00
|
|
|
#include "extensions/renderer/guest_view/mime_handler_view/post_message_support.h"
|
2019-07-17 20:47:27 +02:00
|
|
|
#include "services/service_manager/public/cpp/interface_provider.h"
|
2018-04-19 17:44:42 +02:00
|
|
|
#include "third_party/blink/public/web/web_document.h"
|
|
|
|
#include "third_party/blink/public/web/web_element.h"
|
|
|
|
#include "third_party/blink/public/web/web_local_frame.h"
|
2015-07-16 23:40:01 +02:00
|
|
|
|
|
|
|
namespace extensions {
|
|
|
|
|
2019-07-17 20:47:27 +02:00
|
|
|
CefPrintRenderFrameHelperDelegate::CefPrintRenderFrameHelperDelegate(
|
|
|
|
bool is_windowless)
|
|
|
|
: is_windowless_(is_windowless) {}
|
|
|
|
|
2017-09-06 23:40:58 +02:00
|
|
|
CefPrintRenderFrameHelperDelegate::~CefPrintRenderFrameHelperDelegate() {}
|
2015-07-16 23:40:01 +02:00
|
|
|
|
|
|
|
// Return the PDF object element if |frame| is the out of process PDF extension.
|
2017-09-06 23:40:58 +02:00
|
|
|
blink::WebElement CefPrintRenderFrameHelperDelegate::GetPdfElement(
|
2017-05-17 11:29:28 +02:00
|
|
|
blink::WebLocalFrame* frame) {
|
2017-04-20 21:28:17 +02:00
|
|
|
GURL url = frame->GetDocument().Url();
|
2019-07-17 20:47:27 +02:00
|
|
|
bool inside_print_preview = url.GetOrigin() == chrome::kChromeUIPrintURL;
|
|
|
|
bool inside_pdf_extension =
|
|
|
|
url.SchemeIs(extensions::kExtensionScheme) &&
|
|
|
|
url.host_piece() == extension_misc::kPdfExtensionId;
|
|
|
|
if (inside_print_preview || inside_pdf_extension) {
|
2015-07-16 23:40:01 +02:00
|
|
|
// <object> with id="plugin" is created in
|
2021-03-18 17:59:17 +01:00
|
|
|
// chrome/browser/resources/pdf/pdf_viewer_base.js.
|
|
|
|
auto viewer_element = frame->GetDocument().GetElementById("viewer");
|
|
|
|
if (!viewer_element.IsNull() && !viewer_element.ShadowRoot().IsNull()) {
|
|
|
|
auto plugin_element =
|
|
|
|
viewer_element.ShadowRoot().QuerySelector("#plugin");
|
|
|
|
if (!plugin_element.IsNull()) {
|
|
|
|
return plugin_element;
|
|
|
|
}
|
2015-07-16 23:40:01 +02:00
|
|
|
}
|
|
|
|
NOTREACHED();
|
|
|
|
}
|
|
|
|
return blink::WebElement();
|
|
|
|
}
|
|
|
|
|
2017-09-06 23:40:58 +02:00
|
|
|
bool CefPrintRenderFrameHelperDelegate::IsPrintPreviewEnabled() {
|
2019-07-17 20:47:27 +02:00
|
|
|
return !is_windowless_ && PrintPreviewEnabled();
|
2015-07-16 23:40:01 +02:00
|
|
|
}
|
|
|
|
|
2017-09-06 23:40:58 +02:00
|
|
|
bool CefPrintRenderFrameHelperDelegate::OverridePrint(
|
|
|
|
blink::WebLocalFrame* frame) {
|
2019-07-16 19:59:21 +02:00
|
|
|
auto* post_message_support =
|
|
|
|
extensions::PostMessageSupport::FromWebLocalFrame(frame);
|
|
|
|
if (post_message_support) {
|
2015-07-16 23:40:01 +02:00
|
|
|
// This message is handled in chrome/browser/resources/pdf/pdf.js and
|
|
|
|
// instructs the PDF plugin to print. This is to make window.print() on a
|
|
|
|
// PDF plugin document correctly print the PDF. See
|
|
|
|
// https://crbug.com/448720.
|
|
|
|
base::DictionaryValue message;
|
|
|
|
message.SetString("type", "print");
|
2019-07-16 19:59:21 +02:00
|
|
|
post_message_support->PostMessageFromValue(message);
|
2015-07-16 23:40:01 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace extensions
|