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#include "config.h"
27#include "WebServiceWorkerProvider.h"
28
29#if ENABLE(SERVICE_WORKER)
30
31#include "NetworkProcessConnection.h"
32#include "WebProcess.h"
33#include "WebSWClientConnection.h"
34#include "WebSWServerConnection.h"
35#include <WebCore/CachedResource.h>
36#include <WebCore/Exception.h>
37#include <WebCore/ExceptionCode.h>
38#include <WebCore/SchemeRegistry.h>
39#include <WebCore/ServiceWorkerJob.h>
40#include <pal/SessionID.h>
41#include <wtf/text/WTFString.h>
42
43namespace WebKit {
44using namespace PAL;
45using namespace WebCore;
46
47WebServiceWorkerProvider& WebServiceWorkerProvider::singleton()
48{
49 static NeverDestroyed<WebServiceWorkerProvider> provider;
50 return provider;
51}
52
53WebServiceWorkerProvider::WebServiceWorkerProvider()
54{
55}
56
57WebCore::SWClientConnection& WebServiceWorkerProvider::serviceWorkerConnectionForSession(SessionID sessionID)
58{
59 ASSERT(sessionID.isValid());
60 return WebProcess::singleton().ensureNetworkProcessConnection().serviceWorkerConnectionForSession(sessionID);
61}
62
63WebCore::SWClientConnection* WebServiceWorkerProvider::existingServiceWorkerConnectionForSession(SessionID sessionID)
64{
65 ASSERT(sessionID.isValid());
66 auto* networkProcessConnection = WebProcess::singleton().existingNetworkProcessConnection();
67 if (!networkProcessConnection)
68 return nullptr;
69 return networkProcessConnection->existingServiceWorkerConnectionForSession(sessionID);
70}
71
72static inline bool shouldHandleFetch(const ResourceLoaderOptions& options)
73{
74 if (options.serviceWorkersMode == ServiceWorkersMode::None)
75 return false;
76
77 if (isPotentialNavigationOrSubresourceRequest(options.destination))
78 return false;
79
80 return !!options.serviceWorkerRegistrationIdentifier;
81}
82
83void WebServiceWorkerProvider::handleFetch(ResourceLoader& loader, PAL::SessionID sessionID, bool shouldClearReferrerOnHTTPSToHTTPRedirect, ServiceWorkerClientFetch::Callback&& callback)
84{
85 if (!SchemeRegistry::canServiceWorkersHandleURLScheme(loader.request().url().protocol().toStringWithoutCopying()) || !shouldHandleFetch(loader.options())) {
86 callback(ServiceWorkerClientFetch::Result::Unhandled);
87 return;
88 }
89
90 auto& connection = WebProcess::singleton().ensureNetworkProcessConnection().serviceWorkerConnectionForSession(sessionID);
91 auto fetchIdentifier = makeObjectIdentifier<FetchIdentifierType>(loader.identifier());
92 m_ongoingFetchTasks.add(fetchIdentifier, ServiceWorkerClientFetch::create(*this, loader, fetchIdentifier, connection, shouldClearReferrerOnHTTPSToHTTPRedirect, WTFMove(callback)));
93}
94
95bool WebServiceWorkerProvider::cancelFetch(FetchIdentifier fetchIdentifier)
96{
97 auto fetch = m_ongoingFetchTasks.take(fetchIdentifier);
98 if (fetch)
99 (*fetch)->cancel();
100 return !!fetch;
101}
102
103void WebServiceWorkerProvider::fetchFinished(FetchIdentifier fetchIdentifier)
104{
105 m_ongoingFetchTasks.take(fetchIdentifier);
106}
107
108void WebServiceWorkerProvider::didReceiveServiceWorkerClientFetchMessage(IPC::Connection& connection, IPC::Decoder& decoder)
109{
110 if (auto fetch = m_ongoingFetchTasks.get(makeObjectIdentifier<FetchIdentifierType>(decoder.destinationID())))
111 fetch->didReceiveMessage(connection, decoder);
112}
113
114} // namespace WebKit
115
116#endif // ENABLE(SERVICE_WORKER)
117