Bug 1292432 part 6 - Make StyleSheet implement GetCssRules/InsertRule/DeleteRule. r?heycam draft
authorXidorn Quan <me@upsuper.org>
Tue, 04 Oct 2016 15:58:38 +1100
changeset 423110 c74cdd5b46c00501503de663ee29cd3410f1baf6
parent 423109 7a4f37d8084da8cc6d645723fde6f23bb63bb10f
child 423111 63ff49ab688be00e6a06e5c68c56c5a4dc767aae
push id31799
push userxquan@mozilla.com
push dateMon, 10 Oct 2016 07:37:57 +0000
reviewersheycam
bugs1292432
milestone52.0a1
Bug 1292432 part 6 - Make StyleSheet implement GetCssRules/InsertRule/DeleteRule. r?heycam MozReview-Commit-ID: KD552jWZnA3
layout/style/CSSStyleSheet.cpp
layout/style/CSSStyleSheet.h
layout/style/ServoStyleSheet.cpp
layout/style/ServoStyleSheet.h
layout/style/StyleSheet.cpp
layout/style/StyleSheet.h
--- a/layout/style/CSSStyleSheet.cpp
+++ b/layout/style/CSSStyleSheet.cpp
@@ -1677,53 +1677,35 @@ CSSStyleSheet::GetCssRules(nsIDOMCSSRule
   ErrorResult rv;
   nsCOMPtr<nsIDOMCSSRuleList> rules =
     GetCssRules(Some(nsContentUtils::SubjectPrincipal()), rv);
   rules.forget(aCssRules);
   return rv.StealNSResult();
 }
 
 CSSRuleList*
-CSSStyleSheet::GetCssRules(const Maybe<nsIPrincipal*>& aSubjectPrincipal,
-                           ErrorResult& aRv)
+CSSStyleSheet::GetCssRulesInternal(ErrorResult& aRv)
 {
-  if (!AreRulesAvailable(aSubjectPrincipal, aRv)) {
-    return nullptr;
-  }
-
-  // OK, security check passed, so get the rule collection
   if (!mRuleCollection) {
     mRuleCollection = new CSSRuleListImpl(this);
   }
-
   return mRuleCollection;
 }
 
 NS_IMETHODIMP    
 CSSStyleSheet::InsertRule(const nsAString& aRule,
                           uint32_t aIndex,
                           uint32_t* aReturn)
 {
   ErrorResult rv;
   *aReturn =
     InsertRule(aRule, aIndex, Some(nsContentUtils::SubjectPrincipal()), rv);
   return rv.StealNSResult();
 }
 
