1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use embedder_traits::user_contents::UserContentManagerId;
use embedder_traits::{InputEvent, MouseLeftViewportEvent, Theme};
use euclid::Point2D;
use log::warn;
use rustc_hash::FxHashMap;
use script_traits::{ConstellationInputEvent, ScriptThreadMessage};
use servo_base::Epoch;
use servo_base::id::{BrowsingContextId, PipelineId, WebViewId};
use style_traits::CSSPixel;
use crate::browsingcontext::BrowsingContext;
use crate::pipeline::Pipeline;
use crate::session_history::JointSessionHistory;
/// The `Constellation`'s view of a `WebView` in the embedding layer. This tracks all of the
/// `Constellation` state for this `WebView`.
pub(crate) struct ConstellationWebView {
/// The [`WebViewId`] of this [`ConstellationWebView`].
webview_id: WebViewId,
/// The [`PipelineId`] of the currently active pipeline at the top level of this WebView.
pub active_top_level_pipeline_id: Option<PipelineId>,
/// A counter for changes to [`Self::active_top_level_pipeline_id`].
pub active_top_level_pipeline_epoch: Epoch,
/// The currently focused browsing context in this webview for key events.
/// The focused pipeline is the current entry of the focused browsing
/// context.
pub focused_browsing_context_id: BrowsingContextId,
/// The [`BrowsingContextId`] of the currently hovered browsing context, to use for
/// knowing which frame is currently receiving cursor events.
pub hovered_browsing_context_id: Option<BrowsingContextId>,
/// The last mouse move point in the coordinate space of the Pipeline that it
/// happened int.
pub last_mouse_move_point: Point2D<f32, CSSPixel>,
/// The joint session history for this webview.
pub session_history: JointSessionHistory,
/// The [`UserContentManagerId`] for all pipelines in this `WebView`. This is `Some`
/// if the embedder has set a `UserContentManager` using the WebViewBuilder API and
/// it is `None` otherwise.
pub user_content_manager_id: Option<UserContentManagerId>,
/// The [`Theme`] that this [`ConstellationWebView`] uses. This is communicated to all
/// `ScriptThread`s so that they know how to render the contents of a particular `WebView.
theme: Theme,
/// Whether accessibility is active for this webview.
///
/// Set by [`crate::Constellation::set_accessibility_active()`], and forwarded to the
/// webview’s *active* pipelines (of those that represent documents) at any given moment
/// via [`ScriptThreadMessage::SetAccessibilityActive`] in `set_accessibility_active()`
/// and [`crate::Constellation::set_frame_tree_for_webview()`].
pub accessibility_active: bool,
}
impl ConstellationWebView {
pub(crate) fn new(
webview_id: WebViewId,
focused_browsing_context_id: BrowsingContextId,
user_content_manager_id: Option<UserContentManagerId>,
) -> Self {
Self {
webview_id,
user_content_manager_id,
active_top_level_pipeline_id: None,
active_top_level_pipeline_epoch: Epoch::default(),
focused_browsing_context_id,
hovered_browsing_context_id: None,
last_mouse_move_point: Default::default(),
session_history: JointSessionHistory::new(),
theme: Theme::Light,
accessibility_active: false,
}
}
/// Set the [`Theme`] on this [`ConstellationWebView`] returning true if the theme changed.
pub(crate) fn set_theme(&mut self, new_theme: Theme) -> bool {
let old_theme = std::mem::replace(&mut self.theme, new_theme);
old_theme != self.theme
}
/// Get the [`Theme`] of this [`ConstellationWebView`].
pub(crate) fn theme(&self) -> Theme {
self.theme
}
fn target_pipeline_id_for_input_event(
&self,
event: &ConstellationInputEvent,
browsing_contexts: &FxHashMap<BrowsingContextId, BrowsingContext>,
) -> Option<PipelineId> {
if let Some(hit_test_result) = &event.hit_test_result {
return Some(hit_test_result.pipeline_id);
}
// If there's no hit test, send the event to either the hovered or focused browsing context,
// depending on the event type.
let browsing_context_id = if matches!(event.event.event, InputEvent::MouseLeftViewport(_)) {
self.hovered_browsing_context_id
.unwrap_or(self.focused_browsing_context_id)
} else {
self.focused_browsing_context_id
};
Some(browsing_contexts.get(&browsing_context_id)?.pipeline_id)
}
/// Forward the [`InputEvent`] to this [`ConstellationWebView`]. Returns false if
/// the event could not be forwarded or true otherwise.
pub(crate) fn forward_input_event(
&mut self,
event: ConstellationInputEvent,
pipelines: &FxHashMap<PipelineId, Pipeline>,
browsing_contexts: &FxHashMap<BrowsingContextId, BrowsingContext>,
) -> bool {
let Some(pipeline_id) = self.target_pipeline_id_for_input_event(&event, browsing_contexts)
else {
warn!("Unknown pipeline for input event. Ignoring.");
return false;
};
let Some(pipeline) = pipelines.get(&pipeline_id) else {
warn!("Unknown pipeline id {pipeline_id:?} for input event. Ignoring.");
return false;
};
let mut update_hovered_browsing_context =
|newly_hovered_browsing_context_id, focus_moving_to_another_iframe: bool| {
let old_hovered_context_id = std::mem::replace(
&mut self.hovered_browsing_context_id,
newly_hovered_browsing_context_id,
);
if old_hovered_context_id == newly_hovered_browsing_context_id {
return;
}
let Some(old_hovered_context_id) = old_hovered_context_id else {
return;
};
let Some(pipeline) = browsing_contexts
.get(&old_hovered_context_id)
.and_then(|browsing_context| pipelines.get(&browsing_context.pipeline_id))
else {
return;
};
let mut synthetic_mouse_leave_event = event.clone();
synthetic_mouse_leave_event.event.event =
InputEvent::MouseLeftViewport(MouseLeftViewportEvent {
focus_moving_to_another_iframe,
});
let _ = pipeline
.event_loop
.send(ScriptThreadMessage::SendInputEvent(
self.webview_id,
pipeline.id,
synthetic_mouse_leave_event,
));
};
if let InputEvent::MouseLeftViewport(_) = &event.event.event {
update_hovered_browsing_context(None, false);
return true;
}
if let InputEvent::MouseMove(_) = &event.event.event {
update_hovered_browsing_context(Some(pipeline.browsing_context_id), true);
self.last_mouse_move_point = event
.hit_test_result
.as_ref()
.expect("MouseMove events should always have hit tests.")
.point_in_viewport;
}
let _ = pipeline
.event_loop
.send(ScriptThreadMessage::SendInputEvent(
self.webview_id,
pipeline.id,
event,
));
true
}
}