2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
2014-04-25 09:49:37 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2013-04-23 04:18:07 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-01-22 06:31:37 +00:00
|
|
|
#include <string>
|
|
|
|
|
2018-03-09 09:31:09 +00:00
|
|
|
#include "content/public/renderer/render_frame.h"
|
2020-04-29 22:51:41 +00:00
|
|
|
#include "content/public/renderer/render_frame_observer.h"
|
2019-09-19 15:09:15 +00:00
|
|
|
#include "gin/dictionary.h"
|
|
|
|
#include "gin/handle.h"
|
|
|
|
#include "gin/object_template_builder.h"
|
|
|
|
#include "gin/wrappable.h"
|
2019-04-02 22:38:16 +00:00
|
|
|
#include "services/service_manager/public/cpp/interface_provider.h"
|
2019-09-19 15:09:15 +00:00
|
|
|
#include "shell/common/api/api.mojom.h"
|
2019-10-31 07:56:00 +00:00
|
|
|
#include "shell/common/gin_converters/blink_converter.h"
|
|
|
|
#include "shell/common/gin_converters/value_converter.h"
|
2020-06-04 23:25:25 +00:00
|
|
|
#include "shell/common/gin_helper/error_thrower.h"
|
2020-03-12 01:07:54 +00:00
|
|
|
#include "shell/common/gin_helper/function_template_extensions.h"
|
2019-11-01 06:10:32 +00:00
|
|
|
#include "shell/common/gin_helper/promise.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/node_bindings.h"
|
|
|
|
#include "shell/common/node_includes.h"
|
2020-03-12 01:07:54 +00:00
|
|
|
#include "shell/common/v8_value_serializer.h"
|
2022-02-08 16:33:21 +00:00
|
|
|
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
|
2018-07-20 16:08:18 +00:00
|
|
|
#include "third_party/blink/public/web/web_local_frame.h"
|
2020-03-12 01:07:54 +00:00
|
|
|
#include "third_party/blink/public/web/web_message_port_converter.h"
|
2013-12-11 07:48:19 +00:00
|
|
|
|
2014-07-28 07:29:51 +00:00
|
|
|
using blink::WebLocalFrame;
|
2018-04-18 01:55:30 +00:00
|
|
|
using content::RenderFrame;
|
2013-04-23 04:18:07 +00:00
|
|
|
|
2019-01-22 06:31:37 +00:00
|
|
|
namespace {
|
2013-04-23 04:18:07 +00:00
|
|
|
|
2020-06-04 23:25:25 +00:00
|
|
|
const char kIPCMethodCalledAfterContextReleasedError[] =
|
|
|
|
"IPC method called after context was released";
|
|
|
|
|
2018-03-09 09:31:09 +00:00
|
|
|
RenderFrame* GetCurrentRenderFrame() {
|
2017-06-16 20:42:33 +00:00
|
|
|
WebLocalFrame* frame = WebLocalFrame::FrameForCurrentContext();
|
2013-04-23 04:18:07 +00:00
|
|
|
if (!frame)
|
2016-07-10 09:52:28 +00:00
|
|
|
return nullptr;
|
2013-04-23 04:18:07 +00:00
|
|
|
|
2018-03-09 09:31:09 +00:00
|
|
|
return RenderFrame::FromWebFrame(frame);
|
2013-04-23 04:18:07 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 22:51:41 +00:00
|
|
|
class IPCRenderer : public gin::Wrappable<IPCRenderer>,
|
2024-05-29 18:07:02 +00:00
|
|
|
private content::RenderFrameObserver {
|
2019-04-02 22:38:16 +00:00
|
|
|
public:
|
2019-09-19 15:09:15 +00:00
|
|
|
static gin::WrapperInfo kWrapperInfo;
|
|
|
|
|
|
|
|
static gin::Handle<IPCRenderer> Create(v8::Isolate* isolate) {
|
|
|
|
return gin::CreateHandle(isolate, new IPCRenderer(isolate));
|
|
|
|
}
|
|
|
|
|
2020-04-29 22:51:41 +00:00
|
|
|
explicit IPCRenderer(v8::Isolate* isolate)
|
|
|
|
: content::RenderFrameObserver(GetCurrentRenderFrame()) {
|
2019-04-02 22:38:16 +00:00
|
|
|
RenderFrame* render_frame = GetCurrentRenderFrame();
|
|
|
|
DCHECK(render_frame);
|
2020-04-29 22:51:41 +00:00
|
|
|
weak_context_ =
|
|
|
|
v8::Global<v8::Context>(isolate, isolate->GetCurrentContext());
|
|
|
|
weak_context_.SetWeak();
|
2019-06-05 00:10:31 +00:00
|
|
|
|
2022-02-08 16:33:21 +00:00
|
|
|
render_frame->GetRemoteAssociatedInterfaces()->GetInterface(
|
2022-03-19 02:50:05 +00:00
|
|
|
&electron_ipc_remote_);
|
2019-04-02 22:38:16 +00:00
|
|
|
}
|
2019-06-05 00:10:31 +00:00
|
|
|
|
2022-03-19 02:50:05 +00:00
|
|
|
void OnDestruct() override { electron_ipc_remote_.reset(); }
|
2020-04-29 22:51:41 +00:00
|
|
|
|
|
|
|
void WillReleaseScriptContext(v8::Local<v8::Context> context,
|
|
|
|
int32_t world_id) override {
|
|
|
|
if (weak_context_.IsEmpty() ||
|
|
|
|
weak_context_.Get(context->GetIsolate()) == context)
|
2022-03-19 02:50:05 +00:00
|
|
|
electron_ipc_remote_.reset();
|
2020-04-29 22:51:41 +00:00
|
|
|
}
|
|
|
|
|
2019-09-19 15:09:15 +00:00
|
|
|
// gin::Wrappable:
|
|
|
|
gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
|
|
|
|
v8::Isolate* isolate) override {
|
|
|
|
return gin::Wrappable<IPCRenderer>::GetObjectTemplateBuilder(isolate)
|
2020-04-29 22:51:41 +00:00
|
|
|
.SetMethod("send", &IPCRenderer::SendMessage)
|
2019-04-03 21:22:23 +00:00
|
|
|
.SetMethod("sendSync", &IPCRenderer::SendSync)
|
2019-05-31 17:25:19 +00:00
|
|
|
.SetMethod("sendToHost", &IPCRenderer::SendToHost)
|
2020-03-12 01:07:54 +00:00
|
|
|
.SetMethod("invoke", &IPCRenderer::Invoke)
|
|
|
|
.SetMethod("postMessage", &IPCRenderer::PostMessage);
|
2019-04-02 22:38:16 +00:00
|
|
|
}
|
2019-06-05 00:10:31 +00:00
|
|
|
|
2019-09-19 15:09:15 +00:00
|
|
|
const char* GetTypeName() override { return "IPCRenderer"; }
|
2019-04-02 22:38:16 +00:00
|
|
|
|
2019-09-19 15:09:15 +00:00
|
|
|
private:
|
2020-04-29 22:51:41 +00:00
|
|
|
void SendMessage(v8::Isolate* isolate,
|
2020-06-04 23:25:25 +00:00
|
|
|
gin_helper::ErrorThrower thrower,
|
2020-04-29 22:51:41 +00:00
|
|
|
bool internal,
|
|
|
|
const std::string& channel,
|
|
|
|
v8::Local<v8::Value> arguments) {
|
2022-03-19 02:50:05 +00:00
|
|
|
if (!electron_ipc_remote_) {
|
2020-06-04 23:25:25 +00:00
|
|
|
thrower.ThrowError(kIPCMethodCalledAfterContextReleasedError);
|
|
|
|
return;
|
|
|
|
}
|
2019-10-09 17:59:08 +00:00
|
|
|
blink::CloneableMessage message;
|
2020-03-12 01:07:54 +00:00
|
|
|
if (!electron::SerializeV8Value(isolate, arguments, &message)) {
|
2019-10-09 17:59:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-03-19 02:50:05 +00:00
|
|
|
electron_ipc_remote_->Message(internal, channel, std::move(message));
|
2019-04-02 22:38:16 +00:00
|
|
|
}
|
|
|
|
|
2019-09-19 15:09:15 +00:00
|
|
|
v8::Local<v8::Promise> Invoke(v8::Isolate* isolate,
|
2020-06-04 23:25:25 +00:00
|
|
|
gin_helper::ErrorThrower thrower,
|
2019-08-23 22:45:50 +00:00
|
|
|
bool internal,
|
2019-05-31 17:25:19 +00:00
|
|
|
const std::string& channel,
|
2019-10-09 17:59:08 +00:00
|
|
|
v8::Local<v8::Value> arguments) {
|
2022-03-19 02:50:05 +00:00
|
|
|
if (!electron_ipc_remote_) {
|
2020-06-04 23:25:25 +00:00
|
|
|
thrower.ThrowError(kIPCMethodCalledAfterContextReleasedError);
|
|
|
|
return v8::Local<v8::Promise>();
|
|
|
|
}
|
2019-10-09 17:59:08 +00:00
|
|
|
blink::CloneableMessage message;
|
2020-03-12 01:07:54 +00:00
|
|
|
if (!electron::SerializeV8Value(isolate, arguments, &message)) {
|
2019-10-09 17:59:08 +00:00
|
|
|
return v8::Local<v8::Promise>();
|
|
|
|
}
|
2019-11-01 06:10:32 +00:00
|
|
|
gin_helper::Promise<blink::CloneableMessage> p(isolate);
|
2019-05-31 17:25:19 +00:00
|
|
|
auto handle = p.GetHandle();
|
2019-06-05 00:10:31 +00:00
|
|
|
|
2022-03-19 02:50:05 +00:00
|
|
|
electron_ipc_remote_->Invoke(
|
2019-10-09 17:59:08 +00:00
|
|
|
internal, channel, std::move(message),
|
|
|
|
base::BindOnce(
|
2019-11-01 06:10:32 +00:00
|
|
|
[](gin_helper::Promise<blink::CloneableMessage> p,
|
|
|
|
blink::CloneableMessage result) { p.Resolve(result); },
|
2019-10-09 17:59:08 +00:00
|
|
|
std::move(p)));
|
2019-05-31 17:25:19 +00:00
|
|
|
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2020-03-12 01:07:54 +00:00
|
|
|
void PostMessage(v8::Isolate* isolate,
|
|
|
|
gin_helper::ErrorThrower thrower,
|
|
|
|
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) {
|
2022-03-19 02:50:05 +00:00
|
|
|
if (!electron_ipc_remote_) {
|
2020-06-04 23:25:25 +00:00
|
|
|
thrower.ThrowError(kIPCMethodCalledAfterContextReleasedError);
|
|
|
|
return;
|
|
|
|
}
|
2020-03-12 01:07:54 +00:00
|
|
|
blink::TransferableMessage transferable_message;
|
|
|
|
if (!electron::SerializeV8Value(isolate, message_value,
|
|
|
|
&transferable_message)) {
|
|
|
|
// SerializeV8Value sets an exception.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<v8::Local<v8::Object>> transferables;
|
2022-01-13 13:52:48 +00:00
|
|
|
if (transfer && !transfer.value()->IsUndefined()) {
|
2020-03-12 01:07:54 +00:00
|
|
|
if (!gin::ConvertFromV8(isolate, *transfer, &transferables)) {
|
|
|
|
thrower.ThrowTypeError("Invalid value for transfer");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<blink::MessagePortChannel> ports;
|
|
|
|
for (auto& transferable : transferables) {
|
2024-01-10 22:23:35 +00:00
|
|
|
std::optional<blink::MessagePortChannel> port =
|
2020-03-12 01:07:54 +00:00
|
|
|
blink::WebMessagePortConverter::
|
|
|
|
DisentangleAndExtractMessagePortChannel(isolate, transferable);
|
|
|
|
if (!port.has_value()) {
|
|
|
|
thrower.ThrowTypeError("Invalid value for transfer");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ports.emplace_back(port.value());
|
|
|
|
}
|
|
|
|
|
|
|
|
transferable_message.ports = std::move(ports);
|
2022-03-19 02:50:05 +00:00
|
|
|
electron_ipc_remote_->ReceivePostMessage(channel,
|
|
|
|
std::move(transferable_message));
|
2020-03-12 01:07:54 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 17:59:08 +00:00
|
|
|
void SendToHost(v8::Isolate* isolate,
|
2020-06-04 23:25:25 +00:00
|
|
|
gin_helper::ErrorThrower thrower,
|
2019-10-09 17:59:08 +00:00
|
|
|
const std::string& channel,
|
|
|
|
v8::Local<v8::Value> arguments) {
|
2022-03-19 02:50:05 +00:00
|
|
|
if (!electron_ipc_remote_) {
|
2020-06-04 23:25:25 +00:00
|
|
|
thrower.ThrowError(kIPCMethodCalledAfterContextReleasedError);
|
|
|
|
return;
|
|
|
|
}
|
2019-10-09 17:59:08 +00:00
|
|
|
blink::CloneableMessage message;
|
2020-03-12 01:07:54 +00:00
|
|
|
if (!electron::SerializeV8Value(isolate, arguments, &message)) {
|
2019-10-09 17:59:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-03-19 02:50:05 +00:00
|
|
|
electron_ipc_remote_->MessageHost(channel, std::move(message));
|
2019-04-03 21:22:23 +00:00
|
|
|
}
|
|
|
|
|
2020-03-12 01:07:54 +00:00
|
|
|
v8::Local<v8::Value> SendSync(v8::Isolate* isolate,
|
2020-06-04 23:25:25 +00:00
|
|
|
gin_helper::ErrorThrower thrower,
|
2020-03-12 01:07:54 +00:00
|
|
|
bool internal,
|
|
|
|
const std::string& channel,
|
|
|
|
v8::Local<v8::Value> arguments) {
|
2022-03-19 02:50:05 +00:00
|
|
|
if (!electron_ipc_remote_) {
|
2020-06-04 23:25:25 +00:00
|
|
|
thrower.ThrowError(kIPCMethodCalledAfterContextReleasedError);
|
|
|
|
return v8::Local<v8::Value>();
|
|
|
|
}
|
2019-10-09 17:59:08 +00:00
|
|
|
blink::CloneableMessage message;
|
2020-03-12 01:07:54 +00:00
|
|
|
if (!electron::SerializeV8Value(isolate, arguments, &message)) {
|
|
|
|
return v8::Local<v8::Value>();
|
2019-10-09 17:59:08 +00:00
|
|
|
}
|
2019-05-31 17:25:19 +00:00
|
|
|
|
2019-10-09 17:59:08 +00:00
|
|
|
blink::CloneableMessage result;
|
2022-03-19 02:50:05 +00:00
|
|
|
electron_ipc_remote_->MessageSync(internal, channel, std::move(message),
|
|
|
|
&result);
|
2020-03-12 01:07:54 +00:00
|
|
|
return electron::DeserializeV8Value(isolate, result);
|
2019-04-02 22:38:16 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 22:51:41 +00:00
|
|
|
v8::Global<v8::Context> weak_context_;
|
2022-03-19 02:50:05 +00:00
|
|
|
mojo::AssociatedRemote<electron::mojom::ElectronApiIPC> electron_ipc_remote_;
|
2019-04-02 22:38:16 +00:00
|
|
|
};
|
2013-04-23 13:52:19 +00:00
|
|
|
|
2019-09-19 15:09:15 +00:00
|
|
|
gin::WrapperInfo IPCRenderer::kWrapperInfo = {gin::kEmbedderNativeGin};
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
void Initialize(v8::Local<v8::Object> exports,
|
|
|
|
v8::Local<v8::Value> unused,
|
|
|
|
v8::Local<v8::Context> context,
|
|
|
|
void* priv) {
|
2019-09-19 15:09:15 +00:00
|
|
|
gin::Dictionary dict(context->GetIsolate(), exports);
|
2019-04-02 22:38:16 +00:00
|
|
|
dict.Set("ipc", IPCRenderer::Create(context->GetIsolate()));
|
2013-04-23 04:18:07 +00:00
|
|
|
}
|
|
|
|
|
2018-12-14 16:01:29 +00:00
|
|
|
} // namespace
|
2013-04-23 04:18:07 +00:00
|
|
|
|
2023-02-09 01:31:38 +00:00
|
|
|
NODE_LINKED_BINDING_CONTEXT_AWARE(electron_renderer_ipc, Initialize)
|