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#pragma once
27
28#include "WebResourceLoader.h"
29#include <WebCore/LoaderStrategy.h>
30#include <WebCore/ResourceError.h>
31#include <WebCore/ResourceLoader.h>
32#include <WebCore/ResourceResponse.h>
33#include <wtf/HashSet.h>
34#include <wtf/RunLoop.h>
35
36namespace WebCore {
37struct FetchOptions;
38}
39
40namespace WebKit {
41
42class NetworkProcessConnection;
43class WebURLSchemeTaskProxy;
44typedef uint64_t ResourceLoadIdentifier;
45
46class WebLoaderStrategy final : public WebCore::LoaderStrategy {
47 WTF_MAKE_NONCOPYABLE(WebLoaderStrategy); WTF_MAKE_FAST_ALLOCATED;
48public:
49 WebLoaderStrategy();
50 ~WebLoaderStrategy() final;
51
52 void loadResource(WebCore::Frame&, WebCore::CachedResource&, WebCore::ResourceRequest&&, const WebCore::ResourceLoaderOptions&, CompletionHandler<void(RefPtr<WebCore::SubresourceLoader>&&)>&&) final;
53 void loadResourceSynchronously(WebCore::FrameLoader&, unsigned long resourceLoadIdentifier, const WebCore::ResourceRequest&, WebCore::ClientCredentialPolicy, const WebCore::FetchOptions&, const WebCore::HTTPHeaderMap&, WebCore::ResourceError&, WebCore::ResourceResponse&, Vector<char>& data) final;
54 void pageLoadCompleted(WebCore::PageIdentifier) final;
55
56 void remove(WebCore::ResourceLoader*) final;
57 void setDefersLoading(WebCore::ResourceLoader&, bool) final;
58 void crossOriginRedirectReceived(WebCore::ResourceLoader*, const URL& redirectURL) final;
59
60 void servePendingRequests(WebCore::ResourceLoadPriority minimumPriority) final;
61
62 void suspendPendingRequests() final;
63 void resumePendingRequests() final;
64
65 bool usePingLoad() const final;
66 void startPingLoad(WebCore::Frame&, WebCore::ResourceRequest&, const WebCore::HTTPHeaderMap& originalRequestHeaders, const WebCore::FetchOptions&, WebCore::ContentSecurityPolicyImposition, PingLoadCompletionHandler&&) final;
67 void didFinishPingLoad(uint64_t pingLoadIdentifier, WebCore::ResourceError&&, WebCore::ResourceResponse&&);
68
69 void preconnectTo(WebCore::FrameLoader&, const URL&, WebCore::StoredCredentialsPolicy, PreconnectCompletionHandler&&) final;
70 void didFinishPreconnection(uint64_t preconnectionIdentifier, WebCore::ResourceError&&);
71
72 void setCaptureExtraNetworkLoadMetricsEnabled(bool) final;
73
74 WebResourceLoader* webResourceLoaderForIdentifier(ResourceLoadIdentifier identifier) const { return m_webResourceLoaders.get(identifier); }
75 void schedulePluginStreamLoad(WebCore::Frame&, WebCore::NetscapePlugInStreamLoaderClient&, WebCore::ResourceRequest&&, CompletionHandler<void(RefPtr<WebCore::NetscapePlugInStreamLoader>&&)>&&);
76
77 void networkProcessCrashed();
78
79 void addURLSchemeTaskProxy(WebURLSchemeTaskProxy&);
80 void removeURLSchemeTaskProxy(WebURLSchemeTaskProxy&);
81
82 void scheduleLoadFromNetworkProcess(WebCore::ResourceLoader&, const WebCore::ResourceRequest&, const WebResourceLoader::TrackingParameters&, PAL::SessionID, bool shouldClearReferrerOnHTTPSToHTTPRedirect, Seconds maximumBufferingTime);
83
84 bool isOnLine() const final;
85 void addOnlineStateChangeListener(Function<void(bool)>&&) final;
86 void setOnLineState(bool);
87
88private:
89 void scheduleLoad(WebCore::ResourceLoader&, WebCore::CachedResource*, bool shouldClearReferrerOnHTTPSToHTTPRedirect);
90 void scheduleInternallyFailedLoad(WebCore::ResourceLoader&);
91 void internallyFailedLoadTimerFired();
92 void startLocalLoad(WebCore::ResourceLoader&);
93 bool tryLoadingUsingURLSchemeHandler(WebCore::ResourceLoader&);
94
95 struct SyncLoadResult {
96 WebCore::ResourceResponse response;
97 WebCore::ResourceError error;
98 Vector<char> data;
99 };
100 Optional<SyncLoadResult> tryLoadingSynchronouslyUsingURLSchemeHandler(WebCore::FrameLoader&, ResourceLoadIdentifier, const WebCore::ResourceRequest&);
101
102 WebCore::ResourceResponse responseFromResourceLoadIdentifier(uint64_t resourceLoadIdentifier) final;
103 Vector<WebCore::NetworkTransactionInformation> intermediateLoadInformationFromResourceLoadIdentifier(uint64_t resourceLoadIdentifier) final;
104 WebCore::NetworkLoadMetrics networkMetricsFromResourceLoadIdentifier(uint64_t resourceLoadIdentifier) final;
105
106 bool shouldPerformSecurityChecks() const final;
107 bool havePerformedSecurityChecks(const WebCore::ResourceResponse&) const final;
108
109 Vector<uint64_t> ongoingLoads() const final
110 {
111 return WTF::map(m_webResourceLoaders, [](auto&& keyValue) -> uint64_t {
112 return keyValue.key;
113 });
114 }
115
116 HashSet<RefPtr<WebCore::ResourceLoader>> m_internallyFailedResourceLoaders;
117 RunLoop::Timer<WebLoaderStrategy> m_internallyFailedLoadTimer;
118
119 HashMap<unsigned long, RefPtr<WebResourceLoader>> m_webResourceLoaders;
120 HashMap<unsigned long, WebURLSchemeTaskProxy*> m_urlSchemeTasks;
121 HashMap<unsigned long, PingLoadCompletionHandler> m_pingLoadCompletionHandlers;
122 HashMap<unsigned long, PreconnectCompletionHandler> m_preconnectCompletionHandlers;
123 Vector<Function<void(bool)>> m_onlineStateChangeListeners;
124 bool m_isOnLine { true };
125 HashSet<WebResourceLoader*> m_loadersWithUploads;
126};
127
128} // namespace WebKit
129