blob: bcc8dab1637b034311e863488a0ed71e5eafc7aa [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
29#if ENABLE(NETWORK_PROCESS)
30
beidson@apple.comaec2e292012-11-14 23:59:01 +000031#include "DataReference.h"
32#include "Logging.h"
beidson@apple.comaec2e292012-11-14 23:59:01 +000033#include "NetworkProcessConnection.h"
ap@apple.comcd1fa452013-03-26 21:27:45 +000034#include "NetworkResourceLoaderMessages.h"
beidson@apple.comaec2e292012-11-14 23:59:01 +000035#include "WebCoreArgumentCoders.h"
beidson@apple.com1bc450a2012-12-21 18:46:04 +000036#include "WebErrors.h"
beidson@apple.comaec2e292012-11-14 23:59:01 +000037#include "WebProcess.h"
ap@apple.com178e91d2014-05-18 05:29:36 +000038#include <WebCore/ApplicationCacheHost.h>
ossy@webkit.org84c850d2013-12-12 12:30:58 +000039#include <WebCore/CertificateInfo.h>
andersca@apple.com0ab76de2013-04-22 22:56:19 +000040#include <WebCore/DocumentLoader.h>
beidson@apple.com472f6d22013-03-18 18:05:26 +000041#include <WebCore/ResourceBuffer.h>
ap@apple.com18ae25f2013-01-27 01:01:08 +000042#include <WebCore/ResourceError.h>
beidson@apple.com12ab9642012-11-14 19:43:42 +000043#include <WebCore/ResourceLoader.h>
44
45using namespace WebCore;
46
47namespace WebKit {
48
beidson@apple.comaec2e292012-11-14 23:59:01 +000049PassRefPtr<WebResourceLoader> WebResourceLoader::create(PassRefPtr<ResourceLoader> coreLoader)
beidson@apple.com12ab9642012-11-14 19:43:42 +000050{
beidson@apple.comaec2e292012-11-14 23:59:01 +000051 return adoptRef(new WebResourceLoader(coreLoader));
beidson@apple.com12ab9642012-11-14 19:43:42 +000052}
53
beidson@apple.comaec2e292012-11-14 23:59:01 +000054WebResourceLoader::WebResourceLoader(PassRefPtr<WebCore::ResourceLoader> coreLoader)
55 : m_coreLoader(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{
65 return WebProcess::shared().networkConnection()->connection();
66}
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.com44925772013-01-23 20:21:19 +000073void WebResourceLoader::cancelResourceLoader()
74{
75 m_coreLoader->cancel();
76}
77
beidson@apple.comf8d64742013-03-29 23:02:28 +000078void WebResourceLoader::detachFromCoreLoader()
79{
80 m_coreLoader = 0;
81}
82
ap@apple.comcd1fa452013-03-26 21:27:45 +000083void WebResourceLoader::willSendRequest(const ResourceRequest& proposedRequest, const ResourceResponse& redirectResponse)
beidson@apple.comaec2e292012-11-14 23:59:01 +000084{
85 LOG(Network, "(WebProcess) WebResourceLoader::willSendRequest to '%s'", proposedRequest.url().string().utf8().data());
beidson@apple.comf8d64742013-03-29 23:02:28 +000086
akling@apple.coma34e5222013-09-10 03:44:05 +000087 Ref<WebResourceLoader> protect(*this);
ap@apple.com178e91d2014-05-18 05:29:36 +000088
ap@apple.comcd1fa452013-03-26 21:27:45 +000089 ResourceRequest newRequest = proposedRequest;
ap@apple.com178e91d2014-05-18 05:29:36 +000090 if (m_coreLoader->documentLoader()->applicationCacheHost()->maybeLoadFallbackForRedirect(m_coreLoader.get(), newRequest, redirectResponse))
91 return;
commit-queue@webkit.org0edb4112014-09-11 17:20:18 +000092 // FIXME: Do we need to update NetworkResourceLoader clientCredentialPolicy in case loader policy is DoNotAskClientForCrossOriginCredentials?
beidson@apple.comaec2e292012-11-14 23:59:01 +000093 m_coreLoader->willSendRequest(newRequest, redirectResponse);
beidson@apple.comf8d64742013-03-29 23:02:28 +000094
95 if (!m_coreLoader)
96 return;
97
ap@apple.comcd1fa452013-03-26 21:27:45 +000098 send(Messages::NetworkResourceLoader::ContinueWillSendRequest(newRequest));
beidson@apple.comaec2e292012-11-14 23:59:01 +000099}
100
ap@apple.com0c055062013-04-03 18:23:34 +0000101void WebResourceLoader::didSendData(uint64_t bytesSent, uint64_t totalBytesToBeSent)
102{
103 m_coreLoader->didSendData(bytesSent, totalBytesToBeSent);
104}
105
antti@apple.com6d2fe7d2014-09-09 08:19:16 +0000106void WebResourceLoader::didReceiveResponse(const ResourceResponse& response, bool needsContinueDidReceiveResponseMessage)
beidson@apple.comaec2e292012-11-14 23:59:01 +0000107{
ap@apple.com61423822012-12-20 00:58:25 +0000108 LOG(Network, "(WebProcess) WebResourceLoader::didReceiveResponseWithCertificateInfo for '%s'. Status %d.", m_coreLoader->url().string().utf8().data(), response.httpStatusCode());
andersca@apple.com028c0732013-04-23 21:56:59 +0000109
akling@apple.coma34e5222013-09-10 03:44:05 +0000110 Ref<WebResourceLoader> protect(*this);
andersca@apple.com028c0732013-04-23 21:56:59 +0000111
antti@apple.com6d2fe7d2014-09-09 08:19:16 +0000112 if (m_coreLoader->documentLoader()->applicationCacheHost()->maybeLoadFallbackForResponse(m_coreLoader.get(), response))
ap@apple.com178e91d2014-05-18 05:29:36 +0000113 return;
aestes@apple.com08cc6992014-08-05 17:49:44 +0000114
115#if USE(QUICK_LOOK)
116 // Refrain from calling didReceiveResponse if QuickLook will convert this response, since the MIME type of the
117 // converted resource isn't yet known. WebResourceLoaderQuickLookDelegate will later call didReceiveResponse upon
118 // receiving the converted data.
antti@apple.com6d2fe7d2014-09-09 08:19:16 +0000119 m_coreLoader->documentLoader()->setQuickLookHandle(QuickLookHandle::create(resourceLoader(), response.nsURLResponse()));
aestes@apple.com06613142014-08-07 00:16:36 +0000120 if (!m_coreLoader->documentLoader()->quickLookHandle())
aestes@apple.com08cc6992014-08-05 17:49:44 +0000121#endif
antti@apple.com6d2fe7d2014-09-09 08:19:16 +0000122 m_coreLoader->didReceiveResponse(response);
andersca@apple.com0ab76de2013-04-22 22:56:19 +0000123
beidson@apple.com6b677332013-04-30 19:21:07 +0000124 // 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 +0000125 if (!m_coreLoader)
126 return;
127
ap@apple.come2515012013-04-28 03:22:55 +0000128 if (needsContinueDidReceiveResponseMessage)
andersca@apple.com0ab76de2013-04-22 22:56:19 +0000129 send(Messages::NetworkResourceLoader::ContinueDidReceiveResponse());
beidson@apple.comaec2e292012-11-14 23:59:01 +0000130}
131
andersca@apple.com69388f02013-12-30 20:20:02 +0000132void WebResourceLoader::didReceiveData(const IPC::DataReference& data, int64_t encodedDataLength)
beidson@apple.comaec2e292012-11-14 23:59:01 +0000133{
134 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 +0000135
136#if USE(QUICK_LOOK)
aestes@apple.com06613142014-08-07 00:16:36 +0000137 if (QuickLookHandle* quickLookHandle = m_coreLoader->documentLoader()->quickLookHandle()) {
138 if (quickLookHandle->didReceiveData(adoptCF(CFDataCreate(kCFAllocatorDefault, data.data(), data.size())).get()))
commit-queue@webkit.org1cabcea2014-03-19 19:24:30 +0000139 return;
140 }
141#endif
beidson@apple.comc289a142013-03-13 21:36:29 +0000142 m_coreLoader->didReceiveData(reinterpret_cast<const char*>(data.data()), data.size(), encodedDataLength, DataPayloadBytes);
beidson@apple.comaec2e292012-11-14 23:59:01 +0000143}
144
145void WebResourceLoader::didFinishResourceLoad(double finishTime)
146{
147 LOG(Network, "(WebProcess) WebResourceLoader::didFinishResourceLoad for '%s'", m_coreLoader->url().string().utf8().data());
commit-queue@webkit.org1cabcea2014-03-19 19:24:30 +0000148
149#if USE(QUICK_LOOK)
aestes@apple.com06613142014-08-07 00:16:36 +0000150 if (QuickLookHandle* quickLookHandle = m_coreLoader->documentLoader()->quickLookHandle()) {
151 if (quickLookHandle->didFinishLoading())
152 return;
153 }
commit-queue@webkit.org1cabcea2014-03-19 19:24:30 +0000154#endif
beidson@apple.comaec2e292012-11-14 23:59:01 +0000155 m_coreLoader->didFinishLoading(finishTime);
156}
157
158void WebResourceLoader::didFailResourceLoad(const ResourceError& error)
159{
160 LOG(Network, "(WebProcess) WebResourceLoader::didFailResourceLoad for '%s'", m_coreLoader->url().string().utf8().data());
161
commit-queue@webkit.org1cabcea2014-03-19 19:24:30 +0000162#if USE(QUICK_LOOK)
aestes@apple.com06613142014-08-07 00:16:36 +0000163 if (QuickLookHandle* quickLookHandle = m_coreLoader->documentLoader()->quickLookHandle())
164 quickLookHandle->didFail();
commit-queue@webkit.org1cabcea2014-03-19 19:24:30 +0000165#endif
ap@apple.com178e91d2014-05-18 05:29:36 +0000166 if (m_coreLoader->documentLoader()->applicationCacheHost()->maybeLoadFallbackForError(m_coreLoader.get(), error))
167 return;
beidson@apple.comaec2e292012-11-14 23:59:01 +0000168 m_coreLoader->didFail(error);
169}
170
ossy@webkit.orgb72e86c02013-10-10 15:35:02 +0000171#if ENABLE(SHAREABLE_RESOURCE)
beidson@apple.comaec2e292012-11-14 23:59:01 +0000172void WebResourceLoader::didReceiveResource(const ShareableResource::Handle& handle, double finishTime)
173{
174 LOG(Network, "(WebProcess) WebResourceLoader::didReceiveResource for '%s'", m_coreLoader->url().string().utf8().data());
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()) {
aestes@apple.com425d3142014-05-23 00:40:23 +0000178 RetainPtr<CFDataRef> cfBuffer = handle.tryWrapInCFData();
179 if (cfBuffer) {
aestes@apple.com06613142014-08-07 00:16:36 +0000180 if (quickLookHandle->didReceiveData(cfBuffer.get())) {
181 quickLookHandle->didFinishLoading();
aestes@apple.com425d3142014-05-23 00:40:23 +0000182 return;
183 }
184 } else
aestes@apple.com06613142014-08-07 00:16:36 +0000185 quickLookHandle->didFail();
aestes@apple.com425d3142014-05-23 00:40:23 +0000186 }
187#endif
188
beidson@apple.com21302482013-03-22 00:14:03 +0000189 RefPtr<SharedBuffer> buffer = handle.tryWrapInSharedBuffer();
190 if (!buffer) {
191 LOG_ERROR("Unable to create buffer from ShareableResource sent from the network process.");
beidson@apple.com472f6d22013-03-18 18:05:26 +0000192 m_coreLoader->didFail(internalError(m_coreLoader->request().url()));
193 return;
194 }
beidson@apple.comaec2e292012-11-14 23:59:01 +0000195
akling@apple.coma34e5222013-09-10 03:44:05 +0000196 Ref<WebResourceLoader> protect(*this);
beidson@apple.comf8d64742013-03-29 23:02:28 +0000197
beidson@apple.comaec2e292012-11-14 23:59:01 +0000198 // Only send data to the didReceiveData callback if it exists.
beidson@apple.com21302482013-03-22 00:14:03 +0000199 if (buffer->size())
beidson@apple.com472f6d22013-03-18 18:05:26 +0000200 m_coreLoader->didReceiveBuffer(buffer.get(), buffer->size(), DataPayloadWholeResource);
beidson@apple.comaec2e292012-11-14 23:59:01 +0000201
beidson@apple.comf8d64742013-03-29 23:02:28 +0000202 if (!m_coreLoader)
203 return;
204
beidson@apple.comaec2e292012-11-14 23:59:01 +0000205 m_coreLoader->didFinishLoading(finishTime);
206}
ossy@webkit.orgb72e86c02013-10-10 15:35:02 +0000207#endif
beidson@apple.comaec2e292012-11-14 23:59:01 +0000208
ossy@webkit.orgba31fed2013-09-17 08:20:21 +0000209#if USE(PROTECTION_SPACE_AUTH_CALLBACK)
ap@apple.comcd1fa452013-03-26 21:27:45 +0000210void WebResourceLoader::canAuthenticateAgainstProtectionSpace(const ProtectionSpace& protectionSpace)
beidson@apple.com29824ff2012-11-19 19:22:48 +0000211{
akling@apple.coma34e5222013-09-10 03:44:05 +0000212 Ref<WebResourceLoader> protect(*this);
beidson@apple.comf8d64742013-03-29 23:02:28 +0000213
ap@apple.comcd1fa452013-03-26 21:27:45 +0000214 bool result = m_coreLoader->canAuthenticateAgainstProtectionSpace(protectionSpace);
beidson@apple.comf8d64742013-03-29 23:02:28 +0000215
216 if (!m_coreLoader)
217 return;
218
ap@apple.comcd1fa452013-03-26 21:27:45 +0000219 send(Messages::NetworkResourceLoader::ContinueCanAuthenticateAgainstProtectionSpace(result));
beidson@apple.com29824ff2012-11-19 19:22:48 +0000220}
ossy@webkit.orgba31fed2013-09-17 08:20:21 +0000221#endif
beidson@apple.com29824ff2012-11-19 19:22:48 +0000222
beidson@apple.com12ab9642012-11-14 19:43:42 +0000223} // namespace WebKit
224
225#endif // ENABLE(NETWORK_PROCESS)