Bug 1292432 part 6 - Make StyleSheet implement GetCssRules/InsertRule/DeleteRule. r=heycam
authorXidorn Quan <me@upsuper.org>
Fri, 14 Oct 2016 22:25:38 +1100
changeset 360860 c8cac9478593585d5e058dc87f202699fa3bcaaa
parent 360859 0366529b94f1ab614bdfd98b253b309944e00138
child 360861 2617bfd8c4d41d3c5c53de864b8dab0424bf3a3a
push id6795
push userjlund@mozilla.com
push dateMon, 23 Jan 2017 14:19:46 +0000
treeherdermozilla-beta@76101b503191 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersheycam
bugs1292432
milestone52.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
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(*nsContentUtils::SubjectPrincipal(), rv);
   rules.forget(aCssRules);
   return rv.StealNSResult();
 }
 
 CSSRuleList*
-CSSStyleSheet::GetCssRules(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, *nsContentUtils::SubjectPrincipal(), rv);
   return rv.StealNSResult();
 }
 
-uint32_t
-CSSStyleSheet::InsertRule(const nsAString& aRule, uint32_t aIndex,
-                          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, *nsContentUtils::SubjectPrincipal(), rv);
   return rv.StealNSResult();
 }
 
 void
-CSSStyleSheet::DeleteRule(uint32_t aIndex,
-                          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
@@ -26,17 +26,16 @@
 #include "mozilla/CORSMode.h"
 #include "nsCycleCollectionParticipant.h"
 #include "nsWrapperCache.h"
 #include "mozilla/net/ReferrerPolicy.h"
 #include "mozilla/dom/SRIMetadata.h"
 
 class CSSRuleListImpl;
 class nsCSSRuleProcessor;
-class nsIPrincipal;
 class nsIURI;
 class nsMediaList;
 class nsMediaQueryResultCacheKey;
 class nsStyleSet;
 class nsPresContext;
 class nsXMLNameSpaceMap;
 
 namespace mozilla {
@@ -174,22 +173,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);
@@ -224,25 +217,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(nsIPrincipal& aSubjectPrincipal,
-                                ErrorResult& aRv);
-  uint32_t InsertRule(const nsAString& aRule, uint32_t aIndex,
-                      nsIPrincipal& aSubjectPrincipal,
-                      ErrorResult& aRv);
-
-  void DeleteRule(uint32_t aIndex,
-                  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);
@@ -277,16 +264,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
@@ -58,16 +58,22 @@ 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;
   StyleSheetInfo mSheetInfo;
 
   friend class StyleSheet;
 };
--- a/layout/style/StyleSheet.cpp
+++ b/layout/style/StyleSheet.cpp
@@ -149,16 +149,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(nsIPrincipal& aSubjectPrincipal,
+                        ErrorResult& aRv)
+{
+  if (!AreRulesAvailable(aSubjectPrincipal, aRv)) {
+    return nullptr;
+  }
+  FORWARD_INTERNAL(GetCssRulesInternal, (aRv))
+}
+
+uint32_t
+StyleSheet::InsertRule(const nsAString& aRule, uint32_t aIndex,
+                       nsIPrincipal& aSubjectPrincipal,
+                       ErrorResult& aRv)
+{
+  if (!AreRulesAvailable(aSubjectPrincipal, aRv)) {
+    return 0;
+  }
+  FORWARD_INTERNAL(InsertRuleInternal, (aRule, aIndex, aRv))
+}
+
+void
+StyleSheet::DeleteRule(uint32_t aIndex,
+                       nsIPrincipal& aSubjectPrincipal,
+                       ErrorResult& aRv)
+{
+  if (!AreRulesAvailable(aSubjectPrincipal, aRv)) {
+    return;
+  }
+  FORWARD_INTERNAL(DeleteRuleInternal, (aIndex, aRv))
+}
+
+#undef FORWARD_INTERNAL
+
 void
 StyleSheet::SubjectSubsumesInnerPrincipal(nsIPrincipal& aSubjectPrincipal,
                                           ErrorResult& aRv)
 {
   StyleSheetInfo& info = SheetInfo();
 
   if (aSubjectPrincipal.Subsumes(info.mPrincipal)) {
     return;
@@ -189,17 +231,17 @@ StyleSheet::SubjectSubsumesInnerPrincipa
   WillDirty();
 
   info.mPrincipal = &aSubjectPrincipal;
 
   DidDirty();
 }
 
 bool
-StyleSheet::AreRulesAvailable(const Maybe<nsIPrincipal*>& aSubjectPrincipal,
+StyleSheet::AreRulesAvailable(nsIPrincipal& aSubjectPrincipal,
                               ErrorResult& aRv)
 {
   // Rules are not available on incomplete sheets.
   if (!SheetInfo().mComplete) {
     aRv.Throw(NS_ERROR_DOM_INVALID_ACCESS_ERR);
     return false;
   }
   //-- Security check: Only scripts whose principal subsumes that of the
--- a/layout/style/StyleSheet.h
+++ b/layout/style/StyleSheet.h
@@ -13,24 +13,26 @@
 #include "mozilla/StyleBackendType.h"
 #include "mozilla/CORSMode.h"
 #include "mozilla/ServoUtils.h"
 
 #include "nsIDOMCSSStyleSheet.h"
 
 class nsIDocument;
 class nsINode;
+class nsIPrincipal;
 
 namespace mozilla {
 
 class CSSStyleSheet;
 class ServoStyleSheet;
 struct StyleSheetInfo;
 
 namespace dom {
+class CSSRuleList;
 class SRIMetadata;
 } // namespace dom
 
 /**
  * Superclass for data common to CSSStyleSheet and ServoStyleSheet.
  */
 class StyleSheet : public nsIDOMCSSStyleSheet
 {
@@ -120,16 +122,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(nsIPrincipal& aSubjectPrincipal,
+                                ErrorResult& aRv);
+  uint32_t InsertRule(const nsAString& aRule, uint32_t aIndex,
+                      nsIPrincipal& aSubjectPrincipal,
+                      ErrorResult& aRv);
+  void DeleteRule(uint32_t aIndex,
+                  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;
 
@@ -144,17 +156,17 @@ private:
   // 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
   // completely loaded. aRv will have an exception set if this function
   // returns false.
-  bool AreRulesAvailable(const Maybe<nsIPrincipal*>& aSubjectPrincipal,
+  bool AreRulesAvailable(nsIPrincipal& aSubjectPrincipal,
                          ErrorResult& aRv);
 
 protected:
   // Return success if the subject principal subsumes the principal of our
   // inner, error otherwise.  This will also succeed if the subject has
   // UniversalXPConnect or if access is allowed by CORS.  In the latter case,
   // it will set the principal of the inner to the subject principal.
   void SubjectSubsumesInnerPrincipal(nsIPrincipal& aSubjectPrincipal,