electron_web_contents_utility_handler_impl.cc 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright (c) 2022 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_web_contents_utility_handler_impl.h"
  5. #include <utility>
  6. #include "content/public/browser/render_frame_host.h"
  7. #include "content/public/browser/render_process_host.h"
  8. #include "mojo/public/cpp/bindings/self_owned_receiver.h"
  9. namespace electron {
  10. ElectronWebContentsUtilityHandlerImpl::ElectronWebContentsUtilityHandlerImpl(
  11. content::RenderFrameHost* frame_host,
  12. mojo::PendingAssociatedReceiver<mojom::ElectronWebContentsUtility> receiver)
  13. : render_frame_host_id_(frame_host->GetGlobalId()) {
  14. content::WebContents* web_contents =
  15. content::WebContents::FromRenderFrameHost(frame_host);
  16. DCHECK(web_contents);
  17. content::WebContentsObserver::Observe(web_contents);
  18. receiver_.Bind(std::move(receiver));
  19. receiver_.set_disconnect_handler(base::BindOnce(
  20. &ElectronWebContentsUtilityHandlerImpl::OnConnectionError, GetWeakPtr()));
  21. }
  22. ElectronWebContentsUtilityHandlerImpl::
  23. ~ElectronWebContentsUtilityHandlerImpl() = default;
  24. void ElectronWebContentsUtilityHandlerImpl::WebContentsDestroyed() {
  25. delete this;
  26. }
  27. void ElectronWebContentsUtilityHandlerImpl::OnConnectionError() {
  28. delete this;
  29. }
  30. void ElectronWebContentsUtilityHandlerImpl::OnFirstNonEmptyLayout() {
  31. api::WebContents* api_web_contents = api::WebContents::From(web_contents());
  32. if (api_web_contents) {
  33. api_web_contents->OnFirstNonEmptyLayout(GetRenderFrameHost());
  34. }
  35. }
  36. void ElectronWebContentsUtilityHandlerImpl::UpdateDraggableRegions(
  37. std::vector<mojom::DraggableRegionPtr> regions) {
  38. api::WebContents* api_web_contents = api::WebContents::From(web_contents());
  39. if (api_web_contents) {
  40. api_web_contents->UpdateDraggableRegions(std::move(regions));
  41. }
  42. }
  43. void ElectronWebContentsUtilityHandlerImpl::SetTemporaryZoomLevel(
  44. double level) {
  45. api::WebContents* api_web_contents = api::WebContents::From(web_contents());
  46. if (api_web_contents) {
  47. api_web_contents->SetTemporaryZoomLevel(level);
  48. }
  49. }
  50. void ElectronWebContentsUtilityHandlerImpl::DoGetZoomLevel(
  51. DoGetZoomLevelCallback callback) {
  52. api::WebContents* api_web_contents = api::WebContents::From(web_contents());
  53. if (api_web_contents) {
  54. api_web_contents->DoGetZoomLevel(std::move(callback));
  55. }
  56. }
  57. content::RenderFrameHost*
  58. ElectronWebContentsUtilityHandlerImpl::GetRenderFrameHost() {
  59. return content::RenderFrameHost::FromID(render_frame_host_id_);
  60. }
  61. // static
  62. void ElectronWebContentsUtilityHandlerImpl::Create(
  63. content::RenderFrameHost* frame_host,
  64. mojo::PendingAssociatedReceiver<mojom::ElectronWebContentsUtility>
  65. receiver) {
  66. new ElectronWebContentsUtilityHandlerImpl(frame_host, std::move(receiver));
  67. }
  68. } // namespace electron