BrowsingContext.h 13 KB

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