BrowsingContext.h 11 KB

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