ServiceWorkerContainer.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) 2024, Tim Ledbetter <tim.ledbetter@ladybird.org>
  3. * Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/Runtime/Realm.h>
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/Bindings/ServiceWorkerContainerPrototype.h>
  10. #include <LibWeb/DOMURL/DOMURL.h>
  11. #include <LibWeb/HTML/EventNames.h>
  12. #include <LibWeb/HTML/ServiceWorkerContainer.h>
  13. #include <LibWeb/StorageAPI/StorageKey.h>
  14. namespace Web::HTML {
  15. JS_DEFINE_ALLOCATOR(ServiceWorkerContainer);
  16. ServiceWorkerContainer::ServiceWorkerContainer(JS::Realm& realm)
  17. : DOM::EventTarget(realm)
  18. , m_service_worker_client(relevant_settings_object(*this))
  19. {
  20. }
  21. ServiceWorkerContainer::~ServiceWorkerContainer() = default;
  22. void ServiceWorkerContainer::initialize(JS::Realm& realm)
  23. {
  24. Base::initialize(realm);
  25. WEB_SET_PROTOTYPE_FOR_INTERFACE(ServiceWorkerContainer);
  26. }
  27. void ServiceWorkerContainer::visit_edges(Cell::Visitor& visitor)
  28. {
  29. Base::visit_edges(visitor);
  30. visitor.visit(m_service_worker_client);
  31. }
  32. JS::NonnullGCPtr<ServiceWorkerContainer> ServiceWorkerContainer::create(JS::Realm& realm)
  33. {
  34. return realm.heap().allocate<ServiceWorkerContainer>(realm, realm);
  35. }
  36. // https://w3c.github.io/ServiceWorker/#navigator-service-worker-register
  37. JS::NonnullGCPtr<JS::Promise> ServiceWorkerContainer::register_(String script_url, RegistrationOptions const& options)
  38. {
  39. auto& realm = this->realm();
  40. // Note: The register(scriptURL, options) method creates or updates a service worker registration for the given scope url.
  41. // If successful, a service worker registration ties the provided scriptURL to a scope url,
  42. // which is subsequently used for navigation matching.
  43. // 1. Let p be a promise.
  44. auto p = WebIDL::create_promise(realm);
  45. // FIXME: 2. Set scriptURL to the result of invoking Get Trusted Type compliant string with TrustedScriptURL,
  46. // this's relevant global object, scriptURL, "ServiceWorkerContainer register", and "script".
  47. // 3 Let client be this's service worker client.
  48. auto client = m_service_worker_client;
  49. // 4. Let scriptURL be the result of parsing scriptURL with this's relevant settings object’s API base URL.
  50. auto base_url = relevant_settings_object(*this).api_base_url();
  51. auto parsed_script_url = DOMURL::parse(script_url, base_url);
  52. // 5. Let scopeURL be null.
  53. Optional<URL::URL> scope_url;
  54. // 6. If options["scope"] exists, set scopeURL to the result of parsing options["scope"] with this's relevant settings object’s API base URL.
  55. if (options.scope.has_value()) {
  56. scope_url = DOMURL::parse(options.scope.value(), base_url);
  57. }
  58. // 7. Invoke Start Register with scopeURL, scriptURL, p, client, client’s creation URL, options["type"], and options["updateViaCache"].
  59. start_register(scope_url, parsed_script_url, p, client, client->creation_url, options.type, options.update_via_cache);
  60. // 8. Return p.
  61. return verify_cast<JS::Promise>(*p->promise());
  62. }
  63. // https://w3c.github.io/ServiceWorker/#start-register-algorithm
  64. void ServiceWorkerContainer::start_register(Optional<URL::URL> scope_url, URL::URL script_url, JS::NonnullGCPtr<WebIDL::Promise> promise, EnvironmentSettingsObject& client, URL::URL, Bindings::WorkerType, Bindings::ServiceWorkerUpdateViaCache)
  65. {
  66. auto& realm = this->realm();
  67. auto& vm = realm.vm();
  68. // 1. If scriptURL is failure, reject promise with a TypeError and abort these steps.
  69. if (!script_url.is_valid()) {
  70. WebIDL::reject_promise(realm, promise, JS::TypeError::create(realm, "scriptURL is not a valid URL"sv));
  71. return;
  72. }
  73. // 2. Set scriptURL’s fragment to null.
  74. // Note: The user agent does not store the fragment of the script’s url.
  75. // This means that the fragment does not have an effect on identifying service workers.
  76. script_url.set_fragment({});
  77. // 3. If scriptURL’s scheme is not one of "http" and "https", reject promise with a TypeError and abort these steps.
  78. if (!script_url.scheme().is_one_of("http"sv, "https"sv)) {
  79. WebIDL::reject_promise(realm, promise, JS::TypeError::create(realm, "scriptURL must have a scheme of 'http' or 'https'"sv));
  80. return;
  81. }
  82. // 4. If any of the strings in scriptURL’s path contains either ASCII case-insensitive "%2f" or ASCII case-insensitive "%5c",
  83. // reject promise with a TypeError and abort these steps.
  84. auto invalid_path = script_url.paths().first_matching([&](auto& path) {
  85. return path.contains("%2f"sv, CaseSensitivity::CaseInsensitive) || path.contains("%5c"sv, CaseSensitivity::CaseInsensitive);
  86. });
  87. if (invalid_path.has_value()) {
  88. WebIDL::reject_promise(realm, promise, JS::TypeError::create(realm, "scriptURL path must not contain '%2f' or '%5c'"sv));
  89. return;
  90. }
  91. // 5. If scopeURL is null, set scopeURL to the result of parsing the string "./" with scriptURL.
  92. // Note: The scope url for the registration is set to the location of the service worker script by default.
  93. if (!scope_url.has_value()) {
  94. scope_url = DOMURL::parse("./"sv, script_url);
  95. }
  96. // 6. If scopeURL is failure, reject promise with a TypeError and abort these steps.
  97. if (!scope_url->is_valid()) {
  98. WebIDL::reject_promise(realm, promise, JS::TypeError::create(realm, "scopeURL is not a valid URL"sv));
  99. return;
  100. }
  101. // 7. Set scopeURL’s fragment to null.
  102. // Note: The user agent does not store the fragment of the scope url.
  103. // This means that the fragment does not have an effect on identifying service worker registrations.
  104. scope_url->set_fragment({});
  105. // 8. If scopeURL’s scheme is not one of "http" and "https", reject promise with a TypeError and abort these steps.
  106. if (!scope_url->scheme().is_one_of("http"sv, "https"sv)) {
  107. WebIDL::reject_promise(realm, promise, JS::TypeError::create(realm, "scopeURL must have a scheme of 'http' or 'https'"sv));
  108. return;
  109. }
  110. // 9. If any of the strings in scopeURL’s path contains either ASCII case-insensitive "%2f" or ASCII case-insensitive "%5c",
  111. // reject promise with a TypeError and abort these steps.
  112. invalid_path = scope_url->paths().first_matching([&](auto& path) {
  113. return path.contains("%2f"sv, CaseSensitivity::CaseInsensitive) || path.contains("%5c"sv, CaseSensitivity::CaseInsensitive);
  114. });
  115. if (invalid_path.has_value()) {
  116. WebIDL::reject_promise(realm, promise, JS::TypeError::create(realm, "scopeURL path must not contain '%2f' or '%5c'"sv));
  117. return;
  118. }
  119. // 10. Let storage key be the result of running obtain a storage key given client.
  120. auto storage_key = StorageAPI::obtain_a_storage_key(client);
  121. // FIXME: Ad-Hoc. Spec should handle this failure here, or earlier.
  122. if (!storage_key.has_value()) {
  123. WebIDL::reject_promise(realm, promise, JS::TypeError::create(realm, "Failed to obtain a storage key"sv));
  124. return;
  125. }
  126. // FIXME: Schedule the job
  127. // 11. Let job be the result of running Create Job with register, storage key, scopeURL, scriptURL, promise, and client.
  128. // 12. Set job’s worker type to workerType.
  129. // 13. Set job’s update via cache to updateViaCache.
  130. // 14. Set job’s referrer to referrer.
  131. // 15. Invoke Schedule Job with job.
  132. WebIDL::reject_promise(realm, promise, *vm.throw_completion<JS::InternalError>(JS::ErrorType::NotImplemented, "ServiceWorkerContainer::start_register"sv).value());
  133. }
  134. #undef __ENUMERATE
  135. #define __ENUMERATE(attribute_name, event_name) \
  136. void ServiceWorkerContainer::set_##attribute_name(WebIDL::CallbackType* value) \
  137. { \
  138. set_event_handler_attribute(event_name, move(value)); \
  139. } \
  140. WebIDL::CallbackType* ServiceWorkerContainer::attribute_name() \
  141. { \
  142. return event_handler_attribute(event_name); \
  143. }
  144. ENUMERATE_SERVICE_WORKER_CONTAINER_EVENT_HANDLERS(__ENUMERATE)
  145. #undef __ENUMERATE
  146. }