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 "WebSWContextManagerConnectionMessages.h"
33#include <WebCore/SWContextManager.h>
34#include <WebCore/ServiceWorkerClientData.h>
35#include <WebCore/ServiceWorkerTypes.h>
36
37namespace IPC {
38class FormDataReference;
39}
40
41namespace WebCore {
42struct FetchOptions;
43class ResourceRequest;
44struct ServiceWorkerContextData;
45}
46
47namespace WebKit {
48
49class ServiceWorkerFrameLoaderClient;
50struct WebPreferencesStore;
51
52class WebSWContextManagerConnection final : public WebCore::SWContextManager::Connection, public IPC::MessageReceiver {
53public:
54 WebSWContextManagerConnection(Ref<IPC::Connection>&&, uint64_t pageGroupID, WebCore::PageIdentifier, const WebPreferencesStore&);
55 ~WebSWContextManagerConnection();
56
57 void didReceiveMessage(IPC::Connection&, IPC::Decoder&) final;
58 void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&) final;
59
60 void removeFrameLoaderClient(ServiceWorkerFrameLoaderClient&);
61
62private:
63 void updatePreferencesStore(const WebPreferencesStore&);
64
65 // WebCore::SWContextManager::Connection.
66 void postMessageToServiceWorkerClient(const WebCore::ServiceWorkerClientIdentifier& destinationIdentifier, WebCore::MessageWithMessagePorts&&, WebCore::ServiceWorkerIdentifier sourceIdentifier, const String& sourceOrigin) final;
67 void didFinishInstall(Optional<WebCore::ServiceWorkerJobDataIdentifier>, WebCore::ServiceWorkerIdentifier, bool wasSuccessful) final;
68 void didFinishActivation(WebCore::ServiceWorkerIdentifier) final;
69 void setServiceWorkerHasPendingEvents(WebCore::ServiceWorkerIdentifier, bool) final;
70 void workerTerminated(WebCore::ServiceWorkerIdentifier) final;
71 void findClientByIdentifier(WebCore::ServiceWorkerIdentifier, WebCore::ServiceWorkerClientIdentifier, FindClientByIdentifierCallback&&) final;
72 void matchAll(WebCore::ServiceWorkerIdentifier, const WebCore::ServiceWorkerClientQueryOptions&, WebCore::ServiceWorkerClientsMatchAllCallback&&) final;
73 void claim(WebCore::ServiceWorkerIdentifier, CompletionHandler<void()>&&) final;
74 void skipWaiting(WebCore::ServiceWorkerIdentifier, Function<void()>&&) final;
75 void setScriptResource(WebCore::ServiceWorkerIdentifier, const URL&, const WebCore::ServiceWorkerContextData::ImportedScript&) final;
76 bool isThrottleable() const final;
77
78 // IPC messages.
79 void serviceWorkerStartedWithMessage(Optional<WebCore::ServiceWorkerJobDataIdentifier>, WebCore::ServiceWorkerIdentifier, const String& exceptionMessage) final;
80 void installServiceWorker(const WebCore::ServiceWorkerContextData&, PAL::SessionID, String&& userAgent);
81 void startFetch(WebCore::SWServerConnectionIdentifier, WebCore::ServiceWorkerIdentifier, WebCore::FetchIdentifier, WebCore::ResourceRequest&&, WebCore::FetchOptions&&, IPC::FormDataReference&&, String&& referrer);
82 void cancelFetch(WebCore::SWServerConnectionIdentifier, WebCore::ServiceWorkerIdentifier, WebCore::FetchIdentifier);
83 void continueDidReceiveFetchResponse(WebCore::SWServerConnectionIdentifier, WebCore::ServiceWorkerIdentifier, WebCore::FetchIdentifier);
84 void postMessageToServiceWorker(WebCore::ServiceWorkerIdentifier destinationIdentifier, WebCore::MessageWithMessagePorts&&, WebCore::ServiceWorkerOrClientData&& sourceData);
85 void fireInstallEvent(WebCore::ServiceWorkerIdentifier);
86 void fireActivateEvent(WebCore::ServiceWorkerIdentifier);
87 void terminateWorker(WebCore::ServiceWorkerIdentifier);
88 void syncTerminateWorker(WebCore::ServiceWorkerIdentifier, Messages::WebSWContextManagerConnection::SyncTerminateWorker::DelayedReply&&);
89 void findClientByIdentifierCompleted(uint64_t requestIdentifier, Optional<WebCore::ServiceWorkerClientData>&&, bool hasSecurityError);
90 void matchAllCompleted(uint64_t matchAllRequestIdentifier, Vector<WebCore::ServiceWorkerClientData>&&);
91 void claimCompleted(uint64_t claimRequestIdentifier);
92 void didFinishSkipWaiting(uint64_t callbackID);
93 void setUserAgent(String&& userAgent);
94 NO_RETURN void terminateProcess();
95 void setThrottleState(bool isThrottleable);
96
97 Ref<IPC::Connection> m_connectionToNetworkProcess;
98 uint64_t m_pageGroupID;
99 WebCore::PageIdentifier m_pageID;
100 uint64_t m_previousServiceWorkerID { 0 };
101
102 WebCore::SecurityOrigin::StorageBlockingPolicy m_storageBlockingPolicy { WebCore::SecurityOrigin::StorageBlockingPolicy::AllowAllStorage };
103
104 HashSet<std::unique_ptr<ServiceWorkerFrameLoaderClient>> m_loaders;
105 HashMap<uint64_t, FindClientByIdentifierCallback> m_findClientByIdentifierRequests;
106 HashMap<uint64_t, WebCore::ServiceWorkerClientsMatchAllCallback> m_matchAllRequests;
107 HashMap<uint64_t, WTF::CompletionHandler<void()>> m_claimRequests;
108 HashMap<uint64_t, WTF::Function<void()>> m_skipWaitingRequests;
109 uint64_t m_previousRequestIdentifier { 0 };
110 String m_userAgent;
111 bool m_isThrottleable { true };
112};
113
114} // namespace WebKit
115
116#endif // ENABLE(SERVICE_WORKER)
117