canvas: prevent unwrap on offscreen canvas (#37460)

Remove all unwrap usage on offscreen canvas to prevent panic.

Testing:
`tests/wpt/tests/html/canvas/offscreen/manual/the-offscreen-canvas/offscreencanvas.transfercontrol.to.offscreen-crash.html`
Fixes: #37415

---------

Signed-off-by: Wu Yu Wei <yuweiwu@pm.me>
Signed-off-by: Yu Wei Wu <yuweiwu@YunoMacBook-Air.local>
Co-authored-by: Yu Wei Wu <yuweiwu@YunoMacBook-Air.local>
This commit is contained in:
Ngo Iok Ui (Wu Yu Wei) 2025-06-16 14:39:56 +09:00 committed by GitHub
parent f8f7c6ebd1
commit e69962e646
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 58 additions and 26 deletions

View file

@ -140,8 +140,8 @@ impl CanvasContext for CanvasRenderingContext2D {
self.canvas_state.get_canvas_id()
}
fn canvas(&self) -> HTMLCanvasElementOrOffscreenCanvas {
self.canvas.clone()
fn canvas(&self) -> Option<HTMLCanvasElementOrOffscreenCanvas> {
Some(self.canvas.clone())
}
fn update_rendering(&self) {

View file

@ -75,7 +75,7 @@ impl CanvasContext for OffscreenCanvasRenderingContext2D {
self.context.context_id()
}
fn canvas(&self) -> HTMLCanvasElementOrOffscreenCanvas {
fn canvas(&self) -> Option<HTMLCanvasElementOrOffscreenCanvas> {
self.context.canvas()
}
@ -98,7 +98,7 @@ impl OffscreenCanvasRenderingContext2DMethods<crate::DomTypeHolder>
// https://html.spec.whatwg.org/multipage/offscreencontext2d-canvas
fn Canvas(&self) -> DomRoot<OffscreenCanvas> {
match self.context.canvas() {
HTMLCanvasElementOrOffscreenCanvas::OffscreenCanvas(canvas) => canvas,
Some(HTMLCanvasElementOrOffscreenCanvas::OffscreenCanvas(canvas)) => canvas,
_ => panic!("Should not be called from onscreen canvas"),
}
}

View file

@ -908,7 +908,7 @@ impl CanvasContext for WebGL2RenderingContext {
self.base.context_id()
}
fn canvas(&self) -> HTMLCanvasElementOrOffscreenCanvas {
fn canvas(&self) -> Option<HTMLCanvasElementOrOffscreenCanvas> {
self.base.canvas().clone()
}

View file

@ -1914,8 +1914,8 @@ impl CanvasContext for WebGLRenderingContext {
self.webgl_sender.context_id()
}
fn canvas(&self) -> HTMLCanvasElementOrOffscreenCanvas {
self.canvas.clone()
fn canvas(&self) -> Option<HTMLCanvasElementOrOffscreenCanvas> {
Some(self.canvas.clone())
}
fn resize(&self) {

View file

@ -294,8 +294,8 @@ impl CanvasContext for GPUCanvasContext {
})
}
fn canvas(&self) -> HTMLCanvasElementOrOffscreenCanvas {
self.canvas.clone()
fn canvas(&self) -> Option<HTMLCanvasElementOrOffscreenCanvas> {
Some(self.canvas.clone())
}
}