Browse Source

LibWeb: Make PerformanceTiming GC-allocated

Andreas Kling 2 years ago
parent
commit
5f4d4ffe39

+ 0 - 1
Userland/Libraries/LibWeb/Forward.h

@@ -474,7 +474,6 @@ class MutationRecordWrapper;
 class NodeListWrapper;
 class OptionConstructor;
 class Path2DWrapper;
-class PerformanceTimingWrapper;
 class RangePrototype;
 class ResizeObserverWrapper;
 class SelectionWrapper;

+ 9 - 1
Userland/Libraries/LibWeb/HighResolutionTime/Performance.cpp

@@ -10,13 +10,13 @@
 #include <LibWeb/DOM/EventDispatcher.h>
 #include <LibWeb/HTML/Window.h>
 #include <LibWeb/HighResolutionTime/Performance.h>
+#include <LibWeb/NavigationTiming/PerformanceTiming.h>
 
 namespace Web::HighResolutionTime {
 
 Performance::Performance(HTML::Window& window)
     : DOM::EventTarget(window.realm())
     , m_window(window)
-    , m_timing(make<NavigationTiming::PerformanceTiming>(window))
 {
     set_prototype(&window.ensure_web_prototype<Bindings::PerformancePrototype>("Performance"));
     m_timer.start();
@@ -28,6 +28,14 @@ void Performance::visit_edges(Cell::Visitor& visitor)
 {
     Base::visit_edges(visitor);
     visitor.visit(m_window.ptr());
+    visitor.visit(m_timing.ptr());
+}
+
+JS::GCPtr<NavigationTiming::PerformanceTiming> Performance::timing()
+{
+    if (!m_timing)
+        m_timing = heap().allocate<NavigationTiming::PerformanceTiming>(realm(), *m_window);
+    return m_timing;
 }
 
 double Performance::time_origin() const

+ 2 - 4
Userland/Libraries/LibWeb/HighResolutionTime/Performance.h

@@ -10,7 +10,6 @@
 #include <LibCore/ElapsedTimer.h>
 #include <LibWeb/Bindings/Wrappable.h>
 #include <LibWeb/DOM/EventTarget.h>
-#include <LibWeb/NavigationTiming/PerformanceTiming.h>
 
 namespace Web::HighResolutionTime {
 
@@ -23,7 +22,7 @@ public:
     double now() const { return m_timer.elapsed(); }
     double time_origin() const;
 
-    JS::GCPtr<NavigationTiming::PerformanceTiming> timing() { return *m_timing; }
+    JS::GCPtr<NavigationTiming::PerformanceTiming> timing();
 
 private:
     explicit Performance(HTML::Window&);
@@ -31,10 +30,9 @@ private:
     virtual void visit_edges(Cell::Visitor&) override;
 
     JS::NonnullGCPtr<HTML::Window> m_window;
+    JS::GCPtr<NavigationTiming::PerformanceTiming> m_timing;
 
     Core::ElapsedTimer m_timer;
-
-    OwnPtr<NavigationTiming::PerformanceTiming> m_timing;
 };
 
 }

+ 9 - 1
Userland/Libraries/LibWeb/NavigationTiming/PerformanceTiming.cpp

@@ -9,10 +9,18 @@
 namespace Web::NavigationTiming {
 
 PerformanceTiming::PerformanceTiming(HTML::Window& window)
-    : m_window(JS::make_handle(window))
+    : PlatformObject(window.realm())
+    , m_window(window)
 {
+    set_prototype(&window.cached_web_prototype("PerformanceTiming"));
 }
 
 PerformanceTiming::~PerformanceTiming() = default;
 
+void PerformanceTiming::visit_edges(Cell::Visitor& visitor)
+{
+    Base::visit_edges(visitor);
+    visitor.visit(m_window.ptr());
+}
+
 }

+ 10 - 7
Userland/Libraries/LibWeb/NavigationTiming/PerformanceTiming.h

@@ -6,21 +6,18 @@
 
 #pragma once
 
-#include <AK/RefCountForwarder.h>
 #include <AK/StdLibExtras.h>
 #include <LibWeb/Bindings/Wrappable.h>
 #include <LibWeb/HTML/Window.h>
 
 namespace Web::NavigationTiming {
 
-class PerformanceTiming final
-    : public RefCounted<PerformanceTiming>
-    , public Bindings::Wrappable {
+class PerformanceTiming final : public Bindings::PlatformObject {
+    WEB_PLATFORM_OBJECT(PerformanceTiming, Bindings::PlatformObject);
+
 public:
-    using WrapperType = Bindings::PerformanceTimingWrapper;
     using AllowOwnPtr = TrueType;
 
-    explicit PerformanceTiming(HTML::Window&);
     ~PerformanceTiming();
 
     u64 navigation_start() { return 0; }
@@ -46,7 +43,13 @@ public:
     u64 load_event_end() { return 0; }
 
 private:
-    JS::Handle<HTML::Window> m_window;
+    explicit PerformanceTiming(HTML::Window&);
+
+    virtual void visit_edges(Cell::Visitor&) override;
+
+    JS::GCPtr<HTML::Window> m_window;
 };
 
 }
+
+WRAPPER_HACK(PerformanceTiming, Web::NavigationTiming)

+ 1 - 1
Userland/Libraries/LibWeb/idl_files.cmake

@@ -156,7 +156,7 @@ libweb_js_wrapper(HTML/WorkerLocation)
 libweb_js_wrapper(HTML/WorkerNavigator)
 libweb_js_wrapper(HighResolutionTime/Performance NO_INSTANCE)
 libweb_js_wrapper(IntersectionObserver/IntersectionObserver)
-libweb_js_wrapper(NavigationTiming/PerformanceTiming)
+libweb_js_wrapper(NavigationTiming/PerformanceTiming NO_INSTANCE)
 libweb_js_wrapper(RequestIdleCallback/IdleDeadline)
 libweb_js_wrapper(ResizeObserver/ResizeObserver)
 libweb_js_wrapper(SVG/SVGAnimatedLength)