electron_api_sw_ipc_handler_impl.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright (c) 2025 Salesforce, 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_SW_IPC_HANDLER_IMPL_H_
  5. #define ELECTRON_SHELL_BROWSER_ELECTRON_API_SW_IPC_HANDLER_IMPL_H_
  6. #include <string>
  7. #include "base/memory/weak_ptr.h"
  8. #include "content/public/browser/browser_thread.h"
  9. #include "content/public/browser/render_process_host_observer.h"
  10. #include "electron/shell/common/api/api.mojom.h"
  11. #include "gin/handle.h"
  12. #include "mojo/public/cpp/bindings/associated_receiver.h"
  13. #include "shell/common/gin_helper/event.h"
  14. namespace content {
  15. class RenderProcessHost;
  16. }
  17. namespace electron {
  18. class ElectronBrowserContext;
  19. namespace api {
  20. class Session;
  21. }
  22. class ElectronApiSWIPCHandlerImpl : public mojom::ElectronApiIPC,
  23. public content::RenderProcessHostObserver {
  24. public:
  25. explicit ElectronApiSWIPCHandlerImpl(
  26. content::RenderProcessHost* render_process_host,
  27. int64_t version_id,
  28. mojo::PendingAssociatedReceiver<mojom::ElectronApiIPC> receiver);
  29. static void BindReceiver(
  30. int render_process_id,
  31. int64_t version_id,
  32. mojo::PendingAssociatedReceiver<mojom::ElectronApiIPC> receiver);
  33. // disable copy
  34. ElectronApiSWIPCHandlerImpl(const ElectronApiSWIPCHandlerImpl&) = delete;
  35. ElectronApiSWIPCHandlerImpl& operator=(const ElectronApiSWIPCHandlerImpl&) =
  36. delete;
  37. ~ElectronApiSWIPCHandlerImpl() override;
  38. // mojom::ElectronApiIPC:
  39. void Message(bool internal,
  40. const std::string& channel,
  41. blink::CloneableMessage arguments) override;
  42. void Invoke(bool internal,
  43. const std::string& channel,
  44. blink::CloneableMessage arguments,
  45. InvokeCallback callback) override;
  46. void ReceivePostMessage(const std::string& channel,
  47. blink::TransferableMessage message) override;
  48. void MessageSync(bool internal,
  49. const std::string& channel,
  50. blink::CloneableMessage arguments,
  51. MessageSyncCallback callback) override;
  52. void MessageHost(const std::string& channel,
  53. blink::CloneableMessage arguments) override;
  54. base::WeakPtr<ElectronApiSWIPCHandlerImpl> GetWeakPtr() {
  55. return weak_factory_.GetWeakPtr();
  56. }
  57. private:
  58. ElectronBrowserContext* GetBrowserContext();
  59. api::Session* GetSession();
  60. gin::Handle<gin_helper::internal::Event> MakeIPCEvent(
  61. v8::Isolate* isolate,
  62. api::Session* session,
  63. bool internal,
  64. electron::mojom::ElectronApiIPC::InvokeCallback callback =
  65. electron::mojom::ElectronApiIPC::InvokeCallback());
  66. // content::RenderProcessHostObserver
  67. void RenderProcessExited(
  68. content::RenderProcessHost* host,
  69. const content::ChildProcessTerminationInfo& info) override;
  70. void RemoteDisconnected();
  71. // Destroys this instance by removing it from the ServiceWorkerIPCList.
  72. void Destroy();
  73. // This is safe because ElectronApiSWIPCHandlerImpl is tied to the life time
  74. // of RenderProcessHost.
  75. const raw_ptr<content::RenderProcessHost> render_process_host_;
  76. // Service worker version ID.
  77. int64_t version_id_;
  78. mojo::AssociatedReceiver<mojom::ElectronApiIPC> receiver_{this};
  79. base::WeakPtrFactory<ElectronApiSWIPCHandlerImpl> weak_factory_{this};
  80. };
  81. } // namespace electron
  82. #endif // ELECTRON_SHELL_BROWSER_ELECTRON_API_SW_IPC_HANDLER_IMPL_H_