Location.cpp 24 KB

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