CloseWatcher.cpp 6.2 KB

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