BrowsingContext.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c) 2018-2022, 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/HTML/Origin.h>
  18. #include <LibWeb/HTML/SessionHistoryEntry.h>
  19. #include <LibWeb/Loader/FrameLoader.h>
  20. #include <LibWeb/Page/EventHandler.h>
  21. #include <LibWeb/TreeNode.h>
  22. namespace Web::HTML {
  23. class BrowsingContext : public TreeNode<BrowsingContext> {
  24. public:
  25. static NonnullRefPtr<BrowsingContext> create_a_new_browsing_context(Page&, JS::GCPtr<DOM::Document> creator, JS::GCPtr<DOM::Element> embedder);
  26. ~BrowsingContext();
  27. class ViewportClient {
  28. public:
  29. virtual ~ViewportClient() = default;
  30. virtual void browsing_context_did_set_viewport_rect(Gfx::IntRect const&) = 0;
  31. };
  32. void register_viewport_client(ViewportClient&);
  33. void unregister_viewport_client(ViewportClient&);
  34. bool is_top_level() const;
  35. bool is_focused_context() const;
  36. DOM::Document const* active_document() const;
  37. DOM::Document* active_document();
  38. void set_active_document(DOM::Document*);
  39. Page* page() { return m_page; }
  40. Page const* page() const { return m_page; }
  41. Gfx::IntSize const& size() const { return m_size; }
  42. void set_size(Gfx::IntSize const&);
  43. void set_needs_display();
  44. void set_needs_display(Gfx::IntRect const&);
  45. Gfx::IntPoint const& viewport_scroll_offset() const { return m_viewport_scroll_offset; }
  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(Gfx::IntPoint const&);
  53. void scroll_to_anchor(String const&);
  54. BrowsingContext& top_level_browsing_context()
  55. {
  56. BrowsingContext* context = this;
  57. while (context->parent())
  58. context = context->parent();
  59. return *context;
  60. }
  61. BrowsingContext const& top_level_browsing_context() const { return const_cast<BrowsingContext*>(this)->top_level_browsing_context(); }
  62. BrowsingContext* choose_a_browsing_context(StringView name, bool noopener);
  63. HTML::BrowsingContextContainer* container() { return m_container; }
  64. HTML::BrowsingContextContainer const* container() const { return m_container; }
  65. Gfx::IntPoint to_top_level_position(Gfx::IntPoint const&);
  66. Gfx::IntRect to_top_level_rect(Gfx::IntRect const&);
  67. DOM::Position const& cursor_position() const { return m_cursor_position; }
  68. void set_cursor_position(DOM::Position);
  69. bool increment_cursor_position_offset();
  70. bool decrement_cursor_position_offset();
  71. bool cursor_blink_state() const { return m_cursor_blink_state; }
  72. String selected_text() const;
  73. void select_all();
  74. void did_edit(Badge<EditEventHandler>);
  75. void register_frame_nesting(AK::URL const&);
  76. bool is_frame_nesting_allowed(AK::URL const&) const;
  77. void set_frame_nesting_levels(HashMap<AK::URL, size_t> frame_nesting_levels) { m_frame_nesting_levels = move(frame_nesting_levels); };
  78. HashMap<AK::URL, size_t> const& frame_nesting_levels() const { return m_frame_nesting_levels; }
  79. DOM::Document* container_document();
  80. DOM::Document const* container_document() const;
  81. bool has_a_rendering_opportunity() const;
  82. JS::GCPtr<DOM::Node> currently_focused_area();
  83. String const& name() const { return m_name; }
  84. void set_name(String const& name) { m_name = name; }
  85. Vector<SessionHistoryEntry>& session_history() { return m_session_history; }
  86. Vector<SessionHistoryEntry> const& session_history() const { return m_session_history; }
  87. // https://html.spec.whatwg.org/multipage/dom.html#still-on-its-initial-about:blank-document
  88. bool still_on_its_initial_about_blank_document() const;
  89. private:
  90. explicit BrowsingContext(Page&, HTML::BrowsingContextContainer*);
  91. void reset_cursor_blink_cycle();
  92. WeakPtr<Page> m_page;
  93. FrameLoader m_loader;
  94. Web::EventHandler m_event_handler;
  95. // https://html.spec.whatwg.org/multipage/history.html#session-history
  96. Vector<SessionHistoryEntry> m_session_history;
  97. // https://html.spec.whatwg.org/multipage/browsers.html#creator-url
  98. Optional<AK::URL> m_creator_url;
  99. // https://html.spec.whatwg.org/multipage/browsers.html#creator-base-url
  100. Optional<AK::URL> m_creator_base_url;
  101. // https://html.spec.whatwg.org/multipage/browsers.html#creator-origin
  102. Optional<HTML::Origin> m_creator_origin;
  103. WeakPtr<HTML::BrowsingContextContainer> m_container;
  104. JS::Handle<DOM::Document> m_active_document;
  105. Gfx::IntSize m_size;
  106. Gfx::IntPoint m_viewport_scroll_offset;
  107. DOM::Position m_cursor_position;
  108. RefPtr<Core::Timer> m_cursor_blink_timer;
  109. bool m_cursor_blink_state { false };
  110. HashTable<ViewportClient*> m_viewport_clients;
  111. HashMap<AK::URL, size_t> m_frame_nesting_levels;
  112. String m_name;
  113. };
  114. HTML::Origin determine_the_origin(BrowsingContext const& browsing_context, Optional<AK::URL> url, SandboxingFlagSet sandbox_flags, Optional<HTML::Origin> invocation_origin);
  115. }