electron/shell/renderer/api/electron_api_ipc_renderer.cc
electron-roller[bot] 2783f76f1f
chore: bump chromium to 140.0.7281.0 (38-x-y) (#47559)
* chore: bump chromium in DEPS to 139.0.7258.6

* chore: bump chromium in DEPS to 139.0.7258.5

* chore: bump chromium in DEPS to 140.0.7270.1

* chore: bump chromium in DEPS to 140.0.7271.1

* chore: bump chromium in DEPS to 140.0.7273.0

* chore: bump chromium in DEPS to 140.0.7273.1

* chore: bump chromium in DEPS to 140.0.7275.1

* chore: bump chromium in DEPS to 140.0.7275.4

* chore: bump chromium in DEPS to 140.0.7277.1

* chore: bump chromium in DEPS to 140.0.7279.1

* chore: bump chromium in DEPS to 140.0.7281.1

* chore: bump chromium in DEPS to 140.0.7283.1

* chore: bump chromium in DEPS to 140.0.7285.1

* chore: bump chromium in DEPS to 140.0.7287.1

* chore: bump chromium in DEPS to 140.0.7289.0

* chore: bump chromium in DEPS to 140.0.7289.1

* chore: bump chromium in DEPS to 140.0.7291.1

* chore: bump chromium in DEPS to 140.0.7293.1

* chore: bump chromium in DEPS to 140.0.7295.1

* chore: bump chromium in DEPS to 140.0.7296.0

* chore: bump chromium to 140.0.7281.0 (main) (#47616)

cherry picked from 603cafad7e

* chore: bump chromium in DEPS to 140.0.7269.2

* chore: bump chromium in DEPS to 140.0.7270.0

* chore: bump chromium in DEPS to 140.0.7271.0

* chore: bump chromium in DEPS to 140.0.7273.0

* 6516731: [ExclusiveAccessForAndroid] remove unneeded includes & deps | https://chromium-review.googlesource.com/c/chromium/src/+/6516731

* 6694809: dbus: Ensure systemd scope is started before using any portal services | https://chromium-review.googlesource.com/c/chromium/src/+/6694809

* chore: patch chromium

* chore: export patches

* chore: bump chromium in DEPS to 140.0.7275.0

* 6677511: [pepper] More pepper removal | https://chromium-review.googlesource.com/c/chromium/src/+/6677511

* 6513641: [gin] Rename gin::Wrappable to gin::DeprecatedWrappable | https://chromium-review.googlesource.com/c/chromium/src/+/6513641

* chore: export chromium patches

* 6513641: [gin] Rename gin::Wrappable to gin::DeprecatedWrappable | https://chromium-review.googlesource.com/c/chromium/src/+/6513641

* chore: bump chromium in DEPS to 140.0.7277.0

* chore: bump chromium in DEPS to 140.0.7279.0

* chore: bump chromium in DEPS to 140.0.7281.0

* 6677314: Plumb enabled client hints in the network requestion to network layer

https://chromium-review.googlesource.com/c/chromium/src/+/6677314

* 6351556: [source-phase-imports] Support Wasm Source Phase Imports

https://chromium-review.googlesource.com/c/chromium/src/+/6351556

* 6700077: [renderer] Avoid calls to deprecated GetIsolate methods

https://chromium-review.googlesource.com/c/chromium/src/+/6700077

* 6692873: Reland "Reland "FSA: Only normalize the hardcoded rules once during initialization""

https://chromium-review.googlesource.com/c/chromium/src/+/6692873

* 6686234: [gin] Cleanup NamedPropertyInterceptor for Wrappable

https://chromium-review.googlesource.com/c/chromium/src/+/6686234

* chore: export patches

* 6667723: Remove content_enable_legacy_ipc GN arg.

https://chromium-review.googlesource.com/c/chromium/src/+/6667723

* 6646566: ui: Move NativeWindowTracker to its own directory

https://chromium-review.googlesource.com/c/chromium/src/+/6646566

* fix: add missing includes

* 6580522: [WAR, DNR] Fix unsafe redirect error to web accessible resource

https://chromium-review.googlesource.com/c/chromium/src/+/6580522

* 6680477: Implement `completeCode` endpoint and expose to DevTools

https://chromium-review.googlesource.com/c/chromium/src/+/6680477

* 6677511: [pepper] More pepper removal

https://chromium-review.googlesource.com/c/chromium/src/+/6677511

* 6696689: Rename views::WidgetFocusManager -> NativeViewFocusManager

https://chromium-review.googlesource.com/c/chromium/src/+/6696689

* 6702812: Move wtf/text/string_impl*.* to "blink" namespace

https://chromium-review.googlesource.com/c/chromium/src/+/6702812

* chore: fix dialog patch

* 6702431: [animation-trigger] Parse timeline-trigger-name

https://chromium-review.googlesource.com/c/chromium/src/+/6702431

* chore: fixup patch indices

* feat: replace webFrame.routingId with webFrame.frameToken

* feat: WebFrameMain.prototype.frameToken

* test: refactor to use replacement APIs

* chore: fixup pip patch

* test: adjust webFrame tests for frameToken changes

* 6703757: Reland "Enable -fsanitize=array-bounds in non-UBSan builds"

https://chromium-review.googlesource.com/c/chromium/src/+/6703757

* test: switch to frameTokens

* test: routingId is fine to test in the main process

* docs: add routingId to breaking changes

* docs: update plugin-crashed event

* chore: fixup linux dialog patch

---------

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>
Co-authored-by: alice <alice@makenotion.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
Co-authored-by: Samuel Maddock <smaddock@slack-corp.com>
(cherry picked from commit 603cafad7e)

---------

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>
Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>
Co-authored-by: alice <alice@makenotion.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
Co-authored-by: Samuel Maddock <smaddock@slack-corp.com>
2025-07-15 12:05:29 -04:00

277 lines
9.6 KiB
C++

// Copyright (c) 2013 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include <string>
#include "content/public/renderer/render_frame.h"
#include "content/public/renderer/render_frame_observer.h"
#include "content/public/renderer/worker_thread.h"
#include "gin/dictionary.h"
#include "gin/handle.h"
#include "gin/object_template_builder.h"
#include "gin/wrappable.h"
#include "services/service_manager/public/cpp/interface_provider.h"
#include "shell/common/api/api.mojom.h"
#include "shell/common/gin_converters/blink_converter.h"
#include "shell/common/gin_converters/value_converter.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/gin_helper/error_thrower.h"
#include "shell/common/gin_helper/function_template_extensions.h"
#include "shell/common/gin_helper/promise.h"
#include "shell/common/node_bindings.h"
#include "shell/common/node_includes.h"
#include "shell/common/v8_util.h"
#include "shell/renderer/preload_realm_context.h"
#include "shell/renderer/service_worker_data.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
#include "third_party/blink/public/web/modules/service_worker/web_service_worker_context_proxy.h"
#include "third_party/blink/public/web/web_local_frame.h"
#include "third_party/blink/public/web/web_message_port_converter.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h" // nogncheck
using blink::WebLocalFrame;
using content::RenderFrame;
namespace {
const char kIPCMethodCalledAfterContextReleasedError[] =
"IPC method called after context was released";
RenderFrame* GetCurrentRenderFrame() {
WebLocalFrame* frame = WebLocalFrame::FrameForCurrentContext();
if (!frame)
return nullptr;
return RenderFrame::FromWebFrame(frame);
}
// Thread identifier for the main renderer thread (as opposed to a service
// worker thread).
inline constexpr int kMainThreadId = 0;
bool IsWorkerThread() {
return content::WorkerThread::GetCurrentId() != kMainThreadId;
}
template <typename T>
class IPCBase : public gin::DeprecatedWrappable<T> {
public:
static gin::DeprecatedWrapperInfo kWrapperInfo;
static gin::Handle<T> Create(v8::Isolate* isolate) {
return gin::CreateHandle(isolate, new T(isolate));
}
void SendMessage(v8::Isolate* isolate,
gin_helper::ErrorThrower thrower,
bool internal,
const std::string& channel,
v8::Local<v8::Value> arguments) {
if (!electron_ipc_remote_) {
thrower.ThrowError(kIPCMethodCalledAfterContextReleasedError);
return;
}
blink::CloneableMessage message;
if (!electron::SerializeV8Value(isolate, arguments, &message)) {
return;
}
electron_ipc_remote_->Message(internal, channel, std::move(message));
}
v8::Local<v8::Promise> Invoke(v8::Isolate* isolate,
gin_helper::ErrorThrower thrower,
bool internal,
const std::string& channel,
v8::Local<v8::Value> arguments) {
if (!electron_ipc_remote_) {
thrower.ThrowError(kIPCMethodCalledAfterContextReleasedError);
return {};
}
blink::CloneableMessage message;
if (!electron::SerializeV8Value(isolate, arguments, &message)) {
return {};
}
gin_helper::Promise<blink::CloneableMessage> p(isolate);
auto handle = p.GetHandle();
electron_ipc_remote_->Invoke(
internal, channel, std::move(message),
base::BindOnce(
[](gin_helper::Promise<blink::CloneableMessage> p,
blink::CloneableMessage result) { p.Resolve(result); },
std::move(p)));
return handle;
}
void PostMessage(v8::Isolate* isolate,
gin_helper::ErrorThrower thrower,
const std::string& channel,
v8::Local<v8::Value> message_value,
std::optional<v8::Local<v8::Value>> transfer) {
if (!electron_ipc_remote_) {
thrower.ThrowError(kIPCMethodCalledAfterContextReleasedError);
return;
}
blink::TransferableMessage transferable_message;
if (!electron::SerializeV8Value(isolate, message_value,
&transferable_message)) {
// SerializeV8Value sets an exception.
return;
}
std::vector<v8::Local<v8::Object>> transferables;
if (transfer && !transfer.value()->IsUndefined()) {
if (!gin::ConvertFromV8(isolate, *transfer, &transferables)) {
thrower.ThrowTypeError("Invalid value for transfer");
return;
}
}
std::vector<blink::MessagePortChannel> ports;
for (auto& transferable : transferables) {
std::optional<blink::MessagePortChannel> port =
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);
electron_ipc_remote_->ReceivePostMessage(channel,
std::move(transferable_message));
}
void SendToHost(v8::Isolate* isolate,
gin_helper::ErrorThrower thrower,
const std::string& channel,
v8::Local<v8::Value> arguments) {
if (!electron_ipc_remote_) {
thrower.ThrowError(kIPCMethodCalledAfterContextReleasedError);
return;
}
blink::CloneableMessage message;
if (!electron::SerializeV8Value(isolate, arguments, &message)) {
return;
}
electron_ipc_remote_->MessageHost(channel, std::move(message));
}
v8::Local<v8::Value> SendSync(v8::Isolate* isolate,
gin_helper::ErrorThrower thrower,
bool internal,
const std::string& channel,
v8::Local<v8::Value> arguments) {
if (!electron_ipc_remote_) {
thrower.ThrowError(kIPCMethodCalledAfterContextReleasedError);
return {};
}
blink::CloneableMessage message;
if (!electron::SerializeV8Value(isolate, arguments, &message)) {
return {};
}
blink::CloneableMessage result;
electron_ipc_remote_->MessageSync(internal, channel, std::move(message),
&result);
return electron::DeserializeV8Value(isolate, result);
}
// gin::Wrappable:
gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
v8::Isolate* isolate) override {
return gin::DeprecatedWrappable<T>::GetObjectTemplateBuilder(isolate)
.SetMethod("send", &T::SendMessage)
.SetMethod("sendSync", &T::SendSync)
.SetMethod("sendToHost", &T::SendToHost)
.SetMethod("invoke", &T::Invoke)
.SetMethod("postMessage", &T::PostMessage);
}
protected:
mojo::AssociatedRemote<electron::mojom::ElectronApiIPC> electron_ipc_remote_;
};
class IPCRenderFrame : public IPCBase<IPCRenderFrame>,
private content::RenderFrameObserver {
public:
explicit IPCRenderFrame(v8::Isolate* isolate)
: content::RenderFrameObserver(GetCurrentRenderFrame()) {
v8::Local<v8::Context> context = isolate->GetCurrentContext();
blink::ExecutionContext* execution_context =
blink::ExecutionContext::From(context);
if (execution_context->IsWindow()) {
RenderFrame* render_frame = GetCurrentRenderFrame();
DCHECK(render_frame);
render_frame->GetRemoteAssociatedInterfaces()->GetInterface(
&electron_ipc_remote_);
} else {
NOTREACHED();
}
weak_context_ =
v8::Global<v8::Context>(isolate, isolate->GetCurrentContext());
weak_context_.SetWeak();
}
void OnDestruct() override { electron_ipc_remote_.reset(); }
void WillReleaseScriptContext(v8::Local<v8::Context> context,
int32_t world_id) override {
if (weak_context_.IsEmpty() ||
weak_context_.Get(context->GetIsolate()) == context) {
OnDestruct();
}
}
const char* GetTypeName() override { return "IPCRenderFrame"; }
private:
v8::Global<v8::Context> weak_context_;
};
template <>
gin::DeprecatedWrapperInfo IPCBase<IPCRenderFrame>::kWrapperInfo = {
gin::kEmbedderNativeGin};
class IPCServiceWorker : public IPCBase<IPCServiceWorker>,
public content::WorkerThread::Observer {
public:
explicit IPCServiceWorker(v8::Isolate* isolate) {
DCHECK(IsWorkerThread());
content::WorkerThread::AddObserver(this);
electron::ServiceWorkerData* service_worker_data =
electron::preload_realm::GetServiceWorkerData(
isolate->GetCurrentContext());
DCHECK(service_worker_data);
service_worker_data->proxy()->GetRemoteAssociatedInterface(
electron_ipc_remote_.BindNewEndpointAndPassReceiver());
}
void WillStopCurrentWorkerThread() override { electron_ipc_remote_.reset(); }
const char* GetTypeName() override { return "IPCServiceWorker"; }
};
template <>
gin::DeprecatedWrapperInfo IPCBase<IPCServiceWorker>::kWrapperInfo = {
gin::kEmbedderNativeGin};
void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context,
void* priv) {
gin_helper::Dictionary dict(context->GetIsolate(), exports);
dict.SetMethod("createForRenderFrame", &IPCRenderFrame::Create);
dict.SetMethod("createForServiceWorker", &IPCServiceWorker::Create);
}
} // namespace
NODE_LINKED_BINDING_CONTEXT_AWARE(electron_renderer_ipc, Initialize)