diff options
author | Andreas Kling <kling@serenityos.org> | 2021-12-09 12:51:05 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-12-09 21:28:52 +0100 |
commit | d368b0869822cb554238ed66d6e4cd9e8659f949 (patch) | |
tree | 2f200588c45f3ec3646124839d3394297304e4b1 /Userland/Libraries/LibWeb/DOM | |
parent | e1287a9a45f3b7faaa34dde0bf9357bd05d99679 (diff) | |
download | serenity-d368b0869822cb554238ed66d6e4cd9e8659f949.zip |
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.)
Diffstat (limited to 'Userland/Libraries/LibWeb/DOM')
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/DOMImplementation.cpp | 10 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/DOMImplementation.h | 13 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Document.cpp | 8 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Document.h | 5 |
4 files changed, 22 insertions, 14 deletions
diff --git a/Userland/Libraries/LibWeb/DOM/DOMImplementation.cpp b/Userland/Libraries/LibWeb/DOM/DOMImplementation.cpp index 7dab0f5c62..d064b761e6 100644 --- a/Userland/Libraries/LibWeb/DOM/DOMImplementation.cpp +++ b/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); diff --git a/Userland/Libraries/LibWeb/DOM/DOMImplementation.h b/Userland/Libraries/LibWeb/DOM/DOMImplementation.h index 34762c3dd9..e35ae20ead 100644 --- a/Userland/Libraries/LibWeb/DOM/DOMImplementation.h +++ b/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(); } }; } diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index c6d5bc1367..75fcb34f47 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/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; +} + } diff --git a/Userland/Libraries/LibWeb/DOM/Document.h b/Userland/Libraries/LibWeb/DOM/Document.h index 4c4330cfce..9f374ec385 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.h +++ b/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 }; |