Bug 1292432 part 2 - Make StyleSheet inherit nsIDOMCSSStyleSheet. r=heycam
authorXidorn Quan <me@upsuper.org>
Fri, 14 Oct 2016 22:25:38 +1100
changeset 360856 b7e8b25c9e0797dc20e53d6f36b363c53bf8a8c0
parent 360855 25a49da5b2af6dfbfda0117e737dec0efdd5f438
child 360857 37f810ce05dc200f9f6305a919ebedd57e4826ce
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 2 - Make StyleSheet inherit nsIDOMCSSStyleSheet. r=heycam Some method impls are also moved from CSSStyleSheet to StyleSheet so that they can be shared between the two subclasses. The new interface methods added to ServoStyleSheet is currently left unimplemented. They would be implemented in later patches. MozReview-Commit-ID: 45wHT9BSHTK
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
@@ -1077,34 +1077,32 @@ CSSStyleSheetInner::SizeOfIncludingThis(
 
 // -------------------------------
 // CSS Style Sheet
 //
 
 CSSStyleSheet::CSSStyleSheet(css::SheetParsingMode aParsingMode,
                              CORSMode aCORSMode, ReferrerPolicy aReferrerPolicy)
   : StyleSheet(StyleBackendType::Gecko, aParsingMode),
-    mTitle(),
     mParent(nullptr),
     mOwnerRule(nullptr),
     mDirty(false),
     mInRuleProcessorCache(false),
     mScopeElement(nullptr),
     mRuleProcessors(nullptr)
 {
   mInner = new CSSStyleSheetInner(this, aCORSMode, aReferrerPolicy,
                                   SRIMetadata());
 }
 
 CSSStyleSheet::CSSStyleSheet(css::SheetParsingMode aParsingMode,
                              CORSMode aCORSMode,
                              ReferrerPolicy aReferrerPolicy,
                              const SRIMetadata& aIntegrity)
   : StyleSheet(StyleBackendType::Gecko, aParsingMode),
-    mTitle(),
     mParent(nullptr),
     mOwnerRule(nullptr),
     mDirty(false),
     mInRuleProcessorCache(false),
     mScopeElement(nullptr),
     mRuleProcessors(nullptr)
 {
   mInner = new CSSStyleSheetInner(this, aCORSMode, aReferrerPolicy,
@@ -1112,17 +1110,16 @@ CSSStyleSheet::CSSStyleSheet(css::SheetP
 }
 
 CSSStyleSheet::CSSStyleSheet(const CSSStyleSheet& aCopy,
                              CSSStyleSheet* aParentToUse,
                              css::ImportRule* aOwnerRuleToUse,
                              nsIDocument* aDocumentToUse,
                              nsINode* aOwningNodeToUse)
   : StyleSheet(aCopy, aDocumentToUse, aOwningNodeToUse),
-    mTitle(aCopy.mTitle),
     mParent(aParentToUse),
     mOwnerRule(aOwnerRuleToUse),
     mDirty(aCopy.mDirty),
     mInRuleProcessorCache(false),
     mScopeElement(nullptr),
     mInner(aCopy.mInner),
     mRuleProcessors(nullptr)
 {
@@ -1318,22 +1315,16 @@ CSSStyleSheet::AddStyleSet(nsStyleSet* a
 
 void
 CSSStyleSheet::DropStyleSet(nsStyleSet* aStyleSet)
 {
   DebugOnly<bool> found = mStyleSets.RemoveElement(aStyleSet);
   NS_ASSERTION(found, "didn't find style set");
 }
 
-void
-CSSStyleSheet::GetType(nsString& aType) const
-{
-  aType.AssignLiteral("text/css");
-}
-
 bool
 CSSStyleSheet::UseForPresentation(nsPresContext* aPresContext,
                                   nsMediaQueryResultCacheKey& aKey) const
 {
   if (mMedia) {
     return mMedia->Matches(aPresContext, &aKey);
   }
   return true;
@@ -1670,86 +1661,28 @@ CSSStyleSheet::RegisterNamespaceRule(css
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
   AddNamespaceRuleToMap(aRule, mInner->mNameSpaceMap);
   return NS_OK;
 }
 
   // nsIDOMStyleSheet interface
-NS_IMETHODIMP    
-CSSStyleSheet::GetType(nsAString& aType)
-{
-  aType.AssignLiteral("text/css");
-  return NS_OK;
-}
-
-NS_IMETHODIMP    
-CSSStyleSheet::GetDisabled(bool* aDisabled)
-{
-  *aDisabled = Disabled();
-  return NS_OK;
-}
-
-NS_IMETHODIMP    
-CSSStyleSheet::SetDisabled(bool aDisabled)
-{
-  // DOM method, so handle BeginUpdate/EndUpdate
-  MOZ_AUTO_DOC_UPDATE(mDocument, UPDATE_STYLE, true);
-  CSSStyleSheet::SetEnabled(!aDisabled);
-  return NS_OK;
-}
-
-NS_IMETHODIMP
-CSSStyleSheet::GetOwnerNode(nsIDOMNode** aOwnerNode)
-{
-  nsCOMPtr<nsIDOMNode> ownerNode = do_QueryInterface(GetOwnerNode());
-  ownerNode.forget(aOwnerNode);
-  return NS_OK;
-}
 
 NS_IMETHODIMP
 CSSStyleSheet::GetParentStyleSheet(nsIDOMStyleSheet** aParentStyleSheet)
 {
   NS_ENSURE_ARG_POINTER(aParentStyleSheet);
 
   NS_IF_ADDREF(*aParentStyleSheet = mParent);
 
   return NS_OK;
 }
 
 NS_IMETHODIMP
-CSSStyleSheet::GetHref(nsAString& aHref)
-{
-  if (mInner->mOriginalSheetURI) {
-    nsAutoCString str;
-    nsresult rv = mInner->mOriginalSheetURI->GetSpec(str);
-    NS_ENSURE_SUCCESS(rv, rv);
-    CopyUTF8toUTF16(str, aHref);
-  } else {
-    SetDOMStringToNull(aHref);
-  }
-
-  return NS_OK;
-}
-
-void
-CSSStyleSheet::GetTitle(nsString& aTitle) const
-{
-  aTitle = mTitle;
-}
-
-NS_IMETHODIMP
-CSSStyleSheet::GetTitle(nsAString& aTitle)
-{
-  aTitle.Assign(mTitle);
-  return NS_OK;
-}
-
-NS_IMETHODIMP
 CSSStyleSheet::GetMedia(nsIDOMMediaList** aMedia)
 {
   NS_ADDREF(*aMedia = Media());
   return NS_OK;
 }
 
 nsMediaList*
 CSSStyleSheet::Media()
--- a/layout/style/CSSStyleSheet.h
+++ b/layout/style/CSSStyleSheet.h
@@ -15,17 +15,16 @@
 #include "mozilla/StyleSheet.h"
 #include "mozilla/StyleSheetInfo.h"
 #include "mozilla/css/SheetParsingMode.h"
 #include "mozilla/dom/Element.h"
 
 #include "nscore.h"
 #include "nsCOMPtr.h"
 #include "nsAutoPtr.h"
-#include "nsIDOMCSSStyleSheet.h"
 #include "nsICSSLoaderObserver.h"
 #include "nsTArrayForwardDeclare.h"
 #include "nsString.h"
 #include "mozilla/CORSMode.h"
 #include "nsCycleCollectionParticipant.h"
 #include "nsWrapperCache.h"
 #include "mozilla/net/ReferrerPolicy.h"
 #include "mozilla/dom/SRIMetadata.h"
@@ -96,37 +95,34 @@ struct CSSStyleSheetInner : public Style
 
 // CID for the CSSStyleSheet class
 // 7985c7ac-9ddc-444d-9899-0c86ec122f54
 #define NS_CSS_STYLE_SHEET_IMPL_CID     \
 { 0x7985c7ac, 0x9ddc, 0x444d, \
   { 0x98, 0x99, 0x0c, 0x86, 0xec, 0x12, 0x2f, 0x54 } }
 
 
-class CSSStyleSheet final : public nsIDOMCSSStyleSheet,
-                            public nsICSSLoaderObserver,
-                            public nsWrapperCache,
-                            public StyleSheet
+class CSSStyleSheet final : public StyleSheet
+                          , public nsICSSLoaderObserver
+                          , public nsWrapperCache
 {
 public:
   typedef net::ReferrerPolicy ReferrerPolicy;
   CSSStyleSheet(css::SheetParsingMode aParsingMode,
                 CORSMode aCORSMode, ReferrerPolicy aReferrerPolicy);
   CSSStyleSheet(css::SheetParsingMode aParsingMode,
                 CORSMode aCORSMode, ReferrerPolicy aReferrerPolicy,
                 const dom::SRIMetadata& aIntegrity);
 
   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(CSSStyleSheet,
                                                          nsIDOMCSSStyleSheet)
 
   NS_DECLARE_STATIC_IID_ACCESSOR(NS_CSS_STYLE_SHEET_IMPL_CID)
 
-  void GetTitle(nsString& aTitle) const;
-  void GetType(nsString& aType) const;
   bool HasRules() const;
 
   /**
    * Set the stylesheet to be enabled.  This may or may not make it
    * applicable.  Note that this WILL inform the sheet's document of
    * its new applicable state if the state changes but WILL NOT call
    * BeginUpdate() or EndUpdate() on the document -- calling those is
    * the caller's responsibility.  This allows use of SetEnabled when
@@ -201,17 +197,18 @@ public:
   bool UseForPresentation(nsPresContext* aPresContext,
                             nsMediaQueryResultCacheKey& aKey) const;
 
   nsresult ReparseSheet(const nsAString& aInput);
 
   void SetInRuleProcessorCache() { mInRuleProcessorCache = true; }
 
   // nsIDOMStyleSheet interface
-  NS_DECL_NSIDOMSTYLESHEET
+  NS_IMETHOD GetParentStyleSheet(nsIDOMStyleSheet** aParentStyleSheet) final;
+  NS_IMETHOD GetMedia(nsIDOMMediaList** aMedia) final;
 
   // nsIDOMCSSStyleSheet interface
   NS_DECL_NSIDOMCSSSTYLESHEET
 
   // Function used as a callback to rebuild our inner's child sheet
   // list after we clone a unique inner for ourselves.
   static bool RebuildChildList(css::Rule* aRule, void* aBuilder);
 
@@ -219,32 +216,18 @@ public:
 
   dom::Element* GetScopeElement() const { return mScopeElement; }
   void SetScopeElement(dom::Element* aScopeElement)
   {
     mScopeElement = aScopeElement;
   }
 
   // WebIDL StyleSheet API
-  // Our CSSStyleSheet::GetType is a const method, so it ends up
-  // ambiguous with with the XPCOM version.  Just disambiguate.
-  void GetType(nsString& aType) {
-    const_cast<const CSSStyleSheet*>(this)->GetType(aType);
-  }
-  // Our XPCOM GetHref is fine for WebIDL
-  using StyleSheet::GetOwnerNode;
   CSSStyleSheet* GetParentStyleSheet() const { return mParent; }
-  // Our CSSStyleSheet::GetTitle is a const method, so it ends up
-  // ambiguous with with the XPCOM version.  Just disambiguate.
-  void GetTitle(nsString& aTitle) {
-    const_cast<const CSSStyleSheet*>(this)->GetTitle(aTitle);
-  }
   nsMediaList* Media();
-  bool Disabled() const { return mDisabled; }
-  // The XPCOM SetDisabled is fine for WebIDL
 
   // 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);
@@ -304,17 +287,16 @@ 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:
-  nsString              mTitle;
   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
@@ -18,16 +18,24 @@ ServoStyleSheet::ServoStyleSheet(css::Sh
 {
 }
 
 ServoStyleSheet::~ServoStyleSheet()
 {
   DropSheet();
 }
 
+NS_INTERFACE_MAP_BEGIN(ServoStyleSheet)
+  NS_INTERFACE_MAP_ENTRY(nsIDOMCSSStyleSheet)
+  NS_INTERFACE_MAP_ENTRY(nsIDOMStyleSheet)
+NS_INTERFACE_MAP_END
+
+NS_IMPL_ADDREF(ServoStyleSheet)
+NS_IMPL_RELEASE(ServoStyleSheet)
+
 bool
 ServoStyleSheet::HasRules() const
 {
   return mSheet && Servo_StyleSheet_HasRules(mSheet);
 }
 
 void
 ServoStyleSheet::SetOwningDocument(nsIDocument* aDocument)
@@ -99,9 +107,50 @@ ServoStyleSheet::SizeOfIncludingThis(Mal
 #ifdef DEBUG
 void
 ServoStyleSheet::List(FILE* aOut, int32_t aIndex) const
 {
   MOZ_CRASH("stylo: not implemented");
 }
 #endif
 
+// nsIDOMStyleSheet implementation
+
+NS_IMETHODIMP
+ServoStyleSheet::GetParentStyleSheet(nsIDOMStyleSheet** aParentStyleSheet)
+{
+  return NS_ERROR_NOT_IMPLEMENTED;
+}
+
+NS_IMETHODIMP
+ServoStyleSheet::GetMedia(nsIDOMMediaList** aMedia)
+{
+  return NS_ERROR_NOT_IMPLEMENTED;
+}
+
+// nsIDOMCSSStyleSheet implementation
+
+nsresult
+ServoStyleSheet::GetOwnerRule(nsIDOMCSSRule** aOwnerRule)
+{
+  return NS_ERROR_NOT_IMPLEMENTED;
+}
+
+nsresult
+ServoStyleSheet::GetCssRules(nsIDOMCSSRuleList** aCssRules)
+{
+  return NS_ERROR_NOT_IMPLEMENTED;
+}
+
+nsresult
+ServoStyleSheet::InsertRule(const nsAString& rule,
+                            uint32_t aIndex, uint32_t* aReturn)
+{
+  return NS_ERROR_NOT_IMPLEMENTED;
+}
+
+nsresult
+ServoStyleSheet::DeleteRule(uint32_t aIndex)
+{
+  return NS_ERROR_NOT_IMPLEMENTED;
+}
+
 } // namespace mozilla
--- a/layout/style/ServoStyleSheet.h
+++ b/layout/style/ServoStyleSheet.h
@@ -22,17 +22,17 @@ namespace mozilla {
 class ServoStyleSheet : public StyleSheet
 {
 public:
   ServoStyleSheet(css::SheetParsingMode aParsingMode,
                   CORSMode aCORSMode,
                   net::ReferrerPolicy aReferrerPolicy,
                   const dom::SRIMetadata& aIntegrity);
 
-  NS_INLINE_DECL_REFCOUNTING(ServoStyleSheet)
+  NS_DECL_ISUPPORTS
 
   bool HasRules() const;
 
   void SetOwningDocument(nsIDocument* aDocument);
 
   ServoStyleSheet* GetParentSheet() const;
   void AppendStyleSheet(ServoStyleSheet* aSheet);
 
@@ -45,16 +45,23 @@ public:
   size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const;
 
 #ifdef DEBUG
   void List(FILE* aOut = stdout, int32_t aIndex = 0) const;
 #endif
 
   RawServoStyleSheet* RawSheet() const { return mSheet; }
 
+  // nsIDOMStyleSheet interface
+  NS_IMETHOD GetParentStyleSheet(nsIDOMStyleSheet** aParentStyleSheet) final;
+  NS_IMETHOD GetMedia(nsIDOMMediaList** aMedia) final;
+
+  // nsIDOMCSSStyleSheet interface
+  NS_DECL_NSIDOMCSSSTYLESHEET
+
 protected:
   ~ServoStyleSheet();
 
 private:
   void DropSheet();
 
   RefPtr<RawServoStyleSheet> mSheet;
   StyleSheetInfo mSheetInfo;
--- a/layout/style/StyleSheet.cpp
+++ b/layout/style/StyleSheet.cpp
@@ -22,17 +22,18 @@ StyleSheet::StyleSheet(StyleBackendType 
   , mType(aType)
   , mDisabled(false)
 {
 }
 
 StyleSheet::StyleSheet(const StyleSheet& aCopy,
                        nsIDocument* aDocumentToUse,
                        nsINode* aOwningNodeToUse)
-  : mDocument(aDocumentToUse)
+  : mTitle(aCopy.mTitle)
+  , mDocument(aDocumentToUse)
   , mOwningNode(aOwningNodeToUse)
   , mParsingMode(aCopy.mParsingMode)
   , mType(aCopy.mType)
   , mDisabled(aCopy.mDisabled)
 {
 }
 
 mozilla::dom::CSSStyleSheetParsingMode
@@ -90,9 +91,67 @@ StyleSheetInfo::StyleSheetInfo(CORSMode 
   , mPrincipalSet(false)
 #endif
 {
   if (!mPrincipal) {
     NS_RUNTIMEABORT("nsNullPrincipal::Init failed");
   }
 }
 
+// nsIDOMStyleSheet interface
+
+NS_IMETHODIMP
+StyleSheet::GetType(nsAString& aType)
+{
+  aType.AssignLiteral("text/css");
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+StyleSheet::GetDisabled(bool* aDisabled)
+{
+  *aDisabled = Disabled();
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+StyleSheet::SetDisabled(bool aDisabled)
+{
+  // DOM method, so handle BeginUpdate/EndUpdate
+  MOZ_AUTO_DOC_UPDATE(mDocument, UPDATE_STYLE, true);
+  if (IsGecko()) {
+    AsGecko()->SetEnabled(!aDisabled);
+  } else {
+    MOZ_CRASH("stylo: unimplemented SetEnabled");
+  }
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+StyleSheet::GetOwnerNode(nsIDOMNode** aOwnerNode)
+{
+  nsCOMPtr<nsIDOMNode> ownerNode = do_QueryInterface(GetOwnerNode());
+  ownerNode.forget(aOwnerNode);
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+StyleSheet::GetHref(nsAString& aHref)
+{
+  if (nsIURI* sheetURI = SheetInfo().mOriginalSheetURI) {
+    nsAutoCString str;
+    nsresult rv = sheetURI->GetSpec(str);
+    NS_ENSURE_SUCCESS(rv, rv);
+    CopyUTF8toUTF16(str, aHref);
+  } else {
+    SetDOMStringToNull(aHref);
+  }
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+StyleSheet::GetTitle(nsAString& aTitle)
+{
+  aTitle.Assign(mTitle);
+  return NS_OK;
+}
+
 } // namespace mozilla
--- a/layout/style/StyleSheet.h
+++ b/layout/style/StyleSheet.h
@@ -9,33 +9,35 @@
 
 #include "mozilla/css/SheetParsingMode.h"
 #include "mozilla/dom/CSSStyleSheetBinding.h"
 #include "mozilla/net/ReferrerPolicy.h"
 #include "mozilla/StyleBackendType.h"
 #include "mozilla/CORSMode.h"
 #include "mozilla/ServoUtils.h"
 
+#include "nsIDOMCSSStyleSheet.h"
+
 class nsIDocument;
 class nsINode;
 
 namespace mozilla {
 
 class CSSStyleSheet;
 class ServoStyleSheet;
 struct StyleSheetInfo;
 
 namespace dom {
 class SRIMetadata;
 } // namespace dom
 
 /**
  * Superclass for data common to CSSStyleSheet and ServoStyleSheet.
  */
-class StyleSheet
+class StyleSheet : public nsIDOMCSSStyleSheet
 {
 protected:
   StyleSheet(StyleBackendType aType, css::SheetParsingMode aParsingMode);
   StyleSheet(const StyleSheet& aCopy,
              nsIDocument* aDocumentToUse,
              nsINode* aOwningNodeToUse);
 
 public:
@@ -110,23 +112,40 @@ public:
   // Get this style sheet's integrity metadata
   inline void GetIntegrity(dom::SRIMetadata& aResult) const;
 
   inline size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const;
 #ifdef DEBUG
   inline void List(FILE* aOut = stdout, int32_t aIndex = 0) const;
 #endif
 
+  // 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.
+
+  // 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;
+
 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;
 
 protected:
+  nsString              mTitle;
   nsIDocument*          mDocument; // weak ref; parents maintain this for their children
   nsINode*              mOwningNode; // weak ref
 
   // mParsingMode controls access to nonstandard style constructs that
   // are not safe for use on the public Web but necessary in UA sheets
   // and/or useful in user sheets.
   css::SheetParsingMode mParsingMode;