Support updating a service worker registration's updateViaCache flag
[WebKit-https.git] / Source / WebCore / workers / service / SWClientConnection.h
1 /*
2  * Copyright (C) 2017 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #pragma once
27
28 #if ENABLE(SERVICE_WORKER)
29
30 #include "DocumentIdentifier.h"
31 #include "ServiceWorkerJob.h"
32 #include "ServiceWorkerTypes.h"
33 #include <wtf/CompletionHandler.h>
34 #include <wtf/HashMap.h>
35 #include <wtf/ThreadSafeRefCounted.h>
36
37 namespace WebCore {
38
39 class ResourceError;
40 class SecurityOrigin;
41 class SerializedScriptValue;
42 class ServiceWorkerContainer;
43 class ServiceWorkerRegistration;
44 class SharedBuffer;
45 enum class ServiceWorkerRegistrationState;
46 enum class ServiceWorkerState;
47 enum class ShouldNotifyWhenResolved;
48 struct ExceptionData;
49 struct ServiceWorkerClientData;
50 struct ServiceWorkerClientIdentifier;
51 struct ServiceWorkerData;
52 struct ServiceWorkerFetchResult;
53 struct ServiceWorkerRegistrationData;
54
55 class SWClientConnection : public ThreadSafeRefCounted<SWClientConnection> {
56 public:
57     WEBCORE_EXPORT virtual ~SWClientConnection();
58
59     using RegistrationCallback = WTF::CompletionHandler<void(std::optional<ServiceWorkerRegistrationData>&&)>;
60     virtual void matchRegistration(const SecurityOrigin& topOrigin, const URL& clientURL, RegistrationCallback&&) = 0;
61
62     using GetRegistrationsCallback = WTF::CompletionHandler<void(Vector<ServiceWorkerRegistrationData>&&)>;
63     virtual void getRegistrations(const SecurityOrigin& topOrigin, const URL& clientURL, GetRegistrationsCallback&&) = 0;
64
65     using WhenRegistrationReadyCallback = WTF::Function<void(ServiceWorkerRegistrationData&&)>;
66     virtual void whenRegistrationReady(const SecurityOrigin& topOrigin, const URL& clientURL, WhenRegistrationReadyCallback&&) = 0;
67
68     virtual void addServiceWorkerRegistrationInServer(ServiceWorkerRegistrationIdentifier) = 0;
69     virtual void removeServiceWorkerRegistrationInServer(ServiceWorkerRegistrationIdentifier) = 0;
70
71     void scheduleJob(ServiceWorkerJob&);
72     void finishedFetchingScript(ServiceWorkerJob&, const String&);
73     void failedFetchingScript(ServiceWorkerJob&, const ResourceError&);
74
75     virtual void didResolveRegistrationPromise(const ServiceWorkerRegistrationKey&) = 0;
76
77     virtual void postMessageToServiceWorker(ServiceWorkerIdentifier destination, Ref<SerializedScriptValue>&& message, const ServiceWorkerOrClientIdentifier& source) = 0;
78
79     virtual SWServerConnectionIdentifier serverConnectionIdentifier() const = 0;
80     virtual bool mayHaveServiceWorkerRegisteredForOrigin(const SecurityOrigin&) const = 0;
81     virtual void syncTerminateWorker(ServiceWorkerIdentifier) = 0;
82
83     virtual void registerServiceWorkerClient(const SecurityOrigin& topOrigin, const ServiceWorkerClientData&, const std::optional<ServiceWorkerIdentifier>&) = 0;
84     virtual void unregisterServiceWorkerClient(DocumentIdentifier) = 0;
85
86 protected:
87     WEBCORE_EXPORT SWClientConnection();
88
89     WEBCORE_EXPORT void jobRejectedInServer(const ServiceWorkerJobDataIdentifier&, const ExceptionData&);
90     WEBCORE_EXPORT void registrationJobResolvedInServer(const ServiceWorkerJobDataIdentifier&, ServiceWorkerRegistrationData&&, ShouldNotifyWhenResolved);
91     WEBCORE_EXPORT void unregistrationJobResolvedInServer(const ServiceWorkerJobDataIdentifier&, bool unregistrationResult);
92     WEBCORE_EXPORT void startScriptFetchForServer(const ServiceWorkerJobDataIdentifier&, FetchOptions::Cache);
93     WEBCORE_EXPORT void postMessageToServiceWorkerClient(DocumentIdentifier destinationContextIdentifier, Ref<SerializedScriptValue>&& message, ServiceWorkerData&& source, const String& sourceOrigin);
94     WEBCORE_EXPORT void updateRegistrationState(ServiceWorkerRegistrationIdentifier, ServiceWorkerRegistrationState, const std::optional<ServiceWorkerData>&);
95     WEBCORE_EXPORT void updateWorkerState(ServiceWorkerIdentifier, ServiceWorkerState);
96     WEBCORE_EXPORT void fireUpdateFoundEvent(ServiceWorkerRegistrationIdentifier);
97     WEBCORE_EXPORT void setRegistrationLastUpdateTime(ServiceWorkerRegistrationIdentifier, WallTime);
98     WEBCORE_EXPORT void setRegistrationUpdateViaCache(ServiceWorkerRegistrationIdentifier, ServiceWorkerUpdateViaCache);
99     WEBCORE_EXPORT void notifyClientsOfControllerChange(const HashSet<DocumentIdentifier>& contextIdentifiers, ServiceWorkerData&& newController);
100
101     WEBCORE_EXPORT void clearPendingJobs();
102
103 private:
104     virtual void scheduleJobInServer(const ServiceWorkerJobData&) = 0;
105     virtual void finishFetchingScriptInServer(const ServiceWorkerFetchResult&) = 0;
106
107     HashMap<ServiceWorkerJobIdentifier, RefPtr<ServiceWorkerJob>> m_scheduledJobs;
108 };
109
110 } // namespace WebCore
111
112 #endif // ENABLE(SERVICE_WORKER)