BrowsingContext.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Function.h>
  8. #include <AK/Noncopyable.h>
  9. #include <AK/RefPtr.h>
  10. #include <AK/WeakPtr.h>
  11. #include <LibCore/Timer.h>
  12. #include <LibGfx/Bitmap.h>
  13. #include <LibGfx/Rect.h>
  14. #include <LibGfx/Size.h>
  15. #include <LibWeb/DOM/Position.h>
  16. #include <LibWeb/HTML/BrowsingContextContainer.h>
  17. #include <LibWeb/Loader/FrameLoader.h>
  18. #include <LibWeb/Page/EventHandler.h>
  19. #include <LibWeb/TreeNode.h>
  20. namespace Web::HTML {
  21. class BrowsingContext : public TreeNode<BrowsingContext> {
  22. public:
  23. static NonnullRefPtr<BrowsingContext> create_nested(Page& page, HTML::BrowsingContextContainer& container) { return adopt_ref(*new BrowsingContext(page, &container)); }
  24. static NonnullRefPtr<BrowsingContext> create(Page& page) { return adopt_ref(*new BrowsingContext(page, nullptr)); }
  25. ~BrowsingContext();
  26. class ViewportClient {
  27. public:
  28. virtual ~ViewportClient() = default;
  29. virtual void browsing_context_did_set_viewport_rect(Gfx::IntRect const&) = 0;
  30. };
  31. void register_viewport_client(ViewportClient&);
  32. void unregister_viewport_client(ViewportClient&);
  33. bool is_top_level() const;
  34. bool is_focused_context() const;
  35. DOM::Document const* active_document() const { return m_active_document; }
  36. DOM::Document* active_document() { return m_active_document; }
  37. void set_active_document(DOM::Document*);
  38. Page* page() { return m_page; }
  39. Page const* page() const { return m_page; }
  40. Gfx::IntSize const& size() const { return m_size; }
  41. void set_size(Gfx::IntSize const&);
  42. void set_needs_display();
  43. void set_needs_display(Gfx::IntRect const&);
  44. Gfx::IntPoint const& viewport_scroll_offset() const { return m_viewport_scroll_offset; }
  45. void set_viewport_scroll_offset(Gfx::IntPoint const&);
  46. Gfx::IntRect viewport_rect() const { return { m_viewport_scroll_offset, m_size }; }
  47. void set_viewport_rect(Gfx::IntRect const&);
  48. FrameLoader& loader() { return m_loader; }
  49. FrameLoader const& loader() const { return m_loader; }
  50. Web::EventHandler& event_handler() { return m_event_handler; }
  51. Web::EventHandler const& event_handler() const { return m_event_handler; }
  52. void scroll_to_anchor(String const&);
  53. BrowsingContext& top_level_browsing_context()
  54. {
  55. BrowsingContext* context = this;
  56. while (context->parent())
  57. context = context->parent();
  58. return *context;
  59. }
  60. BrowsingContext const& top_level_browsing_context() const { return const_cast<BrowsingContext*>(this)->top_level_browsing_context(); }
  61. HTML::BrowsingContextContainer* container() { return m_container; }
  62. HTML::BrowsingContextContainer const* container() const { return m_container; }
  63. Gfx::IntPoint to_top_level_position(Gfx::IntPoint const&);
  64. Gfx::IntRect to_top_level_rect(Gfx::IntRect const&);
  65. DOM::Position const& cursor_position() const { return m_cursor_position; }
  66. void set_cursor_position(DOM::Position);
  67. bool increment_cursor_position_offset();
  68. bool decrement_cursor_position_offset();
  69. bool cursor_blink_state() const { return m_cursor_blink_state; }
  70. String selected_text() const;
  71. void select_all();
  72. void did_edit(Badge<EditEventHandler>);
  73. void register_frame_nesting(AK::URL const&);
  74. bool is_frame_nesting_allowed(AK::URL const&) const;
  75. void set_frame_nesting_levels(HashMap<AK::URL, size_t> frame_nesting_levels) { m_frame_nesting_levels = move(frame_nesting_levels); };
  76. HashMap<AK::URL, size_t> const& frame_nesting_levels() const { return m_frame_nesting_levels; }
  77. DOM::Document* container_document();
  78. DOM::Document const* container_document() const;
  79. bool has_a_rendering_opportunity() const;
  80. RefPtr<DOM::Node> currently_focused_area();
  81. String const& name() const { return m_name; }
  82. void set_name(String const& name) { m_name = name; }
  83. private:
  84. explicit BrowsingContext(Page&, HTML::BrowsingContextContainer*);
  85. void reset_cursor_blink_cycle();
  86. WeakPtr<Page> m_page;
  87. FrameLoader m_loader;
  88. Web::EventHandler m_event_handler;
  89. WeakPtr<HTML::BrowsingContextContainer> m_container;
  90. RefPtr<DOM::Document> m_active_document;
  91. Gfx::IntSize m_size;
  92. Gfx::IntPoint m_viewport_scroll_offset;
  93. DOM::Position m_cursor_position;
  94. RefPtr<Core::Timer> m_cursor_blink_timer;
  95. bool m_cursor_blink_state { false };
  96. HashTable<ViewportClient*> m_viewport_clients;
  97. HashMap<AK::URL, size_t> m_frame_nesting_levels;
  98. String m_name;
  99. };
  100. }