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 "ServiceWorkerClientFetch.h" |
28 | |
29 | #if ENABLE(SERVICE_WORKER) |
30 | |
31 | #include "DataReference.h" |
32 | #include "WebSWClientConnection.h" |
33 | #include "WebServiceWorkerProvider.h" |
34 | #include <WebCore/CrossOriginAccessControl.h> |
35 | #include <WebCore/Document.h> |
36 | #include <WebCore/Frame.h> |
37 | #include <WebCore/NotImplemented.h> |
38 | #include <WebCore/ResourceError.h> |
39 | #include <WebCore/SharedBuffer.h> |
40 | |
41 | namespace WebKit { |
42 | using namespace WebCore; |
43 | |
44 | Ref<ServiceWorkerClientFetch> ServiceWorkerClientFetch::create(WebServiceWorkerProvider& serviceWorkerProvider, Ref<WebCore::ResourceLoader>&& loader, FetchIdentifier identifier, Ref<WebSWClientConnection>&& connection, bool shouldClearReferrerOnHTTPSToHTTPRedirect, Callback&& callback) |
45 | { |
46 | auto fetch = adoptRef(*new ServiceWorkerClientFetch { serviceWorkerProvider, WTFMove(loader), identifier, WTFMove(connection), shouldClearReferrerOnHTTPSToHTTPRedirect, WTFMove(callback) }); |
47 | fetch->start(); |
48 | return fetch; |
49 | } |
50 | |
51 | ServiceWorkerClientFetch::~ServiceWorkerClientFetch() |
52 | { |
53 | } |
54 | |
55 | ServiceWorkerClientFetch::ServiceWorkerClientFetch(WebServiceWorkerProvider& serviceWorkerProvider, Ref<WebCore::ResourceLoader>&& loader, FetchIdentifier identifier, Ref<WebSWClientConnection>&& connection, bool shouldClearReferrerOnHTTPSToHTTPRedirect, Callback&& callback) |
56 | : m_serviceWorkerProvider(serviceWorkerProvider) |
57 | , m_loader(WTFMove(loader)) |
58 | , m_identifier(identifier) |
59 | , m_connection(WTFMove(connection)) |
60 | , m_callback(WTFMove(callback)) |
61 | , m_shouldClearReferrerOnHTTPSToHTTPRedirect(shouldClearReferrerOnHTTPSToHTTPRedirect) |
62 | { |
63 | } |
64 | |
65 | void ServiceWorkerClientFetch::start() |
66 | { |
67 | auto request = m_loader->request(); |
68 | auto& options = m_loader->options(); |
69 | |
70 | auto referrer = request.httpReferrer(); |
71 | |
72 | // We are intercepting fetch calls after going through the HTTP layer, which may add some specific headers. |
73 | cleanHTTPRequestHeadersForAccessControl(request, options.httpHeadersToKeep); |
74 | |
75 | ASSERT(options.serviceWorkersMode != ServiceWorkersMode::None); |
76 | m_serviceWorkerRegistrationIdentifier = options.serviceWorkerRegistrationIdentifier.value(); |
77 | m_connection->startFetch(m_identifier, m_serviceWorkerRegistrationIdentifier, request, options, referrer); |
78 | } |
79 | |
80 | void ServiceWorkerClientFetch::didReceiveRedirectResponse(ResourceResponse&& response) |
81 | { |
82 | callOnMainThread([this, protectedThis = makeRef(*this), response = WTFMove(response)]() mutable { |
83 | if (!m_loader) |
84 | return; |
85 | |
86 | response.setSource(ResourceResponse::Source::ServiceWorker); |
87 | |
88 | m_loader->willSendRequest(m_loader->request().redirectedRequest(response, m_shouldClearReferrerOnHTTPSToHTTPRedirect), response, [this, protectedThis = protectedThis.copyRef()](ResourceRequest&& request) { |
89 | if (!m_loader || request.isNull()) { |
90 | if (auto callback = WTFMove(m_callback)) |
91 | callback(Result::Succeeded); |
92 | return; |
93 | } |
94 | ASSERT(request == m_loader->request()); |
95 | start(); |
96 | }); |
97 | }); |
98 | } |
99 | |
100 | void ServiceWorkerClientFetch::didReceiveResponse(ResourceResponse&& response, bool needsContinueDidReceiveResponseMessage) |
101 | { |
102 | callOnMainThread([this, protectedThis = makeRef(*this), response = WTFMove(response), needsContinueDidReceiveResponseMessage]() mutable { |
103 | if (!m_loader) |
104 | return; |
105 | |
106 | if (auto callback = WTFMove(m_callback)) |
107 | callback(Result::Succeeded); |
108 | |
109 | ASSERT(!response.isRedirection() || !response.httpHeaderFields().contains(HTTPHeaderName::Location)); |
110 | |
111 | if (!needsContinueDidReceiveResponseMessage) { |
112 | m_loader->didReceiveResponse(response, [] { }); |
113 | return; |
114 | } |
115 | |
116 | m_loader->didReceiveResponse(response, [this, protectedThis = WTFMove(protectedThis)] { |
117 | if (!m_loader) |
118 | return; |
119 | |
120 | m_connection->continueDidReceiveFetchResponse(m_identifier, m_serviceWorkerRegistrationIdentifier); |
121 | }); |
122 | }); |
123 | } |
124 | |
125 | void ServiceWorkerClientFetch::didReceiveData(const IPC::DataReference& dataReference, int64_t encodedDataLength) |
126 | { |
127 | auto* data = reinterpret_cast<const char*>(dataReference.data()); |
128 | callOnMainThread([this, protectedThis = makeRef(*this), encodedDataLength, buffer = SharedBuffer::create(data, dataReference.size())]() mutable { |
129 | if (!m_loader) |
130 | return; |
131 | |
132 | m_encodedDataLength += encodedDataLength; |
133 | |
134 | m_loader->didReceiveBuffer(WTFMove(buffer), m_encodedDataLength, DataPayloadBytes); |
135 | }); |
136 | } |
137 | |
138 | void ServiceWorkerClientFetch::didReceiveFormData(const IPC::FormDataReference&) |
139 | { |
140 | // FIXME: Implement form data reading. |
141 | } |
142 | |
143 | void ServiceWorkerClientFetch::didFinish() |
144 | { |
145 | callOnMainThread([this, protectedThis = makeRef(*this)] { |
146 | if (!m_loader) |
147 | return; |
148 | |
149 | ASSERT(!m_callback); |
150 | |
151 | m_loader->didFinishLoading(NetworkLoadMetrics { }); |
152 | m_serviceWorkerProvider.fetchFinished(m_identifier); |
153 | }); |
154 | } |
155 | |
156 | void ServiceWorkerClientFetch::didFail(ResourceError&& error) |
157 | { |
158 | callOnMainThread([this, protectedThis = makeRef(*this), error = WTFMove(error)] { |
159 | if (!m_loader) |
160 | return; |
161 | |
162 | auto* document = m_loader->frame() ? m_loader->frame()->document() : nullptr; |
163 | if (document) { |
164 | if (m_loader->options().destination != FetchOptions::Destination::EmptyString || error.isGeneral()) |
165 | document->addConsoleMessage(MessageSource::JS, MessageLevel::Error, error.localizedDescription()); |
166 | if (m_loader->options().destination != FetchOptions::Destination::EmptyString) |
167 | document->addConsoleMessage(MessageSource::JS, MessageLevel::Error, makeString("Cannot load " , error.failingURL().string(), "." )); |
168 | } |
169 | |
170 | m_loader->didFail(error); |
171 | |
172 | if (auto callback = WTFMove(m_callback)) |
173 | callback(Result::Succeeded); |
174 | |
175 | m_serviceWorkerProvider.fetchFinished(m_identifier); |
176 | }); |
177 | } |
178 | |
179 | void ServiceWorkerClientFetch::didNotHandle() |
180 | { |
181 | callOnMainThread([this, protectedThis = makeRef(*this)] { |
182 | if (!m_loader) |
183 | return; |
184 | |
185 | if (auto callback = WTFMove(m_callback)) |
186 | callback(Result::Unhandled); |
187 | |
188 | m_serviceWorkerProvider.fetchFinished(m_identifier); |
189 | }); |
190 | } |
191 | |
192 | void ServiceWorkerClientFetch::cancel() |
193 | { |
194 | if (auto callback = WTFMove(m_callback)) |
195 | callback(Result::Cancelled); |
196 | |
197 | m_connection->cancelFetch(m_identifier, m_serviceWorkerRegistrationIdentifier); |
198 | m_loader = nullptr; |
199 | } |
200 | |
201 | } // namespace WebKit |
202 | |
203 | #endif // ENABLE(SERVICE_WORKER) |
204 | |