BrowsingContext.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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/NavigableContainer.h>
  19. #include <LibWeb/HTML/Origin.h>
  20. #include <LibWeb/HTML/SandboxingFlagSet.h>
  21. #include <LibWeb/HTML/SessionHistoryEntry.h>
  22. #include <LibWeb/HTML/TokenizedFeatures.h>
  23. #include <LibWeb/HTML/VisibilityState.h>
  24. #include <LibWeb/Platform/Timer.h>
  25. #include <LibWeb/TreeNode.h>
  26. namespace Web::HTML {
  27. class BrowsingContext final : public JS::Cell {
  28. JS_CELL(BrowsingContext, JS::Cell);
  29. JS_DECLARE_ALLOCATOR(BrowsingContext);
  30. public:
  31. struct BrowsingContextAndDocument {
  32. JS::NonnullGCPtr<BrowsingContext> browsing_context;
  33. JS::NonnullGCPtr<DOM::Document> document;
  34. };
  35. 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);
  36. static WebIDL::ExceptionOr<BrowsingContextAndDocument> create_a_new_auxiliary_browsing_context_and_document(JS::NonnullGCPtr<Page> page, JS::NonnullGCPtr<HTML::BrowsingContext> opener);
  37. virtual ~BrowsingContext() override;
  38. JS::NonnullGCPtr<HTML::TraversableNavigable> top_level_traversable() const;
  39. JS::GCPtr<BrowsingContext> parent() const { return m_parent; }
  40. JS::GCPtr<BrowsingContext> first_child() const;
  41. JS::GCPtr<BrowsingContext> next_sibling() const;
  42. bool is_ancestor_of(BrowsingContext const&) const;
  43. bool is_familiar_with(BrowsingContext const&) const;
  44. template<typename Callback>
  45. TraversalDecision for_each_in_inclusive_subtree(Callback callback) const
  46. {
  47. if (callback(*this) == TraversalDecision::Break)
  48. return TraversalDecision::Break;
  49. for (auto child = first_child(); child; child = child->next_sibling()) {
  50. if (child->for_each_in_inclusive_subtree(callback) == TraversalDecision::Break)
  51. return TraversalDecision::Break;
  52. }
  53. return TraversalDecision::Continue;
  54. }
  55. template<typename Callback>
  56. TraversalDecision for_each_in_inclusive_subtree(Callback callback)
  57. {
  58. if (callback(*this) == TraversalDecision::Break)
  59. return TraversalDecision::Break;
  60. for (auto child = first_child(); child; child = child->next_sibling()) {
  61. if (child->for_each_in_inclusive_subtree(callback) == TraversalDecision::Break)
  62. return TraversalDecision::Break;
  63. }
  64. return TraversalDecision::Continue;
  65. }
  66. template<typename Callback>
  67. TraversalDecision for_each_in_subtree(Callback callback) const
  68. {
  69. for (auto child = first_child(); child; child = child->next_sibling()) {
  70. if (child->for_each_in_inclusive_subtree(callback) == TraversalDecision::Break)
  71. return TraversalDecision::Break;
  72. }
  73. return TraversalDecision::Continue;
  74. }
  75. template<typename Callback>
  76. TraversalDecision for_each_in_subtree(Callback callback)
  77. {
  78. for (auto child = first_child(); child; child = child->next_sibling()) {
  79. if (child->for_each_in_inclusive_subtree(callback) == TraversalDecision::Break)
  80. return TraversalDecision::Break;
  81. }
  82. return TraversalDecision::Continue;
  83. }
  84. bool is_top_level() const;
  85. DOM::Document const* active_document() const;
  86. DOM::Document* active_document();
  87. HTML::WindowProxy* window_proxy();
  88. HTML::WindowProxy const* window_proxy() const;
  89. void set_window_proxy(JS::GCPtr<WindowProxy>);
  90. HTML::Window* active_window();
  91. HTML::Window const* active_window() const;
  92. Page& page() { return m_page; }
  93. Page const& page() const { return m_page; }
  94. JS::GCPtr<BrowsingContext> top_level_browsing_context() const;
  95. BrowsingContextGroup* group();
  96. void set_group(BrowsingContextGroup*);
  97. // https://html.spec.whatwg.org/multipage/browsers.html#bcg-remove
  98. void remove();
  99. // https://html.spec.whatwg.org/multipage/origin.html#one-permitted-sandboxed-navigator
  100. BrowsingContext const* the_one_permitted_sandboxed_navigator() const;
  101. bool has_navigable_been_destroyed() const;
  102. JS::GCPtr<BrowsingContext> opener_browsing_context() const { return m_opener_browsing_context; }
  103. void set_opener_browsing_context(JS::GCPtr<BrowsingContext> browsing_context) { m_opener_browsing_context = browsing_context; }
  104. void set_is_popup(TokenizedFeature::Popup is_popup) { m_is_popup = is_popup; }
  105. private:
  106. explicit BrowsingContext(JS::NonnullGCPtr<Page>);
  107. virtual void visit_edges(Cell::Visitor&) override;
  108. JS::NonnullGCPtr<Page> m_page;
  109. // https://html.spec.whatwg.org/multipage/document-sequences.html#browsing-context
  110. JS::GCPtr<HTML::WindowProxy> m_window_proxy;
  111. // https://html.spec.whatwg.org/multipage/browsers.html#opener-browsing-context
  112. JS::GCPtr<BrowsingContext> m_opener_browsing_context;
  113. // https://html.spec.whatwg.org/multipage/document-sequences.html#opener-origin-at-creation
  114. Optional<HTML::Origin> m_opener_origin_at_creation;
  115. // https://html.spec.whatwg.org/multipage/browsers.html#is-popup
  116. TokenizedFeature::Popup m_is_popup { TokenizedFeature::Popup::No };
  117. // https://html.spec.whatwg.org/multipage/document-sequences.html#is-auxiliary
  118. bool m_is_auxiliary { false };
  119. // https://html.spec.whatwg.org/multipage/document-sequences.html#browsing-context-initial-url
  120. Optional<URL::URL> m_initial_url;
  121. // https://html.spec.whatwg.org/multipage/document-sequences.html#virtual-browsing-context-group-id
  122. u64 m_virtual_browsing_context_group_id = { 0 };
  123. // https://html.spec.whatwg.org/multipage/browsers.html#tlbc-group
  124. JS::GCPtr<BrowsingContextGroup> m_group;
  125. JS::GCPtr<BrowsingContext> m_parent;
  126. JS::GCPtr<BrowsingContext> m_first_child;
  127. JS::GCPtr<BrowsingContext> m_last_child;
  128. JS::GCPtr<BrowsingContext> m_next_sibling;
  129. JS::GCPtr<BrowsingContext> m_previous_sibling;
  130. };
  131. HTML::Origin determine_the_origin(URL::URL const& url, SandboxingFlagSet sandbox_flags, Optional<HTML::Origin> source_origin);
  132. SandboxingFlagSet determine_the_creation_sandboxing_flags(BrowsingContext const&, JS::GCPtr<DOM::Element> embedder);
  133. // FIXME: Find a better home for these
  134. bool url_matches_about_blank(URL::URL const& url);
  135. bool url_matches_about_srcdoc(URL::URL const& url);
  136. }