-uint32_t
-CSSStyleSheet::InsertRule(const nsAString& aRule, uint32_t aIndex,
-                          const Maybe<nsIPrincipal*>& aSubjectPrincipal,
-                          ErrorResult& aRv)
-{
-  if (!AreRulesAvailable(aSubjectPrincipal, aRv)) {
-    return 0;
-  }
-  return InsertRuleInternal(aRule, aIndex, aRv);
-}
-
 static bool
 RuleHasPendingChildSheet(css::Rule *cssRule)
 {
   nsCOMPtr<nsIDOMCSSImportRule> importRule(do_QueryInterface(cssRule));
   NS_ASSERTION(importRule, "Rule which has type IMPORT_RULE and does not implement nsIDOMCSSImportRule!");
   nsCOMPtr<nsIDOMCSSStyleSheet> childSheet;
   importRule->GetStyleSheet(getter_AddRefs(childSheet));
   RefPtr<CSSStyleSheet> cssSheet = do_QueryObject(childSheet);
@@ -1854,24 +1836,18 @@ NS_IMETHODIMP
 CSSStyleSheet::DeleteRule(uint32_t aIndex)
 {
   ErrorResult rv;
   DeleteRule(aIndex, Some(nsContentUtils::SubjectPrincipal()), rv);
   return rv.StealNSResult();
 }
 
 void
-CSSStyleSheet::DeleteRule(uint32_t aIndex,
-                          const Maybe<nsIPrincipal*>& aSubjectPrincipal,
-                          ErrorResult& aRv)
+CSSStyleSheet::DeleteRuleInternal(uint32_t aIndex, ErrorResult& aRv)
 {
-  if (!AreRulesAvailable(aSubjectPrincipal, aRv)) {
-    return;
-  }
-
   // XXX TBI: handle @rule types
   mozAutoDocUpdate updateBatch(mDocument, UPDATE_STYLE, true);
     
   WillDirty();
 
   if (aIndex >= uint32_t(mInner->mOrderedRules.Count())) {
     aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
     return;
--- a/layout/style/CSSStyleSheet.h
+++ b/layout/style/CSSStyleSheet.h
@@ -179,22 +179,16 @@ public:
   }
 
   nsresult AddRuleProcessor(nsCSSRuleProcessor* aProcessor);
   nsresult DropRuleProcessor(nsCSSRuleProcessor* aProcessor);
 
   void AddStyleSet(nsStyleSet* aStyleSet);
   void DropStyleSet(nsStyleSet* aStyleSet);
 
-  /**
-   * Like the DOM insertRule() method, but doesn't do any security checks
-   */
-  uint32_t InsertRuleInternal(const nsAString& aRule,
-                              uint32_t aIndex, ErrorResult& aRv);
-
   // nsICSSLoaderObserver interface
   NS_IMETHOD StyleSheetLoaded(StyleSheet* aSheet, bool aWasAlternate,
                               nsresult aStatus) override;
 
   void EnsureUniqueInner();
 
   // Append all of this sheet's child sheets to aArray.
   void AppendAllChildSheets(nsTArray<CSSStyleSheet*>& aArray);
@@ -229,25 +223,19 @@ public:
   CSSStyleSheet* GetParentStyleSheet() const { return mParent; }
   nsMediaList* Media();
 
   // WebIDL CSSStyleSheet API
   // Can't be inline because we can't include ImportRule here.  And can't be
   // called GetOwnerRule because that would be ambiguous with the ImportRule
   // version.
   nsIDOMCSSRule* GetDOMOwnerRule() const;
-  dom::CSSRuleList* GetCssRules(const Maybe<nsIPrincipal*>& aSubjectPrincipal,
-                                ErrorResult& aRv);
-  uint32_t InsertRule(const nsAString& aRule, uint32_t aIndex,
-                      const Maybe<nsIPrincipal*>& aSubjectPrincipal,
-                      ErrorResult& aRv);
-
-  void DeleteRule(uint32_t aIndex,
-                  const Maybe<nsIPrincipal*>& aSubjectPrincipal,
-                  ErrorResult& aRv);
+  using StyleSheet::GetCssRules;
+  using StyleSheet::InsertRule;
+  using StyleSheet::DeleteRule;
 
   // WebIDL miscellaneous bits
   dom::ParentObject GetParentObject() const {
     if (mOwningNode) {
       return dom::ParentObject(mOwningNode);
     }
 
     return dom::ParentObject(static_cast<nsIDOMCSSStyleSheet*>(mParent), mParent);
@@ -282,16 +270,22 @@ protected:
   void DropMedia();
 
   // Unlink our inner, if needed, for cycle collection
   void UnlinkInner();
   // Traverse our inner, if needed, for cycle collection
   void TraverseInner(nsCycleCollectionTraversalCallback &);
 
 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);
+
   RefPtr<nsMediaList> mMedia;
   RefPtr<CSSStyleSheet> mNext;
   CSSStyleSheet*        mParent;    // weak ref
   css::ImportRule*      mOwnerRule; // weak ref
 
   RefPtr<CSSRuleListImpl> mRuleCollection;
   bool                  mDirty; // has been modified 
   bool                  mInRuleProcessorCache;
--- a/layout/style/ServoStyleSheet.cpp
+++ b/layout/style/ServoStyleSheet.cpp
@@ -148,9 +148,30 @@ ServoStyleSheet::InsertRule(const nsAStr
 }
 
 nsresult
 ServoStyleSheet::DeleteRule(uint32_t aIndex)
 {
   return NS_ERROR_NOT_IMPLEMENTED;
 }
 
+CSSRuleList*
+ServoStyleSheet::GetCssRulesInternal(ErrorResult& aRv)
+{
+  aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
+  return nullptr;
+}
+
+uint32_t
+ServoStyleSheet::InsertRuleInternal(const nsAString& aRule,
+                                    uint32_t aIndex, ErrorResult& aRv)
+{
+  aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
+  return 0;
+}
+
+void
+ServoStyleSheet::DeleteRuleInternal(uint32_t aIndex, ErrorResult& aRv)
+{
+  aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
+}
+
 } // namespace mozilla
--- a/layout/style/ServoStyleSheet.h
+++ b/layout/style/ServoStyleSheet.h
@@ -59,17 +59,25 @@ public:
   NS_DECL_NSIDOMCSSSTYLESHEET
 
   void WillDirty() {}
   void DidDirty() {}
 
 protected:
   ~ServoStyleSheet();
 
+  // 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);
+
 private:
   void DropSheet();
 
   RefPtr<RawServoStyleSheet> mSheet;
+
+  friend class StyleSheet;
 };
 
 } // namespace mozilla
 
 #endif // mozilla_ServoStyleSheet_h
