BrowsingContext.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 <LibJS/Heap/Cell.h>
  15. #include <LibWeb/DOM/Position.h>
  16. #include <LibWeb/HTML/BrowsingContextContainer.h>
  17. #include <LibWeb/HTML/HistoryHandlingBehavior.h>
  18. #include <LibWeb/HTML/Origin.h>
  19. #include <LibWeb/HTML/SessionHistoryEntry.h>
  20. #include <LibWeb/HTML/VisibilityState.h>
  21. #include <LibWeb/Loader/FrameLoader.h>
  22. #include <LibWeb/Page/EventHandler.h>
  23. #include <LibWeb/Platform/Timer.h>
  24. #include <LibWeb/TreeNode.h>
  25. namespace Web::HTML {
  26. class BrowsingContext final
  27. : public JS::Cell
  28. , public Weakable<BrowsingContext> {
  29. JS_CELL(BrowsingContext, JS::Cell);
  30. public:
  31. static JS::NonnullGCPtr<BrowsingContext> create_a_new_browsing_context(Page&, JS::GCPtr<DOM::Document> creator, JS::GCPtr<DOM::Element> embedder, BrowsingContextGroup&);
  32. static JS::NonnullGCPtr<BrowsingContext> create_a_new_top_level_browsing_context(Page&);
  33. ~BrowsingContext();
  34. JS::GCPtr<BrowsingContext> parent() const { return m_parent; }
  35. void append_child(JS::NonnullGCPtr<BrowsingContext>);
  36. void remove_child(JS::NonnullGCPtr<BrowsingContext>);
  37. JS::GCPtr<BrowsingContext> first_child() const;
  38. JS::GCPtr<BrowsingContext> next_sibling() const;
  39. bool is_ancestor_of(BrowsingContext const&) const;
  40. template<typename Callback>
  41. IterationDecision for_each_in_inclusive_subtree(Callback callback) const
  42. {
  43. if (callback(*this) == IterationDecision::Break)
  44. return IterationDecision::Break;
  45. for (auto child = first_child(); child; child = child->next_sibling()) {
  46. if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
  47. return IterationDecision::Break;
  48. }
  49. return IterationDecision::Continue;
  50. }
  51. template<typename Callback>
  52. IterationDecision for_each_in_inclusive_subtree(Callback callback)
  53. {
  54. if (callback(*this) == IterationDecision::Break)
  55. return IterationDecision::Break;
  56. for (auto child = first_child(); child; child = child->next_sibling()) {
  57. if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
  58. return IterationDecision::Break;
  59. }
  60. return IterationDecision::Continue;
  61. }
  62. template<typename Callback>
  63. void for_each_child(Callback callback) const
  64. {
  65. for (auto node = first_child(); node; node = node->next_sibling())
  66. callback(*node);
  67. }
  68. template<typename Callback>
  69. void for_each_child(Callback callback)
  70. {
  71. for (auto node = first_child(); node; node = node->next_sibling())
  72. callback(*node);
  73. }
  74. template<typename Callback>
  75. IterationDecision for_each_in_subtree(Callback callback) const
  76. {
  77. for (auto child = first_child(); child; child = child->next_sibling()) {
  78. if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
  79. return IterationDecision::Break;
  80. }
  81. return IterationDecision::Continue;
  82. }
  83. template<typename Callback>
  84. IterationDecision for_each_in_subtree(Callback callback)
  85. {
  86. for (auto child = first_child(); child; child = child->next_sibling()) {
  87. if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
  88. return IterationDecision::Break;
  89. }
  90. return IterationDecision::Continue;
  91. }
  92. class ViewportClient {
  93. public:
  94. virtual ~ViewportClient() = default;
  95. virtual void browsing_context_did_set_viewport_rect(Gfx::IntRect const&) = 0;
  96. };
  97. void register_viewport_client(ViewportClient&);
  98. void unregister_viewport_client(ViewportClient&);
  99. bool is_top_level() const;
  100. bool is_focused_context() const;
  101. DOM::Document const* active_document() const;
  102. DOM::Document* active_document();
  103. void set_active_document(JS::NonnullGCPtr<DOM::Document>);
  104. HTML::WindowProxy* window_proxy();
  105. HTML::WindowProxy const* window_proxy() const;
  106. HTML::Window* active_window();
  107. HTML::Window const* active_window() const;
  108. Page* page() { return m_page; }
  109. Page const* page() const { return m_page; }
  110. Gfx::IntSize const& size() const { return m_size; }
  111. void set_size(Gfx::IntSize const&);
  112. void set_needs_display();
  113. void set_needs_display(Gfx::IntRect const&);
  114. Gfx::IntPoint const& viewport_scroll_offset() const { return m_viewport_scroll_offset; }
  115. Gfx::IntRect viewport_rect() const { return { m_viewport_scroll_offset, m_size }; }
  116. void set_viewport_rect(Gfx::IntRect const&);
  117. FrameLoader& loader() { return m_loader; }
  118. FrameLoader const& loader() const { return m_loader; }
  119. Web::EventHandler& event_handler() { return m_event_handler; }
  120. Web::EventHandler const& event_handler() const { return m_event_handler; }
  121. void scroll_to(Gfx::IntPoint const&);
  122. void scroll_to_anchor(String const&);
  123. BrowsingContext& top_level_browsing_context()
  124. {
  125. BrowsingContext* context = this;
  126. while (context->parent())
  127. context = context->parent();
  128. return *context;
  129. }
  130. BrowsingContext const& top_level_browsing_context() const { return const_cast<BrowsingContext*>(this)->top_level_browsing_context(); }
  131. BrowsingContext* choose_a_browsing_context(StringView name, bool noopener);
  132. size_t document_tree_child_browsing_context_count() const;
  133. bool is_child_of(BrowsingContext const&) const;
  134. HTML::BrowsingContextContainer* container() { return m_container; }
  135. HTML::BrowsingContextContainer const* container() const { return m_container; }
  136. Gfx::IntPoint to_top_level_position(Gfx::IntPoint const&);
  137. Gfx::IntRect to_top_level_rect(Gfx::IntRect const&);
  138. DOM::Position const& cursor_position() const { return m_cursor_position; }
  139. void set_cursor_position(DOM::Position);
  140. bool increment_cursor_position_offset();
  141. bool decrement_cursor_position_offset();
  142. bool cursor_blink_state() const { return m_cursor_blink_state; }
  143. String selected_text() const;
  144. void select_all();
  145. void did_edit(Badge<EditEventHandler>);
  146. void register_frame_nesting(AK::URL const&);
  147. bool is_frame_nesting_allowed(AK::URL const&) const;
  148. void set_frame_nesting_levels(HashMap<AK::URL, size_t> frame_nesting_levels) { m_frame_nesting_levels = move(frame_nesting_levels); };
  149. HashMap<AK::URL, size_t> const& frame_nesting_levels() const { return m_frame_nesting_levels; }
  150. DOM::Document* container_document();
  151. DOM::Document const* container_document() const;
  152. bool has_a_rendering_opportunity() const;
  153. JS::GCPtr<DOM::Node> currently_focused_area();
  154. String const& name() const { return m_name; }
  155. void set_name(String const& name) { m_name = name; }
  156. Vector<SessionHistoryEntry>& session_history() { return m_session_history; }
  157. Vector<SessionHistoryEntry> const& session_history() const { return m_session_history; }
  158. size_t session_history_index() const { return *m_session_history_index; }
  159. // https://html.spec.whatwg.org/multipage/dom.html#still-on-its-initial-about:blank-document
  160. bool still_on_its_initial_about_blank_document() const;
  161. BrowsingContextGroup* group();
  162. void set_group(BrowsingContextGroup*);
  163. // https://html.spec.whatwg.org/multipage/browsers.html#bcg-remove
  164. void remove();
  165. // https://html.spec.whatwg.org/multipage/browsers.html#allowed-to-navigate
  166. bool is_allowed_to_navigate(BrowsingContext const&) const;
  167. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigate
  168. WebIDL::ExceptionOr<void> navigate(
  169. NonnullRefPtr<Fetch::Infrastructure::Request> resource,
  170. BrowsingContext& source_browsing_context,
  171. bool exceptions_enabled = false,
  172. HistoryHandlingBehavior history_handling = HistoryHandlingBehavior::Default,
  173. Optional<PolicyContainer> history_policy_container = {},
  174. String navigation_type = "other",
  175. Optional<String> navigation_id = {},
  176. Function<void(NonnullRefPtr<Fetch::Infrastructure::Response>)> process_response_end_of_body = {});
  177. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigate-fragid
  178. WebIDL::ExceptionOr<void> navigate_to_a_fragment(AK::URL const&, HistoryHandlingBehavior, String navigation_id);
  179. // https://html.spec.whatwg.org/multipage/origin.html#one-permitted-sandboxed-navigator
  180. BrowsingContext const* the_one_permitted_sandboxed_navigator() const;
  181. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#traverse-the-history
  182. WebIDL::ExceptionOr<void> traverse_the_history(size_t entry_index, HistoryHandlingBehavior = HistoryHandlingBehavior::Default, bool explicit_history_navigation = false);
  183. Vector<JS::Handle<DOM::Document>> document_family() const;
  184. bool document_family_contains(DOM::Document const&) const;
  185. VisibilityState system_visibility_state() const;
  186. void set_system_visibility_state(VisibilityState);
  187. // https://html.spec.whatwg.org/multipage/window-object.html#a-browsing-context-is-discarded
  188. void discard();
  189. // https://html.spec.whatwg.org/multipage/window-object.html#close-a-browsing-context
  190. void close();
  191. private:
  192. explicit BrowsingContext(Page&, HTML::BrowsingContextContainer*);
  193. virtual void visit_edges(Cell::Visitor&) override;
  194. void reset_cursor_blink_cycle();
  195. void scroll_offset_did_change();
  196. WeakPtr<Page> m_page;
  197. FrameLoader m_loader;
  198. Web::EventHandler m_event_handler;
  199. // https://html.spec.whatwg.org/multipage/history.html#current-entry
  200. SessionHistoryEntry& current_entry() { return m_session_history[*m_session_history_index]; }
  201. SessionHistoryEntry const& current_entry() const { return m_session_history[*m_session_history_index]; }
  202. Optional<size_t> m_session_history_index { 0 };
  203. // https://html.spec.whatwg.org/multipage/history.html#session-history
  204. Vector<SessionHistoryEntry> m_session_history;
  205. // https://html.spec.whatwg.org/multipage/browsers.html#creator-url
  206. Optional<AK::URL> m_creator_url;
  207. // https://html.spec.whatwg.org/multipage/browsers.html#creator-base-url
  208. Optional<AK::URL> m_creator_base_url;
  209. // https://html.spec.whatwg.org/multipage/browsers.html#creator-origin
  210. Optional<HTML::Origin> m_creator_origin;
  211. JS::GCPtr<HTML::BrowsingContextContainer> m_container;
  212. Gfx::IntSize m_size;
  213. Gfx::IntPoint m_viewport_scroll_offset;
  214. // https://html.spec.whatwg.org/multipage/browsers.html#browsing-context
  215. JS::GCPtr<HTML::WindowProxy> m_window_proxy;
  216. DOM::Position m_cursor_position;
  217. RefPtr<Platform::Timer> m_cursor_blink_timer;
  218. bool m_cursor_blink_state { false };
  219. HashTable<ViewportClient*> m_viewport_clients;
  220. HashMap<AK::URL, size_t> m_frame_nesting_levels;
  221. String m_name;
  222. // https://html.spec.whatwg.org/multipage/browsers.html#tlbc-group
  223. JS::GCPtr<BrowsingContextGroup> m_group;
  224. // https://html.spec.whatwg.org/multipage/interaction.html#system-visibility-state
  225. VisibilityState m_system_visibility_state { VisibilityState::Hidden };
  226. JS::GCPtr<BrowsingContext> m_parent;
  227. JS::GCPtr<BrowsingContext> m_first_child;
  228. JS::GCPtr<BrowsingContext> m_last_child;
  229. JS::GCPtr<BrowsingContext> m_next_sibling;
  230. JS::GCPtr<BrowsingContext> m_previous_sibling;
  231. };
  232. HTML::Origin determine_the_origin(BrowsingContext const& browsing_context, Optional<AK::URL> url, SandboxingFlagSet sandbox_flags, Optional<HTML::Origin> invocation_origin);
  233. }