feat: service worker preload scripts for improved extensions support (#44411)

* feat: preload scripts for service workers

* feat: service worker IPC

* test: service worker preload scripts and ipc
This commit is contained in:
Sam Maddock 2025-01-31 09:32:45 -05:00 committed by GitHub
parent bc22ee7897
commit 26da3c5d6e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
67 changed files with 2103 additions and 298 deletions

View file

@ -1762,6 +1762,12 @@ gin::Handle<Session> Session::CreateFrom(
// to use partition strings, instead of using the Session object directly.
handle->Pin(isolate);
v8::TryCatch try_catch(isolate);
gin_helper::CallMethod(isolate, handle.get(), "_init");
if (try_catch.HasCaught()) {
node::errors::TriggerUncaughtException(isolate, try_catch);
}
App::Get()->EmitWithoutEvent("session-created", handle);
return handle;

View file

@ -18,6 +18,7 @@
#include "gin/wrappable.h"
#include "services/network/public/mojom/host_resolver.mojom-forward.h"
#include "services/network/public/mojom/ssl_config.mojom-forward.h"
#include "shell/browser/api/ipc_dispatcher.h"
#include "shell/browser/event_emitter_mixin.h"
#include "shell/browser/net/resolve_proxy_helper.h"
#include "shell/common/gin_helper/cleaned_up_at_exit.h"
@ -66,6 +67,7 @@ class Session final : public gin::Wrappable<Session>,
public gin_helper::Constructible<Session>,
public gin_helper::EventEmitterMixin<Session>,
public gin_helper::CleanedUpAtExit,
public IpcDispatcher<Session>,
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
private SpellcheckHunspellDictionary::Observer,
#endif

View file

@ -132,6 +132,7 @@
#include "shell/common/gin_helper/locker.h"
#include "shell/common/gin_helper/object_template_builder.h"
#include "shell/common/gin_helper/promise.h"
#include "shell/common/gin_helper/reply_channel.h"
#include "shell/common/language_util.h"
#include "shell/common/node_includes.h"
#include "shell/common/node_util.h"
@ -1982,66 +1983,6 @@ void WebContents::OnFirstNonEmptyLayout(
}
}
namespace {
// This object wraps the InvokeCallback so that if it gets GC'd by V8, we can
// still call the callback and send an error. Not doing so causes a Mojo DCHECK,
// since Mojo requires callbacks to be called before they are destroyed.
class ReplyChannel final : public gin::Wrappable<ReplyChannel> {
public:
using InvokeCallback = electron::mojom::ElectronApiIPC::InvokeCallback;
static gin::Handle<ReplyChannel> Create(v8::Isolate* isolate,
InvokeCallback callback) {
return gin::CreateHandle(isolate, new ReplyChannel(std::move(callback)));
}
// gin::Wrappable
static gin::WrapperInfo kWrapperInfo;
gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
v8::Isolate* isolate) override {
return gin::Wrappable<ReplyChannel>::GetObjectTemplateBuilder(isolate)
.SetMethod("sendReply", &ReplyChannel::SendReply);
}
const char* GetTypeName() override { return "ReplyChannel"; }
void SendError(const std::string& msg) {
v8::Isolate* isolate = electron::JavascriptEnvironment::GetIsolate();
// If there's no current context, it means we're shutting down, so we
// don't need to send an event.
if (!isolate->GetCurrentContext().IsEmpty()) {
v8::HandleScope scope(isolate);
auto message = gin::DataObjectBuilder(isolate).Set("error", msg).Build();
SendReply(isolate, message);
}
}
private:
explicit ReplyChannel(InvokeCallback callback)
: callback_(std::move(callback)) {}
~ReplyChannel() override {
if (callback_)
SendError("reply was never sent");
}
bool SendReply(v8::Isolate* isolate, v8::Local<v8::Value> arg) {
if (!callback_)
return false;
blink::CloneableMessage message;
if (!gin::ConvertFromV8(isolate, arg, &message)) {
return false;
}
std::move(callback_).Run(std::move(message));
return true;
}
InvokeCallback callback_;
};
gin::WrapperInfo ReplyChannel::kWrapperInfo = {gin::kEmbedderNativeGin};
} // namespace
gin::Handle<gin_helper::internal::Event> WebContents::MakeEventWithSender(
v8::Isolate* isolate,
content::RenderFrameHost* frame,
@ -2050,7 +1991,7 @@ gin::Handle<gin_helper::internal::Event> WebContents::MakeEventWithSender(
if (!GetWrapper(isolate).ToLocal(&wrapper)) {
if (callback) {
// We must always invoke the callback if present.
ReplyChannel::Create(isolate, std::move(callback))
gin_helper::internal::ReplyChannel::Create(isolate, std::move(callback))
->SendError("WebContents was destroyed");
}
return {};
@ -2058,9 +1999,10 @@ gin::Handle<gin_helper::internal::Event> WebContents::MakeEventWithSender(
gin::Handle<gin_helper::internal::Event> event =
gin_helper::internal::Event::New(isolate);
gin_helper::Dictionary dict(isolate, event.ToV8().As<v8::Object>());
dict.Set("type", "frame");
if (callback)
dict.Set("_replyChannel",
ReplyChannel::Create(isolate, std::move(callback)));
dict.Set("_replyChannel", gin_helper::internal::ReplyChannel::Create(
isolate, std::move(callback)));
if (frame) {
dict.SetGetter("senderFrame", frame);
dict.Set("frameId", frame->GetRoutingID());

View file

@ -0,0 +1,89 @@
// Copyright (c) 2025 Salesforce, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ELECTRON_SHELL_BROWSER_API_IPC_DISPATCHER_H_
#define ELECTRON_SHELL_BROWSER_API_IPC_DISPATCHER_H_
#include <string>
#include "base/trace_event/trace_event.h"
#include "base/values.h"
#include "gin/handle.h"
#include "shell/browser/api/message_port.h"
#include "shell/browser/javascript_environment.h"
#include "shell/common/api/api.mojom.h"
#include "shell/common/gin_converters/blink_converter.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/gin_helper/event.h"
#include "shell/common/gin_helper/reply_channel.h"
#include "shell/common/v8_util.h"
namespace electron {
// Handles dispatching IPCs to JS.
// See ipc-dispatch.ts for JS listeners.
template <typename T>
class IpcDispatcher {
public:
void Message(gin::Handle<gin_helper::internal::Event>& event,
const std::string& channel,
blink::CloneableMessage args) {
TRACE_EVENT1("electron", "IpcDispatcher::Message", "channel", channel);
emitter()->EmitWithoutEvent("-ipc-message", event, channel, args);
}
void Invoke(gin::Handle<gin_helper::internal::Event>& event,
const std::string& channel,
blink::CloneableMessage arguments,
electron::mojom::ElectronApiIPC::InvokeCallback callback) {
TRACE_EVENT1("electron", "IpcHelper::Invoke", "channel", channel);
v8::Isolate* isolate = electron::JavascriptEnvironment::GetIsolate();
gin_helper::Dictionary dict(isolate, event.ToV8().As<v8::Object>());
dict.Set("_replyChannel", gin_helper::internal::ReplyChannel::Create(
isolate, std::move(callback)));
emitter()->EmitWithoutEvent("-ipc-invoke", event, channel,
std::move(arguments));
}
void ReceivePostMessage(gin::Handle<gin_helper::internal::Event>& event,
const std::string& channel,
blink::TransferableMessage message) {
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
v8::HandleScope handle_scope(isolate);
auto wrapped_ports =
MessagePort::EntanglePorts(isolate, std::move(message.ports));
v8::Local<v8::Value> message_value =
electron::DeserializeV8Value(isolate, message);
emitter()->EmitWithoutEvent("-ipc-ports", event, channel, message_value,
std::move(wrapped_ports));
}
void MessageSync(
gin::Handle<gin_helper::internal::Event>& event,
const std::string& channel,
blink::CloneableMessage arguments,
electron::mojom::ElectronApiIPC::MessageSyncCallback callback) {
TRACE_EVENT1("electron", "IpcHelper::MessageSync", "channel", channel);
v8::Isolate* isolate = electron::JavascriptEnvironment::GetIsolate();
gin_helper::Dictionary dict(isolate, event.ToV8().As<v8::Object>());
dict.Set("_replyChannel", gin_helper::internal::ReplyChannel::Create(
isolate, std::move(callback)));
emitter()->EmitWithoutEvent("-ipc-message-sync", event, channel,
std::move(arguments));
}
private:
inline T* emitter() {
// T must inherit from gin_helper::EventEmitterMixin<T>
return static_cast<T*>(this);
}
};
} // namespace electron
#endif // ELECTRON_SHELL_BROWSER_API_IPC_DISPATCHER_H_

View file

@ -0,0 +1,199 @@
// Copyright (c) 2025 Salesforce, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/browser/electron_api_sw_ipc_handler_impl.h"
#include <utility>
#include "base/containers/unique_ptr_adapters.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "shell/browser/api/electron_api_session.h"
#include "shell/browser/electron_browser_context.h"
#include "shell/browser/javascript_environment.h"
#include "shell/common/gin_helper/dictionary.h"
namespace electron {
namespace {
const void* const kUserDataKey = &kUserDataKey;
class ServiceWorkerIPCList : public base::SupportsUserData::Data {
public:
std::vector<std::unique_ptr<ElectronApiSWIPCHandlerImpl>> list;
static ServiceWorkerIPCList* Get(
content::RenderProcessHost* render_process_host,
bool create_if_not_exists) {
auto* service_worker_ipc_list = static_cast<ServiceWorkerIPCList*>(
render_process_host->GetUserData(kUserDataKey));
if (!service_worker_ipc_list && !create_if_not_exists) {
return nullptr;
}
if (!service_worker_ipc_list) {
auto new_ipc_list = std::make_unique<ServiceWorkerIPCList>();
service_worker_ipc_list = new_ipc_list.get();
render_process_host->SetUserData(kUserDataKey, std::move(new_ipc_list));
}
return service_worker_ipc_list;
}
};
} // namespace
ElectronApiSWIPCHandlerImpl::ElectronApiSWIPCHandlerImpl(
content::RenderProcessHost* render_process_host,
int64_t version_id,
mojo::PendingAssociatedReceiver<mojom::ElectronApiIPC> receiver)
: render_process_host_(render_process_host), version_id_(version_id) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
receiver_.Bind(std::move(receiver));
receiver_.set_disconnect_handler(
base::BindOnce(&ElectronApiSWIPCHandlerImpl::RemoteDisconnected,
base::Unretained(this)));
render_process_host_->AddObserver(this);
}
ElectronApiSWIPCHandlerImpl::~ElectronApiSWIPCHandlerImpl() {
render_process_host_->RemoveObserver(this);
}
void ElectronApiSWIPCHandlerImpl::RemoteDisconnected() {
receiver_.reset();
Destroy();
}
void ElectronApiSWIPCHandlerImpl::Message(bool internal,
const std::string& channel,
blink::CloneableMessage arguments) {
auto* session = GetSession();
if (session) {
v8::Isolate* isolate = electron::JavascriptEnvironment::GetIsolate();
v8::HandleScope handle_scope(isolate);
gin::Handle<gin_helper::internal::Event> event =
MakeIPCEvent(isolate, internal);
session->Message(event, channel, std::move(arguments));
}
}
void ElectronApiSWIPCHandlerImpl::Invoke(bool internal,
const std::string& channel,
blink::CloneableMessage arguments,
InvokeCallback callback) {
auto* session = GetSession();
if (session) {
v8::Isolate* isolate = electron::JavascriptEnvironment::GetIsolate();
v8::HandleScope handle_scope(isolate);
gin::Handle<gin_helper::internal::Event> event =
MakeIPCEvent(isolate, internal);
session->Invoke(event, channel, std::move(arguments), std::move(callback));
}
}
void ElectronApiSWIPCHandlerImpl::ReceivePostMessage(
const std::string& channel,
blink::TransferableMessage message) {
auto* session = GetSession();
if (session) {
v8::Isolate* isolate = electron::JavascriptEnvironment::GetIsolate();
v8::HandleScope handle_scope(isolate);
gin::Handle<gin_helper::internal::Event> event =
MakeIPCEvent(isolate, false);
session->ReceivePostMessage(event, channel, std::move(message));
}
}
void ElectronApiSWIPCHandlerImpl::MessageSync(bool internal,
const std::string& channel,
blink::CloneableMessage arguments,
MessageSyncCallback callback) {
auto* session = GetSession();
if (session) {
v8::Isolate* isolate = electron::JavascriptEnvironment::GetIsolate();
v8::HandleScope handle_scope(isolate);
gin::Handle<gin_helper::internal::Event> event =
MakeIPCEvent(isolate, internal);
session->MessageSync(event, channel, std::move(arguments),
std::move(callback));
}
}
void ElectronApiSWIPCHandlerImpl::MessageHost(
const std::string& channel,
blink::CloneableMessage arguments) {
NOTIMPLEMENTED(); // Service workers have no <webview>
}
ElectronBrowserContext* ElectronApiSWIPCHandlerImpl::GetBrowserContext() {
auto* browser_context = static_cast<ElectronBrowserContext*>(
render_process_host_->GetBrowserContext());
return browser_context;
}
api::Session* ElectronApiSWIPCHandlerImpl::GetSession() {
return api::Session::FromBrowserContext(GetBrowserContext());
}
gin::Handle<gin_helper::internal::Event>
ElectronApiSWIPCHandlerImpl::MakeIPCEvent(v8::Isolate* isolate, bool internal) {
gin::Handle<gin_helper::internal::Event> event =
gin_helper::internal::Event::New(isolate);
v8::Local<v8::Object> event_object = event.ToV8().As<v8::Object>();
gin_helper::Dictionary dict(isolate, event_object);
dict.Set("type", "service-worker");
dict.Set("versionId", version_id_);
dict.Set("processId", render_process_host_->GetID().GetUnsafeValue());
// Set session to provide context for getting preloads
dict.Set("session", GetSession());
if (internal)
dict.SetHidden("internal", internal);
return event;
}
void ElectronApiSWIPCHandlerImpl::Destroy() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
auto* service_worker_ipc_list = ServiceWorkerIPCList::Get(
render_process_host_, /*create_if_not_exists=*/false);
CHECK(service_worker_ipc_list);
// std::erase_if will lead to a call to the destructor for this object.
std::erase_if(service_worker_ipc_list->list, base::MatchesUniquePtr(this));
}
void ElectronApiSWIPCHandlerImpl::RenderProcessExited(
content::RenderProcessHost* host,
const content::ChildProcessTerminationInfo& info) {
CHECK_EQ(host, render_process_host_);
// TODO(crbug.com/1407197): Investigate clearing the user data from
// RenderProcessHostImpl::Cleanup.
Destroy();
// This instance has now been deleted.
}
// static
void ElectronApiSWIPCHandlerImpl::BindReceiver(
int render_process_id,
int64_t version_id,
mojo::PendingAssociatedReceiver<mojom::ElectronApiIPC> receiver) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
auto* render_process_host =
content::RenderProcessHost::FromID(render_process_id);
if (!render_process_host) {
return;
}
auto* service_worker_ipc_list = ServiceWorkerIPCList::Get(
render_process_host, /*create_if_not_exists=*/true);
service_worker_ipc_list->list.push_back(
std::make_unique<ElectronApiSWIPCHandlerImpl>(
render_process_host, version_id, std::move(receiver)));
}
} // namespace electron

View file

@ -0,0 +1,98 @@
// Copyright (c) 2025 Salesforce, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ELECTRON_SHELL_BROWSER_ELECTRON_API_SW_IPC_HANDLER_IMPL_H_
#define ELECTRON_SHELL_BROWSER_ELECTRON_API_SW_IPC_HANDLER_IMPL_H_
#include <string>
#include "base/memory/weak_ptr.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host_observer.h"
#include "electron/shell/common/api/api.mojom.h"
#include "gin/handle.h"
#include "mojo/public/cpp/bindings/associated_receiver.h"
#include "shell/common/gin_helper/event.h"
namespace content {
class RenderProcessHost;
}
namespace electron {
class ElectronBrowserContext;
namespace api {
class Session;
}
class ElectronApiSWIPCHandlerImpl : public mojom::ElectronApiIPC,
public content::RenderProcessHostObserver {
public:
explicit ElectronApiSWIPCHandlerImpl(
content::RenderProcessHost* render_process_host,
int64_t version_id,
mojo::PendingAssociatedReceiver<mojom::ElectronApiIPC> receiver);
static void BindReceiver(
int render_process_id,
int64_t version_id,
mojo::PendingAssociatedReceiver<mojom::ElectronApiIPC> receiver);
// disable copy
ElectronApiSWIPCHandlerImpl(const ElectronApiSWIPCHandlerImpl&) = delete;
ElectronApiSWIPCHandlerImpl& operator=(const ElectronApiSWIPCHandlerImpl&) =
delete;
~ElectronApiSWIPCHandlerImpl() override;
// mojom::ElectronApiIPC:
void Message(bool internal,
const std::string& channel,
blink::CloneableMessage arguments) override;
void Invoke(bool internal,
const std::string& channel,
blink::CloneableMessage arguments,
InvokeCallback callback) override;
void ReceivePostMessage(const std::string& channel,
blink::TransferableMessage message) override;
void MessageSync(bool internal,
const std::string& channel,
blink::CloneableMessage arguments,
MessageSyncCallback callback) override;
void MessageHost(const std::string& channel,
blink::CloneableMessage arguments) override;
base::WeakPtr<ElectronApiSWIPCHandlerImpl> GetWeakPtr() {
return weak_factory_.GetWeakPtr();
}
private:
ElectronBrowserContext* GetBrowserContext();
api::Session* GetSession();
gin::Handle<gin_helper::internal::Event> MakeIPCEvent(v8::Isolate* isolate,
bool internal);
// content::RenderProcessHostObserver
void RenderProcessExited(
content::RenderProcessHost* host,
const content::ChildProcessTerminationInfo& info) override;
void RemoteDisconnected();
// Destroys this instance by removing it from the ServiceWorkerIPCList.
void Destroy();
// This is safe because ElectronApiSWIPCHandlerImpl is tied to the life time
// of RenderProcessHost.
const raw_ptr<content::RenderProcessHost> render_process_host_;
// Service worker version ID.
int64_t version_id_;
mojo::AssociatedReceiver<mojom::ElectronApiIPC> receiver_{this};
base::WeakPtrFactory<ElectronApiSWIPCHandlerImpl> weak_factory_{this};
};
} // namespace electron
#endif // ELECTRON_SHELL_BROWSER_ELECTRON_API_SW_IPC_HANDLER_IMPL_H_

View file

@ -79,6 +79,7 @@
#include "shell/browser/bluetooth/electron_bluetooth_delegate.h"
#include "shell/browser/child_web_contents_tracker.h"
#include "shell/browser/electron_api_ipc_handler_impl.h"
#include "shell/browser/electron_api_sw_ipc_handler_impl.h"
#include "shell/browser/electron_autofill_driver_factory.h"
#include "shell/browser/electron_browser_context.h"
#include "shell/browser/electron_browser_main_parts.h"
@ -581,6 +582,18 @@ void ElectronBrowserClient::AppendExtraCommandLineSwitches(
web_preferences->AppendCommandLineSwitches(
command_line, IsRendererSubFrame(unsafe_process_id));
}
// Service worker processes should only run preloads if one has been
// registered prior to startup.
auto* render_process_host = content::RenderProcessHost::FromID(process_id);
if (render_process_host) {
auto* browser_context = render_process_host->GetBrowserContext();
auto* session_prefs =
SessionPreferences::FromBrowserContext(browser_context);
if (session_prefs->HasServiceWorkerPreloadScript()) {
command_line->AppendSwitch(switches::kServiceWorkerPreload);
}
}
}
}
@ -1409,6 +1422,13 @@ void ElectronBrowserClient::OverrideURLLoaderFactoryParams(
void ElectronBrowserClient::RegisterAssociatedInterfaceBindersForServiceWorker(
const content::ServiceWorkerVersionBaseInfo& service_worker_version_info,
blink::AssociatedInterfaceRegistry& associated_registry) {
CHECK(service_worker_version_info.process_id !=
content::ChildProcessHost::kInvalidUniqueID);
associated_registry.AddInterface<mojom::ElectronApiIPC>(
base::BindRepeating(&ElectronApiSWIPCHandlerImpl::BindReceiver,
service_worker_version_info.process_id,
service_worker_version_info.version_id));
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
associated_registry.AddInterface<extensions::mojom::RendererHost>(
base::BindRepeating(&extensions::RendererStartupHelper::BindForRenderer,

View file

@ -30,4 +30,13 @@ SessionPreferences* SessionPreferences::FromBrowserContext(
return static_cast<SessionPreferences*>(context->GetUserData(&kLocatorKey));
}
bool SessionPreferences::HasServiceWorkerPreloadScript() {
const auto& preloads = preload_scripts();
auto it = std::find_if(
preloads.begin(), preloads.end(), [](const PreloadScript& script) {
return script.script_type == PreloadScript::ScriptType::kServiceWorker;
});
return it != preloads.end();
}
} // namespace electron

View file

@ -28,6 +28,8 @@ class SessionPreferences : public base::SupportsUserData::Data {
std::vector<PreloadScript>& preload_scripts() { return preload_scripts_; }
bool HasServiceWorkerPreloadScript();
private:
SessionPreferences();