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
37namespace WebCore {
38
39class ResourceError;
40class SecurityOrigin;
41class SerializedScriptValue;
42class ServiceWorkerContainer;
43class ServiceWorkerRegistration;
44class SharedBuffer;
45enum class ServiceWorkerRegistrationState : uint8_t;
46enum class ServiceWorkerState : uint8_t;
47enum class ShouldNotifyWhenResolved : bool;
48struct ExceptionData;
49struct MessageWithMessagePorts;
50struct ServiceWorkerClientData;
51struct ServiceWorkerClientIdentifier;
52struct ServiceWorkerData;
53struct ServiceWorkerFetchResult;
54struct ServiceWorkerRegistrationData;
55
56class SWClientConnection : public ThreadSafeRefCounted<SWClientConnection> {
57public:
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 virtual void storeRegistrationsOnDiskForTesting(CompletionHandler<void()>&& callback) { callback(); }
92
93protected:
94 WEBCORE_EXPORT SWClientConnection();
95
96 WEBCORE_EXPORT void jobRejectedInServer(ServiceWorkerJobIdentifier, const ExceptionData&);
97 WEBCORE_EXPORT void registrationJobResolvedInServer(ServiceWorkerJobIdentifier, ServiceWorkerRegistrationData&&, ShouldNotifyWhenResolved);
98 WEBCORE_EXPORT void unregistrationJobResolvedInServer(ServiceWorkerJobIdentifier, bool unregistrationResult);
99 WEBCORE_EXPORT void startScriptFetchForServer(ServiceWorkerJobIdentifier, const ServiceWorkerRegistrationKey&, FetchOptions::Cache);
100 WEBCORE_EXPORT void postMessageToServiceWorkerClient(DocumentIdentifier destinationContextIdentifier, MessageWithMessagePorts&&, ServiceWorkerData&& source, String&& sourceOrigin);
101 WEBCORE_EXPORT void updateRegistrationState(ServiceWorkerRegistrationIdentifier, ServiceWorkerRegistrationState, const Optional<ServiceWorkerData>&);
102 WEBCORE_EXPORT void updateWorkerState(ServiceWorkerIdentifier, ServiceWorkerState);
103 WEBCORE_EXPORT void fireUpdateFoundEvent(ServiceWorkerRegistrationIdentifier);
104 WEBCORE_EXPORT void setRegistrationLastUpdateTime(ServiceWorkerRegistrationIdentifier, WallTime);
105 WEBCORE_EXPORT void setRegistrationUpdateViaCache(ServiceWorkerRegistrationIdentifier, ServiceWorkerUpdateViaCache);
106 WEBCORE_EXPORT void notifyClientsOfControllerChange(const HashSet<DocumentIdentifier>& contextIdentifiers, ServiceWorkerData&& newController);
107
108 WEBCORE_EXPORT void clearPendingJobs();
109
110private:
111 virtual void scheduleJobInServer(const ServiceWorkerJobData&) = 0;
112
113 enum class IsJobComplete { No, Yes };
114 bool postTaskForJob(ServiceWorkerJobIdentifier, IsJobComplete, WTF::Function<void(ServiceWorkerJob&)>&&);
115
116 HashMap<ServiceWorkerJobIdentifier, DocumentOrWorkerIdentifier> m_scheduledJobSources;
117};
118
119} // namespace WebCore
120
121#endif // ENABLE(SERVICE_WORKER)
122