electron_api_ipc_handler_impl.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 <vector>
  8. #include "base/memory/weak_ptr.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 MessageTo(int32_t web_contents_id,
  45. const std::string& channel,
  46. blink::CloneableMessage arguments) override;
  47. void MessageHost(const std::string& channel,
  48. blink::CloneableMessage arguments) override;
  49. base::WeakPtr<ElectronApiIPCHandlerImpl> GetWeakPtr() {
  50. return weak_factory_.GetWeakPtr();
  51. }
  52. private:
  53. ~ElectronApiIPCHandlerImpl() override;
  54. // content::WebContentsObserver:
  55. void WebContentsDestroyed() override;
  56. void OnConnectionError();
  57. content::RenderFrameHost* GetRenderFrameHost();
  58. const int render_process_id_;
  59. const int render_frame_id_;
  60. mojo::AssociatedReceiver<mojom::ElectronApiIPC> receiver_{this};
  61. base::WeakPtrFactory<ElectronApiIPCHandlerImpl> weak_factory_{this};
  62. };
  63. } // namespace electron
  64. #endif // ELECTRON_SHELL_BROWSER_ELECTRON_API_IPC_HANDLER_IMPL_H_