CloseWatcher.cpp 5.9 KB

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