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 "Connection.h"
31#include "MessageReceiver.h"
32#include "MessageSender.h"
33#include "ServiceWorkerClientFetch.h"
34#include "SharedMemory.h"
35#include <WebCore/MessageWithMessagePorts.h>
36#include <WebCore/SWClientConnection.h>
37#include <pal/SessionID.h>
38#include <wtf/UniqueRef.h>
39
40namespace WebCore {
41struct ExceptionData;
42class ResourceLoader;
43}
44
45namespace WebKit {
46
47class WebSWOriginTable;
48class WebServiceWorkerProvider;
49
50class WebSWClientConnection final : public WebCore::SWClientConnection, private IPC::MessageSender, public IPC::MessageReceiver {
51public:
52 static Ref<WebSWClientConnection> create(PAL::SessionID sessionID) { return adoptRef(*new WebSWClientConnection { sessionID }); }
53 ~WebSWClientConnection();
54
55 WebCore::SWServerConnectionIdentifier serverConnectionIdentifier() const final;
56
57 void addServiceWorkerRegistrationInServer(WebCore::ServiceWorkerRegistrationIdentifier) final;
58 void removeServiceWorkerRegistrationInServer(WebCore::ServiceWorkerRegistrationIdentifier) final;
59
60 void disconnectedFromWebProcess();
61 void didReceiveMessage(IPC::Connection&, IPC::Decoder&) final;
62
63 bool mayHaveServiceWorkerRegisteredForOrigin(const WebCore::SecurityOriginData&) const final;
64 void startFetch(WebCore::FetchIdentifier, WebCore::ServiceWorkerRegistrationIdentifier, const WebCore::ResourceRequest&, const WebCore::FetchOptions&, const String& referrer);
65 void cancelFetch(WebCore::FetchIdentifier, WebCore::ServiceWorkerRegistrationIdentifier);
66 void continueDidReceiveFetchResponse(WebCore::FetchIdentifier, WebCore::ServiceWorkerRegistrationIdentifier);
67
68 void connectionToServerLost();
69
70 void syncTerminateWorker(WebCore::ServiceWorkerIdentifier) final;
71
72 PAL::SessionID sessionID() const { return m_sessionID; }
73
74private:
75 explicit WebSWClientConnection(PAL::SessionID);
76
77 void initializeConnectionIfNeeded();
78
79 void scheduleJobInServer(const WebCore::ServiceWorkerJobData&) final;
80 void finishFetchingScriptInServer(const WebCore::ServiceWorkerFetchResult&) final;
81 void postMessageToServiceWorker(WebCore::ServiceWorkerIdentifier destinationIdentifier, WebCore::MessageWithMessagePorts&&, const WebCore::ServiceWorkerOrClientIdentifier& source) final;
82 void registerServiceWorkerClient(const WebCore::SecurityOrigin& topOrigin, const WebCore::ServiceWorkerClientData&, const Optional<WebCore::ServiceWorkerRegistrationIdentifier>&, const String& userAgent) final;
83 void unregisterServiceWorkerClient(WebCore::DocumentIdentifier) final;
84
85 void matchRegistration(WebCore::SecurityOriginData&& topOrigin, const URL& clientURL, RegistrationCallback&&) final;
86 void didMatchRegistration(uint64_t matchRequestIdentifier, Optional<WebCore::ServiceWorkerRegistrationData>&&);
87 void didGetRegistrations(uint64_t matchRequestIdentifier, Vector<WebCore::ServiceWorkerRegistrationData>&&);
88 void whenRegistrationReady(const WebCore::SecurityOrigin& topOrigin, const URL& clientURL, WhenRegistrationReadyCallback&&) final;
89 void registrationReady(uint64_t callbackID, WebCore::ServiceWorkerRegistrationData&&);
90
91 void getRegistrations(WebCore::SecurityOriginData&& topOrigin, const URL& clientURL, GetRegistrationsCallback&&) final;
92
93 void didResolveRegistrationPromise(const WebCore::ServiceWorkerRegistrationKey&) final;
94 void updateThrottleState() final;
95 bool isThrottleable() const final { return m_isThrottleable; }
96 void storeRegistrationsOnDiskForTesting(CompletionHandler<void()>&&) final;
97
98 void scheduleStorageJob(const WebCore::ServiceWorkerJobData&);
99
100 void runOrDelayTaskForImport(WTF::Function<void()>&& task);
101
102 IPC::Connection* messageSenderConnection() const final { return m_connection.get(); }
103 uint64_t messageSenderDestinationID() const final { return m_identifier.toUInt64(); }
104
105 void setSWOriginTableSharedMemory(const SharedMemory::Handle&);
106 void setSWOriginTableIsImported();
107
108 template<typename U> void ensureConnectionAndSend(const U& message);
109
110 PAL::SessionID m_sessionID;
111 WebCore::SWServerConnectionIdentifier m_identifier;
112
113 RefPtr<IPC::Connection> m_connection;
114 UniqueRef<WebSWOriginTable> m_swOriginTable;
115
116 uint64_t m_previousCallbackIdentifier { 0 };
117 HashMap<uint64_t, RegistrationCallback> m_ongoingMatchRegistrationTasks;
118 HashMap<uint64_t, GetRegistrationsCallback> m_ongoingGetRegistrationsTasks;
119 HashMap<uint64_t, WhenRegistrationReadyCallback> m_ongoingRegistrationReadyTasks;
120 Deque<WTF::Function<void()>> m_tasksPendingOriginImport;
121 bool m_isThrottleable { true };
122}; // class WebSWServerConnection
123
124} // namespace WebKit
125
126#endif // ENABLE(SERVICE_WORKER)
127