Location.cpp 27 KB

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