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 : uint8_t; |
46 | enum class ServiceWorkerState : uint8_t; |
47 | enum class ShouldNotifyWhenResolved : bool; |
48 | struct ExceptionData; |
49 | struct MessageWithMessagePorts; |
50 | struct ServiceWorkerClientData; |
51 | struct ServiceWorkerClientIdentifier; |
52 | struct ServiceWorkerData; |
53 | struct ServiceWorkerFetchResult; |
54 | struct ServiceWorkerRegistrationData; |
55 | |
56 | class SWClientConnection : public ThreadSafeRefCounted<SWClientConnection> { |
57 | public: |
58 | WEBCORE_EXPORT virtual ~SWClientConnection(); |
59 | |
60 | using RegistrationCallback = WTF::CompletionHandler<void(Optional<ServiceWorkerRegistrationData>&&)>; |
61 | virtual void matchRegistration(SecurityOriginData&& topOrigin, const URL& clientURL, RegistrationCallback&&) = 0; |
62 | |
63 | using GetRegistrationsCallback = WTF::CompletionHandler<void(Vector<ServiceWorkerRegistrationData>&&)>; |
64 | virtual void getRegistrations(SecurityOriginData&& topOrigin, const URL& clientURL, GetRegistrationsCallback&&) = 0; |
65 | |
66 | using WhenRegistrationReadyCallback = WTF::Function<void(ServiceWorkerRegistrationData&&)>; |
67 | virtual void whenRegistrationReady(const SecurityOrigin& topOrigin, const URL& clientURL, WhenRegistrationReadyCallback&&) = 0; |
68 | |
69 | virtual void addServiceWorkerRegistrationInServer(ServiceWorkerRegistrationIdentifier) = 0; |
70 | virtual void removeServiceWorkerRegistrationInServer(ServiceWorkerRegistrationIdentifier) = 0; |
71 | |
72 | void scheduleJob(DocumentOrWorkerIdentifier, const ServiceWorkerJobData&); |
73 | void failedFetchingScript(ServiceWorkerJobIdentifier, const ServiceWorkerRegistrationKey&, const ResourceError&); |
74 | |
75 | virtual void didResolveRegistrationPromise(const ServiceWorkerRegistrationKey&) = 0; |
76 | |
77 | virtual void postMessageToServiceWorker(ServiceWorkerIdentifier destination, MessageWithMessagePorts&&, const ServiceWorkerOrClientIdentifier& source) = 0; |
78 | |
79 | virtual SWServerConnectionIdentifier serverConnectionIdentifier() const = 0; |
80 | virtual bool mayHaveServiceWorkerRegisteredForOrigin(const SecurityOriginData&) const = 0; |
81 | virtual void syncTerminateWorker(ServiceWorkerIdentifier) = 0; |
82 | |
83 | virtual void registerServiceWorkerClient(const SecurityOrigin& topOrigin, const ServiceWorkerClientData&, const Optional<ServiceWorkerRegistrationIdentifier>&, const String& userAgent) = 0; |
84 | virtual void unregisterServiceWorkerClient(DocumentIdentifier) = 0; |
85 | |
86 | virtual void finishFetchingScriptInServer(const ServiceWorkerFetchResult&) = 0; |
87 | |
88 | virtual bool isThrottleable() const = 0; |
89 | virtual void updateThrottleState() = 0; |
90 | |
91 | protected: |
92 | WEBCORE_EXPORT SWClientConnection(); |
93 | |
94 | WEBCORE_EXPORT void jobRejectedInServer(ServiceWorkerJobIdentifier, const ExceptionData&); |
95 | WEBCORE_EXPORT void registrationJobResolvedInServer(ServiceWorkerJobIdentifier, ServiceWorkerRegistrationData&&, ShouldNotifyWhenResolved); |
96 | WEBCORE_EXPORT void unregistrationJobResolvedInServer(ServiceWorkerJobIdentifier, bool unregistrationResult); |
97 | WEBCORE_EXPORT void startScriptFetchForServer(ServiceWorkerJobIdentifier, const ServiceWorkerRegistrationKey&, FetchOptions::Cache); |
98 | WEBCORE_EXPORT void postMessageToServiceWorkerClient(DocumentIdentifier destinationContextIdentifier, MessageWithMessagePorts&&, ServiceWorkerData&& source, String&& sourceOrigin); |
99 | WEBCORE_EXPORT void updateRegistrationState(ServiceWorkerRegistrationIdentifier, ServiceWorkerRegistrationState, const Optional<ServiceWorkerData>&); |
100 | WEBCORE_EXPORT void updateWorkerState(ServiceWorkerIdentifier, ServiceWorkerState); |
101 | WEBCORE_EXPORT void fireUpdateFoundEvent(ServiceWorkerRegistrationIdentifier); |
102 | WEBCORE_EXPORT void setRegistrationLastUpdateTime(ServiceWorkerRegistrationIdentifier, WallTime); |
103 | WEBCORE_EXPORT void setRegistrationUpdateViaCache(ServiceWorkerRegistrationIdentifier, ServiceWorkerUpdateViaCache); |
104 | WEBCORE_EXPORT void notifyClientsOfControllerChange(const HashSet<DocumentIdentifier>& contextIdentifiers, ServiceWorkerData&& newController); |
105 | |
106 | WEBCORE_EXPORT void clearPendingJobs(); |
107 | |
108 | private: |
109 | virtual void scheduleJobInServer(const ServiceWorkerJobData&) = 0; |
110 | |
111 | enum class IsJobComplete { No, Yes }; |
112 | bool postTaskForJob(ServiceWorkerJobIdentifier, IsJobComplete, WTF::Function<void(ServiceWorkerJob&)>&&); |
113 | |
114 | HashMap<ServiceWorkerJobIdentifier, DocumentOrWorkerIdentifier> m_scheduledJobSources; |
115 | }; |
116 | |
117 | } // namespace WebCore |
118 | |
119 | #endif // ENABLE(SERVICE_WORKER) |
120 | |