LibWeb: Use smart pointers between DOM and Layout tree

DOM::Node now points to its LayoutNode with a WeakPtr.
LayoutNode points to its DOM::Node and DOM::Document with RefPtrs.

Layout trees come and go in response to various events, so the DOM tree
already has to deal with that. The DOM should always live at least as
long as the layout tree, so this patch enforces that assumption by
making layout nodes keep their corresponding DOM objects alive.

This may not be optimal, but it removes a lot of ambiguous raw pointer
action which is not worth accomodating.
This commit is contained in:
Andreas Kling 2020-10-22 20:26:32 +02:00
parent a316ca0e0d
commit 385d744989
Notes: sideshowbarker 2024-07-19 01:48:06 +09:00
3 changed files with 12 additions and 4 deletions

View file

@ -217,4 +217,12 @@ void Node::removed_last_ref()
delete this;
}
void Node::set_layout_node(Badge<LayoutNode>, LayoutNode* layout_node) const
{
if (layout_node)
m_layout_node = layout_node->make_weak_ptr();
else
m_layout_node = nullptr;
}
}

View file

@ -118,7 +118,7 @@ public:
const LayoutNode* layout_node() const { return m_layout_node; }
LayoutNode* layout_node() { return m_layout_node; }
void set_layout_node(Badge<LayoutNode>, LayoutNode* layout_node) const { m_layout_node = layout_node; }
void set_layout_node(Badge<LayoutNode>, LayoutNode*) const;
virtual bool is_child_allowed(const Node&) const { return true; }
@ -138,7 +138,7 @@ protected:
Node(Document&, NodeType);
Document* m_document { nullptr };
mutable LayoutNode* m_layout_node { nullptr };
mutable WeakPtr<LayoutNode> m_layout_node;
NodeType m_type { NodeType::INVALID };
bool m_needs_style_update { true };
};

View file

@ -178,8 +178,8 @@ protected:
private:
friend class LayoutNodeWithStyle;
DOM::Document& m_document;
DOM::Node* m_node { nullptr };
NonnullRefPtr<DOM::Document> m_document;
RefPtr<DOM::Node> m_node;
bool m_inline { false };
bool m_has_style { false };