Internals.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/VM.h>
  7. #include <LibWeb/Bindings/InternalsPrototype.h>
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/DOM/Document.h>
  10. #include <LibWeb/DOM/Event.h>
  11. #include <LibWeb/DOM/EventTarget.h>
  12. #include <LibWeb/HTML/BrowsingContext.h>
  13. #include <LibWeb/HTML/HTMLElement.h>
  14. #include <LibWeb/HTML/Window.h>
  15. #include <LibWeb/Internals/Internals.h>
  16. #include <LibWeb/Page/InputEvent.h>
  17. #include <LibWeb/Page/Page.h>
  18. #include <LibWeb/Painting/PaintableBox.h>
  19. #include <LibWeb/Painting/ViewportPaintable.h>
  20. namespace Web::Internals {
  21. JS_DEFINE_ALLOCATOR(Internals);
  22. Internals::Internals(JS::Realm& realm)
  23. : Bindings::PlatformObject(realm)
  24. {
  25. }
  26. Internals::~Internals() = default;
  27. void Internals::initialize(JS::Realm& realm)
  28. {
  29. Base::initialize(realm);
  30. WEB_SET_PROTOTYPE_FOR_INTERFACE(Internals);
  31. }
  32. void Internals::signal_text_test_is_done()
  33. {
  34. global_object().browsing_context()->page().client().page_did_finish_text_test();
  35. }
  36. void Internals::gc()
  37. {
  38. vm().heap().collect_garbage();
  39. }
  40. JS::Object* Internals::hit_test(double x, double y)
  41. {
  42. auto* active_document = global_object().browsing_context()->top_level_browsing_context()->active_document();
  43. // NOTE: Force a layout update just before hit testing. This is because the current layout tree, which is required
  44. // for stacking context traversal, might not exist if this call occurs between the tear_down_layout_tree()
  45. // and update_layout() calls
  46. active_document->update_layout();
  47. auto result = active_document->paintable_box()->hit_test({ x, y }, Painting::HitTestType::Exact);
  48. if (result.has_value()) {
  49. auto hit_tеsting_result = JS::Object::create(realm(), nullptr);
  50. hit_tеsting_result->define_direct_property("node", result->dom_node(), JS::default_attributes);
  51. hit_tеsting_result->define_direct_property("indexInNode", JS::Value(result->index_in_node), JS::default_attributes);
  52. return hit_tеsting_result;
  53. }
  54. return nullptr;
  55. }
  56. void Internals::send_text(HTML::HTMLElement& target, String const& text)
  57. {
  58. auto& page = global_object().browsing_context()->page();
  59. target.focus();
  60. for (auto code_point : text.code_points())
  61. page.handle_keydown(UIEvents::code_point_to_key_code(code_point), 0, code_point);
  62. }
  63. void Internals::send_key(HTML::HTMLElement& target, String const& key_name)
  64. {
  65. auto key_code = UIEvents::key_code_from_string(key_name);
  66. target.focus();
  67. global_object().browsing_context()->page().handle_keydown(key_code, 0, 0);
  68. }
  69. void Internals::commit_text()
  70. {
  71. global_object().browsing_context()->page().handle_keydown(UIEvents::Key_Return, 0, 0);
  72. }
  73. void Internals::click(double x, double y)
  74. {
  75. click(x, y, UIEvents::MouseButton::Primary);
  76. }
  77. void Internals::middle_click(double x, double y)
  78. {
  79. click(x, y, UIEvents::MouseButton::Middle);
  80. }
  81. void Internals::click(double x, double y, UIEvents::MouseButton button)
  82. {
  83. auto& page = global_object().browsing_context()->page();
  84. auto position = page.css_to_device_point({ x, y });
  85. page.handle_mousedown(position, position, button, 0, 0);
  86. page.handle_mouseup(position, position, button, 0, 0);
  87. }
  88. void Internals::move_pointer_to(double x, double y)
  89. {
  90. auto& page = global_object().browsing_context()->page();
  91. auto position = page.css_to_device_point({ x, y });
  92. page.handle_mousemove(position, position, 0, 0);
  93. }
  94. void Internals::wheel(double x, double y, double delta_x, double delta_y)
  95. {
  96. auto& page = global_object().browsing_context()->page();
  97. auto position = page.css_to_device_point({ x, y });
  98. page.handle_mousewheel(position, position, 0, 0, 0, delta_x, delta_y);
  99. }
  100. WebIDL::ExceptionOr<bool> Internals::dispatch_user_activated_event(DOM::EventTarget& target, DOM::Event& event)
  101. {
  102. event.set_is_trusted(true);
  103. return target.dispatch_event(event);
  104. }
  105. JS::NonnullGCPtr<InternalAnimationTimeline> Internals::create_internal_animation_timeline()
  106. {
  107. auto& realm = this->realm();
  108. return realm.heap().allocate<InternalAnimationTimeline>(realm, realm);
  109. }
  110. void Internals::simulate_drag_start(double x, double y, String const& name, String const& contents)
  111. {
  112. Vector<HTML::SelectedFile> files;
  113. files.empend(name.to_byte_string(), MUST(ByteBuffer::copy(contents.bytes())));
  114. auto& page = global_object().browsing_context()->page();
  115. auto position = page.css_to_device_point({ x, y });
  116. page.handle_drag_and_drop_event(DragEvent::Type::DragStart, position, position, UIEvents::MouseButton::Primary, 0, 0, move(files));
  117. }
  118. void Internals::simulate_drag_move(double x, double y)
  119. {
  120. auto& page = global_object().browsing_context()->page();
  121. auto position = page.css_to_device_point({ x, y });
  122. page.handle_drag_and_drop_event(DragEvent::Type::DragMove, position, position, UIEvents::MouseButton::Primary, 0, 0, {});
  123. }
  124. void Internals::simulate_drop(double x, double y)
  125. {
  126. auto& page = global_object().browsing_context()->page();
  127. auto position = page.css_to_device_point({ x, y });
  128. page.handle_drag_and_drop_event(DragEvent::Type::Drop, position, position, UIEvents::MouseButton::Primary, 0, 0, {});
  129. }
  130. }