blob: 2985ccee21d7cd5fda9fb260c3ac33264ac6ee97 [file] [log] [blame]
beidson@apple.com12ab9642012-11-14 19:43:42 +00001/*
ap@apple.comcd1fa452013-03-26 21:27:45 +00002 * Copyright (C) 2012, 2013 Apple Inc. All rights reserved.
beidson@apple.com12ab9642012-11-14 19:43:42 +00003 *
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 "WebResourceLoader.h"
28
beidson@apple.comaec2e292012-11-14 23:59:01 +000029#include "DataReference.h"
30#include "Logging.h"
beidson@apple.comaec2e292012-11-14 23:59:01 +000031#include "NetworkProcessConnection.h"
ap@apple.comcd1fa452013-03-26 21:27:45 +000032#include "NetworkResourceLoaderMessages.h"
beidson@apple.comaec2e292012-11-14 23:59:01 +000033#include "WebCoreArgumentCoders.h"
beidson@apple.com1bc450a2012-12-21 18:46:04 +000034#include "WebErrors.h"
beidson@apple.comaec2e292012-11-14 23:59:01 +000035#include "WebProcess.h"
ap@apple.com178e91d2014-05-18 05:29:36 +000036#include <WebCore/ApplicationCacheHost.h>
ossy@webkit.org84c850d2013-12-12 12:30:58 +000037#include <WebCore/CertificateInfo.h>
andersca@apple.com0ab76de2013-04-22 22:56:19 +000038#include <WebCore/DocumentLoader.h>
ap@apple.com18ae25f2013-01-27 01:01:08 +000039#include <WebCore/ResourceError.h>
beidson@apple.com12ab9642012-11-14 19:43:42 +000040#include <WebCore/ResourceLoader.h>
aestes@apple.com564640a2015-08-14 21:08:13 +000041#include <WebCore/SubresourceLoader.h>
beidson@apple.com12ab9642012-11-14 19:43:42 +000042
43using namespace WebCore;
44
commit-queue@webkit.org6d635222016-03-08 06:44:59 +000045#define WEBRESOURCELOADER_LOG_ALWAYS(...) LOG_ALWAYS(isAlwaysOnLoggingAllowed(), __VA_ARGS__)
46
beidson@apple.com12ab9642012-11-14 19:43:42 +000047namespace WebKit {
48
youenn.fablet@crf.canon.fr55144c52016-06-02 08:45:14 +000049Ref<WebResourceLoader> WebResourceLoader::create(Ref<ResourceLoader>&& coreLoader)
beidson@apple.com12ab9642012-11-14 19:43:42 +000050{
youenn.fablet@crf.canon.fr55144c52016-06-02 08:45:14 +000051 return adoptRef(*new WebResourceLoader(WTFMove(coreLoader)));
beidson@apple.com12ab9642012-11-14 19:43:42 +000052}
53
youenn.fablet@crf.canon.fr55144c52016-06-02 08:45:14 +000054WebResourceLoader::WebResourceLoader(Ref<WebCore::ResourceLoader>&& coreLoader)
55 : m_coreLoader(WTFMove(coreLoader))
beidson@apple.com12ab9642012-11-14 19:43:42 +000056{
57}
58
59WebResourceLoader::~WebResourceLoader()
60{
61}
62
andersca@apple.com69388f02013-12-30 20:20:02 +000063IPC::Connection* WebResourceLoader::messageSenderConnection()
beidson@apple.com7fb049d2012-11-17 00:40:15 +000064{
cdumez@apple.com4faf1ef2015-01-30 20:49:33 +000065 return WebProcess::singleton().networkConnection()->connection();
beidson@apple.com7fb049d2012-11-17 00:40:15 +000066}
67
andersca@apple.comd4aa5922013-05-25 00:17:20 +000068uint64_t WebResourceLoader::messageSenderDestinationID()
beidson@apple.com7fb049d2012-11-17 00:40:15 +000069{
70 return m_coreLoader->identifier();
71}
72
beidson@apple.comf8d64742013-03-29 23:02:28 +000073void WebResourceLoader::detachFromCoreLoader()
74{
cdumez@apple.comd839ea12015-07-04 19:42:18 +000075 m_coreLoader = nullptr;
beidson@apple.comf8d64742013-03-29 23:02:28 +000076}
77
ap@apple.comcd1fa452013-03-26 21:27:45 +000078void WebResourceLoader::willSendRequest(const ResourceRequest& proposedRequest, const ResourceResponse& redirectResponse)
beidson@apple.comaec2e292012-11-14 23:59:01 +000079{
commit-queue@webkit.org6d635222016-03-08 06:44:59 +000080 LOG(Network, "(WebProcess) WebResourceLoader::willSendRequest to '%s'", proposedRequest.url().string().latin1().data());
81 WEBRESOURCELOADER_LOG_ALWAYS("WebResourceLoader::willSendRequest, WebResourceLoader = %p", this);
beidson@apple.comf8d64742013-03-29 23:02:28 +000082
jpfau@apple.comfb06c002014-12-02 04:28:25 +000083 RefPtr<WebResourceLoader> protect(this);
ap@apple.com178e91d2014-05-18 05:29:36 +000084
ap@apple.comcd1fa452013-03-26 21:27:45 +000085 ResourceRequest newRequest = proposedRequest;
ap@apple.com178e91d2014-05-18 05:29:36 +000086 if (m_coreLoader->documentLoader()->applicationCacheHost()->maybeLoadFallbackForRedirect(m_coreLoader.get(), newRequest, redirectResponse))
87 return;
commit-queue@webkit.org0edb4112014-09-11 17:20:18 +000088 // FIXME: Do we need to update NetworkResourceLoader clientCredentialPolicy in case loader policy is DoNotAskClientForCrossOriginCredentials?
aestes@apple.com13aae082016-01-02 08:03:08 +000089 m_coreLoader->willSendRequest(WTFMove(newRequest), redirectResponse, [protect](ResourceRequest&& request) {
jpfau@apple.comfb06c002014-12-02 04:28:25 +000090 if (!protect->m_coreLoader)
91 return;
92
commit-queue@webkit.org599506b2015-08-04 20:48:47 +000093 protect->send(Messages::NetworkResourceLoader::ContinueWillSendRequest(request));
jpfau@apple.comfb06c002014-12-02 04:28:25 +000094 });
beidson@apple.comaec2e292012-11-14 23:59:01 +000095}
96
ap@apple.com0c055062013-04-03 18:23:34 +000097void WebResourceLoader::didSendData(uint64_t bytesSent, uint64_t totalBytesToBeSent)
98{
99 m_coreLoader->didSendData(bytesSent, totalBytesToBeSent);
100}
101
antti@apple.com6d2fe7d2014-09-09 08:19:16 +0000102void WebResourceLoader::didReceiveResponse(const ResourceResponse& response, bool needsContinueDidReceiveResponseMessage)
beidson@apple.comaec2e292012-11-14 23:59:01 +0000103{
commit-queue@webkit.org6d635222016-03-08 06:44:59 +0000104 LOG(Network, "(WebProcess) WebResourceLoader::didReceiveResponse for '%s'. Status %d.", m_coreLoader->url().string().latin1().data(), response.httpStatusCode());
105 WEBRESOURCELOADER_LOG_ALWAYS("WebResourceLoader::didReceiveResponse, WebResourceLoader = %p, status = %d.", this, response.httpStatusCode());
andersca@apple.com028c0732013-04-23 21:56:59 +0000106
akling@apple.coma34e5222013-09-10 03:44:05 +0000107 Ref<WebResourceLoader> protect(*this);
andersca@apple.com028c0732013-04-23 21:56:59 +0000108
antti@apple.com6d2fe7d2014-09-09 08:19:16 +0000109 if (m_coreLoader->documentLoader()->applicationCacheHost()->maybeLoadFallbackForResponse(m_coreLoader.get(), response))
ap@apple.com178e91d2014-05-18 05:29:36 +0000110 return;
aestes@apple.com08cc6992014-08-05 17:49:44 +0000111
antti@apple.com5ffeafc2014-10-20 17:17:32 +0000112 bool shoudCallCoreLoaderDidReceiveResponse = true;
aestes@apple.com08cc6992014-08-05 17:49:44 +0000113#if USE(QUICK_LOOK)
114 // Refrain from calling didReceiveResponse if QuickLook will convert this response, since the MIME type of the
115 // converted resource isn't yet known. WebResourceLoaderQuickLookDelegate will later call didReceiveResponse upon
116 // receiving the converted data.
cdumez@apple.com87da4e22016-04-18 15:36:34 +0000117 bool isMainLoad = m_coreLoader->documentLoader()->mainResourceLoader() == m_coreLoader;
118 if (isMainLoad && QuickLookHandle::shouldCreateForMIMEType(response.mimeType())) {
119 m_coreLoader->documentLoader()->setQuickLookHandle(QuickLookHandle::create(*m_coreLoader, response));
antti@apple.com5ffeafc2014-10-20 17:17:32 +0000120 shoudCallCoreLoaderDidReceiveResponse = false;
121 }
aestes@apple.com08cc6992014-08-05 17:49:44 +0000122#endif
antti@apple.com5ffeafc2014-10-20 17:17:32 +0000123 if (shoudCallCoreLoaderDidReceiveResponse)
antti@apple.com6d2fe7d2014-09-09 08:19:16 +0000124 m_coreLoader->didReceiveResponse(response);
andersca@apple.com0ab76de2013-04-22 22:56:19 +0000125
beidson@apple.com6b677332013-04-30 19:21:07 +0000126 // If m_coreLoader becomes null as a result of the didReceiveResponse callback, we can't use the send function().
ap@apple.com63cec802013-04-29 18:08:57 +0000127 if (!m_coreLoader)
128 return;
129
ap@apple.come2515012013-04-28 03:22:55 +0000130 if (needsContinueDidReceiveResponseMessage)
andersca@apple.com0ab76de2013-04-22 22:56:19 +0000131 send(Messages::NetworkResourceLoader::ContinueDidReceiveResponse());
beidson@apple.comaec2e292012-11-14 23:59:01 +0000132}
133
andersca@apple.com69388f02013-12-30 20:20:02 +0000134void WebResourceLoader::didReceiveData(const IPC::DataReference& data, int64_t encodedDataLength)
beidson@apple.comaec2e292012-11-14 23:59:01 +0000135{
commit-queue@webkit.org6d635222016-03-08 06:44:59 +0000136 LOG(Network, "(WebProcess) WebResourceLoader::didReceiveData of size %lu for '%s'", data.size(), m_coreLoader->url().string().latin1().data());
137 WEBRESOURCELOADER_LOG_ALWAYS("WebResourceLoader::didReceiveData, WebResourceLoader = %p, size = %lu", this, data.size());
commit-queue@webkit.org1cabcea2014-03-19 19:24:30 +0000138
139#if USE(QUICK_LOOK)
aestes@apple.com06613142014-08-07 00:16:36 +0000140 if (QuickLookHandle* quickLookHandle = m_coreLoader->documentLoader()->quickLookHandle()) {
141 if (quickLookHandle->didReceiveData(adoptCF(CFDataCreate(kCFAllocatorDefault, data.data(), data.size())).get()))
commit-queue@webkit.org1cabcea2014-03-19 19:24:30 +0000142 return;
143 }
144#endif
beidson@apple.comc289a142013-03-13 21:36:29 +0000145 m_coreLoader->didReceiveData(reinterpret_cast<const char*>(data.data()), data.size(), encodedDataLength, DataPayloadBytes);
beidson@apple.comaec2e292012-11-14 23:59:01 +0000146}
147
148void WebResourceLoader::didFinishResourceLoad(double finishTime)
149{
commit-queue@webkit.org6d635222016-03-08 06:44:59 +0000150 LOG(Network, "(WebProcess) WebResourceLoader::didFinishResourceLoad for '%s'", m_coreLoader->url().string().latin1().data());
151 WEBRESOURCELOADER_LOG_ALWAYS("WebResourceLoader::didFinishResourceLoad, WebResourceLoader = %p", this);
commit-queue@webkit.org1cabcea2014-03-19 19:24:30 +0000152
153#if USE(QUICK_LOOK)
aestes@apple.com06613142014-08-07 00:16:36 +0000154 if (QuickLookHandle* quickLookHandle = m_coreLoader->documentLoader()->quickLookHandle()) {
155 if (quickLookHandle->didFinishLoading())
156 return;
157 }
commit-queue@webkit.org1cabcea2014-03-19 19:24:30 +0000158#endif
beidson@apple.comaec2e292012-11-14 23:59:01 +0000159 m_coreLoader->didFinishLoading(finishTime);
160}
161
162void WebResourceLoader::didFailResourceLoad(const ResourceError& error)
163{
commit-queue@webkit.org6d635222016-03-08 06:44:59 +0000164 LOG(Network, "(WebProcess) WebResourceLoader::didFailResourceLoad for '%s'", m_coreLoader->url().string().latin1().data());
165 WEBRESOURCELOADER_LOG_ALWAYS("WebResourceLoader::didFailResourceLoad, WebResourceLoader = %p", this);
166
commit-queue@webkit.org1cabcea2014-03-19 19:24:30 +0000167#if USE(QUICK_LOOK)
aestes@apple.com06613142014-08-07 00:16:36 +0000168 if (QuickLookHandle* quickLookHandle = m_coreLoader->documentLoader()->quickLookHandle())
169 quickLookHandle->didFail();
commit-queue@webkit.org1cabcea2014-03-19 19:24:30 +0000170#endif
ap@apple.com178e91d2014-05-18 05:29:36 +0000171 if (m_coreLoader->documentLoader()->applicationCacheHost()->maybeLoadFallbackForError(m_coreLoader.get(), error))
172 return;
beidson@apple.comaec2e292012-11-14 23:59:01 +0000173 m_coreLoader->didFail(error);
174}
175
ossy@webkit.orgb72e86c02013-10-10 15:35:02 +0000176#if ENABLE(SHAREABLE_RESOURCE)
beidson@apple.comaec2e292012-11-14 23:59:01 +0000177void WebResourceLoader::didReceiveResource(const ShareableResource::Handle& handle, double finishTime)
178{
commit-queue@webkit.org6d635222016-03-08 06:44:59 +0000179 LOG(Network, "(WebProcess) WebResourceLoader::didReceiveResource for '%s'", m_coreLoader->url().string().latin1().data());
180 WEBRESOURCELOADER_LOG_ALWAYS("WebResourceLoader::didReceiveResource, WebResourceLoader = %p", this);
beidson@apple.comaec2e292012-11-14 23:59:01 +0000181
carlosgc@webkit.orgfc025bc2015-05-20 07:04:34 +0000182 RefPtr<SharedBuffer> buffer = handle.tryWrapInSharedBuffer();
183
aestes@apple.com425d3142014-05-23 00:40:23 +0000184#if USE(QUICK_LOOK)
aestes@apple.com06613142014-08-07 00:16:36 +0000185 if (QuickLookHandle* quickLookHandle = m_coreLoader->documentLoader()->quickLookHandle()) {
carlosgc@webkit.orgfc025bc2015-05-20 07:04:34 +0000186 if (buffer) {
187 if (quickLookHandle->didReceiveData(buffer->existingCFData())) {
aestes@apple.com06613142014-08-07 00:16:36 +0000188 quickLookHandle->didFinishLoading();
aestes@apple.com425d3142014-05-23 00:40:23 +0000189 return;
190 }
191 } else
aestes@apple.com06613142014-08-07 00:16:36 +0000192 quickLookHandle->didFail();
aestes@apple.com425d3142014-05-23 00:40:23 +0000193 }
194#endif
195
beidson@apple.com21302482013-03-22 00:14:03 +0000196 if (!buffer) {
197 LOG_ERROR("Unable to create buffer from ShareableResource sent from the network process.");
beidson@apple.com472f6d22013-03-18 18:05:26 +0000198 m_coreLoader->didFail(internalError(m_coreLoader->request().url()));
199 return;
200 }
beidson@apple.comaec2e292012-11-14 23:59:01 +0000201
akling@apple.coma34e5222013-09-10 03:44:05 +0000202 Ref<WebResourceLoader> protect(*this);
beidson@apple.comf8d64742013-03-29 23:02:28 +0000203
beidson@apple.comaec2e292012-11-14 23:59:01 +0000204 // Only send data to the didReceiveData callback if it exists.
carlosgc@webkit.org9fb0c4f2016-03-01 17:42:35 +0000205 if (unsigned bufferSize = buffer->size())
206 m_coreLoader->didReceiveBuffer(buffer.release(), bufferSize, DataPayloadWholeResource);
beidson@apple.comaec2e292012-11-14 23:59:01 +0000207
beidson@apple.comf8d64742013-03-29 23:02:28 +0000208 if (!m_coreLoader)
209 return;
210
beidson@apple.comaec2e292012-11-14 23:59:01 +0000211 m_coreLoader->didFinishLoading(finishTime);
212}
ossy@webkit.orgb72e86c02013-10-10 15:35:02 +0000213#endif
beidson@apple.comaec2e292012-11-14 23:59:01 +0000214
ossy@webkit.orgba31fed2013-09-17 08:20:21 +0000215#if USE(PROTECTION_SPACE_AUTH_CALLBACK)
ap@apple.comcd1fa452013-03-26 21:27:45 +0000216void WebResourceLoader::canAuthenticateAgainstProtectionSpace(const ProtectionSpace& protectionSpace)
beidson@apple.com29824ff2012-11-19 19:22:48 +0000217{
achristensen@apple.com5c468c52016-02-02 23:42:40 +0000218 bool result = m_coreLoader ? m_coreLoader->canAuthenticateAgainstProtectionSpace(protectionSpace) : false;
achristensen@apple.comcba6fe92016-01-09 01:21:57 +0000219
ap@apple.comcd1fa452013-03-26 21:27:45 +0000220 send(Messages::NetworkResourceLoader::ContinueCanAuthenticateAgainstProtectionSpace(result));
beidson@apple.com29824ff2012-11-19 19:22:48 +0000221}
ossy@webkit.orgba31fed2013-09-17 08:20:21 +0000222#endif
beidson@apple.com29824ff2012-11-19 19:22:48 +0000223
commit-queue@webkit.org6d635222016-03-08 06:44:59 +0000224bool WebResourceLoader::isAlwaysOnLoggingAllowed() const
225{
226 return resourceLoader() && resourceLoader()->isAlwaysOnLoggingAllowed();
227}
228
beidson@apple.com12ab9642012-11-14 19:43:42 +0000229} // namespace WebKit