electron_api_web_frame_main.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright (c) 2020 Samuel Maddock <[email protected]>.
  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_API_ELECTRON_API_WEB_FRAME_MAIN_H_
  5. #define ELECTRON_SHELL_BROWSER_API_ELECTRON_API_WEB_FRAME_MAIN_H_
  6. #include <optional>
  7. #include <string>
  8. #include <vector>
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/process/process.h"
  11. #include "content/public/browser/frame_tree_node_id.h"
  12. #include "content/public/browser/global_routing_id.h"
  13. #include "gin/wrappable.h"
  14. #include "mojo/public/cpp/bindings/pending_receiver.h"
  15. #include "mojo/public/cpp/bindings/remote.h"
  16. #include "shell/browser/event_emitter_mixin.h"
  17. #include "shell/common/api/api.mojom.h"
  18. #include "shell/common/gin_helper/constructible.h"
  19. #include "shell/common/gin_helper/pinnable.h"
  20. #include "third_party/blink/public/mojom/page/page_visibility_state.mojom-forward.h"
  21. class GURL;
  22. namespace content {
  23. class RenderFrameHost;
  24. }
  25. namespace gin {
  26. class Arguments;
  27. template <typename T>
  28. class Handle;
  29. } // namespace gin
  30. namespace gin_helper {
  31. template <typename T>
  32. class Promise;
  33. } // namespace gin_helper
  34. namespace electron::api {
  35. class WebContents;
  36. // Bindings for accessing frames from the main process.
  37. class WebFrameMain final : public gin::Wrappable<WebFrameMain>,
  38. public gin_helper::EventEmitterMixin<WebFrameMain>,
  39. public gin_helper::Pinnable<WebFrameMain>,
  40. public gin_helper::Constructible<WebFrameMain> {
  41. public:
  42. // Create a new WebFrameMain and return the V8 wrapper of it.
  43. static gin::Handle<WebFrameMain> New(v8::Isolate* isolate);
  44. static gin::Handle<WebFrameMain> From(
  45. v8::Isolate* isolate,
  46. content::RenderFrameHost* render_frame_host);
  47. static WebFrameMain* FromFrameTreeNodeId(
  48. content::FrameTreeNodeId frame_tree_node_id);
  49. static WebFrameMain* FromFrameToken(
  50. content::GlobalRenderFrameHostToken frame_token);
  51. static WebFrameMain* FromRenderFrameHost(
  52. content::RenderFrameHost* render_frame_host);
  53. // gin_helper::Constructible
  54. static void FillObjectTemplate(v8::Isolate*, v8::Local<v8::ObjectTemplate>);
  55. static const char* GetClassName() { return "WebFrameMain"; }
  56. // gin::Wrappable
  57. static gin::WrapperInfo kWrapperInfo;
  58. const char* GetTypeName() override;
  59. content::RenderFrameHost* render_frame_host() const;
  60. // disable copy
  61. WebFrameMain(const WebFrameMain&) = delete;
  62. WebFrameMain& operator=(const WebFrameMain&) = delete;
  63. protected:
  64. explicit WebFrameMain(content::RenderFrameHost* render_frame);
  65. ~WebFrameMain() override;
  66. private:
  67. friend class WebContents;
  68. // Called when FrameTreeNode is deleted.
  69. void Destroyed();
  70. // Mark RenderFrameHost as disposed and to no longer access it. This can
  71. // happen when the WebFrameMain v8-forward.handle is GC'd or when a
  72. // FrameTreeNode is removed.
  73. void MarkRenderFrameDisposed();
  74. // Swap out the internal RFH when cross-origin navigation occurs.
  75. void UpdateRenderFrameHost(content::RenderFrameHost* rfh);
  76. const mojo::Remote<mojom::ElectronRenderer>& GetRendererApi();
  77. void MaybeSetupMojoConnection();
  78. void TeardownMojoConnection();
  79. void OnRendererConnectionError();
  80. [[nodiscard]] bool HasRenderFrame() const;
  81. // Throws a JS error if HasRenderFrame() is false.
  82. // WebFrameMain can outlive its RenderFrameHost pointer,
  83. // so we need to check whether its been disposed of
  84. // prior to accessing it.
  85. bool CheckRenderFrame() const;
  86. v8::Local<v8::Promise> ExecuteJavaScript(gin::Arguments* args,
  87. const std::u16string& code);
  88. bool Reload();
  89. bool IsDestroyed() const;
  90. void Send(v8::Isolate* isolate,
  91. bool internal,
  92. const std::string& channel,
  93. v8::Local<v8::Value> args);
  94. void PostMessage(v8::Isolate* isolate,
  95. const std::string& channel,
  96. v8::Local<v8::Value> message_value,
  97. std::optional<v8::Local<v8::Value>> transfer);
  98. bool Detached() const;
  99. content::FrameTreeNodeId FrameTreeNodeID() const;
  100. std::string Name() const;
  101. base::ProcessId OSProcessID() const;
  102. int32_t ProcessID() const;
  103. int RoutingID() const;
  104. GURL URL() const;
  105. std::string Origin() const;
  106. blink::mojom::PageVisibilityState VisibilityState() const;
  107. content::RenderFrameHost* Top() const;
  108. content::RenderFrameHost* Parent() const;
  109. std::vector<content::RenderFrameHost*> Frames() const;
  110. std::vector<content::RenderFrameHost*> FramesInSubtree() const;
  111. const char* LifecycleStateForTesting() const;
  112. v8::Local<v8::Promise> CollectDocumentJSCallStack(gin::Arguments* args);
  113. void CollectedJavaScriptCallStack(
  114. gin_helper::Promise<base::Value> promise,
  115. const std::string& untrusted_javascript_call_stack,
  116. const std::optional<blink::LocalFrameToken>& remote_frame_token);
  117. void DOMContentLoaded();
  118. mojo::Remote<mojom::ElectronRenderer> renderer_api_;
  119. mojo::PendingReceiver<mojom::ElectronRenderer> pending_receiver_;
  120. content::FrameTreeNodeId frame_tree_node_id_;
  121. content::GlobalRenderFrameHostToken frame_token_;
  122. // Whether the RenderFrameHost has been removed and that it should no longer
  123. // be accessed.
  124. bool render_frame_disposed_ = false;
  125. // Whether the content::RenderFrameHost is detached from the frame
  126. // tree. This can occur while it's running unload handlers.
  127. bool render_frame_detached_;
  128. base::WeakPtrFactory<WebFrameMain> weak_factory_{this};
  129. };
  130. } // namespace electron::api
  131. #endif // ELECTRON_SHELL_BROWSER_API_ELECTRON_API_WEB_FRAME_MAIN_H_