CloseWatcher.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (c) 2024, the Ladybird developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/TypeCasts.h>
  7. #include <LibWeb/Bindings/CloseWatcherPrototype.h>
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/DOM/Document.h>
  10. #include <LibWeb/DOM/EventDispatcher.h>
  11. #include <LibWeb/DOM/IDLEventListener.h>
  12. #include <LibWeb/HTML/CloseWatcher.h>
  13. #include <LibWeb/HTML/CloseWatcherManager.h>
  14. #include <LibWeb/HTML/EventHandler.h>
  15. #include <LibWeb/HTML/Window.h>
  16. namespace Web::HTML {
  17. JS_DEFINE_ALLOCATOR(CloseWatcher);
  18. // https://html.spec.whatwg.org/multipage/interaction.html#establish-a-close-watcher
  19. JS::NonnullGCPtr<CloseWatcher> CloseWatcher::establish(HTML::Window& window)
  20. {
  21. // 1. Assert: window's associated Document is fully active.
  22. VERIFY(window.associated_document().is_fully_active());
  23. // 2. Let closeWatcher be a new close watcher
  24. auto close_watcher = window.heap().allocate<CloseWatcher>(window.realm(), window.realm());
  25. // 3. Let manager be window's associated close watcher manager
  26. auto manager = window.close_watcher_manager();
  27. // 4 - 6. Moved to CloseWatcherManager::add
  28. manager->add(close_watcher);
  29. // 7. Return close_watcher.
  30. return close_watcher;
  31. }
  32. // https://html.spec.whatwg.org/multipage/interaction.html#dom-closewatcher
  33. WebIDL::ExceptionOr<JS::NonnullGCPtr<CloseWatcher>> CloseWatcher::construct_impl(JS::Realm& realm, CloseWatcherOptions const& options)
  34. {
  35. // 1. If this's relevant global object's associated Document is not fully active, then return an "InvalidStateError" DOMException.
  36. // FIXME: Not in spec explicitly, but this should account for detached iframes too. See /close-watcher/frame-removal.html WPT.
  37. auto& window = verify_cast<HTML::Window>(realm.global_object());
  38. if (!window.associated_document().is_fully_active())
  39. return WebIDL::InvalidStateError::create(realm, "The document is not fully active."_fly_string);
  40. // 2. Let close_watcher be the result of establishing a close watcher
  41. auto close_watcher = establish(window);
  42. // 3. If options["signal"] exists, then:
  43. if (options.signal) {
  44. // FIXME: 3.1 If options["signal"]'s aborted, then destroy closeWatcher.
  45. // FIXME: 3.2 Add the following steps to options["signal"]:
  46. }
  47. return close_watcher;
  48. }
  49. CloseWatcher::CloseWatcher(JS::Realm& realm)
  50. : DOM::EventTarget(realm)
  51. {
  52. }
  53. // https://html.spec.whatwg.org/multipage/interaction.html#close-watcher-request-close
  54. bool CloseWatcher::request_close()
  55. {
  56. // 1. If closeWatcher is not active, then return.
  57. if (!m_is_active)
  58. return true;
  59. // 2. If closeWatcher's is running cancel action is true, then return true.
  60. if (m_is_running_cancel_action)
  61. return true;
  62. // 3. Let window be closeWatcher's window.
  63. auto& window = verify_cast<HTML::Window>(realm().global_object());
  64. // 4. If window's associated Document is not fully active, then return true.
  65. if (!window.associated_document().is_fully_active())
  66. return true;
  67. // 5. Let canPreventClose be true if window's close watcher manager's groups's size is less than window's close watcher manager's allowed number of groups,
  68. // and window has history-action activation; otherwise false.
  69. auto manager = window.close_watcher_manager();
  70. bool can_prevent_close = manager->can_prevent_close() && window.has_history_action_activation();
  71. // 6. Set closeWatcher's is running cancel action to true.
  72. m_is_running_cancel_action = true;
  73. // 7. Let shouldContinue be the result of running closeWatcher's cancel action given canPreventClose.
  74. bool should_continue = dispatch_event(DOM::Event::create(realm(), HTML::EventNames::cancel, { .cancelable = can_prevent_close }));
  75. // 8. Set closeWatcher's is running cancel action to false.
  76. m_is_running_cancel_action = false;
  77. // 9. If shouldContinue is false, then:
  78. if (!should_continue) {
  79. // 9.1 Assert: canPreventClose is true.
  80. VERIFY(can_prevent_close);
  81. // 9.2 Consume history-action user activation given window.
  82. window.consume_history_action_user_activation();
  83. return false;
  84. }
  85. // 10. Close closeWatcher.
  86. close();
  87. // 11. Return true.
  88. return true;
  89. }
  90. // https://html.spec.whatwg.org/multipage/interaction.html#close-watcher-close
  91. void CloseWatcher::close()
  92. {
  93. // 1. If closeWatcher is not active, then return.
  94. if (!m_is_active)
  95. return;
  96. // 2. If closeWatcher's window's associated Document is not fully active, then return.
  97. if (!verify_cast<HTML::Window>(realm().global_object()).associated_document().is_fully_active())
  98. return;
  99. // 3. Destroy closeWatcher.
  100. destroy();
  101. // 4. Run closeWatcher's close action.
  102. dispatch_event(DOM::Event::create(realm(), HTML::EventNames::close));
  103. }
  104. // https://html.spec.whatwg.org/multipage/interaction.html#close-watcher-destroy
  105. void CloseWatcher::destroy()
  106. {
  107. // 1. Let manager be closeWatcher's window's close watcher manager.
  108. auto manager = verify_cast<HTML::Window>(realm().global_object()).close_watcher_manager();
  109. // 2-3. Moved to CloseWatcherManager::remove
  110. manager->remove(*this);
  111. m_is_active = false;
  112. }
  113. void CloseWatcher::initialize(JS::Realm& realm)
  114. {
  115. Base::initialize(realm);
  116. WEB_SET_PROTOTYPE_FOR_INTERFACE(CloseWatcher);
  117. }
  118. void CloseWatcher::set_oncancel(WebIDL::CallbackType* event_handler)
  119. {
  120. set_event_handler_attribute(HTML::EventNames::cancel, event_handler);
  121. }
  122. WebIDL::CallbackType* CloseWatcher::oncancel()
  123. {
  124. return event_handler_attribute(HTML::EventNames::cancel);
  125. }
  126. void CloseWatcher::set_onclose(WebIDL::CallbackType* event_handler)
  127. {
  128. set_event_handler_attribute(HTML::EventNames::close, event_handler);
  129. }
  130. WebIDL::CallbackType* CloseWatcher::onclose()
  131. {
  132. return event_handler_attribute(HTML::EventNames::close);
  133. }
  134. }