From 9865a4194c57f7a7b74f06ff65de5b081e79e745 Mon Sep 17 00:00:00 2001 From: Cameron McCormack Date: Wed, 10 Oct 2018 02:58:20 +0000 Subject: [PATCH] style: Merge css::{URLValueData, ImageValue} into css::URLValue. Differential Revision: https://phabricator.services.mozilla.com/D8061 --- components/style/gecko/conversions.rs | 4 +- components/style/gecko/url.rs | 159 ++++++++---------- .../style/gecko_bindings/sugar/refptr.rs | 5 - components/style/properties/gecko.mako.rs | 14 +- 4 files changed, 80 insertions(+), 102 deletions(-) diff --git a/components/style/gecko/conversions.rs b/components/style/gecko/conversions.rs index 846f2113f777..84ad514c5893 100644 --- a/components/style/gecko/conversions.rs +++ b/components/style/gecko/conversions.rs @@ -225,13 +225,13 @@ impl nsStyleImage { match image { GenericImage::Gradient(boxed_gradient) => self.set_gradient(*boxed_gradient), GenericImage::Url(ref url) => unsafe { - bindings::Gecko_SetLayerImageImageValue(self, url.0.image_value.get()); + bindings::Gecko_SetLayerImageImageValue(self, (url.0).0.url_value.get()); }, GenericImage::Rect(ref image_rect) => { unsafe { bindings::Gecko_SetLayerImageImageValue( self, - image_rect.url.0.image_value.get(), + (image_rect.url.0).0.url_value.get(), ); bindings::Gecko_InitializeImageCropRect(self); diff --git a/components/style/gecko/url.rs b/components/style/gecko/url.rs index 8390196c5238..263fa674ce2b 100644 --- a/components/style/gecko/url.rs +++ b/components/style/gecko/url.rs @@ -7,10 +7,9 @@ use cssparser::Parser; use gecko_bindings::bindings; use gecko_bindings::structs::ServoBundledURI; -use gecko_bindings::structs::mozilla::css::URLValueData; use gecko_bindings::structs::root::{RustString, nsStyleImageRequest}; use gecko_bindings::structs::root::mozilla::CORSMode; -use gecko_bindings::structs::root::mozilla::css::{ImageValue, URLValue}; +use gecko_bindings::structs::root::mozilla::css::URLValue; use gecko_bindings::sugar::refptr::RefPtr; use malloc_size_of::{MallocSizeOf, MallocSizeOfOps}; use nsstring::nsCString; @@ -38,8 +37,7 @@ pub struct CssUrl { } impl CssUrl { - /// Try to parse a URL from a string value that is a valid CSS token for a - /// URL. + /// Parse a URL from a string value that is a valid CSS token for a URL. pub fn parse_from_string(url: String, context: &ParserContext) -> Self { CssUrl { serialization: Arc::new(url), @@ -54,8 +52,8 @@ impl CssUrl { false } - /// Convert from URLValueData to SpecifiedUrl. - unsafe fn from_url_value_data(url: &URLValueData) -> Self { + /// Convert from URLValue to CssUrl. + unsafe fn from_url_value(url: &URLValue) -> Self { let arc_type = &url.mString as *const _ as *const RawOffsetArc; CssUrl { serialization: Arc::from_raw_offset((*arc_type).clone()), @@ -117,7 +115,7 @@ impl MallocSizeOf for CssUrl { } } -/// A specified url() value for general usage. +/// A specified non-image `url()` value. #[derive(Clone, Debug, SpecifiedValueInfo, ToCss)] pub struct SpecifiedUrl { /// The specified url value. @@ -129,72 +127,19 @@ pub struct SpecifiedUrl { } impl SpecifiedUrl { - fn from_css_url(url: CssUrl) -> Self { - let url_value = unsafe { - let ptr = bindings::Gecko_NewURLValue(url.for_ffi()); - // We do not expect Gecko_NewURLValue returns null. - debug_assert!(!ptr.is_null()); - RefPtr::from_addrefed(ptr) - }; - Self { url, url_value } - } -} - -impl PartialEq for SpecifiedUrl { - fn eq(&self, other: &Self) -> bool { - self.url.eq(&other.url) - } -} - -impl Eq for SpecifiedUrl {} - -impl Parse for SpecifiedUrl { - fn parse<'i, 't>( - context: &ParserContext, - input: &mut Parser<'i, 't>, - ) -> Result> { - CssUrl::parse(context, input).map(Self::from_css_url) - } -} - -impl MallocSizeOf for SpecifiedUrl { - fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - let mut n = self.url.size_of(ops); - // Although this is a RefPtr, this is the primary reference because - // SpecifiedUrl is responsible for creating the url_value. So we - // measure unconditionally here. - n += unsafe { bindings::Gecko_URLValue_SizeOfIncludingThis(self.url_value.get()) }; - n - } -} - -/// A specified url() value for image. -/// -/// This exists so that we can construct `ImageValue` and reuse it. -#[derive(Clone, Debug, SpecifiedValueInfo, ToCss)] -pub struct SpecifiedImageUrl { - /// The specified url value. - pub url: CssUrl, - /// Gecko's ImageValue so that we can reuse it while rematching a - /// property with this specified value. - #[css(skip)] - pub image_value: RefPtr, -} - -impl SpecifiedImageUrl { - /// Parse a URL from a string value. See SpecifiedUrl::parse_from_string. + /// Parse a URL from a string value. pub fn parse_from_string(url: String, context: &ParserContext) -> Self { Self::from_css_url(CssUrl::parse_from_string(url, context)) } fn from_css_url_with_cors(url: CssUrl, cors: CORSMode) -> Self { - let image_value = unsafe { - let ptr = bindings::Gecko_ImageValue_Create(url.for_ffi(), cors); - // We do not expect Gecko_ImageValue_Create returns null. + let url_value = unsafe { + let ptr = bindings::Gecko_URLValue_Create(url.for_ffi(), cors); + // We do not expect Gecko_URLValue_Create returns null. debug_assert!(!ptr.is_null()); RefPtr::from_addrefed(ptr) }; - Self { url, image_value } + Self { url, url_value } } fn from_css_url(url: CssUrl) -> Self { @@ -206,18 +151,9 @@ impl SpecifiedImageUrl { use gecko_bindings::structs::root::mozilla::CORSMode_CORS_ANONYMOUS; Self::from_css_url_with_cors(url, CORSMode_CORS_ANONYMOUS) } - - /// Provides an alternate method for parsing that associates the URL - /// with anonymous CORS headers. - pub fn parse_with_cors_anonymous<'i, 't>( - context: &ParserContext, - input: &mut Parser<'i, 't>, - ) -> Result> { - CssUrl::parse(context, input).map(Self::from_css_url_with_cors_anonymous) - } } -impl Parse for SpecifiedImageUrl { +impl Parse for SpecifiedUrl { fn parse<'i, 't>( context: &ParserContext, input: &mut Parser<'i, 't>, @@ -226,21 +162,21 @@ impl Parse for SpecifiedImageUrl { } } -impl PartialEq for SpecifiedImageUrl { +impl PartialEq for SpecifiedUrl { fn eq(&self, other: &Self) -> bool { self.url.eq(&other.url) } } -impl Eq for SpecifiedImageUrl {} +impl Eq for SpecifiedUrl {} -impl MallocSizeOf for SpecifiedImageUrl { +impl MallocSizeOf for SpecifiedUrl { fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { let mut n = self.url.size_of(ops); // Although this is a RefPtr, this is the primary reference because - // SpecifiedUrl is responsible for creating the image_value. So we + // SpecifiedUrl is responsible for creating the url_value. So we // measure unconditionally here. - n += unsafe { bindings::Gecko_ImageValue_SizeOfIncludingThis(self.image_value.get()) }; + n += unsafe { bindings::Gecko_URLValue_SizeOfIncludingThis(self.url_value.get()) }; n } } @@ -259,6 +195,37 @@ impl ToComputedValue for SpecifiedUrl { } } +/// A specified image `url()` value. +#[derive(Clone, Debug, Eq, MallocSizeOf, PartialEq, SpecifiedValueInfo, ToCss)] +pub struct SpecifiedImageUrl(pub SpecifiedUrl); + +impl SpecifiedImageUrl { + /// Parse a URL from a string value that is a valid CSS token for a URL. + pub fn parse_from_string(url: String, context: &ParserContext) -> Self { + SpecifiedImageUrl(SpecifiedUrl::parse_from_string(url, context)) + } + + /// Provides an alternate method for parsing that associates the URL + /// with anonymous CORS headers. + pub fn parse_with_cors_anonymous<'i, 't>( + context: &ParserContext, + input: &mut Parser<'i, 't>, + ) -> Result> { + CssUrl::parse(context, input) + .map(SpecifiedUrl::from_css_url_with_cors_anonymous) + .map(SpecifiedImageUrl) + } +} + +impl Parse for SpecifiedImageUrl { + fn parse<'i, 't>( + context: &ParserContext, + input: &mut Parser<'i, 't>, + ) -> Result> { + SpecifiedUrl::parse(context, input).map(SpecifiedImageUrl) + } +} + impl ToComputedValue for SpecifiedImageUrl { type ComputedValue = ComputedImageUrl; @@ -274,9 +241,9 @@ impl ToComputedValue for SpecifiedImageUrl { } fn serialize_computed_url( - url_value_data: &URLValueData, + url_value: &URLValue, dest: &mut CssWriter, - get_url: unsafe extern "C" fn(*const URLValueData, *mut nsCString), + get_url: unsafe extern "C" fn(*const URLValue, *mut nsCString) -> (), ) -> fmt::Result where W: Write, @@ -284,13 +251,13 @@ where dest.write_str("url(")?; unsafe { let mut string = nsCString::new(); - get_url(url_value_data, &mut string); + get_url(url_value, &mut string); string.as_str_unchecked().to_css(dest)?; } dest.write_char(')') } -/// The computed value of a CSS `url()`. +/// The computed value of a CSS non-image `url()`. /// /// The only difference between specified and computed URLs is the /// serialization. @@ -303,7 +270,7 @@ impl ToCss for ComputedUrl { W: Write, { serialize_computed_url( - &self.0.url_value._base, + &self.0.url_value, dest, bindings::Gecko_GetComputedURLSpec, ) @@ -313,12 +280,17 @@ impl ToCss for ComputedUrl { impl ComputedUrl { /// Convert from RefPtr to ComputedUrl. pub unsafe fn from_url_value(url_value: RefPtr) -> Self { - let url = CssUrl::from_url_value_data(&url_value._base); + let url = CssUrl::from_url_value(&*url_value); ComputedUrl(SpecifiedUrl { url, url_value }) } + + /// Get a raw pointer to the URLValue held by this ComputedUrl, for FFI. + pub fn url_value_ptr(&self) -> *mut URLValue { + self.0.url_value.get() + } } -/// The computed value of a CSS `url()` for image. +/// The computed value of a CSS image `url()`. #[derive(Clone, Debug, Eq, MallocSizeOf, PartialEq)] pub struct ComputedImageUrl(pub SpecifiedImageUrl); @@ -328,7 +300,7 @@ impl ToCss for ComputedImageUrl { W: Write, { serialize_computed_url( - &self.0.image_value._base, + &(self.0).0.url_value, dest, bindings::Gecko_GetComputedImageURLSpec, ) @@ -338,8 +310,13 @@ impl ToCss for ComputedImageUrl { impl ComputedImageUrl { /// Convert from nsStyleImageReques to ComputedImageUrl. pub unsafe fn from_image_request(image_request: &nsStyleImageRequest) -> Self { - let image_value = image_request.mImageValue.to_safe(); - let url = CssUrl::from_url_value_data(&image_value._base); - ComputedImageUrl(SpecifiedImageUrl { url, image_value }) + let url_value = image_request.mImageValue.to_safe(); + let url = CssUrl::from_url_value(&*url_value); + ComputedImageUrl(SpecifiedImageUrl(SpecifiedUrl { url, url_value })) + } + + /// Get a raw pointer to the URLValue held by this ComputedImageUrl, for FFI. + pub fn url_value_ptr(&self) -> *mut URLValue { + (self.0).0.url_value.get() } } diff --git a/components/style/gecko_bindings/sugar/refptr.rs b/components/style/gecko_bindings/sugar/refptr.rs index 835e77098c93..d7e7141e6026 100644 --- a/components/style/gecko_bindings/sugar/refptr.rs +++ b/components/style/gecko_bindings/sugar/refptr.rs @@ -297,11 +297,6 @@ impl_threadsafe_refcount!( bindings::Gecko_AddRefGridTemplateAreasValueArbitraryThread, bindings::Gecko_ReleaseGridTemplateAreasValueArbitraryThread ); -impl_threadsafe_refcount!( - structs::ImageValue, - bindings::Gecko_AddRefImageValueArbitraryThread, - bindings::Gecko_ReleaseImageValueArbitraryThread -); impl_threadsafe_refcount!( structs::SharedFontList, bindings::Gecko_AddRefSharedFontListArbitraryThread, diff --git a/components/style/properties/gecko.mako.rs b/components/style/properties/gecko.mako.rs index 8b6298b01d25..0c0077104506 100644 --- a/components/style/properties/gecko.mako.rs +++ b/components/style/properties/gecko.mako.rs @@ -689,7 +689,10 @@ def set_gecko_property(ffi_name, expr): } SVGPaintKind::PaintServer(url) => { unsafe { - bindings::Gecko_nsStyleSVGPaint_SetURLValue(paint, url.0.url_value.get()); + bindings::Gecko_nsStyleSVGPaint_SetURLValue( + paint, + url.url_value_ptr(), + ) } } SVGPaintKind::Color(color) => { @@ -4153,7 +4156,10 @@ fn static_assert() { } UrlOrNone::Url(ref url) => { unsafe { - Gecko_SetListStyleImageImageValue(&mut self.gecko, url.0.image_value.get()); + Gecko_SetListStyleImageImageValue( + &mut self.gecko, + url.url_value_ptr(), + ); } } } @@ -5358,7 +5364,7 @@ clip-path unsafe { Gecko_SetCursorImageValue( &mut self.gecko.mCursorImages[i], - v.images[i].url.0.image_value.get(), + v.images[i].url.url_value_ptr(), ); } @@ -5659,7 +5665,7 @@ clip-path unsafe { bindings::Gecko_SetContentDataImageValue( &mut self.gecko.mContents[i], - url.0.image_value.get(), + url.url_value_ptr(), ) } }