electron_pdf_document_helper_client.cc 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright (c) 2015 Slack Technologies, 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/browser/electron_pdf_document_helper_client.h"
  5. #include "chrome/browser/pdf/pdf_viewer_stream_manager.h"
  6. #include "chrome/common/content_restriction.h"
  7. #include "components/pdf/browser/pdf_frame_util.h"
  8. #include "content/public/browser/render_frame_host.h"
  9. #include "content/public/browser/web_contents.h"
  10. #include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest.h"
  11. #include "pdf/content_restriction.h"
  12. #include "pdf/pdf_features.h"
  13. #include "shell/browser/api/electron_api_web_contents.h"
  14. ElectronPDFDocumentHelperClient::ElectronPDFDocumentHelperClient() = default;
  15. ElectronPDFDocumentHelperClient::~ElectronPDFDocumentHelperClient() = default;
  16. void ElectronPDFDocumentHelperClient::UpdateContentRestrictions(
  17. content::RenderFrameHost* render_frame_host,
  18. int content_restrictions) {
  19. // UpdateContentRestrictions potentially gets called twice from
  20. // pdf/pdf_view_web_plugin.cc. The first time it is potentially called is
  21. // when loading starts and it is called with a restriction on printing. The
  22. // second time it is called is when loading is finished and if printing is
  23. // allowed there won't be a printing restriction passed, so we can use this
  24. // second call to notify that the pdf document is ready to print.
  25. if (!(content_restrictions & chrome_pdf::kContentRestrictionPrint)) {
  26. // It's a WebView - emit the event on the WebView webContents.
  27. auto* guest_view = extensions::MimeHandlerViewGuest::FromRenderFrameHost(
  28. render_frame_host);
  29. if (guest_view) {
  30. auto* gv_api_wc =
  31. electron::api::WebContents::From(guest_view->embedder_web_contents());
  32. if (gv_api_wc)
  33. gv_api_wc->PDFReadyToPrint();
  34. return;
  35. }
  36. auto* wc = content::WebContents::FromRenderFrameHost(render_frame_host);
  37. if (wc) {
  38. auto* api_wc =
  39. electron::api::WebContents::From(wc->GetOuterWebContents());
  40. if (api_wc)
  41. api_wc->PDFReadyToPrint();
  42. }
  43. }
  44. }
  45. void ElectronPDFDocumentHelperClient::SetPluginCanSave(
  46. content::RenderFrameHost* render_frame_host,
  47. bool can_save) {
  48. if (chrome_pdf::features::IsOopifPdfEnabled()) {
  49. auto* pdf_viewer_stream_manager =
  50. pdf::PdfViewerStreamManager::FromWebContents(
  51. content::WebContents::FromRenderFrameHost(render_frame_host));
  52. if (!pdf_viewer_stream_manager) {
  53. return;
  54. }
  55. content::RenderFrameHost* embedder_host =
  56. pdf_frame_util::GetEmbedderHost(render_frame_host);
  57. CHECK(embedder_host);
  58. pdf_viewer_stream_manager->SetPluginCanSave(embedder_host, can_save);
  59. return;
  60. }
  61. auto* guest_view =
  62. extensions::MimeHandlerViewGuest::FromRenderFrameHost(render_frame_host);
  63. if (guest_view) {
  64. guest_view->SetPluginCanSave(can_save);
  65. }
  66. }
  67. #if BUILDFLAG(ENABLE_SCREEN_AI_SERVICE)
  68. void ElectronPDFDocumentHelperClient::OnSearchifyStateChange(
  69. bool busy,
  70. content::WebContents* contents) {
  71. // TODO(crbug.com/360803943): Show promo and manage progress bubble.
  72. }
  73. #endif