Bug 1315601 part 13 - Move common code of DeleteRuleFromGroup/InsertRuleIntoGroup from CSSStyleSheet to StyleSheet. r=heycam draft
authorXidorn Quan <me@upsuper.org>
Wed, 08 Mar 2017 18:04:15 +1100
changeset 497349 379cfa5390c2235b56d9539ec85d9bf821b8f209
parent 497348 39815c7f1c4ab65b32fa5b41c4231c3e76661628
child 497350 dadf70886038ae4d1d62945ee4f4fbc4a3e756aa
push id48869
push userxquan@mozilla.com
push dateMon, 13 Mar 2017 06:50:23 +0000
reviewersheycam
bugs1315601
milestone55.0a1
Bug 1315601 part 13 - Move common code of DeleteRuleFromGroup/InsertRuleIntoGroup from CSSStyleSheet to StyleSheet. r=heycam MozReview-Commit-ID: BDxnYb0OkCk
layout/style/CSSStyleSheet.cpp
layout/style/CSSStyleSheet.h
layout/style/GroupRule.cpp
layout/style/StyleSheet.cpp
layout/style/StyleSheet.h
--- a/layout/style/CSSStyleSheet.cpp
+++ b/layout/style/CSSStyleSheet.cpp
@@ -896,72 +896,30 @@ CSSStyleSheet::DeleteRuleInternal(uint32
 
     if (mDocument) {
       mDocument->StyleRuleRemoved(this, rule);
     }
   }
 }
 
 nsresult
