8cd495c1c90c717d83ddf7b3f13f651dbea6fd28
[WebKit-https.git] / Source / WebKit / NetworkProcess / ServiceWorker / WebSWServerConnection.h
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 "MessageReceiver.h"
31 #include "MessageSender.h"
32 #include "ServiceWorkerFetchTask.h"
33 #include <WebCore/FetchIdentifier.h>
34 #include <WebCore/ProcessIdentifier.h>
35 #include <WebCore/SWServer.h>
36 #include <pal/SessionID.h>
37 #include <wtf/HashMap.h>
38
39 namespace IPC {
40 class FormDataReference;
41 }
42
43 namespace WebCore {
44 class ServiceWorkerRegistrationKey;
45 struct ClientOrigin;
46 struct ExceptionData;
47 struct MessageWithMessagePorts;
48 struct ServiceWorkerClientData;
49 }
50
51 namespace WebKit {
52
53 class NetworkProcess;
54
55 class WebSWServerConnection : public WebCore::SWServer::Connection, public IPC::MessageSender, public IPC::MessageReceiver {
56 public:
57     WebSWServerConnection(NetworkProcess&, WebCore::SWServer&, IPC::Connection&, WebCore::ProcessIdentifier);
58     WebSWServerConnection(const WebSWServerConnection&) = delete;
59     ~WebSWServerConnection() final;
60
61     IPC::Connection& ipcConnection() const { return m_contentConnection.get(); }
62
63     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) final;
64     void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&);
65     
66     PAL::SessionID sessionID() const;
67
68 private:
69     // Implement SWServer::Connection (Messages to the client WebProcess)
70     void rejectJobInClient(WebCore::ServiceWorkerJobIdentifier, const WebCore::ExceptionData&) final;
71     void resolveRegistrationJobInClient(WebCore::ServiceWorkerJobIdentifier, const WebCore::ServiceWorkerRegistrationData&, WebCore::ShouldNotifyWhenResolved) final;
72     void resolveUnregistrationJobInClient(WebCore::ServiceWorkerJobIdentifier, const WebCore::ServiceWorkerRegistrationKey&, bool unregistrationResult) final;
73     void startScriptFetchInClient(WebCore::ServiceWorkerJobIdentifier, const WebCore::ServiceWorkerRegistrationKey&, WebCore::FetchOptions::Cache) final;
74     void updateRegistrationStateInClient(WebCore::ServiceWorkerRegistrationIdentifier, WebCore::ServiceWorkerRegistrationState, const Optional<WebCore::ServiceWorkerData>&) final;
75     void updateWorkerStateInClient(WebCore::ServiceWorkerIdentifier, WebCore::ServiceWorkerState) final;
76     void fireUpdateFoundEvent(WebCore::ServiceWorkerRegistrationIdentifier) final;
77     void setRegistrationLastUpdateTime(WebCore::ServiceWorkerRegistrationIdentifier, WallTime) final;
78     void setRegistrationUpdateViaCache(WebCore::ServiceWorkerRegistrationIdentifier, WebCore::ServiceWorkerUpdateViaCache) final;
79     void notifyClientsOfControllerChange(const HashSet<WebCore::DocumentIdentifier>& contextIdentifiers, const WebCore::ServiceWorkerData& newController);
80     void registrationReady(uint64_t registrationReadyRequestIdentifier, WebCore::ServiceWorkerRegistrationData&&) final;
81
82     void scheduleJobInServer(WebCore::ServiceWorkerJobData&&);
83
84     void startFetch(WebCore::ServiceWorkerRegistrationIdentifier, WebCore::FetchIdentifier, WebCore::ResourceRequest&&, WebCore::FetchOptions&&, IPC::FormDataReference&&, String&& referrer);
85     void cancelFetch(WebCore::ServiceWorkerRegistrationIdentifier, WebCore::FetchIdentifier);
86     void continueDidReceiveFetchResponse(WebCore::ServiceWorkerRegistrationIdentifier, WebCore::FetchIdentifier);
87
88     void matchRegistration(uint64_t registrationMatchRequestIdentifier, const WebCore::SecurityOriginData& topOrigin, const URL& clientURL);
89     void getRegistrations(uint64_t registrationMatchRequestIdentifier, const WebCore::SecurityOriginData& topOrigin, const URL& clientURL);
90
91     void registerServiceWorkerClient(WebCore::SecurityOriginData&& topOrigin, WebCore::ServiceWorkerClientData&&, const Optional<WebCore::ServiceWorkerRegistrationIdentifier>&, String&& userAgent);
92     void unregisterServiceWorkerClient(const WebCore::ServiceWorkerClientIdentifier&);
93     void syncTerminateWorkerFromClient(WebCore::ServiceWorkerIdentifier&&, CompletionHandler<void()>&&);
94
95     void postMessageToServiceWorkerClient(WebCore::DocumentIdentifier destinationContextIdentifier, const WebCore::MessageWithMessagePorts&, WebCore::ServiceWorkerIdentifier sourceServiceWorkerIdentifier, const String& sourceOrigin) final;
96
97     void contextConnectionCreated(WebCore::SWServerToContextConnection&) final;
98
99     bool isThrottleable() const { return m_isThrottleable; }
100     bool hasMatchingClient(const WebCore::RegistrableDomain&) const;
101     bool computeThrottleState(const WebCore::RegistrableDomain&) const;
102     void setThrottleState(bool isThrottleable);
103     void updateThrottleState();
104
105     void postMessageToServiceWorker(WebCore::ServiceWorkerIdentifier destination, WebCore::MessageWithMessagePorts&&, const WebCore::ServiceWorkerOrClientIdentifier& source);
106
107     IPC::Connection* messageSenderConnection() const final { return m_contentConnection.ptr(); }
108     uint64_t messageSenderDestinationID() const final { return 0; }
109     
110     template<typename U> static void sendToContextProcess(WebCore::SWServerToContextConnection&, U&& message);
111
112     Ref<IPC::Connection> m_contentConnection;
113     Ref<NetworkProcess> m_networkProcess;
114     HashMap<WebCore::ServiceWorkerClientIdentifier, WebCore::ClientOrigin> m_clientOrigins;
115     bool m_isThrottleable { true };
116 };
117
118 } // namespace WebKit
119
120 #endif // ENABLE(SERVICE_WORKER)