BrowsingContext.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. JS_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(JS::NonnullGCPtr<Page> page, JS::NonnullGCPtr<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(JS::NonnullGCPtr<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(JS::NonnullGCPtr<Page> page, JS::GCPtr<DOM::Document> creator, JS::GCPtr<DOM::Element> embedder, JS::NonnullGCPtr<BrowsingContextGroup> group)
  115. {
  116. auto& vm = group->vm();
  117. // 1. Let browsingContext be a new browsing context.
  118. JS::NonnullGCPtr<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. // 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. JS::GCPtr<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. // FIXME: active sandboxing flag set: sandboxFlags
  190. // load timing info: loadTimingInfo
  191. document->set_load_timing_info(load_timing_info);
  192. // is initial about:blank: true
  193. document->set_is_initial_about_blank(true);
  194. // Spec issue: https://github.com/whatwg/html/issues/10261
  195. document->set_ready_to_run_scripts();
  196. // about base URL: creatorBaseURL
  197. document->set_about_base_url(creator_base_url);
  198. // allow declarative shadow roots: true
  199. document->set_allow_declarative_shadow_roots(true);
  200. // 16. If creator is non-null, then:
  201. if (creator) {
  202. // 1. Set document's referrer to the serialization of creator's URL.
  203. document->set_referrer(MUST(String::from_byte_string(creator->url().serialize())));
  204. // 2. Set document's policy container to a clone of creator's policy container.
  205. document->set_policy_container(creator->policy_container());
  206. // 3. If creator's origin is same origin with creator's relevant settings object's top-level origin,
  207. if (creator->origin().is_same_origin(creator->relevant_settings_object().top_level_origin)) {
  208. // then set document's opener policy to creator's browsing context's top-level browsing context's active document's opener policy.
  209. VERIFY(creator->browsing_context());
  210. VERIFY(creator->browsing_context()->top_level_browsing_context()->active_document());
  211. document->set_opener_policy(creator->browsing_context()->top_level_browsing_context()->active_document()->opener_policy());
  212. }
  213. }
  214. // 17. Assert: document's URL and document's relevant settings object's creation URL are about:blank.
  215. VERIFY(document->url() == "about:blank"sv);
  216. VERIFY(document->relevant_settings_object().creation_url == "about:blank"sv);
  217. // 18. Mark document as ready for post-load tasks.
  218. document->set_ready_for_post_load_tasks(true);
  219. // 19. Populate with html/head/body given document.
  220. populate_with_html_head_body(*document);
  221. // 20. Make active document.
  222. document->make_active();
  223. // 21. Completely finish loading document.
  224. document->completely_finish_loading();
  225. // 22. Return browsingContext and document.
  226. return BrowsingContext::BrowsingContextAndDocument { browsing_context, document };
  227. }
  228. BrowsingContext::BrowsingContext(JS::NonnullGCPtr<Page> page)
  229. : m_page(page)
  230. {
  231. }
  232. BrowsingContext::~BrowsingContext() = default;
  233. void BrowsingContext::visit_edges(Cell::Visitor& visitor)
  234. {
  235. Base::visit_edges(visitor);
  236. visitor.visit(m_page);
  237. visitor.visit(m_window_proxy);
  238. visitor.visit(m_group);
  239. visitor.visit(m_first_child);
  240. visitor.visit(m_last_child);
  241. visitor.visit(m_next_sibling);
  242. visitor.visit(m_previous_sibling);
  243. visitor.visit(m_opener_browsing_context);
  244. }
  245. // https://html.spec.whatwg.org/multipage/document-sequences.html#bc-traversable
  246. JS::NonnullGCPtr<HTML::TraversableNavigable> BrowsingContext::top_level_traversable() const
  247. {
  248. // A browsing context's top-level traversable is its active document's node navigable's top-level traversable.
  249. auto traversable = active_document()->navigable()->top_level_traversable();
  250. VERIFY(traversable);
  251. VERIFY(traversable->is_top_level_traversable());
  252. return *traversable;
  253. }
  254. // https://html.spec.whatwg.org/multipage/browsers.html#top-level-browsing-context
  255. bool BrowsingContext::is_top_level() const
  256. {
  257. // FIXME: Remove this. The active document's navigable is sometimes null when it shouldn't be, failing assertions.
  258. return true;
  259. // A top-level browsing context is a browsing context whose active document's node navigable is a traversable navigable.
  260. return active_document() != nullptr && active_document()->navigable() != nullptr && active_document()->navigable()->is_traversable();
  261. }
  262. JS::GCPtr<BrowsingContext> BrowsingContext::top_level_browsing_context() const
  263. {
  264. auto const* start = this;
  265. // 1. If start's active document is not fully active, then return null.
  266. if (!start->active_document()->is_fully_active()) {
  267. return nullptr;
  268. }
  269. // 2. Let navigable be start's active document's node navigable.
  270. auto navigable = start->active_document()->navigable();
  271. // 3. While navigable's parent is not null, set navigable to navigable's parent.
  272. while (navigable->parent()) {
  273. navigable = navigable->parent();
  274. }
  275. // 4. Return navigable's active browsing context.
  276. return navigable->active_browsing_context();
  277. }
  278. DOM::Document const* BrowsingContext::active_document() const
  279. {
  280. auto* window = active_window();
  281. if (!window)
  282. return nullptr;
  283. return &window->associated_document();
  284. }
  285. DOM::Document* BrowsingContext::active_document()
  286. {
  287. auto* window = active_window();
  288. if (!window)
  289. return nullptr;
  290. return &window->associated_document();
  291. }
  292. // https://html.spec.whatwg.org/multipage/browsers.html#active-window
  293. HTML::Window* BrowsingContext::active_window()
  294. {
  295. return m_window_proxy->window();
  296. }
  297. // https://html.spec.whatwg.org/multipage/browsers.html#active-window
  298. HTML::Window const* BrowsingContext::active_window() const
  299. {
  300. return m_window_proxy->window();
  301. }
  302. HTML::WindowProxy* BrowsingContext::window_proxy()
  303. {
  304. return m_window_proxy.ptr();
  305. }
  306. HTML::WindowProxy const* BrowsingContext::window_proxy() const
  307. {
  308. return m_window_proxy.ptr();
  309. }
  310. void BrowsingContext::set_window_proxy(JS::GCPtr<WindowProxy> window_proxy)
  311. {
  312. m_window_proxy = move(window_proxy);
  313. }
  314. BrowsingContextGroup* BrowsingContext::group()
  315. {
  316. return m_group;
  317. }
  318. BrowsingContextGroup const* BrowsingContext::group() const
  319. {
  320. return m_group;
  321. }
  322. void BrowsingContext::set_group(BrowsingContextGroup* group)
  323. {
  324. m_group = group;
  325. }
  326. // https://html.spec.whatwg.org/multipage/browsers.html#bcg-remove
  327. void BrowsingContext::remove()
  328. {
  329. // 1. Assert: browsingContext's group is non-null, because a browsing context only gets discarded once.
  330. VERIFY(group());
  331. // 2. Let group be browsingContext's group.
  332. JS::NonnullGCPtr<BrowsingContextGroup> group = *this->group();
  333. // 3. Set browsingContext's group to null.
  334. set_group(nullptr);
  335. // 4. Remove browsingContext from group's browsing context set.
  336. group->browsing_context_set().remove(*this);
  337. // 5. If group's browsing context set is empty, then remove group from the user agent's browsing context group set.
  338. // NOTE: This is done by ~BrowsingContextGroup() when the refcount reaches 0.
  339. }
  340. // https://html.spec.whatwg.org/multipage/origin.html#one-permitted-sandboxed-navigator
  341. BrowsingContext const* BrowsingContext::the_one_permitted_sandboxed_navigator() const
  342. {
  343. // FIXME: Implement this.
  344. return nullptr;
  345. }
  346. JS::GCPtr<BrowsingContext> BrowsingContext::first_child() const
  347. {
  348. return m_first_child;
  349. }
  350. JS::GCPtr<BrowsingContext> BrowsingContext::next_sibling() const
  351. {
  352. return m_next_sibling;
  353. }
  354. // https://html.spec.whatwg.org/multipage/document-sequences.html#ancestor-browsing-context
  355. bool BrowsingContext::is_ancestor_of(BrowsingContext const& potential_descendant) const
  356. {
  357. // A browsing context potentialDescendant is said to be an ancestor of a browsing context potentialAncestor if the following algorithm returns true:
  358. // 1. Let potentialDescendantDocument be potentialDescendant's active document.
  359. auto const* potential_descendant_document = potential_descendant.active_document();
  360. // 2. If potentialDescendantDocument is not fully active, then return false.
  361. if (!potential_descendant_document->is_fully_active())
  362. return false;
  363. // 3. Let ancestorBCs be the list obtained by taking the browsing context of the active document of each member of potentialDescendantDocument's ancestor navigables.
  364. for (auto const& ancestor : potential_descendant_document->ancestor_navigables()) {
  365. auto ancestor_browsing_context = ancestor->active_browsing_context();
  366. // 4. If ancestorBCs contains potentialAncestor, then return true.
  367. if (ancestor_browsing_context == this)
  368. return true;
  369. }
  370. // 5. Return false.
  371. return false;
  372. }
  373. // https://html.spec.whatwg.org/multipage/document-sequences.html#familiar-with
  374. bool BrowsingContext::is_familiar_with(BrowsingContext const& other) const
  375. {
  376. // A browsing context A is familiar with a second browsing context B if the following algorithm returns true:
  377. auto const& A = *this;
  378. auto const& B = other;
  379. // 1. If A's active document's origin is same origin with B's active document's origin, then return true.
  380. if (A.active_document()->origin().is_same_origin(B.active_document()->origin()))
  381. return true;
  382. // 2. If A's top-level browsing context is B, then return true.
  383. if (A.top_level_browsing_context() == &B)
  384. return true;
  385. // 3. If B is an auxiliary browsing context and A is familiar with B's opener browsing context, then return true.
  386. if (B.opener_browsing_context() != nullptr && A.is_familiar_with(*B.opener_browsing_context()))
  387. return true;
  388. // 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.
  389. // NOTE: This includes the case where A is an ancestor browsing context of B.
  390. // If B's active document is not fully active then it cannot have ancestor browsing context
  391. if (!B.active_document()->is_fully_active())
  392. return false;
  393. for (auto const& ancestor : B.active_document()->ancestor_navigables()) {
  394. if (ancestor->active_document()->origin().is_same_origin(A.active_document()->origin()))
  395. return true;
  396. }
  397. // 5. Return false.
  398. return false;
  399. }
  400. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#snapshotting-target-snapshot-params
  401. SandboxingFlagSet determine_the_creation_sandboxing_flags(BrowsingContext const&, JS::GCPtr<DOM::Element>)
  402. {
  403. // FIXME: Populate this once we have the proper flag sets on BrowsingContext
  404. return {};
  405. }
  406. bool BrowsingContext::has_navigable_been_destroyed() const
  407. {
  408. auto navigable = active_document()->navigable();
  409. return !navigable || navigable->has_been_destroyed();
  410. }
  411. }