mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 15:12:45 +00:00 
			
		
		
		
	LibWeb: Let Document have a direct GCPtr to its containing Web::Page
With this change, Document now always has a Web::Page. This means we no longer rely on the breakable link between Document and BrowsingContext to find a relevant Web::Page. Fixes #22290
This commit is contained in:
		
							parent
							
								
									b2b5297997
								
							
						
					
					
						commit
						70193c0009
					
				
					 12 changed files with 35 additions and 10 deletions
				
			
		|  | @ -0,0 +1,2 @@ | ||||||
|  | PASS! (Didn't crash) | ||||||
|  | data:text/png, | ||||||
|  | @ -0,0 +1,9 @@ | ||||||
|  | <script src="../include.js"></script> | ||||||
|  | <script> | ||||||
|  |     test(() => { | ||||||
|  |         let e = document.createElement("div"); | ||||||
|  |         e.innerHTML = "<img src='data:text/png,'>"; | ||||||
|  |         println("PASS! (Didn't crash)"); | ||||||
|  |         println(e.firstChild.src); | ||||||
|  |     }); | ||||||
|  | </script> | ||||||
|  | @ -9,6 +9,7 @@ | ||||||
| #include <LibWeb/Bindings/HostDefined.h> | #include <LibWeb/Bindings/HostDefined.h> | ||||||
| #include <LibWeb/Bindings/Intrinsics.h> | #include <LibWeb/Bindings/Intrinsics.h> | ||||||
| #include <LibWeb/HTML/Scripting/Environments.h> | #include <LibWeb/HTML/Scripting/Environments.h> | ||||||
|  | #include <LibWeb/Page/Page.h> | ||||||
| 
 | 
 | ||||||
| namespace Web::Bindings { | namespace Web::Bindings { | ||||||
| 
 | 
 | ||||||
|  | @ -17,6 +18,7 @@ void HostDefined::visit_edges(JS::Cell::Visitor& visitor) | ||||||
|     JS::Realm::HostDefined::visit_edges(visitor); |     JS::Realm::HostDefined::visit_edges(visitor); | ||||||
|     visitor.visit(environment_settings_object); |     visitor.visit(environment_settings_object); | ||||||
|     visitor.visit(intrinsics); |     visitor.visit(intrinsics); | ||||||
|  |     visitor.visit(page); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -14,9 +14,10 @@ | ||||||
| namespace Web::Bindings { | namespace Web::Bindings { | ||||||
| 
 | 
 | ||||||
| struct HostDefined : public JS::Realm::HostDefined { | struct HostDefined : public JS::Realm::HostDefined { | ||||||
|     HostDefined(JS::NonnullGCPtr<HTML::EnvironmentSettingsObject> eso, JS::NonnullGCPtr<Intrinsics> intrinsics) |     HostDefined(JS::NonnullGCPtr<HTML::EnvironmentSettingsObject> eso, JS::NonnullGCPtr<Intrinsics> intrinsics, JS::NonnullGCPtr<Page> page) | ||||||
|         : environment_settings_object(eso) |         : environment_settings_object(eso) | ||||||
|         , intrinsics(intrinsics) |         , intrinsics(intrinsics) | ||||||
|  |         , page(page) | ||||||
|     { |     { | ||||||
|     } |     } | ||||||
|     virtual ~HostDefined() override = default; |     virtual ~HostDefined() override = default; | ||||||
|  | @ -24,6 +25,7 @@ struct HostDefined : public JS::Realm::HostDefined { | ||||||
| 
 | 
 | ||||||
|     JS::NonnullGCPtr<HTML::EnvironmentSettingsObject> environment_settings_object; |     JS::NonnullGCPtr<HTML::EnvironmentSettingsObject> environment_settings_object; | ||||||
|     JS::NonnullGCPtr<Intrinsics> intrinsics; |     JS::NonnullGCPtr<Intrinsics> intrinsics; | ||||||
|  |     JS::NonnullGCPtr<Page> page; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| [[nodiscard]] inline HTML::EnvironmentSettingsObject& host_defined_environment_settings_object(JS::Realm& realm) | [[nodiscard]] inline HTML::EnvironmentSettingsObject& host_defined_environment_settings_object(JS::Realm& realm) | ||||||
|  | @ -31,4 +33,9 @@ struct HostDefined : public JS::Realm::HostDefined { | ||||||
|     return *verify_cast<HostDefined>(realm.host_defined())->environment_settings_object; |     return *verify_cast<HostDefined>(realm.host_defined())->environment_settings_object; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | [[nodiscard]] inline Page& host_defined_page(JS::Realm& realm) | ||||||
|  | { | ||||||
|  |     return *verify_cast<HostDefined>(realm.host_defined())->page; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -236,6 +236,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Document>> Document::create_and_initialize( | ||||||
|         // FIXME: Why do we assume `creation_url` is non-empty here? Is this a spec bug?
 |         // FIXME: Why do we assume `creation_url` is non-empty here? Is this a spec bug?
 | ||||||
|         // FIXME: Why do we assume `top_level_creation_url` is non-empty here? Is this a spec bug?
 |         // FIXME: Why do we assume `top_level_creation_url` is non-empty here? Is this a spec bug?
 | ||||||
|         HTML::WindowEnvironmentSettingsObject::setup( |         HTML::WindowEnvironmentSettingsObject::setup( | ||||||
|  |             browsing_context->page(), | ||||||
|             creation_url.value(), |             creation_url.value(), | ||||||
|             move(realm_execution_context), |             move(realm_execution_context), | ||||||
|             navigation_params.reserved_environment.visit( |             navigation_params.reserved_environment.visit( | ||||||
|  | @ -321,6 +322,7 @@ JS::NonnullGCPtr<Document> Document::create(JS::Realm& realm, AK::URL const& url | ||||||
| 
 | 
 | ||||||
| Document::Document(JS::Realm& realm, const AK::URL& url) | Document::Document(JS::Realm& realm, const AK::URL& url) | ||||||
|     : ParentNode(realm, *this, NodeType::DOCUMENT_NODE) |     : ParentNode(realm, *this, NodeType::DOCUMENT_NODE) | ||||||
|  |     , m_page(Bindings::host_defined_page(realm)) | ||||||
|     , m_style_computer(make<CSS::StyleComputer>(*this)) |     , m_style_computer(make<CSS::StyleComputer>(*this)) | ||||||
|     , m_url(url) |     , m_url(url) | ||||||
| { | { | ||||||
|  | @ -353,6 +355,7 @@ void Document::initialize(JS::Realm& realm) | ||||||
| void Document::visit_edges(Cell::Visitor& visitor) | void Document::visit_edges(Cell::Visitor& visitor) | ||||||
| { | { | ||||||
|     Base::visit_edges(visitor); |     Base::visit_edges(visitor); | ||||||
|  |     visitor.visit(m_page); | ||||||
|     visitor.visit(m_window); |     visitor.visit(m_window); | ||||||
|     visitor.visit(m_layout_root); |     visitor.visit(m_layout_root); | ||||||
|     visitor.visit(m_style_sheets); |     visitor.visit(m_style_sheets); | ||||||
|  | @ -1908,12 +1911,12 @@ void Document::update_readiness(HTML::DocumentReadyState readiness_value) | ||||||
| 
 | 
 | ||||||
| Page* Document::page() | Page* Document::page() | ||||||
| { | { | ||||||
|     return m_browsing_context ? &m_browsing_context->page() : nullptr; |     return m_page; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| Page const* Document::page() const | Page const* Document::page() const | ||||||
| { | { | ||||||
|     return m_browsing_context ? &m_browsing_context->page() : nullptr; |     return m_page; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| EventTarget* Document::get_parent(Event const& event) | EventTarget* Document::get_parent(Event const& event) | ||||||
|  |  | ||||||
|  | @ -572,6 +572,7 @@ private: | ||||||
| 
 | 
 | ||||||
|     Element* find_a_potential_indicated_element(FlyString const& fragment) const; |     Element* find_a_potential_indicated_element(FlyString const& fragment) const; | ||||||
| 
 | 
 | ||||||
|  |     JS::NonnullGCPtr<Page> m_page; | ||||||
|     OwnPtr<CSS::StyleComputer> m_style_computer; |     OwnPtr<CSS::StyleComputer> m_style_computer; | ||||||
|     JS::GCPtr<CSS::StyleSheetList> m_style_sheets; |     JS::GCPtr<CSS::StyleSheetList> m_style_sheets; | ||||||
|     JS::GCPtr<Node> m_hovered_node; |     JS::GCPtr<Node> m_hovered_node; | ||||||
|  |  | ||||||
|  | @ -175,6 +175,7 @@ WebIDL::ExceptionOr<BrowsingContext::BrowsingContextAndDocument> BrowsingContext | ||||||
| 
 | 
 | ||||||
|     // 12. Set up a window environment settings object with about:blank, realm execution context, null, topLevelCreationURL, and topLevelOrigin.
 |     // 12. Set up a window environment settings object with about:blank, realm execution context, null, topLevelCreationURL, and topLevelOrigin.
 | ||||||
|     WindowEnvironmentSettingsObject::setup( |     WindowEnvironmentSettingsObject::setup( | ||||||
|  |         page, | ||||||
|         AK::URL("about:blank"), |         AK::URL("about:blank"), | ||||||
|         move(realm_execution_context), |         move(realm_execution_context), | ||||||
|         {}, |         {}, | ||||||
|  |  | ||||||
|  | @ -29,7 +29,7 @@ void WindowEnvironmentSettingsObject::visit_edges(JS::Cell::Visitor& visitor) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // https://html.spec.whatwg.org/multipage/window-object.html#set-up-a-window-environment-settings-object
 | // https://html.spec.whatwg.org/multipage/window-object.html#set-up-a-window-environment-settings-object
 | ||||||
| void WindowEnvironmentSettingsObject::setup(AK::URL const& creation_url, NonnullOwnPtr<JS::ExecutionContext> execution_context, Optional<Environment> reserved_environment, AK::URL top_level_creation_url, Origin top_level_origin) | void WindowEnvironmentSettingsObject::setup(Page& page, AK::URL const& creation_url, NonnullOwnPtr<JS::ExecutionContext> execution_context, Optional<Environment> reserved_environment, AK::URL top_level_creation_url, Origin top_level_origin) | ||||||
| { | { | ||||||
|     // 1. Let realm be the value of execution context's Realm component.
 |     // 1. Let realm be the value of execution context's Realm component.
 | ||||||
|     auto realm = execution_context->realm; |     auto realm = execution_context->realm; | ||||||
|  | @ -74,7 +74,7 @@ void WindowEnvironmentSettingsObject::setup(AK::URL const& creation_url, Nonnull | ||||||
|     // 7. Set realm's [[HostDefined]] field to settings object.
 |     // 7. Set realm's [[HostDefined]] field to settings object.
 | ||||||
|     // Non-Standard: We store the ESO next to the web intrinsics in a custom HostDefined object
 |     // Non-Standard: We store the ESO next to the web intrinsics in a custom HostDefined object
 | ||||||
|     auto intrinsics = realm->heap().allocate<Bindings::Intrinsics>(*realm, *realm); |     auto intrinsics = realm->heap().allocate<Bindings::Intrinsics>(*realm, *realm); | ||||||
|     auto host_defined = make<Bindings::HostDefined>(settings_object, intrinsics); |     auto host_defined = make<Bindings::HostDefined>(settings_object, intrinsics, page); | ||||||
|     realm->set_host_defined(move(host_defined)); |     realm->set_host_defined(move(host_defined)); | ||||||
| 
 | 
 | ||||||
|     // Non-Standard: We cannot fully initialize window object until *after* the we set up
 |     // Non-Standard: We cannot fully initialize window object until *after* the we set up
 | ||||||
|  |  | ||||||
|  | @ -16,7 +16,7 @@ class WindowEnvironmentSettingsObject final : public EnvironmentSettingsObject { | ||||||
|     JS_DECLARE_ALLOCATOR(WindowEnvironmentSettingsObject); |     JS_DECLARE_ALLOCATOR(WindowEnvironmentSettingsObject); | ||||||
| 
 | 
 | ||||||
| public: | public: | ||||||
|     static void setup(AK::URL const& creation_url, NonnullOwnPtr<JS::ExecutionContext>, Optional<Environment>, AK::URL top_level_creation_url, Origin top_level_origin); |     static void setup(Page&, AK::URL const& creation_url, NonnullOwnPtr<JS::ExecutionContext>, Optional<Environment>, AK::URL top_level_creation_url, Origin top_level_origin); | ||||||
| 
 | 
 | ||||||
|     virtual ~WindowEnvironmentSettingsObject() override; |     virtual ~WindowEnvironmentSettingsObject() override; | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -11,7 +11,7 @@ namespace Web::HTML { | ||||||
| 
 | 
 | ||||||
| JS_DEFINE_ALLOCATOR(WorkerEnvironmentSettingsObject); | JS_DEFINE_ALLOCATOR(WorkerEnvironmentSettingsObject); | ||||||
| 
 | 
 | ||||||
| JS::NonnullGCPtr<WorkerEnvironmentSettingsObject> WorkerEnvironmentSettingsObject::setup(NonnullOwnPtr<JS::ExecutionContext> execution_context /* FIXME: null or an environment reservedEnvironment, a URL topLevelCreationURL, and an origin topLevelOrigin */) | JS::NonnullGCPtr<WorkerEnvironmentSettingsObject> WorkerEnvironmentSettingsObject::setup(JS::NonnullGCPtr<Page> page, NonnullOwnPtr<JS::ExecutionContext> execution_context /* FIXME: null or an environment reservedEnvironment, a URL topLevelCreationURL, and an origin topLevelOrigin */) | ||||||
| { | { | ||||||
|     auto realm = execution_context->realm; |     auto realm = execution_context->realm; | ||||||
|     VERIFY(realm); |     VERIFY(realm); | ||||||
|  | @ -22,7 +22,7 @@ JS::NonnullGCPtr<WorkerEnvironmentSettingsObject> WorkerEnvironmentSettingsObjec | ||||||
|     settings_object->target_browsing_context = nullptr; |     settings_object->target_browsing_context = nullptr; | ||||||
| 
 | 
 | ||||||
|     auto intrinsics = realm->heap().allocate<Bindings::Intrinsics>(*realm, *realm); |     auto intrinsics = realm->heap().allocate<Bindings::Intrinsics>(*realm, *realm); | ||||||
|     auto host_defined = make<Bindings::HostDefined>(settings_object, intrinsics); |     auto host_defined = make<Bindings::HostDefined>(settings_object, intrinsics, page); | ||||||
|     realm->set_host_defined(move(host_defined)); |     realm->set_host_defined(move(host_defined)); | ||||||
| 
 | 
 | ||||||
|     // Non-Standard: We cannot fully initialize worker object until *after* the we set up
 |     // Non-Standard: We cannot fully initialize worker object until *after* the we set up
 | ||||||
|  |  | ||||||
|  | @ -24,7 +24,7 @@ public: | ||||||
|     { |     { | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     static JS::NonnullGCPtr<WorkerEnvironmentSettingsObject> setup(NonnullOwnPtr<JS::ExecutionContext> execution_context /* FIXME: null or an environment reservedEnvironment, a URL topLevelCreationURL, and an origin topLevelOrigin */); |     static JS::NonnullGCPtr<WorkerEnvironmentSettingsObject> setup(JS::NonnullGCPtr<Page> page, NonnullOwnPtr<JS::ExecutionContext> execution_context /* FIXME: null or an environment reservedEnvironment, a URL topLevelCreationURL, and an origin topLevelOrigin */); | ||||||
| 
 | 
 | ||||||
|     virtual ~WorkerEnvironmentSettingsObject() override = default; |     virtual ~WorkerEnvironmentSettingsObject() override = default; | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -55,7 +55,7 @@ void DedicatedWorkerHost::run() | ||||||
| 
 | 
 | ||||||
|     // 9. Set up a worker environment settings object with realm execution context,
 |     // 9. Set up a worker environment settings object with realm execution context,
 | ||||||
|     //    outside settings, and unsafeWorkerCreationTime, and let inside settings be the result.
 |     //    outside settings, and unsafeWorkerCreationTime, and let inside settings be the result.
 | ||||||
|     auto inner_settings = Web::HTML::WorkerEnvironmentSettingsObject::setup(move(realm_execution_context)); |     auto inner_settings = Web::HTML::WorkerEnvironmentSettingsObject::setup(m_page, move(realm_execution_context)); | ||||||
| 
 | 
 | ||||||
|     auto& console_object = *inner_settings->realm().intrinsics().console_object(); |     auto& console_object = *inner_settings->realm().intrinsics().console_object(); | ||||||
|     m_console = adopt_ref(*new Web::HTML::WorkerDebugConsoleClient(console_object.console())); |     m_console = adopt_ref(*new Web::HTML::WorkerDebugConsoleClient(console_object.console())); | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Andreas Kling
						Andreas Kling