electron_extensions_api_client.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright (c) 2019 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/extensions/electron_extensions_api_client.h"
  5. #include <memory>
  6. #include <string>
  7. #include "electron/buildflags/buildflags.h"
  8. #include "extensions/browser/guest_view/extensions_guest_view_manager_delegate.h"
  9. #include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest_delegate.h"
  10. #include "printing/buildflags/buildflags.h"
  11. #include "shell/browser/api/electron_api_web_contents.h"
  12. #include "shell/browser/extensions/api/management/electron_management_api_delegate.h"
  13. #include "shell/browser/extensions/electron_extension_web_contents_observer.h"
  14. #include "shell/browser/extensions/electron_messaging_delegate.h"
  15. #include "v8/include/v8.h"
  16. #if BUILDFLAG(ENABLE_PRINTING)
  17. #include "components/printing/browser/print_manager_utils.h"
  18. #include "shell/browser/printing/print_view_manager_electron.h"
  19. #endif
  20. #if BUILDFLAG(ENABLE_PDF_VIEWER)
  21. #include "components/pdf/browser/pdf_web_contents_helper.h" // nogncheck
  22. #include "shell/browser/electron_pdf_web_contents_helper_client.h"
  23. #endif
  24. namespace extensions {
  25. class ElectronGuestViewManagerDelegate
  26. : public ExtensionsGuestViewManagerDelegate {
  27. public:
  28. ElectronGuestViewManagerDelegate() : ExtensionsGuestViewManagerDelegate() {}
  29. ~ElectronGuestViewManagerDelegate() override = default;
  30. // disable copy
  31. ElectronGuestViewManagerDelegate(const ElectronGuestViewManagerDelegate&) =
  32. delete;
  33. ElectronGuestViewManagerDelegate& operator=(
  34. const ElectronGuestViewManagerDelegate&) = delete;
  35. // GuestViewManagerDelegate:
  36. void OnGuestAdded(content::WebContents* guest_web_contents) const override {
  37. v8::Isolate* isolate = v8::Isolate::GetCurrent();
  38. v8::HandleScope scope(isolate);
  39. electron::api::WebContents::FromOrCreate(isolate, guest_web_contents);
  40. }
  41. };
  42. class ElectronMimeHandlerViewGuestDelegate
  43. : public MimeHandlerViewGuestDelegate {
  44. public:
  45. ElectronMimeHandlerViewGuestDelegate() = default;
  46. ~ElectronMimeHandlerViewGuestDelegate() override = default;
  47. // disable copy
  48. ElectronMimeHandlerViewGuestDelegate(
  49. const ElectronMimeHandlerViewGuestDelegate&) = delete;
  50. ElectronMimeHandlerViewGuestDelegate& operator=(
  51. const ElectronMimeHandlerViewGuestDelegate&) = delete;
  52. // MimeHandlerViewGuestDelegate.
  53. bool HandleContextMenu(content::RenderFrameHost& render_frame_host,
  54. const content::ContextMenuParams& params) override {
  55. auto* web_contents =
  56. content::WebContents::FromRenderFrameHost(&render_frame_host);
  57. if (!web_contents)
  58. return true;
  59. electron::api::WebContents* api_web_contents =
  60. electron::api::WebContents::From(
  61. web_contents->GetOutermostWebContents());
  62. if (api_web_contents)
  63. api_web_contents->HandleContextMenu(render_frame_host, params);
  64. return true;
  65. }
  66. void RecordLoadMetric(bool in_main_frame,
  67. const std::string& mime_type) override {}
  68. };
  69. ElectronExtensionsAPIClient::ElectronExtensionsAPIClient() = default;
  70. ElectronExtensionsAPIClient::~ElectronExtensionsAPIClient() = default;
  71. MessagingDelegate* ElectronExtensionsAPIClient::GetMessagingDelegate() {
  72. if (!messaging_delegate_)
  73. messaging_delegate_ = std::make_unique<ElectronMessagingDelegate>();
  74. return messaging_delegate_.get();
  75. }
  76. void ElectronExtensionsAPIClient::AttachWebContentsHelpers(
  77. content::WebContents* web_contents) const {
  78. #if BUILDFLAG(ENABLE_PRINTING)
  79. electron::PrintViewManagerElectron::CreateForWebContents(web_contents);
  80. #endif
  81. #if BUILDFLAG(ENABLE_PDF_VIEWER)
  82. pdf::PDFWebContentsHelper::CreateForWebContentsWithClient(
  83. web_contents, std::make_unique<ElectronPDFWebContentsHelperClient>());
  84. #endif
  85. extensions::ElectronExtensionWebContentsObserver::CreateForWebContents(
  86. web_contents);
  87. }
  88. ManagementAPIDelegate*
  89. ElectronExtensionsAPIClient::CreateManagementAPIDelegate() const {
  90. return new ElectronManagementAPIDelegate;
  91. }
  92. std::unique_ptr<MimeHandlerViewGuestDelegate>
  93. ElectronExtensionsAPIClient::CreateMimeHandlerViewGuestDelegate(
  94. MimeHandlerViewGuest* guest) const {
  95. return std::make_unique<ElectronMimeHandlerViewGuestDelegate>();
  96. }
  97. std::unique_ptr<guest_view::GuestViewManagerDelegate>
  98. ElectronExtensionsAPIClient::CreateGuestViewManagerDelegate() const {
  99. return std::make_unique<ElectronGuestViewManagerDelegate>();
  100. }
  101. } // namespace extensions