ServiceWorkerContainer.cpp 8.4 KB

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