Bug 1452143: Make InspectorUtils.getAllStyleSheets handle Shadow DOM, and also optionally not return UA / User sheets. r?bholley draft
authorEmilio Cobos Álvarez <emilio@crisal.io>
Mon, 16 Apr 2018 19:10:57 +0200
changeset 783197 f70ff2be9eef3ecf2e8315e8a9385de1345a6057
parent 783196 54edb6260b0b253a238f7d46ae0d66b93a1721c9
child 783198 86f5325f547fcba5ca180871863d9253dcff6c62
push id106640
push userbmo:emilio@crisal.io
push dateMon, 16 Apr 2018 19:43:11 +0000
reviewersbholley
bugs1452143
milestone61.0a1
Bug 1452143: Make InspectorUtils.getAllStyleSheets handle Shadow DOM, and also optionally not return UA / User sheets. r?bholley We don't want to reparse over and over shared sheets, and that confused code pretty heavily. MozReview-Commit-ID: 7qkXoCoPNFW
dom/chrome-webidl/InspectorUtils.webidl
layout/inspector/InspectorUtils.cpp
layout/inspector/InspectorUtils.h
layout/style/ErrorReporter.cpp
layout/style/ServoStyleSet.cpp
layout/style/ServoStyleSet.h
--- a/dom/chrome-webidl/InspectorUtils.webidl
+++ b/dom/chrome-webidl/InspectorUtils.webidl
@@ -6,17 +6,18 @@
 
 /**
  * A collection of utility methods for use by devtools.
  *
  * See InspectorUtils.h for documentation on these methods.
  */
 [ChromeOnly]
 namespace InspectorUtils {
-  sequence<StyleSheet> getAllStyleSheets(Document document);
+  // documentOnly tells whether user and UA sheets should get included.
+  sequence<StyleSheet> getAllStyleSheets(Document document, optional boolean documentOnly = false);
   sequence<CSSRule> getCSSStyleRules(
     Element element,
     [TreatNullAs=EmptyString] optional DOMString pseudo = "");
   unsigned long getRuleLine(CSSRule rule);
   unsigned long getRuleColumn(CSSRule rule);
   unsigned long getRelativeRuleLine(CSSRule rule);
   boolean hasRulesModifiedByCSSOM(CSSStyleSheet sheet);
   [NewObject] CSSLexer getCSSLexer(DOMString text);
--- a/layout/inspector/InspectorUtils.cpp
+++ b/layout/inspector/InspectorUtils.cpp
@@ -56,36 +56,42 @@ using namespace mozilla::dom;
 extern const char* const kCSSRawProperties[];
 
 namespace mozilla {
 namespace dom {
 
 /* static */ void
 InspectorUtils::GetAllStyleSheets(GlobalObject& aGlobalObject,
                                   nsIDocument& aDocument,
+                                  bool aDocumentOnly,
                                   nsTArray<RefPtr<StyleSheet>>& aResult)
 {
   // Get the agent, then user and finally xbl sheets in the style set.
   nsIPresShell* presShell = aDocument.GetShell();
 
   if (presShell) {
     ServoStyleSet* styleSet = presShell->StyleSet();
-    SheetType sheetType = SheetType::Agent;
-    for (int32_t i = 0; i < styleSet->SheetCount(sheetType); i++) {
-      aResult.AppendElement(styleSet->StyleSheetAt(sheetType, i));
-    }
-    sheetType = SheetType::User;
-    for (int32_t i = 0; i < styleSet->SheetCount(sheetType); i++) {
-      aResult.AppendElement(styleSet->StyleSheetAt(sheetType, i));
+
+    if (!aDocumentOnly) {
+      SheetType sheetType = SheetType::Agent;
+      for (int32_t i = 0; i < styleSet->SheetCount(sheetType); i++) {
+        aResult.AppendElement(styleSet->StyleSheetAt(sheetType, i));
+      }
+      sheetType = SheetType::User;
+      for (int32_t i = 0; i < styleSet->SheetCount(sheetType); i++) {
+        aResult.AppendElement(styleSet->StyleSheetAt(sheetType, i));
+      }
     }
 
     AutoTArray<StyleSheet*, 32> xblSheetArray;
-    styleSet->AppendAllXBLStyleSheets(xblSheetArray);
+    styleSet->AppendAllNonDocumentAuthorSheets(xblSheetArray);
 
     // The XBL stylesheet array will quite often be full of duplicates. Cope:
+    //
+    // FIXME(emilio, bug 1454467): I think this is not true since bug 1452525.
     nsTHashtable<nsPtrHashKey<StyleSheet>> sheetSet;
     for (StyleSheet* sheet : xblSheetArray) {
       if (!sheetSet.Contains(sheet)) {
         sheetSet.PutEntry(sheet);
         aResult.AppendElement(sheet);
       }
     }
   }
@@ -1042,26 +1048,18 @@ InspectorUtils::ClearPseudoClassLocks(Gl
 
 /* static */ void
 InspectorUtils::ParseStyleSheet(GlobalObject& aGlobalObject,
                                 StyleSheet& aSheet,
                                 const nsAString& aInput,
                                 ErrorResult& aRv)
 {
 
-  RefPtr<ServoStyleSheet> servoSheet = do_QueryObject(&aSheet);
-  if (servoSheet) {
-    nsresult rv = servoSheet->ReparseSheet(aInput);
-    if (NS_FAILED(rv)) {
-      aRv.Throw(rv);
-    }
-    return;
-  }
-
-  aRv.Throw(NS_ERROR_INVALID_POINTER);
+  RefPtr<ServoStyleSheet> servoSheet = aSheet.AsServo();
+  aRv = servoSheet->ReparseSheet(aInput);
 }
 
 void
 InspectorUtils::ScrollElementIntoView(GlobalObject& aGlobalObject,
                                       Element& aElement)
 {
   nsIPresShell* presShell = aElement.OwnerDoc()->GetShell();
   if (!presShell) {
--- a/layout/inspector/InspectorUtils.h
+++ b/layout/inspector/InspectorUtils.h
@@ -32,16 +32,17 @@ namespace dom {
 /**
  * A collection of utility methods for use by devtools.
  */
 class InspectorUtils
 {
 public:
   static void GetAllStyleSheets(GlobalObject& aGlobal,
                                 nsIDocument& aDocument,
+                                bool aDocumentOnly,
                                 nsTArray<RefPtr<StyleSheet>>& aResult);
   static void GetCSSStyleRules(GlobalObject& aGlobal,
                                Element& aElement,
                                const nsAString& aPseudo,
                                nsTArray<RefPtr<css::Rule>>& aResult);
 
   /**
    * Get the line number of a rule.
--- a/layout/style/ErrorReporter.cpp
+++ b/layout/style/ErrorReporter.cpp
@@ -141,16 +141,17 @@ ErrorReporter::ErrorReporter(const Style
   , mErrorLineNumber(0)
   , mPrevErrorLineNumber(0)
   , mErrorColNumber(0)
 {
 }
 
 ErrorReporter::~ErrorReporter()
 {
+  MOZ_ASSERT(NS_IsMainThread());
   // Schedule deferred cleanup for cached data. We want to strike a
   // balance between performance and memory usage, so we only allow
   // short-term caching.
   if (sSpecCache && sSpecCache->IsInUse() && !sSpecCache->IsPending()) {
     nsCOMPtr<nsIRunnable> runnable(sSpecCache);
     nsresult rv =
       SystemGroup::Dispatch(TaskCategory::Other, runnable.forget());
     if (NS_FAILED(rv)) {
--- a/layout/style/ServoStyleSet.cpp
+++ b/layout/style/ServoStyleSet.cpp
@@ -864,20 +864,25 @@ ServoStyleSet::SheetCount(SheetType aTyp
 ServoStyleSheet*
 ServoStyleSet::StyleSheetAt(SheetType aType, int32_t aIndex) const
 {
   MOZ_ASSERT(IsCSSSheetType(aType));
   return mSheets[aType][aIndex];
 }
 
 void
-ServoStyleSet::AppendAllXBLStyleSheets(nsTArray<StyleSheet*>& aArray) const
+ServoStyleSet::AppendAllNonDocumentAuthorSheets(nsTArray<StyleSheet*>& aArray) const
 {
   if (mDocument) {
     mDocument->BindingManager()->AppendAllSheets(aArray);
+    EnumerateShadowRoots(*mDocument, [&](ShadowRoot& aShadowRoot) {
+      for (auto index : IntegerRange(aShadowRoot.SheetCount())) {
+        aArray.AppendElement(aShadowRoot.SheetAt(index));
+      }
+    });
   }
 }
 
 nsresult
 ServoStyleSet::RemoveDocStyleSheet(ServoStyleSheet* aSheet)
 {
   return RemoveStyleSheet(SheetType::Doc, aSheet);
 }
--- a/layout/style/ServoStyleSet.h
+++ b/layout/style/ServoStyleSet.h
@@ -239,17 +239,17 @@ public:
                          const nsTArray<RefPtr<ServoStyleSheet>>& aNewSheets);
   nsresult InsertStyleSheetBefore(SheetType aType,
                                   ServoStyleSheet* aNewSheet,
                                   ServoStyleSheet* aReferenceSheet);
 
   int32_t SheetCount(SheetType aType) const;
   ServoStyleSheet* StyleSheetAt(SheetType aType, int32_t aIndex) const;
 
-  void AppendAllXBLStyleSheets(nsTArray<StyleSheet*>& aArray) const;
+  void AppendAllNonDocumentAuthorSheets(nsTArray<StyleSheet*>& aArray) const;
 
   template<typename Func>
   void EnumerateStyleSheetArrays(Func aCallback) const {
     for (const auto& sheetArray : mSheets) {
       aCallback(sheetArray);
     }
   }