BrowsingContext.h 7.0 KB

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