From 385d7449896171d6df90dae144ed1a3dac4528c6 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 22 Oct 2020 20:26:32 +0200 Subject: [PATCH] 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. --- Libraries/LibWeb/DOM/Node.cpp | 8 ++++++++ Libraries/LibWeb/DOM/Node.h | 4 ++-- Libraries/LibWeb/Layout/LayoutNode.h | 4 ++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Libraries/LibWeb/DOM/Node.cpp b/Libraries/LibWeb/DOM/Node.cpp index ac5b349429..a06a6e9e50 100644 --- a/Libraries/LibWeb/DOM/Node.cpp +++ b/Libraries/LibWeb/DOM/Node.cpp @@ -217,4 +217,12 @@ void Node::removed_last_ref() delete this; } +void Node::set_layout_node(Badge, LayoutNode* layout_node) const +{ + if (layout_node) + m_layout_node = layout_node->make_weak_ptr(); + else + m_layout_node = nullptr; +} + } diff --git a/Libraries/LibWeb/DOM/Node.h b/Libraries/LibWeb/DOM/Node.h index df6e301647..9b2573c3fc 100644 --- a/Libraries/LibWeb/DOM/Node.h +++ b/Libraries/LibWeb/DOM/Node.h @@ -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* layout_node) const { m_layout_node = layout_node; } + void set_layout_node(Badge, 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 m_layout_node; NodeType m_type { NodeType::INVALID }; bool m_needs_style_update { true }; }; diff --git a/Libraries/LibWeb/Layout/LayoutNode.h b/Libraries/LibWeb/Layout/LayoutNode.h index 1b72b5645b..8b955288af 100644 --- a/Libraries/LibWeb/Layout/LayoutNode.h +++ b/Libraries/LibWeb/Layout/LayoutNode.h @@ -178,8 +178,8 @@ protected: private: friend class LayoutNodeWithStyle; - DOM::Document& m_document; - DOM::Node* m_node { nullptr }; + NonnullRefPtr m_document; + RefPtr m_node; bool m_inline { false }; bool m_has_style { false };