Add direct script to embedder channel (#39039)

This PR **removes** `ScriptToConstellationMessage::ForwardToEmbedder`,
and replaces it with an explicit `ScriptToEmbedderChannel`. This new
channel is based on `GenericCallback` and in single-process mode will
directly send the message the to the embedder and wake it. In
multi-process mode, the message is routed via the ROUTER, since waking
is only possible from the same process currently. This means in
multi-process mode there are likely no direct perf benefits, since we
still need to hop the message over the ROUTER (instead of over the
constellation).
In single-process mode we can directly send the message to the embedder,
which should provide a noticable latency improvement in all cases where
script is blocked waiting on the embedder to reply.

This does not change the way the embedder receives messages - the
receiving end is unchanged.

## How was sending messages to the embedder working before?

1. Script wraps it's message to the embedder in
`ScriptToConstellationMessage::ForwardToEmbedder` and sends it to
constellation.
2. The [constellation event loop] receives the message in
[handle_request]
3. If deserialization fails, [an error is logged and the message is
ignored]
4. Since our message came from script, it is handle in
[handle_request_from_script]
5. The message is logged with trace log level
6. If the pipeline is closed, [a warning is logged and the message
ignored]
7. The wrapped `EmbedderMsg` [is forwarded to the embedder]. Sending the
message also invokes `wake()` on the embedder eventloop waker.

[constellation event loop]:
2e1b2e7260/components/constellation/constellation.rs (L755)

[handle request]:
2e1b2e7260/components/constellation/constellation.rs (L1182)

[an error is logged and the message is ignored]:
2e1b2e7260/components/constellation/constellation.rs (L1252)

[handle_request_from_script]:
https://github.com/servo/servo/blob/main/components/constellation/constellation.rs#L1590
 
[a warning is logged and the message ignored]:
2e1b2e7260/components/constellation/constellation.rs (L1599)

[is forwarded to the embedder]:
2e1b2e7260/components/constellation/constellation.rs (L1701)

Testing: Communication between Script and Embedder is extensive, so this
should be covered by existing tests.

Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
This commit is contained in:
Jonathan Schwender 2025-09-02 08:33:44 +02:00 committed by GitHub
parent 97c8c83cbb
commit f4dd2960b8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 120 additions and 113 deletions

View file

@ -3,8 +3,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use base::id::WebViewId;
use constellation_traits::{ScriptToConstellationChan, ScriptToConstellationMessage};
use embedder_traits::EmbedderMsg;
use embedder_traits::{EmbedderMsg, ScriptToEmbedderChan};
use ipc_channel::ipc::channel;
use malloc_size_of_derive::MallocSizeOf;
@ -19,25 +18,21 @@ pub trait ClipboardProvider {
#[derive(MallocSizeOf)]
pub(crate) struct EmbedderClipboardProvider {
pub constellation_sender: ScriptToConstellationChan,
pub embedder_sender: ScriptToEmbedderChan,
pub webview_id: WebViewId,
}
impl ClipboardProvider for EmbedderClipboardProvider {
fn get_text(&mut self) -> Result<String, String> {
let (tx, rx) = channel().unwrap();
self.constellation_sender
.send(ScriptToConstellationMessage::ForwardToEmbedder(
EmbedderMsg::GetClipboardText(self.webview_id, tx),
))
self.embedder_sender
.send(EmbedderMsg::GetClipboardText(self.webview_id, tx))
.unwrap();
rx.recv().unwrap()
}
fn set_text(&mut self, s: String) {
self.constellation_sender
.send(ScriptToConstellationMessage::ForwardToEmbedder(
EmbedderMsg::SetClipboardText(self.webview_id, s),
))
self.embedder_sender
.send(EmbedderMsg::SetClipboardText(self.webview_id, s))
.unwrap();
}
}

View file

@ -8,8 +8,8 @@ use base::id::{Index, PipelineId, PipelineNamespaceId};
use constellation_traits::ScriptToConstellationChan;
use devtools_traits::{DevtoolScriptControlMsg, ScriptToDevtoolsControlMsg, SourceInfo, WorkerId};
use dom_struct::dom_struct;
use embedder_traits::JavaScriptEvaluationError;
use embedder_traits::resources::{self, Resource};
use embedder_traits::{JavaScriptEvaluationError, ScriptToEmbedderChan};
use ipc_channel::ipc::IpcSender;
use js::jsval::UndefinedValue;
use js::rust::wrappers::JS_DefineDebuggerObject;
@ -66,6 +66,7 @@ impl DebuggerGlobalScope {
mem_profiler_chan: mem::ProfilerChan,
time_profiler_chan: time::ProfilerChan,
script_to_constellation_chan: ScriptToConstellationChan,
script_to_embedder_chan: ScriptToEmbedderChan,
resource_threads: ResourceThreads,
#[cfg(feature = "webgpu")] gpu_id_hub: std::sync::Arc<IdentityHub>,
can_gc: CanGc,
@ -77,6 +78,7 @@ impl DebuggerGlobalScope {
mem_profiler_chan,
time_profiler_chan,
script_to_constellation_chan,
script_to_embedder_chan,
resource_threads,
MutableOrigin::new(ImmutableOrigin::new_opaque()),
ServoUrl::parse_with_base(None, "about:internal/debugger")

View file

@ -438,6 +438,7 @@ impl DedicatedWorkerGlobalScope {
init.mem_profiler_chan.clone(),
init.time_profiler_chan.clone(),
init.script_to_constellation_chan.clone(),
init.script_to_embedder_chan.clone(),
init.resource_threads.clone(),
#[cfg(feature = "webgpu")]
gpu_id_hub.clone(),

View file

@ -58,6 +58,7 @@ impl DissimilarOriginWindow {
global_to_clone_from.mem_profiler_chan().clone(),
global_to_clone_from.time_profiler_chan().clone(),
global_to_clone_from.script_to_constellation_chan().clone(),
global_to_clone_from.script_to_embedder_chan().clone(),
global_to_clone_from.resource_threads().clone(),
global_to_clone_from.origin().clone(),
global_to_clone_from.creation_url().clone(),

View file

@ -1345,10 +1345,10 @@ impl DocumentEventHandler {
},
ClipboardEventType::Paste => {
let (sender, receiver) = ipc::channel().unwrap();
self.window
.send_to_constellation(ScriptToConstellationMessage::ForwardToEmbedder(
EmbedderMsg::GetClipboardText(self.window.webview_id(), sender),
));
self.window.send_to_embedder(EmbedderMsg::GetClipboardText(
self.window.webview_id(),
sender,
));
let text_contents = receiver
.recv()
.map(Result::unwrap_or_default)

View file

@ -26,7 +26,7 @@ use content_security_policy::CspList;
use crossbeam_channel::Sender;
use devtools_traits::{PageError, ScriptToDevtoolsControlMsg};
use dom_struct::dom_struct;
use embedder_traits::{EmbedderMsg, JavaScriptEvaluationError};
use embedder_traits::{EmbedderMsg, JavaScriptEvaluationError, ScriptToEmbedderChan};
use fonts::FontContext;
use ipc_channel::ipc::{self, IpcSender};
use ipc_channel::router::ROUTER;
@ -254,6 +254,11 @@ pub(crate) struct GlobalScope {
#[no_trace]
script_to_constellation_chan: ScriptToConstellationChan,
/// A handle for communicating messages to the Embedder.
#[ignore_malloc_size_of = "channels are hard"]
#[no_trace]
script_to_embedder_chan: ScriptToEmbedderChan,
/// <https://html.spec.whatwg.org/multipage/#in-error-reporting-mode>
in_error_reporting_mode: Cell<bool>,
@ -741,6 +746,7 @@ impl GlobalScope {
mem_profiler_chan: profile_mem::ProfilerChan,
time_profiler_chan: profile_time::ProfilerChan,
script_to_constellation_chan: ScriptToConstellationChan,
script_to_embedder_chan: ScriptToEmbedderChan,
resource_threads: ResourceThreads,
origin: MutableOrigin,
creation_url: ServoUrl,
@ -770,6 +776,7 @@ impl GlobalScope {
mem_profiler_chan,
time_profiler_chan,
script_to_constellation_chan,
script_to_embedder_chan,
in_error_reporting_mode: Default::default(),
resource_threads,
timers: OnceCell::default(),
@ -2477,8 +2484,12 @@ impl GlobalScope {
&self.script_to_constellation_chan
}
pub(crate) fn script_to_embedder_chan(&self) -> &ScriptToEmbedderChan {
&self.script_to_embedder_chan
}
pub(crate) fn send_to_embedder(&self, msg: EmbedderMsg) {
self.send_to_constellation(ScriptToConstellationMessage::ForwardToEmbedder(msg));
self.script_to_embedder_chan().send(msg).unwrap();
}
pub(crate) fn send_to_constellation(&self, msg: ScriptToConstellationMessage) {

View file

@ -456,10 +456,10 @@ impl HTMLInputElement {
prefix: Option<Prefix>,
document: &Document,
) -> HTMLInputElement {
let constellation_sender = document
let embedder_sender = document
.window()
.as_global_scope()
.script_to_constellation_chan()
.script_to_embedder_chan()
.clone();
HTMLInputElement {
htmlelement: HTMLElement::new_inherited_with_state(
@ -478,7 +478,7 @@ impl HTMLInputElement {
Single,
DOMString::new(),
EmbedderClipboardProvider {
constellation_sender,
embedder_sender,
webview_id: document.webview_id(),
},
None,

View file

@ -141,10 +141,10 @@ impl HTMLTextAreaElement {
prefix: Option<Prefix>,
document: &Document,
) -> HTMLTextAreaElement {
let constellation_sender = document
let embedder_sender = document
.window()
.as_global_scope()
.script_to_constellation_chan()
.script_to_embedder_chan()
.clone();
HTMLTextAreaElement {
htmlelement: HTMLElement::new_inherited_with_state(
@ -158,7 +158,7 @@ impl HTMLTextAreaElement {
Lines::Multiple,
DOMString::new(),
EmbedderClipboardProvider {
constellation_sender,
embedder_sender,
webview_id: document.webview_id(),
},
None,

View file

@ -35,9 +35,9 @@ use devtools_traits::{ScriptToDevtoolsControlMsg, TimelineMarker, TimelineMarker
use dom_struct::dom_struct;
use embedder_traits::user_content_manager::{UserContentManager, UserScript};
use embedder_traits::{
AlertResponse, ConfirmResponse, EmbedderMsg, PromptResponse, SimpleDialog, Theme,
UntrustedNodeAddress, ViewportDetails, WebDriverJSError, WebDriverJSResult,
WebDriverLoadStatus,
AlertResponse, ConfirmResponse, EmbedderMsg, PromptResponse, ScriptToEmbedderChan,
SimpleDialog, Theme, UntrustedNodeAddress, ViewportDetails, WebDriverJSError,
WebDriverJSResult, WebDriverLoadStatus,
};
use euclid::default::{Point2D as UntypedPoint2D, Rect as UntypedRect, Size2D as UntypedSize2D};
use euclid::{Point2D, Scale, Size2D, Vector2D};
@ -2995,7 +2995,10 @@ impl Window {
}
pub(crate) fn send_to_embedder(&self, msg: EmbedderMsg) {
self.send_to_constellation(ScriptToConstellationMessage::ForwardToEmbedder(msg));
self.as_global_scope()
.script_to_embedder_chan()
.send(msg)
.unwrap();
}
pub(crate) fn send_to_constellation(&self, msg: ScriptToConstellationMessage) {
@ -3100,6 +3103,7 @@ impl Window {
time_profiler_chan: TimeProfilerChan,
devtools_chan: Option<IpcSender<ScriptToDevtoolsControlMsg>>,
constellation_chan: ScriptToConstellationChan,
embedder_chan: ScriptToEmbedderChan,
control_chan: GenericSender<ScriptThreadMessage>,
pipeline_id: PipelineId,
parent_info: Option<PipelineId>,
@ -3139,6 +3143,7 @@ impl Window {
mem_profiler_chan,
time_profiler_chan,
constellation_chan,
embedder_chan,
resource_threads,
origin,
creation_url,

View file

@ -90,6 +90,7 @@ pub(crate) fn prepare_workerscope_init(
time_profiler_chan: global.time_profiler_chan().clone(),
from_devtools_sender: devtools_sender,
script_to_constellation_chan: global.script_to_constellation_chan().clone(),
script_to_embedder_chan: global.script_to_embedder_chan().clone(),
worker_id: worker_id.unwrap_or_else(|| WorkerId(Uuid::new_v4())),
pipeline_id: global.pipeline_id(),
origin: global.origin().immutable().clone(),
@ -185,6 +186,7 @@ impl WorkerGlobalScope {
init.mem_profiler_chan,
init.time_profiler_chan,
init.script_to_constellation_chan,
init.script_to_embedder_chan,
init.resource_threads,
MutableOrigin::new(init.origin),
init.creation_url,

View file

@ -10,7 +10,7 @@ use constellation_traits::{ScriptToConstellationChan, ScriptToConstellationMessa
use crossbeam_channel::Sender;
use devtools_traits::ScriptToDevtoolsControlMsg;
use dom_struct::dom_struct;
use embedder_traits::JavaScriptEvaluationError;
use embedder_traits::{JavaScriptEvaluationError, ScriptToEmbedderChan};
use ipc_channel::ipc::IpcSender;
use js::jsval::UndefinedValue;
use net_traits::ResourceThreads;
@ -100,6 +100,7 @@ impl WorkletGlobalScope {
init.mem_profiler_chan.clone(),
init.time_profiler_chan.clone(),
script_to_constellation_chan,
init.to_embedder_sender.clone(),
init.resource_threads.clone(),
MutableOrigin::new(ImmutableOrigin::new_opaque()),
base_url.clone(),
@ -198,6 +199,8 @@ pub(crate) struct WorkletGlobalScopeInit {
pub(crate) devtools_chan: Option<IpcSender<ScriptToDevtoolsControlMsg>>,
/// Messages to send to constellation
pub(crate) to_constellation_sender: GenericSender<(PipelineId, ScriptToConstellationMessage)>,
/// Messages to send to the Embedder
pub(crate) to_embedder_sender: ScriptToEmbedderChan,
/// The image cache
pub(crate) image_cache: Arc<dyn ImageCache>,
/// Identity manager for WebGPU resources

View file

@ -16,6 +16,7 @@ use bluetooth_traits::BluetoothRequest;
use constellation_traits::ScriptToConstellationMessage;
use crossbeam_channel::{Receiver, SendError, Sender, select};
use devtools_traits::{DevtoolScriptControlMsg, ScriptToDevtoolsControlMsg};
use embedder_traits::ScriptToEmbedderChan;
use ipc_channel::ipc::IpcSender;
use net_traits::FetchResponseMsg;
use net_traits::image_cache::ImageCacheResponseMessage;
@ -344,6 +345,10 @@ pub(crate) struct ScriptThreadSenders {
pub(crate) pipeline_to_constellation_sender:
GenericSender<(PipelineId, ScriptToConstellationMessage)>,
/// A channel to send messages to the Embedder.
#[no_trace]
pub(crate) pipeline_to_embedder_sender: ScriptToEmbedderChan,
/// The shared [`IpcSender`] which is sent to the `ImageCache` when requesting an image. The
/// messages on this channel are routed to crossbeam [`Sender`] on the router thread, which
/// in turn sends messages to [`ScriptThreadReceivers::image_cache_receiver`].

View file

@ -781,6 +781,10 @@ impl ScriptThread {
.senders
.pipeline_to_constellation_sender
.clone(),
to_embedder_sender: script_thread
.senders
.pipeline_to_embedder_sender
.clone(),
image_cache,
#[cfg(feature = "webgpu")]
gpu_id_hub: script_thread.gpu_id_hub.clone(),
@ -941,6 +945,7 @@ impl ScriptThread {
bluetooth_sender: state.bluetooth_sender,
constellation_sender: state.constellation_sender,
pipeline_to_constellation_sender: state.pipeline_to_constellation_sender.sender.clone(),
pipeline_to_embedder_sender: state.pipeline_to_embedder_sender.clone(),
image_cache_sender: ipc_image_cache_sender,
time_profiler_sender: state.time_profiler_sender,
memory_profiler_sender: state.memory_profiler_sender,
@ -966,6 +971,7 @@ impl ScriptThread {
senders.memory_profiler_sender.clone(),
senders.time_profiler_sender.clone(),
script_to_constellation_chan,
senders.pipeline_to_embedder_sender.clone(),
state.resource_threads.clone(),
#[cfg(feature = "webgpu")]
gpu_id_hub.clone(),
@ -3312,6 +3318,7 @@ impl ScriptThread {
self.senders.time_profiler_sender.clone(),
self.senders.devtools_server_sender.clone(),
script_to_constellation_chan,
self.senders.pipeline_to_embedder_sender.clone(),
self.senders.constellation_sender.clone(),
incomplete.pipeline_id,
incomplete.parent_info,