Location.cpp 27 KB

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