-CSSStyleSheet::DeleteRuleFromGroup(css::GroupRule* aGroup, uint32_t aIndex)
+CSSStyleSheet::InsertRuleIntoGroupInternal(const nsAString& aRule,
+                                           css::GroupRule* aGroup,
+                                           uint32_t aIndex)
 {
-  NS_ENSURE_ARG_POINTER(aGroup);
-  NS_ASSERTION(mInner->mComplete, "No deleting from an incomplete sheet!");
-  RefPtr<css::Rule> rule = aGroup->GetStyleRuleAt(aIndex);
-  NS_ENSURE_TRUE(rule, NS_ERROR_ILLEGAL_VALUE);
-
-  // check that the rule actually belongs to this sheet!
-  if (this != rule->GetStyleSheet()) {
-    return NS_ERROR_INVALID_ARG;
-  }
-
-  mozAutoDocUpdate updateBatch(mDocument, UPDATE_STYLE, true);
-  
-  WillDirty();
-
-  nsresult result = aGroup->DeleteStyleRuleAt(aIndex);
-  NS_ENSURE_SUCCESS(result, result);
-  
-  rule->SetStyleSheet(nullptr);
-  
-  DidDirty();
-
-  if (mDocument) {
-    mDocument->StyleRuleRemoved(this, rule);
-  }
-
-  return NS_OK;
-}
-
-nsresult
-CSSStyleSheet::InsertRuleIntoGroup(const nsAString & aRule,
-                                   css::GroupRule* aGroup,
-                                   uint32_t aIndex)
-{
-  NS_ASSERTION(mInner->mComplete, "No inserting into an incomplete sheet!");
-  // check that the group actually belongs to this sheet!
-  if (this != aGroup->GetStyleSheet()) {
-    return NS_ERROR_INVALID_ARG;
-  }
-
   // Hold strong ref to the CSSLoader in case the document update
   // kills the document
   RefPtr<css::Loader> loader;
   if (mDocument) {
     loader = mDocument->CSSLoader();
     NS_ASSERTION(loader, "Document with no CSS loader!");
   }
 
   nsCSSParser css(loader, this);
 
-  // parse and grab the rule
-  mozAutoDocUpdate updateBatch(mDocument, UPDATE_STYLE, true);
-
-  WillDirty();
-
   RefPtr<css::Rule> rule;
   nsresult result = css.ParseRule(aRule, mInner->mSheetURI, mInner->mBaseURI,
                                   mInner->mPrincipal, getter_AddRefs(rule));
   if (NS_FAILED(result))
     return result;
 
   switch (rule->GetType()) {
     case css::Rule::STYLE_RULE:
@@ -979,25 +937,17 @@ CSSStyleSheet::InsertRuleIntoGroup(const
     case css::Rule::NAMESPACE_RULE:
       // these aren't
       return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR;
     default:
       NS_NOTREACHED("unexpected rule type");
       return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR;
   }
 
-  result = aGroup->InsertStyleRuleAt(aIndex, rule);
-  NS_ENSURE_SUCCESS(result, result);
-  DidDirty();
-
-  if (mDocument) {
-    mDocument->StyleRuleAdded(this, rule);
-  }
-
-  return NS_OK;
+  return aGroup->InsertStyleRuleAt(aIndex, rule);
 }
 
 // nsICSSLoaderObserver implementation
 NS_IMETHODIMP
 CSSStyleSheet::StyleSheetLoaded(StyleSheet* aSheet,
                                 bool aWasAlternate,
                                 nsresult aStatus)
 {
--- a/layout/style/CSSStyleSheet.h
+++ b/layout/style/CSSStyleSheet.h
@@ -111,19 +111,16 @@ public:
 #endif
 
   // XXX do these belong here or are they generic?
   void AppendStyleRule(css::Rule* aRule);
 
   int32_t StyleRuleCount() const;
   css::Rule* GetStyleRuleAt(int32_t aIndex) const;
 
-  nsresult DeleteRuleFromGroup(css::GroupRule* aGroup, uint32_t aIndex);
-  nsresult InsertRuleIntoGroup(const nsAString& aRule, css::GroupRule* aGroup, uint32_t aIndex);
-
   void SetOwnerRule(css::ImportRule* aOwnerRule) { mOwnerRule = aOwnerRule; /* Not ref counted */ }
   css::ImportRule* GetOwnerRule() const { return mOwnerRule; }
   // Workaround overloaded-virtual warning in GCC.
   using StyleSheet::GetOwnerRule;
 
   nsXMLNameSpaceMap* GetNameSpaceMap() const {
     return Inner()->mNameSpaceMap;
   }
@@ -217,16 +214,19 @@ protected:
   virtual void TraverseInner(nsCycleCollectionTraversalCallback &) override;
 
 protected:
   // Internal methods which do not have security check and completeness check.
   dom::CSSRuleList* GetCssRulesInternal(ErrorResult& aRv);
   uint32_t InsertRuleInternal(const nsAString& aRule,
                               uint32_t aIndex, ErrorResult& aRv);
   void DeleteRuleInternal(uint32_t aIndex, ErrorResult& aRv);
+  nsresult InsertRuleIntoGroupInternal(const nsAString& aRule,
+                                       css::GroupRule* aGroup,
+                                       uint32_t aIndex);
 
   void EnabledStateChangedInternal();
 
   css::ImportRule*      mOwnerRule; // weak ref
 
   RefPtr<CSSRuleListImpl> mRuleCollection;
   bool                  mDirty; // has been modified 
   bool                  mInRuleProcessorCache;
--- a/layout/style/GroupRule.cpp
+++ b/layout/style/GroupRule.cpp
@@ -363,18 +363,17 @@ GroupRule::InsertRule(const nsAString& a
   uint32_t count = StyleRuleCount();
   if (aIndex > count) {
     aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
     return 0;
   }
 
   NS_ASSERTION(count <= INT32_MAX, "Too many style rules!");
 
-  nsresult rv =
-    sheet->AsGecko()->InsertRuleIntoGroup(aRule, this, aIndex);
+  nsresult rv = sheet->InsertRuleIntoGroup(aRule, this, aIndex);
   if (NS_FAILED(rv)) {
     aRv.Throw(rv);
     return 0;
   }
   return aIndex;
 }
 
 nsresult
@@ -397,17 +396,17 @@ GroupRule::DeleteRule(uint32_t aIndex, E
   uint32_t count = StyleRuleCount();
   if (aIndex >= count) {
     aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
     return;
   }
 
   NS_ASSERTION(count <= INT32_MAX, "Too many style rules!");
 
-  nsresult rv = sheet->AsGecko()->DeleteRuleFromGroup(this, aIndex);
+  nsresult rv = sheet->DeleteRuleFromGroup(this, aIndex);
   if (NS_FAILED(rv)) {
     aRv.Throw(rv);
   }
 }
 
 #undef CALL_INNER
 
 } // namespace css
--- a/layout/style/StyleSheet.cpp
+++ b/layout/style/StyleSheet.cpp
@@ -417,16 +417,81 @@ StyleSheet::DeleteRule(uint32_t aIndex,
                        ErrorResult& aRv)
 {
   if (!AreRulesAvailable(aSubjectPrincipal, aRv)) {
     return;
   }
   FORWARD_INTERNAL(DeleteRuleInternal, (aIndex, aRv))
 }
 
+nsresult
+StyleSheet::DeleteRuleFromGroup(css::GroupRule* aGroup, uint32_t aIndex)
+{
+  NS_ENSURE_ARG_POINTER(aGroup);
+  NS_ASSERTION(IsComplete(), "No deleting from an incomplete sheet!");
+  RefPtr<css::Rule> rule = aGroup->GetStyleRuleAt(aIndex);
+  NS_ENSURE_TRUE(rule, NS_ERROR_ILLEGAL_VALUE);
+
+  // check that the rule actually belongs to this sheet!
+  if (this != rule->GetStyleSheet()) {
+    return NS_ERROR_INVALID_ARG;
+  }
+
+  mozAutoDocUpdate updateBatch(mDocument, UPDATE_STYLE, true);
+
+  WillDirty();
+
+  nsresult result = aGroup->DeleteStyleRuleAt(aIndex);
+  NS_ENSURE_SUCCESS(result, result);
+
+  rule->SetStyleSheet(nullptr);
+
+  DidDirty();
+
+  if (mDocument) {
+    mDocument->StyleRuleRemoved(this, rule);
+  }
+
+  return NS_OK;
+}
+
+nsresult
+StyleSheet::InsertRuleIntoGroup(const nsAString& aRule,
+                                css::GroupRule* aGroup,
+                                uint32_t aIndex)
+{
+  NS_ASSERTION(IsComplete(), "No inserting into an incomplete sheet!");
+  // check that the group actually belongs to this sheet!
+  if (this != aGroup->GetStyleSheet()) {
+    return NS_ERROR_INVALID_ARG;
+  }
+
+  // parse and grab the rule
+  mozAutoDocUpdate updateBatch(mDocument, UPDATE_STYLE, true);
+
+  WillDirty();
+
+  nsresult result;
+  if (IsGecko()) {
+    result = AsGecko()->InsertRuleIntoGroupInternal(aRule, aGroup, aIndex);
+  } else {
+    // TODO
+    result = NS_ERROR_NOT_IMPLEMENTED;
+  }
+  NS_ENSURE_SUCCESS(result, result);
+
+  DidDirty();
+
+  if (mDocument) {
+    mDocument->StyleRuleAdded(this, aGroup->GetStyleRuleAt(aIndex));
+  }
+
+  return NS_OK;
+}
+
 void
 StyleSheet::EnabledStateChanged()
 {
   FORWARD_INTERNAL(EnabledStateChangedInternal, ())
 }
 
 #undef FORWARD_INTERNAL
 
--- a/layout/style/StyleSheet.h
+++ b/layout/style/StyleSheet.h
@@ -31,16 +31,17 @@ struct CSSStyleSheetInner;
 
 namespace dom {
 class CSSRuleList;
 class MediaList;
 class SRIMetadata;
 } // namespace dom
 
 namespace css {
+class GroupRule;
 class ImportRule;
 class Rule;
 }
 
 /**
  * Superclass for data common to CSSStyleSheet and ServoStyleSheet.
  */
 class StyleSheet : public nsIDOMCSSStyleSheet
@@ -207,16 +208,20 @@ public:
   NS_IMETHOD DeleteRule(uint32_t aIndex) final;
 
   // Changes to sheets should be inside of a WillDirty-DidDirty pair.
   // However, the calls do not need to be matched; it's ok to call
   // WillDirty and then make no change and skip the DidDirty call.
   inline void WillDirty();
   inline void DidDirty();
 
+  nsresult DeleteRuleFromGroup(css::GroupRule* aGroup, uint32_t aIndex);
+  nsresult InsertRuleIntoGroup(const nsAString& aRule,
+                               css::GroupRule* aGroup, uint32_t aIndex);
+
 private:
   // Get a handle to the various stylesheet bits which live on the 'inner' for
   // gecko stylesheets and live on the StyleSheet for Servo stylesheets.
   inline StyleSheetInfo& SheetInfo();
   inline const StyleSheetInfo& SheetInfo() const;
 
   // Check if the rules are available for read and write.
   // It does the security check as well as whether the rules have been