1efc93c02a3ca2b9bf0edf0aa905e5f9bb0d3bd3
[WebKit-https.git] / Source / WebKit / WebProcess / Network / WebLoaderStrategy.h
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
36 namespace WebCore {
37 struct FetchOptions;
38 }
39
40 namespace WebKit {
41
42 class NetworkProcessConnection;
43 class WebURLSchemeTaskProxy;
44 typedef uint64_t ResourceLoadIdentifier;
45
46 class WebLoaderStrategy final : public WebCore::LoaderStrategy {
47     WTF_MAKE_NONCOPYABLE(WebLoaderStrategy); WTF_MAKE_FAST_ALLOCATED;
48 public:
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(uint64_t webPageID) 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     void startPingLoad(WebCore::Frame&, WebCore::ResourceRequest&, const WebCore::HTTPHeaderMap& originalRequestHeaders, const WebCore::FetchOptions&, WebCore::ContentSecurityPolicyImposition, PingLoadCompletionHandler&&) final;
66     void didFinishPingLoad(uint64_t pingLoadIdentifier, WebCore::ResourceError&&, WebCore::ResourceResponse&&);
67
68     void preconnectTo(WebCore::FrameLoader&, const URL&, WebCore::StoredCredentialsPolicy, PreconnectCompletionHandler&&) final;
69     void didFinishPreconnection(uint64_t preconnectionIdentifier, WebCore::ResourceError&&);
70
71     void setCaptureExtraNetworkLoadMetricsEnabled(bool) final;
72
73     WebResourceLoader* webResourceLoaderForIdentifier(ResourceLoadIdentifier identifier) const { return m_webResourceLoaders.get(identifier); }
74     void schedulePluginStreamLoad(WebCore::Frame&, WebCore::NetscapePlugInStreamLoaderClient&, WebCore::ResourceRequest&&, CompletionHandler<void(RefPtr<WebCore::NetscapePlugInStreamLoader>&&)>&&);
75
76     void networkProcessCrashed();
77
78     void addURLSchemeTaskProxy(WebURLSchemeTaskProxy&);
79     void removeURLSchemeTaskProxy(WebURLSchemeTaskProxy&);
80
81     void scheduleLoadFromNetworkProcess(WebCore::ResourceLoader&, const WebCore::ResourceRequest&, const WebResourceLoader::TrackingParameters&, PAL::SessionID, bool shouldClearReferrerOnHTTPSToHTTPRedirect, Seconds maximumBufferingTime);
82
83     bool isOnLine() const final;
84     void addOnlineStateChangeListener(Function<void(bool)>&&) final;
85     void setOnLineState(bool);
86
87 private:
88     void scheduleLoad(WebCore::ResourceLoader&, WebCore::CachedResource*, bool shouldClearReferrerOnHTTPSToHTTPRedirect);
89     void scheduleInternallyFailedLoad(WebCore::ResourceLoader&);
90     void internallyFailedLoadTimerFired();
91     void startLocalLoad(WebCore::ResourceLoader&);
92     bool tryLoadingUsingURLSchemeHandler(WebCore::ResourceLoader&);
93     
94     struct SyncLoadResult {
95         WebCore::ResourceResponse response;
96         WebCore::ResourceError error;
97         Vector<char> data;
98     };
99     Optional<SyncLoadResult> tryLoadingSynchronouslyUsingURLSchemeHandler(WebCore::FrameLoader&, ResourceLoadIdentifier, const WebCore::ResourceRequest&);
100
101     WebCore::ResourceResponse responseFromResourceLoadIdentifier(uint64_t resourceLoadIdentifier) final;
102     Vector<WebCore::NetworkTransactionInformation> intermediateLoadInformationFromResourceLoadIdentifier(uint64_t resourceLoadIdentifier) final;
103     WebCore::NetworkLoadMetrics networkMetricsFromResourceLoadIdentifier(uint64_t resourceLoadIdentifier) final;
104
105     bool shouldPerformSecurityChecks() const final;
106     bool havePerformedSecurityChecks(const WebCore::ResourceResponse&) const final;
107
108     Vector<uint64_t> ongoingLoads() const final
109     {
110         return WTF::map(m_webResourceLoaders, [](auto&& keyValue) -> uint64_t {
111             return keyValue.key;
112         });
113     }
114
115     HashSet<RefPtr<WebCore::ResourceLoader>> m_internallyFailedResourceLoaders;
116     RunLoop::Timer<WebLoaderStrategy> m_internallyFailedLoadTimer;
117     
118     HashMap<unsigned long, RefPtr<WebResourceLoader>> m_webResourceLoaders;
119     HashMap<unsigned long, WebURLSchemeTaskProxy*> m_urlSchemeTasks;
120     HashMap<unsigned long, PingLoadCompletionHandler> m_pingLoadCompletionHandlers;
121     HashMap<unsigned long, PreconnectCompletionHandler> m_preconnectCompletionHandlers;
122     Vector<Function<void(bool)>> m_onlineStateChangeListeners;
123     bool m_isOnLine { true };
124 };
125
126 } // namespace WebKit