BrowsingContext.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 <LibGfx/Bitmap.h>
  12. #include <LibGfx/Rect.h>
  13. #include <LibGfx/Size.h>
  14. #include <LibWeb/DOM/Position.h>
  15. #include <LibWeb/HTML/BrowsingContextContainer.h>
  16. #include <LibWeb/HTML/HistoryHandlingBehavior.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/Platform/Timer.h>
  22. #include <LibWeb/TreeNode.h>
  23. namespace Web::HTML {
  24. class BrowsingContext : public TreeNode<BrowsingContext> {
  25. public:
  26. static NonnullRefPtr<BrowsingContext> create_a_new_browsing_context(Page&, JS::GCPtr<DOM::Document> creator, JS::GCPtr<DOM::Element> embedder, BrowsingContextGroup&);
  27. static NonnullRefPtr<BrowsingContext> create_a_new_top_level_browsing_context(Page&);
  28. ~BrowsingContext();
  29. class ViewportClient {
  30. public:
  31. virtual ~ViewportClient() = default;
  32. virtual void browsing_context_did_set_viewport_rect(Gfx::IntRect const&) = 0;
  33. };
  34. void register_viewport_client(ViewportClient&);
  35. void unregister_viewport_client(ViewportClient&);
  36. bool is_top_level() const;
  37. bool is_focused_context() const;
  38. DOM::Document const* active_document() const;
  39. DOM::Document* active_document();
  40. void set_active_document(DOM::Document*);
  41. HTML::Window* active_window();
  42. HTML::Window const* active_window() const;
  43. Page* page() { return m_page; }
  44. Page const* page() const { return m_page; }
  45. Gfx::IntSize const& size() const { return m_size; }
  46. void set_size(Gfx::IntSize const&);
  47. void set_needs_display();
  48. void set_needs_display(Gfx::IntRect const&);
  49. Gfx::IntPoint const& viewport_scroll_offset() const { return m_viewport_scroll_offset; }
  50. Gfx::IntRect viewport_rect() const { return { m_viewport_scroll_offset, m_size }; }
  51. void set_viewport_rect(Gfx::IntRect const&);
  52. FrameLoader& loader() { return m_loader; }
  53. FrameLoader const& loader() const { return m_loader; }
  54. Web::EventHandler& event_handler() { return m_event_handler; }
  55. Web::EventHandler const& event_handler() const { return m_event_handler; }
  56. void scroll_to(Gfx::IntPoint const&);
  57. void scroll_to_anchor(String const&);
  58. BrowsingContext& top_level_browsing_context()
  59. {
  60. BrowsingContext* context = this;
  61. while (context->parent())
  62. context = context->parent();
  63. return *context;
  64. }
  65. BrowsingContext const& top_level_browsing_context() const { return const_cast<BrowsingContext*>(this)->top_level_browsing_context(); }
  66. BrowsingContext* choose_a_browsing_context(StringView name, bool noopener);
  67. size_t document_tree_child_browsing_context_count() const;
  68. bool is_child_of(BrowsingContext const&) const;
  69. HTML::BrowsingContextContainer* container() { return m_container; }
  70. HTML::BrowsingContextContainer const* container() const { return m_container; }
  71. Gfx::IntPoint to_top_level_position(Gfx::IntPoint const&);
  72. Gfx::IntRect to_top_level_rect(Gfx::IntRect const&);
  73. DOM::Position const& cursor_position() const { return m_cursor_position; }
  74. void set_cursor_position(DOM::Position);
  75. bool increment_cursor_position_offset();
  76. bool decrement_cursor_position_offset();
  77. bool cursor_blink_state() const { return m_cursor_blink_state; }
  78. String selected_text() const;
  79. void select_all();
  80. void did_edit(Badge<EditEventHandler>);
  81. void register_frame_nesting(AK::URL const&);
  82. bool is_frame_nesting_allowed(AK::URL const&) const;
  83. void set_frame_nesting_levels(HashMap<AK::URL, size_t> frame_nesting_levels) { m_frame_nesting_levels = move(frame_nesting_levels); };
  84. HashMap<AK::URL, size_t> const& frame_nesting_levels() const { return m_frame_nesting_levels; }
  85. DOM::Document* container_document();
  86. DOM::Document const* container_document() const;
  87. bool has_a_rendering_opportunity() const;
  88. JS::GCPtr<DOM::Node> currently_focused_area();
  89. String const& name() const { return m_name; }
  90. void set_name(String const& name) { m_name = name; }
  91. Vector<SessionHistoryEntry>& session_history() { return m_session_history; }
  92. Vector<SessionHistoryEntry> const& session_history() const { return m_session_history; }
  93. // https://html.spec.whatwg.org/multipage/dom.html#still-on-its-initial-about:blank-document
  94. bool still_on_its_initial_about_blank_document() const;
  95. BrowsingContextGroup* group();
  96. void set_group(BrowsingContextGroup*);
  97. // https://html.spec.whatwg.org/multipage/browsers.html#bcg-remove
  98. void remove();
  99. // https://html.spec.whatwg.org/multipage/browsers.html#allowed-to-navigate
  100. bool is_allowed_to_navigate(BrowsingContext const&) const;
  101. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigate
  102. DOM::ExceptionOr<void> navigate(
  103. Fetch::Infrastructure::Request resource,
  104. BrowsingContext& source_browsing_context,
  105. bool exceptions_enabled = false,
  106. HistoryHandlingBehavior history_handling = HistoryHandlingBehavior::Default,
  107. Optional<PolicyContainer> history_policy_container = {},
  108. String navigation_type = "other",
  109. Optional<String> navigation_id = {},
  110. Function<void(NonnullOwnPtr<Fetch::Infrastructure::Response>)> process_response_end_of_body = {});
  111. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigate-fragid
  112. DOM::ExceptionOr<void> navigate_to_a_fragment(AK::URL const&, HistoryHandlingBehavior, String navigation_id);
  113. // https://html.spec.whatwg.org/multipage/origin.html#one-permitted-sandboxed-navigator
  114. BrowsingContext const* the_one_permitted_sandboxed_navigator() const;
  115. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#traverse-the-history
  116. DOM::ExceptionOr<void> traverse_the_history(size_t entry_index, HistoryHandlingBehavior = HistoryHandlingBehavior::Default, bool explicit_history_navigation = false);
  117. bool document_family_contains(DOM::Document const&) const;
  118. private:
  119. explicit BrowsingContext(Page&, HTML::BrowsingContextContainer*);
  120. void reset_cursor_blink_cycle();
  121. void scroll_offset_did_change();
  122. WeakPtr<Page> m_page;
  123. FrameLoader m_loader;
  124. Web::EventHandler m_event_handler;
  125. // https://html.spec.whatwg.org/multipage/history.html#current-entry
  126. SessionHistoryEntry& current_entry() { return m_session_history[*m_session_history_index]; }
  127. SessionHistoryEntry const& current_entry() const { return m_session_history[*m_session_history_index]; }
  128. Optional<size_t> m_session_history_index { 0 };
  129. // https://html.spec.whatwg.org/multipage/history.html#session-history
  130. Vector<SessionHistoryEntry> m_session_history;
  131. // https://html.spec.whatwg.org/multipage/browsers.html#creator-url
  132. Optional<AK::URL> m_creator_url;
  133. // https://html.spec.whatwg.org/multipage/browsers.html#creator-base-url
  134. Optional<AK::URL> m_creator_base_url;
  135. // https://html.spec.whatwg.org/multipage/browsers.html#creator-origin
  136. Optional<HTML::Origin> m_creator_origin;
  137. WeakPtr<HTML::BrowsingContextContainer> m_container;
  138. JS::Handle<DOM::Document> m_active_document;
  139. Gfx::IntSize m_size;
  140. Gfx::IntPoint m_viewport_scroll_offset;
  141. DOM::Position m_cursor_position;
  142. RefPtr<Platform::Timer> m_cursor_blink_timer;
  143. bool m_cursor_blink_state { false };
  144. HashTable<ViewportClient*> m_viewport_clients;
  145. HashMap<AK::URL, size_t> m_frame_nesting_levels;
  146. String m_name;
  147. // https://html.spec.whatwg.org/multipage/browsers.html#tlbc-group
  148. RefPtr<BrowsingContextGroup> m_group;
  149. };
  150. HTML::Origin determine_the_origin(BrowsingContext const& browsing_context, Optional<AK::URL> url, SandboxingFlagSet sandbox_flags, Optional<HTML::Origin> invocation_origin);
  151. }