Bug 1404243 Part 4 - Change StyleShapeSource's URL value storage by using nsStyleImage. draft
authorTing-Yu Lin <tlin@mozilla.com>
Fri, 29 Sep 2017 16:24:23 +0800
changeset 673202 1d9f89de86dded0a53791e7dea7e4d1ffdff1c6f
parent 673201 070e993fd42511de5a3ca7b72bc3b07177382a66
child 734030 77a3f523cc7b2fd3ab33f8b4ff4780387f91c9db
push id82495
push userbmo:tlin@mozilla.com
push dateMon, 02 Oct 2017 03:57:39 +0000
bugs1404243
milestone58.0a1
Bug 1404243 Part 4 - Change StyleShapeSource's URL value storage by using nsStyleImage. This is for implementing shape-outside: <image> later. MozReview-Commit-ID: 93TmLecRjRx
layout/style/nsStyleStruct.cpp
layout/style/nsStyleStruct.h
--- a/layout/style/nsStyleStruct.cpp
+++ b/layout/style/nsStyleStruct.cpp
@@ -1037,108 +1037,101 @@ StyleBasicShape::GetShapeTypeName() cons
   NS_NOTREACHED("unexpected type");
   return eCSSKeyword_UNKNOWN;
 }
 
 // --------------------
 // StyleShapeSource
 
 StyleShapeSource::StyleShapeSource(const StyleShapeSource& aSource)
