CanGc fixes and checks in multiple files (#33836)

* CanGc fixes and checks in multiple files

Signed-off-by: L Ashwin B <lashwinib@gmail.com>

* CanGc fixes in storageevent.rs

Signed-off-by: L Ashwin B <lashwinib@gmail.com>

---------

Signed-off-by: L Ashwin B <lashwinib@gmail.com>
This commit is contained in:
chickenleaf 2024-10-15 10:00:48 +05:30 committed by GitHub
parent 2f1862aaf5
commit 1e39787573
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 74 additions and 59 deletions

View file

@ -183,6 +183,7 @@ DOMInterfaces = {
'RTCPeerConnection': { 'RTCPeerConnection': {
'inRealms': ['AddIceCandidate', 'CreateAnswer', 'CreateOffer', 'SetLocalDescription', 'SetRemoteDescription'], 'inRealms': ['AddIceCandidate', 'CreateAnswer', 'CreateOffer', 'SetLocalDescription', 'SetRemoteDescription'],
'canGc': ['Close'],
}, },
'Range': { 'Range': {

View file

@ -4645,6 +4645,7 @@ impl DocumentMethods for Document {
"storageevent" => Ok(DomRoot::upcast(StorageEvent::new_uninitialized( "storageevent" => Ok(DomRoot::upcast(StorageEvent::new_uninitialized(
&self.window, &self.window,
"".into(), "".into(),
can_gc,
))), ))),
"touchevent" => Ok(DomRoot::upcast(TouchEvent::new_uninitialized( "touchevent" => Ok(DomRoot::upcast(TouchEvent::new_uninitialized(
&self.window, &self.window,
@ -4652,7 +4653,10 @@ impl DocumentMethods for Document {
&TouchList::new(&self.window, &[]), &TouchList::new(&self.window, &[]),
&TouchList::new(&self.window, &[]), &TouchList::new(&self.window, &[]),
))), ))),
"uievent" | "uievents" => Ok(DomRoot::upcast(UIEvent::new_uninitialized(&self.window))), "uievent" | "uievents" => Ok(DomRoot::upcast(UIEvent::new_uninitialized(
&self.window,
can_gc,
))),
_ => Err(Error::NotSupported), _ => Err(Error::NotSupported),
} }
} }

View file

@ -31,7 +31,7 @@ use crate::dom::globalscope::GlobalScope;
use crate::dom::hashchangeevent::HashChangeEvent; use crate::dom::hashchangeevent::HashChangeEvent;
use crate::dom::popstateevent::PopStateEvent; use crate::dom::popstateevent::PopStateEvent;
use crate::dom::window::Window; use crate::dom::window::Window;
use crate::script_runtime::JSContext; use crate::script_runtime::{CanGc, JSContext};
enum PushOrReplace { enum PushOrReplace {
Push, Push,
@ -83,7 +83,7 @@ impl History {
/// <https://html.spec.whatwg.org/multipage/#history-traversal> /// <https://html.spec.whatwg.org/multipage/#history-traversal>
/// Steps 5-16 /// Steps 5-16
#[allow(unsafe_code)] #[allow(unsafe_code)]
pub fn activate_state(&self, state_id: Option<HistoryStateId>, url: ServoUrl) { pub fn activate_state(&self, state_id: Option<HistoryStateId>, url: ServoUrl, can_gc: CanGc) {
// Steps 5 // Steps 5
let document = self.window.Document(); let document = self.window.Document();
let old_url = document.url().clone(); let old_url = document.url().clone();
@ -139,6 +139,7 @@ impl History {
self.window.upcast::<EventTarget>(), self.window.upcast::<EventTarget>(),
&self.window, &self.window,
unsafe { HandleValue::from_raw(self.state.handle()) }, unsafe { HandleValue::from_raw(self.state.handle()) },
can_gc,
); );
} }

View file

@ -69,16 +69,14 @@ impl PopStateEvent {
ev ev
} }
pub fn dispatch_jsval(target: &EventTarget, window: &Window, state: HandleValue) { pub fn dispatch_jsval(
let event = PopStateEvent::new( target: &EventTarget,
window, window: &Window,
None, state: HandleValue,
atom!("popstate"), can_gc: CanGc,
false, ) {
false, let event =
state, PopStateEvent::new(window, None, atom!("popstate"), false, false, state, can_gc);
CanGc::note(),
);
event.upcast::<Event>().fire(target); event.upcast::<Event>().fire(target);
} }
} }

View file

@ -749,7 +749,7 @@ impl RTCPeerConnectionMethods for RTCPeerConnection {
} }
/// <https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close> /// <https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close>
fn Close(&self) { fn Close(&self, can_gc: CanGc) {
// Step 1 // Step 1
if self.closed.get() { if self.closed.get() {
return; return;
@ -765,7 +765,7 @@ impl RTCPeerConnectionMethods for RTCPeerConnection {
// Step 6 // Step 6
for (_, val) in self.data_channels.borrow().iter() { for (_, val) in self.data_channels.borrow().iter() {
val.on_state_change(DataChannelState::Closed, CanGc::note()); val.on_state_change(DataChannelState::Closed, can_gc);
} }
// Step 7-10 // Step 7-10

View file

@ -49,8 +49,12 @@ impl StorageEvent {
} }
} }
pub fn new_uninitialized(window: &Window, url: DOMString) -> DomRoot<StorageEvent> { pub fn new_uninitialized(
Self::new_uninitialized_with_proto(window, None, url, CanGc::note()) window: &Window,
url: DOMString,
can_gc: CanGc,
) -> DomRoot<StorageEvent> {
Self::new_uninitialized_with_proto(window, None, url, can_gc)
} }
fn new_uninitialized_with_proto( fn new_uninitialized_with_proto(

View file

@ -38,8 +38,8 @@ impl UIEvent {
} }
} }
pub fn new_uninitialized(window: &Window) -> DomRoot<UIEvent> { pub fn new_uninitialized(window: &Window, can_gc: CanGc) -> DomRoot<UIEvent> {
Self::new_uninitialized_with_proto(window, None, CanGc::note()) Self::new_uninitialized_with_proto(window, None, can_gc)
} }
fn new_uninitialized_with_proto( fn new_uninitialized_with_proto(
@ -57,16 +57,10 @@ impl UIEvent {
cancelable: EventCancelable, cancelable: EventCancelable,
view: Option<&Window>, view: Option<&Window>,
detail: i32, detail: i32,
can_gc: CanGc,
) -> DomRoot<UIEvent> { ) -> DomRoot<UIEvent> {
Self::new_with_proto( Self::new_with_proto(
window, window, None, type_, can_bubble, cancelable, view, detail, can_gc,
None,
type_,
can_bubble,
cancelable,
view,
detail,
CanGc::note(),
) )
} }

View file

@ -1463,9 +1463,10 @@ impl ScriptThread {
id: PipelineId, id: PipelineId,
size: WindowSizeData, size: WindowSizeData,
size_type: WindowSizeType, size_type: WindowSizeType,
can_gc: CanGc,
) { ) {
self.profile_event(ScriptThreadEventCategory::Resize, Some(id), || { self.profile_event(ScriptThreadEventCategory::Resize, Some(id), || {
self.handle_resize_event(id, size, size_type); self.handle_resize_event(id, size, size_type, can_gc);
}); });
} }
@ -1694,7 +1695,7 @@ impl ScriptThread {
if let Some((size, size_type)) = document.window().take_unhandled_resize_event() { if let Some((size, size_type)) = document.window().take_unhandled_resize_event() {
// Resize steps. // Resize steps.
self.run_the_resize_steps(pipeline_id, size, size_type); self.run_the_resize_steps(pipeline_id, size, size_type, can_gc);
// Evaluate media queries and report changes. // Evaluate media queries and report changes.
document document
@ -2366,7 +2367,7 @@ impl ScriptThread {
can_gc, can_gc,
), ),
ConstellationControlMsg::UpdateHistoryState(pipeline_id, history_state_id, url) => { ConstellationControlMsg::UpdateHistoryState(pipeline_id, history_state_id, url) => {
self.handle_update_history_state_msg(pipeline_id, history_state_id, url) self.handle_update_history_state_msg(pipeline_id, history_state_id, url, can_gc)
}, },
ConstellationControlMsg::RemoveHistoryStates(pipeline_id, history_states) => { ConstellationControlMsg::RemoveHistoryStates(pipeline_id, history_states) => {
self.handle_remove_history_states(pipeline_id, history_states) self.handle_remove_history_states(pipeline_id, history_states)
@ -2375,7 +2376,7 @@ impl ScriptThread {
self.handle_focus_iframe_msg(parent_pipeline_id, frame_id) self.handle_focus_iframe_msg(parent_pipeline_id, frame_id)
}, },
ConstellationControlMsg::WebDriverScriptCommand(pipeline_id, msg) => { ConstellationControlMsg::WebDriverScriptCommand(pipeline_id, msg) => {
self.handle_webdriver_msg(pipeline_id, msg) self.handle_webdriver_msg(pipeline_id, msg, can_gc)
}, },
ConstellationControlMsg::WebFontLoaded(pipeline_id, success) => { ConstellationControlMsg::WebFontLoaded(pipeline_id, success) => {
self.handle_web_font_loaded(pipeline_id, success) self.handle_web_font_loaded(pipeline_id, success)
@ -2630,7 +2631,12 @@ impl ScriptThread {
} }
} }
fn handle_webdriver_msg(&self, pipeline_id: PipelineId, msg: WebDriverScriptCommand) { fn handle_webdriver_msg(
&self,
pipeline_id: PipelineId,
msg: WebDriverScriptCommand,
can_gc: CanGc,
) {
// https://github.com/servo/servo/issues/23535 // https://github.com/servo/servo/issues/23535
// These two messages need different treatment since the JS script might mutate // These two messages need different treatment since the JS script might mutate
// `self.documents`, which would conflict with the immutable borrow of it that // `self.documents`, which would conflict with the immutable borrow of it that
@ -2777,7 +2783,7 @@ impl ScriptThread {
webdriver_handlers::handle_get_active_element(&documents, pipeline_id, reply) webdriver_handlers::handle_get_active_element(&documents, pipeline_id, reply)
}, },
WebDriverScriptCommand::GetPageSource(reply) => { WebDriverScriptCommand::GetPageSource(reply) => {
webdriver_handlers::handle_get_page_source(&documents, pipeline_id, reply) webdriver_handlers::handle_get_page_source(&documents, pipeline_id, reply, can_gc)
}, },
WebDriverScriptCommand::GetCookies(reply) => { WebDriverScriptCommand::GetCookies(reply) => {
webdriver_handlers::handle_get_cookies(&documents, pipeline_id, reply) webdriver_handlers::handle_get_cookies(&documents, pipeline_id, reply)
@ -2818,6 +2824,7 @@ impl ScriptThread {
pipeline_id, pipeline_id,
node_id, node_id,
reply, reply,
can_gc,
) )
}, },
WebDriverScriptCommand::GetElementText(node_id, reply) => { WebDriverScriptCommand::GetElementText(node_id, reply) => {
@ -2829,6 +2836,7 @@ impl ScriptThread {
pipeline_id, pipeline_id,
node_id, node_id,
reply, reply,
can_gc,
) )
}, },
WebDriverScriptCommand::GetBrowsingContextId(webdriver_frame_id, reply) => { WebDriverScriptCommand::GetBrowsingContextId(webdriver_frame_id, reply) => {
@ -3125,6 +3133,7 @@ impl ScriptThread {
pipeline_id: PipelineId, pipeline_id: PipelineId,
history_state_id: Option<HistoryStateId>, history_state_id: Option<HistoryStateId>,
url: ServoUrl, url: ServoUrl,
can_gc: CanGc,
) { ) {
let window = self.documents.borrow().find_window(pipeline_id); let window = self.documents.borrow().find_window(pipeline_id);
match window { match window {
@ -3134,7 +3143,9 @@ impl ScriptThread {
pipeline_id pipeline_id
); );
}, },
Some(window) => window.History().activate_state(history_state_id, url), Some(window) => window
.History()
.activate_state(history_state_id, url, can_gc),
} }
} }
@ -4018,6 +4029,7 @@ impl ScriptThread {
pipeline_id: PipelineId, pipeline_id: PipelineId,
new_size: WindowSizeData, new_size: WindowSizeData,
size_type: WindowSizeType, size_type: WindowSizeType,
can_gc: CanGc,
) { ) {
let document = match self.documents.borrow().find_document(pipeline_id) { let document = match self.documents.borrow().find_document(pipeline_id) {
Some(document) => document, Some(document) => document,
@ -4046,6 +4058,7 @@ impl ScriptThread {
EventCancelable::NotCancelable, EventCancelable::NotCancelable,
Some(window), Some(window),
0i32, 0i32,
can_gc,
); );
uievent.upcast::<Event>().fire(window.upcast()); uievent.upcast::<Event>().fire(window.upcast());
} }

