BrowsingContext.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.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/CrossOriginOpenerPolicy.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.serialize_path() == "blank"sv
  40. && url.raw_username().is_empty()
  41. && url.raw_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.serialize_path() == "srcdoc"sv
  50. && !url.query().has_value()
  51. && url.raw_username().is_empty()
  52. && url.raw_password().is_empty()
  53. && url.host().has<Empty>();
  54. }
  55. // https://html.spec.whatwg.org/multipage/document-sequences.html#determining-the-origin
  56. HTML::Origin determine_the_origin(URL::URL const& url, SandboxingFlagSet sandbox_flags, Optional<HTML::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 HTML::Origin {};
  61. }
  62. // FIXME: 2. If url is null, then return a new opaque origin.
  63. // FIXME: There appears to be no way to get a null URL here, so it might be a spec bug.
  64. // 3. If url is about:srcdoc, then:
  65. if (url == "about:srcdoc"sv) {
  66. // 1. Assert: sourceOrigin is non-null.
  67. VERIFY(source_origin.has_value());
  68. // 2. Return sourceOrigin.
  69. return source_origin.release_value();
  70. }
  71. // 4. If url matches about:blank and sourceOrigin is non-null, then return sourceOrigin.
  72. if (url_matches_about_blank(url) && source_origin.has_value())
  73. return source_origin.release_value();
  74. // 5. Return url's origin.
  75. return DOMURL::url_origin(url);
  76. }
  77. // https://html.spec.whatwg.org/multipage/document-sequences.html#creating-a-new-auxiliary-browsing-context
  78. WebIDL::ExceptionOr<BrowsingContext::BrowsingContextAndDocument> BrowsingContext::create_a_new_auxiliary_browsing_context_and_document(JS::NonnullGCPtr<Page> page, JS::NonnullGCPtr<HTML::BrowsingContext> opener)
  79. {
  80. // 1. Let openerTopLevelBrowsingContext be opener's top-level traversable's active browsing context.
  81. auto opener_top_level_browsing_context = opener->top_level_traversable()->active_browsing_context();
  82. // 2. Let group be openerTopLevelBrowsingContext's group.
  83. auto group = opener_top_level_browsing_context->group();
  84. // 3. Assert: group is non-null, as navigating invokes this directly.
  85. VERIFY(group);
  86. // 4. Set browsingContext and document be the result of creating a new browsing context and document with opener's active document, null, and group.
  87. auto [browsing_context, document] = TRY(create_a_new_browsing_context_and_document(page, opener->active_document(), nullptr, *group));
  88. // FIXME: 5. Set browsingContext's is auxiliary to true.
  89. // 6. Append browsingContext to group.
  90. group->append(browsing_context);
  91. // 7. Set browsingContext's opener browsing context to opener.
  92. browsing_context->set_opener_browsing_context(opener);
  93. // FIXME: 8. Set browsingContext's virtual browsing context group ID to openerTopLevelBrowsingContext's virtual browsing context group ID.
  94. // FIXME: 9. Set browsingContext's opener origin at creation to opener's active document's origin.
  95. // 10. Return browsingContext and document.
  96. return BrowsingContext::BrowsingContextAndDocument { browsing_context, document };
  97. }
  98. // https://html.spec.whatwg.org/multipage/document-sequences.html#creating-a-new-browsing-context
  99. 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)
  100. {
  101. auto& vm = group->vm();
  102. // 1. Let browsingContext be a new browsing context.
  103. JS::NonnullGCPtr<BrowsingContext> browsing_context = *vm.heap().allocate_without_realm<BrowsingContext>(page);
  104. // 2. Let unsafeContextCreationTime be the unsafe shared current time.
  105. [[maybe_unused]] auto unsafe_context_creation_time = HighResolutionTime::unsafe_shared_current_time();
  106. // 3. Let creatorOrigin be null.
  107. Optional<Origin> creator_origin = {};
  108. // FIXME: This algorithm needs re-aligned with the spec
  109. Optional<URL::URL> creator_base_url = {};
  110. // 4. If creator is non-null, then:
  111. if (creator) {
  112. // 1. Set creatorOrigin to creator's origin.
  113. creator_origin = creator->origin();
  114. // FIXME: This algorithm needs re-aligned with the spec
  115. creator_base_url = creator->base_url();
  116. // FIXME: 2. Set browsingContext's creator base URL to an algorithm which returns creator's base URL.
  117. // FIXME: 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.
  118. }
  119. // FIXME: 5. Let sandboxFlags be the result of determining the creation sandboxing flags given browsingContext and embedder.
  120. SandboxingFlagSet sandbox_flags = {};
  121. // 6. Let origin be the result of determining the origin given about:blank, sandboxFlags, and creatorOrigin.
  122. auto origin = determine_the_origin(URL::URL("about:blank"sv), sandbox_flags, creator_origin);
  123. // FIXME: 7. Let permissionsPolicy be the result of creating a permissions policy given browsingContext and origin. [PERMISSIONSPOLICY]
  124. // FIXME: 8. Let agent be the result of obtaining a similar-origin window agent given origin, group, and false.
  125. JS::GCPtr<Window> window;
  126. // 9. Let realm execution context be the result of creating a new JavaScript realm given agent and the following customizations:
  127. auto realm_execution_context = Bindings::create_a_new_javascript_realm(
  128. Bindings::main_thread_vm(),
  129. [&](JS::Realm& realm) -> JS::Object* {
  130. auto window_proxy = realm.heap().allocate<WindowProxy>(realm, realm);
  131. browsing_context->set_window_proxy(window_proxy);
  132. // - For the global object, create a new Window object.
  133. window = Window::create(realm);
  134. return window.ptr();
  135. },
  136. [&](JS::Realm&) -> JS::Object* {
  137. // - For the global this binding, use browsingContext's WindowProxy object.
  138. return browsing_context->window_proxy();
  139. });
  140. // 10. Let topLevelCreationURL be about:blank if embedder is null; otherwise embedder's relevant settings object's top-level creation URL.
  141. auto top_level_creation_url = !embedder ? URL::URL("about:blank") : relevant_settings_object(*embedder).top_level_creation_url;
  142. // 11. Let topLevelOrigin be origin if embedder is null; otherwise embedder's relevant settings object's top-level origin.
  143. auto top_level_origin = !embedder ? origin : relevant_settings_object(*embedder).origin();
  144. // 12. Set up a window environment settings object with about:blank, realm execution context, null, topLevelCreationURL, and topLevelOrigin.
  145. WindowEnvironmentSettingsObject::setup(
  146. page,
  147. URL::URL("about:blank"),
  148. move(realm_execution_context),
  149. {},
  150. top_level_creation_url,
  151. top_level_origin);
  152. // 13. Let loadTimingInfo be a new document load timing info with its navigation start time set to the result of calling
  153. // coarsen time with unsafeContextCreationTime and the new environment settings object's cross-origin isolated capability.
  154. auto load_timing_info = DOM::DocumentLoadTimingInfo();
  155. load_timing_info.navigation_start_time = HighResolutionTime::coarsen_time(
  156. unsafe_context_creation_time,
  157. verify_cast<WindowEnvironmentSettingsObject>(Bindings::host_defined_environment_settings_object(window->realm())).cross_origin_isolated_capability() == CanUseCrossOriginIsolatedAPIs::Yes);
  158. // 14. Let document be a new Document, with:
  159. auto document = HTML::HTMLDocument::create(window->realm());
  160. // Non-standard
  161. window->set_associated_document(*document);
  162. // type: "html"
  163. document->set_document_type(DOM::Document::Type::HTML);
  164. // content type: "text/html"
  165. document->set_content_type("text/html"_string);
  166. // mode: "quirks"
  167. document->set_quirks_mode(DOM::QuirksMode::Yes);
  168. // origin: origin
  169. document->set_origin(origin);
  170. // browsing context: browsingContext
  171. document->set_browsing_context(browsing_context);
  172. // FIXME: permissions policy: permissionsPolicy
  173. // FIXME: active sandboxing flag set: sandboxFlags
  174. // load timing info: loadTimingInfo
  175. document->set_load_timing_info(load_timing_info);
  176. // is initial about:blank: true
  177. document->set_is_initial_about_blank(true);
  178. // about base URL: creatorBaseURL
  179. document->set_about_base_url(creator_base_url);
  180. // 15. If creator is non-null, then:
  181. if (creator) {
  182. // 1. Set document's referrer to the serialization of creator's URL.
  183. document->set_referrer(MUST(String::from_byte_string(creator->url().serialize())));
  184. // FIXME: 2. Set document's policy container to a clone of creator's policy container.
  185. // 3. If creator's origin is same origin with creator's relevant settings object's top-level origin,
  186. if (creator->origin().is_same_origin(creator->relevant_settings_object().top_level_origin)) {
  187. // then set document's cross-origin opener policy to creator's browsing context's top-level browsing context's active document's cross-origin opener policy.
  188. VERIFY(creator->browsing_context());
  189. VERIFY(creator->browsing_context()->top_level_browsing_context()->active_document());
  190. document->set_cross_origin_opener_policy(creator->browsing_context()->top_level_browsing_context()->active_document()->cross_origin_opener_policy());
  191. }
  192. }
  193. // 16. Assert: document's URL and document's relevant settings object's creation URL are about:blank.
  194. VERIFY(document->url() == "about:blank"sv);
  195. VERIFY(document->relevant_settings_object().creation_url == "about:blank"sv);
  196. // 17. Mark document as ready for post-load tasks.
  197. document->set_ready_for_post_load_tasks(true);
  198. // 18. Ensure that document has a single child html node, which itself has two empty child nodes: a head element, and a body element.
  199. auto html_node = TRY(DOM::create_element(document, HTML::TagNames::html, Namespace::HTML));
  200. auto head_element = TRY(DOM::create_element(document, HTML::TagNames::head, Namespace::HTML));
  201. TRY(html_node->append_child(head_element));
  202. auto body_element = TRY(DOM::create_element(document, HTML::TagNames::body, Namespace::HTML));
  203. TRY(html_node->append_child(body_element));
  204. TRY(document->append_child(html_node));
  205. // 19. Make active document.
  206. document->make_active();
  207. // 20. Completely finish loading document.
  208. document->completely_finish_loading();
  209. // 21. Return browsingContext and document.
  210. return BrowsingContext::BrowsingContextAndDocument { browsing_context, document };
  211. }
  212. BrowsingContext::BrowsingContext(JS::NonnullGCPtr<Page> page)
  213. : m_page(page)
  214. , m_event_handler({}, *this)
  215. {
  216. m_cursor_blink_timer = Core::Timer::create_repeating(500, [this] {
  217. if (!is_focused_context())
  218. return;
  219. if (!m_cursor_position)
  220. return;
  221. auto node = m_cursor_position->node();
  222. if (!node)
  223. return;
  224. node->document().update_layout();
  225. if (node->paintable()) {
  226. m_cursor_blink_state = !m_cursor_blink_state;
  227. node->paintable()->set_needs_display();
  228. }
  229. }).release_value_but_fixme_should_propagate_errors();
  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_cursor_position);
  237. visitor.visit(m_window_proxy);
  238. visitor.visit(m_group);
  239. visitor.visit(m_parent);
  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. m_event_handler.visit_edges(visitor);
  246. }
  247. // https://html.spec.whatwg.org/multipage/document-sequences.html#bc-traversable
  248. JS::NonnullGCPtr<HTML::TraversableNavigable> BrowsingContext::top_level_traversable() const
  249. {
  250. // A browsing context's top-level traversable is its active document's node navigable's top-level traversable.
  251. auto traversable = active_document()->navigable()->top_level_traversable();
  252. VERIFY(traversable);
  253. VERIFY(traversable->is_top_level_traversable());
  254. return *traversable;
  255. }
  256. void BrowsingContext::did_edit(Badge<EditEventHandler>)
  257. {
  258. reset_cursor_blink_cycle();
  259. if (m_cursor_position && is<DOM::Text>(*m_cursor_position->node())) {
  260. auto& text_node = static_cast<DOM::Text&>(*m_cursor_position->node());
  261. if (auto* text_node_owner = text_node.editable_text_node_owner())
  262. text_node_owner->did_edit_text_node({});
  263. }
  264. }
  265. void BrowsingContext::reset_cursor_blink_cycle()
  266. {
  267. m_cursor_blink_state = true;
  268. m_cursor_blink_timer->restart();
  269. if (m_cursor_position && m_cursor_position->node()->paintable())
  270. m_cursor_position->node()->paintable()->set_needs_display();
  271. }
  272. // https://html.spec.whatwg.org/multipage/browsers.html#top-level-browsing-context
  273. bool BrowsingContext::is_top_level() const
  274. {
  275. // A browsing context that has no parent browsing context is the top-level browsing context for itself and all of the browsing contexts for which it is an ancestor browsing context.
  276. return !parent();
  277. }
  278. bool BrowsingContext::is_focused_context() const
  279. {
  280. return &m_page->focused_context() == this;
  281. }
  282. JS::GCPtr<BrowsingContext> BrowsingContext::top_level_browsing_context() const
  283. {
  284. auto const* start = this;
  285. // 1. If start's active document is not fully active, then return null.
  286. if (!start->active_document()->is_fully_active()) {
  287. return nullptr;
  288. }
  289. // 2. Let navigable be start's active document's node navigable.
  290. auto navigable = start->active_document()->navigable();
  291. // 3. While navigable's parent is not null, set navigable to navigable's parent.
  292. while (navigable->parent()) {
  293. navigable = navigable->parent();
  294. }
  295. // 4. Return navigable's active browsing context.
  296. return navigable->active_browsing_context();
  297. }
  298. void BrowsingContext::set_cursor_position(JS::NonnullGCPtr<DOM::Position> position)
  299. {
  300. if (m_cursor_position && m_cursor_position->equals(position))
  301. return;
  302. if (m_cursor_position && m_cursor_position->node()->paintable())
  303. m_cursor_position->node()->paintable()->set_needs_display();
  304. m_cursor_position = position;
  305. if (m_cursor_position && m_cursor_position->node()->paintable())
  306. m_cursor_position->node()->paintable()->set_needs_display();
  307. reset_cursor_blink_cycle();
  308. }
  309. static String visible_text_in_range(DOM::Range const& range)
  310. {
  311. // NOTE: This is an adaption of Range stringification, but we skip over DOM nodes that don't have a corresponding layout node.
  312. StringBuilder builder;
  313. if (range.start_container() == range.end_container() && is<DOM::Text>(*range.start_container())) {
  314. if (!range.start_container()->layout_node())
  315. return String {};
  316. return MUST(static_cast<DOM::Text const&>(*range.start_container()).data().substring_from_byte_offset(range.start_offset(), range.end_offset() - range.start_offset()));
  317. }
  318. if (is<DOM::Text>(*range.start_container()) && range.start_container()->layout_node())
  319. builder.append(static_cast<DOM::Text const&>(*range.start_container()).data().bytes_as_string_view().substring_view(range.start_offset()));
  320. for (DOM::Node const* node = range.start_container(); node != range.end_container()->next_sibling(); node = node->next_in_pre_order()) {
  321. if (is<DOM::Text>(*node) && range.contains_node(*node) && node->layout_node())
  322. builder.append(static_cast<DOM::Text const&>(*node).data());
  323. }
  324. if (is<DOM::Text>(*range.end_container()) && range.end_container()->layout_node())
  325. builder.append(static_cast<DOM::Text const&>(*range.end_container()).data().bytes_as_string_view().substring_view(0, range.end_offset()));
  326. return MUST(builder.to_string());
  327. }
  328. String BrowsingContext::selected_text() const
  329. {
  330. auto const* document = active_document();
  331. if (!document)
  332. return String {};
  333. auto selection = const_cast<DOM::Document&>(*document).get_selection();
  334. auto range = selection->range();
  335. if (!range)
  336. return String {};
  337. return visible_text_in_range(*range);
  338. }
  339. void BrowsingContext::select_all()
  340. {
  341. auto* document = active_document();
  342. if (!document)
  343. return;
  344. auto* body = document->body();
  345. if (!body)
  346. return;
  347. auto selection = document->get_selection();
  348. if (!selection)
  349. return;
  350. (void)selection->select_all_children(*document->body());
  351. }
  352. void BrowsingContext::paste(String const& text)
  353. {
  354. auto* document = active_document();
  355. if (!document)
  356. return;
  357. m_event_handler.handle_paste(text);
  358. }
  359. bool BrowsingContext::increment_cursor_position_offset()
  360. {
  361. if (!m_cursor_position->increment_offset())
  362. return false;
  363. reset_cursor_blink_cycle();
  364. return true;
  365. }
  366. bool BrowsingContext::decrement_cursor_position_offset()
  367. {
  368. if (!m_cursor_position->decrement_offset())
  369. return false;
  370. reset_cursor_blink_cycle();
  371. return true;
  372. }
  373. // https://html.spec.whatwg.org/multipage/interaction.html#currently-focused-area-of-a-top-level-browsing-context
  374. JS::GCPtr<DOM::Node> BrowsingContext::currently_focused_area()
  375. {
  376. // 1. If topLevelBC does not have system focus, then return null.
  377. if (!is_focused_context())
  378. return nullptr;
  379. // 2. Let candidate be topLevelBC's active document.
  380. auto* candidate = active_document();
  381. // 3. While candidate's focused area is a browsing context container with a non-null nested browsing context:
  382. // set candidate to the active document of that browsing context container's nested browsing context.
  383. while (candidate->focused_element()
  384. && is<HTML::NavigableContainer>(candidate->focused_element())
  385. && static_cast<HTML::NavigableContainer&>(*candidate->focused_element()).nested_browsing_context()) {
  386. candidate = static_cast<HTML::NavigableContainer&>(*candidate->focused_element()).nested_browsing_context()->active_document();
  387. }
  388. // 4. If candidate's focused area is non-null, set candidate to candidate's focused area.
  389. if (candidate->focused_element()) {
  390. // NOTE: We return right away here instead of assigning to candidate,
  391. // since that would require compromising type safety.
  392. return candidate->focused_element();
  393. }
  394. // 5. Return candidate.
  395. return candidate;
  396. }
  397. DOM::Document const* BrowsingContext::active_document() const
  398. {
  399. auto* window = active_window();
  400. if (!window)
  401. return nullptr;
  402. return &window->associated_document();
  403. }
  404. DOM::Document* BrowsingContext::active_document()
  405. {
  406. auto* window = active_window();
  407. if (!window)
  408. return nullptr;
  409. return &window->associated_document();
  410. }
  411. // https://html.spec.whatwg.org/multipage/browsers.html#active-window
  412. HTML::Window* BrowsingContext::active_window()
  413. {
  414. return m_window_proxy->window();
  415. }
  416. // https://html.spec.whatwg.org/multipage/browsers.html#active-window
  417. HTML::Window const* BrowsingContext::active_window() const
  418. {
  419. return m_window_proxy->window();
  420. }
  421. HTML::WindowProxy* BrowsingContext::window_proxy()
  422. {
  423. return m_window_proxy.ptr();
  424. }
  425. HTML::WindowProxy const* BrowsingContext::window_proxy() const
  426. {
  427. return m_window_proxy.ptr();
  428. }
  429. void BrowsingContext::set_window_proxy(JS::GCPtr<WindowProxy> window_proxy)
  430. {
  431. m_window_proxy = move(window_proxy);
  432. }
  433. BrowsingContextGroup* BrowsingContext::group()
  434. {
  435. return m_group;
  436. }
  437. void BrowsingContext::set_group(BrowsingContextGroup* group)
  438. {
  439. m_group = group;
  440. }
  441. // https://html.spec.whatwg.org/multipage/browsers.html#bcg-remove
  442. void BrowsingContext::remove()
  443. {
  444. // 1. Assert: browsingContext's group is non-null, because a browsing context only gets discarded once.
  445. VERIFY(group());
  446. // 2. Let group be browsingContext's group.
  447. JS::NonnullGCPtr<BrowsingContextGroup> group = *this->group();
  448. // 3. Set browsingContext's group to null.
  449. set_group(nullptr);
  450. // 4. Remove browsingContext from group's browsing context set.
  451. group->browsing_context_set().remove(*this);
  452. // 5. If group's browsing context set is empty, then remove group from the user agent's browsing context group set.
  453. // NOTE: This is done by ~BrowsingContextGroup() when the refcount reaches 0.
  454. }
  455. // https://html.spec.whatwg.org/multipage/origin.html#one-permitted-sandboxed-navigator
  456. BrowsingContext const* BrowsingContext::the_one_permitted_sandboxed_navigator() const
  457. {
  458. // FIXME: Implement this.
  459. return nullptr;
  460. }
  461. JS::GCPtr<BrowsingContext> BrowsingContext::first_child() const
  462. {
  463. return m_first_child;
  464. }
  465. JS::GCPtr<BrowsingContext> BrowsingContext::next_sibling() const
  466. {
  467. return m_next_sibling;
  468. }
  469. bool BrowsingContext::is_ancestor_of(BrowsingContext const& other) const
  470. {
  471. for (auto ancestor = other.parent(); ancestor; ancestor = ancestor->parent()) {
  472. if (ancestor == this)
  473. return true;
  474. }
  475. return false;
  476. }
  477. // https://html.spec.whatwg.org/multipage/document-sequences.html#familiar-with
  478. bool BrowsingContext::is_familiar_with(BrowsingContext const& other) const
  479. {
  480. // A browsing context A is familiar with a second browsing context B if the following algorithm returns true:
  481. auto const& A = *this;
  482. auto const& B = other;
  483. // 1. If A's active document's origin is same origin with B's active document's origin, then return true.
  484. if (A.active_document()->origin().is_same_origin(B.active_document()->origin()))
  485. return true;
  486. // 2. If A's top-level browsing context is B, then return true.
  487. if (A.top_level_browsing_context() == &B)
  488. return true;
  489. // 3. If B is an auxiliary browsing context and A is familiar with B's opener browsing context, then return true.
  490. if (B.opener_browsing_context() != nullptr && A.is_familiar_with(*B.opener_browsing_context()))
  491. return true;
  492. // 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.
  493. // NOTE: This includes the case where A is an ancestor browsing context of B.
  494. for (auto ancestor = B.parent(); ancestor; ancestor = ancestor->parent()) {
  495. if (ancestor->active_document()->origin().is_same_origin(A.active_document()->origin()))
  496. return true;
  497. }
  498. // 5. Return false.
  499. return false;
  500. }
  501. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#snapshotting-target-snapshot-params
  502. SandboxingFlagSet determine_the_creation_sandboxing_flags(BrowsingContext const&, JS::GCPtr<DOM::Element>)
  503. {
  504. // FIXME: Populate this once we have the proper flag sets on BrowsingContext
  505. return {};
  506. }
  507. bool BrowsingContext::has_navigable_been_destroyed() const
  508. {
  509. auto navigable = active_document()->navigable();
  510. return navigable && navigable->has_been_destroyed();
  511. }
  512. }