Location.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /*
  2. * Copyright (c) 2020-2023, Andreas Kling <kling@serenityos.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. for (auto& property : m_default_properties)
  33. visitor.visit(property);
  34. }
  35. void Location::initialize(JS::Realm& realm)
  36. {
  37. Base::initialize(realm);
  38. WEB_SET_PROTOTYPE_FOR_INTERFACE(Location);
  39. // FIXME: Implement steps 2.-4.
  40. // 5. Set the value of the [[DefaultProperties]] internal slot of location to location.[[OwnPropertyKeys]]().
  41. // NOTE: In LibWeb this happens before the ESO is set up, so we must avoid location's custom [[OwnPropertyKeys]].
  42. m_default_properties.extend(MUST(Object::internal_own_property_keys()));
  43. }
  44. // https://html.spec.whatwg.org/multipage/history.html#relevant-document
  45. JS::GCPtr<DOM::Document> Location::relevant_document() const
  46. {
  47. // A Location object has an associated relevant Document, which is this Location object's
  48. // relevant global object's browsing context's active document, if this Location object's
  49. // relevant global object's browsing context is non-null, and null otherwise.
  50. auto* browsing_context = verify_cast<HTML::Window>(HTML::relevant_global_object(*this)).browsing_context();
  51. return browsing_context ? browsing_context->active_document() : nullptr;
  52. }
  53. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#location-object-navigate
  54. WebIDL::ExceptionOr<void> Location::navigate(URL::URL url, Bindings::NavigationHistoryBehavior history_handling)
  55. {
  56. // 1. Let navigable be location's relevant global object's navigable.
  57. auto navigable = verify_cast<HTML::Window>(HTML::relevant_global_object(*this)).navigable();
  58. // 2. Let sourceDocument be the incumbent global object's associated Document.
  59. auto& source_document = verify_cast<HTML::Window>(incumbent_global_object()).associated_document();
  60. // 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".
  61. if (!relevant_document()->is_completely_loaded() && !verify_cast<HTML::Window>(incumbent_global_object()).has_transient_activation()) {
  62. history_handling = Bindings::NavigationHistoryBehavior::Replace;
  63. }
  64. // 4. Navigate navigable to url using sourceDocument, with exceptionsEnabled set to true and historyHandling set to historyHandling.
  65. TRY(navigable->navigate({ .url = url,
  66. .source_document = source_document,
  67. .exceptions_enabled = true,
  68. .history_handling = history_handling }));
  69. return {};
  70. }
  71. // https://html.spec.whatwg.org/multipage/history.html#concept-location-url
  72. URL::URL Location::url() const
  73. {
  74. // A Location object has an associated url, which is this Location object's relevant Document's URL,
  75. // if this Location object's relevant Document is non-null, and about:blank otherwise.
  76. auto const relevant_document = this->relevant_document();
  77. return relevant_document ? relevant_document->url() : "about:blank"sv;
  78. }
  79. // https://html.spec.whatwg.org/multipage/history.html#dom-location-href
  80. WebIDL::ExceptionOr<String> Location::href() const
  81. {
  82. auto& vm = this->vm();
  83. // 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.
  84. auto const relevant_document = this->relevant_document();
  85. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  86. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_fly_string);
  87. // 2. Return this's url, serialized.
  88. return TRY_OR_THROW_OOM(vm, String::from_byte_string(url().serialize()));
  89. }
  90. // https://html.spec.whatwg.org/multipage/history.html#the-location-interface:dom-location-href-2
  91. WebIDL::ExceptionOr<void> Location::set_href(String const& new_href)
  92. {
  93. auto& vm = this->vm();
  94. auto& window = verify_cast<HTML::Window>(HTML::current_global_object());
  95. // 1. If this's relevant Document is null, then return.
  96. auto const relevant_document = this->relevant_document();
  97. if (!relevant_document)
  98. return {};
  99. // 2. Parse the given value relative to the entry settings object. If that failed, throw a TypeError exception.
  100. auto href_url = window.associated_document().parse_url(new_href.to_byte_string());
  101. if (!href_url.is_valid())
  102. return vm.throw_completion<JS::URIError>(TRY_OR_THROW_OOM(vm, String::formatted("Invalid URL '{}'", new_href)));
  103. // 3. Location-object navigate given the resulting URL record.
  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().serialize_origin()));
  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. WebIDL::ExceptionOr<void> Location::set_protocol(String const&)
  130. {
  131. auto& vm = this->vm();
  132. return vm.throw_completion<JS::InternalError>(JS::ErrorType::NotImplemented, "Location.protocol setter");
  133. }
  134. // https://html.spec.whatwg.org/multipage/history.html#dom-location-host
  135. WebIDL::ExceptionOr<String> Location::host() const
  136. {
  137. auto& vm = this->vm();
  138. // 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.
  139. auto const relevant_document = this->relevant_document();
  140. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  141. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_fly_string);
  142. // 2. Let url be this's url.
  143. auto url = this->url();
  144. // 3. If url's host is null, return the empty string.
  145. if (url.host().has<Empty>())
  146. return String {};
  147. // 4. If url's port is null, return url's host, serialized.
  148. if (!url.port().has_value())
  149. return TRY_OR_THROW_OOM(vm, url.serialized_host());
  150. // 5. Return url's host, serialized, followed by ":" and url's port, serialized.
  151. return TRY_OR_THROW_OOM(vm, String::formatted("{}:{}", TRY_OR_THROW_OOM(vm, url.serialized_host()), *url.port()));
  152. }
  153. WebIDL::ExceptionOr<void> Location::set_host(String const&)
  154. {
  155. auto& vm = this->vm();
  156. return vm.throw_completion<JS::InternalError>(JS::ErrorType::NotImplemented, "Location.host setter");
  157. }
  158. // https://html.spec.whatwg.org/multipage/history.html#dom-location-hostname
  159. WebIDL::ExceptionOr<String> Location::hostname() const
  160. {
  161. auto& vm = this->vm();
  162. // 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.
  163. auto const relevant_document = this->relevant_document();
  164. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  165. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_fly_string);
  166. auto url = this->url();
  167. // 2. If this's url's host is null, return the empty string.
  168. if (url.host().has<Empty>())
  169. return String {};
  170. // 3. Return this's url's host, serialized.
  171. return TRY_OR_THROW_OOM(vm, url.serialized_host());
  172. }
  173. WebIDL::ExceptionOr<void> Location::set_hostname(String const&)
  174. {
  175. auto& vm = this->vm();
  176. return vm.throw_completion<JS::InternalError>(JS::ErrorType::NotImplemented, "Location.hostname setter");
  177. }
  178. // https://html.spec.whatwg.org/multipage/history.html#dom-location-port
  179. WebIDL::ExceptionOr<String> Location::port() const
  180. {
  181. auto& vm = this->vm();
  182. // 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.
  183. auto const relevant_document = this->relevant_document();
  184. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  185. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_fly_string);
  186. auto url = this->url();
  187. // 2. If this's url's port is null, return the empty string.
  188. if (!url.port().has_value())
  189. return String {};
  190. // 3. Return this's url's port, serialized.
  191. return TRY_OR_THROW_OOM(vm, String::number(*url.port()));
  192. }
  193. WebIDL::ExceptionOr<void> Location::set_port(String const&)
  194. {
  195. auto& vm = this->vm();
  196. return vm.throw_completion<JS::InternalError>(JS::ErrorType::NotImplemented, "Location.port setter");
  197. }
  198. // https://html.spec.whatwg.org/multipage/history.html#dom-location-pathname
  199. WebIDL::ExceptionOr<String> Location::pathname() const
  200. {
  201. auto& vm = this->vm();
  202. // 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.
  203. auto const relevant_document = this->relevant_document();
  204. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  205. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_fly_string);
  206. // 2. Return the result of URL path serializing this Location object's url.
  207. return TRY_OR_THROW_OOM(vm, String::from_byte_string(url().serialize_path()));
  208. }
  209. WebIDL::ExceptionOr<void> Location::set_pathname(String const&)
  210. {
  211. auto& vm = this->vm();
  212. return vm.throw_completion<JS::InternalError>(JS::ErrorType::NotImplemented, "Location.pathname setter");
  213. }
  214. // https://html.spec.whatwg.org/multipage/history.html#dom-location-search
  215. WebIDL::ExceptionOr<String> Location::search() const
  216. {
  217. auto& vm = this->vm();
  218. // 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.
  219. auto const relevant_document = this->relevant_document();
  220. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  221. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_fly_string);
  222. auto url = this->url();
  223. // 2. If this's url's query is either null or the empty string, return the empty string.
  224. if (!url.query().has_value() || url.query()->is_empty())
  225. return String {};
  226. // 3. Return "?", followed by this's url's query.
  227. return TRY_OR_THROW_OOM(vm, String::formatted("?{}", url.query()));
  228. }
  229. WebIDL::ExceptionOr<void> Location::set_search(String const&)
  230. {
  231. auto& vm = this->vm();
  232. return vm.throw_completion<JS::InternalError>(JS::ErrorType::NotImplemented, "Location.search setter");
  233. }
  234. // https://html.spec.whatwg.org/multipage/history.html#dom-location-hash
  235. WebIDL::ExceptionOr<String> Location::hash() const
  236. {
  237. auto& vm = this->vm();
  238. // 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.
  239. auto const relevant_document = this->relevant_document();
  240. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  241. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_fly_string);
  242. auto url = this->url();
  243. // 2. If this's url's fragment is either null or the empty string, return the empty string.
  244. if (!url.fragment().has_value() || url.fragment()->is_empty())
  245. return String {};
  246. // 3. Return "#", followed by this's url's fragment.
  247. return TRY_OR_THROW_OOM(vm, String::formatted("#{}", *url.fragment()));
  248. }
  249. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-location-hash
  250. WebIDL::ExceptionOr<void> Location::set_hash(String const& value)
  251. {
  252. // The hash setter steps are:
  253. auto const relevant_document = this->relevant_document();
  254. // 1. If this's relevant Document is null, then return.
  255. if (!relevant_document)
  256. return {};
  257. // 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.
  258. if (!relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  259. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_fly_string);
  260. // 3. Let copyURL be a copy of this's url.
  261. auto copy_url = this->url();
  262. // 4. Let input be the given value with a single leading "#" removed, if any.
  263. auto input = value.bytes_as_string_view().trim("#"sv, TrimMode::Left);
  264. // 5. Set copyURL's fragment to the empty string.
  265. copy_url.set_fragment(String {});
  266. // 6. Basic URL parse input, with copyURL as url and fragment state as state override.
  267. auto result_url = URL::Parser::basic_parse(input, {}, copy_url, URL::Parser::State::Fragment);
  268. // 7. If copyURL's fragment is this's url's fragment, then return.
  269. if (copy_url.fragment() == this->url().fragment())
  270. return {};
  271. // 8. 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-reload
  276. void Location::reload() const
  277. {
  278. // 1. Let document be this's relevant Document.
  279. auto document = relevant_document();
  280. // 2. If document is null, then return.
  281. if (!document)
  282. return;
  283. // FIXME: 3. If document's origin is not same origin-domain with the entry settings object's origin, then throw a "SecurityError" DOMException.
  284. // 4. Reload document's node navigable.
  285. document->navigable()->reload();
  286. }
  287. // https://html.spec.whatwg.org/multipage/history.html#dom-location-replace
  288. WebIDL::ExceptionOr<void> Location::replace(String const& url)
  289. {
  290. // 1. If this's relevant Document is null, then return.
  291. if (!relevant_document())
  292. return {};
  293. // 2. Parse url relative to the entry settings object. If that failed, throw a "SyntaxError" DOMException.
  294. auto replace_url = entry_settings_object().parse_url(url);
  295. if (!replace_url.is_valid())
  296. return WebIDL::SyntaxError::create(realm(), MUST(String::formatted("Invalid URL '{}'", url)));
  297. // 3. Location-object navigate this to the resulting URL record given "replace".
  298. TRY(navigate(replace_url, Bindings::NavigationHistoryBehavior::Replace));
  299. return {};
  300. }
  301. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-location-assign
  302. WebIDL::ExceptionOr<void> Location::assign(String const& url)
  303. {
  304. // 1. If this's relevant Document is null, then return.
  305. auto const relevant_document = this->relevant_document();
  306. if (!relevant_document)
  307. return {};
  308. // 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.
  309. if (!relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  310. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_fly_string);
  311. // 3. Parse url relative to the entry settings object. If that failed, throw a "SyntaxError" DOMException.
  312. auto assign_url = entry_settings_object().parse_url(url);
  313. if (!assign_url.is_valid())
  314. return WebIDL::SyntaxError::create(realm(), MUST(String::formatted("Invalid URL '{}'", url)));
  315. // 4. Location-object navigate this to the resulting URL record.
  316. TRY(navigate(assign_url));
  317. return {};
  318. }
  319. // 7.10.5.1 [[GetPrototypeOf]] ( ), https://html.spec.whatwg.org/multipage/history.html#location-getprototypeof
  320. JS::ThrowCompletionOr<JS::Object*> Location::internal_get_prototype_of() const
  321. {
  322. // 1. If IsPlatformObjectSameOrigin(this) is true, then return ! OrdinaryGetPrototypeOf(this).
  323. if (HTML::is_platform_object_same_origin(*this))
  324. return MUST(JS::Object::internal_get_prototype_of());
  325. // 2. Return null.
  326. return nullptr;
  327. }
  328. // 7.10.5.2 [[SetPrototypeOf]] ( V ), https://html.spec.whatwg.org/multipage/history.html#location-setprototypeof
  329. JS::ThrowCompletionOr<bool> Location::internal_set_prototype_of(Object* prototype)
  330. {
  331. // 1. Return ! SetImmutablePrototype(this, V).
  332. return MUST(set_immutable_prototype(prototype));
  333. }
  334. // 7.10.5.3 [[IsExtensible]] ( ), https://html.spec.whatwg.org/multipage/history.html#location-isextensible
  335. JS::ThrowCompletionOr<bool> Location::internal_is_extensible() const
  336. {
  337. // 1. Return true.
  338. return true;
  339. }
  340. // 7.10.5.4 [[PreventExtensions]] ( ), https://html.spec.whatwg.org/multipage/history.html#location-preventextensions
  341. JS::ThrowCompletionOr<bool> Location::internal_prevent_extensions()
  342. {
  343. // 1. Return false.
  344. return false;
  345. }
  346. // 7.10.5.5 [[GetOwnProperty]] ( P ), https://html.spec.whatwg.org/multipage/history.html#location-getownproperty
  347. JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> Location::internal_get_own_property(JS::PropertyKey const& property_key) const
  348. {
  349. auto& vm = this->vm();
  350. // 1. If IsPlatformObjectSameOrigin(this) is true, then:
  351. if (HTML::is_platform_object_same_origin(*this)) {
  352. // 1. Let desc be OrdinaryGetOwnProperty(this, P).
  353. auto descriptor = MUST(Object::internal_get_own_property(property_key));
  354. // 2. If the value of the [[DefaultProperties]] internal slot of this contains P, then set desc.[[Configurable]] to true.
  355. auto property_key_value = property_key.is_symbol()
  356. ? JS::Value { property_key.as_symbol() }
  357. : JS::PrimitiveString::create(vm, property_key.to_string());
  358. if (m_default_properties.contains_slow(property_key_value))
  359. descriptor->configurable = true;
  360. // 3. Return desc.
  361. return descriptor;
  362. }
  363. // 2. Let property be CrossOriginGetOwnPropertyHelper(this, P).
  364. auto property = HTML::cross_origin_get_own_property_helper(const_cast<Location*>(this), property_key);
  365. // 3. If property is not undefined, then return property.
  366. if (property.has_value())
  367. return property;
  368. // 4. Return ? CrossOriginPropertyFallback(P).
  369. return TRY(HTML::cross_origin_property_fallback(vm, property_key));
  370. }
  371. // 7.10.5.6 [[DefineOwnProperty]] ( P, Desc ), https://html.spec.whatwg.org/multipage/history.html#location-defineownproperty
  372. JS::ThrowCompletionOr<bool> Location::internal_define_own_property(JS::PropertyKey const& property_key, JS::PropertyDescriptor const& descriptor)
  373. {
  374. // 1. If IsPlatformObjectSameOrigin(this) is true, then:
  375. if (HTML::is_platform_object_same_origin(*this)) {
  376. // 1. If the value of the [[DefaultProperties]] internal slot of this contains P, then return false.
  377. // 2. Return ? OrdinaryDefineOwnProperty(this, P, Desc).
  378. return JS::Object::internal_define_own_property(property_key, descriptor);
  379. }
  380. // 2. Throw a "SecurityError" DOMException.
  381. return throw_completion(WebIDL::SecurityError::create(realm(), MUST(String::formatted("Can't define property '{}' on cross-origin object", property_key))));
  382. }
  383. // 7.10.5.7 [[Get]] ( P, Receiver ), https://html.spec.whatwg.org/multipage/history.html#location-get
  384. JS::ThrowCompletionOr<JS::Value> Location::internal_get(JS::PropertyKey const& property_key, JS::Value receiver, JS::CacheablePropertyMetadata* cacheable_metadata) const
  385. {
  386. auto& vm = this->vm();
  387. // 1. If IsPlatformObjectSameOrigin(this) is true, then return ? OrdinaryGet(this, P, Receiver).
  388. if (HTML::is_platform_object_same_origin(*this))
  389. return JS::Object::internal_get(property_key, receiver, cacheable_metadata);
  390. // 2. Return ? CrossOriginGet(this, P, Receiver).
  391. return HTML::cross_origin_get(vm, static_cast<JS::Object const&>(*this), property_key, receiver);
  392. }
  393. // 7.10.5.8 [[Set]] ( P, V, Receiver ), https://html.spec.whatwg.org/multipage/history.html#location-set
  394. JS::ThrowCompletionOr<bool> Location::internal_set(JS::PropertyKey const& property_key, JS::Value value, JS::Value receiver, JS::CacheablePropertyMetadata* cacheable_metadata)
  395. {
  396. auto& vm = this->vm();
  397. // 1. If IsPlatformObjectSameOrigin(this) is true, then return ? OrdinarySet(this, P, V, Receiver).
  398. if (HTML::is_platform_object_same_origin(*this))
  399. return JS::Object::internal_set(property_key, value, receiver, cacheable_metadata);
  400. // 2. Return ? CrossOriginSet(this, P, V, Receiver).
  401. return HTML::cross_origin_set(vm, static_cast<JS::Object&>(*this), property_key, value, receiver);
  402. }
  403. // 7.10.5.9 [[Delete]] ( P ), https://html.spec.whatwg.org/multipage/history.html#location-delete
  404. JS::ThrowCompletionOr<bool> Location::internal_delete(JS::PropertyKey const& property_key)
  405. {
  406. // 1. If IsPlatformObjectSameOrigin(this) is true, then return ? OrdinaryDelete(this, P).
  407. if (HTML::is_platform_object_same_origin(*this))
  408. return JS::Object::internal_delete(property_key);
  409. // 2. Throw a "SecurityError" DOMException.
  410. return throw_completion(WebIDL::SecurityError::create(realm(), MUST(String::formatted("Can't delete property '{}' on cross-origin object", property_key))));
  411. }
  412. // 7.10.5.10 [[OwnPropertyKeys]] ( ), https://html.spec.whatwg.org/multipage/history.html#location-ownpropertykeys
  413. JS::ThrowCompletionOr<JS::MarkedVector<JS::Value>> Location::internal_own_property_keys() const
  414. {
  415. // 1. If IsPlatformObjectSameOrigin(this) is true, then return OrdinaryOwnPropertyKeys(this).
  416. if (HTML::is_platform_object_same_origin(*this))
  417. return JS::Object::internal_own_property_keys();
  418. // 2. Return CrossOriginOwnPropertyKeys(this).
  419. return HTML::cross_origin_own_property_keys(this);
  420. }
  421. }