View file

@ -374,7 +374,7 @@ pub fn handle_get_browsing_context_id(
} }
// https://w3c.github.io/webdriver/#dfn-center-point // https://w3c.github.io/webdriver/#dfn-center-point
fn get_element_in_view_center_point(element: &Element) -> Option<Point2D<i64>> { fn get_element_in_view_center_point(element: &Element, can_gc: CanGc) -> Option<Point2D<i64>> {
window_from_node(element.upcast::<Node>()) window_from_node(element.upcast::<Node>())
.Document() .Document()
.GetBody() .GetBody()
@ -382,10 +382,7 @@ fn get_element_in_view_center_point(element: &Element) -> Option<Point2D<i64>> {
.and_then(|body| { .and_then(|body| {
// Step 1: Let rectangle be the first element of the DOMRect sequence // Step 1: Let rectangle be the first element of the DOMRect sequence
// returned by calling getClientRects() on element. // returned by calling getClientRects() on element.
element element.GetClientRects(can_gc).first().map(|rectangle| {
.GetClientRects(CanGc::note())
.first()
.map(|rectangle| {
let x = rectangle.X().round() as i64; let x = rectangle.X().round() as i64;
let y = rectangle.Y().round() as i64; let y = rectangle.Y().round() as i64;
let width = rectangle.Width().round() as i64; let width = rectangle.Width().round() as i64;
@ -415,11 +412,12 @@ pub fn handle_get_element_in_view_center_point(
pipeline: PipelineId, pipeline: PipelineId,
element_id: String, element_id: String,
reply: IpcSender<Result<Option<(i64, i64)>, ErrorStatus>>, reply: IpcSender<Result<Option<(i64, i64)>, ErrorStatus>>,
can_gc: CanGc,
) { ) {
reply reply
.send( .send(
find_node_by_unique_id(documents, pipeline, element_id).map(|node| { find_node_by_unique_id(documents, pipeline, element_id).map(|node| {
get_element_in_view_center_point(node.downcast::<Element>().unwrap()) get_element_in_view_center_point(node.downcast::<Element>().unwrap(), can_gc)
.map(|point| (point.x, point.y)) .map(|point| (point.x, point.y))
}), }),
) )
@ -715,6 +713,7 @@ pub fn handle_get_page_source(
documents: &Documents, documents: &Documents,
pipeline: PipelineId, pipeline: PipelineId,
reply: IpcSender<Result<String, ErrorStatus>>, reply: IpcSender<Result<String, ErrorStatus>>,
can_gc: CanGc,
) { ) {
reply reply
.send( .send(
@ -725,7 +724,7 @@ pub fn handle_get_page_source(
Some(element) => match element.GetOuterHTML() { Some(element) => match element.GetOuterHTML() {
Ok(source) => Ok(source.to_string()), Ok(source) => Ok(source.to_string()),
Err(_) => { Err(_) => {
match XMLSerializer::new(document.window(), None, CanGc::note()) match XMLSerializer::new(document.window(), None, can_gc)
.SerializeToString(element.upcast::<Node>()) .SerializeToString(element.upcast::<Node>())
{ {
Ok(source) => Ok(source.to_string()), Ok(source) => Ok(source.to_string()),
@ -926,6 +925,7 @@ pub fn handle_get_bounding_client_rect(
pipeline: PipelineId, pipeline: PipelineId,
element_id: String, element_id: String,
reply: IpcSender<Result<Rect<f32>, ErrorStatus>>, reply: IpcSender<Result<Rect<f32>, ErrorStatus>>,
can_gc: CanGc,
) { ) {
reply reply
.send( .send(
@ -933,7 +933,7 @@ pub fn handle_get_bounding_client_rect(
.downcast::<Element>( .downcast::<Element>(
) { ) {
Some(element) => { Some(element) => {
let rect = element.GetBoundingClientRect(CanGc::note()); let rect = element.GetBoundingClientRect(can_gc);
Ok(Rect::new( Ok(Rect::new(
Point2D::new(rect.X() as f32, rect.Y() as f32), Point2D::new(rect.X() as f32, rect.Y() as f32),
Size2D::new(rect.Width() as f32, rect.Height() as f32), Size2D::new(rect.Width() as f32, rect.Height() as f32),