Bug 1373018 - Part 1: stylo: Introduce ServoStyleContext and GeckoStyleContext subclasses; r?bholley draft
authorManish Goregaokar <manishearth@gmail.com>
Sat, 10 Jun 2017 22:27:45 -0700
changeset 595116 97b42be7d1bb639639a6f5a4194c56949034e515
parent 595115 157a8c956cbc5b1c3f296f12a0a550346eec77c9
child 595117 8087e7e9fc8a75d8086c43f449e03c547e22cccf
push id64258
push userbmo:manishearth@gmail.com
push dateFri, 16 Jun 2017 03:04:15 +0000
reviewersbholley
bugs1373018
milestone56.0a1
Bug 1373018 - Part 1: stylo: Introduce ServoStyleContext and GeckoStyleContext subclasses; r?bholley MozReview-Commit-ID: GY1GfkWMK0n
layout/style/GeckoStyleContext.cpp
layout/style/GeckoStyleContext.h
layout/style/ServoStyleContext.cpp
layout/style/ServoStyleContext.h
layout/style/ServoStyleRule.h
layout/style/moz.build
layout/style/nsCSSRuleProcessor.cpp
layout/style/nsICSSLoaderObserver.h
layout/style/nsStyleContext.cpp
layout/style/nsStyleContext.h
new file mode 100644
--- /dev/null
+++ b/layout/style/GeckoStyleContext.cpp
@@ -0,0 +1,43 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "mozilla/GeckoStyleContext.h"
+
+#include "nsStyleConsts.h"
+#include "nsStyleStruct.h"
+#include "nsPresContext.h"
+#include "nsRuleNode.h"
+
+using namespace mozilla;
+
+GeckoStyleContext::GeckoStyleContext(nsStyleContext* aParent,
+                                     nsIAtom* aPseudoTag,
+                                     CSSPseudoElementType aPseudoType,
+                                     already_AddRefed<nsRuleNode> aRuleNode,
+                                     bool aSkipParentDisplayBasedStyleFixup)
+  : nsStyleContext(aParent, OwningStyleContextSource(Move(aRuleNode)),
+                   aPseudoTag, aPseudoType)
+{
+#ifdef MOZ_STYLO
+  mPresContext = mSource.AsGeckoRuleNode()->PresContext();
+#endif
+
+  if (aParent) {
+#ifdef DEBUG
+    nsRuleNode *r1 = mParent->RuleNode(), *r2 = mSource.AsGeckoRuleNode();
+    while (r1->GetParent())
+      r1 = r1->GetParent();
+    while (r2->GetParent())
+      r2 = r2->GetParent();
+    NS_ASSERTION(r1 == r2, "must be in the same rule tree as parent");
+#endif
+  } else {
+    PresContext()->PresShell()->StyleSet()->RootStyleContextAdded();
+  }
+
+  mSource.AsGeckoRuleNode()->SetUsedDirectly(); // before ApplyStyleFixups()!
+  FinishConstruction();
+  ApplyStyleFixups(aSkipParentDisplayBasedStyleFixup);
+}
new file mode 100644
--- /dev/null
+++ b/layout/style/GeckoStyleContext.h
@@ -0,0 +1,25 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_GeckoStyleContext_h
+#define mozilla_GeckoStyleContext_h
+
+#include "nsStyleContext.h"
+
+namespace mozilla {
+
+class GeckoStyleContext final : public nsStyleContext {
+public:
+  GeckoStyleContext(nsStyleContext* aParent,
+                    nsIAtom* aPseudoTag,
+                    CSSPseudoElementType aPseudoType,
+                    already_AddRefed<nsRuleNode> aRuleNode,
+                    bool aSkipParentDisplayBasedStyleFixup);
+};
+
+}
+
+#endif // mozilla_GeckoStyleContext_h
new file mode 100644
--- /dev/null
+++ b/layout/style/ServoStyleContext.cpp
@@ -0,0 +1,32 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "mozilla/ServoStyleContext.h"
+
+#include "nsStyleConsts.h"
+#include "nsStyleStruct.h"
+#include "nsPresContext.h"
+
+#include "mozilla/ServoBindings.h"
+
+using namespace mozilla;
+
+ServoStyleContext::ServoStyleContext(nsStyleContext* aParent,
+                               nsPresContext* aPresContext,
+                               nsIAtom* aPseudoTag,
+                               CSSPseudoElementType aPseudoType,
+                               already_AddRefed<ServoComputedValues> aComputedValues)
+  : nsStyleContext(aParent, OwningStyleContextSource(Move(aComputedValues)),
+                   aPseudoTag, aPseudoType)
+{
+#ifdef MOZ_STYLO
+  mPresContext = aPresContext;
+#endif
+
+  FinishConstruction();
+
+  // No need to call ApplyStyleFixups here, since fixups are handled by Servo when
+  // producing the ServoComputedValues.
+}
new file mode 100644
--- /dev/null
+++ b/layout/style/ServoStyleContext.h
@@ -0,0 +1,25 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_ServoStyleContext_h
+#define mozilla_ServoStyleContext_h
+
+#include "nsStyleContext.h"
+
+namespace mozilla {
+
+class ServoStyleContext final : public nsStyleContext {
+public:
+  ServoStyleContext(nsStyleContext* aParent,
+                    nsPresContext* aPresContext,
+                    nsIAtom* aPseudoTag,
+                    CSSPseudoElementType aPseudoType,
+                    already_AddRefed<ServoComputedValues> aComputedValues);
+};
+
+}
+
+#endif // mozilla_ServoStyleContext_h
--- a/layout/style/ServoStyleRule.h
+++ b/layout/style/ServoStyleRule.h
@@ -8,16 +8,17 @@
 
 #ifndef mozilla_ServoStyleRule_h
 #define mozilla_ServoStyleRule_h
 
 #include "mozilla/BindingStyleRule.h"
 #include "mozilla/ServoBindingTypes.h"
 
 #include "nsIDOMCSSStyleRule.h"
