LocationObject.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/FlyString.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 <LibWeb/Bindings/LocationObject.h>
  14. #include <LibWeb/Bindings/LocationPrototype.h>
  15. #include <LibWeb/DOM/DOMException.h>
  16. #include <LibWeb/DOM/Document.h>
  17. #include <LibWeb/HTML/CrossOrigin/AbstractOperations.h>
  18. #include <LibWeb/HTML/Window.h>
  19. namespace Web::Bindings {
  20. // https://html.spec.whatwg.org/multipage/history.html#the-location-interface
  21. LocationObject::LocationObject(JS::Realm& realm)
  22. : PlatformObject(realm)
  23. {
  24. set_prototype(&verify_cast<HTML::Window>(realm.global_object()).cached_web_prototype("Location"));
  25. }
  26. LocationObject::~LocationObject() = default;
  27. void LocationObject::visit_edges(Cell::Visitor& visitor)
  28. {
  29. Base::visit_edges(visitor);
  30. for (auto& property : m_default_properties)
  31. visitor.visit(property);
  32. }
  33. void LocationObject::initialize(JS::Realm& realm)
  34. {
  35. auto& vm = this->vm();
  36. Object::initialize(realm);
  37. u8 attr = JS::Attribute::Writable | JS::Attribute::Enumerable;
  38. define_native_accessor(realm, "href", href_getter, href_setter, attr);
  39. define_native_accessor(realm, "host", host_getter, {}, attr);
  40. define_native_accessor(realm, "hostname", hostname_getter, {}, attr);
  41. define_native_accessor(realm, "pathname", pathname_getter, {}, attr);
  42. define_native_accessor(realm, "hash", hash_getter, {}, attr);
  43. define_native_accessor(realm, "search", search_getter, {}, attr);
  44. define_native_accessor(realm, "protocol", protocol_getter, {}, attr);
  45. define_native_accessor(realm, "port", port_getter, {}, attr);
  46. define_native_function(realm, "reload", reload, 0, JS::Attribute::Enumerable);
  47. define_native_function(realm, "replace", replace, 1, JS::Attribute::Enumerable);
  48. define_native_function(realm, vm.names.toString, href_getter, 0, JS::Attribute::Enumerable);
  49. // 5. Set the value of the [[DefaultProperties]] internal slot of location to location.[[OwnPropertyKeys]]().
  50. // NOTE: In LibWeb this happens before the ESO is set up, so we must avoid location's custom [[OwnPropertyKeys]].
  51. m_default_properties.extend(MUST(Object::internal_own_property_keys()));
  52. }
  53. // https://html.spec.whatwg.org/multipage/history.html#relevant-document
  54. DOM::Document const* LocationObject::relevant_document() const
  55. {
  56. // A Location object has an associated relevant Document, which is this Location object's
  57. // relevant global object's browsing context's active document, if this Location object's
  58. // relevant global object's browsing context is non-null, and null otherwise.
  59. auto* browsing_context = verify_cast<HTML::Window>(HTML::relevant_global_object(*this)).browsing_context();
  60. return browsing_context ? browsing_context->active_document() : nullptr;
  61. }
  62. // https://html.spec.whatwg.org/multipage/history.html#concept-location-url
  63. AK::URL LocationObject::url() const
  64. {
  65. // A Location object has an associated url, which is this Location object's relevant Document's URL,
  66. // if this Location object's relevant Document is non-null, and about:blank otherwise.
  67. auto const* relevant_document = this->relevant_document();
  68. return relevant_document ? relevant_document->url() : "about:blank"sv;
  69. }
  70. static JS::ThrowCompletionOr<LocationObject*> typed_this_value(JS::VM& vm)
  71. {
  72. auto this_value = vm.this_value();
  73. if (!this_value.is_object() || !is<LocationObject>(this_value.as_object()))
  74. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "Location");
  75. return static_cast<LocationObject*>(&this_value.as_object());
  76. }
  77. // https://html.spec.whatwg.org/multipage/history.html#dom-location-href
  78. JS_DEFINE_NATIVE_FUNCTION(LocationObject::href_getter)
  79. {
  80. auto* location_object = TRY(typed_this_value(vm));
  81. // FIXME: 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.
  82. // 2. Return this's url, serialized.
  83. return JS::js_string(vm, location_object->url().to_string());
  84. }
  85. // https://html.spec.whatwg.org/multipage/history.html#the-location-interface:dom-location-href-2
  86. JS_DEFINE_NATIVE_FUNCTION(LocationObject::href_setter)
  87. {
  88. auto& window = verify_cast<HTML::Window>(HTML::current_global_object());
  89. // FIXME: 1. If this's relevant Document is null, then return.
  90. // 2. Parse the given value relative to the entry settings object. If that failed, throw a TypeError exception.
  91. auto new_href = TRY(vm.argument(0).to_string(vm));
  92. auto href_url = window.associated_document().parse_url(new_href);
  93. if (!href_url.is_valid())
  94. return vm.throw_completion<JS::URIError>(String::formatted("Invalid URL '{}'", new_href));
  95. // 3. Location-object navigate given the resulting URL record.
  96. window.did_set_location_href({}, href_url);
  97. return JS::js_undefined();
  98. }
  99. // https://html.spec.whatwg.org/multipage/history.html#dom-location-pathname
  100. JS_DEFINE_NATIVE_FUNCTION(LocationObject::pathname_getter)
  101. {
  102. auto* location_object = TRY(typed_this_value(vm));
  103. // FIXME: 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.
  104. // 2. Return the result of URL path serializing this Location object's url.
  105. return JS::js_string(vm, location_object->url().path());
  106. }
  107. // https://html.spec.whatwg.org/multipage/history.html#dom-location-hostname
  108. JS_DEFINE_NATIVE_FUNCTION(LocationObject::hostname_getter)
  109. {
  110. auto* location_object = TRY(typed_this_value(vm));
  111. // FIXME: 1. If this's relevant Document is non-null and its origin is not same origin-domain with the entry settings object's origin, then throw a "SecurityError" DOMException.
  112. // 2. If this's url's host is null, return the empty string.
  113. if (location_object->url().host().is_null())
  114. return JS::js_string(vm, String::empty());
  115. // 3. Return this's url's host, serialized.
  116. return JS::js_string(vm, location_object->url().host());
  117. }
  118. // https://html.spec.whatwg.org/multipage/history.html#dom-location-host
  119. JS_DEFINE_NATIVE_FUNCTION(LocationObject::host_getter)
  120. {
  121. auto* location_object = TRY(typed_this_value(vm));
  122. // FIXME: 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. // 2. Let url be this's url.
  124. auto url = location_object->url();
  125. // 3. If url's host is null, return the empty string.
  126. if (url.host().is_null())
  127. return JS::js_string(vm, String::empty());
  128. // 4. If url's port is null, return url's host, serialized.
  129. if (!url.port().has_value())
  130. return JS::js_string(vm, url.host());
  131. // 5. Return url's host, serialized, followed by ":" and url's port, serialized.
  132. return JS::js_string(vm, String::formatted("{}:{}", url.host(), *url.port()));
  133. }
  134. // https://html.spec.whatwg.org/multipage/history.html#dom-location-hash
  135. JS_DEFINE_NATIVE_FUNCTION(LocationObject::hash_getter)
  136. {
  137. auto* location_object = TRY(typed_this_value(vm));
  138. // FIXME: 1. If this's relevant Document is non-null and its origin is not same origin-domain with the entry settings object's origin, then throw a "SecurityError" DOMException.
  139. // 2. If this's url's fragment is either null or the empty string, return the empty string.
  140. if (location_object->url().fragment().is_empty())
  141. return JS::js_string(vm, String::empty());
  142. // 3. Return "#", followed by this's url's fragment.
  143. return JS::js_string(vm, String::formatted("#{}", location_object->url().fragment()));
  144. }
  145. // https://html.spec.whatwg.org/multipage/history.html#dom-location-search
  146. JS_DEFINE_NATIVE_FUNCTION(LocationObject::search_getter)
  147. {
  148. auto* location_object = TRY(typed_this_value(vm));
  149. // FIXME: 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. // 2. If this's url's query is either null or the empty string, return the empty string.
  151. if (location_object->url().query().is_empty())
  152. return JS::js_string(vm, String::empty());
  153. // 3. Return "?", followed by this's url's query.
  154. return JS::js_string(vm, String::formatted("?{}", location_object->url().query()));
  155. }
  156. // https://html.spec.whatwg.org/multipage/history.html#dom-location-protocol
  157. JS_DEFINE_NATIVE_FUNCTION(LocationObject::protocol_getter)
  158. {
  159. auto* location_object = TRY(typed_this_value(vm));
  160. // FIXME: 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.
  161. // 2. Return this's url's scheme, followed by ":".
  162. return JS::js_string(vm, String::formatted("{}:", location_object->url().protocol()));
  163. }
  164. // https://html.spec.whatwg.org/multipage/history.html#dom-location-port
  165. JS_DEFINE_NATIVE_FUNCTION(LocationObject::port_getter)
  166. {
  167. auto* location_object = TRY(typed_this_value(vm));
  168. // FIXME: 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.
  169. // 2. If this's url's port is null, return the empty string.
  170. if (!location_object->url().port().has_value())
  171. return JS::js_string(vm, String::empty());
  172. // 3. Return this's url's port, serialized.
  173. return JS::js_string(vm, String::number(*location_object->url().port()));
  174. }
  175. // https://html.spec.whatwg.org/multipage/history.html#dom-location-reload
  176. JS_DEFINE_NATIVE_FUNCTION(LocationObject::reload)
  177. {
  178. auto& window = verify_cast<HTML::Window>(HTML::current_global_object());
  179. window.did_call_location_reload({});
  180. return JS::js_undefined();
  181. }
  182. // https://html.spec.whatwg.org/multipage/history.html#dom-location-replace
  183. JS_DEFINE_NATIVE_FUNCTION(LocationObject::replace)
  184. {
  185. auto& window = verify_cast<HTML::Window>(HTML::current_global_object());
  186. auto url = TRY(vm.argument(0).to_string(vm));
  187. // FIXME: This needs spec compliance work.
  188. window.did_call_location_replace({}, move(url));
  189. return JS::js_undefined();
  190. }
  191. // 7.10.5.1 [[GetPrototypeOf]] ( ), https://html.spec.whatwg.org/multipage/history.html#location-getprototypeof
  192. JS::ThrowCompletionOr<JS::Object*> LocationObject::internal_get_prototype_of() const
  193. {
  194. // 1. If IsPlatformObjectSameOrigin(this) is true, then return ! OrdinaryGetPrototypeOf(this).
  195. if (HTML::is_platform_object_same_origin(*this))
  196. return MUST(JS::Object::internal_get_prototype_of());
  197. // 2. Return null.
  198. return nullptr;
  199. }
  200. // 7.10.5.2 [[SetPrototypeOf]] ( V ), https://html.spec.whatwg.org/multipage/history.html#location-setprototypeof
  201. JS::ThrowCompletionOr<bool> LocationObject::internal_set_prototype_of(Object* prototype)
  202. {
  203. // 1. Return ! SetImmutablePrototype(this, V).
  204. return MUST(set_immutable_prototype(prototype));
  205. }
  206. // 7.10.5.3 [[IsExtensible]] ( ), https://html.spec.whatwg.org/multipage/history.html#location-isextensible
  207. JS::ThrowCompletionOr<bool> LocationObject::internal_is_extensible() const
  208. {
  209. // 1. Return true.
  210. return true;
  211. }
  212. // 7.10.5.4 [[PreventExtensions]] ( ), https://html.spec.whatwg.org/multipage/history.html#location-preventextensions
  213. JS::ThrowCompletionOr<bool> LocationObject::internal_prevent_extensions()
  214. {
  215. // 1. Return false.
  216. return false;
  217. }
  218. // 7.10.5.5 [[GetOwnProperty]] ( P ), https://html.spec.whatwg.org/multipage/history.html#location-getownproperty
  219. JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> LocationObject::internal_get_own_property(JS::PropertyKey const& property_key) const
  220. {
  221. auto& vm = this->vm();
  222. // 1. If IsPlatformObjectSameOrigin(this) is true, then:
  223. if (HTML::is_platform_object_same_origin(*this)) {
  224. // 1. Let desc be OrdinaryGetOwnProperty(this, P).
  225. auto descriptor = MUST(Object::internal_get_own_property(property_key));
  226. // 2. If the value of the [[DefaultProperties]] internal slot of this contains P, then set desc.[[Configurable]] to true.
  227. auto property_key_value = property_key.is_symbol()
  228. ? JS::Value { property_key.as_symbol() }
  229. : JS::js_string(vm, property_key.to_string());
  230. if (m_default_properties.contains_slow(property_key_value))
  231. descriptor->configurable = true;
  232. // 3. Return desc.
  233. return descriptor;
  234. }
  235. // 2. Let property be CrossOriginGetOwnPropertyHelper(this, P).
  236. auto property = HTML::cross_origin_get_own_property_helper(const_cast<LocationObject*>(this), property_key);
  237. // 3. If property is not undefined, then return property.
  238. if (property.has_value())
  239. return property;
  240. // 4. Return ? CrossOriginPropertyFallback(P).
  241. return TRY(HTML::cross_origin_property_fallback(vm, property_key));
  242. }
  243. // 7.10.5.6 [[DefineOwnProperty]] ( P, Desc ), https://html.spec.whatwg.org/multipage/history.html#location-defineownproperty
  244. JS::ThrowCompletionOr<bool> LocationObject::internal_define_own_property(JS::PropertyKey const& property_key, JS::PropertyDescriptor const& descriptor)
  245. {
  246. // 1. If IsPlatformObjectSameOrigin(this) is true, then:
  247. if (HTML::is_platform_object_same_origin(*this)) {
  248. // 1. If the value of the [[DefaultProperties]] internal slot of this contains P, then return false.
  249. // 2. Return ? OrdinaryDefineOwnProperty(this, P, Desc).
  250. return JS::Object::internal_define_own_property(property_key, descriptor);
  251. }
  252. // 2. Throw a "SecurityError" DOMException.
  253. return throw_completion(DOM::SecurityError::create(global_object(), String::formatted("Can't define property '{}' on cross-origin object", property_key)));
  254. }
  255. // 7.10.5.7 [[Get]] ( P, Receiver ), https://html.spec.whatwg.org/multipage/history.html#location-get
  256. JS::ThrowCompletionOr<JS::Value> LocationObject::internal_get(JS::PropertyKey const& property_key, JS::Value receiver) const
  257. {
  258. auto& vm = this->vm();
  259. // 1. If IsPlatformObjectSameOrigin(this) is true, then return ? OrdinaryGet(this, P, Receiver).
  260. if (HTML::is_platform_object_same_origin(*this))
  261. return JS::Object::internal_get(property_key, receiver);
  262. // 2. Return ? CrossOriginGet(this, P, Receiver).
  263. return HTML::cross_origin_get(vm, static_cast<JS::Object const&>(*this), property_key, receiver);
  264. }
  265. // 7.10.5.8 [[Set]] ( P, V, Receiver ), https://html.spec.whatwg.org/multipage/history.html#location-set
  266. JS::ThrowCompletionOr<bool> LocationObject::internal_set(JS::PropertyKey const& property_key, JS::Value value, JS::Value receiver)
  267. {
  268. auto& vm = this->vm();
  269. // 1. If IsPlatformObjectSameOrigin(this) is true, then return ? OrdinarySet(this, P, V, Receiver).
  270. if (HTML::is_platform_object_same_origin(*this))
  271. return JS::Object::internal_set(property_key, value, receiver);
  272. // 2. Return ? CrossOriginSet(this, P, V, Receiver).
  273. return HTML::cross_origin_set(vm, static_cast<JS::Object&>(*this), property_key, value, receiver);
  274. }
  275. // 7.10.5.9 [[Delete]] ( P ), https://html.spec.whatwg.org/multipage/history.html#location-delete
  276. JS::ThrowCompletionOr<bool> LocationObject::internal_delete(JS::PropertyKey const& property_key)
  277. {
  278. // 1. If IsPlatformObjectSameOrigin(this) is true, then return ? OrdinaryDelete(this, P).
  279. if (HTML::is_platform_object_same_origin(*this))
  280. return JS::Object::internal_delete(property_key);
  281. // 2. Throw a "SecurityError" DOMException.
  282. return throw_completion(DOM::SecurityError::create(global_object(), String::formatted("Can't delete property '{}' on cross-origin object", property_key)));
  283. }
  284. // 7.10.5.10 [[OwnPropertyKeys]] ( ), https://html.spec.whatwg.org/multipage/history.html#location-ownpropertykeys
  285. JS::ThrowCompletionOr<JS::MarkedVector<JS::Value>> LocationObject::internal_own_property_keys() const
  286. {
  287. // 1. If IsPlatformObjectSameOrigin(this) is true, then return OrdinaryOwnPropertyKeys(this).
  288. if (HTML::is_platform_object_same_origin(*this))
  289. return JS::Object::internal_own_property_keys();
  290. // 2. Return CrossOriginOwnPropertyKeys(this).
  291. return HTML::cross_origin_own_property_keys(this);
  292. }
  293. }