Location.cpp 24 KB

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