| 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 | |
| 40 | namespace WebCore { |
| 41 | struct ExceptionData; |
| 42 | class ResourceLoader; |
| 43 | } |
| 44 | |
| 45 | namespace WebKit { |
| 46 | |
| 47 | class WebSWOriginTable; |
| 48 | class WebServiceWorkerProvider; |
| 49 | |
| 50 | class WebSWClientConnection final : public WebCore::SWClientConnection, public IPC::MessageSender, public IPC::MessageReceiver { |
| 51 | public: |
| 52 | static Ref<WebSWClientConnection> create(IPC::Connection& connection, PAL::SessionID sessionID) { return adoptRef(*new WebSWClientConnection { connection, sessionID }); } |
| 53 | ~WebSWClientConnection(); |
| 54 | |
| 55 | WebCore::SWServerConnectionIdentifier serverConnectionIdentifier() const final { return m_identifier; } |
| 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 | private: |
| 73 | WebSWClientConnection(IPC::Connection&, PAL::SessionID); |
| 74 | |
| 75 | void scheduleJobInServer(const WebCore::ServiceWorkerJobData&) final; |
| 76 | void finishFetchingScriptInServer(const WebCore::ServiceWorkerFetchResult&) final; |
| 77 | void postMessageToServiceWorker(WebCore::ServiceWorkerIdentifier destinationIdentifier, WebCore::MessageWithMessagePorts&&, const WebCore::ServiceWorkerOrClientIdentifier& source) final; |
| 78 | void registerServiceWorkerClient(const WebCore::SecurityOrigin& topOrigin, const WebCore::ServiceWorkerClientData&, const Optional<WebCore::ServiceWorkerRegistrationIdentifier>&, const String& userAgent) final; |
| 79 | void unregisterServiceWorkerClient(WebCore::DocumentIdentifier) final; |
| 80 | |
| 81 | void matchRegistration(WebCore::SecurityOriginData&& topOrigin, const URL& clientURL, RegistrationCallback&&) final; |
| 82 | void didMatchRegistration(uint64_t matchRequestIdentifier, Optional<WebCore::ServiceWorkerRegistrationData>&&); |
| 83 | void didGetRegistrations(uint64_t matchRequestIdentifier, Vector<WebCore::ServiceWorkerRegistrationData>&&); |
| 84 | void whenRegistrationReady(const WebCore::SecurityOrigin& topOrigin, const URL& clientURL, WhenRegistrationReadyCallback&&) final; |
| 85 | void registrationReady(uint64_t callbackID, WebCore::ServiceWorkerRegistrationData&&); |
| 86 | |
| 87 | void getRegistrations(WebCore::SecurityOriginData&& topOrigin, const URL& clientURL, GetRegistrationsCallback&&) final; |
| 88 | |
| 89 | void didResolveRegistrationPromise(const WebCore::ServiceWorkerRegistrationKey&) final; |
| 90 | void updateThrottleState() final; |
| 91 | bool isThrottleable() const final { return m_isThrottleable; } |
| 92 | |
| 93 | void scheduleStorageJob(const WebCore::ServiceWorkerJobData&); |
| 94 | |
| 95 | void runOrDelayTaskForImport(WTF::Function<void()>&& task); |
| 96 | |
| 97 | IPC::Connection* messageSenderConnection() const final { return m_connection.ptr(); } |
| 98 | uint64_t messageSenderDestinationID() const final { return m_identifier.toUInt64(); } |
| 99 | |
| 100 | void setSWOriginTableSharedMemory(const SharedMemory::Handle&); |
| 101 | void setSWOriginTableIsImported(); |
| 102 | |
| 103 | PAL::SessionID m_sessionID; |
| 104 | WebCore::SWServerConnectionIdentifier m_identifier; |
| 105 | |
| 106 | Ref<IPC::Connection> m_connection; |
| 107 | UniqueRef<WebSWOriginTable> m_swOriginTable; |
| 108 | |
| 109 | uint64_t m_previousCallbackIdentifier { 0 }; |
| 110 | HashMap<uint64_t, RegistrationCallback> m_ongoingMatchRegistrationTasks; |
| 111 | HashMap<uint64_t, GetRegistrationsCallback> m_ongoingGetRegistrationsTasks; |
| 112 | HashMap<uint64_t, WhenRegistrationReadyCallback> m_ongoingRegistrationReadyTasks; |
| 113 | Deque<WTF::Function<void()>> m_tasksPendingOriginImport; |
| 114 | bool m_isThrottleable { true }; |
| 115 | }; // class WebSWServerConnection |
| 116 | |
| 117 | } // namespace WebKit |
| 118 | |
| 119 | #endif // ENABLE(SERVICE_WORKER) |
| 120 | |