PerformanceMark.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/Intrinsics.h>
  7. #include <LibWeb/Bindings/PerformanceMarkPrototype.h>
  8. #include <LibWeb/HTML/StructuredSerialize.h>
  9. #include <LibWeb/HTML/Window.h>
  10. #include <LibWeb/HighResolutionTime/TimeOrigin.h>
  11. #include <LibWeb/NavigationTiming/EntryNames.h>
  12. #include <LibWeb/PerformanceTimeline/EntryTypes.h>
  13. #include <LibWeb/UserTiming/PerformanceMark.h>
  14. #include <LibWeb/WebIDL/ExceptionOr.h>
  15. namespace Web::UserTiming {
  16. JS_DEFINE_ALLOCATOR(PerformanceMark);
  17. PerformanceMark::PerformanceMark(JS::Realm& realm, String const& name, HighResolutionTime::DOMHighResTimeStamp start_time, HighResolutionTime::DOMHighResTimeStamp duration, JS::Value detail)
  18. : PerformanceTimeline::PerformanceEntry(realm, name, start_time, duration)
  19. , m_detail(detail)
  20. {
  21. }
  22. PerformanceMark::~PerformanceMark() = default;
  23. // https://w3c.github.io/user-timing/#dfn-performancemark-constructor
  24. WebIDL::ExceptionOr<JS::NonnullGCPtr<PerformanceMark>> PerformanceMark::construct_impl(JS::Realm& realm, String const& mark_name, Web::UserTiming::PerformanceMarkOptions const& mark_options)
  25. {
  26. auto& current_global_object = realm.global_object();
  27. auto& vm = realm.vm();
  28. // 1. If the current global object is a Window object and markName uses the same name as a read only attribute in the PerformanceTiming interface, throw a SyntaxError.
  29. if (is<HTML::Window>(current_global_object)) {
  30. bool matched = false;
  31. #define __ENUMERATE_NAVIGATION_TIMING_ENTRY_NAME(name, _) \
  32. if (mark_name == NavigationTiming::EntryNames::name) \
  33. matched = true;
  34. ENUMERATE_NAVIGATION_TIMING_ENTRY_NAMES
  35. #undef __ENUMERATE_NAVIGATION_TIMING_ENTRY_NAME
  36. if (matched)
  37. return WebIDL::SyntaxError::create(realm, MUST(String::formatted("'{}' markName cannot be used in a Window context because it is part of the PerformanceTiming interface", mark_name)));
  38. }
  39. // NOTE: Step 2 (creating the entry) is done after determining values, as we set the values once during creation and never change them after.
  40. // 3. Set entry's name attribute to markName.
  41. auto const& name = mark_name;
  42. // 4. Set entry's entryType attribute to DOMString "mark".
  43. // NOTE: Already done via the `entry_type` virtual function.
  44. // 5. Set entry's startTime attribute as follows:
  45. HighResolutionTime::DOMHighResTimeStamp start_time { 0.0 };
  46. // 1. If markOptions's startTime member is present, then:
  47. if (mark_options.start_time.has_value()) {
  48. // 1. If markOptions's startTime is negative, throw a TypeError.
  49. if (mark_options.start_time.value() < 0.0)
  50. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "startTime cannot be negative"sv };
  51. // 2. Otherwise, set entry's startTime to the value of markOptions's startTime.
  52. start_time = mark_options.start_time.value();
  53. }
  54. // 2. Otherwise, set it to the value that would be returned by the Performance object's now() method.
  55. else {
  56. // FIXME: Performance#now doesn't currently use TimeOrigin's functions, update this and Performance#now to match Performance#now's specification.
  57. start_time = HighResolutionTime::unsafe_shared_current_time();
  58. }
  59. // 6. Set entry's duration attribute to 0.
  60. constexpr HighResolutionTime::DOMHighResTimeStamp duration = 0.0;
  61. // 7. If markOptions's detail is null, set entry's detail to null.
  62. JS::Value detail;
  63. if (mark_options.detail.is_null()) {
  64. detail = JS::js_null();
  65. }
  66. // 8. Otherwise:
  67. else {
  68. // 1. Let record be the result of calling the StructuredSerialize algorithm on markOptions's detail.
  69. auto record = TRY(HTML::structured_serialize(vm, mark_options.detail));
  70. // 2. Set entry's detail to the result of calling the StructuredDeserialize algorithm on record and the current realm.
  71. detail = TRY(HTML::structured_deserialize(vm, record, realm, Optional<HTML::DeserializationMemory> {}));
  72. }
  73. // 2. Create a new PerformanceMark object (entry) with the current global object's realm.
  74. return realm.heap().allocate<PerformanceMark>(realm, realm, name, start_time, duration, detail);
  75. }
  76. FlyString const& PerformanceMark::entry_type() const
  77. {
  78. return PerformanceTimeline::EntryTypes::mark;
  79. }
  80. void PerformanceMark::initialize(JS::Realm& realm)
  81. {
  82. Base::initialize(realm);
  83. WEB_SET_PROTOTYPE_FOR_INTERFACE(PerformanceMark);
  84. }
  85. void PerformanceMark::visit_edges(JS::Cell::Visitor& visitor)
  86. {
  87. Base::visit_edges(visitor);
  88. visitor.visit(m_detail);
  89. }
  90. }