From 1cd3d6bd4cd0071c8c35d1f891b9f1edaaf20ac7 Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Tue, 31 Mar 2020 22:44:47 +0200 Subject: [PATCH] Introduce >::current_request This safe helper contains the only source of unsafety from the actual image layout helpers methods, making them completely safe. --- components/script/dom/htmlimageelement.rs | 51 +++++++++-------------- components/script/dom/node.rs | 18 ++++---- 2 files changed, 26 insertions(+), 43 deletions(-) diff --git a/components/script/dom/htmlimageelement.rs b/components/script/dom/htmlimageelement.rs index cd32e6c95dec..b5f20784c8e5 100644 --- a/components/script/dom/htmlimageelement.rs +++ b/components/script/dom/htmlimageelement.rs @@ -1366,53 +1366,40 @@ impl MicrotaskRunnable for ImageElementMicrotask { } pub trait LayoutHTMLImageElementHelpers { - #[allow(unsafe_code)] - unsafe fn image(self) -> Option>; - #[allow(unsafe_code)] - unsafe fn image_url(self) -> Option; - #[allow(unsafe_code)] - unsafe fn image_density(self) -> Option; - #[allow(unsafe_code)] - unsafe fn image_data(self) -> (Option>, Option); + fn image(self) -> Option>; + fn image_url(self) -> Option; + fn image_density(self) -> Option; + fn image_data(self) -> (Option>, Option); fn get_width(self) -> LengthOrPercentageOrAuto; fn get_height(self) -> LengthOrPercentageOrAuto; } -impl LayoutHTMLImageElementHelpers for LayoutDom<'_, HTMLImageElement> { +impl<'dom> LayoutDom<'dom, HTMLImageElement> { #[allow(unsafe_code)] - unsafe fn image(self) -> Option> { - (*self.unsafe_get()) - .current_request - .borrow_for_layout() - .image - .clone() + fn current_request(self) -> &'dom ImageRequest { + unsafe { self.unsafe_get().current_request.borrow_for_layout() } + } +} + +impl LayoutHTMLImageElementHelpers for LayoutDom<'_, HTMLImageElement> { + fn image(self) -> Option> { + self.current_request().image.clone() } - #[allow(unsafe_code)] - unsafe fn image_url(self) -> Option { - (*self.unsafe_get()) - .current_request - .borrow_for_layout() - .parsed_url - .clone() + fn image_url(self) -> Option { + self.current_request().parsed_url.clone() } - #[allow(unsafe_code)] - unsafe fn image_data(self) -> (Option>, Option) { - let current_request = (*self.unsafe_get()).current_request.borrow_for_layout(); + fn image_data(self) -> (Option>, Option) { + let current_request = self.current_request(); ( current_request.image.clone(), current_request.metadata.clone(), ) } - #[allow(unsafe_code)] - unsafe fn image_density(self) -> Option { - (*self.unsafe_get()) - .current_request - .borrow_for_layout() - .current_pixel_density - .clone() + fn image_density(self) -> Option { + self.current_request().current_pixel_density.clone() } fn get_width(self) -> LengthOrPercentageOrAuto { diff --git a/components/script/dom/node.rs b/components/script/dom/node.rs index 7348431d4f38..b7215cfd92b0 100644 --- a/components/script/dom/node.rs +++ b/components/script/dom/node.rs @@ -1493,25 +1493,21 @@ impl<'dom> LayoutNodeHelpers<'dom> for LayoutDom<'dom, Node> { #[allow(unsafe_code)] fn image_url(self) -> Option { - unsafe { - self.downcast::() - .expect("not an image!") - .image_url() - } + self.downcast::() + .expect("not an image!") + .image_url() } #[allow(unsafe_code)] fn image_data(self) -> Option<(Option>, Option)> { - unsafe { self.downcast::().map(|e| e.image_data()) } + self.downcast::().map(|e| e.image_data()) } #[allow(unsafe_code)] fn image_density(self) -> Option { - unsafe { - self.downcast::() - .expect("not an image!") - .image_density() - } + self.downcast::() + .expect("not an image!") + .image_density() } fn canvas_data(self) -> Option {