BrowsingContext.h 9.2 KB

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