Bug 1410074: Honor CallerType for media query parsing. r?xidorn draft
authorEmilio Cobos Álvarez <emilio@crisal.io>
Thu, 19 Oct 2017 16:16:02 +0200
changeset 685282 42a68096e8003bef8abb958c5b3898f25a239a02
parent 685281 d3bf386e0edeedcf99a1c45a703507845be28225
child 685283 5763e9aa7e30437e1ce16f7ef84fa421427e79b5
push id85891
push userbmo:emilio@crisal.io
push dateTue, 24 Oct 2017 10:29:04 +0000
reviewersxidorn
bugs1410074
milestone58.0a1
Bug 1410074: Honor CallerType for media query parsing. r?xidorn We don't support setText (though it wouldn't be hard). It doesn't really matter anyway. MozReview-Commit-ID: 6M4HHqVJ1dp
layout/style/ServoBindingList.h
layout/style/ServoBindings.toml
layout/style/ServoMediaList.cpp
layout/style/nsCSSParser.cpp
servo/ports/geckolib/glue.rs
--- a/layout/style/ServoBindingList.h
+++ b/layout/style/ServoBindingList.h
@@ -511,17 +511,18 @@ SERVO_BINDING_FUNC(Servo_MediaList_Creat
 SERVO_BINDING_FUNC(Servo_MediaList_DeepClone, RawServoMediaListStrong,
                    RawServoMediaListBorrowed list)
 SERVO_BINDING_FUNC(Servo_MediaList_Matches, bool,
                    RawServoMediaListBorrowed list,
                    RawServoStyleSetBorrowed set)
 SERVO_BINDING_FUNC(Servo_MediaList_GetText, void,
                    RawServoMediaListBorrowed list, nsAString* result)
 SERVO_BINDING_FUNC(Servo_MediaList_SetText, void,
-                   RawServoMediaListBorrowed list, const nsACString* text)
+                   RawServoMediaListBorrowed list, const nsACString* text,
+                   mozilla::dom::CallerType aCallerType)
 SERVO_BINDING_FUNC(Servo_MediaList_GetLength, uint32_t,
                    RawServoMediaListBorrowed list)
 SERVO_BINDING_FUNC(Servo_MediaList_GetMediumAt, bool,
                    RawServoMediaListBorrowed list, uint32_t index,
                    nsAString* result)
 SERVO_BINDING_FUNC(Servo_MediaList_AppendMedium, void,
                    RawServoMediaListBorrowed list, const nsACString* new_medium)
 SERVO_BINDING_FUNC(Servo_MediaList_DeleteMedium, bool,
--- a/layout/style/ServoBindings.toml
+++ b/layout/style/ServoBindings.toml
@@ -385,16 +385,17 @@ raw-lines = [
 ]
 whitelist-functions = ["Servo_.*", "Gecko_.*"]
 structs-types = [
     "mozilla::css::GridTemplateAreasValue",
     "mozilla::css::ErrorReporter",
     "mozilla::css::ImageValue",
     "mozilla::css::URLValue",
     "mozilla::css::URLValueData",
+    "mozilla::dom::CallerType",
     "mozilla::AnonymousCounterStyle",
     "mozilla::AtomArray",
     "mozilla::MallocSizeOf",
     "mozilla::OriginFlags",
     "mozilla::UniquePtr",
     "ServoRawOffsetArc",
     "nsIContent",
     "nsIDocument",
--- a/layout/style/ServoMediaList.cpp
+++ b/layout/style/ServoMediaList.cpp
@@ -45,17 +45,17 @@ ServoMediaList::SetText(const nsAString&
   SetTextInternal(aMediaText, dom::CallerType::NonSystem);
 }
 
 void
 ServoMediaList::SetTextInternal(const nsAString& aMediaText,
                                 dom::CallerType aCallerType)
 {
   NS_ConvertUTF16toUTF8 mediaText(aMediaText);
-  Servo_MediaList_SetText(mRawList, &mediaText);
+  Servo_MediaList_SetText(mRawList, &mediaText, aCallerType);
 }
 
 uint32_t
 ServoMediaList::Length()
 {
   return Servo_MediaList_GetLength(mRawList);
 }
 
--- a/layout/style/nsCSSParser.cpp
+++ b/layout/style/nsCSSParser.cpp
@@ -1324,19 +1324,16 @@ protected:
     eCSSSection_Import,
     eCSSSection_NameSpace,
     eCSSSection_General
   };
   nsCSSSection  mSection;
 
   nsXMLNameSpaceMap *mNameSpaceMap;  // weak, mSheet owns it
 
-  // Whether the caller of the DOM API that invoked us is chrome or not.
-  dom::CallerType mDomCallerType = dom::CallerType::NonSystem;
-
   // After an UngetToken is done this flag is true. The next call to
   // GetToken clears the flag.
   bool mHavePushBack : 1;
 
   // True if we are in quirks mode; false in standards or almost standards mode
   bool          mNavQuirkMode : 1;
 
   // True when the hashless color quirk applies.
@@ -1561,28 +1558,30 @@ CSSParserImpl::InitScanner(nsCSSScanner&
   mScanner = &aScanner;
   mReporter = &aReporter;
   mScanner->SetErrorReporter(mReporter);
 
   mBaseURI = aBaseURI;
   mSheetURI = aSheetURI;
   mSheetPrincipal = aSheetPrincipal;
   mHavePushBack = false;
-  mDomCallerType = aCallerType;
+  mIsChrome =
+    aCallerType == dom::CallerType::System ||
+    (aSheetURI && dom::IsChromeURI(aSheetURI));
 }
 
 void
 CSSParserImpl::ReleaseScanner()
 {
   mScanner = nullptr;
   mReporter = nullptr;
   mBaseURI = nullptr;
   mSheetURI = nullptr;
   mSheetPrincipal = nullptr;
-  mDomCallerType = dom::CallerType::NonSystem;
+  mIsChrome = false;
 }
 
 nsresult
 CSSParserImpl::ParseSheet(const nsAString& aInput,
                           nsIURI*          aSheetURI,
                           nsIURI*          aBaseURI,
                           nsIPrincipal*    aSheetPrincipal,
                           uint32_t         aLineNumber,
@@ -1627,17 +1626,16 @@ CSSParserImpl::ParseSheet(const nsAStrin
       }
     }
   }
   else {
     mSection = eCSSSection_Charset; // sheet is empty, any rules are fair
   }
 
   mParsingMode = mSheet->ParsingMode();
