electron_api_ipc_handler_impl.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #ifndef ELECTRON_SHELL_BROWSER_ELECTRON_API_IPC_HANDLER_IMPL_H_
  5. #define ELECTRON_SHELL_BROWSER_ELECTRON_API_IPC_HANDLER_IMPL_H_
  6. #include <string>
  7. #include "base/memory/weak_ptr.h"
  8. #include "content/public/browser/global_routing_id.h"
  9. #include "content/public/browser/web_contents_observer.h"
  10. #include "electron/shell/common/api/api.mojom.h"
  11. #include "mojo/public/cpp/bindings/associated_receiver.h"
  12. #include "shell/browser/api/electron_api_web_contents.h"
  13. namespace content {
  14. class RenderFrameHost;
  15. }
  16. namespace electron {
  17. class ElectronApiIPCHandlerImpl : public mojom::ElectronApiIPC,
  18. public content::WebContentsObserver {
  19. public:
  20. explicit ElectronApiIPCHandlerImpl(
  21. content::RenderFrameHost* render_frame_host,
  22. mojo::PendingAssociatedReceiver<mojom::ElectronApiIPC> receiver);
  23. static void Create(
  24. content::RenderFrameHost* frame_host,
  25. mojo::PendingAssociatedReceiver<mojom::ElectronApiIPC> receiver);
  26. // disable copy
  27. ElectronApiIPCHandlerImpl(const ElectronApiIPCHandlerImpl&) = delete;
  28. ElectronApiIPCHandlerImpl& operator=(const ElectronApiIPCHandlerImpl&) =
  29. delete;
  30. // mojom::ElectronApiIPC:
  31. void Message(bool internal,
  32. const std::string& channel,
  33. blink::CloneableMessage arguments) override;
  34. void Invoke(bool internal,
  35. const std::string& channel,
  36. blink::CloneableMessage arguments,
  37. InvokeCallback callback) override;
  38. void ReceivePostMessage(const std::string& channel,
  39. blink::TransferableMessage message) override;
  40. void MessageSync(bool internal,
  41. const std::string& channel,
  42. blink::CloneableMessage arguments,
  43. MessageSyncCallback callback) override;
  44. void MessageHost(const std::string& channel,
  45. blink::CloneableMessage arguments) override;
  46. base::WeakPtr<ElectronApiIPCHandlerImpl> GetWeakPtr() {
  47. return weak_factory_.GetWeakPtr();
  48. }
  49. private:
  50. ~ElectronApiIPCHandlerImpl() override;
  51. // content::WebContentsObserver:
  52. void WebContentsDestroyed() override;
  53. void OnConnectionError();
  54. content::RenderFrameHost* GetRenderFrameHost();
  55. content::GlobalRenderFrameHostId render_frame_host_id_;
  56. mojo::AssociatedReceiver<mojom::ElectronApiIPC> receiver_{this};
  57. base::WeakPtrFactory<ElectronApiIPCHandlerImpl> weak_factory_{this};
  58. };
  59. } // namespace electron
  60. #endif // ELECTRON_SHELL_BROWSER_ELECTRON_API_IPC_HANDLER_IMPL_H_