BrowsingContext.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <andreas@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/MainThreadVM.h>
  7. #include <LibWeb/Bindings/PrincipalHostDefined.h>
  8. #include <LibWeb/DOM/Document.h>
  9. #include <LibWeb/DOM/ElementFactory.h>
  10. #include <LibWeb/DOM/Event.h>
  11. #include <LibWeb/DOM/HTMLCollection.h>
  12. #include <LibWeb/DOM/Range.h>
  13. #include <LibWeb/DOMURL/DOMURL.h>
  14. #include <LibWeb/HTML/BrowsingContext.h>
  15. #include <LibWeb/HTML/BrowsingContextGroup.h>
  16. #include <LibWeb/HTML/CrossOrigin/OpenerPolicy.h>
  17. #include <LibWeb/HTML/DocumentState.h>
  18. #include <LibWeb/HTML/HTMLAnchorElement.h>
  19. #include <LibWeb/HTML/HTMLDocument.h>
  20. #include <LibWeb/HTML/HTMLInputElement.h>
  21. #include <LibWeb/HTML/NavigableContainer.h>
  22. #include <LibWeb/HTML/SandboxingFlagSet.h>
  23. #include <LibWeb/HTML/Scripting/WindowEnvironmentSettingsObject.h>
  24. #include <LibWeb/HTML/TraversableNavigable.h>
  25. #include <LibWeb/HTML/Window.h>
  26. #include <LibWeb/HTML/WindowProxy.h>
  27. #include <LibWeb/HighResolutionTime/TimeOrigin.h>
  28. #include <LibWeb/Layout/BreakNode.h>
  29. #include <LibWeb/Layout/Viewport.h>
  30. #include <LibWeb/Namespace.h>
  31. #include <LibWeb/Page/Page.h>
  32. #include <LibWeb/Painting/Paintable.h>
  33. namespace Web::HTML {
  34. GC_DEFINE_ALLOCATOR(BrowsingContext);
  35. // https://html.spec.whatwg.org/multipage/urls-and-fetching.html#matches-about:blank
  36. bool url_matches_about_blank(URL::URL const& url)
  37. {
  38. // A URL matches about:blank if its scheme is "about", its path contains a single string "blank", its username and password are the empty string, and its host is null.
  39. return url.scheme() == "about"sv
  40. && url.paths().size() == 1 && url.paths()[0] == "blank"sv
  41. && url.username().is_empty()
  42. && url.password().is_empty()
  43. && url.host().has<Empty>();
  44. }
  45. // https://html.spec.whatwg.org/multipage/urls-and-fetching.html#matches-about:srcdoc
  46. bool url_matches_about_srcdoc(URL::URL const& url)
  47. {
  48. // A URL matches about:srcdoc if its scheme is "about", its path contains a single string "srcdoc", its query is null, its username and password are the empty string, and its host is null.
  49. return url.scheme() == "about"sv
  50. && url.paths().size() == 1 && url.paths()[0] == "srcdoc"sv
  51. && !url.query().has_value()
  52. && url.username().is_empty()
  53. && url.password().is_empty()
  54. && url.host().has<Empty>();
  55. }
  56. // https://html.spec.whatwg.org/multipage/document-sequences.html#determining-the-origin
  57. URL::Origin determine_the_origin(Optional<URL::URL const&> url, SandboxingFlagSet sandbox_flags, Optional<URL::Origin> source_origin)
  58. {
  59. // 1. If sandboxFlags has its sandboxed origin browsing context flag set, then return a new opaque origin.
  60. if (has_flag(sandbox_flags, SandboxingFlagSet::SandboxedOrigin)) {
  61. return URL::Origin {};
  62. }
  63. // 2. If url is null, then return a new opaque origin.
  64. if (!url.has_value()) {
  65. return URL::Origin {};
  66. }
  67. // 3. If url is about:srcdoc, then:
  68. if (url == "about:srcdoc"sv) {
  69. // 1. Assert: sourceOrigin is non-null.
  70. VERIFY(source_origin.has_value());
  71. // 2. Return sourceOrigin.
  72. return source_origin.release_value();
  73. }
  74. // 4. If url matches about:blank and sourceOrigin is non-null, then return sourceOrigin.
  75. if (url_matches_about_blank(*url) && source_origin.has_value())
  76. return source_origin.release_value();
  77. // 5. Return url's origin.
  78. return url->origin();
  79. }
  80. // https://html.spec.whatwg.org/multipage/document-sequences.html#creating-a-new-auxiliary-browsing-context
  81. WebIDL::ExceptionOr<BrowsingContext::BrowsingContextAndDocument> BrowsingContext::create_a_new_auxiliary_browsing_context_and_document(GC::Ref<Page> page, GC::Ref<HTML::BrowsingContext> opener)
  82. {
  83. // 1. Let openerTopLevelBrowsingContext be opener's top-level traversable's active browsing context.
  84. auto opener_top_level_browsing_context = opener->top_level_traversable()->active_browsing_context();
  85. // 2. Let group be openerTopLevelBrowsingContext's group.
  86. auto group = opener_top_level_browsing_context->group();
  87. // 3. Assert: group is non-null, as navigating invokes this directly.
  88. VERIFY(group);
  89. // 4. Set browsingContext and document be the result of creating a new browsing context and document with opener's active document, null, and group.
  90. auto [browsing_context, document] = TRY(create_a_new_browsing_context_and_document(page, opener->active_document(), nullptr, *group));
  91. // 5. Set browsingContext's is auxiliary to true.
  92. browsing_context->m_is_auxiliary = true;
  93. // 6. Append browsingContext to group.
  94. group->append(browsing_context);
  95. // 7. Set browsingContext's opener browsing context to opener.
  96. browsing_context->set_opener_browsing_context(opener);
  97. // 8. Set browsingContext's virtual browsing context group ID to openerTopLevelBrowsingContext's virtual browsing context group ID.
  98. browsing_context->m_virtual_browsing_context_group_id = opener_top_level_browsing_context->m_virtual_browsing_context_group_id;
  99. // 9. Set browsingContext's opener origin at creation to opener's active document's origin.
  100. browsing_context->m_opener_origin_at_creation = opener->active_document()->origin();
  101. // 10. Return browsingContext and document.
  102. return BrowsingContext::BrowsingContextAndDocument { browsing_context, document };
  103. }
  104. static void populate_with_html_head_body(GC::Ref<DOM::Document> document)
  105. {
  106. auto html_node = MUST(DOM::create_element(document, HTML::TagNames::html, Namespace::HTML));
  107. auto head_element = MUST(DOM::create_element(document, HTML::TagNames::head, Namespace::HTML));
  108. MUST(html_node->append_child(head_element));
  109. auto body_element = MUST(DOM::create_element(document, HTML::TagNames::body, Namespace::HTML));
  110. MUST(html_node->append_child(body_element));
  111. MUST(document->append_child(html_node));
  112. }
  113. // https://html.spec.whatwg.org/multipage/document-sequences.html#creating-a-new-browsing-context
  114. WebIDL::ExceptionOr<BrowsingContext::BrowsingContextAndDocument> BrowsingContext::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)
  115. {
  116. auto& vm = group->vm();
  117. // 1. Let browsingContext be a new browsing context.
  118. GC::Ref<BrowsingContext> browsing_context = *vm.heap().allocate<BrowsingContext>(page);
  119. // 2. Let unsafeContextCreationTime be the unsafe shared current time.
  120. [[maybe_unused]] auto unsafe_context_creation_time = HighResolutionTime::unsafe_shared_current_time();
  121. // 3. Let creatorOrigin be null.
  122. Optional<URL::Origin> creator_origin = {};
  123. // 4. Let creatorBaseURL be null.
  124. Optional<URL::URL> creator_base_url = {};
  125. // 5. If creator is non-null, then:
  126. if (creator) {
  127. // 1. Set creatorOrigin to creator's origin.
  128. creator_origin = creator->origin();
  129. // 2. Set creatorBaseURL to creator's document base URL.
  130. creator_base_url = creator->base_url();
  131. // 3. Set browsingContext's virtual browsing context group ID to creator's browsing context's top-level browsing context's virtual browsing context group ID.
  132. VERIFY(creator->browsing_context());
  133. browsing_context->m_virtual_browsing_context_group_id = creator->browsing_context()->top_level_browsing_context()->m_virtual_browsing_context_group_id;
  134. }
  135. // FIXME: 6. Let sandboxFlags be the result of determining the creation sandboxing flags given browsingContext and embedder.
  136. SandboxingFlagSet sandbox_flags = {};
  137. // 7. Let origin be the result of determining the origin given about:blank, sandboxFlags, and creatorOrigin.
  138. auto origin = determine_the_origin(URL::URL("about:blank"sv), sandbox_flags, creator_origin);
  139. // FIXME: 8. Let permissionsPolicy be the result of creating a permissions policy given embedder and origin. [PERMISSIONSPOLICY]
  140. // FIXME: 9. Let agent be the result of obtaining a similar-origin window agent given origin, group, and false.
  141. GC::Ptr<Window> window;
  142. // 10. Let realm execution context be the result of creating a new JavaScript realm given agent and the following customizations:
  143. auto realm_execution_context = Bindings::create_a_new_javascript_realm(
  144. Bindings::main_thread_vm(),
  145. [&](JS::Realm& realm) -> JS::Object* {
  146. auto window_proxy = realm.create<WindowProxy>(realm);
  147. browsing_context->set_window_proxy(window_proxy);
  148. // - For the global object, create a new Window object.
  149. window = Window::create(realm);
  150. return window.ptr();
  151. },
  152. [&](JS::Realm&) -> JS::Object* {
  153. // - For the global this binding, use browsingContext's WindowProxy object.
  154. return browsing_context->window_proxy();
  155. });
  156. // 11. Let topLevelCreationURL be about:blank if embedder is null; otherwise embedder's relevant settings object's top-level creation URL.
  157. auto top_level_creation_url = !embedder ? URL::URL("about:blank") : relevant_settings_object(*embedder).top_level_creation_url;
  158. // 12. Let topLevelOrigin be origin if embedder is null; otherwise embedder's relevant settings object's top-level origin.
  159. auto top_level_origin = !embedder ? origin : relevant_settings_object(*embedder).origin();
  160. // 13. Set up a window environment settings object with about:blank, realm execution context, null, topLevelCreationURL, and topLevelOrigin.
  161. WindowEnvironmentSettingsObject::setup(
  162. page,
  163. URL::URL("about:blank"),
  164. move(realm_execution_context),
  165. {},
  166. top_level_creation_url,
  167. top_level_origin);
  168. // 14. Let loadTimingInfo be a new document load timing info with its navigation start time set to the result of calling
  169. // coarsen time with unsafeContextCreationTime and the new environment settings object's cross-origin isolated capability.
  170. auto load_timing_info = DOM::DocumentLoadTimingInfo();
  171. load_timing_info.navigation_start_time = HighResolutionTime::coarsen_time(
  172. unsafe_context_creation_time,
  173. verify_cast<WindowEnvironmentSettingsObject>(Bindings::principal_host_defined_environment_settings_object(window->realm())).cross_origin_isolated_capability() == CanUseCrossOriginIsolatedAPIs::Yes);
  174. // 15. Let document be a new Document, with:
  175. auto document = HTML::HTMLDocument::create(window->realm());
  176. // Non-standard
  177. window->set_associated_document(*document);
  178. // type: "html"
  179. document->set_document_type(DOM::Document::Type::HTML);
  180. // content type: "text/html"
  181. document->set_content_type("text/html"_string);
  182. // mode: "quirks"
  183. document->set_quirks_mode(DOM::QuirksMode::Yes);
  184. // origin: origin
  185. document->set_origin(origin);
  186. // browsing context: browsingContext
  187. document->set_browsing_context(browsing_context);
  188. // FIXME: permissions policy: permissionsPolicy
  189. // active sandboxing flag set: sandboxFlags
  190. document->set_active_sandboxing_flag_set(sandbox_flags);
  191. // load timing info: loadTimingInfo
  192. document->set_load_timing_info(load_timing_info);
  193. // is initial about:blank: true
  194. document->set_is_initial_about_blank(true);
  195. // Spec issue: https://github.com/whatwg/html/issues/10261
  196. document->set_ready_to_run_scripts();
  197. // about base URL: creatorBaseURL
  198. document->set_about_base_url(creator_base_url);
  199. // allow declarative shadow roots: true
  200. document->set_allow_declarative_shadow_roots(true);
  201. // 16. If creator is non-null, then:
  202. if (creator) {
  203. // 1. Set document's referrer to the serialization of creator's URL.
  204. document->set_referrer(MUST(String::from_byte_string(creator->url().serialize())));
  205. // 2. Set document's policy container to a clone of creator's policy container.
  206. document->set_policy_container(creator->policy_container());
  207. // 3. If creator's origin is same origin with creator's relevant settings object's top-level origin,
  208. if (creator->origin().is_same_origin(creator->relevant_settings_object().top_level_origin)) {
  209. // then set document's opener policy to creator's browsing context's top-level browsing context's active document's opener policy.
  210. VERIFY(creator->browsing_context());
  211. VERIFY(creator->browsing_context()->top_level_browsing_context()->active_document());
  212. document->set_opener_policy(creator->browsing_context()->top_level_browsing_context()->active_document()->opener_policy());
  213. }
  214. }
  215. // 17. Assert: document's URL and document's relevant settings object's creation URL are about:blank.
  216. VERIFY(document->url() == "about:blank"sv);
  217. VERIFY(document->relevant_settings_object().creation_url == "about:blank"sv);
  218. // 18. Mark document as ready for post-load tasks.
  219. document->set_ready_for_post_load_tasks(true);
  220. // 19. Populate with html/head/body given document.
  221. populate_with_html_head_body(*document);
  222. // 20. Make active document.
  223. document->make_active();
  224. // 21. Completely finish loading document.
  225. document->completely_finish_loading();
  226. // 22. Return browsingContext and document.
  227. return BrowsingContext::BrowsingContextAndDocument { browsing_context, document };
  228. }
  229. BrowsingContext::BrowsingContext(GC::Ref<Page> page)
  230. : m_page(page)
  231. {
  232. }
  233. BrowsingContext::~BrowsingContext() = default;
  234. void BrowsingContext::visit_edges(Cell::Visitor& visitor)
  235. {
  236. Base::visit_edges(visitor);
  237. visitor.visit(m_page);
  238. visitor.visit(m_window_proxy);
  239. visitor.visit(m_group);
  240. visitor.visit(m_first_child);
  241. visitor.visit(m_last_child);
  242. visitor.visit(m_next_sibling);
  243. visitor.visit(m_previous_sibling);
  244. visitor.visit(m_opener_browsing_context);
  245. }
  246. // https://html.spec.whatwg.org/multipage/document-sequences.html#bc-traversable
  247. GC::Ref<HTML::TraversableNavigable> BrowsingContext::top_level_traversable() const
  248. {
  249. // A browsing context's top-level traversable is its active document's node navigable's top-level traversable.
  250. auto traversable = active_document()->navigable()->top_level_traversable();
  251. VERIFY(traversable);
  252. VERIFY(traversable->is_top_level_traversable());
  253. return *traversable;
  254. }
  255. // https://html.spec.whatwg.org/multipage/browsers.html#top-level-browsing-context
  256. bool BrowsingContext::is_top_level() const
  257. {
  258. // FIXME: Remove this. The active document's navigable is sometimes null when it shouldn't be, failing assertions.
  259. return true;
  260. // A top-level browsing context is a browsing context whose active document's node navigable is a traversable navigable.
  261. return active_document() != nullptr && active_document()->navigable() != nullptr && active_document()->navigable()->is_traversable();
  262. }
  263. GC::Ptr<BrowsingContext> BrowsingContext::top_level_browsing_context() const
  264. {
  265. auto const* start = this;
  266. // 1. If start's active document is not fully active, then return null.
  267. if (!start->active_document()->is_fully_active()) {
  268. return nullptr;
  269. }
  270. // 2. Let navigable be start's active document's node navigable.
  271. auto navigable = start->active_document()->navigable();
  272. // 3. While navigable's parent is not null, set navigable to navigable's parent.
  273. while (navigable->parent()) {
  274. navigable = navigable->parent();
  275. }
  276. // 4. Return navigable's active browsing context.
  277. return navigable->active_browsing_context();
  278. }
  279. DOM::Document const* BrowsingContext::active_document() const
  280. {
  281. auto* window = active_window();
  282. if (!window)
  283. return nullptr;
  284. return &window->associated_document();
  285. }
  286. DOM::Document* BrowsingContext::active_document()
  287. {
  288. auto* window = active_window();
  289. if (!window)
  290. return nullptr;
  291. return &window->associated_document();
  292. }
  293. // https://html.spec.whatwg.org/multipage/browsers.html#active-window
  294. HTML::Window* BrowsingContext::active_window()
  295. {
  296. return m_window_proxy->window();
  297. }
  298. // https://html.spec.whatwg.org/multipage/browsers.html#active-window
  299. HTML::Window const* BrowsingContext::active_window() const
  300. {
  301. return m_window_proxy->window();
  302. }
  303. HTML::WindowProxy* BrowsingContext::window_proxy()
  304. {
  305. return m_window_proxy.ptr();
  306. }
  307. HTML::WindowProxy const* BrowsingContext::window_proxy() const
  308. {
  309. return m_window_proxy.ptr();
  310. }
  311. void BrowsingContext::set_window_proxy(GC::Ptr<WindowProxy> window_proxy)
  312. {
  313. m_window_proxy = move(window_proxy);
  314. }
  315. BrowsingContextGroup* BrowsingContext::group()
  316. {
  317. return m_group;
  318. }
  319. BrowsingContextGroup const* BrowsingContext::group() const
  320. {
  321. return m_group;
  322. }
  323. void BrowsingContext::set_group(BrowsingContextGroup* group)
  324. {
  325. m_group = group;
  326. }
  327. // https://html.spec.whatwg.org/multipage/browsers.html#bcg-remove
  328. void BrowsingContext::remove()
  329. {
  330. // 1. Assert: browsingContext's group is non-null, because a browsing context only gets discarded once.
  331. VERIFY(group());
  332. // 2. Let group be browsingContext's group.
  333. GC::Ref<BrowsingContextGroup> group = *this->group();
  334. // 3. Set browsingContext's group to null.
  335. set_group(nullptr);
  336. // 4. Remove browsingContext from group's browsing context set.
  337. group->browsing_context_set().remove(*this);
  338. // 5. If group's browsing context set is empty, then remove group from the user agent's browsing context group set.
  339. // NOTE: This is done by ~BrowsingContextGroup() when the refcount reaches 0.
  340. }
  341. // https://html.spec.whatwg.org/multipage/origin.html#one-permitted-sandboxed-navigator
  342. BrowsingContext const* BrowsingContext::the_one_permitted_sandboxed_navigator() const
  343. {
  344. // FIXME: Implement this.
  345. return nullptr;
  346. }
  347. GC::Ptr<BrowsingContext> BrowsingContext::first_child() const
  348. {
  349. return m_first_child;
  350. }
  351. GC::Ptr<BrowsingContext> BrowsingContext::next_sibling() const
  352. {
  353. return m_next_sibling;
  354. }
  355. // https://html.spec.whatwg.org/multipage/document-sequences.html#ancestor-browsing-context
  356. bool BrowsingContext::is_ancestor_of(BrowsingContext const& potential_descendant) const
  357. {
  358. // A browsing context potentialDescendant is said to be an ancestor of a browsing context potentialAncestor if the following algorithm returns true:
  359. // 1. Let potentialDescendantDocument be potentialDescendant's active document.
  360. auto const* potential_descendant_document = potential_descendant.active_document();
  361. // 2. If potentialDescendantDocument is not fully active, then return false.
  362. if (!potential_descendant_document->is_fully_active())
  363. return false;
  364. // 3. Let ancestorBCs be the list obtained by taking the browsing context of the active document of each member of potentialDescendantDocument's ancestor navigables.
  365. for (auto const& ancestor : potential_descendant_document->ancestor_navigables()) {
  366. auto ancestor_browsing_context = ancestor->active_browsing_context();
  367. // 4. If ancestorBCs contains potentialAncestor, then return true.
  368. if (ancestor_browsing_context == this)
  369. return true;
  370. }
  371. // 5. Return false.
  372. return false;
  373. }
  374. // https://html.spec.whatwg.org/multipage/document-sequences.html#familiar-with
  375. bool BrowsingContext::is_familiar_with(BrowsingContext const& other) const
  376. {
  377. // A browsing context A is familiar with a second browsing context B if the following algorithm returns true:
  378. auto const& A = *this;
  379. auto const& B = other;
  380. // 1. If A's active document's origin is same origin with B's active document's origin, then return true.
  381. if (A.active_document()->origin().is_same_origin(B.active_document()->origin()))
  382. return true;
  383. // 2. If A's top-level browsing context is B, then return true.
  384. if (A.top_level_browsing_context() == &B)
  385. return true;
  386. // 3. If B is an auxiliary browsing context and A is familiar with B's opener browsing context, then return true.
  387. if (B.opener_browsing_context() != nullptr && A.is_familiar_with(*B.opener_browsing_context()))
  388. return true;
  389. // 4. If there exists an ancestor browsing context of B whose active document has the same origin as the active document of A, then return true.
  390. // NOTE: This includes the case where A is an ancestor browsing context of B.
  391. // If B's active document is not fully active then it cannot have ancestor browsing context
  392. if (!B.active_document()->is_fully_active())
  393. return false;
  394. for (auto const& ancestor : B.active_document()->ancestor_navigables()) {
  395. if (ancestor->active_document()->origin().is_same_origin(A.active_document()->origin()))
  396. return true;
  397. }
  398. // 5. Return false.
  399. return false;
  400. }
  401. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#snapshotting-target-snapshot-params
  402. SandboxingFlagSet determine_the_creation_sandboxing_flags(BrowsingContext const&, GC::Ptr<DOM::Element>)
  403. {
  404. // FIXME: Populate this once we have the proper flag sets on BrowsingContext
  405. return {};
  406. }
  407. bool BrowsingContext::has_navigable_been_destroyed() const
  408. {
  409. auto navigable = active_document()->navigable();
  410. return !navigable || navigable->has_been_destroyed();
  411. }
  412. }