2020-10-09 16:50:46 +00:00
|
|
|
// Copyright (c) 2020 Samuel Maddock <sam@samuelmaddock.com>.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2021-11-22 07:34:31 +00:00
|
|
|
#ifndef ELECTRON_SHELL_BROWSER_API_ELECTRON_API_WEB_FRAME_MAIN_H_
|
|
|
|
#define ELECTRON_SHELL_BROWSER_API_ELECTRON_API_WEB_FRAME_MAIN_H_
|
2020-10-09 16:50:46 +00:00
|
|
|
|
2024-01-10 22:23:35 +00:00
|
|
|
#include <optional>
|
2020-10-09 16:50:46 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2023-05-11 20:07:39 +00:00
|
|
|
#include "base/memory/raw_ptr.h"
|
2021-02-09 20:16:21 +00:00
|
|
|
#include "base/memory/weak_ptr.h"
|
2020-10-26 03:03:34 +00:00
|
|
|
#include "base/process/process.h"
|
2024-09-25 11:19:39 +00:00
|
|
|
#include "content/public/browser/frame_tree_node_id.h"
|
2020-10-09 16:50:46 +00:00
|
|
|
#include "gin/wrappable.h"
|
2024-07-29 17:42:57 +00:00
|
|
|
#include "mojo/public/cpp/bindings/pending_receiver.h"
|
2021-06-03 08:05:04 +00:00
|
|
|
#include "mojo/public/cpp/bindings/remote.h"
|
2021-09-01 22:21:15 +00:00
|
|
|
#include "shell/browser/event_emitter_mixin.h"
|
2024-09-17 19:58:56 +00:00
|
|
|
#include "shell/common/api/api.mojom.h"
|
2021-01-15 00:00:37 +00:00
|
|
|
#include "shell/common/gin_helper/constructible.h"
|
2021-02-08 19:30:18 +00:00
|
|
|
#include "shell/common/gin_helper/pinnable.h"
|
2021-04-22 16:00:58 +00:00
|
|
|
#include "third_party/blink/public/mojom/page/page_visibility_state.mojom-forward.h"
|
2020-10-09 16:50:46 +00:00
|
|
|
|
|
|
|
class GURL;
|
|
|
|
|
|
|
|
namespace content {
|
|
|
|
class RenderFrameHost;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace gin {
|
|
|
|
class Arguments;
|
2024-07-29 17:42:57 +00:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class Handle;
|
|
|
|
} // namespace gin
|
2020-10-09 16:50:46 +00:00
|
|
|
|
2022-06-29 19:55:47 +00:00
|
|
|
namespace electron::api {
|
2020-10-09 16:50:46 +00:00
|
|
|
|
2021-08-18 18:23:41 +00:00
|
|
|
class WebContents;
|
|
|
|
|
2020-10-09 16:50:46 +00:00
|
|
|
// Bindings for accessing frames from the main process.
|
2024-09-04 23:40:02 +00:00
|
|
|
class WebFrameMain final : public gin::Wrappable<WebFrameMain>,
|
|
|
|
public gin_helper::EventEmitterMixin<WebFrameMain>,
|
|
|
|
public gin_helper::Pinnable<WebFrameMain>,
|
|
|
|
public gin_helper::Constructible<WebFrameMain> {
|
2020-10-09 16:50:46 +00:00
|
|
|
public:
|
2021-01-15 00:00:37 +00:00
|
|
|
// Create a new WebFrameMain and return the V8 wrapper of it.
|
|
|
|
static gin::Handle<WebFrameMain> New(v8::Isolate* isolate);
|
|
|
|
|
2020-10-09 16:50:46 +00:00
|
|
|
static gin::Handle<WebFrameMain> From(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
content::RenderFrameHost* render_frame_host);
|
2022-08-03 23:55:12 +00:00
|
|
|
static gin::Handle<WebFrameMain> FromOrNull(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
content::RenderFrameHost* render_frame_host);
|
2024-09-25 11:19:39 +00:00
|
|
|
static WebFrameMain* FromFrameTreeNodeId(
|
|
|
|
content::FrameTreeNodeId frame_tree_node_id);
|
2021-08-18 18:23:41 +00:00
|
|
|
static WebFrameMain* FromRenderFrameHost(
|
|
|
|
content::RenderFrameHost* render_frame_host);
|
2021-02-09 20:16:21 +00:00
|
|
|
|
2023-07-10 09:49:20 +00:00
|
|
|
// gin_helper::Constructible
|
|
|
|
static void FillObjectTemplate(v8::Isolate*, v8::Local<v8::ObjectTemplate>);
|
|
|
|
static const char* GetClassName() { return "WebFrameMain"; }
|
|
|
|
|
2020-10-09 16:50:46 +00:00
|
|
|
// gin::Wrappable
|
|
|
|
static gin::WrapperInfo kWrapperInfo;
|
|
|
|
const char* GetTypeName() override;
|
|
|
|
|
2021-08-18 18:23:41 +00:00
|
|
|
content::RenderFrameHost* render_frame_host() const { return render_frame_; }
|
|
|
|
|
2021-11-03 11:41:45 +00:00
|
|
|
// disable copy
|
|
|
|
WebFrameMain(const WebFrameMain&) = delete;
|
|
|
|
WebFrameMain& operator=(const WebFrameMain&) = delete;
|
|
|
|
|
2020-10-09 16:50:46 +00:00
|
|
|
protected:
|
|
|
|
explicit WebFrameMain(content::RenderFrameHost* render_frame);
|
|
|
|
~WebFrameMain() override;
|
|
|
|
|
|
|
|
private:
|
2021-08-18 18:23:41 +00:00
|
|
|
friend class WebContents;
|
|
|
|
|
|
|
|
// Called when FrameTreeNode is deleted.
|
|
|
|
void Destroyed();
|
|
|
|
|
|
|
|
// Mark RenderFrameHost as disposed and to no longer access it. This can
|
2024-07-29 17:42:57 +00:00
|
|
|
// happen when the WebFrameMain v8-forward.handle is GC'd or when a
|
|
|
|
// FrameTreeNode is removed.
|
2021-08-18 18:23:41 +00:00
|
|
|
void MarkRenderFrameDisposed();
|
|
|
|
|
|
|
|
// Swap out the internal RFH when cross-origin navigation occurs.
|
|
|
|
void UpdateRenderFrameHost(content::RenderFrameHost* rfh);
|
|
|
|
|
|
|
|
const mojo::Remote<mojom::ElectronRenderer>& GetRendererApi();
|
2022-05-19 18:34:58 +00:00
|
|
|
void MaybeSetupMojoConnection();
|
|
|
|
void TeardownMojoConnection();
|
|
|
|
void OnRendererConnectionError();
|
2021-08-18 18:23:41 +00:00
|
|
|
|
2020-10-09 16:50:46 +00:00
|
|
|
// WebFrameMain can outlive its RenderFrameHost pointer so we need to check
|
|
|
|
// whether its been disposed of prior to accessing it.
|
|
|
|
bool CheckRenderFrame() const;
|
|
|
|
|
|
|
|
v8::Local<v8::Promise> ExecuteJavaScript(gin::Arguments* args,
|
2021-03-16 16:18:45 +00:00
|
|
|
const std::u16string& code);
|
2021-01-26 21:46:09 +00:00
|
|
|
bool Reload();
|
2021-01-15 00:00:37 +00:00
|
|
|
void Send(v8::Isolate* isolate,
|
|
|
|
bool internal,
|
|
|
|
const std::string& channel,
|
|
|
|
v8::Local<v8::Value> args);
|
|
|
|
void PostMessage(v8::Isolate* isolate,
|
|
|
|
const std::string& channel,
|
|
|
|
v8::Local<v8::Value> message_value,
|
2024-01-10 22:23:35 +00:00
|
|
|
std::optional<v8::Local<v8::Value>> transfer);
|
2020-10-09 16:50:46 +00:00
|
|
|
|
2024-09-25 11:19:39 +00:00
|
|
|
int FrameTreeNodeIDAsInt() const;
|
2021-01-26 21:46:09 +00:00
|
|
|
std::string Name() const;
|
|
|
|
base::ProcessId OSProcessID() const;
|
|
|
|
int ProcessID() const;
|
|
|
|
int RoutingID() const;
|
|
|
|
GURL URL() const;
|
2022-09-01 00:40:57 +00:00
|
|
|
std::string Origin() const;
|
2021-04-22 16:00:58 +00:00
|
|
|
blink::mojom::PageVisibilityState VisibilityState() const;
|
2021-01-26 21:46:09 +00:00
|
|
|
|
|
|
|
content::RenderFrameHost* Top() const;
|
|
|
|
content::RenderFrameHost* Parent() const;
|
|
|
|
std::vector<content::RenderFrameHost*> Frames() const;
|
|
|
|
std::vector<content::RenderFrameHost*> FramesInSubtree() const;
|
2020-10-09 16:50:46 +00:00
|
|
|
|
2021-09-01 22:21:15 +00:00
|
|
|
void DOMContentLoaded();
|
2021-02-09 20:16:21 +00:00
|
|
|
|
|
|
|
mojo::Remote<mojom::ElectronRenderer> renderer_api_;
|
|
|
|
mojo::PendingReceiver<mojom::ElectronRenderer> pending_receiver_;
|
|
|
|
|
2024-09-25 11:19:39 +00:00
|
|
|
content::FrameTreeNodeId frame_tree_node_id_;
|
2021-08-18 18:23:41 +00:00
|
|
|
|
2023-05-11 20:07:39 +00:00
|
|
|
raw_ptr<content::RenderFrameHost> render_frame_ = nullptr;
|
2020-10-09 16:50:46 +00:00
|
|
|
|
|
|
|
// Whether the RenderFrameHost has been removed and that it should no longer
|
|
|
|
// be accessed.
|
|
|
|
bool render_frame_disposed_ = false;
|
|
|
|
|
2021-02-09 20:16:21 +00:00
|
|
|
base::WeakPtrFactory<WebFrameMain> weak_factory_{this};
|
2020-10-09 16:50:46 +00:00
|
|
|
};
|
|
|
|
|
2022-06-29 19:55:47 +00:00
|
|
|
} // namespace electron::api
|
2020-10-09 16:50:46 +00:00
|
|
|
|
2021-11-22 07:34:31 +00:00
|
|
|
#endif // ELECTRON_SHELL_BROWSER_API_ELECTRON_API_WEB_FRAME_MAIN_H_
|