Support container.getRegistration() / getRegistrations() inside service workers
[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     virtual void addServiceWorkerRegistrationInServer(ServiceWorkerRegistrationIdentifier) = 0;
66     virtual void removeServiceWorkerRegistrationInServer(ServiceWorkerRegistrationIdentifier) = 0;
67
68     void scheduleJob(ServiceWorkerJob&);
69     void finishedFetchingScript(ServiceWorkerJob&, const String&);
70     void failedFetchingScript(ServiceWorkerJob&, const ResourceError&);
71
72     virtual void didResolveRegistrationPromise(const ServiceWorkerRegistrationKey&) = 0;
73
74     virtual void postMessageToServiceWorker(ServiceWorkerIdentifier destination, Ref<SerializedScriptValue>&& message, ServiceWorkerClientIdentifier sourceIdentifier, ServiceWorkerClientData&& sourceData) = 0;
75     virtual void postMessageToServiceWorker(ServiceWorkerIdentifier destination, Ref<SerializedScriptValue>&& message, ServiceWorkerIdentifier source) = 0;
76
77     virtual SWServerConnectionIdentifier serverConnectionIdentifier() const = 0;
78     virtual bool mayHaveServiceWorkerRegisteredForOrigin(const SecurityOrigin&) const = 0;
79     virtual void syncTerminateWorker(ServiceWorkerIdentifier) = 0;
80
81     virtual void serviceWorkerStartedControllingClient(ServiceWorkerIdentifier, ServiceWorkerRegistrationIdentifier, DocumentIdentifier) = 0;
82     virtual void serviceWorkerStoppedControllingClient(ServiceWorkerIdentifier, ServiceWorkerRegistrationIdentifier, DocumentIdentifier) = 0;
83
84     virtual void registerServiceWorkerClient(const SecurityOrigin& topOrigin, DocumentIdentifier, const ServiceWorkerClientData&) = 0;
85     virtual void unregisterServiceWorkerClient(DocumentIdentifier) = 0;
86
87 protected:
88     WEBCORE_EXPORT SWClientConnection();
89
90     WEBCORE_EXPORT void jobRejectedInServer(const ServiceWorkerJobDataIdentifier&, const ExceptionData&);
91     WEBCORE_EXPORT void registrationJobResolvedInServer(const ServiceWorkerJobDataIdentifier&, ServiceWorkerRegistrationData&&, ShouldNotifyWhenResolved);
92     WEBCORE_EXPORT void unregistrationJobResolvedInServer(const ServiceWorkerJobDataIdentifier&, bool unregistrationResult);
93     WEBCORE_EXPORT void startScriptFetchForServer(const ServiceWorkerJobDataIdentifier&);
94     WEBCORE_EXPORT void postMessageToServiceWorkerClient(DocumentIdentifier destinationContextIdentifier, Ref<SerializedScriptValue>&& message, ServiceWorkerData&& source, const String& sourceOrigin);
95     WEBCORE_EXPORT void updateRegistrationState(ServiceWorkerRegistrationIdentifier, ServiceWorkerRegistrationState, const std::optional<ServiceWorkerData>&);
96     WEBCORE_EXPORT void updateWorkerState(ServiceWorkerIdentifier, ServiceWorkerState);
97     WEBCORE_EXPORT void fireUpdateFoundEvent(ServiceWorkerRegistrationIdentifier);
98     WEBCORE_EXPORT void notifyClientsOfControllerChange(const HashSet<DocumentIdentifier>& contextIdentifiers, ServiceWorkerData&& newController);
99
100     WEBCORE_EXPORT void clearPendingJobs();
101
102 private:
103     virtual void scheduleJobInServer(const ServiceWorkerJobData&) = 0;
104     virtual void finishFetchingScriptInServer(const ServiceWorkerFetchResult&) = 0;
105
106     HashMap<ServiceWorkerJobIdentifier, RefPtr<ServiceWorkerJob>> m_scheduledJobs;
107 };
108
109 } // namespace WebCore
110
111 #endif // ENABLE(SERVICE_WORKER)