print_render_frame_helper_delegate.cc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright (c) 2018 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/renderer/printing/print_render_frame_helper_delegate.h"
  5. #include <utility>
  6. #include "content/public/renderer/render_frame.h"
  7. #include "electron/buildflags/buildflags.h"
  8. #include "third_party/blink/public/web/web_element.h"
  9. #include "third_party/blink/public/web/web_local_frame.h"
  10. #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
  11. #include "components/pdf/common/pdf_util.h"
  12. #include "extensions/common/constants.h"
  13. #include "extensions/renderer/guest_view/mime_handler_view/post_message_support.h"
  14. #endif // BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
  15. namespace electron {
  16. PrintRenderFrameHelperDelegate::PrintRenderFrameHelperDelegate() = default;
  17. PrintRenderFrameHelperDelegate::~PrintRenderFrameHelperDelegate() = default;
  18. // Return the PDF object element if |frame| is the out of process PDF extension.
  19. blink::WebElement PrintRenderFrameHelperDelegate::GetPdfElement(
  20. blink::WebLocalFrame* frame) {
  21. #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
  22. if (frame->Parent() && IsPdfInternalPluginAllowedOrigin(
  23. frame->Parent()->GetSecurityOrigin(), {})) {
  24. auto plugin_element = frame->GetDocument().QuerySelector("embed");
  25. DCHECK(!plugin_element.IsNull());
  26. return plugin_element;
  27. }
  28. #endif // BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
  29. return {};
  30. }
  31. bool PrintRenderFrameHelperDelegate::IsPrintPreviewEnabled() {
  32. return false;
  33. }
  34. bool PrintRenderFrameHelperDelegate::OverridePrint(
  35. blink::WebLocalFrame* frame) {
  36. #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
  37. auto* post_message_support =
  38. extensions::PostMessageSupport::FromWebLocalFrame(frame);
  39. if (post_message_support) {
  40. // This message is handled in chrome/browser/resources/pdf/pdf_viewer.js and
  41. // instructs the PDF plugin to print. This is to make window.print() on a
  42. // PDF plugin document correctly print the PDF. See
  43. // https://crbug.com/448720.
  44. base::Value::Dict message;
  45. message.Set("type", "print");
  46. post_message_support->PostMessageFromValue(base::Value(std::move(message)));
  47. return true;
  48. }
  49. #endif // BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
  50. return false;
  51. }
  52. } // namespace electron