blob: 2a0f559acf5d2c660cc904c85f9990de535adf7f [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
45namespace WebKit {
46
gyuyoung.kim@webkit.org0de87062015-07-09 02:31:21 +000047Ref<WebResourceLoader> WebResourceLoader::create(PassRefPtr<ResourceLoader> coreLoader)
beidson@apple.com12ab9642012-11-14 19:43:42 +000048{
gyuyoung.kim@webkit.org0de87062015-07-09 02:31:21 +000049 return adoptRef(*new WebResourceLoader(coreLoader));
beidson@apple.com12ab9642012-11-14 19:43:42 +000050}
51
beidson@apple.comaec2e292012-11-14 23:59:01 +000052WebResourceLoader::WebResourceLoader(PassRefPtr<WebCore::ResourceLoader> coreLoader)
53 : m_coreLoader(coreLoader)
beidson@apple.com12ab9642012-11-14 19:43:42 +000054{
55}
56
57WebResourceLoader::~WebResourceLoader()
58{
59}
60
andersca@apple.com69388f02013-12-30 20:20:02 +000061IPC::Connection* WebResourceLoader::messageSenderConnection()
beidson@apple.com7fb049d2012-11-17 00:40:15 +000062{
cdumez@apple.com4faf1ef2015-01-30 20:49:33 +000063 return WebProcess::singleton().networkConnection()->connection();
beidson@apple.com7fb049d2012-11-17 00:40:15 +000064}
65
andersca@apple.comd4aa5922013-05-25 00:17:20 +000066uint64_t WebResourceLoader::messageSenderDestinationID()
beidson@apple.com7fb049d2012-11-17 00:40:15 +000067{
68 return m_coreLoader->identifier();
69}
70
beidson@apple.comf8d64742013-03-29 23:02:28 +000071void WebResourceLoader::detachFromCoreLoader()
72{
cdumez@apple.comd839ea12015-07-04 19:42:18 +000073 m_coreLoader = nullptr;
beidson@apple.comf8d64742013-03-29 23:02:28 +000074}
75
ap@apple.comcd1fa452013-03-26 21:27:45 +000076void WebResourceLoader::willSendRequest(const ResourceRequest& proposedRequest, const ResourceResponse& redirectResponse)
beidson@apple.comaec2e292012-11-14 23:59:01 +000077{
78 LOG(Network, "(WebProcess) WebResourceLoader::willSendRequest to '%s'", proposedRequest.url().string().utf8().data());
beidson@apple.comf8d64742013-03-29 23:02:28 +000079
jpfau@apple.comfb06c002014-12-02 04:28:25 +000080 RefPtr<WebResourceLoader> protect(this);
ap@apple.com178e91d2014-05-18 05:29:36 +000081
ap@apple.comcd1fa452013-03-26 21:27:45 +000082 ResourceRequest newRequest = proposedRequest;
ap@apple.com178e91d2014-05-18 05:29:36 +000083 if (m_coreLoader->documentLoader()->applicationCacheHost()->maybeLoadFallbackForRedirect(m_coreLoader.get(), newRequest, redirectResponse))
84 return;
commit-queue@webkit.org0edb4112014-09-11 17:20:18 +000085 // FIXME: Do we need to update NetworkResourceLoader clientCredentialPolicy in case loader policy is DoNotAskClientForCrossOriginCredentials?
aestes@apple.com13aae082016-01-02 08:03:08 +000086 m_coreLoader->willSendRequest(WTFMove(newRequest), redirectResponse, [protect](ResourceRequest&& request) {
jpfau@apple.comfb06c002014-12-02 04:28:25 +000087 if (!protect->m_coreLoader)
88 return;
89
commit-queue@webkit.org599506b2015-08-04 20:48:47 +000090 protect->send(Messages::NetworkResourceLoader::ContinueWillSendRequest(request));
jpfau@apple.comfb06c002014-12-02 04:28:25 +000091 });
beidson@apple.comaec2e292012-11-14 23:59:01 +000092}
93
ap@apple.com0c055062013-04-03 18:23:34 +000094void WebResourceLoader::didSendData(uint64_t bytesSent, uint64_t totalBytesToBeSent)
95{
96 m_coreLoader->didSendData(bytesSent, totalBytesToBeSent);
97}
98
antti@apple.com6d2fe7d2014-09-09 08:19:16 +000099void WebResourceLoader::didReceiveResponse(const ResourceResponse& response, bool needsContinueDidReceiveResponseMessage)
beidson@apple.comaec2e292012-11-14 23:59:01 +0000100{
mcatanzaro@igalia.com8ba78152016-01-01 00:42:09 +0000101 LOG(Network, "(WebProcess) WebResourceLoader::didReceiveResponse for '%s'. Status %d.", m_coreLoader->url().string().utf8().data(), response.httpStatusCode());
andersca@apple.com028c0732013-04-23 21:56:59 +0000102
akling@apple.coma34e5222013-09-10 03:44:05 +0000103 Ref<WebResourceLoader> protect(*this);
andersca@apple.com028c0732013-04-23 21:56:59 +0000104
antti@apple.com6d2fe7d2014-09-09 08:19:16 +0000105 if (m_coreLoader->documentLoader()->applicationCacheHost()->maybeLoadFallbackForResponse(m_coreLoader.get(), response))
ap@apple.com178e91d2014-05-18 05:29:36 +0000106 return;
aestes@apple.com08cc6992014-08-05 17:49:44 +0000107
antti@apple.com5ffeafc2014-10-20 17:17:32 +0000108 bool shoudCallCoreLoaderDidReceiveResponse = true;
aestes@apple.com08cc6992014-08-05 17:49:44 +0000109#if USE(QUICK_LOOK)
110 // Refrain from calling didReceiveResponse if QuickLook will convert this response, since the MIME type of the
111 // converted resource isn't yet known. WebResourceLoaderQuickLookDelegate will later call didReceiveResponse upon
112 // receiving the converted data.
antti@apple.com5ffeafc2014-10-20 17:17:32 +0000113 bool isMainLoad = m_coreLoader->documentLoader()->mainResourceLoader() == m_coreLoader;
114 if (isMainLoad && QuickLookHandle::shouldCreateForMIMEType(response.mimeType())) {
115 m_coreLoader->documentLoader()->setQuickLookHandle(QuickLookHandle::create(*m_coreLoader, response));
116 shoudCallCoreLoaderDidReceiveResponse = false;
117 }
aestes@apple.com08cc6992014-08-05 17:49:44 +0000118#endif
antti@apple.com5ffeafc2014-10-20 17:17:32 +0000119 if (shoudCallCoreLoaderDidReceiveResponse)
antti@apple.com6d2fe7d2014-09-09 08:19:16 +0000120 m_coreLoader->didReceiveResponse(response);
andersca@apple.com0ab76de2013-04-22 22:56:19 +0000121
beidson@apple.com6b677332013-04-30 19:21:07 +0000122 // 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 +0000123 if (!m_coreLoader)
124 return;
125
ap@apple.come2515012013-04-28 03:22:55 +0000126 if (needsContinueDidReceiveResponseMessage)
andersca@apple.com0ab76de2013-04-22 22:56:19 +0000127 send(Messages::NetworkResourceLoader::ContinueDidReceiveResponse());
beidson@apple.comaec2e292012-11-14 23:59:01 +0000128}
129
andersca@apple.com69388f02013-12-30 20:20:02 +0000130void WebResourceLoader::didReceiveData(const IPC::DataReference& data, int64_t encodedDataLength)
beidson@apple.comaec2e292012-11-14 23:59:01 +0000131{
132 LOG(Network, "(WebProcess) WebResourceLoader::didReceiveData of size %i for '%s'", (int)data.size(), m_coreLoader->url().string().utf8().data());
commit-queue@webkit.org1cabcea2014-03-19 19:24:30 +0000133
134#if USE(QUICK_LOOK)
aestes@apple.com06613142014-08-07 00:16:36 +0000135 if (QuickLookHandle* quickLookHandle = m_coreLoader->documentLoader()->quickLookHandle()) {
136 if (quickLookHandle->didReceiveData(adoptCF(CFDataCreate(kCFAllocatorDefault, data.data(), data.size())).get()))
commit-queue@webkit.org1cabcea2014-03-19 19:24:30 +0000137 return;
138 }
139#endif
beidson@apple.comc289a142013-03-13 21:36:29 +0000140 m_coreLoader->didReceiveData(reinterpret_cast<const char*>(data.data()), data.size(), encodedDataLength, DataPayloadBytes);
beidson@apple.comaec2e292012-11-14 23:59:01 +0000141}
142
143void WebResourceLoader::didFinishResourceLoad(double finishTime)
144{
145 LOG(Network, "(WebProcess) WebResourceLoader::didFinishResourceLoad for '%s'", m_coreLoader->url().string().utf8().data());
commit-queue@webkit.org1cabcea2014-03-19 19:24:30 +0000146
147#if USE(QUICK_LOOK)
aestes@apple.com06613142014-08-07 00:16:36 +0000148 if (QuickLookHandle* quickLookHandle = m_coreLoader->documentLoader()->quickLookHandle()) {
149 if (quickLookHandle->didFinishLoading())
150 return;
151 }
commit-queue@webkit.org1cabcea2014-03-19 19:24:30 +0000152#endif
beidson@apple.comaec2e292012-11-14 23:59:01 +0000153 m_coreLoader->didFinishLoading(finishTime);
154}
155
156void WebResourceLoader::didFailResourceLoad(const ResourceError& error)
157{
158 LOG(Network, "(WebProcess) WebResourceLoader::didFailResourceLoad for '%s'", m_coreLoader->url().string().utf8().data());
159
commit-queue@webkit.org1cabcea2014-03-19 19:24:30 +0000160#if USE(QUICK_LOOK)
aestes@apple.com06613142014-08-07 00:16:36 +0000161 if (QuickLookHandle* quickLookHandle = m_coreLoader->documentLoader()->quickLookHandle())
162 quickLookHandle->didFail();
commit-queue@webkit.org1cabcea2014-03-19 19:24:30 +0000163#endif
ap@apple.com178e91d2014-05-18 05:29:36 +0000164 if (m_coreLoader->documentLoader()->applicationCacheHost()->maybeLoadFallbackForError(m_coreLoader.get(), error))
165 return;
beidson@apple.comaec2e292012-11-14 23:59:01 +0000166 m_coreLoader->didFail(error);
167}
168
ossy@webkit.orgb72e86c02013-10-10 15:35:02 +0000169#if ENABLE(SHAREABLE_RESOURCE)
beidson@apple.comaec2e292012-11-14 23:59:01 +0000170void WebResourceLoader::didReceiveResource(const ShareableResource::Handle& handle, double finishTime)
171{
172 LOG(Network, "(WebProcess) WebResourceLoader::didReceiveResource for '%s'", m_coreLoader->url().string().utf8().data());
173
carlosgc@webkit.orgfc025bc2015-05-20 07:04:34 +0000174 RefPtr<SharedBuffer> buffer = handle.tryWrapInSharedBuffer();
175
aestes@apple.com425d3142014-05-23 00:40:23 +0000176#if USE(QUICK_LOOK)
aestes@apple.com06613142014-08-07 00:16:36 +0000177 if (QuickLookHandle* quickLookHandle = m_coreLoader->documentLoader()->quickLookHandle()) {
carlosgc@webkit.orgfc025bc2015-05-20 07:04:34 +0000178 if (buffer) {
179 if (quickLookHandle->didReceiveData(buffer->existingCFData())) {
aestes@apple.com06613142014-08-07 00:16:36 +0000180 quickLookHandle->didFinishLoading();
aestes@apple.com425d3142014-05-23 00:40:23 +0000181 return;
182 }
183 } else
aestes@apple.com06613142014-08-07 00:16:36 +0000184 quickLookHandle->didFail();
aestes@apple.com425d3142014-05-23 00:40:23 +0000185 }
186#endif
187
beidson@apple.com21302482013-03-22 00:14:03 +0000188 if (!buffer) {
189 LOG_ERROR("Unable to create buffer from ShareableResource sent from the network process.");
beidson@apple.com472f6d22013-03-18 18:05:26 +0000190 m_coreLoader->didFail(internalError(m_coreLoader->request().url()));
191 return;
192 }
beidson@apple.comaec2e292012-11-14 23:59:01 +0000193
akling@apple.coma34e5222013-09-10 03:44:05 +0000194 Ref<WebResourceLoader> protect(*this);
beidson@apple.comf8d64742013-03-29 23:02:28 +0000195
beidson@apple.comaec2e292012-11-14 23:59:01 +0000196 // Only send data to the didReceiveData callback if it exists.
carlosgc@webkit.org9fb0c4f2016-03-01 17:42:35 +0000197 if (unsigned bufferSize = buffer->size())
198 m_coreLoader->didReceiveBuffer(buffer.release(), bufferSize, DataPayloadWholeResource);
beidson@apple.comaec2e292012-11-14 23:59:01 +0000199
beidson@apple.comf8d64742013-03-29 23:02:28 +0000200 if (!m_coreLoader)
201 return;
202
beidson@apple.comaec2e292012-11-14 23:59:01 +0000203 m_coreLoader->didFinishLoading(finishTime);
204}
ossy@webkit.orgb72e86c02013-10-10 15:35:02 +0000205#endif
beidson@apple.comaec2e292012-11-14 23:59:01 +0000206
ossy@webkit.orgba31fed2013-09-17 08:20:21 +0000207#if USE(PROTECTION_SPACE_AUTH_CALLBACK)
ap@apple.comcd1fa452013-03-26 21:27:45 +0000208void WebResourceLoader::canAuthenticateAgainstProtectionSpace(const ProtectionSpace& protectionSpace)
beidson@apple.com29824ff2012-11-19 19:22:48 +0000209{
achristensen@apple.com5c468c52016-02-02 23:42:40 +0000210 bool result = m_coreLoader ? m_coreLoader->canAuthenticateAgainstProtectionSpace(protectionSpace) : false;
achristensen@apple.comcba6fe92016-01-09 01:21:57 +0000211
ap@apple.comcd1fa452013-03-26 21:27:45 +0000212 send(Messages::NetworkResourceLoader::ContinueCanAuthenticateAgainstProtectionSpace(result));
beidson@apple.com29824ff2012-11-19 19:22:48 +0000213}
ossy@webkit.orgba31fed2013-09-17 08:20:21 +0000214#endif
beidson@apple.com29824ff2012-11-19 19:22:48 +0000215
beidson@apple.com12ab9642012-11-14 19:43:42 +0000216} // namespace WebKit