BrowsingContext.h 8.5 KB

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