Drop FrameLoaderClient::sessionID()
[WebKit-https.git] / Source / WebKit / WebProcess / Storage / WebSWContextManagerConnection.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 "Connection.h"
31 #include "MessageReceiver.h"
32 #include "WebPageProxyIdentifier.h"
33 #include "WebSWContextManagerConnectionMessages.h"
34 #include <WebCore/EmptyFrameLoaderClient.h>
35 #include <WebCore/SWContextManager.h>
36 #include <WebCore/ServiceWorkerClientData.h>
37 #include <WebCore/ServiceWorkerTypes.h>
38
39 namespace IPC {
40 class FormDataReference;
41 }
42
43 namespace WebCore {
44 struct FetchOptions;
45 class ResourceRequest;
46 struct ServiceWorkerContextData;
47 }
48
49 namespace WebKit {
50
51 class ServiceWorkerFrameLoaderClient;
52 struct WebPreferencesStore;
53
54 class WebSWContextManagerConnection final : public WebCore::SWContextManager::Connection, public IPC::MessageReceiver {
55 public:
56     WebSWContextManagerConnection(Ref<IPC::Connection>&&, WebCore::RegistrableDomain&&, PAL::SessionID, uint64_t pageGroupID, WebPageProxyIdentifier, WebCore::PageIdentifier, const WebPreferencesStore&);
57     ~WebSWContextManagerConnection();
58
59     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) final;
60     void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&) final;
61
62     void removeFrameLoaderClient(ServiceWorkerFrameLoaderClient&);
63
64 private:
65     void updatePreferencesStore(const WebPreferencesStore&);
66
67     // WebCore::SWContextManager::Connection.
68     void postMessageToServiceWorkerClient(const WebCore::ServiceWorkerClientIdentifier& destinationIdentifier, const WebCore::MessageWithMessagePorts&, WebCore::ServiceWorkerIdentifier sourceIdentifier, const String& sourceOrigin) final;
69     void didFinishInstall(Optional<WebCore::ServiceWorkerJobDataIdentifier>, WebCore::ServiceWorkerIdentifier, bool wasSuccessful) final;
70     void didFinishActivation(WebCore::ServiceWorkerIdentifier) final;
71     void setServiceWorkerHasPendingEvents(WebCore::ServiceWorkerIdentifier, bool) final;
72     void workerTerminated(WebCore::ServiceWorkerIdentifier) final;
73     void findClientByIdentifier(WebCore::ServiceWorkerIdentifier, WebCore::ServiceWorkerClientIdentifier, FindClientByIdentifierCallback&&) final;
74     void matchAll(WebCore::ServiceWorkerIdentifier, const WebCore::ServiceWorkerClientQueryOptions&, WebCore::ServiceWorkerClientsMatchAllCallback&&) final;
75     void claim(WebCore::ServiceWorkerIdentifier, CompletionHandler<void()>&&) final;
76     void skipWaiting(WebCore::ServiceWorkerIdentifier, Function<void()>&&) final;
77     void setScriptResource(WebCore::ServiceWorkerIdentifier, const URL&, const WebCore::ServiceWorkerContextData::ImportedScript&) final;
78     bool isThrottleable() const final;
79
80     // IPC messages.
81     void serviceWorkerStarted(Optional<WebCore::ServiceWorkerJobDataIdentifier>, WebCore::ServiceWorkerIdentifier, bool doesHandleFetch) final;
82     void serviceWorkerFailedToStart(Optional<WebCore::ServiceWorkerJobDataIdentifier>, WebCore::ServiceWorkerIdentifier, const String& exceptionMessage) final;
83     void installServiceWorker(const WebCore::ServiceWorkerContextData&, PAL::SessionID, String&& userAgent);
84     void startFetch(WebCore::SWServerConnectionIdentifier, WebCore::ServiceWorkerIdentifier, WebCore::FetchIdentifier, WebCore::ResourceRequest&&, WebCore::FetchOptions&&, IPC::FormDataReference&&, String&& referrer);
85     void cancelFetch(WebCore::SWServerConnectionIdentifier, WebCore::ServiceWorkerIdentifier, WebCore::FetchIdentifier);
86     void continueDidReceiveFetchResponse(WebCore::SWServerConnectionIdentifier, WebCore::ServiceWorkerIdentifier, WebCore::FetchIdentifier);
87     void postMessageToServiceWorker(WebCore::ServiceWorkerIdentifier destinationIdentifier, WebCore::MessageWithMessagePorts&&, WebCore::ServiceWorkerOrClientData&& sourceData);
88     void fireInstallEvent(WebCore::ServiceWorkerIdentifier);
89     void fireActivateEvent(WebCore::ServiceWorkerIdentifier);
90     void softUpdate(WebCore::ServiceWorkerIdentifier);
91     void terminateWorker(WebCore::ServiceWorkerIdentifier);
92     void syncTerminateWorker(WebCore::ServiceWorkerIdentifier, Messages::WebSWContextManagerConnection::SyncTerminateWorker::DelayedReply&&);
93     void findClientByIdentifierCompleted(uint64_t requestIdentifier, Optional<WebCore::ServiceWorkerClientData>&&, bool hasSecurityError);
94     void matchAllCompleted(uint64_t matchAllRequestIdentifier, Vector<WebCore::ServiceWorkerClientData>&&);
95     void claimCompleted(uint64_t claimRequestIdentifier);
96     void didFinishSkipWaiting(uint64_t callbackID);
97     void setUserAgent(String&& userAgent);
98     NO_RETURN void terminateProcess();
99     void setThrottleState(bool isThrottleable);
100
101     Ref<IPC::Connection> m_connectionToNetworkProcess;
102     WebCore::RegistrableDomain m_registrableDomain;
103     uint64_t m_pageGroupID;
104     WebPageProxyIdentifier m_webPageProxyID;
105     WebCore::PageIdentifier m_pageID;
106     uint64_t m_previousServiceWorkerID { 0 };
107
108     WebCore::SecurityOrigin::StorageBlockingPolicy m_storageBlockingPolicy { WebCore::SecurityOrigin::StorageBlockingPolicy::AllowAllStorage };
109
110     HashSet<std::unique_ptr<ServiceWorkerFrameLoaderClient>> m_loaders;
111     HashMap<uint64_t, FindClientByIdentifierCallback> m_findClientByIdentifierRequests;
112     HashMap<uint64_t, WebCore::ServiceWorkerClientsMatchAllCallback> m_matchAllRequests;
113     HashMap<uint64_t, WTF::CompletionHandler<void()>> m_claimRequests;
114     HashMap<uint64_t, WTF::Function<void()>> m_skipWaitingRequests;
115     uint64_t m_previousRequestIdentifier { 0 };
116     String m_userAgent;
117     bool m_isThrottleable { true };
118 };
119
120 class ServiceWorkerFrameLoaderClient final : public WebCore::EmptyFrameLoaderClient {
121 public:
122     ServiceWorkerFrameLoaderClient(WebSWContextManagerConnection&, WebPageProxyIdentifier, WebCore::PageIdentifier, WebCore::FrameIdentifier, const String& userAgent);
123
124     void setUserAgent(String&& userAgent) { m_userAgent = WTFMove(userAgent); }
125     
126     WebPageProxyIdentifier webPageProxyID() const { return m_webPageProxyID; }
127     Optional<WebCore::PageIdentifier> pageID() const final { return m_pageID; }
128     Optional<WebCore::FrameIdentifier> frameID() const final { return m_frameID; }
129
130 private:
131     Ref<WebCore::DocumentLoader> createDocumentLoader(const WebCore::ResourceRequest&, const WebCore::SubstituteData&) final;
132
133     void frameLoaderDestroyed() final { m_connection.removeFrameLoaderClient(*this); }
134
135     bool shouldUseCredentialStorage(WebCore::DocumentLoader*, unsigned long) final { return true; }
136     bool isServiceWorkerFrameLoaderClient() const final { return true; }
137
138     String userAgent(const URL&) final { return m_userAgent; }
139
140     WebSWContextManagerConnection& m_connection;
141     WebPageProxyIdentifier m_webPageProxyID;
142     WebCore::PageIdentifier m_pageID;
143     WebCore::FrameIdentifier m_frameID;
144     String m_userAgent;
145 };
146
147 } // namespace WebKit
148
149 SPECIALIZE_TYPE_TRAITS_BEGIN(WebKit::ServiceWorkerFrameLoaderClient)
150     static bool isType(const WebCore::FrameLoaderClient& frameLoaderClient) { return frameLoaderClient.isServiceWorkerFrameLoaderClient(); }
151 SPECIALIZE_TYPE_TRAITS_END()
152
153 #endif // ENABLE(SERVICE_WORKER)