--- a/layout/style/StyleSheet.cpp
+++ b/layout/style/StyleSheet.cpp
@@ -130,16 +130,58 @@ StyleSheet::GetHref(nsAString& aHref)
 
 NS_IMETHODIMP
 StyleSheet::GetTitle(nsAString& aTitle)
 {
   aTitle.Assign(mTitle);
   return NS_OK;
 }
 
+// WebIDL CSSStyleSheet API
+
+#define FORWARD_INTERNAL(method_, args_) \
+  if (IsServo()) { \
+    return AsServo()->method_ args_; \
+  } \
+  return AsGecko()->method_ args_;
+
+dom::CSSRuleList*
+StyleSheet::GetCssRules(const Maybe<nsIPrincipal*>& aSubjectPrincipal,
+                        ErrorResult& aRv)
+{
+  if (!AreRulesAvailable(aSubjectPrincipal, aRv)) {
+    return nullptr;
+  }
+  FORWARD_INTERNAL(GetCssRulesInternal, (aRv))
+}
+
+uint32_t
+StyleSheet::InsertRule(const nsAString& aRule, uint32_t aIndex,
+                       const Maybe<nsIPrincipal*>& aSubjectPrincipal,
+                       ErrorResult& aRv)
+{
+  if (!AreRulesAvailable(aSubjectPrincipal, aRv)) {
+    return 0;
+  }
+  FORWARD_INTERNAL(InsertRuleInternal, (aRule, aIndex, aRv))
+}
+
+void
+StyleSheet::DeleteRule(uint32_t aIndex,
+                       const Maybe<nsIPrincipal*>& aSubjectPrincipal,
+                       ErrorResult& aRv)
+{
+  if (!AreRulesAvailable(aSubjectPrincipal, aRv)) {
+    return;
+  }
+  FORWARD_INTERNAL(DeleteRuleInternal, (aIndex, aRv))
+}
+
+#undef FORWARD_INTERNAL
+
 void
 StyleSheet::SubjectSubsumesInnerPrincipal(
   const Maybe<nsIPrincipal*>& aSubjectPrincipal, ErrorResult& aRv)
 {
   StyleSheetInfo& info = SheetInfo();
 
   MOZ_ASSERT(aSubjectPrincipal.isSome());
 
--- a/layout/style/StyleSheet.h
+++ b/layout/style/StyleSheet.h
@@ -21,16 +21,17 @@ class nsINode;
 namespace mozilla {
 
 class CSSStyleSheet;
 class ServoStyleSheet;
 class StyleSheetInfo;
 
 namespace dom {
 class SRIMetadata;
+class CSSRuleList;
 } // namespace dom
 
 /**
  * Superclass for data common to CSSStyleSheet and ServoStyleSheet.
  */
 class StyleSheet : public nsIDOMCSSStyleSheet
 {
 protected:
@@ -134,16 +135,26 @@ public:
   // WebIDL StyleSheet API
   // The XPCOM GetType is fine for WebIDL.
   // The XPCOM GetHref is fine for WebIDL
   // GetOwnerNode is defined above.
   // The XPCOM GetTitle is fine for WebIDL.
   bool Disabled() const { return mDisabled; }
   // The XPCOM SetDisabled is fine for WebIDL
 
+  // WebIDL CSSStyleSheet API
+  dom::CSSRuleList* GetCssRules(const Maybe<nsIPrincipal*>& aSubjectPrincipal,
+                                ErrorResult& aRv);
+  uint32_t InsertRule(const nsAString& aRule, uint32_t aIndex,
+                      const Maybe<nsIPrincipal*>& aSubjectPrincipal,
+                      ErrorResult& aRv);
+  void DeleteRule(uint32_t aIndex,
+                  const Maybe<nsIPrincipal*>& aSubjectPrincipal,
+                  ErrorResult& aRv);
+
   // nsIDOMStyleSheet interface
   NS_IMETHOD GetType(nsAString& aType) final;
   NS_IMETHOD GetDisabled(bool* aDisabled) final;
   NS_IMETHOD SetDisabled(bool aDisabled) final;
   NS_IMETHOD GetOwnerNode(nsIDOMNode** aOwnerNode) final;
   NS_IMETHOD GetHref(nsAString& aHref) final;
   NS_IMETHOD GetTitle(nsAString& aTitle) final;