Environments.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
  3. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2022, networkException <networkexception@serenityos.org>
  5. * Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include <LibWeb/Bindings/MainThreadVM.h>
  10. #include <LibWeb/Bindings/PrincipalHostDefined.h>
  11. #include <LibWeb/Bindings/SyntheticHostDefined.h>
  12. #include <LibWeb/DOM/Document.h>
  13. #include <LibWeb/DOMURL/DOMURL.h>
  14. #include <LibWeb/Fetch/Infrastructure/FetchRecord.h>
  15. #include <LibWeb/HTML/Scripting/Environments.h>
  16. #include <LibWeb/HTML/Scripting/ExceptionReporter.h>
  17. #include <LibWeb/HTML/Scripting/WindowEnvironmentSettingsObject.h>
  18. #include <LibWeb/HTML/Window.h>
  19. #include <LibWeb/HTML/WorkerGlobalScope.h>
  20. #include <LibWeb/Page/Page.h>
  21. #include <LibWeb/SecureContexts/AbstractOperations.h>
  22. #include <LibWeb/StorageAPI/StorageManager.h>
  23. namespace Web::HTML {
  24. Environment::~Environment() = default;
  25. void Environment::visit_edges(Cell::Visitor& visitor)
  26. {
  27. Base::visit_edges(visitor);
  28. visitor.visit(target_browsing_context);
  29. }
  30. EnvironmentSettingsObject::EnvironmentSettingsObject(NonnullOwnPtr<JS::ExecutionContext> realm_execution_context)
  31. : m_realm_execution_context(move(realm_execution_context))
  32. {
  33. m_realm_execution_context->context_owner = this;
  34. // Register with the responsible event loop so we can perform step 4 of "perform a microtask checkpoint".
  35. responsible_event_loop().register_environment_settings_object({}, *this);
  36. }
  37. EnvironmentSettingsObject::~EnvironmentSettingsObject()
  38. {
  39. responsible_event_loop().unregister_environment_settings_object({}, *this);
  40. }
  41. void EnvironmentSettingsObject::initialize(JS::Realm& realm)
  42. {
  43. Base::initialize(realm);
  44. m_module_map = realm.heap().allocate<ModuleMap>();
  45. }
  46. void EnvironmentSettingsObject::visit_edges(Cell::Visitor& visitor)
  47. {
  48. Base::visit_edges(visitor);
  49. visitor.visit(m_responsible_event_loop);
  50. visitor.visit(m_module_map);
  51. m_realm_execution_context->visit_edges(visitor);
  52. visitor.visit(m_fetch_group);
  53. visitor.visit(m_storage_manager);
  54. }
  55. JS::ExecutionContext& EnvironmentSettingsObject::realm_execution_context()
  56. {
  57. // NOTE: All environment settings objects are created with a realm execution context, so it's stored and returned here in the base class.
  58. return *m_realm_execution_context;
  59. }
  60. JS::ExecutionContext const& EnvironmentSettingsObject::realm_execution_context() const
  61. {
  62. // NOTE: All environment settings objects are created with a realm execution context, so it's stored and returned here in the base class.
  63. return *m_realm_execution_context;
  64. }
  65. ModuleMap& EnvironmentSettingsObject::module_map()
  66. {
  67. return *m_module_map;
  68. }
  69. // https://html.spec.whatwg.org/multipage/webappapis.html#environment-settings-object%27s-realm
  70. JS::Realm& EnvironmentSettingsObject::realm()
  71. {
  72. // An environment settings object's realm execution context's Realm component is the environment settings object's Realm.
  73. return *realm_execution_context().realm;
  74. }
  75. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-settings-object-global
  76. JS::Object& EnvironmentSettingsObject::global_object()
  77. {
  78. // An environment settings object's Realm then has a [[GlobalObject]] field, which contains the environment settings object's global object.
  79. return realm().global_object();
  80. }
  81. // https://html.spec.whatwg.org/multipage/webappapis.html#responsible-event-loop
  82. EventLoop& EnvironmentSettingsObject::responsible_event_loop()
  83. {
  84. // An environment settings object's responsible event loop is its global object's relevant agent's event loop.
  85. // This is here in case the realm that is holding onto this ESO is destroyed before the ESO is. The responsible event loop pointer is needed in the ESO destructor to deregister from the event loop.
  86. // FIXME: Figure out why the realm can be destroyed before the ESO, as the realm is holding onto this with an OwnPtr, but the heap block deallocator calls the ESO destructor directly instead of through the realm destructor.
  87. if (m_responsible_event_loop)
  88. return *m_responsible_event_loop;
  89. auto& vm = global_object().vm();
  90. auto& event_loop = verify_cast<Bindings::WebEngineCustomData>(vm.custom_data())->event_loop;
  91. m_responsible_event_loop = event_loop;
  92. return *event_loop;
  93. }
  94. // https://html.spec.whatwg.org/multipage/webappapis.html#check-if-we-can-run-script
  95. // https://whatpr.org/html/9893/webappapis.html#check-if-we-can-run-script
  96. RunScriptDecision can_run_script(JS::Realm const& realm)
  97. {
  98. // 1. If the global object specified by realm is a Window object whose Document object is not fully active, then return "do not run".
  99. if (is<HTML::Window>(realm.global_object()) && !verify_cast<HTML::Window>(realm.global_object()).associated_document().is_fully_active())
  100. return RunScriptDecision::DoNotRun;
  101. // 2. If scripting is disabled for realm, then return "do not run".
  102. if (is_scripting_disabled(realm))
  103. return RunScriptDecision::DoNotRun;
  104. // 3. Return "run".
  105. return RunScriptDecision::Run;
  106. }
  107. // https://html.spec.whatwg.org/multipage/webappapis.html#prepare-to-run-script
  108. // https://whatpr.org/html/9893/b8ea975...df5706b/webappapis.html#prepare-to-run-script
  109. void prepare_to_run_script(JS::Realm& realm)
  110. {
  111. // 1. Push realms's execution context onto the JavaScript execution context stack; it is now the running JavaScript execution context.
  112. realm.global_object().vm().push_execution_context(execution_context_of_realm(realm));
  113. // FIXME: 2. If realm is a principal realm, then:
  114. // FIXME: 2.1 Let settings be realm's settings object.
  115. // FIXME: 2.2 Add settings to the currently running task's script evaluation environment settings object set.
  116. }
  117. // https://whatpr.org/html/9893/b8ea975...df5706b/webappapis.html#concept-realm-execution-context
  118. JS::ExecutionContext const& execution_context_of_realm(JS::Realm const& realm)
  119. {
  120. VERIFY(realm.host_defined());
  121. // 1. If realm is a principal realm, then return the realm execution context of the environment settings object of realm.
  122. if (is<Bindings::PrincipalHostDefined>(*realm.host_defined()))
  123. return static_cast<Bindings::PrincipalHostDefined const&>(*realm.host_defined()).environment_settings_object->realm_execution_context();
  124. // 2. Assert: realm is a synthetic realm.
  125. // 3. Return the execution context of the synthetic realm settings object of realm.
  126. return *verify_cast<Bindings::SyntheticHostDefined>(*realm.host_defined()).synthetic_realm_settings.execution_context;
  127. }
  128. // https://html.spec.whatwg.org/multipage/webappapis.html#clean-up-after-running-script
  129. // https://whatpr.org/html/9893/webappapis.html#clean-up-after-running-script
  130. void clean_up_after_running_script(JS::Realm const& realm)
  131. {
  132. auto& vm = realm.global_object().vm();
  133. // 1. Assert: realm's execution context is the running JavaScript execution context.
  134. VERIFY(&execution_context_of_realm(realm) == &vm.running_execution_context());
  135. // 2. Remove realm's execution context from the JavaScript execution context stack.
  136. vm.pop_execution_context();
  137. // 3. If the JavaScript execution context stack is now empty, perform a microtask checkpoint. (If this runs scripts, these algorithms will be invoked reentrantly.)
  138. if (vm.execution_context_stack().is_empty())
  139. main_thread_event_loop().perform_a_microtask_checkpoint();
  140. }
  141. static JS::ExecutionContext* top_most_script_having_execution_context(JS::VM& vm)
  142. {
  143. // Here, the topmost script-having execution context is the topmost entry of the JavaScript execution context stack that has a non-null ScriptOrModule component,
  144. // or null if there is no such entry in the JavaScript execution context stack.
  145. auto execution_context = vm.execution_context_stack().last_matching([&](JS::ExecutionContext* context) {
  146. return !context->script_or_module.has<Empty>();
  147. });
  148. if (!execution_context.has_value())
  149. return nullptr;
  150. return execution_context.value();
  151. }
  152. // https://html.spec.whatwg.org/multipage/webappapis.html#prepare-to-run-a-callback
  153. void prepare_to_run_callback(JS::Realm& realm)
  154. {
  155. auto& vm = realm.global_object().vm();
  156. // 1. Push realm onto the backup incumbent settings object stack.
  157. // NOTE: The spec doesn't say which event loop's stack to put this on. However, all the examples of the incumbent settings object use iframes and cross browsing context communication to demonstrate the concept.
  158. // This means that it must rely on some global state that can be accessed by all browsing contexts, which is the main thread event loop.
  159. HTML::main_thread_event_loop().push_onto_backup_incumbent_realm_stack(realm);
  160. // 2. Let context be the topmost script-having execution context.
  161. auto* context = top_most_script_having_execution_context(vm);
  162. // 3. If context is not null, increment context's skip-when-determining-incumbent counter.
  163. if (context)
  164. context->skip_when_determining_incumbent_counter++;
  165. }
  166. // https://html.spec.whatwg.org/multipage/urls-and-fetching.html#parse-a-url
  167. URL::URL EnvironmentSettingsObject::parse_url(StringView url)
  168. {
  169. // 1. Let baseURL be environment's base URL, if environment is a Document object; otherwise environment's API base URL.
  170. auto base_url = api_base_url();
  171. // 2. Return the result of applying the URL parser to url, with baseURL.
  172. return DOMURL::parse(url, base_url);
  173. }
  174. // https://html.spec.whatwg.org/multipage/urls-and-fetching.html#encoding-parsing-a-url
  175. URL::URL EnvironmentSettingsObject::encoding_parse_url(StringView url)
  176. {
  177. // 1. Let encoding be UTF-8.
  178. auto encoding = "UTF-8"_string;
  179. // 2. If environment is a Document object, then set encoding to environment's character encoding.
  180. // 3. Otherwise, if environment's relevant global object is a Window object, set encoding to environment's relevant
  181. // global object's associated Document's character encoding.
  182. if (is<HTML::Window>(global_object()))
  183. encoding = static_cast<HTML::Window const&>(global_object()).associated_document().encoding_or_default();
  184. // 4. Let baseURL be environment's base URL, if environment is a Document object; otherwise environment's API base URL.
  185. auto base_url = api_base_url();
  186. // 5. Return the result of applying the URL parser to url, with baseURL and encoding.
  187. return DOMURL::parse(url, base_url, encoding);
  188. }
  189. // https://html.spec.whatwg.org/multipage/urls-and-fetching.html#encoding-parsing-and-serializing-a-url
  190. Optional<String> EnvironmentSettingsObject::encoding_parse_and_serialize_url(StringView url)
  191. {
  192. // 1. Let url be the result of encoding-parsing a URL given url, relative to environment.
  193. auto parsed_url = encoding_parse_url(url);
  194. // 2. If url is failure, then return failure.
  195. if (!parsed_url.is_valid())
  196. return {};
  197. // 3. Return the result of applying the URL serializer to url.
  198. return parsed_url.serialize();
  199. }
  200. // https://html.spec.whatwg.org/multipage/webappapis.html#clean-up-after-running-a-callback
  201. // https://whatpr.org/html/9893/b8ea975...df5706b/webappapis.html#clean-up-after-running-a-callback
  202. void clean_up_after_running_callback(JS::Realm const& realm)
  203. {
  204. auto& vm = realm.global_object().vm();
  205. // 1. Let context be the topmost script-having execution context.
  206. auto* context = top_most_script_having_execution_context(vm);
  207. // 2. If context is not null, decrement context's skip-when-determining-incumbent counter.
  208. if (context)
  209. context->skip_when_determining_incumbent_counter--;
  210. // 3. Assert: the topmost entry of the backup incumbent realm stack is realm.
  211. auto& event_loop = HTML::main_thread_event_loop();
  212. VERIFY(&event_loop.top_of_backup_incumbent_realm_stack() == &realm);
  213. // 4. Remove realm from the backup incumbent realm stack.
  214. event_loop.pop_backup_incumbent_realm_stack();
  215. }
  216. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-script
  217. // https://whatpr.org/html/9893/webappapis.html#concept-environment-script
  218. bool is_scripting_enabled(JS::Realm const& realm)
  219. {
  220. // Scripting is enabled for a realm realm when all of the following conditions are true:
  221. // The user agent supports scripting.
  222. // NOTE: This is always true in LibWeb :^)
  223. // FIXME: Do the right thing for workers.
  224. if (!is<HTML::Window>(realm.global_object()))
  225. return true;
  226. // The user has not disabled scripting for realm at this time. (User agents may provide users with the option to disable scripting globally, or in a finer-grained manner, e.g., on a per-origin basis, down to the level of individual realms.)
  227. auto const& document = verify_cast<HTML::Window>(realm.global_object()).associated_document();
  228. if (!document.page().is_scripting_enabled())
  229. return false;
  230. // FIXME: Either settings's global object is not a Window object, or settings's global object's associated Document's active sandboxing flag set does not have its sandboxed scripts browsing context flag set.
  231. return true;
  232. }
  233. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-noscript
  234. // https://whatpr.org/html/9893/webappapis.html#concept-environment-noscript
  235. bool is_scripting_disabled(JS::Realm const& realm)
  236. {
  237. // Scripting is disabled for a realm when scripting is not enabled for it, i.e., when any of the above conditions are false.
  238. return !is_scripting_enabled(realm);
  239. }
  240. // https://html.spec.whatwg.org/multipage/webappapis.html#module-type-allowed
  241. // https://whatpr.org/html/9893/webappapis.html#module-type-allowed
  242. bool module_type_allowed(JS::Realm const&, StringView module_type)
  243. {
  244. // 1. If moduleType is not "javascript", "css", or "json", then return false.
  245. if (module_type != "javascript"sv && module_type != "css"sv && module_type != "json"sv)
  246. return false;
  247. // FIXME: 2. If moduleType is "css" and the CSSStyleSheet interface is not exposed in realm, then return false.
  248. // 3. Return true.
  249. return true;
  250. }
  251. // https://html.spec.whatwg.org/multipage/webappapis.html#add-module-to-resolved-module-set
  252. void add_module_to_resolved_module_set(JS::Realm& realm, String const& serialized_base_url, String const& normalized_specifier, Optional<URL::URL> const& as_url)
  253. {
  254. // 1. Let global be realm's global object.
  255. auto& global = realm.global_object();
  256. // 2. If global does not implement Window, then return.
  257. if (!is<Window>(global))
  258. return;
  259. // 3. Let record be a new specifier resolution record, with serialized base URL set to serializedBaseURL,
  260. // specifier set to normalizedSpecifier, and specifier as a URL set to asURL.
  261. //
  262. // NOTE: We set 'specifier as a URL set to asURL' as a bool to simplify logic when merging import maps.
  263. SpecifierResolution resolution {
  264. .serialized_base_url = serialized_base_url,
  265. .specifier = normalized_specifier,
  266. .specifier_is_null_or_url_like_that_is_special = !as_url.has_value() || as_url->is_special(),
  267. };
  268. // 4. Append record to global's resolved module set.
  269. return verify_cast<Window>(global).append_resolved_module(move(resolution));
  270. }
  271. // https://whatpr.org/html/9893/webappapis.html#concept-realm-module-map
  272. ModuleMap& module_map_of_realm(JS::Realm& realm)
  273. {
  274. VERIFY(realm.host_defined());
  275. // 1. If realm is a principal realm, then return the module map of the environment settings object of realm.
  276. if (is<Bindings::PrincipalHostDefined>(*realm.host_defined()))
  277. return static_cast<Bindings::PrincipalHostDefined const&>(*realm.host_defined()).environment_settings_object->module_map();
  278. // 2. Assert: realm is a synthetic realm.
  279. // 3. Return the module map of the synthetic realm settings object of realm.
  280. return *verify_cast<Bindings::SyntheticHostDefined>(*realm.host_defined()).synthetic_realm_settings.module_map;
  281. }
  282. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-incumbent-realm
  283. // https://whatpr.org/html/9893/b8ea975...df5706b/webappapis.html#concept-incumbent-realm
  284. JS::Realm& incumbent_realm()
  285. {
  286. auto& event_loop = HTML::main_thread_event_loop();
  287. auto& vm = event_loop.vm();
  288. // 1. Let context be the topmost script-having execution context.
  289. auto* context = top_most_script_having_execution_context(vm);
  290. // 2. If context is null, or if context's skip-when-determining-incumbent counter is greater than zero, then:
  291. if (!context || context->skip_when_determining_incumbent_counter > 0) {
  292. // 1. Assert: the backup incumbent settings object stack is not empty.
  293. // 1. Assert: the backup incumbent realm stack is not empty.
  294. // NOTE: If this assertion fails, it's because the incumbent realm was used with no involvement of JavaScript.
  295. VERIFY(!event_loop.is_backup_incumbent_realm_stack_empty());
  296. // 2. Return the topmost entry of the backup incumbent realm stack.
  297. return event_loop.top_of_backup_incumbent_realm_stack();
  298. }
  299. // 3. Return context's Realm component.
  300. return *context->realm;
  301. }
  302. // https://html.spec.whatwg.org/multipage/webappapis.html#incumbent-settings-object
  303. // https://whatpr.org/html/9893/b8ea975...df5706b/webappapis.html#incumbent-settings-object
  304. EnvironmentSettingsObject& incumbent_settings_object()
  305. {
  306. // Then, the incumbent settings object is the incumbent realm's principal realm settings object.
  307. return principal_realm_settings_object(principal_realm(incumbent_realm()));
  308. }
  309. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-incumbent-global
  310. JS::Object& incumbent_global_object()
  311. {
  312. // Similarly, the incumbent global object is the global object of the incumbent settings object.
  313. return incumbent_settings_object().global_object();
  314. }
  315. // https://whatpr.org/html/9893/webappapis.html#current-principal-realm
  316. JS::Realm& current_principal_realm()
  317. {
  318. auto& event_loop = HTML::main_thread_event_loop();
  319. auto& vm = event_loop.vm();
  320. // The current principal realm is the principal realm of the current realm.
  321. return principal_realm(*vm.current_realm());
  322. }
  323. // https://whatpr.org/html/9893/webappapis.html#concept-principal-realm-of-realm
  324. JS::Realm& principal_realm(GC::Ref<JS::Realm> realm)
  325. {
  326. VERIFY(realm->host_defined());
  327. // 1. If realm.[[HostDefined]] is a synthetic realm settings object, then:
  328. if (is<Bindings::SyntheticHostDefined>(*realm->host_defined())) {
  329. // 1. Assert: realm is a synthetic realm.
  330. // 2. Set realm to the principal realm of realm.[[HostDefined]].
  331. realm = static_cast<Bindings::SyntheticHostDefined const&>(*realm->host_defined()).synthetic_realm_settings.principal_realm;
  332. }
  333. // 2. Assert: realm.[[HostDefined]] is an environment settings object and realm is a principal realm.
  334. VERIFY(is<Bindings::PrincipalHostDefined>(*realm->host_defined()));
  335. // 3. Return realm.
  336. return realm;
  337. }
  338. // https://whatpr.org/html/9893/webappapis.html#concept-realm-settings-object
  339. EnvironmentSettingsObject& principal_realm_settings_object(JS::Realm& realm)
  340. {
  341. // A principal realm has a [[HostDefined]] field, which contains the principal realm's settings object.
  342. return Bindings::principal_host_defined_environment_settings_object(realm);
  343. }
  344. // https://html.spec.whatwg.org/multipage/webappapis.html#current-settings-object
  345. // https://whatpr.org/html/9893/webappapis.html#current-principal-settings-object
  346. EnvironmentSettingsObject& current_principal_settings_object()
  347. {
  348. // Then, the current principal settings object is the environment settings object of the current principal realm.
  349. return principal_realm_settings_object(current_principal_realm());
  350. }
  351. // https://html.spec.whatwg.org/multipage/webappapis.html#current-global-object
  352. // https://whatpr.org/html/9893/webappapis.html#current-principal-global-object
  353. JS::Object& current_principal_global_object()
  354. {
  355. // Similarly, the current principal global object is the global object of the current principal realm.
  356. return current_principal_realm().global_object();
  357. }
  358. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-relevant-realm
  359. JS::Realm& relevant_realm(JS::Object const& object)
  360. {
  361. // The relevant Realm for a platform object is the value of its [[Realm]] field.
  362. return object.shape().realm();
  363. }
  364. // https://whatpr.org/html/9893/webappapis.html#relevant-principal-realm
  365. JS::Realm& relevant_principal_realm(JS::Object const& object)
  366. {
  367. // The relevant principal realm for a platform object o is o's relevant realm's principal realm.
  368. return principal_realm(relevant_realm(object));
  369. }
  370. // https://html.spec.whatwg.org/multipage/webappapis.html#relevant-settings-object
  371. EnvironmentSettingsObject& relevant_settings_object(JS::Object const& object)
  372. {
  373. // Then, the relevant settings object for a platform object o is the environment settings object of the relevant Realm for o.
  374. return Bindings::principal_host_defined_environment_settings_object(relevant_realm(object));
  375. }
  376. EnvironmentSettingsObject& relevant_settings_object(DOM::Node const& node)
  377. {
  378. // Then, the relevant settings object for a platform object o is the environment settings object of the relevant Realm for o.
  379. return const_cast<DOM::Document&>(node.document()).relevant_settings_object();
  380. }
  381. // https://whatpr.org/html/9893/webappapis.html#relevant-principal-settings-object
  382. EnvironmentSettingsObject& relevant_principal_settings_object(JS::Object const& object)
  383. {
  384. // The relevant principal settings object for a platform object o is o's relevant principal realm's environment settings object.
  385. return Bindings::principal_host_defined_environment_settings_object(relevant_principal_realm(object));
  386. }
  387. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-relevant-global
  388. JS::Object& relevant_global_object(JS::Object const& object)
  389. {
  390. // Similarly, the relevant global object for a platform object o is the global object of the relevant Realm for o.
  391. return relevant_realm(object).global_object();
  392. }
  393. // https://whatpr.org/html/9893/webappapis.html#relevant-principal-global
  394. JS::Object& relevant_principal_global_object(JS::Object const& object)
  395. {
  396. // The relevant principal global object for a platform object o is o's relevant principal realm's global object.
  397. return relevant_principal_realm(object).global_object();
  398. }
  399. // https://html.spec.whatwg.org/multipage/webappapis.html#concept-entry-realm
  400. // https://whatpr.org/html/9893/webappapis.html#concept-entry-realm
  401. JS::Realm& entry_realm()
  402. {
  403. auto& event_loop = HTML::main_thread_event_loop();
  404. auto& vm = event_loop.vm();
  405. // With this in hand, we define the entry execution context to be the most recently pushed item in the JavaScript execution context stack that is a realm execution context.
  406. // The entry realm is the principal realm of the entry execution context's Realm component.
  407. // NOTE: Currently all execution contexts in LibJS are realm execution contexts
  408. return principal_realm(*vm.running_execution_context().realm);
  409. }
  410. // https://html.spec.whatwg.org/multipage/webappapis.html#entry-settings-object
  411. EnvironmentSettingsObject& entry_settings_object()
  412. {
  413. // Then, the entry settings object is the environment settings object of the entry realm.
  414. return Bindings::principal_host_defined_environment_settings_object(entry_realm());
  415. }
  416. // https://html.spec.whatwg.org/multipage/webappapis.html#entry-global-object
  417. JS::Object& entry_global_object()
  418. {
  419. // Similarly, the entry global object is the global object of the entry realm.
  420. return entry_realm().global_object();
  421. }
  422. JS::VM& relevant_agent(JS::Object const& object)
  423. {
  424. // The relevant agent for a platform object platformObject is platformObject's relevant Realm's agent.
  425. // Spec Note: This pointer is not yet defined in the JavaScript specification; see tc39/ecma262#1357.
  426. return relevant_realm(object).vm();
  427. }
  428. // https://html.spec.whatwg.org/multipage/webappapis.html#secure-context
  429. bool is_secure_context(Environment const& environment)
  430. {
  431. // 1. If environment is an environment settings object, then:
  432. if (is<EnvironmentSettingsObject>(environment)) {
  433. // 1. Let global be environment's global object.
  434. // FIXME: Add a const global_object() getter to ESO
  435. auto& global = static_cast<EnvironmentSettingsObject&>(const_cast<Environment&>(environment)).global_object();
  436. // 2. If global is a WorkerGlobalScope, then:
  437. if (is<WorkerGlobalScope>(global)) {
  438. // FIXME: 1. If global's owner set[0]'s relevant settings object is a secure context, then return true.
  439. // NOTE: We only need to check the 0th item since they will necessarily all be consistent.
  440. // 2. Return false.
  441. return false;
  442. }
  443. // FIXME: 3. If global is a WorkletGlobalScope, then return true.
  444. // NOTE: Worklets can only be created in secure contexts.
  445. }
  446. // 2. If the result of Is url potentially trustworthy? given environment's top-level creation URL is "Potentially Trustworthy", then return true.
  447. if (SecureContexts::is_url_potentially_trustworthy(environment.top_level_creation_url) == SecureContexts::Trustworthiness::PotentiallyTrustworthy)
  448. return true;
  449. // 3. Return false.
  450. return false;
  451. }
  452. // https://html.spec.whatwg.org/multipage/webappapis.html#non-secure-context
  453. bool is_non_secure_context(Environment const& environment)
  454. {
  455. // An environment is a non-secure context if it is not a secure context.
  456. return !is_secure_context(environment);
  457. }
  458. SerializedEnvironmentSettingsObject EnvironmentSettingsObject::serialize()
  459. {
  460. SerializedEnvironmentSettingsObject object;
  461. object.id = this->id;
  462. object.creation_url = this->creation_url;
  463. object.top_level_creation_url = this->top_level_creation_url;
  464. object.top_level_origin = this->top_level_origin;
  465. object.api_url_character_encoding = api_url_character_encoding();
  466. object.api_base_url = api_base_url();
  467. object.origin = origin();
  468. object.policy_container = policy_container();
  469. object.cross_origin_isolated_capability = cross_origin_isolated_capability();
  470. return object;
  471. }
  472. GC::Ref<StorageAPI::StorageManager> EnvironmentSettingsObject::storage_manager()
  473. {
  474. if (!m_storage_manager)
  475. m_storage_manager = realm().create<StorageAPI::StorageManager>(realm());
  476. return *m_storage_manager;
  477. }
  478. }