Location.cpp 24 KB

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