electron_api_ipc_handler_impl.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 SHELL_BROWSER_ELECTRON_API_IPC_HANDLER_IMPL_H_
  5. #define SHELL_BROWSER_ELECTRON_API_IPC_HANDLER_IMPL_H_
  6. #include <string>
  7. #include <vector>
  8. #include "base/macros.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "content/public/browser/web_contents_observer.h"
  11. #include "electron/shell/common/api/api.mojom.h"
  12. #include "mojo/public/cpp/bindings/associated_receiver.h"
  13. #include "shell/browser/api/electron_api_web_contents.h"
  14. namespace content {
  15. class RenderFrameHost;
  16. }
  17. namespace electron {
  18. class ElectronApiIPCHandlerImpl : public mojom::ElectronApiIPC,
  19. public content::WebContentsObserver {
  20. public:
  21. explicit ElectronApiIPCHandlerImpl(
  22. content::RenderFrameHost* render_frame_host,
  23. mojo::PendingAssociatedReceiver<mojom::ElectronApiIPC> receiver);
  24. static void Create(
  25. content::RenderFrameHost* frame_host,
  26. mojo::PendingAssociatedReceiver<mojom::ElectronApiIPC> receiver);
  27. // mojom::ElectronApiIPC:
  28. void Message(bool internal,
  29. const std::string& channel,
  30. blink::CloneableMessage arguments) override;
  31. void Invoke(bool internal,
  32. const std::string& channel,
  33. blink::CloneableMessage arguments,
  34. InvokeCallback callback) override;
  35. void ReceivePostMessage(const std::string& channel,
  36. blink::TransferableMessage message) override;
  37. void MessageSync(bool internal,
  38. const std::string& channel,
  39. blink::CloneableMessage arguments,
  40. MessageSyncCallback callback) override;
  41. void MessageTo(int32_t web_contents_id,
  42. const std::string& channel,
  43. blink::CloneableMessage arguments) 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. const int render_process_id_;
  56. const int render_frame_id_;
  57. mojo::AssociatedReceiver<mojom::ElectronApiIPC> receiver_{this};
  58. base::WeakPtrFactory<ElectronApiIPCHandlerImpl> weak_factory_{this};
  59. DISALLOW_COPY_AND_ASSIGN(ElectronApiIPCHandlerImpl);
  60. };
  61. } // namespace electron
  62. #endif // SHELL_BROWSER_ELECTRON_API_IPC_HANDLER_IMPL_H_