electron_api_ipc_handler_impl.cc 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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_api_ipc_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. ElectronApiIPCHandlerImpl::ElectronApiIPCHandlerImpl(
  11. content::RenderFrameHost* frame_host,
  12. mojo::PendingAssociatedReceiver<mojom::ElectronApiIPC> 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. &ElectronApiIPCHandlerImpl::OnConnectionError, GetWeakPtr()));
  21. }
  22. ElectronApiIPCHandlerImpl::~ElectronApiIPCHandlerImpl() = default;
  23. void ElectronApiIPCHandlerImpl::WebContentsDestroyed() {
  24. delete this;
  25. }
  26. void ElectronApiIPCHandlerImpl::OnConnectionError() {
  27. delete this;
  28. }
  29. void ElectronApiIPCHandlerImpl::Message(bool internal,
  30. const std::string& channel,
  31. blink::CloneableMessage arguments) {
  32. api::WebContents* api_web_contents = api::WebContents::From(web_contents());
  33. if (api_web_contents) {
  34. api_web_contents->Message(internal, channel, std::move(arguments),
  35. GetRenderFrameHost());
  36. }
  37. }
  38. void ElectronApiIPCHandlerImpl::Invoke(bool internal,
  39. const std::string& channel,
  40. blink::CloneableMessage arguments,
  41. InvokeCallback callback) {
  42. api::WebContents* api_web_contents = api::WebContents::From(web_contents());
  43. if (api_web_contents) {
  44. api_web_contents->Invoke(internal, channel, std::move(arguments),
  45. std::move(callback), GetRenderFrameHost());
  46. }
  47. }
  48. void ElectronApiIPCHandlerImpl::ReceivePostMessage(
  49. const std::string& channel,
  50. blink::TransferableMessage message) {
  51. api::WebContents* api_web_contents = api::WebContents::From(web_contents());
  52. if (api_web_contents) {
  53. api_web_contents->ReceivePostMessage(channel, std::move(message),
  54. GetRenderFrameHost());
  55. }
  56. }
  57. void ElectronApiIPCHandlerImpl::MessageSync(bool internal,
  58. const std::string& channel,
  59. blink::CloneableMessage arguments,
  60. MessageSyncCallback callback) {
  61. api::WebContents* api_web_contents = api::WebContents::From(web_contents());
  62. if (api_web_contents) {
  63. api_web_contents->MessageSync(internal, channel, std::move(arguments),
  64. std::move(callback), GetRenderFrameHost());
  65. }
  66. }
  67. void ElectronApiIPCHandlerImpl::MessageHost(const std::string& channel,
  68. blink::CloneableMessage arguments) {
  69. api::WebContents* api_web_contents = api::WebContents::From(web_contents());
  70. if (api_web_contents) {
  71. api_web_contents->MessageHost(channel, std::move(arguments),
  72. GetRenderFrameHost());
  73. }
  74. }
  75. content::RenderFrameHost* ElectronApiIPCHandlerImpl::GetRenderFrameHost() {
  76. return content::RenderFrameHost::FromID(render_frame_host_id_);
  77. }
  78. // static
  79. void ElectronApiIPCHandlerImpl::Create(
  80. content::RenderFrameHost* frame_host,
  81. mojo::PendingAssociatedReceiver<mojom::ElectronApiIPC> receiver) {
  82. new ElectronApiIPCHandlerImpl(frame_host, std::move(receiver));
  83. }
  84. } // namespace electron