1b42615def448e7f2b29a4d0e1afe46627d7abcd
[WebKit-https.git] / Source / WebKit / WebProcess / Network / WebResourceLoader.cpp
1 /*
2  * Copyright (C) 2012-2017 Apple Inc. All rights reserved.
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
29 #include "DataReference.h"
30 #include "Logging.h"
31 #include "NetworkProcessConnection.h"
32 #include "NetworkResourceLoaderMessages.h"
33 #include "WebCoreArgumentCoders.h"
34 #include "WebErrors.h"
35 #include "WebProcess.h"
36 #include <WebCore/ApplicationCacheHost.h>
37 #include <WebCore/CertificateInfo.h>
38 #include <WebCore/DiagnosticLoggingClient.h>
39 #include <WebCore/DiagnosticLoggingKeys.h>
40 #include <WebCore/DocumentLoader.h>
41 #include <WebCore/Frame.h>
42 #include <WebCore/Page.h>
43 #include <WebCore/ResourceError.h>
44 #include <WebCore/ResourceLoader.h>
45 #include <WebCore/SubresourceLoader.h>
46 #include <wtf/CompletionHandler.h>
47
48 using namespace WebCore;
49
50 #define RELEASE_LOG_IF_ALLOWED(fmt, ...) RELEASE_LOG_IF(isAlwaysOnLoggingAllowed(), Network, "%p - WebResourceLoader::" fmt, this, ##__VA_ARGS__)
51
52 namespace WebKit {
53
54 Ref<WebResourceLoader> WebResourceLoader::create(Ref<ResourceLoader>&& coreLoader, const TrackingParameters& trackingParameters)
55 {
56     return adoptRef(*new WebResourceLoader(WTFMove(coreLoader), trackingParameters));
57 }
58
59 WebResourceLoader::WebResourceLoader(Ref<WebCore::ResourceLoader>&& coreLoader, const TrackingParameters& trackingParameters)
60     : m_coreLoader(WTFMove(coreLoader))
61     , m_trackingParameters(trackingParameters)
62 {
63 }
64
65 WebResourceLoader::~WebResourceLoader()
66 {
67 }
68
69 IPC::Connection* WebResourceLoader::messageSenderConnection()
70 {
71     return &WebProcess::singleton().ensureNetworkProcessConnection().connection();
72 }
73
74 uint64_t WebResourceLoader::messageSenderDestinationID()
75 {
76     return m_coreLoader->identifier();
77 }
78
79 void WebResourceLoader::detachFromCoreLoader()
80 {
81     m_coreLoader = nullptr;
82 }
83
84 void WebResourceLoader::willSendRequest(ResourceRequest&& proposedRequest, ResourceResponse&& redirectResponse)
85 {
86     LOG(Network, "(WebProcess) WebResourceLoader::willSendRequest to '%s'", proposedRequest.url().string().latin1().data());
87     RELEASE_LOG_IF_ALLOWED("willSendRequest: (pageID = %" PRIu64 ", frameID = %" PRIu64 ", resourceID = %" PRIu64 ")", m_trackingParameters.pageID, m_trackingParameters.frameID, m_trackingParameters.resourceID);
88
89     if (m_coreLoader->documentLoader()->applicationCacheHost().maybeLoadFallbackForRedirect(m_coreLoader.get(), proposedRequest, redirectResponse))
90         return;
91
92     m_coreLoader->willSendRequest(WTFMove(proposedRequest), redirectResponse, [protectedThis = makeRef(*this)](ResourceRequest&& request) {
93         if (!protectedThis->m_coreLoader)
94             return;
95
96         protectedThis->send(Messages::NetworkResourceLoader::ContinueWillSendRequest(request, protectedThis->m_coreLoader->isAllowedToAskUserForCredentials()));
97     });
98 }
99
100 void WebResourceLoader::didSendData(uint64_t bytesSent, uint64_t totalBytesToBeSent)
101 {
102     m_coreLoader->didSendData(bytesSent, totalBytesToBeSent);
103 }
104
105 void WebResourceLoader::didReceiveResponse(const ResourceResponse& response, bool needsContinueDidReceiveResponseMessage)
106 {
107     LOG(Network, "(WebProcess) WebResourceLoader::didReceiveResponse for '%s'. Status %d.", m_coreLoader->url().string().latin1().data(), response.httpStatusCode());
108     RELEASE_LOG_IF_ALLOWED("didReceiveResponse: (pageID = %" PRIu64 ", frameID = %" PRIu64 ", resourceID = %" PRIu64 ", status = %d)", m_trackingParameters.pageID, m_trackingParameters.frameID, m_trackingParameters.resourceID, response.httpStatusCode());
109
110     Ref<WebResourceLoader> protectedThis(*this);
111
112     if (m_coreLoader->documentLoader()->applicationCacheHost().maybeLoadFallbackForResponse(m_coreLoader.get(), response))
113         return;
114
115     CompletionHandler<void()> policyDecisionCompletionHandler;
116     if (needsContinueDidReceiveResponseMessage) {
117         policyDecisionCompletionHandler = [this, protectedThis = WTFMove(protectedThis)] {
118             // If m_coreLoader becomes null as a result of the didReceiveResponse callback, we can't use the send function().
119             if (m_coreLoader)
120                 send(Messages::NetworkResourceLoader::ContinueDidReceiveResponse());
121         };
122     }
123
124     m_coreLoader->didReceiveResponse(response, WTFMove(policyDecisionCompletionHandler));
125 }
126
127 void WebResourceLoader::didReceiveData(const IPC::DataReference& data, int64_t encodedDataLength)
128 {
129     LOG(Network, "(WebProcess) WebResourceLoader::didReceiveData of size %lu for '%s'", data.size(), m_coreLoader->url().string().latin1().data());
130
131     if (!m_numBytesReceived) {
132         RELEASE_LOG_IF_ALLOWED("didReceiveData: Started receiving data (pageID = %" PRIu64 ", frameID = %" PRIu64 ", resourceID = %" PRIu64 ")", m_trackingParameters.pageID, m_trackingParameters.frameID, m_trackingParameters.resourceID);
133     }
134     m_numBytesReceived += data.size();
135
136     m_coreLoader->didReceiveData(reinterpret_cast<const char*>(data.data()), data.size(), encodedDataLength, DataPayloadBytes);
137 }
138
139 void WebResourceLoader::didRetrieveDerivedData(const String& type, const IPC::DataReference& data)
140 {
141     LOG(Network, "(WebProcess) WebResourceLoader::didRetrieveDerivedData of size %lu for '%s'", data.size(), m_coreLoader->url().string().latin1().data());
142
143     auto buffer = SharedBuffer::create(data.data(), data.size());
144     m_coreLoader->didRetrieveDerivedDataFromCache(type, buffer.get());
145 }
146
147 void WebResourceLoader::didFinishResourceLoad(const NetworkLoadMetrics& networkLoadMetrics)
148 {
149     LOG(Network, "(WebProcess) WebResourceLoader::didFinishResourceLoad for '%s'", m_coreLoader->url().string().latin1().data());
150     RELEASE_LOG_IF_ALLOWED("didFinishResourceLoad: (pageID = %" PRIu64 ", frameID = %" PRIu64 ", resourceID = %" PRIu64 ", length = %zd)", m_trackingParameters.pageID, m_trackingParameters.frameID, m_trackingParameters.resourceID, m_numBytesReceived);
151
152     m_coreLoader->didFinishLoading(networkLoadMetrics);
153 }
154
155 void WebResourceLoader::didFailResourceLoad(const ResourceError& error)
156 {
157     LOG(Network, "(WebProcess) WebResourceLoader::didFailResourceLoad for '%s'", m_coreLoader->url().string().latin1().data());
158     RELEASE_LOG_IF_ALLOWED("didFailResourceLoad: (pageID = %" PRIu64 ", frameID = %" PRIu64 ", resourceID = %" PRIu64 ")", m_trackingParameters.pageID, m_trackingParameters.frameID, m_trackingParameters.resourceID);
159
160     if (m_coreLoader->documentLoader()->applicationCacheHost().maybeLoadFallbackForError(m_coreLoader.get(), error))
161         return;
162     m_coreLoader->didFail(error);
163 }
164
165 void WebResourceLoader::didBlockAuthenticationChallenge()
166 {
167     LOG(Network, "(WebProcess) WebResourceLoader::didBlockAuthenticationChallenge for '%s'", m_coreLoader->url().string().latin1().data());
168     RELEASE_LOG_IF_ALLOWED("didBlockAuthenticationChallenge: (pageID = %" PRIu64 ", frameID = %" PRIu64 ", resourceID = %" PRIu64 ")", m_trackingParameters.pageID, m_trackingParameters.frameID, m_trackingParameters.resourceID);
169
170     m_coreLoader->didBlockAuthenticationChallenge();
171 }
172
173 #if ENABLE(SHAREABLE_RESOURCE)
174 void WebResourceLoader::didReceiveResource(const ShareableResource::Handle& handle)
175 {
176     LOG(Network, "(WebProcess) WebResourceLoader::didReceiveResource for '%s'", m_coreLoader->url().string().latin1().data());
177     RELEASE_LOG_IF_ALLOWED("didReceiveResource: (pageID = %" PRIu64 ", frameID = %" PRIu64 ", resourceID = %" PRIu64 ")", m_trackingParameters.pageID, m_trackingParameters.frameID, m_trackingParameters.resourceID);
178
179     RefPtr<SharedBuffer> buffer = handle.tryWrapInSharedBuffer();
180
181     if (!buffer) {
182         LOG_ERROR("Unable to create buffer from ShareableResource sent from the network process.");
183         RELEASE_LOG_IF_ALLOWED("didReceiveResource: Unable to create SharedBuffer (pageID = %" PRIu64 ", frameID = %" PRIu64 ", resourceID = %" PRIu64 ")", m_trackingParameters.pageID, m_trackingParameters.frameID, m_trackingParameters.resourceID);
184         if (auto* frame = m_coreLoader->frame()) {
185             if (auto* page = frame->page())
186                 page->diagnosticLoggingClient().logDiagnosticMessage(WebCore::DiagnosticLoggingKeys::internalErrorKey(), WebCore::DiagnosticLoggingKeys::createSharedBufferFailedKey(), WebCore::ShouldSample::No);
187         }
188         m_coreLoader->didFail(internalError(m_coreLoader->request().url()));
189         return;
190     }
191
192     Ref<WebResourceLoader> protect(*this);
193
194     // Only send data to the didReceiveData callback if it exists.
195     if (unsigned bufferSize = buffer->size())
196         m_coreLoader->didReceiveBuffer(buffer.releaseNonNull(), bufferSize, DataPayloadWholeResource);
197
198     if (!m_coreLoader)
199         return;
200
201     NetworkLoadMetrics emptyMetrics;
202     m_coreLoader->didFinishLoading(emptyMetrics);
203 }
204 #endif
205
206 bool WebResourceLoader::isAlwaysOnLoggingAllowed() const
207 {
208     return resourceLoader() && resourceLoader()->isAlwaysOnLoggingAllowed();
209 }
210
211 } // namespace WebKit