From f564b32b75df8f37340c39e4e2886b2e2c65a7c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Thu, 21 Jun 2018 13:09:35 +0200 Subject: [PATCH] style: Better debugging for stylesheets and URLs. Bug: 1470145 Reviewed-by: xidorn MozReview-Commit-ID: FIcz2K1ZYX0 --- components/style/error_reporting.rs | 4 +- components/style/gecko/data.rs | 13 ++++- components/style/gecko/url.rs | 9 ++-- .../style/gecko_bindings/sugar/refptr.rs | 9 ---- components/style/stylesheets/mod.rs | 52 +++++++++++++++---- components/style/stylesheets/stylesheet.rs | 4 +- 6 files changed, 63 insertions(+), 28 deletions(-) diff --git a/components/style/error_reporting.rs b/components/style/error_reporting.rs index af1e7ba5b86..b5d5ba17ef8 100644 --- a/components/style/error_reporting.rs +++ b/components/style/error_reporting.rs @@ -7,7 +7,6 @@ #![deny(missing_docs)] use cssparser::{BasicParseErrorKind, ParseErrorKind, SourceLocation, Token}; -use log; use std::fmt; use style_traits::ParseError; use stylesheets::UrlExtraData; @@ -229,8 +228,10 @@ pub trait ParseErrorReporter { /// This logging is silent by default, and can be enabled with a `RUST_LOG=style=info` /// environment variable. /// (See [`env_logger`](https://rust-lang-nursery.github.io/log/env_logger/).) +#[cfg(feature = "servo")] pub struct RustLogReporter; +#[cfg(feature = "servo")] impl ParseErrorReporter for RustLogReporter { fn report_error( &self, @@ -238,6 +239,7 @@ impl ParseErrorReporter for RustLogReporter { location: SourceLocation, error: ContextualParseError, ) { + use log; if log_enabled!(log::Level::Info) { info!( "Url:\t{}\n{}:{} {}", diff --git a/components/style/gecko/data.rs b/components/style/gecko/data.rs index a4ed8294ac2..90129209361 100644 --- a/components/style/gecko/data.rs +++ b/components/style/gecko/data.rs @@ -17,14 +17,25 @@ use media_queries::{Device, MediaList}; use properties::ComputedValues; use selector_parser::SnapshotMap; use servo_arc::Arc; +use std::fmt; use shared_lock::{Locked, SharedRwLockReadGuard, StylesheetGuards}; use stylesheets::{CssRule, Origin, StylesheetContents, StylesheetInDocument}; use stylist::Stylist; /// Little wrapper to a Gecko style sheet. -#[derive(Debug, Eq, PartialEq)] +#[derive(Eq, PartialEq)] pub struct GeckoStyleSheet(*const DomStyleSheet); +impl fmt::Debug for GeckoStyleSheet { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + let contents = self.contents(); + formatter.debug_struct("GeckoStyleSheet") + .field("origin", &contents.origin) + .field("url_data", &*contents.url_data.read()) + .finish() + } +} + impl ToMediaListKey for ::gecko::data::GeckoStyleSheet { fn to_media_list_key(&self) -> MediaListKey { use std::mem; diff --git a/components/style/gecko/url.rs b/components/style/gecko/url.rs index cb503f4720a..751345d72cb 100644 --- a/components/style/gecko/url.rs +++ b/components/style/gecko/url.rs @@ -6,7 +6,7 @@ use cssparser::Parser; use gecko_bindings::bindings; -use gecko_bindings::structs::{ServoBundledURI, URLExtraData}; +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::css::{ImageValue, URLValue}; @@ -17,6 +17,7 @@ use parser::{Parse, ParserContext}; use servo_arc::{Arc, RawOffsetArc}; use std::fmt::{self, Write}; use std::mem; +use stylesheets::UrlExtraData; use style_traits::{CssWriter, ParseError, ToCss}; use values::computed::{Context, ToComputedValue}; @@ -32,7 +33,7 @@ pub struct CssUrl { /// The URL extra data. #[css(skip)] - pub extra_data: RefPtr, + pub extra_data: UrlExtraData, } impl CssUrl { @@ -58,7 +59,7 @@ impl CssUrl { &url.mString as *const _ as *const RawOffsetArc; CssUrl { serialization: Arc::from_raw_offset((*arc_type).clone()), - extra_data: url.mExtraData.to_safe(), + extra_data: UrlExtraData(url.mExtraData.to_safe()), } } @@ -88,7 +89,7 @@ impl CssUrl { let arc_offset = Arc::into_raw_offset(self.serialization.clone()); ServoBundledURI { mURLString: unsafe { mem::transmute::<_, RawOffsetArc>(arc_offset) }, - mExtraData: self.extra_data.get(), + mExtraData: self.extra_data.0.get(), } } } diff --git a/components/style/gecko_bindings/sugar/refptr.rs b/components/style/gecko_bindings/sugar/refptr.rs index 10c9e627699..6a32b81430e 100644 --- a/components/style/gecko_bindings/sugar/refptr.rs +++ b/components/style/gecko_bindings/sugar/refptr.rs @@ -78,15 +78,6 @@ impl RefPtr { ret } - /// Create a reference to RefPtr from a reference to pointer. - /// - /// The pointer must be valid and non null. - /// - /// This method doesn't touch refcount. - pub unsafe fn from_ptr_ref(ptr: &*mut T) -> &Self { - mem::transmute(ptr) - } - /// Produces an FFI-compatible RefPtr that can be stored in style structs. /// /// structs::RefPtr does not have a destructor, so this may leak diff --git a/components/style/stylesheets/mod.rs b/components/style/stylesheets/mod.rs index 0a9e5fdb9e2..e3641f3e515 100644 --- a/components/style/stylesheets/mod.rs +++ b/components/style/stylesheets/mod.rs @@ -61,22 +61,52 @@ pub type UrlExtraData = ::servo_url::ServoUrl; /// Extra data that the backend may need to resolve url values. #[cfg(feature = "gecko")] -pub type UrlExtraData = - ::gecko_bindings::sugar::refptr::RefPtr<::gecko_bindings::structs::URLExtraData>; +#[derive(Clone, PartialEq)] +pub struct UrlExtraData( + pub ::gecko_bindings::sugar::refptr::RefPtr<::gecko_bindings::structs::URLExtraData> +); #[cfg(feature = "gecko")] impl UrlExtraData { - /// Returns a string for the url. - /// - /// Unimplemented currently. - pub fn as_str(&self) -> &str { - // TODO - "(stylo: not supported)" + /// True if this URL scheme is chrome. + #[inline] + pub fn is_chrome(&self) -> bool { + self.0.mIsChrome } - /// True if this URL scheme is chrome. - pub fn is_chrome(&self) -> bool { - self.mIsChrome + /// Create a reference to this `UrlExtraData` from a reference to pointer. + /// + /// The pointer must be valid and non null. + /// + /// This method doesn't touch refcount. + #[inline] + pub unsafe fn from_ptr_ref(ptr: &*mut ::gecko_bindings::structs::URLExtraData) -> &Self { + ::std::mem::transmute(ptr) + } +} + +#[cfg(feature = "gecko")] +impl fmt::Debug for UrlExtraData { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + use gecko_bindings::{structs, bindings}; + + struct DebugURI(*mut structs::nsIURI); + impl fmt::Debug for DebugURI { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + use nsstring::nsCString; + let mut spec = nsCString::new(); + unsafe { + bindings::Gecko_nsIURI_Debug(self.0, &mut spec); + } + spec.fmt(formatter) + } + } + + formatter.debug_struct("URLExtraData") + .field("is_chrome", &self.is_chrome()) + .field("base", &DebugURI(self.0.mBaseURI.raw::())) + .field("referrer", &DebugURI(self.0.mReferrer.raw::())) + .finish() } } diff --git a/components/style/stylesheets/stylesheet.rs b/components/style/stylesheets/stylesheet.rs index 131466b52f9..ee24a918c73 100644 --- a/components/style/stylesheets/stylesheet.rs +++ b/components/style/stylesheets/stylesheet.rs @@ -176,7 +176,7 @@ macro_rules! rule_filter { } /// A trait to represent a given stylesheet in a document. -pub trait StylesheetInDocument { +pub trait StylesheetInDocument : ::std::fmt::Debug { /// Get the stylesheet origin. fn origin(&self, guard: &SharedRwLockReadGuard) -> Origin; @@ -263,7 +263,7 @@ impl StylesheetInDocument for Stylesheet { /// A simple wrapper over an `Arc`, with pointer comparison, and /// suitable for its use in a `StylesheetSet`. -#[derive(Clone)] +#[derive(Clone, Debug)] #[cfg_attr(feature = "servo", derive(MallocSizeOf))] pub struct DocumentStyleSheet( #[cfg_attr(feature = "servo", ignore_malloc_size_of = "Arc")] pub Arc,