electron_web_contents_utility_handler_impl.cc 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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::SetTemporaryZoomLevel(
  37. double level) {
  38. api::WebContents* api_web_contents = api::WebContents::From(web_contents());
  39. if (api_web_contents) {
  40. api_web_contents->SetTemporaryZoomLevel(level);
  41. }
  42. }
  43. void ElectronWebContentsUtilityHandlerImpl::DoGetZoomLevel(
  44. DoGetZoomLevelCallback callback) {
  45. api::WebContents* api_web_contents = api::WebContents::From(web_contents());
  46. if (api_web_contents) {
  47. api_web_contents->DoGetZoomLevel(std::move(callback));
  48. }
  49. }
  50. content::RenderFrameHost*
  51. ElectronWebContentsUtilityHandlerImpl::GetRenderFrameHost() {
  52. return content::RenderFrameHost::FromID(render_frame_host_id_);
  53. }
  54. // static
  55. void ElectronWebContentsUtilityHandlerImpl::Create(
  56. content::RenderFrameHost* frame_host,
  57. mojo::PendingAssociatedReceiver<mojom::ElectronWebContentsUtility>
  58. receiver) {
  59. new ElectronWebContentsUtilityHandlerImpl(frame_host, std::move(receiver));
  60. }
  61. } // namespace electron