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 "ExceptionOr.h"
31#include "ServiceWorkerClientData.h"
32#include "ServiceWorkerClientQueryOptions.h"
33#include "ServiceWorkerIdentifier.h"
34#include "ServiceWorkerThreadProxy.h"
35#include <wtf/CompletionHandler.h>
36#include <wtf/HashMap.h>
37
38namespace WebCore {
39
40class SerializedScriptValue;
41class ServiceWorkerGlobalScope;
42
43class SWContextManager {
44public:
45 WEBCORE_EXPORT static SWContextManager& singleton();
46
47 class Connection {
48 WTF_MAKE_FAST_ALLOCATED;
49 public:
50 virtual ~Connection() { }
51
52 virtual void postMessageToServiceWorkerClient(const ServiceWorkerClientIdentifier& destinationIdentifier, MessageWithMessagePorts&&, ServiceWorkerIdentifier source, const String& sourceOrigin) = 0;
53 virtual void serviceWorkerStartedWithMessage(Optional<ServiceWorkerJobDataIdentifier>, ServiceWorkerIdentifier, const String& exceptionMessage) = 0;
54 virtual void didFinishInstall(Optional<ServiceWorkerJobDataIdentifier>, ServiceWorkerIdentifier, bool wasSuccessful) = 0;
55 virtual void didFinishActivation(ServiceWorkerIdentifier) = 0;
56 virtual void setServiceWorkerHasPendingEvents(ServiceWorkerIdentifier, bool) = 0;
57 virtual void workerTerminated(ServiceWorkerIdentifier) = 0;
58 virtual void skipWaiting(ServiceWorkerIdentifier, Function<void()>&&) = 0;
59 virtual void setScriptResource(ServiceWorkerIdentifier, const URL&, const ServiceWorkerContextData::ImportedScript&) = 0;
60
61 using FindClientByIdentifierCallback = CompletionHandler<void(ExceptionOr<Optional<ServiceWorkerClientData>>&&)>;
62 virtual void findClientByIdentifier(ServiceWorkerIdentifier, ServiceWorkerClientIdentifier, FindClientByIdentifierCallback&&) = 0;
63 virtual void matchAll(ServiceWorkerIdentifier, const ServiceWorkerClientQueryOptions&, ServiceWorkerClientsMatchAllCallback&&) = 0;
64 virtual void claim(ServiceWorkerIdentifier, CompletionHandler<void()>&&) = 0;
65
66 virtual bool isThrottleable() const = 0;
67 };
68
69 WEBCORE_EXPORT void setConnection(std::unique_ptr<Connection>&&);
70 WEBCORE_EXPORT Connection* connection() const;
71
72 WEBCORE_EXPORT void registerServiceWorkerThreadForInstall(Ref<ServiceWorkerThreadProxy>&&);
73 WEBCORE_EXPORT ServiceWorkerThreadProxy* serviceWorkerThreadProxy(ServiceWorkerIdentifier) const;
74 WEBCORE_EXPORT void postMessageToServiceWorker(ServiceWorkerIdentifier destination, MessageWithMessagePorts&&, ServiceWorkerOrClientData&& sourceData);
75 WEBCORE_EXPORT void fireInstallEvent(ServiceWorkerIdentifier);
76 WEBCORE_EXPORT void fireActivateEvent(ServiceWorkerIdentifier);
77 WEBCORE_EXPORT void terminateWorker(ServiceWorkerIdentifier, Seconds timeout, Function<void()>&&);
78
79 void forEachServiceWorkerThread(const WTF::Function<void(ServiceWorkerThreadProxy&)>&);
80
81 WEBCORE_EXPORT bool postTaskToServiceWorker(ServiceWorkerIdentifier, WTF::Function<void(ServiceWorkerGlobalScope&)>&&);
82
83 using ServiceWorkerCreationCallback = void(uint64_t);
84 void setServiceWorkerCreationCallback(ServiceWorkerCreationCallback* callback) { m_serviceWorkerCreationCallback = callback; }
85
86 ServiceWorkerThreadProxy* workerByID(ServiceWorkerIdentifier identifier) { return m_workerMap.get(identifier); }
87
88private:
89 SWContextManager() = default;
90
91 void startedServiceWorker(Optional<ServiceWorkerJobDataIdentifier>, ServiceWorkerIdentifier, const String& exceptionMessage);
92 NO_RETURN_DUE_TO_CRASH void serviceWorkerFailedToTerminate(ServiceWorkerIdentifier);
93
94 HashMap<ServiceWorkerIdentifier, RefPtr<ServiceWorkerThreadProxy>> m_workerMap;
95 std::unique_ptr<Connection> m_connection;
96 ServiceWorkerCreationCallback* m_serviceWorkerCreationCallback { nullptr };
97
98 class ServiceWorkerTerminationRequest {
99 WTF_MAKE_FAST_ALLOCATED;
100 public:
101 ServiceWorkerTerminationRequest(SWContextManager&, ServiceWorkerIdentifier, Seconds timeout);
102
103 private:
104 Timer m_timeoutTimer;
105 };
106 HashMap<ServiceWorkerIdentifier, std::unique_ptr<ServiceWorkerTerminationRequest>> m_pendingServiceWorkerTerminationRequests;
107};
108
109} // namespace WebCore
110
111#endif // ENABLE(SERVICE_WORKER)
112