Coverage Report

Created: 2026-02-16 07:47

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