Job.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright (c) 2024, Andrew Kaster <andrew@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Types.h>
  8. #include <LibURL/URL.h>
  9. #include <LibWeb/Bindings/ServiceWorkerRegistrationPrototype.h>
  10. #include <LibWeb/Bindings/WorkerPrototype.h>
  11. #include <LibWeb/StorageAPI/StorageKey.h>
  12. namespace Web::ServiceWorker {
  13. struct Job;
  14. using JobQueue = JS::MarkedVector<JS::NonnullGCPtr<Job>>;
  15. // https://w3c.github.io/ServiceWorker/#dfn-job
  16. // FIXME: Consider not making this GC allocated, and give a special JobQueue class responsibility for its referenced GC objects
  17. struct Job : public JS::Cell {
  18. JS_CELL(Job, JS::Cell)
  19. JS_DECLARE_ALLOCATOR(Job);
  20. public:
  21. enum class Type : u8 {
  22. Register,
  23. Update,
  24. Unregister,
  25. };
  26. // https://w3c.github.io/ServiceWorker/#create-job
  27. static JS::NonnullGCPtr<Job> create(JS::VM&, Type, StorageAPI::StorageKey, URL::URL scope_url, URL::URL script_url, JS::GCPtr<WebIDL::Promise>, JS::GCPtr<HTML::EnvironmentSettingsObject> client);
  28. virtual ~Job() override;
  29. Type job_type; // https://w3c.github.io/ServiceWorker/#dfn-job-type
  30. StorageAPI::StorageKey storage_key; // https://w3c.github.io/ServiceWorker/#job-storage-key
  31. URL::URL scope_url;
  32. URL::URL script_url;
  33. Bindings::WorkerType worker_type = Bindings::WorkerType::Classic;
  34. // FIXME: The spec sometimes omits setting update_via_cache after CreateJob. Default to the default value for ServiceWorkerRegistrations
  35. Bindings::ServiceWorkerUpdateViaCache update_via_cache = Bindings::ServiceWorkerUpdateViaCache::Imports;
  36. JS::GCPtr<HTML::EnvironmentSettingsObject> client = nullptr;
  37. Optional<URL::URL> referrer;
  38. // FIXME: Spec just references this as an ECMAScript promise https://github.com/w3c/ServiceWorker/issues/1731
  39. JS::GCPtr<WebIDL::Promise> job_promise = nullptr;
  40. RawPtr<JobQueue> containing_job_queue = nullptr;
  41. Vector<JS::NonnullGCPtr<Job>> list_of_equivalent_jobs;
  42. bool force_cache_bypass = false;
  43. // https://w3c.github.io/ServiceWorker/#dfn-job-equivalent
  44. friend bool operator==(Job const& a, Job const& b)
  45. {
  46. if (a.job_type != b.job_type)
  47. return false;
  48. switch (a.job_type) {
  49. case Type::Register:
  50. case Type::Update:
  51. return a.scope_url == b.scope_url
  52. && a.script_url == b.script_url
  53. && a.worker_type == b.worker_type
  54. && a.update_via_cache == b.update_via_cache;
  55. case Type::Unregister:
  56. return a.scope_url == b.scope_url;
  57. }
  58. }
  59. private:
  60. virtual void visit_edges(JS::Cell::Visitor& visitor) override;
  61. Job(Type, StorageAPI::StorageKey, URL::URL scope_url, URL::URL script_url, JS::GCPtr<WebIDL::Promise>, JS::GCPtr<HTML::EnvironmentSettingsObject> client);
  62. };
  63. // https://w3c.github.io/ServiceWorker/#schedule-job-algorithm
  64. void schedule_job(JS::VM&, JS::NonnullGCPtr<Job>);
  65. }