Location.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. auto& window = verify_cast<HTML::Window>(HTML::current_principal_global_object());
  93. // 1. If this's relevant Document is null, then return.
  94. auto const relevant_document = this->relevant_document();
  95. if (!relevant_document)
  96. return {};
  97. // FIXME: 2. Let url be the result of encoding-parsing a URL given the given value, relative to the entry settings object.
  98. auto href_url = window.associated_document().parse_url(new_href.to_byte_string());
  99. // 3. If url is failure, then throw a "SyntaxError" DOMException.
  100. if (!href_url.is_valid())
  101. return WebIDL::SyntaxError::create(realm, MUST(String::formatted("Invalid URL '{}'", new_href)));
  102. // 4. Location-object navigate this to url.
  103. TRY(navigate(href_url));
  104. return {};
  105. }
  106. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-location-origin
  107. WebIDL::ExceptionOr<String> Location::origin() const
  108. {
  109. // 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.
  110. auto const relevant_document = this->relevant_document();
  111. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  112. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  113. // 2. Return the serialization of this's url's origin.
  114. return url().origin().serialize();
  115. }
  116. // https://html.spec.whatwg.org/multipage/history.html#dom-location-protocol
  117. WebIDL::ExceptionOr<String> Location::protocol() const
  118. {
  119. auto& vm = this->vm();
  120. // 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.
  121. auto const relevant_document = this->relevant_document();
  122. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  123. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  124. // 2. Return this's url's scheme, followed by ":".
  125. return TRY_OR_THROW_OOM(vm, String::formatted("{}:", url().scheme()));
  126. }
  127. // https://html.spec.whatwg.org/multipage/history.html#dom-location-protocol
  128. WebIDL::ExceptionOr<void> Location::set_protocol(String const& value)
  129. {
  130. auto relevant_document = this->relevant_document();
  131. // 1. If this's relevant Document is null, then return.
  132. if (!relevant_document)
  133. return {};
  134. // 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.
  135. if (!relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  136. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  137. // 3. Let copyURL be a copy of this's url.
  138. auto copy_url = this->url();
  139. // 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.
  140. auto possible_failure = URL::Parser::basic_parse(value, {}, &copy_url, URL::Parser::State::SchemeStart);
  141. // 5. If possibleFailure is failure, then throw a "SyntaxError" DOMException.
  142. if (!possible_failure.is_valid())
  143. return WebIDL::SyntaxError::create(realm(), MUST(String::formatted("Failed to set protocol. '{}' is an invalid protocol", value)));
  144. // 6. if copyURL's scheme is not an HTTP(S) scheme, then terminate these steps.
  145. if (!(copy_url.scheme() == "http"sv || copy_url.scheme() == "https"sv))
  146. return {};
  147. // 7. Location-object navigate this to copyURL.
  148. TRY(navigate(copy_url));
  149. return {};
  150. }
  151. // https://html.spec.whatwg.org/multipage/history.html#dom-location-host
  152. WebIDL::ExceptionOr<String> Location::host() const
  153. {
  154. auto& vm = this->vm();
  155. // 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.
  156. auto const relevant_document = this->relevant_document();
  157. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  158. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  159. // 2. Let url be this's url.
  160. auto url = this->url();
  161. // 3. If url's host is null, return the empty string.
  162. if (!url.host().has_value())
  163. return String {};
  164. // 4. If url's port is null, return url's host, serialized.
  165. if (!url.port().has_value())
  166. return url.serialized_host();
  167. // 5. Return url's host, serialized, followed by ":" and url's port, serialized.
  168. return TRY_OR_THROW_OOM(vm, String::formatted("{}:{}", url.serialized_host(), *url.port()));
  169. }
  170. WebIDL::ExceptionOr<void> Location::set_host(String const&)
  171. {
  172. auto& vm = this->vm();
  173. return vm.throw_completion<JS::InternalError>(JS::ErrorType::NotImplemented, "Location.host setter");
  174. }
  175. // https://html.spec.whatwg.org/multipage/history.html#dom-location-hostname
  176. WebIDL::ExceptionOr<String> Location::hostname() const
  177. {
  178. // 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.
  179. auto const relevant_document = this->relevant_document();
  180. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  181. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  182. auto url = this->url();
  183. // 2. If this's url's host is null, return the empty string.
  184. if (!url.host().has_value())
  185. return String {};
  186. // 3. Return this's url's host, serialized.
  187. return url.serialized_host();
  188. }
  189. WebIDL::ExceptionOr<void> Location::set_hostname(String const&)
  190. {
  191. auto& vm = this->vm();
  192. return vm.throw_completion<JS::InternalError>(JS::ErrorType::NotImplemented, "Location.hostname setter");
  193. }
  194. // https://html.spec.whatwg.org/multipage/history.html#dom-location-port
  195. WebIDL::ExceptionOr<String> Location::port() const
  196. {
  197. // 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.
  198. auto const relevant_document = this->relevant_document();
  199. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  200. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  201. auto url = this->url();
  202. // 2. If this's url's port is null, return the empty string.
  203. if (!url.port().has_value())
  204. return String {};
  205. // 3. Return this's url's port, serialized.
  206. return String::number(*url.port());
  207. }
  208. WebIDL::ExceptionOr<void> Location::set_port(String const&)
  209. {
  210. auto& vm = this->vm();
  211. return vm.throw_completion<JS::InternalError>(JS::ErrorType::NotImplemented, "Location.port setter");
  212. }
  213. // https://html.spec.whatwg.org/multipage/history.html#dom-location-pathname
  214. WebIDL::ExceptionOr<String> Location::pathname() const
  215. {
  216. // 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.
  217. auto const relevant_document = this->relevant_document();
  218. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  219. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  220. // 2. Return the result of URL path serializing this Location object's url.
  221. return url().serialize_path();
  222. }
  223. WebIDL::ExceptionOr<void> Location::set_pathname(String const&)
  224. {
  225. auto& vm = this->vm();
  226. return vm.throw_completion<JS::InternalError>(JS::ErrorType::NotImplemented, "Location.pathname setter");
  227. }
  228. // https://html.spec.whatwg.org/multipage/history.html#dom-location-search
  229. WebIDL::ExceptionOr<String> Location::search() const
  230. {
  231. auto& vm = this->vm();
  232. // 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.
  233. auto const relevant_document = this->relevant_document();
  234. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  235. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  236. auto url = this->url();
  237. // 2. If this's url's query is either null or the empty string, return the empty string.
  238. if (!url.query().has_value() || url.query()->is_empty())
  239. return String {};
  240. // 3. Return "?", followed by this's url's query.
  241. return TRY_OR_THROW_OOM(vm, String::formatted("?{}", url.query()));
  242. }
  243. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-location-search
  244. WebIDL::ExceptionOr<void> Location::set_search(String const& value)
  245. {
  246. // The search setter steps are:
  247. auto const relevant_document = this->relevant_document();
  248. // 1. If this's relevant Document is null, then return.
  249. if (!relevant_document)
  250. return {};
  251. // 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.
  252. if (!relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  253. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  254. // 3. Let copyURL be a copy of this's url.
  255. auto copy_url = this->url();
  256. // 4. If the given value is the empty string, set copyURL's query to null.
  257. if (value.is_empty()) {
  258. copy_url.set_query({});
  259. }
  260. // 5. Otherwise, run these substeps:
  261. else {
  262. // 5.1. Let input be the given value with a single leading "?" removed, if any.
  263. auto value_as_string_view = value.bytes_as_string_view();
  264. auto input = value_as_string_view.substring_view(value_as_string_view.starts_with('?'));
  265. // 5.2. Set copyURL's query to the empty string.
  266. copy_url.set_query(String {});
  267. // 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.
  268. (void)URL::Parser::basic_parse(input, {}, &copy_url, URL::Parser::State::Query);
  269. }
  270. // 6. Location-object navigate this to copyURL.
  271. TRY(navigate(copy_url));
  272. return {};
  273. }
  274. // https://html.spec.whatwg.org/multipage/history.html#dom-location-hash
  275. WebIDL::ExceptionOr<String> Location::hash() const
  276. {
  277. auto& vm = this->vm();
  278. // 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.
  279. auto const relevant_document = this->relevant_document();
  280. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  281. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  282. auto url = this->url();
  283. // 2. If this's url's fragment is either null or the empty string, return the empty string.
  284. if (!url.fragment().has_value() || url.fragment()->is_empty())
  285. return String {};
  286. // 3. Return "#", followed by this's url's fragment.
  287. return TRY_OR_THROW_OOM(vm, String::formatted("#{}", *url.fragment()));
  288. }
  289. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-location-hash
  290. WebIDL::ExceptionOr<void> Location::set_hash(String const& value)
  291. {
  292. // The hash setter steps are:
  293. auto const relevant_document = this->relevant_document();
  294. // 1. If this's relevant Document is null, then return.
  295. if (!relevant_document)
  296. return {};
  297. // 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.
  298. if (!relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  299. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  300. // 3. Let copyURL be a copy of this's url.
  301. auto copy_url = this->url();
  302. // 4. Let input be the given value with a single leading "#" removed, if any.
  303. auto input = value.bytes_as_string_view().trim("#"sv, TrimMode::Left);
  304. // 5. Set copyURL's fragment to the empty string.
  305. copy_url.set_fragment(String {});
  306. // 6. Basic URL parse input, with copyURL as url and fragment state as state override.
  307. (void)URL::Parser::basic_parse(input, {}, &copy_url, URL::Parser::State::Fragment);
  308. // 7. If copyURL's fragment is this's url's fragment, then return.
  309. if (copy_url.fragment() == this->url().fragment())
  310. return {};
  311. // 8. Location-object navigate this to copyURL.
  312. TRY(navigate(copy_url));
  313. return {};
  314. }
  315. // https://html.spec.whatwg.org/multipage/history.html#dom-location-reload
  316. void Location::reload() const
  317. {
  318. // 1. Let document be this's relevant Document.
  319. auto document = relevant_document();
  320. // 2. If document is null, then return.
  321. if (!document)
  322. return;
  323. // FIXME: 3. If document's origin is not same origin-domain with the entry settings object's origin, then throw a "SecurityError" DOMException.
  324. // 4. Reload document's node navigable.
  325. document->navigable()->reload();
  326. }
  327. // https://html.spec.whatwg.org/multipage/history.html#dom-location-replace
  328. WebIDL::ExceptionOr<void> Location::replace(String const& url)
  329. {
  330. // 1. If this's relevant Document is null, then return.
  331. if (!relevant_document())
  332. return {};
  333. // 2. Parse url relative to the entry settings object. If that failed, throw a "SyntaxError" DOMException.
  334. auto replace_url = entry_settings_object().parse_url(url);
  335. if (!replace_url.is_valid())
  336. return WebIDL::SyntaxError::create(realm(), MUST(String::formatted("Invalid URL '{}'", url)));
  337. // 3. Location-object navigate this to the resulting URL record given "replace".
  338. TRY(navigate(replace_url, Bindings::NavigationHistoryBehavior::Replace));
  339. return {};
  340. }
  341. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-location-assign
  342. WebIDL::ExceptionOr<void> Location::assign(String const& url)
  343. {
  344. // 1. If this's relevant Document is null, then return.
  345. auto const relevant_document = this->relevant_document();
  346. if (!relevant_document)
  347. return {};
  348. // 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.
  349. if (!relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  350. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  351. // 3. Parse url relative to the entry settings object. If that failed, throw a "SyntaxError" DOMException.
  352. auto assign_url = entry_settings_object().parse_url(url);
  353. if (!assign_url.is_valid())
  354. return WebIDL::SyntaxError::create(realm(), MUST(String::formatted("Invalid URL '{}'", url)));
  355. // 4. Location-object navigate this to the resulting URL record.
  356. TRY(navigate(assign_url));
  357. return {};
  358. }
  359. // 7.10.5.1 [[GetPrototypeOf]] ( ), https://html.spec.whatwg.org/multipage/history.html#location-getprototypeof
  360. JS::ThrowCompletionOr<JS::Object*> Location::internal_get_prototype_of() const
  361. {
  362. // 1. If IsPlatformObjectSameOrigin(this) is true, then return ! OrdinaryGetPrototypeOf(this).
  363. if (HTML::is_platform_object_same_origin(*this))
  364. return MUST(JS::Object::internal_get_prototype_of());
  365. // 2. Return null.
  366. return nullptr;
  367. }
  368. // 7.10.5.2 [[SetPrototypeOf]] ( V ), https://html.spec.whatwg.org/multipage/history.html#location-setprototypeof
  369. JS::ThrowCompletionOr<bool> Location::internal_set_prototype_of(Object* prototype)
  370. {
  371. // 1. Return ! SetImmutablePrototype(this, V).
  372. return MUST(set_immutable_prototype(prototype));
  373. }
  374. // 7.10.5.3 [[IsExtensible]] ( ), https://html.spec.whatwg.org/multipage/history.html#location-isextensible
  375. JS::ThrowCompletionOr<bool> Location::internal_is_extensible() const
  376. {
  377. // 1. Return true.
  378. return true;
  379. }
  380. // 7.10.5.4 [[PreventExtensions]] ( ), https://html.spec.whatwg.org/multipage/history.html#location-preventextensions
  381. JS::ThrowCompletionOr<bool> Location::internal_prevent_extensions()
  382. {
  383. // 1. Return false.
  384. return false;
  385. }
  386. // 7.10.5.5 [[GetOwnProperty]] ( P ), https://html.spec.whatwg.org/multipage/history.html#location-getownproperty
  387. JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> Location::internal_get_own_property(JS::PropertyKey const& property_key) const
  388. {
  389. auto& vm = this->vm();
  390. // 1. If IsPlatformObjectSameOrigin(this) is true, then:
  391. if (HTML::is_platform_object_same_origin(*this)) {
  392. // 1. Let desc be OrdinaryGetOwnProperty(this, P).
  393. auto descriptor = MUST(Object::internal_get_own_property(property_key));
  394. // 2. If the value of the [[DefaultProperties]] internal slot of this contains P, then set desc.[[Configurable]] to true.
  395. auto property_key_value = property_key.is_symbol()
  396. ? JS::Value { property_key.as_symbol() }
  397. : JS::PrimitiveString::create(vm, property_key.to_string());
  398. if (m_default_properties.contains_slow(property_key_value))
  399. descriptor->configurable = true;
  400. // 3. Return desc.
  401. return descriptor;
  402. }
  403. // 2. Let property be CrossOriginGetOwnPropertyHelper(this, P).
  404. auto property = HTML::cross_origin_get_own_property_helper(const_cast<Location*>(this), property_key);
  405. // 3. If property is not undefined, then return property.
  406. if (property.has_value())
  407. return property;
  408. // 4. Return ? CrossOriginPropertyFallback(P).
  409. return TRY(HTML::cross_origin_property_fallback(vm, property_key));
  410. }
  411. // 7.10.5.6 [[DefineOwnProperty]] ( P, Desc ), https://html.spec.whatwg.org/multipage/history.html#location-defineownproperty
  412. JS::ThrowCompletionOr<bool> Location::internal_define_own_property(JS::PropertyKey const& property_key, JS::PropertyDescriptor const& descriptor, Optional<JS::PropertyDescriptor>* precomputed_get_own_property)
  413. {
  414. // 1. If IsPlatformObjectSameOrigin(this) is true, then:
  415. if (HTML::is_platform_object_same_origin(*this)) {
  416. // 1. If the value of the [[DefaultProperties]] internal slot of this contains P, then return false.
  417. // 2. Return ? OrdinaryDefineOwnProperty(this, P, Desc).
  418. return JS::Object::internal_define_own_property(property_key, descriptor, precomputed_get_own_property);
  419. }
  420. // 2. Throw a "SecurityError" DOMException.
  421. return throw_completion(WebIDL::SecurityError::create(realm(), MUST(String::formatted("Can't define property '{}' on cross-origin object", property_key))));
  422. }
  423. // 7.10.5.7 [[Get]] ( P, Receiver ), https://html.spec.whatwg.org/multipage/history.html#location-get
  424. JS::ThrowCompletionOr<JS::Value> Location::internal_get(JS::PropertyKey const& property_key, JS::Value receiver, JS::CacheablePropertyMetadata* cacheable_metadata, PropertyLookupPhase phase) const
  425. {
  426. auto& vm = this->vm();
  427. // 1. If IsPlatformObjectSameOrigin(this) is true, then return ? OrdinaryGet(this, P, Receiver).
  428. if (HTML::is_platform_object_same_origin(*this))
  429. return JS::Object::internal_get(property_key, receiver, cacheable_metadata, phase);
  430. // 2. Return ? CrossOriginGet(this, P, Receiver).
  431. return HTML::cross_origin_get(vm, static_cast<JS::Object const&>(*this), property_key, receiver);
  432. }
  433. // 7.10.5.8 [[Set]] ( P, V, Receiver ), https://html.spec.whatwg.org/multipage/history.html#location-set
  434. JS::ThrowCompletionOr<bool> Location::internal_set(JS::PropertyKey const& property_key, JS::Value value, JS::Value receiver, JS::CacheablePropertyMetadata* cacheable_metadata)
  435. {
  436. auto& vm = this->vm();
  437. // 1. If IsPlatformObjectSameOrigin(this) is true, then return ? OrdinarySet(this, P, V, Receiver).
  438. if (HTML::is_platform_object_same_origin(*this))
  439. return JS::Object::internal_set(property_key, value, receiver, cacheable_metadata);
  440. // 2. Return ? CrossOriginSet(this, P, V, Receiver).
  441. return HTML::cross_origin_set(vm, static_cast<JS::Object&>(*this), property_key, value, receiver);
  442. }
  443. // 7.10.5.9 [[Delete]] ( P ), https://html.spec.whatwg.org/multipage/history.html#location-delete
  444. JS::ThrowCompletionOr<bool> Location::internal_delete(JS::PropertyKey const& property_key)
  445. {
  446. // 1. If IsPlatformObjectSameOrigin(this) is true, then return ? OrdinaryDelete(this, P).
  447. if (HTML::is_platform_object_same_origin(*this))
  448. return JS::Object::internal_delete(property_key);
  449. // 2. Throw a "SecurityError" DOMException.
  450. return throw_completion(WebIDL::SecurityError::create(realm(), MUST(String::formatted("Can't delete property '{}' on cross-origin object", property_key))));
  451. }
  452. // 7.10.5.10 [[OwnPropertyKeys]] ( ), https://html.spec.whatwg.org/multipage/history.html#location-ownpropertykeys
  453. JS::ThrowCompletionOr<GC::MarkedVector<JS::Value>> Location::internal_own_property_keys() const
  454. {
  455. // 1. If IsPlatformObjectSameOrigin(this) is true, then return OrdinaryOwnPropertyKeys(this).
  456. if (HTML::is_platform_object_same_origin(*this))
  457. return JS::Object::internal_own_property_keys();
  458. // 2. Return CrossOriginOwnPropertyKeys(this).
  459. return HTML::cross_origin_own_property_keys(this);
  460. }
  461. }