-  mIsChrome = dom::IsChromeURI(aSheetURI);
   mReusableSheets = aReusableSheets;
 
   nsCSSToken* tk = &mToken;
   for (;;) {
     // Get next non-whitespace token
     if (!GetToken(true)) {
       OUTPUT_ERROR();
       break;
@@ -1655,17 +1653,16 @@ CSSParserImpl::ParseSheet(const nsAStrin
     }
   }
 
   mSheet->SetSourceMapURLFromComment(scanner.GetSourceMapURL());
   mSheet->SetSourceURL(scanner.GetSourceURL());
   ReleaseScanner();
 
   mParsingMode = css::eAuthorSheetFeatures;
-  mIsChrome = false;
   mReusableSheets = nullptr;
 
   return NS_OK;
 }
 
 /**
  * Determines whether the identifier contained in the given string is a
  * vendor-specific identifier, as described in CSS 2.1 section 4.1.2.1.
--- a/servo/ports/geckolib/glue.rs
+++ b/servo/ports/geckolib/glue.rs
@@ -73,17 +73,17 @@ use style::gecko_bindings::bindings::Raw
 use style::gecko_bindings::bindings::RawServoDeclarationBlockBorrowedOrNull;
 use style::gecko_bindings::bindings::RawServoStyleRuleBorrowed;
 use style::gecko_bindings::bindings::RawServoStyleSet;
 use style::gecko_bindings::bindings::ServoStyleContextBorrowedOrNull;
 use style::gecko_bindings::bindings::nsTArrayBorrowed_uintptr_t;
 use style::gecko_bindings::bindings::nsTimingFunctionBorrowed;
 use style::gecko_bindings::bindings::nsTimingFunctionBorrowedMut;
 use style::gecko_bindings::structs;
-use style::gecko_bindings::structs::{CSSPseudoElementType, CompositeOperation};
+use style::gecko_bindings::structs::{CallerType, CSSPseudoElementType, CompositeOperation};
 use style::gecko_bindings::structs::{Loader, LoaderReusableStyleSheets};
 use style::gecko_bindings::structs::{RawServoStyleRule, ServoStyleContextStrong, RustString};
 use style::gecko_bindings::structs::{ServoStyleSheet, SheetParsingMode, nsAtom, nsCSSPropertyID};
 use style::gecko_bindings::structs::{nsCSSFontFaceRule, nsCSSCounterStyleRule};
 use style::gecko_bindings::structs::{nsRestyleHint, nsChangeHint, PropertyValuePair};
 use style::gecko_bindings::structs::AtomArray;
 use style::gecko_bindings::structs::IterationCompositeOperation;
 use style::gecko_bindings::structs::MallocSizeOf as GeckoMallocSizeOf;
@@ -2735,26 +2735,51 @@ pub extern "C" fn Servo_DeclarationBlock
 #[no_mangle]
 pub extern "C" fn Servo_MediaList_GetText(list: RawServoMediaListBorrowed, result: *mut nsAString) {
     read_locked_arc(list, |list: &MediaList| {
         list.to_css(unsafe { result.as_mut().unwrap() }).unwrap();
     })
 }
 
 #[no_mangle]
-pub extern "C" fn Servo_MediaList_SetText(list: RawServoMediaListBorrowed, text: *const nsACString) {
-    let text = unsafe { text.as_ref().unwrap().as_str_unchecked() };
+pub unsafe extern "C" fn Servo_MediaList_SetText(
+    list: RawServoMediaListBorrowed,
+    text: *const nsACString,
+    caller_type: CallerType,
+) {
+    let text = (*text).as_str_unchecked();
+
     let mut input = ParserInput::new(&text);
     let mut parser = Parser::new(&mut input);
-    let url_data = unsafe { dummy_url_data() };
-    let context = ParserContext::new_for_cssom(url_data, Some(CssRuleType::Media),
-                                               PARSING_MODE_DEFAULT,
-                                               QuirksMode::NoQuirks);
-     write_locked_arc(list, |list: &mut MediaList| {
-        *list = parse_media_query_list(&context, &mut parser, &NullReporter);
+    let url_data = dummy_url_data();
+
+    // TODO(emilio): If the need for `CallerType` appears in more places,
+    // consider adding an explicit member in `ParserContext` instead of doing
+    // this (or adding a dummy "chrome://" url data).
+    //
+    // For media query parser it's effectively the same, so
+    let origin = match caller_type {
+        CallerType::System => Origin::UserAgent,
+        CallerType::NonSystem => Origin::Author,
+    };
+
+    let context = ParserContext::new(
+        origin,
+        url_data,
+        Some(CssRuleType::Media),
+        PARSING_MODE_DEFAULT,
+        QuirksMode::NoQuirks,
+    );
+
+    write_locked_arc(list, |list: &mut MediaList| {
+        *list = parse_media_query_list(
+            &context,
+            &mut parser,
+            &NullReporter,
+        );
     })
 }
 
 #[no_mangle]
 pub extern "C" fn Servo_MediaList_GetLength(list: RawServoMediaListBorrowed) -> u32 {
     read_locked_arc(list, |list: &MediaList| list.media_queries.len() as u32)
 }