Location.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. * Copyright (c) 2020-2023, Andreas Kling <andreas@ladybird.org>
  3. * Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/String.h>
  8. #include <AK/StringBuilder.h>
  9. #include <LibGC/MarkedVector.h>
  10. #include <LibJS/Runtime/Completion.h>
  11. #include <LibJS/Runtime/PropertyDescriptor.h>
  12. #include <LibJS/Runtime/PropertyKey.h>
  13. #include <LibURL/Parser.h>
  14. #include <LibWeb/Bindings/LocationPrototype.h>
  15. #include <LibWeb/DOM/Document.h>
  16. #include <LibWeb/HTML/CrossOrigin/AbstractOperations.h>
  17. #include <LibWeb/HTML/Location.h>
  18. #include <LibWeb/HTML/Navigation.h>
  19. #include <LibWeb/HTML/Window.h>
  20. #include <LibWeb/WebIDL/DOMException.h>
  21. namespace Web::HTML {
  22. GC_DEFINE_ALLOCATOR(Location);
  23. // https://html.spec.whatwg.org/multipage/history.html#the-location-interface
  24. Location::Location(JS::Realm& realm)
  25. : PlatformObject(realm, MayInterfereWithIndexedPropertyAccess::Yes)
  26. {
  27. }
  28. Location::~Location() = default;
  29. void Location::visit_edges(Cell::Visitor& visitor)
  30. {
  31. Base::visit_edges(visitor);
  32. visitor.visit(m_default_properties);
  33. }
  34. void Location::initialize(JS::Realm& realm)
  35. {
  36. Base::initialize(realm);
  37. WEB_SET_PROTOTYPE_FOR_INTERFACE(Location);
  38. // FIXME: Implement steps 2.-4.
  39. // 5. Set the value of the [[DefaultProperties]] internal slot of location to location.[[OwnPropertyKeys]]().
  40. // NOTE: In LibWeb this happens before the ESO is set up, so we must avoid location's custom [[OwnPropertyKeys]].
  41. m_default_properties.extend(MUST(Object::internal_own_property_keys()));
  42. }
  43. // https://html.spec.whatwg.org/multipage/history.html#relevant-document
  44. GC::Ptr<DOM::Document> Location::relevant_document() const
  45. {
  46. // A Location object has an associated relevant Document, which is this Location object's
  47. // relevant global object's browsing context's active document, if this Location object's
  48. // relevant global object's browsing context is non-null, and null otherwise.
  49. auto* browsing_context = verify_cast<HTML::Window>(HTML::relevant_global_object(*this)).browsing_context();
  50. return browsing_context ? browsing_context->active_document() : nullptr;
  51. }
  52. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#location-object-navigate
  53. WebIDL::ExceptionOr<void> Location::navigate(URL::URL url, Bindings::NavigationHistoryBehavior history_handling)
  54. {
  55. // 1. Let navigable be location's relevant global object's navigable.
  56. auto navigable = verify_cast<HTML::Window>(HTML::relevant_global_object(*this)).navigable();
  57. // 2. Let sourceDocument be the incumbent global object's associated Document.
  58. auto& source_document = verify_cast<HTML::Window>(incumbent_global_object()).associated_document();
  59. // 3. If location's relevant Document is not yet completely loaded, and the incumbent global object does not have transient activation, then set historyHandling to "replace".
  60. if (!relevant_document()->is_completely_loaded() && !verify_cast<HTML::Window>(incumbent_global_object()).has_transient_activation()) {
  61. history_handling = Bindings::NavigationHistoryBehavior::Replace;
  62. }
  63. // 4. Navigate navigable to url using sourceDocument, with exceptionsEnabled set to true and historyHandling set to historyHandling.
  64. TRY(navigable->navigate({ .url = url,
  65. .source_document = source_document,
  66. .exceptions_enabled = true,
  67. .history_handling = history_handling }));
  68. return {};
  69. }
  70. // https://html.spec.whatwg.org/multipage/history.html#concept-location-url
  71. URL::URL Location::url() const
  72. {
  73. // A Location object has an associated url, which is this Location object's relevant Document's URL,
  74. // if this Location object's relevant Document is non-null, and about:blank otherwise.
  75. auto const relevant_document = this->relevant_document();
  76. return relevant_document ? relevant_document->url() : "about:blank"sv;
  77. }
  78. // https://html.spec.whatwg.org/multipage/history.html#dom-location-href
  79. WebIDL::ExceptionOr<String> Location::href() const
  80. {
  81. // 1. If this's relevant Document is non-null and its origin is not same origin-domain with the entry settings object's origin, then throw a "SecurityError" DOMException.
  82. auto const relevant_document = this->relevant_document();
  83. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  84. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  85. // 2. Return this's url, serialized.
  86. return url().serialize();
  87. }
  88. // https://html.spec.whatwg.org/multipage/history.html#the-location-interface:dom-location-href-2
  89. WebIDL::ExceptionOr<void> Location::set_href(String const& new_href)
  90. {
  91. auto& realm = this->realm();
  92. // 1. If this's relevant Document is null, then return.
  93. auto const relevant_document = this->relevant_document();
  94. if (!relevant_document)
  95. return {};
  96. // 2. Let url be the result of encoding-parsing a URL given the given value, relative to the entry settings object.
  97. auto url = entry_settings_object().encoding_parse_url(new_href.to_byte_string());
  98. // 3. If url is failure, then throw a "SyntaxError" DOMException.
  99. if (!url.is_valid())
  100. return WebIDL::SyntaxError::create(realm, MUST(String::formatted("Invalid URL '{}'", new_href)));
  101. // 4. Location-object navigate this to url.
  102. TRY(navigate(url));
  103. return {};
  104. }
  105. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-location-origin
  106. WebIDL::ExceptionOr<String> Location::origin() const
  107. {
  108. // 1. If this's relevant Document is non-null and its origin is not same origin-domain with the entry settings object's origin, then throw a "SecurityError" DOMException.
  109. auto const relevant_document = this->relevant_document();
  110. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  111. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  112. // 2. Return the serialization of this's url's origin.
  113. return url().origin().serialize();
  114. }
  115. // https://html.spec.whatwg.org/multipage/history.html#dom-location-protocol
  116. WebIDL::ExceptionOr<String> Location::protocol() const
  117. {
  118. auto& vm = this->vm();
  119. // 1. If this's relevant Document is non-null and its origin is not same origin-domain with the entry settings object's origin, then throw a "SecurityError" DOMException.
  120. auto const relevant_document = this->relevant_document();
  121. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  122. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  123. // 2. Return this's url's scheme, followed by ":".
  124. return TRY_OR_THROW_OOM(vm, String::formatted("{}:", url().scheme()));
  125. }
  126. // https://html.spec.whatwg.org/multipage/history.html#dom-location-protocol
  127. WebIDL::ExceptionOr<void> Location::set_protocol(String const& value)
  128. {
  129. auto relevant_document = this->relevant_document();
  130. // 1. If this's relevant Document is null, then return.
  131. if (!relevant_document)
  132. return {};
  133. // 2. If this's relevant Document's origin is not same origin-domain with the entry settings object's origin, then throw a "SecurityError" DOMException.
  134. if (!relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  135. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  136. // 3. Let copyURL be a copy of this's url.
  137. auto copy_url = this->url();
  138. // 4. Let possibleFailure be the result of basic URL parsing the given value, followed by ":", with copyURL as url and scheme start state as state override.
  139. auto possible_failure = URL::Parser::basic_parse(value, {}, &copy_url, URL::Parser::State::SchemeStart);
  140. // 5. If possibleFailure is failure, then throw a "SyntaxError" DOMException.
  141. if (!possible_failure.is_valid())
  142. return WebIDL::SyntaxError::create(realm(), MUST(String::formatted("Failed to set protocol. '{}' is an invalid protocol", value)));
  143. // 6. if copyURL's scheme is not an HTTP(S) scheme, then terminate these steps.
  144. if (!(copy_url.scheme() == "http"sv || copy_url.scheme() == "https"sv))
  145. return {};
  146. // 7. Location-object navigate this to copyURL.
  147. TRY(navigate(copy_url));
  148. return {};
  149. }
  150. // https://html.spec.whatwg.org/multipage/history.html#dom-location-host
  151. WebIDL::ExceptionOr<String> Location::host() const
  152. {
  153. auto& vm = this->vm();
  154. // 1. If this's relevant Document is non-null and its origin is not same origin-domain with the entry settings object's origin, then throw a "SecurityError" DOMException.
  155. auto const relevant_document = this->relevant_document();
  156. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  157. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  158. // 2. Let url be this's url.
  159. auto url = this->url();
  160. // 3. If url's host is null, return the empty string.
  161. if (!url.host().has_value())
  162. return String {};
  163. // 4. If url's port is null, return url's host, serialized.
  164. if (!url.port().has_value())
  165. return url.serialized_host();
  166. // 5. Return url's host, serialized, followed by ":" and url's port, serialized.
  167. return TRY_OR_THROW_OOM(vm, String::formatted("{}:{}", url.serialized_host(), *url.port()));
  168. }
  169. WebIDL::ExceptionOr<void> Location::set_host(String const&)
  170. {
  171. auto& vm = this->vm();
  172. return vm.throw_completion<JS::InternalError>(JS::ErrorType::NotImplemented, "Location.host setter");
  173. }
  174. // https://html.spec.whatwg.org/multipage/history.html#dom-location-hostname
  175. WebIDL::ExceptionOr<String> Location::hostname() const
  176. {
  177. // 1. If this's relevant Document is non-null and its origin is not same origin-domain with the entry settings object's origin, then throw a "SecurityError" DOMException.
  178. auto const relevant_document = this->relevant_document();
  179. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  180. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  181. auto url = this->url();
  182. // 2. If this's url's host is null, return the empty string.
  183. if (!url.host().has_value())
  184. return String {};
  185. // 3. Return this's url's host, serialized.
  186. return url.serialized_host();
  187. }
  188. WebIDL::ExceptionOr<void> Location::set_hostname(String const&)
  189. {
  190. auto& vm = this->vm();
  191. return vm.throw_completion<JS::InternalError>(JS::ErrorType::NotImplemented, "Location.hostname setter");
  192. }
  193. // https://html.spec.whatwg.org/multipage/history.html#dom-location-port
  194. WebIDL::ExceptionOr<String> Location::port() const
  195. {
  196. // 1. If this's relevant Document is non-null and its origin is not same origin-domain with the entry settings object's origin, then throw a "SecurityError" DOMException.
  197. auto const relevant_document = this->relevant_document();
  198. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  199. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  200. auto url = this->url();
  201. // 2. If this's url's port is null, return the empty string.
  202. if (!url.port().has_value())
  203. return String {};
  204. // 3. Return this's url's port, serialized.
  205. return String::number(*url.port());
  206. }
  207. WebIDL::ExceptionOr<void> Location::set_port(String const&)
  208. {
  209. auto& vm = this->vm();
  210. return vm.throw_completion<JS::InternalError>(JS::ErrorType::NotImplemented, "Location.port setter");
  211. }
  212. // https://html.spec.whatwg.org/multipage/history.html#dom-location-pathname
  213. WebIDL::ExceptionOr<String> Location::pathname() const
  214. {
  215. // 1. If this's relevant Document is non-null and its origin is not same origin-domain with the entry settings object's origin, then throw a "SecurityError" DOMException.
  216. auto const relevant_document = this->relevant_document();
  217. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  218. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  219. // 2. Return the result of URL path serializing this Location object's url.
  220. return url().serialize_path();
  221. }
  222. WebIDL::ExceptionOr<void> Location::set_pathname(String const&)
  223. {
  224. auto& vm = this->vm();
  225. return vm.throw_completion<JS::InternalError>(JS::ErrorType::NotImplemented, "Location.pathname setter");
  226. }
  227. // https://html.spec.whatwg.org/multipage/history.html#dom-location-search
  228. WebIDL::ExceptionOr<String> Location::search() const
  229. {
  230. auto& vm = this->vm();
  231. // 1. If this's relevant Document is non-null and its origin is not same origin-domain with the entry settings object's origin, then throw a "SecurityError" DOMException.
  232. auto const relevant_document = this->relevant_document();
  233. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  234. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  235. auto url = this->url();
  236. // 2. If this's url's query is either null or the empty string, return the empty string.
  237. if (!url.query().has_value() || url.query()->is_empty())
  238. return String {};
  239. // 3. Return "?", followed by this's url's query.
  240. return TRY_OR_THROW_OOM(vm, String::formatted("?{}", url.query()));
  241. }
  242. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-location-search
  243. WebIDL::ExceptionOr<void> Location::set_search(String const& value)
  244. {
  245. // The search setter steps are:
  246. auto const relevant_document = this->relevant_document();
  247. // 1. If this's relevant Document is null, then return.
  248. if (!relevant_document)
  249. return {};
  250. // 2. If this's relevant Document's origin is not same origin-domain with the entry settings object's origin, then throw a "SecurityError" DOMException.
  251. if (!relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  252. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  253. // 3. Let copyURL be a copy of this's url.
  254. auto copy_url = this->url();
  255. // 4. If the given value is the empty string, set copyURL's query to null.
  256. if (value.is_empty()) {
  257. copy_url.set_query({});
  258. }
  259. // 5. Otherwise, run these substeps:
  260. else {
  261. // 5.1. Let input be the given value with a single leading "?" removed, if any.
  262. auto value_as_string_view = value.bytes_as_string_view();
  263. auto input = value_as_string_view.substring_view(value_as_string_view.starts_with('?'));
  264. // 5.2. Set copyURL's query to the empty string.
  265. copy_url.set_query(String {});
  266. // 5.3. Basic URL parse input, with null, the relevant Document's document's character encoding, copyURL as url, and query state as state override.
  267. (void)URL::Parser::basic_parse(input, {}, &copy_url, URL::Parser::State::Query);
  268. }
  269. // 6. Location-object navigate this to copyURL.
  270. TRY(navigate(copy_url));
  271. return {};
  272. }
  273. // https://html.spec.whatwg.org/multipage/history.html#dom-location-hash
  274. WebIDL::ExceptionOr<String> Location::hash() const
  275. {
  276. auto& vm = this->vm();
  277. // 1. If this's relevant Document is non-null and its origin is not same origin-domain with the entry settings object's origin, then throw a "SecurityError" DOMException.
  278. auto const relevant_document = this->relevant_document();
  279. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  280. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  281. auto url = this->url();
  282. // 2. If this's url's fragment is either null or the empty string, return the empty string.
  283. if (!url.fragment().has_value() || url.fragment()->is_empty())
  284. return String {};
  285. // 3. Return "#", followed by this's url's fragment.
  286. return TRY_OR_THROW_OOM(vm, String::formatted("#{}", *url.fragment()));
  287. }
  288. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-location-hash
  289. WebIDL::ExceptionOr<void> Location::set_hash(String const& value)
  290. {
  291. // The hash setter steps are:
  292. auto const relevant_document = this->relevant_document();
  293. // 1. If this's relevant Document is null, then return.
  294. if (!relevant_document)
  295. return {};
  296. // 2. If this's relevant Document's origin is not same origin-domain with the entry settings object's origin, then throw a "SecurityError" DOMException.
  297. if (!relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  298. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  299. // 3. Let copyURL be a copy of this's url.
  300. auto copy_url = this->url();
  301. // 4. Let input be the given value with a single leading "#" removed, if any.
  302. auto input = value.bytes_as_string_view().trim("#"sv, TrimMode::Left);
  303. // 5. Set copyURL's fragment to the empty string.
  304. copy_url.set_fragment(String {});
  305. // 6. Basic URL parse input, with copyURL as url and fragment state as state override.
  306. (void)URL::Parser::basic_parse(input, {}, &copy_url, URL::Parser::State::Fragment);
  307. // 7. If copyURL's fragment is this's url's fragment, then return.
  308. if (copy_url.fragment() == this->url().fragment())
  309. return {};
  310. // 8. Location-object navigate this to copyURL.
  311. TRY(navigate(copy_url));
  312. return {};
  313. }
  314. // https://html.spec.whatwg.org/multipage/history.html#dom-location-reload
  315. void Location::reload() const
  316. {
  317. // 1. Let document be this's relevant Document.
  318. auto document = relevant_document();
  319. // 2. If document is null, then return.
  320. if (!document)
  321. return;
  322. // FIXME: 3. If document's origin is not same origin-domain with the entry settings object's origin, then throw a "SecurityError" DOMException.
  323. // 4. Reload document's node navigable.
  324. document->navigable()->reload();
  325. }
  326. // https://html.spec.whatwg.org/multipage/history.html#dom-location-replace
  327. WebIDL::ExceptionOr<void> Location::replace(String const& url)
  328. {
  329. // 1. If this's relevant Document is null, then return.
  330. if (!relevant_document())
  331. return {};
  332. // 2. Parse url relative to the entry settings object. If that failed, throw a "SyntaxError" DOMException.
  333. auto replace_url = entry_settings_object().parse_url(url);
  334. if (!replace_url.is_valid())
  335. return WebIDL::SyntaxError::create(realm(), MUST(String::formatted("Invalid URL '{}'", url)));
  336. // 3. Location-object navigate this to the resulting URL record given "replace".
  337. TRY(navigate(replace_url, Bindings::NavigationHistoryBehavior::Replace));
  338. return {};
  339. }
  340. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-location-assign
  341. WebIDL::ExceptionOr<void> Location::assign(String const& url)
  342. {
  343. // 1. If this's relevant Document is null, then return.
  344. auto const relevant_document = this->relevant_document();
  345. if (!relevant_document)
  346. return {};
  347. // 2. If this's relevant Document's origin is not same origin-domain with the entry settings object's origin, then throw a "SecurityError" DOMException.
  348. if (!relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  349. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  350. // 3. Parse url relative to the entry settings object. If that failed, throw a "SyntaxError" DOMException.
  351. auto assign_url = entry_settings_object().parse_url(url);
  352. if (!assign_url.is_valid())
  353. return WebIDL::SyntaxError::create(realm(), MUST(String::formatted("Invalid URL '{}'", url)));
  354. // 4. Location-object navigate this to the resulting URL record.
  355. TRY(navigate(assign_url));
  356. return {};
  357. }
  358. // 7.10.5.1 [[GetPrototypeOf]] ( ), https://html.spec.whatwg.org/multipage/history.html#location-getprototypeof
  359. JS::ThrowCompletionOr<JS::Object*> Location::internal_get_prototype_of() const
  360. {
  361. // 1. If IsPlatformObjectSameOrigin(this) is true, then return ! OrdinaryGetPrototypeOf(this).
  362. if (HTML::is_platform_object_same_origin(*this))
  363. return MUST(JS::Object::internal_get_prototype_of());
  364. // 2. Return null.
  365. return nullptr;
  366. }
  367. // 7.10.5.2 [[SetPrototypeOf]] ( V ), https://html.spec.whatwg.org/multipage/history.html#location-setprototypeof
  368. JS::ThrowCompletionOr<bool> Location::internal_set_prototype_of(Object* prototype)
  369. {
  370. // 1. Return ! SetImmutablePrototype(this, V).
  371. return MUST(set_immutable_prototype(prototype));
  372. }
  373. // 7.10.5.3 [[IsExtensible]] ( ), https://html.spec.whatwg.org/multipage/history.html#location-isextensible
  374. JS::ThrowCompletionOr<bool> Location::internal_is_extensible() const
  375. {
  376. // 1. Return true.
  377. return true;
  378. }
  379. // 7.10.5.4 [[PreventExtensions]] ( ), https://html.spec.whatwg.org/multipage/history.html#location-preventextensions
  380. JS::ThrowCompletionOr<bool> Location::internal_prevent_extensions()
  381. {
  382. // 1. Return false.
  383. return false;
  384. }
  385. // 7.10.5.5 [[GetOwnProperty]] ( P ), https://html.spec.whatwg.org/multipage/history.html#location-getownproperty
  386. JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> Location::internal_get_own_property(JS::PropertyKey const& property_key) const
  387. {
  388. auto& vm = this->vm();
  389. // 1. If IsPlatformObjectSameOrigin(this) is true, then:
  390. if (HTML::is_platform_object_same_origin(*this)) {
  391. // 1. Let desc be OrdinaryGetOwnProperty(this, P).
  392. auto descriptor = MUST(Object::internal_get_own_property(property_key));
  393. // 2. If the value of the [[DefaultProperties]] internal slot of this contains P, then set desc.[[Configurable]] to true.
  394. auto property_key_value = property_key.is_symbol()
  395. ? JS::Value { property_key.as_symbol() }
  396. : JS::PrimitiveString::create(vm, property_key.to_string());
  397. if (m_default_properties.contains_slow(property_key_value))
  398. descriptor->configurable = true;
  399. // 3. Return desc.
  400. return descriptor;
  401. }
  402. // 2. Let property be CrossOriginGetOwnPropertyHelper(this, P).
  403. auto property = HTML::cross_origin_get_own_property_helper(const_cast<Location*>(this), property_key);
  404. // 3. If property is not undefined, then return property.
  405. if (property.has_value())
  406. return property;
  407. // 4. Return ? CrossOriginPropertyFallback(P).
  408. return TRY(HTML::cross_origin_property_fallback(vm, property_key));
  409. }
  410. // 7.10.5.6 [[DefineOwnProperty]] ( P, Desc ), https://html.spec.whatwg.org/multipage/history.html#location-defineownproperty
  411. JS::ThrowCompletionOr<bool> Location::internal_define_own_property(JS::PropertyKey const& property_key, JS::PropertyDescriptor const& descriptor, Optional<JS::PropertyDescriptor>* precomputed_get_own_property)
  412. {
  413. // 1. If IsPlatformObjectSameOrigin(this) is true, then:
  414. if (HTML::is_platform_object_same_origin(*this)) {
  415. // 1. If the value of the [[DefaultProperties]] internal slot of this contains P, then return false.
  416. // 2. Return ? OrdinaryDefineOwnProperty(this, P, Desc).
  417. return JS::Object::internal_define_own_property(property_key, descriptor, precomputed_get_own_property);
  418. }
  419. // 2. Throw a "SecurityError" DOMException.
  420. return throw_completion(WebIDL::SecurityError::create(realm(), MUST(String::formatted("Can't define property '{}' on cross-origin object", property_key))));
  421. }
  422. // 7.10.5.7 [[Get]] ( P, Receiver ), https://html.spec.whatwg.org/multipage/history.html#location-get
  423. JS::ThrowCompletionOr<JS::Value> Location::internal_get(JS::PropertyKey const& property_key, JS::Value receiver, JS::CacheablePropertyMetadata* cacheable_metadata, PropertyLookupPhase phase) const
  424. {
  425. auto& vm = this->vm();
  426. // 1. If IsPlatformObjectSameOrigin(this) is true, then return ? OrdinaryGet(this, P, Receiver).
  427. if (HTML::is_platform_object_same_origin(*this))
  428. return JS::Object::internal_get(property_key, receiver, cacheable_metadata, phase);
  429. // 2. Return ? CrossOriginGet(this, P, Receiver).
  430. return HTML::cross_origin_get(vm, static_cast<JS::Object const&>(*this), property_key, receiver);
  431. }
  432. // 7.10.5.8 [[Set]] ( P, V, Receiver ), https://html.spec.whatwg.org/multipage/history.html#location-set
  433. JS::ThrowCompletionOr<bool> Location::internal_set(JS::PropertyKey const& property_key, JS::Value value, JS::Value receiver, JS::CacheablePropertyMetadata* cacheable_metadata)
  434. {
  435. auto& vm = this->vm();
  436. // 1. If IsPlatformObjectSameOrigin(this) is true, then return ? OrdinarySet(this, P, V, Receiver).
  437. if (HTML::is_platform_object_same_origin(*this))
  438. return JS::Object::internal_set(property_key, value, receiver, cacheable_metadata);
  439. // 2. Return ? CrossOriginSet(this, P, V, Receiver).
  440. return HTML::cross_origin_set(vm, static_cast<JS::Object&>(*this), property_key, value, receiver);
  441. }
  442. // 7.10.5.9 [[Delete]] ( P ), https://html.spec.whatwg.org/multipage/history.html#location-delete
  443. JS::ThrowCompletionOr<bool> Location::internal_delete(JS::PropertyKey const& property_key)
  444. {
  445. // 1. If IsPlatformObjectSameOrigin(this) is true, then return ? OrdinaryDelete(this, P).
  446. if (HTML::is_platform_object_same_origin(*this))
  447. return JS::Object::internal_delete(property_key);
  448. // 2. Throw a "SecurityError" DOMException.
  449. return throw_completion(WebIDL::SecurityError::create(realm(), MUST(String::formatted("Can't delete property '{}' on cross-origin object", property_key))));
  450. }
  451. // 7.10.5.10 [[OwnPropertyKeys]] ( ), https://html.spec.whatwg.org/multipage/history.html#location-ownpropertykeys
  452. JS::ThrowCompletionOr<GC::MarkedVector<JS::Value>> Location::internal_own_property_keys() const
  453. {
  454. // 1. If IsPlatformObjectSameOrigin(this) is true, then return OrdinaryOwnPropertyKeys(this).
  455. if (HTML::is_platform_object_same_origin(*this))
  456. return JS::Object::internal_own_property_keys();
  457. // 2. Return CrossOriginOwnPropertyKeys(this).
  458. return HTML::cross_origin_own_property_keys(this);
  459. }
  460. }