beidson@apple.com | 12ab964 | 2012-11-14 19:43:42 +0000 | [diff] [blame] | 1 | /* |
ap@apple.com | cd1fa45 | 2013-03-26 21:27:45 +0000 | [diff] [blame] | 2 | * Copyright (C) 2012, 2013 Apple Inc. All rights reserved. |
beidson@apple.com | 12ab964 | 2012-11-14 19:43:42 +0000 | [diff] [blame] | 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 "WebResourceLoader.h" |
| 28 | |
beidson@apple.com | aec2e29 | 2012-11-14 23:59:01 +0000 | [diff] [blame] | 29 | #include "DataReference.h" |
| 30 | #include "Logging.h" |
beidson@apple.com | aec2e29 | 2012-11-14 23:59:01 +0000 | [diff] [blame] | 31 | #include "NetworkProcessConnection.h" |
ap@apple.com | cd1fa45 | 2013-03-26 21:27:45 +0000 | [diff] [blame] | 32 | #include "NetworkResourceLoaderMessages.h" |
beidson@apple.com | aec2e29 | 2012-11-14 23:59:01 +0000 | [diff] [blame] | 33 | #include "WebCoreArgumentCoders.h" |
beidson@apple.com | 1bc450a | 2012-12-21 18:46:04 +0000 | [diff] [blame] | 34 | #include "WebErrors.h" |
beidson@apple.com | aec2e29 | 2012-11-14 23:59:01 +0000 | [diff] [blame] | 35 | #include "WebProcess.h" |
ap@apple.com | 178e91d | 2014-05-18 05:29:36 +0000 | [diff] [blame] | 36 | #include <WebCore/ApplicationCacheHost.h> |
ossy@webkit.org | 84c850d | 2013-12-12 12:30:58 +0000 | [diff] [blame] | 37 | #include <WebCore/CertificateInfo.h> |
andersca@apple.com | 0ab76de | 2013-04-22 22:56:19 +0000 | [diff] [blame] | 38 | #include <WebCore/DocumentLoader.h> |
ap@apple.com | 18ae25f | 2013-01-27 01:01:08 +0000 | [diff] [blame] | 39 | #include <WebCore/ResourceError.h> |
beidson@apple.com | 12ab964 | 2012-11-14 19:43:42 +0000 | [diff] [blame] | 40 | #include <WebCore/ResourceLoader.h> |
aestes@apple.com | 564640a | 2015-08-14 21:08:13 +0000 | [diff] [blame] | 41 | #include <WebCore/SubresourceLoader.h> |
beidson@apple.com | 12ab964 | 2012-11-14 19:43:42 +0000 | [diff] [blame] | 42 | |
| 43 | using namespace WebCore; |
| 44 | |
| 45 | namespace WebKit { |
| 46 | |
gyuyoung.kim@webkit.org | 0de8706 | 2015-07-09 02:31:21 +0000 | [diff] [blame] | 47 | Ref<WebResourceLoader> WebResourceLoader::create(PassRefPtr<ResourceLoader> coreLoader) |
beidson@apple.com | 12ab964 | 2012-11-14 19:43:42 +0000 | [diff] [blame] | 48 | { |
gyuyoung.kim@webkit.org | 0de8706 | 2015-07-09 02:31:21 +0000 | [diff] [blame] | 49 | return adoptRef(*new WebResourceLoader(coreLoader)); |
beidson@apple.com | 12ab964 | 2012-11-14 19:43:42 +0000 | [diff] [blame] | 50 | } |
| 51 | |
beidson@apple.com | aec2e29 | 2012-11-14 23:59:01 +0000 | [diff] [blame] | 52 | WebResourceLoader::WebResourceLoader(PassRefPtr<WebCore::ResourceLoader> coreLoader) |
| 53 | : m_coreLoader(coreLoader) |
beidson@apple.com | 12ab964 | 2012-11-14 19:43:42 +0000 | [diff] [blame] | 54 | { |
| 55 | } |
| 56 | |
| 57 | WebResourceLoader::~WebResourceLoader() |
| 58 | { |
| 59 | } |
| 60 | |
andersca@apple.com | 69388f0 | 2013-12-30 20:20:02 +0000 | [diff] [blame] | 61 | IPC::Connection* WebResourceLoader::messageSenderConnection() |
beidson@apple.com | 7fb049d | 2012-11-17 00:40:15 +0000 | [diff] [blame] | 62 | { |
cdumez@apple.com | 4faf1ef | 2015-01-30 20:49:33 +0000 | [diff] [blame] | 63 | return WebProcess::singleton().networkConnection()->connection(); |
beidson@apple.com | 7fb049d | 2012-11-17 00:40:15 +0000 | [diff] [blame] | 64 | } |
| 65 | |
andersca@apple.com | d4aa592 | 2013-05-25 00:17:20 +0000 | [diff] [blame] | 66 | uint64_t WebResourceLoader::messageSenderDestinationID() |
beidson@apple.com | 7fb049d | 2012-11-17 00:40:15 +0000 | [diff] [blame] | 67 | { |
| 68 | return m_coreLoader->identifier(); |
| 69 | } |
| 70 | |
beidson@apple.com | f8d6474 | 2013-03-29 23:02:28 +0000 | [diff] [blame] | 71 | void WebResourceLoader::detachFromCoreLoader() |
| 72 | { |
cdumez@apple.com | d839ea1 | 2015-07-04 19:42:18 +0000 | [diff] [blame] | 73 | m_coreLoader = nullptr; |
beidson@apple.com | f8d6474 | 2013-03-29 23:02:28 +0000 | [diff] [blame] | 74 | } |
| 75 | |
ap@apple.com | cd1fa45 | 2013-03-26 21:27:45 +0000 | [diff] [blame] | 76 | void WebResourceLoader::willSendRequest(const ResourceRequest& proposedRequest, const ResourceResponse& redirectResponse) |
beidson@apple.com | aec2e29 | 2012-11-14 23:59:01 +0000 | [diff] [blame] | 77 | { |
| 78 | LOG(Network, "(WebProcess) WebResourceLoader::willSendRequest to '%s'", proposedRequest.url().string().utf8().data()); |
beidson@apple.com | f8d6474 | 2013-03-29 23:02:28 +0000 | [diff] [blame] | 79 | |
jpfau@apple.com | fb06c00 | 2014-12-02 04:28:25 +0000 | [diff] [blame] | 80 | RefPtr<WebResourceLoader> protect(this); |
ap@apple.com | 178e91d | 2014-05-18 05:29:36 +0000 | [diff] [blame] | 81 | |
ap@apple.com | cd1fa45 | 2013-03-26 21:27:45 +0000 | [diff] [blame] | 82 | ResourceRequest newRequest = proposedRequest; |
ap@apple.com | 178e91d | 2014-05-18 05:29:36 +0000 | [diff] [blame] | 83 | if (m_coreLoader->documentLoader()->applicationCacheHost()->maybeLoadFallbackForRedirect(m_coreLoader.get(), newRequest, redirectResponse)) |
| 84 | return; |
commit-queue@webkit.org | 0edb411 | 2014-09-11 17:20:18 +0000 | [diff] [blame] | 85 | // FIXME: Do we need to update NetworkResourceLoader clientCredentialPolicy in case loader policy is DoNotAskClientForCrossOriginCredentials? |
aestes@apple.com | 13aae08 | 2016-01-02 08:03:08 +0000 | [diff] [blame] | 86 | m_coreLoader->willSendRequest(WTFMove(newRequest), redirectResponse, [protect](ResourceRequest&& request) { |
jpfau@apple.com | fb06c00 | 2014-12-02 04:28:25 +0000 | [diff] [blame] | 87 | if (!protect->m_coreLoader) |
| 88 | return; |
| 89 | |
commit-queue@webkit.org | 599506b | 2015-08-04 20:48:47 +0000 | [diff] [blame] | 90 | protect->send(Messages::NetworkResourceLoader::ContinueWillSendRequest(request)); |
jpfau@apple.com | fb06c00 | 2014-12-02 04:28:25 +0000 | [diff] [blame] | 91 | }); |
beidson@apple.com | aec2e29 | 2012-11-14 23:59:01 +0000 | [diff] [blame] | 92 | } |
| 93 | |
ap@apple.com | 0c05506 | 2013-04-03 18:23:34 +0000 | [diff] [blame] | 94 | void WebResourceLoader::didSendData(uint64_t bytesSent, uint64_t totalBytesToBeSent) |
| 95 | { |
| 96 | m_coreLoader->didSendData(bytesSent, totalBytesToBeSent); |
| 97 | } |
| 98 | |
antti@apple.com | 6d2fe7d | 2014-09-09 08:19:16 +0000 | [diff] [blame] | 99 | void WebResourceLoader::didReceiveResponse(const ResourceResponse& response, bool needsContinueDidReceiveResponseMessage) |
beidson@apple.com | aec2e29 | 2012-11-14 23:59:01 +0000 | [diff] [blame] | 100 | { |
mcatanzaro@igalia.com | 8ba7815 | 2016-01-01 00:42:09 +0000 | [diff] [blame] | 101 | LOG(Network, "(WebProcess) WebResourceLoader::didReceiveResponse for '%s'. Status %d.", m_coreLoader->url().string().utf8().data(), response.httpStatusCode()); |
andersca@apple.com | 028c073 | 2013-04-23 21:56:59 +0000 | [diff] [blame] | 102 | |
akling@apple.com | a34e522 | 2013-09-10 03:44:05 +0000 | [diff] [blame] | 103 | Ref<WebResourceLoader> protect(*this); |
andersca@apple.com | 028c073 | 2013-04-23 21:56:59 +0000 | [diff] [blame] | 104 | |
antti@apple.com | 6d2fe7d | 2014-09-09 08:19:16 +0000 | [diff] [blame] | 105 | if (m_coreLoader->documentLoader()->applicationCacheHost()->maybeLoadFallbackForResponse(m_coreLoader.get(), response)) |
ap@apple.com | 178e91d | 2014-05-18 05:29:36 +0000 | [diff] [blame] | 106 | return; |
aestes@apple.com | 08cc699 | 2014-08-05 17:49:44 +0000 | [diff] [blame] | 107 | |
antti@apple.com | 5ffeafc | 2014-10-20 17:17:32 +0000 | [diff] [blame] | 108 | bool shoudCallCoreLoaderDidReceiveResponse = true; |
aestes@apple.com | 08cc699 | 2014-08-05 17:49:44 +0000 | [diff] [blame] | 109 | #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.com | 5ffeafc | 2014-10-20 17:17:32 +0000 | [diff] [blame] | 113 | 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.com | 08cc699 | 2014-08-05 17:49:44 +0000 | [diff] [blame] | 118 | #endif |
antti@apple.com | 5ffeafc | 2014-10-20 17:17:32 +0000 | [diff] [blame] | 119 | if (shoudCallCoreLoaderDidReceiveResponse) |
antti@apple.com | 6d2fe7d | 2014-09-09 08:19:16 +0000 | [diff] [blame] | 120 | m_coreLoader->didReceiveResponse(response); |
andersca@apple.com | 0ab76de | 2013-04-22 22:56:19 +0000 | [diff] [blame] | 121 | |
beidson@apple.com | 6b67733 | 2013-04-30 19:21:07 +0000 | [diff] [blame] | 122 | // If m_coreLoader becomes null as a result of the didReceiveResponse callback, we can't use the send function(). |
ap@apple.com | 63cec80 | 2013-04-29 18:08:57 +0000 | [diff] [blame] | 123 | if (!m_coreLoader) |
| 124 | return; |
| 125 | |
ap@apple.com | e251501 | 2013-04-28 03:22:55 +0000 | [diff] [blame] | 126 | if (needsContinueDidReceiveResponseMessage) |
andersca@apple.com | 0ab76de | 2013-04-22 22:56:19 +0000 | [diff] [blame] | 127 | send(Messages::NetworkResourceLoader::ContinueDidReceiveResponse()); |
beidson@apple.com | aec2e29 | 2012-11-14 23:59:01 +0000 | [diff] [blame] | 128 | } |
| 129 | |
andersca@apple.com | 69388f0 | 2013-12-30 20:20:02 +0000 | [diff] [blame] | 130 | void WebResourceLoader::didReceiveData(const IPC::DataReference& data, int64_t encodedDataLength) |
beidson@apple.com | aec2e29 | 2012-11-14 23:59:01 +0000 | [diff] [blame] | 131 | { |
| 132 | LOG(Network, "(WebProcess) WebResourceLoader::didReceiveData of size %i for '%s'", (int)data.size(), m_coreLoader->url().string().utf8().data()); |
commit-queue@webkit.org | 1cabcea | 2014-03-19 19:24:30 +0000 | [diff] [blame] | 133 | |
| 134 | #if USE(QUICK_LOOK) |
aestes@apple.com | 0661314 | 2014-08-07 00:16:36 +0000 | [diff] [blame] | 135 | if (QuickLookHandle* quickLookHandle = m_coreLoader->documentLoader()->quickLookHandle()) { |
| 136 | if (quickLookHandle->didReceiveData(adoptCF(CFDataCreate(kCFAllocatorDefault, data.data(), data.size())).get())) |
commit-queue@webkit.org | 1cabcea | 2014-03-19 19:24:30 +0000 | [diff] [blame] | 137 | return; |
| 138 | } |
| 139 | #endif |
beidson@apple.com | c289a14 | 2013-03-13 21:36:29 +0000 | [diff] [blame] | 140 | m_coreLoader->didReceiveData(reinterpret_cast<const char*>(data.data()), data.size(), encodedDataLength, DataPayloadBytes); |
beidson@apple.com | aec2e29 | 2012-11-14 23:59:01 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void WebResourceLoader::didFinishResourceLoad(double finishTime) |
| 144 | { |
| 145 | LOG(Network, "(WebProcess) WebResourceLoader::didFinishResourceLoad for '%s'", m_coreLoader->url().string().utf8().data()); |
commit-queue@webkit.org | 1cabcea | 2014-03-19 19:24:30 +0000 | [diff] [blame] | 146 | |
| 147 | #if USE(QUICK_LOOK) |
aestes@apple.com | 0661314 | 2014-08-07 00:16:36 +0000 | [diff] [blame] | 148 | if (QuickLookHandle* quickLookHandle = m_coreLoader->documentLoader()->quickLookHandle()) { |
| 149 | if (quickLookHandle->didFinishLoading()) |
| 150 | return; |
| 151 | } |
commit-queue@webkit.org | 1cabcea | 2014-03-19 19:24:30 +0000 | [diff] [blame] | 152 | #endif |
beidson@apple.com | aec2e29 | 2012-11-14 23:59:01 +0000 | [diff] [blame] | 153 | m_coreLoader->didFinishLoading(finishTime); |
| 154 | } |
| 155 | |
| 156 | void WebResourceLoader::didFailResourceLoad(const ResourceError& error) |
| 157 | { |
| 158 | LOG(Network, "(WebProcess) WebResourceLoader::didFailResourceLoad for '%s'", m_coreLoader->url().string().utf8().data()); |
| 159 | |
commit-queue@webkit.org | 1cabcea | 2014-03-19 19:24:30 +0000 | [diff] [blame] | 160 | #if USE(QUICK_LOOK) |
aestes@apple.com | 0661314 | 2014-08-07 00:16:36 +0000 | [diff] [blame] | 161 | if (QuickLookHandle* quickLookHandle = m_coreLoader->documentLoader()->quickLookHandle()) |
| 162 | quickLookHandle->didFail(); |
commit-queue@webkit.org | 1cabcea | 2014-03-19 19:24:30 +0000 | [diff] [blame] | 163 | #endif |
ap@apple.com | 178e91d | 2014-05-18 05:29:36 +0000 | [diff] [blame] | 164 | if (m_coreLoader->documentLoader()->applicationCacheHost()->maybeLoadFallbackForError(m_coreLoader.get(), error)) |
| 165 | return; |
beidson@apple.com | aec2e29 | 2012-11-14 23:59:01 +0000 | [diff] [blame] | 166 | m_coreLoader->didFail(error); |
| 167 | } |
| 168 | |
ossy@webkit.org | b72e86c0 | 2013-10-10 15:35:02 +0000 | [diff] [blame] | 169 | #if ENABLE(SHAREABLE_RESOURCE) |
beidson@apple.com | aec2e29 | 2012-11-14 23:59:01 +0000 | [diff] [blame] | 170 | void 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.org | fc025bc | 2015-05-20 07:04:34 +0000 | [diff] [blame] | 174 | RefPtr<SharedBuffer> buffer = handle.tryWrapInSharedBuffer(); |
| 175 | |
aestes@apple.com | 425d314 | 2014-05-23 00:40:23 +0000 | [diff] [blame] | 176 | #if USE(QUICK_LOOK) |
aestes@apple.com | 0661314 | 2014-08-07 00:16:36 +0000 | [diff] [blame] | 177 | if (QuickLookHandle* quickLookHandle = m_coreLoader->documentLoader()->quickLookHandle()) { |
carlosgc@webkit.org | fc025bc | 2015-05-20 07:04:34 +0000 | [diff] [blame] | 178 | if (buffer) { |
| 179 | if (quickLookHandle->didReceiveData(buffer->existingCFData())) { |
aestes@apple.com | 0661314 | 2014-08-07 00:16:36 +0000 | [diff] [blame] | 180 | quickLookHandle->didFinishLoading(); |
aestes@apple.com | 425d314 | 2014-05-23 00:40:23 +0000 | [diff] [blame] | 181 | return; |
| 182 | } |
| 183 | } else |
aestes@apple.com | 0661314 | 2014-08-07 00:16:36 +0000 | [diff] [blame] | 184 | quickLookHandle->didFail(); |
aestes@apple.com | 425d314 | 2014-05-23 00:40:23 +0000 | [diff] [blame] | 185 | } |
| 186 | #endif |
| 187 | |
beidson@apple.com | 2130248 | 2013-03-22 00:14:03 +0000 | [diff] [blame] | 188 | if (!buffer) { |
| 189 | LOG_ERROR("Unable to create buffer from ShareableResource sent from the network process."); |
beidson@apple.com | 472f6d2 | 2013-03-18 18:05:26 +0000 | [diff] [blame] | 190 | m_coreLoader->didFail(internalError(m_coreLoader->request().url())); |
| 191 | return; |
| 192 | } |
beidson@apple.com | aec2e29 | 2012-11-14 23:59:01 +0000 | [diff] [blame] | 193 | |
akling@apple.com | a34e522 | 2013-09-10 03:44:05 +0000 | [diff] [blame] | 194 | Ref<WebResourceLoader> protect(*this); |
beidson@apple.com | f8d6474 | 2013-03-29 23:02:28 +0000 | [diff] [blame] | 195 | |
beidson@apple.com | aec2e29 | 2012-11-14 23:59:01 +0000 | [diff] [blame] | 196 | // Only send data to the didReceiveData callback if it exists. |
carlosgc@webkit.org | 9fb0c4f | 2016-03-01 17:42:35 +0000 | [diff] [blame^] | 197 | if (unsigned bufferSize = buffer->size()) |
| 198 | m_coreLoader->didReceiveBuffer(buffer.release(), bufferSize, DataPayloadWholeResource); |
beidson@apple.com | aec2e29 | 2012-11-14 23:59:01 +0000 | [diff] [blame] | 199 | |
beidson@apple.com | f8d6474 | 2013-03-29 23:02:28 +0000 | [diff] [blame] | 200 | if (!m_coreLoader) |
| 201 | return; |
| 202 | |
beidson@apple.com | aec2e29 | 2012-11-14 23:59:01 +0000 | [diff] [blame] | 203 | m_coreLoader->didFinishLoading(finishTime); |
| 204 | } |
ossy@webkit.org | b72e86c0 | 2013-10-10 15:35:02 +0000 | [diff] [blame] | 205 | #endif |
beidson@apple.com | aec2e29 | 2012-11-14 23:59:01 +0000 | [diff] [blame] | 206 | |
ossy@webkit.org | ba31fed | 2013-09-17 08:20:21 +0000 | [diff] [blame] | 207 | #if USE(PROTECTION_SPACE_AUTH_CALLBACK) |
ap@apple.com | cd1fa45 | 2013-03-26 21:27:45 +0000 | [diff] [blame] | 208 | void WebResourceLoader::canAuthenticateAgainstProtectionSpace(const ProtectionSpace& protectionSpace) |
beidson@apple.com | 29824ff | 2012-11-19 19:22:48 +0000 | [diff] [blame] | 209 | { |
achristensen@apple.com | 5c468c5 | 2016-02-02 23:42:40 +0000 | [diff] [blame] | 210 | bool result = m_coreLoader ? m_coreLoader->canAuthenticateAgainstProtectionSpace(protectionSpace) : false; |
achristensen@apple.com | cba6fe9 | 2016-01-09 01:21:57 +0000 | [diff] [blame] | 211 | |
ap@apple.com | cd1fa45 | 2013-03-26 21:27:45 +0000 | [diff] [blame] | 212 | send(Messages::NetworkResourceLoader::ContinueCanAuthenticateAgainstProtectionSpace(result)); |
beidson@apple.com | 29824ff | 2012-11-19 19:22:48 +0000 | [diff] [blame] | 213 | } |
ossy@webkit.org | ba31fed | 2013-09-17 08:20:21 +0000 | [diff] [blame] | 214 | #endif |
beidson@apple.com | 29824ff | 2012-11-19 19:22:48 +0000 | [diff] [blame] | 215 | |
beidson@apple.com | 12ab964 | 2012-11-14 19:43:42 +0000 | [diff] [blame] | 216 | } // namespace WebKit |