Location.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /*
  2. * Copyright (c) 2020-2023, Andreas Kling <andreas@ladybird.org>
  3. * Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/String.h>
  8. #include <AK/StringBuilder.h>
  9. #include <LibGC/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. GC_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. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#the-location-interface
  35. void Location::initialize(JS::Realm& realm)
  36. {
  37. Base::initialize(realm);
  38. WEB_SET_PROTOTYPE_FOR_INTERFACE(Location);
  39. auto& vm = this->vm();
  40. // Step 2: Let valueOf be location's relevant realm.[[Intrinsics]].[[%Object.prototype.valueOf%]].
  41. auto& intrinsics = realm.intrinsics();
  42. auto value_of_function = intrinsics.object_prototype()->get_without_side_effects(vm.names.valueOf);
  43. // Step 3: Perform ! location.[[DefineOwnProperty]]("valueOf", { [[Value]]: valueOf, [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }).
  44. auto value_of_property_descriptor = JS::PropertyDescriptor {
  45. .value = value_of_function,
  46. .writable = false,
  47. .enumerable = false,
  48. .configurable = false,
  49. };
  50. MUST(internal_define_own_property(vm.names.valueOf, value_of_property_descriptor));
  51. // Step 4: Perform ! location.[[DefineOwnProperty]](%Symbol.toPrimitive%, { [[Value]]: undefined, [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }).
  52. auto to_primitive_property_descriptor = JS::PropertyDescriptor {
  53. .value = JS::js_undefined(),
  54. .writable = false,
  55. .enumerable = false,
  56. .configurable = false,
  57. };
  58. MUST(internal_define_own_property(vm.well_known_symbol_to_primitive(), to_primitive_property_descriptor));
  59. // 5. Set the value of the [[DefaultProperties]] internal slot of location to location.[[OwnPropertyKeys]]().
  60. // NOTE: In LibWeb this happens before the ESO is set up, so we must avoid location's custom [[OwnPropertyKeys]].
  61. m_default_properties.extend(MUST(Object::internal_own_property_keys()));
  62. }
  63. // https://html.spec.whatwg.org/multipage/history.html#relevant-document
  64. GC::Ptr<DOM::Document> Location::relevant_document() const
  65. {
  66. // A Location object has an associated relevant Document, which is this Location object's
  67. // relevant global object's browsing context's active document, if this Location object's
  68. // relevant global object's browsing context is non-null, and null otherwise.
  69. auto* browsing_context = verify_cast<HTML::Window>(HTML::relevant_global_object(*this)).browsing_context();
  70. return browsing_context ? browsing_context->active_document() : nullptr;
  71. }
  72. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#location-object-navigate
  73. WebIDL::ExceptionOr<void> Location::navigate(URL::URL url, Bindings::NavigationHistoryBehavior history_handling)
  74. {
  75. // 1. Let navigable be location's relevant global object's navigable.
  76. auto navigable = verify_cast<HTML::Window>(HTML::relevant_global_object(*this)).navigable();
  77. // 2. Let sourceDocument be the incumbent global object's associated Document.
  78. auto& source_document = verify_cast<HTML::Window>(incumbent_global_object()).associated_document();
  79. // 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".
  80. if (!relevant_document()->is_completely_loaded() && !verify_cast<HTML::Window>(incumbent_global_object()).has_transient_activation()) {
  81. history_handling = Bindings::NavigationHistoryBehavior::Replace;
  82. }
  83. // 4. Navigate navigable to url using sourceDocument, with exceptionsEnabled set to true and historyHandling set to historyHandling.
  84. TRY(navigable->navigate({ .url = url,
  85. .source_document = source_document,
  86. .exceptions_enabled = true,
  87. .history_handling = history_handling }));
  88. return {};
  89. }
  90. // https://html.spec.whatwg.org/multipage/history.html#concept-location-url
  91. URL::URL Location::url() const
  92. {
  93. // A Location object has an associated url, which is this Location object's relevant Document's URL,
  94. // if this Location object's relevant Document is non-null, and about:blank otherwise.
  95. auto const relevant_document = this->relevant_document();
  96. return relevant_document ? relevant_document->url() : "about:blank"sv;
  97. }
  98. // https://html.spec.whatwg.org/multipage/history.html#dom-location-href
  99. WebIDL::ExceptionOr<String> Location::href() const
  100. {
  101. // 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.
  102. auto const relevant_document = this->relevant_document();
  103. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  104. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  105. // 2. Return this's url, serialized.
  106. return url().serialize();
  107. }
  108. // https://html.spec.whatwg.org/multipage/history.html#the-location-interface:dom-location-href-2
  109. WebIDL::ExceptionOr<void> Location::set_href(String const& new_href)
  110. {
  111. auto& realm = this->realm();
  112. // 1. If this's relevant Document is null, then return.
  113. auto const relevant_document = this->relevant_document();
  114. if (!relevant_document)
  115. return {};
  116. // 2. Let url be the result of encoding-parsing a URL given the given value, relative to the entry settings object.
  117. auto url = entry_settings_object().encoding_parse_url(new_href.to_byte_string());
  118. // 3. If url is failure, then throw a "SyntaxError" DOMException.
  119. if (!url.is_valid())
  120. return WebIDL::SyntaxError::create(realm, MUST(String::formatted("Invalid URL '{}'", new_href)));
  121. // 4. Location-object navigate this to url.
  122. TRY(navigate(url));
  123. return {};
  124. }
  125. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-location-origin
  126. WebIDL::ExceptionOr<String> Location::origin() const
  127. {
  128. // 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.
  129. auto const relevant_document = this->relevant_document();
  130. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  131. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  132. // 2. Return the serialization of this's url's origin.
  133. return url().origin().serialize();
  134. }
  135. // https://html.spec.whatwg.org/multipage/history.html#dom-location-protocol
  136. WebIDL::ExceptionOr<String> Location::protocol() const
  137. {
  138. auto& vm = this->vm();
  139. // 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.
  140. auto const relevant_document = this->relevant_document();
  141. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  142. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  143. // 2. Return this's url's scheme, followed by ":".
  144. return TRY_OR_THROW_OOM(vm, String::formatted("{}:", url().scheme()));
  145. }
  146. // https://html.spec.whatwg.org/multipage/history.html#dom-location-protocol
  147. WebIDL::ExceptionOr<void> Location::set_protocol(String const& value)
  148. {
  149. auto relevant_document = this->relevant_document();
  150. // 1. If this's relevant Document is null, then return.
  151. if (!relevant_document)
  152. return {};
  153. // 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.
  154. if (!relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  155. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  156. // 3. Let copyURL be a copy of this's url.
  157. auto copy_url = this->url();
  158. // 4. Let possibleFailure be the result of basic URL parsing the given value, followed by ":", with copyURL as url and scheme start state as state override.
  159. auto possible_failure = URL::Parser::basic_parse(value, {}, &copy_url, URL::Parser::State::SchemeStart);
  160. // 5. If possibleFailure is failure, then throw a "SyntaxError" DOMException.
  161. if (!possible_failure.is_valid())
  162. return WebIDL::SyntaxError::create(realm(), MUST(String::formatted("Failed to set protocol. '{}' is an invalid protocol", value)));
  163. // 6. if copyURL's scheme is not an HTTP(S) scheme, then terminate these steps.
  164. if (!(copy_url.scheme() == "http"sv || copy_url.scheme() == "https"sv))
  165. return {};
  166. // 7. Location-object navigate this to copyURL.
  167. TRY(navigate(copy_url));
  168. return {};
  169. }
  170. // https://html.spec.whatwg.org/multipage/history.html#dom-location-host
  171. WebIDL::ExceptionOr<String> Location::host() const
  172. {
  173. auto& vm = this->vm();
  174. // 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.
  175. auto const relevant_document = this->relevant_document();
  176. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  177. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  178. // 2. Let url be this's url.
  179. auto url = this->url();
  180. // 3. If url's host is null, return the empty string.
  181. if (!url.host().has_value())
  182. return String {};
  183. // 4. If url's port is null, return url's host, serialized.
  184. if (!url.port().has_value())
  185. return url.serialized_host();
  186. // 5. Return url's host, serialized, followed by ":" and url's port, serialized.
  187. return TRY_OR_THROW_OOM(vm, String::formatted("{}:{}", url.serialized_host(), *url.port()));
  188. }
  189. WebIDL::ExceptionOr<void> Location::set_host(String const&)
  190. {
  191. auto& vm = this->vm();
  192. return vm.throw_completion<JS::InternalError>(JS::ErrorType::NotImplemented, "Location.host setter");
  193. }
  194. // https://html.spec.whatwg.org/multipage/history.html#dom-location-hostname
  195. WebIDL::ExceptionOr<String> Location::hostname() const
  196. {
  197. // 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.
  198. auto const relevant_document = this->relevant_document();
  199. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  200. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  201. auto url = this->url();
  202. // 2. If this's url's host is null, return the empty string.
  203. if (!url.host().has_value())
  204. return String {};
  205. // 3. Return this's url's host, serialized.
  206. return url.serialized_host();
  207. }
  208. WebIDL::ExceptionOr<void> Location::set_hostname(String const&)
  209. {
  210. auto& vm = this->vm();
  211. return vm.throw_completion<JS::InternalError>(JS::ErrorType::NotImplemented, "Location.hostname setter");
  212. }
  213. // https://html.spec.whatwg.org/multipage/history.html#dom-location-port
  214. WebIDL::ExceptionOr<String> Location::port() const
  215. {
  216. // 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.
  217. auto const relevant_document = this->relevant_document();
  218. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  219. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  220. auto url = this->url();
  221. // 2. If this's url's port is null, return the empty string.
  222. if (!url.port().has_value())
  223. return String {};
  224. // 3. Return this's url's port, serialized.
  225. return String::number(*url.port());
  226. }
  227. WebIDL::ExceptionOr<void> Location::set_port(String const&)
  228. {
  229. auto& vm = this->vm();
  230. return vm.throw_completion<JS::InternalError>(JS::ErrorType::NotImplemented, "Location.port setter");
  231. }
  232. // https://html.spec.whatwg.org/multipage/history.html#dom-location-pathname
  233. WebIDL::ExceptionOr<String> Location::pathname() const
  234. {
  235. // 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.
  236. auto const relevant_document = this->relevant_document();
  237. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  238. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  239. // 2. Return the result of URL path serializing this Location object's url.
  240. return url().serialize_path();
  241. }
  242. WebIDL::ExceptionOr<void> Location::set_pathname(String const&)
  243. {
  244. auto& vm = this->vm();
  245. return vm.throw_completion<JS::InternalError>(JS::ErrorType::NotImplemented, "Location.pathname setter");
  246. }
  247. // https://html.spec.whatwg.org/multipage/history.html#dom-location-search
  248. WebIDL::ExceptionOr<String> Location::search() const
  249. {
  250. auto& vm = this->vm();
  251. // 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.
  252. auto const relevant_document = this->relevant_document();
  253. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  254. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  255. auto url = this->url();
  256. // 2. If this's url's query is either null or the empty string, return the empty string.
  257. if (!url.query().has_value() || url.query()->is_empty())
  258. return String {};
  259. // 3. Return "?", followed by this's url's query.
  260. return TRY_OR_THROW_OOM(vm, String::formatted("?{}", url.query()));
  261. }
  262. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-location-search
  263. WebIDL::ExceptionOr<void> Location::set_search(String const& value)
  264. {
  265. // The search setter steps are:
  266. auto const relevant_document = this->relevant_document();
  267. // 1. If this's relevant Document is null, then return.
  268. if (!relevant_document)
  269. return {};
  270. // 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.
  271. if (!relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  272. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  273. // 3. Let copyURL be a copy of this's url.
  274. auto copy_url = this->url();
  275. // 4. If the given value is the empty string, set copyURL's query to null.
  276. if (value.is_empty()) {
  277. copy_url.set_query({});
  278. }
  279. // 5. Otherwise, run these substeps:
  280. else {
  281. // 5.1. Let input be the given value with a single leading "?" removed, if any.
  282. auto value_as_string_view = value.bytes_as_string_view();
  283. auto input = value_as_string_view.substring_view(value_as_string_view.starts_with('?'));
  284. // 5.2. Set copyURL's query to the empty string.
  285. copy_url.set_query(String {});
  286. // 5.3. Basic URL parse input, with null, the relevant Document's document's character encoding, copyURL as url, and query state as state override.
  287. (void)URL::Parser::basic_parse(input, {}, &copy_url, URL::Parser::State::Query);
  288. }
  289. // 6. Location-object navigate this to copyURL.
  290. TRY(navigate(copy_url));
  291. return {};
  292. }
  293. // https://html.spec.whatwg.org/multipage/history.html#dom-location-hash
  294. WebIDL::ExceptionOr<String> Location::hash() const
  295. {
  296. auto& vm = this->vm();
  297. // 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.
  298. auto const relevant_document = this->relevant_document();
  299. if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  300. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  301. auto url = this->url();
  302. // 2. If this's url's fragment is either null or the empty string, return the empty string.
  303. if (!url.fragment().has_value() || url.fragment()->is_empty())
  304. return String {};
  305. // 3. Return "#", followed by this's url's fragment.
  306. return TRY_OR_THROW_OOM(vm, String::formatted("#{}", *url.fragment()));
  307. }
  308. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-location-hash
  309. WebIDL::ExceptionOr<void> Location::set_hash(String const& value)
  310. {
  311. // The hash setter steps are:
  312. auto const relevant_document = this->relevant_document();
  313. // 1. If this's relevant Document is null, then return.
  314. if (!relevant_document)
  315. return {};
  316. // 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.
  317. if (!relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  318. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  319. // 3. Let copyURL be a copy of this's url.
  320. auto copy_url = this->url();
  321. // 4. Let input be the given value with a single leading "#" removed, if any.
  322. auto input = value.bytes_as_string_view().trim("#"sv, TrimMode::Left);
  323. // 5. Set copyURL's fragment to the empty string.
  324. copy_url.set_fragment(String {});
  325. // 6. Basic URL parse input, with copyURL as url and fragment state as state override.
  326. (void)URL::Parser::basic_parse(input, {}, &copy_url, URL::Parser::State::Fragment);
  327. // 7. If copyURL's fragment is this's url's fragment, then return.
  328. if (copy_url.fragment() == this->url().fragment())
  329. return {};
  330. // 8. Location-object navigate this to copyURL.
  331. TRY(navigate(copy_url));
  332. return {};
  333. }
  334. // https://html.spec.whatwg.org/multipage/history.html#dom-location-reload
  335. void Location::reload() const
  336. {
  337. // 1. Let document be this's relevant Document.
  338. auto document = relevant_document();
  339. // 2. If document is null, then return.
  340. if (!document)
  341. return;
  342. // FIXME: 3. If document's origin is not same origin-domain with the entry settings object's origin, then throw a "SecurityError" DOMException.
  343. // 4. Reload document's node navigable.
  344. document->navigable()->reload();
  345. }
  346. // https://html.spec.whatwg.org/multipage/history.html#dom-location-replace
  347. WebIDL::ExceptionOr<void> Location::replace(String const& url)
  348. {
  349. // 1. If this's relevant Document is null, then return.
  350. if (!relevant_document())
  351. return {};
  352. // 2. Parse url relative to the entry settings object. If that failed, throw a "SyntaxError" DOMException.
  353. auto replace_url = entry_settings_object().parse_url(url);
  354. if (!replace_url.is_valid())
  355. return WebIDL::SyntaxError::create(realm(), MUST(String::formatted("Invalid URL '{}'", url)));
  356. // 3. Location-object navigate this to the resulting URL record given "replace".
  357. TRY(navigate(replace_url, Bindings::NavigationHistoryBehavior::Replace));
  358. return {};
  359. }
  360. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-location-assign
  361. WebIDL::ExceptionOr<void> Location::assign(String const& url)
  362. {
  363. // 1. If this's relevant Document is null, then return.
  364. auto const relevant_document = this->relevant_document();
  365. if (!relevant_document)
  366. return {};
  367. // 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.
  368. if (!relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
  369. return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
  370. // 3. Parse url relative to the entry settings object. If that failed, throw a "SyntaxError" DOMException.
  371. auto assign_url = entry_settings_object().parse_url(url);
  372. if (!assign_url.is_valid())
  373. return WebIDL::SyntaxError::create(realm(), MUST(String::formatted("Invalid URL '{}'", url)));
  374. // 4. Location-object navigate this to the resulting URL record.
  375. TRY(navigate(assign_url));
  376. return {};
  377. }
  378. // 7.10.5.1 [[GetPrototypeOf]] ( ), https://html.spec.whatwg.org/multipage/history.html#location-getprototypeof
  379. JS::ThrowCompletionOr<JS::Object*> Location::internal_get_prototype_of() const
  380. {
  381. // 1. If IsPlatformObjectSameOrigin(this) is true, then return ! OrdinaryGetPrototypeOf(this).
  382. if (HTML::is_platform_object_same_origin(*this))
  383. return MUST(JS::Object::internal_get_prototype_of());
  384. // 2. Return null.
  385. return nullptr;
  386. }
  387. // 7.10.5.2 [[SetPrototypeOf]] ( V ), https://html.spec.whatwg.org/multipage/history.html#location-setprototypeof
  388. JS::ThrowCompletionOr<bool> Location::internal_set_prototype_of(Object* prototype)
  389. {
  390. // 1. Return ! SetImmutablePrototype(this, V).
  391. return MUST(set_immutable_prototype(prototype));
  392. }
  393. // 7.10.5.3 [[IsExtensible]] ( ), https://html.spec.whatwg.org/multipage/history.html#location-isextensible
  394. JS::ThrowCompletionOr<bool> Location::internal_is_extensible() const
  395. {
  396. // 1. Return true.
  397. return true;
  398. }
  399. // 7.10.5.4 [[PreventExtensions]] ( ), https://html.spec.whatwg.org/multipage/history.html#location-preventextensions
  400. JS::ThrowCompletionOr<bool> Location::internal_prevent_extensions()
  401. {
  402. // 1. Return false.
  403. return false;
  404. }
  405. // 7.10.5.5 [[GetOwnProperty]] ( P ), https://html.spec.whatwg.org/multipage/history.html#location-getownproperty
  406. JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> Location::internal_get_own_property(JS::PropertyKey const& property_key) const
  407. {
  408. auto& vm = this->vm();
  409. // 1. If IsPlatformObjectSameOrigin(this) is true, then:
  410. if (HTML::is_platform_object_same_origin(*this)) {
  411. // 1. Let desc be OrdinaryGetOwnProperty(this, P).
  412. auto descriptor = MUST(Object::internal_get_own_property(property_key));
  413. // 2. If the value of the [[DefaultProperties]] internal slot of this contains P, then set desc.[[Configurable]] to true.
  414. auto property_key_value = property_key.is_symbol()
  415. ? JS::Value { property_key.as_symbol() }
  416. : JS::PrimitiveString::create(vm, property_key.to_string());
  417. if (m_default_properties.contains_slow(property_key_value))
  418. descriptor->configurable = true;
  419. // 3. Return desc.
  420. return descriptor;
  421. }
  422. // 2. Let property be CrossOriginGetOwnPropertyHelper(this, P).
  423. auto property = HTML::cross_origin_get_own_property_helper(const_cast<Location*>(this), property_key);
  424. // 3. If property is not undefined, then return property.
  425. if (property.has_value())
  426. return property;
  427. // 4. Return ? CrossOriginPropertyFallback(P).
  428. return TRY(HTML::cross_origin_property_fallback(vm, property_key));
  429. }
  430. // 7.10.5.6 [[DefineOwnProperty]] ( P, Desc ), https://html.spec.whatwg.org/multipage/history.html#location-defineownproperty
  431. JS::ThrowCompletionOr<bool> Location::internal_define_own_property(JS::PropertyKey const& property_key, JS::PropertyDescriptor const& descriptor, Optional<JS::PropertyDescriptor>* precomputed_get_own_property)
  432. {
  433. // 1. If IsPlatformObjectSameOrigin(this) is true, then:
  434. if (HTML::is_platform_object_same_origin(*this)) {
  435. // 1. If the value of the [[DefaultProperties]] internal slot of this contains P, then return false.
  436. // 2. Return ? OrdinaryDefineOwnProperty(this, P, Desc).
  437. return JS::Object::internal_define_own_property(property_key, descriptor, precomputed_get_own_property);
  438. }
  439. // 2. Throw a "SecurityError" DOMException.
  440. return throw_completion(WebIDL::SecurityError::create(realm(), MUST(String::formatted("Can't define property '{}' on cross-origin object", property_key))));
  441. }
  442. // 7.10.5.7 [[Get]] ( P, Receiver ), https://html.spec.whatwg.org/multipage/history.html#location-get
  443. JS::ThrowCompletionOr<JS::Value> Location::internal_get(JS::PropertyKey const& property_key, JS::Value receiver, JS::CacheablePropertyMetadata* cacheable_metadata, PropertyLookupPhase phase) const
  444. {
  445. auto& vm = this->vm();
  446. // 1. If IsPlatformObjectSameOrigin(this) is true, then return ? OrdinaryGet(this, P, Receiver).
  447. if (HTML::is_platform_object_same_origin(*this))
  448. return JS::Object::internal_get(property_key, receiver, cacheable_metadata, phase);
  449. // 2. Return ? CrossOriginGet(this, P, Receiver).
  450. return HTML::cross_origin_get(vm, static_cast<JS::Object const&>(*this), property_key, receiver);
  451. }
  452. // 7.10.5.8 [[Set]] ( P, V, Receiver ), https://html.spec.whatwg.org/multipage/history.html#location-set
  453. JS::ThrowCompletionOr<bool> Location::internal_set(JS::PropertyKey const& property_key, JS::Value value, JS::Value receiver, JS::CacheablePropertyMetadata* cacheable_metadata)
  454. {
  455. auto& vm = this->vm();
  456. // 1. If IsPlatformObjectSameOrigin(this) is true, then return ? OrdinarySet(this, P, V, Receiver).
  457. if (HTML::is_platform_object_same_origin(*this))
  458. return JS::Object::internal_set(property_key, value, receiver, cacheable_metadata);
  459. // 2. Return ? CrossOriginSet(this, P, V, Receiver).
  460. return HTML::cross_origin_set(vm, static_cast<JS::Object&>(*this), property_key, value, receiver);
  461. }
  462. // 7.10.5.9 [[Delete]] ( P ), https://html.spec.whatwg.org/multipage/history.html#location-delete
  463. JS::ThrowCompletionOr<bool> Location::internal_delete(JS::PropertyKey const& property_key)
  464. {
  465. // 1. If IsPlatformObjectSameOrigin(this) is true, then return ? OrdinaryDelete(this, P).
  466. if (HTML::is_platform_object_same_origin(*this))
  467. return JS::Object::internal_delete(property_key);
  468. // 2. Throw a "SecurityError" DOMException.
  469. return throw_completion(WebIDL::SecurityError::create(realm(), MUST(String::formatted("Can't delete property '{}' on cross-origin object", property_key))));
  470. }
  471. // 7.10.5.10 [[OwnPropertyKeys]] ( ), https://html.spec.whatwg.org/multipage/history.html#location-ownpropertykeys
  472. JS::ThrowCompletionOr<GC::MarkedVector<JS::Value>> Location::internal_own_property_keys() const
  473. {
  474. // 1. If IsPlatformObjectSameOrigin(this) is true, then return OrdinaryOwnPropertyKeys(this).
  475. if (HTML::is_platform_object_same_origin(*this))
  476. return JS::Object::internal_own_property_keys();
  477. // 2. Return CrossOriginOwnPropertyKeys(this).
  478. return HTML::cross_origin_own_property_keys(this);
  479. }
  480. }