BrowsingContext.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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/Forward.h>
  15. #include <LibJS/Heap/Cell.h>
  16. #include <LibWeb/DOM/Position.h>
  17. #include <LibWeb/HTML/AbstractBrowsingContext.h>
  18. #include <LibWeb/HTML/ActivateTab.h>
  19. #include <LibWeb/HTML/HistoryHandlingBehavior.h>
  20. #include <LibWeb/HTML/NavigableContainer.h>
  21. #include <LibWeb/HTML/Origin.h>
  22. #include <LibWeb/HTML/SandboxingFlagSet.h>
  23. #include <LibWeb/HTML/SessionHistoryEntry.h>
  24. #include <LibWeb/HTML/TokenizedFeatures.h>
  25. #include <LibWeb/HTML/VisibilityState.h>
  26. #include <LibWeb/Page/EventHandler.h>
  27. #include <LibWeb/Platform/Timer.h>
  28. #include <LibWeb/TreeNode.h>
  29. namespace Web::HTML {
  30. class BrowsingContext final
  31. : public AbstractBrowsingContext
  32. , public Weakable<BrowsingContext> {
  33. JS_CELL(BrowsingContext, AbstractBrowsingContext);
  34. public:
  35. struct BrowsingContextAndDocument {
  36. JS::NonnullGCPtr<BrowsingContext> browsing_context;
  37. JS::NonnullGCPtr<DOM::Document> document;
  38. };
  39. static WebIDL::ExceptionOr<BrowsingContextAndDocument> create_a_new_browsing_context_and_document(Page& page, JS::GCPtr<DOM::Document> creator, JS::GCPtr<DOM::Element> embedder, JS::NonnullGCPtr<BrowsingContextGroup> group);
  40. static WebIDL::ExceptionOr<BrowsingContextAndDocument> create_a_new_auxiliary_browsing_context_and_document(Page& page, JS::NonnullGCPtr<HTML::BrowsingContext> opener);
  41. virtual ~BrowsingContext() override;
  42. JS::NonnullGCPtr<HTML::TraversableNavigable> top_level_traversable() const;
  43. JS::GCPtr<BrowsingContext> parent() const { return m_parent; }
  44. void append_child(JS::NonnullGCPtr<BrowsingContext>);
  45. void remove_child(JS::NonnullGCPtr<BrowsingContext>);
  46. JS::GCPtr<BrowsingContext> first_child() const;
  47. JS::GCPtr<BrowsingContext> next_sibling() const;
  48. bool is_ancestor_of(BrowsingContext const&) const;
  49. template<typename Callback>
  50. IterationDecision for_each_in_inclusive_subtree(Callback callback) const
  51. {
  52. if (callback(*this) == IterationDecision::Break)
  53. return IterationDecision::Break;
  54. for (auto child = first_child(); child; child = child->next_sibling()) {
  55. if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
  56. return IterationDecision::Break;
  57. }
  58. return IterationDecision::Continue;
  59. }
  60. template<typename Callback>
  61. IterationDecision for_each_in_inclusive_subtree(Callback callback)
  62. {
  63. if (callback(*this) == IterationDecision::Break)
  64. return IterationDecision::Break;
  65. for (auto child = first_child(); child; child = child->next_sibling()) {
  66. if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
  67. return IterationDecision::Break;
  68. }
  69. return IterationDecision::Continue;
  70. }
  71. template<typename Callback>
  72. void for_each_child(Callback callback) const
  73. {
  74. for (auto node = first_child(); node; node = node->next_sibling())
  75. callback(*node);
  76. }
  77. template<typename Callback>
  78. void for_each_child(Callback callback)
  79. {
  80. for (auto node = first_child(); node; node = node->next_sibling())
  81. callback(*node);
  82. }
  83. template<typename Callback>
  84. IterationDecision for_each_in_subtree(Callback callback) const
  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. template<typename Callback>
  93. IterationDecision for_each_in_subtree(Callback callback)
  94. {
  95. for (auto child = first_child(); child; child = child->next_sibling()) {
  96. if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
  97. return IterationDecision::Break;
  98. }
  99. return IterationDecision::Continue;
  100. }
  101. bool is_top_level() const;
  102. bool is_focused_context() const;
  103. DOM::Document const* active_document() const;
  104. DOM::Document* active_document();
  105. virtual HTML::WindowProxy* window_proxy() override;
  106. virtual HTML::WindowProxy const* window_proxy() const override;
  107. void set_window_proxy(JS::GCPtr<WindowProxy>);
  108. HTML::Window* active_window();
  109. HTML::Window const* active_window() const;
  110. Page* page() { return m_page; }
  111. Page const* page() const { return m_page; }
  112. Web::EventHandler& event_handler() { return m_event_handler; }
  113. Web::EventHandler const& event_handler() const { return m_event_handler; }
  114. void scroll_to(CSSPixelPoint);
  115. void scroll_to_anchor(DeprecatedString const&);
  116. JS::GCPtr<BrowsingContext> top_level_browsing_context() const;
  117. enum class WindowType {
  118. ExistingOrNone,
  119. NewAndUnrestricted,
  120. NewWithNoOpener,
  121. };
  122. struct ChosenBrowsingContext {
  123. JS::GCPtr<AbstractBrowsingContext> browsing_context;
  124. WindowType window_type;
  125. };
  126. ChosenBrowsingContext choose_a_browsing_context(StringView name, TokenizedFeature::NoOpener no_opener, ActivateTab = ActivateTab::Yes);
  127. HTML::NavigableContainer* container() { return m_container; }
  128. HTML::NavigableContainer const* container() const { return m_container; }
  129. CSSPixelPoint to_top_level_position(CSSPixelPoint);
  130. CSSPixelRect to_top_level_rect(CSSPixelRect const&);
  131. DOM::Position const& cursor_position() const { return m_cursor_position; }
  132. void set_cursor_position(DOM::Position);
  133. bool increment_cursor_position_offset();
  134. bool decrement_cursor_position_offset();
  135. bool cursor_blink_state() const { return m_cursor_blink_state; }
  136. DeprecatedString selected_text() const;
  137. void select_all();
  138. void did_edit(Badge<EditEventHandler>);
  139. bool has_a_rendering_opportunity() const;
  140. JS::GCPtr<DOM::Node> currently_focused_area();
  141. Vector<JS::NonnullGCPtr<SessionHistoryEntry>>& session_history() { return m_session_history; }
  142. Vector<JS::NonnullGCPtr<SessionHistoryEntry>> const& session_history() const { return m_session_history; }
  143. size_t session_history_index() const { return *m_session_history_index; }
  144. // https://html.spec.whatwg.org/multipage/dom.html#still-on-its-initial-about:blank-document
  145. bool still_on_its_initial_about_blank_document() const;
  146. BrowsingContextGroup* group();
  147. void set_group(BrowsingContextGroup*);
  148. // https://html.spec.whatwg.org/multipage/browsers.html#bcg-remove
  149. void remove();
  150. // https://html.spec.whatwg.org/multipage/origin.html#one-permitted-sandboxed-navigator
  151. BrowsingContext const* the_one_permitted_sandboxed_navigator() const;
  152. Vector<JS::Handle<DOM::Document>> document_family() const;
  153. bool document_family_contains(DOM::Document const&) const;
  154. VisibilityState system_visibility_state() const;
  155. void set_system_visibility_state(VisibilityState);
  156. bool has_been_discarded() const { return m_has_been_discarded; }
  157. Optional<AK::URL> const& creator_url() const { return m_creator_url; }
  158. virtual String const& window_handle() const override { return m_window_handle; }
  159. virtual void set_window_handle(String handle) override { m_window_handle = move(handle); }
  160. private:
  161. explicit BrowsingContext(Page&, HTML::NavigableContainer*);
  162. virtual void visit_edges(Cell::Visitor&) override;
  163. void reset_cursor_blink_cycle();
  164. void scroll_offset_did_change();
  165. WeakPtr<Page> m_page;
  166. Web::EventHandler m_event_handler;
  167. // https://html.spec.whatwg.org/multipage/history.html#current-entry
  168. SessionHistoryEntry& current_entry() { return m_session_history[*m_session_history_index]; }
  169. SessionHistoryEntry const& current_entry() const { return m_session_history[*m_session_history_index]; }
  170. Optional<size_t> m_session_history_index { 0 };
  171. // https://html.spec.whatwg.org/multipage/history.html#session-history
  172. Vector<JS::NonnullGCPtr<SessionHistoryEntry>> m_session_history;
  173. // https://html.spec.whatwg.org/multipage/browsers.html#creator-url
  174. Optional<AK::URL> m_creator_url;
  175. // https://html.spec.whatwg.org/multipage/browsers.html#creator-base-url
  176. Optional<AK::URL> m_creator_base_url;
  177. // https://html.spec.whatwg.org/multipage/browsers.html#creator-origin
  178. Optional<HTML::Origin> m_creator_origin;
  179. JS::GCPtr<HTML::NavigableContainer> m_container;
  180. CSSPixelSize m_size;
  181. CSSPixelPoint m_viewport_scroll_offset;
  182. // https://w3c.github.io/webdriver/#dfn-window-handles
  183. String m_window_handle;
  184. // https://html.spec.whatwg.org/multipage/browsers.html#browsing-context
  185. JS::GCPtr<HTML::WindowProxy> m_window_proxy;
  186. DOM::Position m_cursor_position;
  187. RefPtr<Core::Timer> m_cursor_blink_timer;
  188. bool m_cursor_blink_state { false };
  189. DeprecatedString m_name;
  190. // https://html.spec.whatwg.org/multipage/browsers.html#tlbc-group
  191. JS::GCPtr<BrowsingContextGroup> m_group;
  192. // https://html.spec.whatwg.org/multipage/interaction.html#system-visibility-state
  193. VisibilityState m_system_visibility_state { VisibilityState::Hidden };
  194. JS::GCPtr<BrowsingContext> m_parent;
  195. JS::GCPtr<BrowsingContext> m_first_child;
  196. JS::GCPtr<BrowsingContext> m_last_child;
  197. JS::GCPtr<BrowsingContext> m_next_sibling;
  198. JS::GCPtr<BrowsingContext> m_previous_sibling;
  199. bool m_has_been_discarded { false };
  200. };
  201. HTML::Origin determine_the_origin(AK::URL const& url, SandboxingFlagSet sandbox_flags, Optional<HTML::Origin> source_origin);
  202. SandboxingFlagSet determine_the_creation_sandboxing_flags(BrowsingContext const&, JS::GCPtr<DOM::Element> embedder);
  203. // FIXME: Find a better home for this
  204. bool url_matches_about_blank(AK::URL const& url);
  205. }