+#include "nsICSSStyleRuleDOMWrapper.h"
 #include "nsDOMCSSDeclaration.h"
 
 namespace mozilla {
 
 class ServoDeclarationBlock;
 class ServoStyleRule;
 
 class ServoStyleRuleDeclaration final : public nsDOMCSSDeclaration
--- a/layout/style/moz.build
+++ b/layout/style/moz.build
@@ -84,16 +84,17 @@ EXPORTS.mozilla += [
     'CSSEnabledState.h',
     'CSSStyleSheet.h',
     'CSSVariableDeclarations.h',
     'CSSVariableResolver.h',
     'CSSVariableValues.h',
     'DeclarationBlock.h',
     'DeclarationBlockInlines.h',
     'DocumentStyleRootIterator.h',
+    'GeckoStyleContext.h',
     'GenericSpecifiedValues.h',
     'GenericSpecifiedValuesInlines.h',
     'HandleRefPtr.h',
     'IncrementalClearCOMRuleArray.h',
     'LayerAnimationInfo.h',
     'PostTraversalTask.h',
     'PreloadedStyleSheet.h',
     'RuleNodeCacheConditions.h',
@@ -111,16 +112,17 @@ EXPORTS.mozilla += [
     'ServoKeyframeRule.h',
     'ServoKeyframesRule.h',
     'ServoMediaList.h',
     'ServoMediaRule.h',
     'ServoNamespaceRule.h',
     'ServoPageRule.h',
     'ServoPropPrefList.h',
     'ServoSpecifiedValues.h',
+    'ServoStyleContext.h',
     'ServoStyleRule.h',
     'ServoStyleSet.h',
     'ServoStyleSheet.h',
     'ServoSupportsRule.h',
     'ServoTypes.h',
     'ServoUtils.h',
     'SheetType.h',
     'StyleAnimationValue.h',
@@ -188,16 +190,17 @@ UNIFIED_SOURCES += [
     'CSSVariableResolver.cpp',
     'CSSVariableValues.cpp',
     'Declaration.cpp',
     'DocumentStyleRootIterator.cpp',
     'ErrorReporter.cpp',
     'FontFace.cpp',
     'FontFaceSet.cpp',
     'FontFaceSetIterator.cpp',
+    'GeckoStyleContext.cpp',
     'GroupRule.cpp',
     'ImageLoader.cpp',
     'IncrementalClearCOMRuleArray.cpp',
     'LayerAnimationInfo.cpp',
     'Loader.cpp',
     'MediaList.cpp',
     'MediaQueryList.cpp',
     'nsAnimationManager.cpp',
@@ -247,16 +250,17 @@ UNIFIED_SOURCES += [
     'ServoImportRule.cpp',
     'ServoKeyframeRule.cpp',
     'ServoKeyframesRule.cpp',
     'ServoMediaList.cpp',
     'ServoMediaRule.cpp',
     'ServoNamespaceRule.cpp',
     'ServoPageRule.cpp',
     'ServoSpecifiedValues.cpp',
+    'ServoStyleContext.cpp',
     'ServoStyleRule.cpp',
     'ServoStyleSet.cpp',
     'ServoStyleSheet.cpp',
     'ServoSupportsRule.cpp',
     'StyleAnimationValue.cpp',
     'StyleRule.cpp',
     'StyleSheet.cpp',
     'URLExtraData.cpp',
--- a/layout/style/nsCSSRuleProcessor.cpp
+++ b/layout/style/nsCSSRuleProcessor.cpp
@@ -13,16 +13,17 @@
 
 #include "nsAutoPtr.h"
 #include "nsRuleProcessorData.h"
 #include <algorithm>
 #include "nsIAtom.h"
 #include "PLDHashTable.h"
 #include "nsICSSPseudoComparator.h"
 #include "mozilla/MemoryReporting.h"
+#include "mozilla/css/ImportRule.h"
 #include "mozilla/css/StyleRule.h"
 #include "mozilla/css/GroupRule.h"
 #include "nsIDocument.h"
 #include "nsPresContext.h"
 #include "nsGkAtoms.h"
 #include "nsUnicharUtils.h"
 #include "nsError.h"
 #include "nsRuleWalker.h"
--- a/layout/style/nsICSSLoaderObserver.h
+++ b/layout/style/nsICSSLoaderObserver.h
@@ -4,22 +4,25 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 /* internal interface for observing CSS style sheet loads */
 
 #ifndef nsICSSLoaderObserver_h___
 #define nsICSSLoaderObserver_h___
 
 #include "nsISupports.h"
-#include "mozilla/StyleSheet.h"
 
 #define NS_ICSSLOADEROBSERVER_IID \
 { 0xf51fbf2c, 0xfe4b, 0x4a15, \
   { 0xaf, 0x7e, 0x5e, 0x20, 0x64, 0x5f, 0xaf, 0x58 } }
 
+namespace mozilla {
+  class StyleSheet;
+}
+
 class nsICSSLoaderObserver : public nsISupports {
 public:
   NS_DECLARE_STATIC_IID_ACCESSOR(NS_ICSSLOADEROBSERVER_IID)
 
   /**
    * StyleSheetLoaded is called after aSheet is marked complete and before any
    * load events associated with aSheet are fired.
    * @param aSheet the sheet that was loaded. Guaranteed to always be
--- a/layout/style/nsStyleContext.cpp
+++ b/layout/style/nsStyleContext.cpp
@@ -29,16 +29,18 @@
 #include "GeckoProfiler.h"
 #include "nsIDocument.h"
 #include "nsPrintfCString.h"
 #include "RubyUtils.h"
 #include "mozilla/Preferences.h"
 #include "mozilla/ArenaObjectID.h"
 #include "mozilla/StyleSetHandle.h"
 #include "mozilla/StyleSetHandleInlines.h"
+#include "mozilla/GeckoStyleContext.h"
+#include "mozilla/ServoStyleContext.h"
 
 #include "mozilla/ReflowInput.h"
 #include "nsLayoutUtils.h"
 #include "nsCoord.h"
 
 // Ensure the binding function declarations in nsStyleContext.h matches
 // those in ServoBindings.h.
 #include "mozilla/ServoBindings.h"
@@ -95,64 +97,16 @@ nsStyleContext::nsStyleContext(nsStyleCo
   , mBits(((uint64_t)aPseudoType) << NS_STYLE_CONTEXT_TYPE_SHIFT)
   , mRefCnt(0)
 #ifdef DEBUG
   , mFrameRefCnt(0)
   , mComputingStruct(nsStyleStructID_None)
 #endif
 {}
 
-nsStyleContext::nsStyleContext(nsStyleContext* aParent,
-                               nsIAtom* aPseudoTag,
-                               CSSPseudoElementType aPseudoType,
-                               already_AddRefed<nsRuleNode> aRuleNode,
-                               bool aSkipParentDisplayBasedStyleFixup)
-  : nsStyleContext(aParent, OwningStyleContextSource(Move(aRuleNode)),
-                   aPseudoTag, aPseudoType)
-{
-#ifdef MOZ_STYLO
-  mPresContext = mSource.AsGeckoRuleNode()->PresContext();
-#endif
-
-  if (aParent) {
-#ifdef DEBUG
-    nsRuleNode *r1 = mParent->RuleNode(), *r2 = mSource.AsGeckoRuleNode();
-    while (r1->GetParent())
-      r1 = r1->GetParent();
-    while (r2->GetParent())
-      r2 = r2->GetParent();
-    NS_ASSERTION(r1 == r2, "must be in the same rule tree as parent");
-#endif
-  } else {
-    PresContext()->PresShell()->StyleSet()->RootStyleContextAdded();
-  }
-
-  mSource.AsGeckoRuleNode()->SetUsedDirectly(); // before ApplyStyleFixups()!
-  FinishConstruction();
-  ApplyStyleFixups(aSkipParentDisplayBasedStyleFixup);
-}
-
-nsStyleContext::nsStyleContext(nsStyleContext* aParent,
-                               nsPresContext* aPresContext,
-                               nsIAtom* aPseudoTag,
-                               CSSPseudoElementType aPseudoType,
-                               already_AddRefed<ServoComputedValues> aComputedValues)
-  : nsStyleContext(aParent, OwningStyleContextSource(Move(aComputedValues)),
-                   aPseudoTag, aPseudoType)
-{
-#ifdef MOZ_STYLO
-  mPresContext = aPresContext;
-#endif
-
-  FinishConstruction();
-
-  // No need to call ApplyStyleFixups here, since fixups are handled by Servo when
-  // producing the ServoComputedValues.
-}
-
 void
 nsStyleContext::FinishConstruction()
 {
   // This check has to be done "backward", because if it were written the
   // more natural way it wouldn't fail even when it needed to.
   static_assert((UINT64_MAX >> NS_STYLE_CONTEXT_TYPE_SHIFT) >=
                  static_cast<CSSPseudoElementTypeBase>(
                    CSSPseudoElementType::MAX),
@@ -1403,31 +1357,31 @@ NS_NewStyleContext(nsStyleContext* aPare
                    nsIAtom* aPseudoTag,
                    CSSPseudoElementType aPseudoType,
                    nsRuleNode* aRuleNode,
                    bool aSkipParentDisplayBasedStyleFixup)
 {
   RefPtr<nsRuleNode> node = aRuleNode;
   RefPtr<nsStyleContext> context =
     new (aRuleNode->PresContext())
-    nsStyleContext(aParentContext, aPseudoTag, aPseudoType, node.forget(),
+    GeckoStyleContext(aParentContext, aPseudoTag, aPseudoType, node.forget(),
                    aSkipParentDisplayBasedStyleFixup);
   return context.forget();
 }
 
 already_AddRefed<nsStyleContext>
 NS_NewStyleContext(nsStyleContext* aParentContext,
                    nsPresContext* aPresContext,
                    nsIAtom* aPseudoTag,
                    CSSPseudoElementType aPseudoType,
                    already_AddRefed<ServoComputedValues> aComputedValues)
 {
   RefPtr<nsStyleContext> context =
     new (aPresContext)
-    nsStyleContext(aParentContext, aPresContext, aPseudoTag, aPseudoType,
+    ServoStyleContext(aParentContext, aPresContext, aPseudoTag, aPseudoType,
                    Move(aComputedValues));
   return context.forget();
 }
 
 nsIPresShell*
 nsStyleContext::Arena()
 {
   return PresContext()->PresShell();
--- a/layout/style/nsStyleContext.h
+++ b/layout/style/nsStyleContext.h
@@ -47,57 +47,19 @@ extern "C" {
  *
  * Style contexts are reference counted.  References are generally held
  * by:
  *  1. the |nsIFrame|s that are using the style context and
  *  2. any *child* style contexts (this might be the reverse of
  *     expectation, but it makes sense in this case)
  */
 
-class nsStyleContext final
+class nsStyleContext
 {
 public:
-  /**
-   * Create a new style context.
-   * @param aParent  The parent of a style context is used for CSS
-   *                 inheritance.  When the element or pseudo-element
-   *                 this style context represents the style data of
-   *                 inherits a CSS property, the value comes from the
-   *                 parent style context.  This means style context
-   *                 parentage must match the definitions of inheritance
-   *                 in the CSS specification.
-   * @param aPseudoTag  The pseudo-element or anonymous box for which
-   *                    this style context represents style.  Null if
-   *                    this style context is for a normal DOM element.
-   * @param aPseudoType  Must match aPseudoTag.
-   * @param aRuleNode  A rule node representing the ordered sequence of
-   *                   rules that any element, pseudo-element, or
-   *                   anonymous box that this style context is for
-   *                   matches.  See |nsRuleNode| and |nsIStyleRule|.
-   * @param aSkipParentDisplayBasedStyleFixup
-   *                 If set, this flag indicates that we should skip
-   *                 the chunk of ApplyStyleFixups() that applies to
-   *                 special cases where a child element's style may
-   *                 need to be modified based on its parent's display
-   *                 value.
-   */
-  nsStyleContext(nsStyleContext* aParent, nsIAtom* aPseudoTag,
-                 mozilla::CSSPseudoElementType aPseudoType,
-                 already_AddRefed<nsRuleNode> aRuleNode,
-                 bool aSkipParentDisplayBasedStyleFixup);
-
-  // Version of the above that takes a ServoComputedValues instead of a Gecko
-  // nsRuleNode.
-  nsStyleContext(nsStyleContext* aParent,
-                 nsPresContext* aPresContext,
-                 nsIAtom* aPseudoTag,
-                 mozilla::CSSPseudoElementType aPseudoType,
-                 already_AddRefed<ServoComputedValues> aComputedValues);
-
-  void* operator new(size_t sz, nsPresContext* aPresContext);
   void Destroy();
 
   // These two methods are for use by ArenaRefPtr.
   static mozilla::ArenaObjectID ArenaObjectID()
   {
     return mozilla::eArenaObjectID_nsStyleContext;
   }
   nsIPresShell* Arena();
@@ -536,17 +498,17 @@ public:
     } else {
       cachedData = mCachedInheritedData.mStyleStructs[aSID];
     }
     return cachedData;
   }
 
   mozilla::NonOwningStyleContextSource StyleSource() const { return mSource.AsRaw(); }
 
-private:
+protected:
   // Private destructor, to discourage deletion outside of Release():
   ~nsStyleContext();
 
   // Delegated Helper constructor.
   nsStyleContext(nsStyleContext* aParent,
                  mozilla::OwningStyleContextSource&& aSource,
                  nsIAtom* aPseudoTag,
                  mozilla::CSSPseudoElementType aPseudoType);