-  : StyleShapeSource()
-{
-  if (aSource.mType == StyleShapeSourceType::URL) {
-    SetURL(aSource.mURL);
-  } else if (aSource.mType == StyleShapeSourceType::Shape) {
-    SetBasicShape(MakeUnique<StyleBasicShape>(*aSource.mBasicShape),
-                  aSource.mReferenceBox);
-  } else if (aSource.mType == StyleShapeSourceType::Box) {
-    SetReferenceBox(aSource.mReferenceBox);
-  }
+{
+  DoCopy(aSource);
 }
 
 StyleShapeSource&
 StyleShapeSource::operator=(const StyleShapeSource& aOther)
 {
-  if (this == &aOther) {
-    return *this;
-  }
-
-  if (aOther.mType == StyleShapeSourceType::URL) {
-    SetURL(aOther.mURL);
-  } else if (aOther.mType == StyleShapeSourceType::Shape) {
-    SetBasicShape(MakeUnique<StyleBasicShape>(*aOther.mBasicShape),
-                  aOther.mReferenceBox);
-  } else if (aOther.mType == StyleShapeSourceType::Box) {
-    SetReferenceBox(aOther.mReferenceBox);
-  } else {
-    ReleaseRef();
-    mReferenceBox = StyleGeometryBox::NoBox;
-    mType = StyleShapeSourceType::None;
-  }
+  if (this != &aOther) {
+    DoCopy(aOther);
+  }
+
   return *this;
 }
 
 bool
 StyleShapeSource::operator==(const StyleShapeSource& aOther) const
 {
   if (mType != aOther.mType) {
     return false;
   }
 
   if (mType == StyleShapeSourceType::URL) {
-    return DefinitelyEqualURIs(mURL, aOther.mURL);
+    return DefinitelyEqualURIs(GetURL(), aOther.GetURL());
   } else if (mType == StyleShapeSourceType::Shape) {
     return *mBasicShape == *aOther.mBasicShape &&
       mReferenceBox == aOther.mReferenceBox;
   } else if (mType == StyleShapeSourceType::Box) {
     return mReferenceBox == aOther.mReferenceBox;
   }
 
   return true;
 }
 
 bool
 StyleShapeSource::SetURL(css::URLValue* aValue)
 {
   MOZ_ASSERT(aValue);
-  ReleaseRef();
-  mURL = aValue;
-  mURL->AddRef();
+  if (!mShapeImage) {
+    mShapeImage = MakeUnique<nsStyleImage>();
+  }
+  mShapeImage->SetURLValue(do_AddRef(aValue));
   mType = StyleShapeSourceType::URL;
   return true;
 }
 
 void
 StyleShapeSource::SetBasicShape(UniquePtr<StyleBasicShape> aBasicShape,
                                 StyleGeometryBox aReferenceBox)
 {
   NS_ASSERTION(aBasicShape, "expected pointer");
-  ReleaseRef();
   mBasicShape = Move(aBasicShape);
   mReferenceBox = aReferenceBox;
   mType = StyleShapeSourceType::Shape;
 }
 
 void
 StyleShapeSource::SetReferenceBox(StyleGeometryBox aReferenceBox)
 {
-  ReleaseRef();
   mReferenceBox = aReferenceBox;
   mType = StyleShapeSourceType::Box;
 }
 
 void
-StyleShapeSource::ReleaseRef()
-{
-  if (mType == StyleShapeSourceType::URL) {
-    NS_ASSERTION(mURL, "expected pointer");
-    mURL->Release();
-  }
-
-  mURL = nullptr;
+StyleShapeSource::DoCopy(const StyleShapeSource& aOther)
+{
+
+  switch (aOther.mType) {
+    case StyleShapeSourceType::None:
+      mReferenceBox = StyleGeometryBox::NoBox;
+      mType = StyleShapeSourceType::None;
+      break;
+
+    case StyleShapeSourceType::URL:
+      SetURL(aOther.GetURL());
+      break;
+
+    case StyleShapeSourceType::Shape:
+      SetBasicShape(MakeUnique<StyleBasicShape>(*aOther.GetBasicShape()),
+                    aOther.GetReferenceBox());
+      break;
+
+    case StyleShapeSourceType::Box:
+      SetReferenceBox(aOther.GetReferenceBox());
+      break;
+  }
 }
 
 // --------------------
 // nsStyleFilter
 //
 nsStyleFilter::nsStyleFilter()
   : mType(NS_STYLE_FILTER_NONE)
   , mDropShadow(nullptr)
--- a/layout/style/nsStyleStruct.h
+++ b/layout/style/nsStyleStruct.h
@@ -2432,25 +2432,22 @@ private:
   // position of center for ellipse or circle
   Position mPosition;
   // corner radii for inset (0 if not set)
   nsStyleCorners mRadius;
 };
 
 struct StyleShapeSource final
 {
-  StyleShapeSource()
-    : mURL(nullptr)
-  {}
+  StyleShapeSource() = default;
 
   StyleShapeSource(const StyleShapeSource& aSource);
 
   ~StyleShapeSource()
   {
-    ReleaseRef();
   }
 
   StyleShapeSource& operator=(const StyleShapeSource& aOther);
 
   bool operator==(const StyleShapeSource& aOther) const;
 
   bool operator!=(const StyleShapeSource& aOther) const
   {
@@ -2460,17 +2457,19 @@ struct StyleShapeSource final
   StyleShapeSourceType GetType() const
   {
     return mType;
   }
 
   css::URLValue* GetURL() const
   {
     MOZ_ASSERT(mType == StyleShapeSourceType::URL, "Wrong shape source type!");
-    return mURL;
+    return mShapeImage
+      ? static_cast<css::URLValue*>(mShapeImage->GetURLValue())
+      : nullptr;
   }
 
   bool SetURL(css::URLValue* aValue);
 
   const UniquePtr<StyleBasicShape>& GetBasicShape() const
   {
     MOZ_ASSERT(mType == StyleShapeSourceType::Shape, "Wrong shape source type!");
     return mBasicShape;
@@ -2485,24 +2484,22 @@ struct StyleShapeSource final
                mType == StyleShapeSourceType::Shape,
                "Wrong shape source type!");
     return mReferenceBox;
   }
 
   void SetReferenceBox(StyleGeometryBox aReferenceBox);
 
 private:
-  void ReleaseRef();
-
   void* operator new(size_t) = delete;
 
+  void DoCopy(const StyleShapeSource& aOther);
+
   mozilla::UniquePtr<StyleBasicShape> mBasicShape;
-  union {
-    css::URLValue* mURL;
-  };
+  mozilla::UniquePtr<nsStyleImage> mShapeImage;
   StyleShapeSourceType mType = StyleShapeSourceType::None;
   StyleGeometryBox mReferenceBox = StyleGeometryBox::NoBox;
 };
 
 } // namespace mozilla
 
 // Consumers expect to be able to null-test mBinding to determine whether there
 // is a valid binding URI. Since we can't do URL resolution during parallel