Sfoglia il codice sorgente

LibWeb: Make DOMImplementation forward its ref count to DOM::Document

This allows document.implementation to keep the underlying document
alive for as long as we need it (for example, if someone holds on to a
DOMImplementation JS wrapper after the document is GC'd.)
Andreas Kling 3 anni fa
parent
commit
d368b08698

+ 5 - 5
Userland/Libraries/LibWeb/DOM/DOMImplementation.cpp

@@ -15,7 +15,7 @@
 namespace Web::DOM {
 
 DOMImplementation::DOMImplementation(Document& document)
-    : m_document(document)
+    : RefCountForwarder(document)
 {
 }
 
@@ -37,7 +37,7 @@ NonnullRefPtr<Document> DOMImplementation::create_document(const String& namespa
     if (element)
         xml_document->append_child(element.release_nonnull());
 
-    xml_document->set_origin(m_document.origin());
+    xml_document->set_origin(document().origin());
 
     if (namespace_ == Namespace::HTML)
         xml_document->set_content_type("application/xhtml+xml");
@@ -79,16 +79,16 @@ NonnullRefPtr<Document> DOMImplementation::create_html_document(const String& ti
     auto body_element = create_element(html_document, HTML::TagNames::body, Namespace::HTML);
     html_element->append_child(body_element);
 
-    html_document->set_origin(m_document.origin());
+    html_document->set_origin(document().origin());
 
     return html_document;
 }
 
 // https://dom.spec.whatwg.org/#dom-domimplementation-createdocumenttype
-NonnullRefPtr<DocumentType> DOMImplementation::create_document_type(const String& qualified_name, const String& public_id, const String& system_id) const
+NonnullRefPtr<DocumentType> DOMImplementation::create_document_type(String const& qualified_name, String const& public_id, String const& system_id)
 {
     // FIXME: Validate qualified_name.
-    auto document_type = DocumentType::create(m_document);
+    auto document_type = DocumentType::create(document());
     document_type->set_name(qualified_name);
     document_type->set_public_id(public_id);
     document_type->set_system_id(system_id);

+ 8 - 5
Userland/Libraries/LibWeb/DOM/DOMImplementation.h

@@ -7,28 +7,30 @@
 #pragma once
 
 #include <AK/NonnullRefPtr.h>
+#include <AK/RefCountForwarder.h>
 #include <AK/RefCounted.h>
 #include <AK/Weakable.h>
 #include <LibWeb/Bindings/Wrappable.h>
+#include <LibWeb/DOM/Document.h>
 
 namespace Web::DOM {
 
 class DOMImplementation final
-    : public RefCounted<DOMImplementation>
+    : public RefCountForwarder<Document>
     , public Weakable<DOMImplementation>
     , public Bindings::Wrappable {
 public:
     using WrapperType = Bindings::DOMImplementationWrapper;
 
-    static NonnullRefPtr<DOMImplementation> create(Document& document)
+    static NonnullOwnPtr<DOMImplementation> create(Badge<Document>, Document& document)
     {
-        return adopt_ref(*new DOMImplementation(document));
+        return adopt_own(*new DOMImplementation(document));
     }
 
     // FIXME: Add optional DocumentType once supported by IDL
     NonnullRefPtr<Document> create_document(const String&, const String&) const;
     NonnullRefPtr<Document> create_html_document(const String& title) const;
-    NonnullRefPtr<DocumentType> create_document_type(const String&, const String&, const String&) const;
+    NonnullRefPtr<DocumentType> create_document_type(String const& qualified_name, String const& public_id, String const& system_id);
 
     // https://dom.spec.whatwg.org/#dom-domimplementation-hasfeature
     bool has_feature() const { return true; }
@@ -36,7 +38,8 @@ public:
 private:
     explicit DOMImplementation(Document&);
 
-    Document& m_document;
+    Document& document() { return ref_count_target(); }
+    Document const& document() const { return ref_count_target(); }
 };
 
 }

+ 7 - 1
Userland/Libraries/LibWeb/DOM/Document.cpp

@@ -22,6 +22,7 @@
 #include <LibWeb/DOM/Comment.h>
 #include <LibWeb/DOM/CustomEvent.h>
 #include <LibWeb/DOM/DOMException.h>
+#include <LibWeb/DOM/DOMImplementation.h>
 #include <LibWeb/DOM/Document.h>
 #include <LibWeb/DOM/DocumentFragment.h>
 #include <LibWeb/DOM/DocumentType.h>
@@ -71,7 +72,7 @@ Document::Document(const AK::URL& url)
     , m_style_sheets(CSS::StyleSheetList::create(*this))
     , m_url(url)
     , m_window(Window::create_with_document(*this))
-    , m_implementation(DOMImplementation::create(*this))
+    , m_implementation(DOMImplementation::create({}, *this))
     , m_history(HTML::History::create(*this))
 {
     HTML::main_thread_event_loop().register_document({}, *this);
@@ -1136,4 +1137,9 @@ void Document::evaluate_media_queries_and_report_changes()
     }
 }
 
+NonnullRefPtr<DOMImplementation> Document::implementation() const
+{
+    return *m_implementation;
+}
+
 }

+ 2 - 3
Userland/Libraries/LibWeb/DOM/Document.h

@@ -22,7 +22,6 @@
 #include <LibWeb/CSS/StyleComputer.h>
 #include <LibWeb/CSS/StyleSheetList.h>
 #include <LibWeb/Cookie/Cookie.h>
-#include <LibWeb/DOM/DOMImplementation.h>
 #include <LibWeb/DOM/ExceptionOr.h>
 #include <LibWeb/DOM/NonElementParentNode.h>
 #include <LibWeb/DOM/ParentNode.h>
@@ -264,7 +263,7 @@ public:
 
     void completely_finish_loading();
 
-    const NonnullRefPtr<DOMImplementation> implementation() const { return m_implementation; }
+    NonnullRefPtr<DOMImplementation> implementation() const;
 
     RefPtr<HTML::HTMLScriptElement> current_script() const { return m_current_script; }
     void set_current_script(Badge<HTML::HTMLScriptElement>, RefPtr<HTML::HTMLScriptElement> script) { m_current_script = move(script); }
@@ -376,7 +375,7 @@ private:
 
     bool m_ready_for_post_load_tasks { false };
 
-    NonnullRefPtr<DOMImplementation> m_implementation;
+    NonnullOwnPtr<DOMImplementation> m_implementation;
     RefPtr<HTML::HTMLScriptElement> m_current_script;
 
     bool m_should_invalidate_styles_on_attribute_changes { true };