Commit b0acad58 authored by tzik's avatar tzik Committed by Commit Bot

Add external deleter support to WTF::RefCounted

NGPhysicalFragment needs special handling on the destruction
rather than being destroyed by WTF::RefCounted. However, existing
style of the destruction hook will be unavailable after unifying
base::RefCounted and WTF::RefCounted.

This CL adds a proper way to inject the destruction function of a
ref counted object. The destruction function will be injected to
base::RefCounted after the unification.

Bug: 763844
Cq-Include-Trybots: master.tryserver.chromium.linux:linux_layout_tests_layout_ng
Change-Id: If763525a55c4621abd711a16a531817528a42b69
Reviewed-on: https://chromium-review.googlesource.com/681955Reviewed-by: default avatarIan Kilpatrick <ikilpatrick@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Commit-Queue: Taiju Tsuiki <tzik@chromium.org>
Cr-Commit-Position: refs/heads/master@{#504365}
parent 20430a88
...@@ -128,6 +128,11 @@ void AppendFragmentToString(const NGPhysicalFragment* fragment, ...@@ -128,6 +128,11 @@ void AppendFragmentToString(const NGPhysicalFragment* fragment,
} // namespace } // namespace
// static
void NGPhysicalFragmentTraits::Destruct(const NGPhysicalFragment* fragment) {
fragment->Destroy();
}
NGPhysicalFragment::NGPhysicalFragment(LayoutObject* layout_object, NGPhysicalFragment::NGPhysicalFragment(LayoutObject* layout_object,
const ComputedStyle& style, const ComputedStyle& style,
NGPhysicalSize size, NGPhysicalSize size,
......
...@@ -19,6 +19,12 @@ class ComputedStyle; ...@@ -19,6 +19,12 @@ class ComputedStyle;
class LayoutObject; class LayoutObject;
struct NGPixelSnappedPhysicalBoxStrut; struct NGPixelSnappedPhysicalBoxStrut;
class NGPhysicalFragment;
struct CORE_EXPORT NGPhysicalFragmentTraits {
static void Destruct(const NGPhysicalFragment*);
};
// The NGPhysicalFragment contains the output geometry from layout. The // The NGPhysicalFragment contains the output geometry from layout. The
// fragment stores all of its information in the physical coordinate system for // fragment stores all of its information in the physical coordinate system for
// use by paint, hit-testing etc. // use by paint, hit-testing etc.
...@@ -39,9 +45,10 @@ struct NGPixelSnappedPhysicalBoxStrut; ...@@ -39,9 +45,10 @@ struct NGPixelSnappedPhysicalBoxStrut;
// (See https://drafts.csswg.org/css-backgrounds-3/#the-background-image) // (See https://drafts.csswg.org/css-backgrounds-3/#the-background-image)
// - image (<img>, svg <image>) or video (<video>) elements that are // - image (<img>, svg <image>) or video (<video>) elements that are
// placeholders for displaying them. // placeholders for displaying them.
class CORE_EXPORT NGPhysicalFragment : public RefCounted<NGPhysicalFragment>, class CORE_EXPORT NGPhysicalFragment
public DisplayItemClient, : public RefCounted<NGPhysicalFragment, NGPhysicalFragmentTraits>,
public ImageResourceObserver { public DisplayItemClient,
public ImageResourceObserver {
public: public:
enum NGFragmentType { enum NGFragmentType {
kFragmentBox = 0, kFragmentBox = 0,
...@@ -133,13 +140,6 @@ class CORE_EXPORT NGPhysicalFragment : public RefCounted<NGPhysicalFragment>, ...@@ -133,13 +140,6 @@ class CORE_EXPORT NGPhysicalFragment : public RefCounted<NGPhysicalFragment>,
void ShowFragmentTree() const; void ShowFragmentTree() const;
#endif #endif
// Override RefCounted's deref() to ensure operator delete is called on the
// appropriate subclass type.
void Deref() const {
if (DerefBase())
Destroy();
}
protected: protected:
NGPhysicalFragment(LayoutObject* layout_object, NGPhysicalFragment(LayoutObject* layout_object,
const ComputedStyle& style, const ComputedStyle& style,
...@@ -162,6 +162,7 @@ class CORE_EXPORT NGPhysicalFragment : public RefCounted<NGPhysicalFragment>, ...@@ -162,6 +162,7 @@ class CORE_EXPORT NGPhysicalFragment : public RefCounted<NGPhysicalFragment>,
unsigned border_edge_ : 4; // NGBorderEdges::Physical unsigned border_edge_ : 4; // NGBorderEdges::Physical
private: private:
friend struct NGPhysicalFragmentTraits;
void Destroy() const; void Destroy() const;
}; };
......
...@@ -128,7 +128,17 @@ inline void Adopted(RefCountedBase* object) { ...@@ -128,7 +128,17 @@ inline void Adopted(RefCountedBase* object) {
} }
#endif #endif
template <typename T, typename Traits>
class RefCounted;
template <typename T> template <typename T>
struct DefaultRefCountedTraits {
static void Destruct(const T* x) {
WTF::RefCounted<T, DefaultRefCountedTraits>::DeleteInternal(x);
}
};
template <typename T, typename Traits = DefaultRefCountedTraits<T>>
class RefCounted : public RefCountedBase { class RefCounted : public RefCountedBase {
WTF_MAKE_NONCOPYABLE(RefCounted); WTF_MAKE_NONCOPYABLE(RefCounted);
...@@ -140,11 +150,15 @@ class RefCounted : public RefCountedBase { ...@@ -140,11 +150,15 @@ class RefCounted : public RefCountedBase {
public: public:
void Deref() const { void Deref() const {
if (DerefBase()) if (DerefBase())
delete static_cast<const T*>(this); Traits::Destruct(static_cast<const T*>(this));
} }
protected: protected:
RefCounted() {} RefCounted() {}
private:
friend struct DefaultRefCountedTraits<T>;
static void DeleteInternal(const T* x) { delete x; }
}; };
// Allows subclasses to use the default copy constructor. // Allows subclasses to use the default copy constructor.
......
...@@ -55,16 +55,31 @@ class WTF_EXPORT ThreadSafeRefCountedBase { ...@@ -55,16 +55,31 @@ class WTF_EXPORT ThreadSafeRefCountedBase {
base::AtomicRefCount ref_count_; base::AtomicRefCount ref_count_;
}; };
template <class T> template <typename T, typename Traits>
class ThreadSafeRefCounted;
template <typename T>
struct DefaultThreadSafeRefCountedTraits {
static void Destruct(const T* x) {
WTF::ThreadSafeRefCounted<
T, DefaultThreadSafeRefCountedTraits>::DeleteInternal(x);
}
};
template <typename T, typename Traits = DefaultThreadSafeRefCountedTraits<T>>
class ThreadSafeRefCounted : public ThreadSafeRefCountedBase { class ThreadSafeRefCounted : public ThreadSafeRefCountedBase {
public: public:
void Deref() { void Deref() {
if (DerefBase()) if (DerefBase())
delete static_cast<T*>(this); Traits::Destruct(static_cast<T*>(this));
} }
protected: protected:
ThreadSafeRefCounted() {} ThreadSafeRefCounted() {}
private:
friend struct DefaultThreadSafeRefCountedTraits<T>;
static void DeleteInternal(const T* x) { delete x; }
}; };
} // namespace WTF } // namespace WTF
......
...@@ -41,7 +41,7 @@ ...@@ -41,7 +41,7 @@
#endif #endif
namespace WTF { namespace WTF {
template <class T> template <class T, typename Traits>
class ThreadSafeRefCounted; class ThreadSafeRefCounted;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment