Runtime flag for link prefetch and remove link subresource.
[WebKit-https.git] / Source / WebKit / WebProcess / Network / WebLoaderStrategy.cpp
1 /*
2  * Copyright (C) 2012, 2015 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 "WebLoaderStrategy.h"
28
29 #include "DataReference.h"
30 #include "HangDetectionDisabler.h"
31 #include "Logging.h"
32 #include "NetworkConnectionToWebProcessMessages.h"
33 #include "NetworkProcessConnection.h"
34 #include "NetworkResourceLoadParameters.h"
35 #include "SessionTracker.h"
36 #include "WebCompiledContentRuleList.h"
37 #include "WebCoreArgumentCoders.h"
38 #include "WebErrors.h"
39 #include "WebFrame.h"
40 #include "WebFrameLoaderClient.h"
41 #include "WebPage.h"
42 #include "WebPageProxyMessages.h"
43 #include "WebProcess.h"
44 #include "WebResourceLoader.h"
45 #include "WebServiceWorkerProvider.h"
46 #include "WebURLSchemeHandlerProxy.h"
47 #include "WebURLSchemeTaskProxy.h"
48 #include <WebCore/ApplicationCacheHost.h>
49 #include <WebCore/CachedResource.h>
50 #include <WebCore/ContentSecurityPolicy.h>
51 #include <WebCore/DiagnosticLoggingClient.h>
52 #include <WebCore/DiagnosticLoggingKeys.h>
53 #include <WebCore/Document.h>
54 #include <WebCore/DocumentLoader.h>
55 #include <WebCore/FetchOptions.h>
56 #include <WebCore/Frame.h>
57 #include <WebCore/FrameLoader.h>
58 #include <WebCore/NetscapePlugInStreamLoader.h>
59 #include <WebCore/PlatformStrategies.h>
60 #include <WebCore/ReferrerPolicy.h>
61 #include <WebCore/ResourceLoader.h>
62 #include <WebCore/SecurityOrigin.h>
63 #include <WebCore/Settings.h>
64 #include <WebCore/SubresourceLoader.h>
65 #include <WebCore/UserContentProvider.h>
66 #include <pal/SessionID.h>
67 #include <wtf/CompletionHandler.h>
68 #include <wtf/text/CString.h>
69
70 #if USE(QUICK_LOOK)
71 #include <WebCore/QuickLook.h>
72 #endif
73
74 using namespace WebCore;
75
76 #define RELEASE_LOG_IF_ALLOWED(permissionChecker, fmt, ...) RELEASE_LOG_IF(permissionChecker.isAlwaysOnLoggingAllowed(), Network, "%p - WebLoaderStrategy::" fmt, this, ##__VA_ARGS__)
77 #define RELEASE_LOG_ERROR_IF_ALLOWED(permissionChecker, fmt, ...) RELEASE_LOG_ERROR_IF(permissionChecker.isAlwaysOnLoggingAllowed(), Network, "%p - WebLoaderStrategy::" fmt, this, ##__VA_ARGS__)
78
79 namespace WebKit {
80
81 WebLoaderStrategy::WebLoaderStrategy()
82     : m_internallyFailedLoadTimer(RunLoop::main(), this, &WebLoaderStrategy::internallyFailedLoadTimerFired)
83 {
84 }
85
86 WebLoaderStrategy::~WebLoaderStrategy()
87 {
88 }
89
90 void WebLoaderStrategy::loadResource(Frame& frame, CachedResource& resource, ResourceRequest&& request, const ResourceLoaderOptions& options, CompletionHandler<void(RefPtr<SubresourceLoader>&&)>&& completionHandler)
91 {
92     SubresourceLoader::create(frame, resource, WTFMove(request), options, [this, completionHandler = WTFMove(completionHandler), resource = CachedResourceHandle<CachedResource>(&resource), frame = makeRef(frame)] (RefPtr<SubresourceLoader>&& loader) mutable {
93         if (loader)
94             scheduleLoad(*loader, resource.get(), frame->document()->referrerPolicy() == ReferrerPolicy::NoReferrerWhenDowngrade);
95         else
96             RELEASE_LOG_IF_ALLOWED(frame.get(), "loadResource: Unable to create SubresourceLoader (frame = %p", &frame);
97         completionHandler(WTFMove(loader));
98     });
99 }
100
101 void WebLoaderStrategy::schedulePluginStreamLoad(Frame& frame, NetscapePlugInStreamLoaderClient& client, ResourceRequest&& request, CompletionHandler<void(RefPtr<NetscapePlugInStreamLoader>&&)>&& completionHandler)
102 {
103     NetscapePlugInStreamLoader::create(frame, client, WTFMove(request), [this, completionHandler = WTFMove(completionHandler), frame = makeRef(frame)] (RefPtr<NetscapePlugInStreamLoader>&& loader) mutable {
104         if (loader)
105             scheduleLoad(*loader, 0, frame->document()->referrerPolicy() == ReferrerPolicy::NoReferrerWhenDowngrade);
106         completionHandler(WTFMove(loader));
107     });
108 }
109
110 static Seconds maximumBufferingTime(CachedResource* resource)
111 {
112     if (!resource)
113         return 0_s;
114
115     switch (resource->type()) {
116     case CachedResource::Beacon:
117     case CachedResource::CSSStyleSheet:
118     case CachedResource::Script:
119 #if ENABLE(SVG_FONTS)
120     case CachedResource::SVGFontResource:
121 #endif
122     case CachedResource::FontResource:
123 #if ENABLE(APPLICATION_MANIFEST)
124     case CachedResource::ApplicationManifest:
125 #endif
126         return Seconds::infinity();
127     case CachedResource::ImageResource:
128         return 500_ms;
129     case CachedResource::MediaResource:
130         return 50_ms;
131     case CachedResource::MainResource:
132     case CachedResource::Icon:
133     case CachedResource::RawResource:
134     case CachedResource::SVGDocumentResource:
135     case CachedResource::LinkPrefetch:
136 #if ENABLE(VIDEO_TRACK)
137     case CachedResource::TextTrackResource:
138 #endif
139 #if ENABLE(XSLT)
140     case CachedResource::XSLStyleSheet:
141 #endif
142         return 0_s;
143     }
144
145     ASSERT_NOT_REACHED();
146     return 0_s;
147 }
148
149 void WebLoaderStrategy::scheduleLoad(ResourceLoader& resourceLoader, CachedResource* resource, bool shouldClearReferrerOnHTTPSToHTTPRedirect)
150 {
151     ResourceLoadIdentifier identifier = resourceLoader.identifier();
152     ASSERT(identifier);
153
154     auto& frameLoaderClient = resourceLoader.frameLoader()->client();
155
156     WebResourceLoader::TrackingParameters trackingParameters;
157     trackingParameters.pageID = frameLoaderClient.pageID().value();
158     trackingParameters.frameID = frameLoaderClient.frameID().value();
159     trackingParameters.resourceID = identifier;
160     auto sessionID = frameLoaderClient.sessionID();
161
162 #if ENABLE(WEB_ARCHIVE) || ENABLE(MHTML)
163     // If the DocumentLoader schedules this as an archive resource load,
164     // then we should remember the ResourceLoader in our records but not schedule it in the NetworkProcess.
165     if (resourceLoader.documentLoader()->scheduleArchiveLoad(resourceLoader, resourceLoader.request())) {
166         LOG(NetworkScheduling, "(WebProcess) WebLoaderStrategy::scheduleLoad, url '%s' will be handled as an archive resource.", resourceLoader.url().string().utf8().data());
167         RELEASE_LOG_IF_ALLOWED(resourceLoader, "scheduleLoad: URL will be handled as an archive resource (frame = %p, pageID = %" PRIu64 ", frameID = %" PRIu64 ", resourceID = %" PRIu64 ")", resourceLoader.frame(), trackingParameters.pageID, trackingParameters.frameID, identifier);
168         m_webResourceLoaders.set(identifier, WebResourceLoader::create(resourceLoader, trackingParameters));
169         return;
170     }
171 #endif
172
173     if (resourceLoader.documentLoader()->applicationCacheHost().maybeLoadResource(resourceLoader, resourceLoader.request(), resourceLoader.request().url())) {
174         LOG(NetworkScheduling, "(WebProcess) WebLoaderStrategy::scheduleLoad, url '%s' will be loaded from application cache.", resourceLoader.url().string().utf8().data());
175         RELEASE_LOG_IF_ALLOWED(resourceLoader, "scheduleLoad: URL will be loaded from application cache (frame = %p, pageID = %" PRIu64 ", frameID = %" PRIu64 ", resourceID = %" PRIu64 ")", resourceLoader.frame(), trackingParameters.pageID, trackingParameters.frameID, identifier);
176         m_webResourceLoaders.set(identifier, WebResourceLoader::create(resourceLoader, trackingParameters));
177         return;
178     }
179
180     if (resourceLoader.request().url().protocolIsData()) {
181         LOG(NetworkScheduling, "(WebProcess) WebLoaderStrategy::scheduleLoad, url '%s' will be loaded as data.", resourceLoader.url().string().utf8().data());
182         RELEASE_LOG_IF_ALLOWED(resourceLoader, "scheduleLoad: URL will be loaded as data (frame = %p, pageID = %" PRIu64 ", frameID = %" PRIu64 ", resourceID = %" PRIu64 ")", resourceLoader.frame(), trackingParameters.pageID, trackingParameters.frameID, identifier);
183         startLocalLoad(resourceLoader);
184         return;
185     }
186
187 #if USE(QUICK_LOOK)
188     if (isQuickLookPreviewURL(resourceLoader.request().url())) {
189         LOG(NetworkScheduling, "(WebProcess) WebLoaderStrategy::scheduleLoad, url '%s' will be handled as a QuickLook resource.", resourceLoader.url().string().utf8().data());
190         RELEASE_LOG_IF_ALLOWED(resourceLoader, "scheduleLoad: URL will be handled as a QuickLook resource (frame = %p, pageID = %" PRIu64 ", frameID = %" PRIu64 ", resourceID = %" PRIu64 ")", resourceLoader.frame(), trackingParameters.pageID, trackingParameters.frameID, identifier);
191         startLocalLoad(resourceLoader);
192         return;
193     }
194 #endif
195
196 #if USE(SOUP)
197     // For apps that call g_resource_load in a web extension.
198     // https://blogs.gnome.org/alexl/2012/01/26/resources-in-glib/
199     if (resourceLoader.request().url().protocolIs("resource")) {
200         LOG(NetworkScheduling, "(WebProcess) WebLoaderStrategy::scheduleLoad, url '%s' will be handled as a GResource.", resourceLoader.url().string().utf8().data());
201         RELEASE_LOG_IF_ALLOWED(resourceLoader, "scheduleLoad: URL will be handled as a GResource (frame = %p, pageID = %" PRIu64 ", frameID = %" PRIu64 ", resourceID = %" PRIu64 ")", resourceLoader.frame(), trackingParameters.pageID, trackingParameters.frameID, identifier);
202         startLocalLoad(resourceLoader);
203         return;
204     }
205 #endif
206
207 #if ENABLE(SERVICE_WORKER)
208     WebServiceWorkerProvider::singleton().handleFetch(resourceLoader, resource, sessionID, shouldClearReferrerOnHTTPSToHTTPRedirect, [trackingParameters, sessionID, shouldClearReferrerOnHTTPSToHTTPRedirect, maximumBufferingTime = maximumBufferingTime(resource), resourceLoader = makeRef(resourceLoader)] (ServiceWorkerClientFetch::Result result) mutable {
209         if (result != ServiceWorkerClientFetch::Result::Unhandled) {
210             LOG(NetworkScheduling, "(WebProcess) WebLoaderStrategy::scheduleLoad, url '%s' will be scheduled through ServiceWorker handle fetch algorithm", resourceLoader->url().string().latin1().data());
211             return;
212         }
213         if (resourceLoader->options().serviceWorkersMode == ServiceWorkersMode::Only) {
214             callOnMainThread([resourceLoader = WTFMove(resourceLoader)] {
215                 auto error = internalError(resourceLoader->request().url());
216                 error.setType(ResourceError::Type::Cancellation);
217                 resourceLoader->didFail(error);
218             });
219             return;
220         }
221
222         if (!WebProcess::singleton().webLoaderStrategy().tryLoadingUsingURLSchemeHandler(resourceLoader))
223             WebProcess::singleton().webLoaderStrategy().scheduleLoadFromNetworkProcess(resourceLoader.get(), resourceLoader->request(), trackingParameters, sessionID, shouldClearReferrerOnHTTPSToHTTPRedirect, maximumBufferingTime);
224     });
225 #else
226     if (!tryLoadingUsingURLSchemeHandler(resourceLoader))
227         scheduleLoadFromNetworkProcess(resourceLoader, resourceLoader.request(), trackingParameters, sessionID, shouldClearReferrerOnHTTPSToHTTPRedirect, maximumBufferingTime(resource));
228 #endif
229 }
230
231 bool WebLoaderStrategy::tryLoadingUsingURLSchemeHandler(ResourceLoader& resourceLoader)
232 {
233     auto* webFrameLoaderClient = toWebFrameLoaderClient(resourceLoader.frameLoader()->client());
234     auto* webFrame = webFrameLoaderClient ? webFrameLoaderClient->webFrame() : nullptr;
235     auto* webPage = webFrame ? webFrame->page() : nullptr;
236     if (webPage) {
237         if (auto* handler = webPage->urlSchemeHandlerForScheme(resourceLoader.request().url().protocol().toStringWithoutCopying())) {
238             LOG(NetworkScheduling, "(WebProcess) WebLoaderStrategy::scheduleLoad, URL '%s' will be handled by a UIProcess URL scheme handler.", resourceLoader.url().string().utf8().data());
239             RELEASE_LOG_IF_ALLOWED(resourceLoader, "scheduleLoad: URL will be handled by a UIProcess URL scheme handler (frame = %p, resourceID = %lu)", resourceLoader.frame(), resourceLoader.identifier());
240
241             handler->startNewTask(resourceLoader);
242             return true;
243         }
244     }
245     return false;
246 }
247
248 void WebLoaderStrategy::scheduleLoadFromNetworkProcess(ResourceLoader& resourceLoader, const ResourceRequest& request, const WebResourceLoader::TrackingParameters& trackingParameters, PAL::SessionID sessionID, bool shouldClearReferrerOnHTTPSToHTTPRedirect, Seconds maximumBufferingTime)
249 {
250     ResourceLoadIdentifier identifier = resourceLoader.identifier();
251     ASSERT(identifier);
252
253     LOG(NetworkScheduling, "(WebProcess) WebLoaderStrategy::scheduleLoad, url '%s' will be scheduled with the NetworkProcess with priority %d", resourceLoader.url().string().latin1().data(), static_cast<int>(resourceLoader.request().priority()));
254
255     ContentSniffingPolicy contentSniffingPolicy = resourceLoader.shouldSniffContent() ? SniffContent : DoNotSniffContent;
256     ContentEncodingSniffingPolicy contentEncodingSniffingPolicy = resourceLoader.shouldSniffContentEncoding() ? ContentEncodingSniffingPolicy::Sniff : ContentEncodingSniffingPolicy::DoNotSniff;
257     StoredCredentialsPolicy storedCredentialsPolicy = resourceLoader.shouldUseCredentialStorage() ? StoredCredentialsPolicy::Use : StoredCredentialsPolicy::DoNotUse;
258
259     NetworkResourceLoadParameters loadParameters;
260     loadParameters.identifier = identifier;
261     loadParameters.webPageID = trackingParameters.pageID;
262     loadParameters.webFrameID = trackingParameters.frameID;
263     loadParameters.sessionID = sessionID;
264     loadParameters.request = request;
265     loadParameters.contentSniffingPolicy = contentSniffingPolicy;
266     loadParameters.contentEncodingSniffingPolicy = contentEncodingSniffingPolicy;
267     loadParameters.storedCredentialsPolicy = storedCredentialsPolicy;
268     // If there is no WebFrame then this resource cannot be authenticated with the client.
269     loadParameters.clientCredentialPolicy = (loadParameters.webFrameID && loadParameters.webPageID && resourceLoader.isAllowedToAskUserForCredentials()) ? ClientCredentialPolicy::MayAskClientForCredentials : ClientCredentialPolicy::CannotAskClientForCredentials;
270     loadParameters.shouldClearReferrerOnHTTPSToHTTPRedirect = shouldClearReferrerOnHTTPSToHTTPRedirect;
271     loadParameters.defersLoading = resourceLoader.defersLoading();
272     loadParameters.needsCertificateInfo = resourceLoader.shouldIncludeCertificateInfo();
273     loadParameters.maximumBufferingTime = maximumBufferingTime;
274     loadParameters.derivedCachedDataTypesToRetrieve = resourceLoader.options().derivedCachedDataTypesToRetrieve;
275
276     ASSERT((loadParameters.webPageID && loadParameters.webFrameID) || loadParameters.clientCredentialPolicy == ClientCredentialPolicy::CannotAskClientForCredentials);
277
278     RELEASE_LOG_IF_ALLOWED(resourceLoader, "scheduleLoad: Resource is being scheduled with the NetworkProcess (frame = %p, priority = %d, pageID = %" PRIu64 ", frameID = %" PRIu64 ", resourceID = %" PRIu64 ")", resourceLoader.frame(), static_cast<int>(resourceLoader.request().priority()), loadParameters.webPageID, loadParameters.webFrameID, loadParameters.identifier);
279     if (!WebProcess::singleton().ensureNetworkProcessConnection().connection().send(Messages::NetworkConnectionToWebProcess::ScheduleResourceLoad(loadParameters), 0)) {
280         RELEASE_LOG_ERROR_IF_ALLOWED(resourceLoader, "scheduleLoad: Unable to schedule resource with the NetworkProcess (frame = %p, priority = %d, pageID = %" PRIu64 ", frameID = %" PRIu64 ", resourceID = %" PRIu64 ")", resourceLoader.frame(), static_cast<int>(resourceLoader.request().priority()), loadParameters.webPageID, loadParameters.webFrameID, loadParameters.identifier);
281         // We probably failed to schedule this load with the NetworkProcess because it had crashed.
282         // This load will never succeed so we will schedule it to fail asynchronously.
283         scheduleInternallyFailedLoad(resourceLoader);
284         return;
285     }
286
287     m_webResourceLoaders.set(identifier, WebResourceLoader::create(resourceLoader, trackingParameters));
288 }
289
290 void WebLoaderStrategy::scheduleInternallyFailedLoad(WebCore::ResourceLoader& resourceLoader)
291 {
292     m_internallyFailedResourceLoaders.add(&resourceLoader);
293     m_internallyFailedLoadTimer.startOneShot(0_s);
294 }
295
296 void WebLoaderStrategy::internallyFailedLoadTimerFired()
297 {
298     for (auto& resourceLoader : copyToVector(m_internallyFailedResourceLoaders))
299         resourceLoader->didFail(internalError(resourceLoader->url()));
300 }
301
302 void WebLoaderStrategy::startLocalLoad(WebCore::ResourceLoader& resourceLoader)
303 {
304     resourceLoader.start();
305     m_webResourceLoaders.set(resourceLoader.identifier(), WebResourceLoader::create(resourceLoader, { }));
306 }
307
308 void WebLoaderStrategy::addURLSchemeTaskProxy(WebURLSchemeTaskProxy& task)
309 {
310     auto result = m_urlSchemeTasks.add(task.identifier(), &task);
311     ASSERT_UNUSED(result, result.isNewEntry);
312 }
313
314 void WebLoaderStrategy::removeURLSchemeTaskProxy(WebURLSchemeTaskProxy& task)
315 {
316     m_urlSchemeTasks.remove(task.identifier());
317 }
318
319 void WebLoaderStrategy::remove(ResourceLoader* resourceLoader)
320 {
321     ASSERT(resourceLoader);
322     LOG(NetworkScheduling, "(WebProcess) WebLoaderStrategy::remove, url '%s'", resourceLoader->url().string().utf8().data());
323
324     if (auto task = m_urlSchemeTasks.take(resourceLoader->identifier())) {
325         ASSERT(!m_internallyFailedResourceLoaders.contains(resourceLoader));
326         task->stopLoading();
327         return;
328     }
329
330     if (m_internallyFailedResourceLoaders.contains(resourceLoader)) {
331         m_internallyFailedResourceLoaders.remove(resourceLoader);
332         return;
333     }
334     
335     ResourceLoadIdentifier identifier = resourceLoader->identifier();
336     if (!identifier) {
337         LOG_ERROR("WebLoaderStrategy removing a ResourceLoader that has no identifier.");
338         return;
339     }
340
341 #if ENABLE(SERVICE_WORKER)
342     if (WebServiceWorkerProvider::singleton().cancelFetch(identifier))
343         return;
344 #endif
345
346     RefPtr<WebResourceLoader> loader = m_webResourceLoaders.take(identifier);
347     // Loader may not be registered if we created it, but haven't scheduled yet (a bundle client can decide to cancel such request via willSendRequest).
348     if (!loader)
349         return;
350
351     WebProcess::singleton().ensureNetworkProcessConnection().connection().send(Messages::NetworkConnectionToWebProcess::RemoveLoadIdentifier(identifier), 0);
352
353     // It's possible that this WebResourceLoader might be just about to message back to the NetworkProcess (e.g. ContinueWillSendRequest)
354     // but there's no point in doing so anymore.
355     loader->detachFromCoreLoader();
356 }
357
358 void WebLoaderStrategy::setDefersLoading(ResourceLoader* resourceLoader, bool defers)
359 {
360     ResourceLoadIdentifier identifier = resourceLoader->identifier();
361     WebProcess::singleton().ensureNetworkProcessConnection().connection().send(Messages::NetworkConnectionToWebProcess::SetDefersLoading(identifier, defers), 0);
362 }
363
364 void WebLoaderStrategy::crossOriginRedirectReceived(ResourceLoader*, const URL&)
365 {
366     // We handle cross origin redirects entirely within the NetworkProcess.
367     // We override this call in the WebProcess to make it a no-op.
368 }
369
370 void WebLoaderStrategy::servePendingRequests(ResourceLoadPriority)
371 {
372     // This overrides the base class version.
373     // We don't need to do anything as this is handled by the network process.
374 }
375
376 void WebLoaderStrategy::suspendPendingRequests()
377 {
378     // Network process does keep requests in pending state.
379 }
380
381 void WebLoaderStrategy::resumePendingRequests()
382 {
383     // Network process does keep requests in pending state.
384 }
385
386 void WebLoaderStrategy::networkProcessCrashed()
387 {
388     RELEASE_LOG_ERROR(Network, "WebLoaderStrategy::networkProcessCrashed: failing all pending resource loaders");
389
390     for (auto& loader : m_webResourceLoaders)
391         scheduleInternallyFailedLoad(*loader.value->resourceLoader());
392
393     m_webResourceLoaders.clear();
394
395     auto pingLoadCompletionHandlers = WTFMove(m_pingLoadCompletionHandlers);
396     for (auto& pingLoadCompletionHandler : pingLoadCompletionHandlers.values())
397         pingLoadCompletionHandler(internalError(URL()), { });
398
399     auto preconnectCompletionHandlers = WTFMove(m_preconnectCompletionHandlers);
400     for (auto& preconnectCompletionHandler : preconnectCompletionHandlers.values())
401         preconnectCompletionHandler(internalError(URL()));
402 }
403
404 static bool shouldClearReferrerOnHTTPSToHTTPRedirect(Frame* frame)
405 {
406     if (frame) {
407         if (auto* document = frame->document())
408             return document->referrerPolicy() == ReferrerPolicy::NoReferrerWhenDowngrade;
409     }
410     return true;
411 }
412
413 void WebLoaderStrategy::loadResourceSynchronously(FrameLoader& frameLoader, unsigned long resourceLoadIdentifier, const ResourceRequest& request, StoredCredentialsPolicy storedCredentialsPolicy, ClientCredentialPolicy clientCredentialPolicy, ResourceError& error, ResourceResponse& response, Vector<char>& data)
414 {
415     WebFrameLoaderClient* webFrameLoaderClient = toWebFrameLoaderClient(frameLoader.client());
416     WebFrame* webFrame = webFrameLoaderClient ? webFrameLoaderClient->webFrame() : nullptr;
417     WebPage* webPage = webFrame ? webFrame->page() : nullptr;
418
419     NetworkResourceLoadParameters loadParameters;
420     loadParameters.identifier = resourceLoadIdentifier;
421     loadParameters.webPageID = webPage ? webPage->pageID() : 0;
422     loadParameters.webFrameID = webFrame ? webFrame->frameID() : 0;
423     loadParameters.sessionID = webPage ? webPage->sessionID() : PAL::SessionID::defaultSessionID();
424     loadParameters.request = request;
425     loadParameters.contentSniffingPolicy = SniffContent;
426     loadParameters.contentEncodingSniffingPolicy = ContentEncodingSniffingPolicy::Sniff;
427     loadParameters.storedCredentialsPolicy = storedCredentialsPolicy;
428     loadParameters.clientCredentialPolicy = clientCredentialPolicy;
429     loadParameters.shouldClearReferrerOnHTTPSToHTTPRedirect = shouldClearReferrerOnHTTPSToHTTPRedirect(webFrame ? webFrame->coreFrame() : nullptr);
430
431     data.shrink(0);
432
433     HangDetectionDisabler hangDetectionDisabler;
434
435     if (!WebProcess::singleton().ensureNetworkProcessConnection().connection().sendSync(Messages::NetworkConnectionToWebProcess::PerformSynchronousLoad(loadParameters), Messages::NetworkConnectionToWebProcess::PerformSynchronousLoad::Reply(error, response, data), 0, Seconds::infinity(), IPC::SendSyncOption::DoNotProcessIncomingMessagesWhenWaitingForSyncReply)) {
436         RELEASE_LOG_ERROR_IF_ALLOWED(loadParameters.sessionID, "loadResourceSynchronously: failed sending synchronous network process message (pageID = %" PRIu64 ", frameID = %" PRIu64 ", resourceID = %" PRIu64 ")", loadParameters.webPageID, loadParameters.webFrameID, loadParameters.identifier);
437         if (auto* page = webPage->corePage())
438             page->diagnosticLoggingClient().logDiagnosticMessage(WebCore::DiagnosticLoggingKeys::internalErrorKey(), WebCore::DiagnosticLoggingKeys::synchronousMessageFailedKey(), WebCore::ShouldSample::No);
439         response = ResourceResponse();
440         error = internalError(request.url());
441     }
442 }
443
444 static uint64_t generateLoadIdentifier()
445 {
446     static uint64_t identifier = 0;
447     return ++identifier;
448 }
449
450 void WebLoaderStrategy::startPingLoad(Frame& frame, ResourceRequest& request, const HTTPHeaderMap& originalRequestHeaders, const FetchOptions& options, PingLoadCompletionHandler&& completionHandler)
451 {
452     auto* document = frame.document();
453     if (!document) {
454         if (completionHandler)
455             completionHandler(internalError(request.url()), { });
456         return;
457     }
458
459     NetworkResourceLoadParameters loadParameters;
460     loadParameters.identifier = generateLoadIdentifier();
461     loadParameters.request = request;
462     loadParameters.sourceOrigin = &document->securityOrigin();
463     loadParameters.sessionID = frame.page() ? frame.page()->sessionID() : PAL::SessionID::defaultSessionID();
464     loadParameters.storedCredentialsPolicy = options.credentials == FetchOptions::Credentials::Omit ? StoredCredentialsPolicy::DoNotUse : StoredCredentialsPolicy::Use;
465     loadParameters.mode = options.mode;
466     loadParameters.shouldFollowRedirects = options.redirect == FetchOptions::Redirect::Follow;
467     loadParameters.shouldClearReferrerOnHTTPSToHTTPRedirect = shouldClearReferrerOnHTTPSToHTTPRedirect(&frame);
468     if (!document->shouldBypassMainWorldContentSecurityPolicy()) {
469         if (auto * contentSecurityPolicy = document->contentSecurityPolicy())
470             loadParameters.cspResponseHeaders = contentSecurityPolicy->responseHeaders();
471     }
472
473 #if ENABLE(CONTENT_EXTENSIONS)
474     loadParameters.mainDocumentURL = document->topDocument().url();
475
476     if (auto* documentLoader = frame.loader().documentLoader()) {
477         if (auto* page = frame.page()) {
478             page->userContentProvider().forEachContentExtension([&loadParameters](const String& identifier, ContentExtensions::ContentExtension& contentExtension) {
479                 loadParameters.contentRuleLists.append(std::make_pair(identifier, static_cast<const WebCompiledContentRuleList&>(contentExtension.compiledExtension()).data()));
480             }, *documentLoader);
481         }
482     }
483 #endif
484
485     if (completionHandler)
486         m_pingLoadCompletionHandlers.add(loadParameters.identifier, WTFMove(completionHandler));
487
488     WebProcess::singleton().ensureNetworkProcessConnection().connection().send(Messages::NetworkConnectionToWebProcess::LoadPing(WTFMove(loadParameters), originalRequestHeaders), 0);
489 }
490
491 void WebLoaderStrategy::didFinishPingLoad(uint64_t pingLoadIdentifier, ResourceError&& error, ResourceResponse&& response)
492 {
493     if (auto completionHandler = m_pingLoadCompletionHandlers.take(pingLoadIdentifier))
494         completionHandler(WTFMove(error), WTFMove(response));
495 }
496
497 void WebLoaderStrategy::preconnectTo(FrameLoader& frameLoader, const WebCore::URL& url, StoredCredentialsPolicy storedCredentialsPolicy, PreconnectCompletionHandler&& completionHandler)
498 {
499     uint64_t preconnectionIdentifier = generateLoadIdentifier();
500     auto addResult = m_preconnectCompletionHandlers.add(preconnectionIdentifier, WTFMove(completionHandler));
501     ASSERT_UNUSED(addResult, addResult.isNewEntry);
502
503     auto* webFrameLoaderClient = toWebFrameLoaderClient(frameLoader.client());
504     if (!webFrameLoaderClient) {
505         completionHandler(internalError(url));
506         return;
507     }
508     auto* webFrame = webFrameLoaderClient->webFrame();
509     if (!webFrame) {
510         completionHandler(internalError(url));
511         return;
512     }
513     auto* webPage = webFrame->page();
514     if (!webPage) {
515         completionHandler(internalError(url));
516         return;
517     }
518
519     NetworkResourceLoadParameters parameters;
520     parameters.request = ResourceRequest { url };
521     parameters.webPageID = webPage ? webPage->pageID() : 0;
522     parameters.webFrameID = webFrame ? webFrame->frameID() : 0;
523     parameters.sessionID = webPage ? webPage->sessionID() : PAL::SessionID::defaultSessionID();
524     parameters.storedCredentialsPolicy = storedCredentialsPolicy;
525     parameters.shouldPreconnectOnly = PreconnectOnly::Yes;
526
527     WebProcess::singleton().ensureNetworkProcessConnection().connection().send(Messages::NetworkConnectionToWebProcess::PreconnectTo(preconnectionIdentifier, WTFMove(parameters)), 0);
528 }
529
530 void WebLoaderStrategy::didFinishPreconnection(uint64_t preconnectionIdentifier, ResourceError&& error)
531 {
532     if (auto completionHandler = m_preconnectCompletionHandlers.take(preconnectionIdentifier))
533         completionHandler(WTFMove(error));
534 }
535
536 void WebLoaderStrategy::storeDerivedDataToCache(const SHA1::Digest& bodyHash, const String& type, const String& partition, WebCore::SharedBuffer& data)
537 {
538     NetworkCache::DataKey key { partition, type, bodyHash };
539     IPC::SharedBufferDataReference dataReference { &data };
540     WebProcess::singleton().ensureNetworkProcessConnection().connection().send(Messages::NetworkConnectionToWebProcess::StoreDerivedDataToCache(key, dataReference), 0);
541 }
542
543 void WebLoaderStrategy::setCaptureExtraNetworkLoadMetricsEnabled(bool enabled)
544 {
545     WebProcess::singleton().ensureNetworkProcessConnection().connection().send(Messages::NetworkConnectionToWebProcess::SetCaptureExtraNetworkLoadMetricsEnabled(enabled), 0);
546 }
547
548 } // namespace WebKit