Skip to content

Commit

Permalink
style: Merge css::{URLValueData, ImageValue} into css::URLValue.
Browse files Browse the repository at this point in the history
  • Loading branch information
heycam authored and emilio committed Oct 18, 2018
1 parent cd439df commit 9865a41
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 102 deletions.
4 changes: 2 additions & 2 deletions components/style/gecko/conversions.rs
Expand Up @@ -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);

Expand Down
159 changes: 68 additions & 91 deletions components/style/gecko/url.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand All @@ -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<String>;
CssUrl {
serialization: Arc::from_raw_offset((*arc_type).clone()),
Expand Down Expand Up @@ -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.
Expand All @@ -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<Self, ParseError<'i>> {
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<ImageValue>,
}

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 {
Expand All @@ -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<Self, ParseError<'i>> {
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>,
Expand All @@ -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
}
}
Expand All @@ -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<Self, ParseError<'i>> {
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<Self, ParseError<'i>> {
SpecifiedUrl::parse(context, input).map(SpecifiedImageUrl)
}
}

impl ToComputedValue for SpecifiedImageUrl {
type ComputedValue = ComputedImageUrl;

Expand All @@ -274,23 +241,23 @@ impl ToComputedValue for SpecifiedImageUrl {
}

fn serialize_computed_url<W>(
url_value_data: &URLValueData,
url_value: &URLValue,
dest: &mut CssWriter<W>,
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,
{
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.
Expand All @@ -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,
)
Expand All @@ -313,12 +280,17 @@ impl ToCss for ComputedUrl {
impl ComputedUrl {
/// Convert from RefPtr<URLValue> to ComputedUrl.
pub unsafe fn from_url_value(url_value: RefPtr<URLValue>) -> 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);

Expand All @@ -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,
)
Expand All @@ -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()
}
}
5 changes: 0 additions & 5 deletions components/style/gecko_bindings/sugar/refptr.rs
Expand Up @@ -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,
Expand Down
14 changes: 10 additions & 4 deletions components/style/properties/gecko.mako.rs
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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(),
);
}
}
}
Expand Down Expand Up @@ -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(),
);
}

Expand Down Expand Up @@ -5659,7 +5665,7 @@ clip-path
unsafe {
bindings::Gecko_SetContentDataImageValue(
&mut self.gecko.mContents[i],
url.0.image_value.get(),
url.url_value_ptr(),
)
}
}
Expand Down

0 comments on commit 9865a41

Please sign in to comment.