blob: 07aab38513d2530ee985c6820b2f17af3fca4591 [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"
ap@apple.com61423822012-12-20 00:58:25 +000035#include "PlatformCertificateInfo.h"
beidson@apple.comaec2e292012-11-14 23:59:01 +000036#include "WebCoreArgumentCoders.h"
beidson@apple.com1bc450a2012-12-21 18:46:04 +000037#include "WebErrors.h"
beidson@apple.comaec2e292012-11-14 23:59:01 +000038#include "WebProcess.h"
andersca@apple.com0ab76de2013-04-22 22:56:19 +000039#include <WebCore/DocumentLoader.h>
beidson@apple.com472f6d22013-03-18 18:05:26 +000040#include <WebCore/ResourceBuffer.h>
ap@apple.com18ae25f2013-01-27 01:01:08 +000041#include <WebCore/ResourceError.h>
beidson@apple.com12ab9642012-11-14 19:43:42 +000042#include <WebCore/ResourceLoader.h>
43
44using namespace WebCore;
45
46namespace WebKit {
47
beidson@apple.comaec2e292012-11-14 23:59:01 +000048PassRefPtr<WebResourceLoader> WebResourceLoader::create(PassRefPtr<ResourceLoader> coreLoader)
beidson@apple.com12ab9642012-11-14 19:43:42 +000049{
beidson@apple.comaec2e292012-11-14 23:59:01 +000050 return adoptRef(new WebResourceLoader(coreLoader));
beidson@apple.com12ab9642012-11-14 19:43:42 +000051}
52
beidson@apple.comaec2e292012-11-14 23:59:01 +000053WebResourceLoader::WebResourceLoader(PassRefPtr<WebCore::ResourceLoader> coreLoader)
54 : m_coreLoader(coreLoader)
beidson@apple.com12ab9642012-11-14 19:43:42 +000055{
56}
57
58WebResourceLoader::~WebResourceLoader()
59{
60}
61
beidson@apple.com7fb049d2012-11-17 00:40:15 +000062CoreIPC::Connection* WebResourceLoader::connection() const
63{
64 return WebProcess::shared().networkConnection()->connection();
65}
66
67uint64_t WebResourceLoader::destinationID() const
68{
69 return m_coreLoader->identifier();
70}
71
beidson@apple.com44925772013-01-23 20:21:19 +000072void WebResourceLoader::cancelResourceLoader()
73{
74 m_coreLoader->cancel();
75}
76
beidson@apple.comf8d64742013-03-29 23:02:28 +000077void WebResourceLoader::detachFromCoreLoader()
78{
79 m_coreLoader = 0;
80}
81
ap@apple.comcd1fa452013-03-26 21:27:45 +000082void WebResourceLoader::willSendRequest(const ResourceRequest& proposedRequest, const ResourceResponse& redirectResponse)
beidson@apple.comaec2e292012-11-14 23:59:01 +000083{
84 LOG(Network, "(WebProcess) WebResourceLoader::willSendRequest to '%s'", proposedRequest.url().string().utf8().data());
beidson@apple.comf8d64742013-03-29 23:02:28 +000085
86 RefPtr<WebResourceLoader> protector(this);
beidson@apple.comaec2e292012-11-14 23:59:01 +000087
ap@apple.comcd1fa452013-03-26 21:27:45 +000088 ResourceRequest newRequest = proposedRequest;
beidson@apple.comaec2e292012-11-14 23:59:01 +000089 m_coreLoader->willSendRequest(newRequest, redirectResponse);
beidson@apple.comf8d64742013-03-29 23:02:28 +000090
91 if (!m_coreLoader)
92 return;
93
ap@apple.comcd1fa452013-03-26 21:27:45 +000094 send(Messages::NetworkResourceLoader::ContinueWillSendRequest(newRequest));
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
ap@apple.come2515012013-04-28 03:22:55 +0000102void WebResourceLoader::didReceiveResponseWithCertificateInfo(const ResourceResponse& response, const PlatformCertificateInfo& certificateInfo, bool needsContinueDidReceiveResponseMessage)
beidson@apple.comaec2e292012-11-14 23:59:01 +0000103{
ap@apple.com61423822012-12-20 00:58:25 +0000104 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 +0000105
106 RefPtr<WebResourceLoader> protector(this);
107
ap@apple.com61423822012-12-20 00:58:25 +0000108 ResourceResponse responseCopy(response);
109 responseCopy.setCertificateChain(certificateInfo.certificateChain());
110 m_coreLoader->didReceiveResponse(responseCopy);
andersca@apple.com0ab76de2013-04-22 22:56:19 +0000111
ap@apple.com63cec802013-04-29 18:08:57 +0000112 if (!m_coreLoader)
113 return;
114
ap@apple.come2515012013-04-28 03:22:55 +0000115 if (needsContinueDidReceiveResponseMessage)
andersca@apple.com0ab76de2013-04-22 22:56:19 +0000116 send(Messages::NetworkResourceLoader::ContinueDidReceiveResponse());
beidson@apple.comaec2e292012-11-14 23:59:01 +0000117}
118
beidson@apple.comc289a142013-03-13 21:36:29 +0000119void WebResourceLoader::didReceiveData(const CoreIPC::DataReference& data, int64_t encodedDataLength)
beidson@apple.comaec2e292012-11-14 23:59:01 +0000120{
121 LOG(Network, "(WebProcess) WebResourceLoader::didReceiveData of size %i for '%s'", (int)data.size(), m_coreLoader->url().string().utf8().data());
beidson@apple.comc289a142013-03-13 21:36:29 +0000122 m_coreLoader->didReceiveData(reinterpret_cast<const char*>(data.data()), data.size(), encodedDataLength, DataPayloadBytes);
beidson@apple.comaec2e292012-11-14 23:59:01 +0000123}
124
125void WebResourceLoader::didFinishResourceLoad(double finishTime)
126{
127 LOG(Network, "(WebProcess) WebResourceLoader::didFinishResourceLoad for '%s'", m_coreLoader->url().string().utf8().data());
128 m_coreLoader->didFinishLoading(finishTime);
129}
130
131void WebResourceLoader::didFailResourceLoad(const ResourceError& error)
132{
133 LOG(Network, "(WebProcess) WebResourceLoader::didFailResourceLoad for '%s'", m_coreLoader->url().string().utf8().data());
134
135 m_coreLoader->didFail(error);
136}
137
138void WebResourceLoader::didReceiveResource(const ShareableResource::Handle& handle, double finishTime)
139{
140 LOG(Network, "(WebProcess) WebResourceLoader::didReceiveResource for '%s'", m_coreLoader->url().string().utf8().data());
141
beidson@apple.com21302482013-03-22 00:14:03 +0000142 RefPtr<SharedBuffer> buffer = handle.tryWrapInSharedBuffer();
143 if (!buffer) {
144 LOG_ERROR("Unable to create buffer from ShareableResource sent from the network process.");
beidson@apple.com472f6d22013-03-18 18:05:26 +0000145 m_coreLoader->didFail(internalError(m_coreLoader->request().url()));
146 return;
147 }
beidson@apple.comaec2e292012-11-14 23:59:01 +0000148
beidson@apple.comf8d64742013-03-29 23:02:28 +0000149 RefPtr<WebResourceLoader> protector(this);
150
beidson@apple.comaec2e292012-11-14 23:59:01 +0000151 // Only send data to the didReceiveData callback if it exists.
beidson@apple.com21302482013-03-22 00:14:03 +0000152 if (buffer->size())
beidson@apple.com472f6d22013-03-18 18:05:26 +0000153 m_coreLoader->didReceiveBuffer(buffer.get(), buffer->size(), DataPayloadWholeResource);
beidson@apple.comaec2e292012-11-14 23:59:01 +0000154
beidson@apple.comf8d64742013-03-29 23:02:28 +0000155 if (!m_coreLoader)
156 return;
157
beidson@apple.comaec2e292012-11-14 23:59:01 +0000158 m_coreLoader->didFinishLoading(finishTime);
159}
160
ap@apple.comcd1fa452013-03-26 21:27:45 +0000161void WebResourceLoader::canAuthenticateAgainstProtectionSpace(const ProtectionSpace& protectionSpace)
beidson@apple.com29824ff2012-11-19 19:22:48 +0000162{
beidson@apple.comf8d64742013-03-29 23:02:28 +0000163 RefPtr<WebResourceLoader> protector(this);
164
ap@apple.comcd1fa452013-03-26 21:27:45 +0000165 bool result = m_coreLoader->canAuthenticateAgainstProtectionSpace(protectionSpace);
beidson@apple.comf8d64742013-03-29 23:02:28 +0000166
167 if (!m_coreLoader)
168 return;
169
ap@apple.comcd1fa452013-03-26 21:27:45 +0000170 send(Messages::NetworkResourceLoader::ContinueCanAuthenticateAgainstProtectionSpace(result));
beidson@apple.com29824ff2012-11-19 19:22:48 +0000171}
172
beidson@apple.com12ab9642012-11-14 19:43:42 +0000173} // namespace WebKit
174
175#endif // ENABLE(NETWORK_PROCESS)