BrowsingContext.cpp 21 KB

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