Move URL from WebCore to WTF
[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&, 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 storeDerivedDataToCache(const SHA1::Digest& bodyHash, const String& type, const String& partition, WebCore::SharedBuffer&) final;
72
73     void setCaptureExtraNetworkLoadMetricsEnabled(bool) final;
74
75     WebResourceLoader* webResourceLoaderForIdentifier(ResourceLoadIdentifier identifier) const { return m_webResourceLoaders.get(identifier); }
76     void schedulePluginStreamLoad(WebCore::Frame&, WebCore::NetscapePlugInStreamLoaderClient&, WebCore::ResourceRequest&&, CompletionHandler<void(RefPtr<WebCore::NetscapePlugInStreamLoader>&&)>&&);
77
78     void networkProcessCrashed();
79
80     void addURLSchemeTaskProxy(WebURLSchemeTaskProxy&);
81     void removeURLSchemeTaskProxy(WebURLSchemeTaskProxy&);
82
83     void scheduleLoadFromNetworkProcess(WebCore::ResourceLoader&, const WebCore::ResourceRequest&, const WebResourceLoader::TrackingParameters&, PAL::SessionID, bool shouldClearReferrerOnHTTPSToHTTPRedirect, Seconds maximumBufferingTime);
84
85     bool isOnLine() const final;
86     void addOnlineStateChangeListener(Function<void(bool)>&&) final;
87     void setOnLineState(bool);
88
89 private:
90     void scheduleLoad(WebCore::ResourceLoader&, WebCore::CachedResource*, bool shouldClearReferrerOnHTTPSToHTTPRedirect);
91     void scheduleInternallyFailedLoad(WebCore::ResourceLoader&);
92     void internallyFailedLoadTimerFired();
93     void startLocalLoad(WebCore::ResourceLoader&);
94     bool tryLoadingUsingURLSchemeHandler(WebCore::ResourceLoader&);
95     
96     struct SyncLoadResult {
97         WebCore::ResourceResponse response;
98         WebCore::ResourceError error;
99         Vector<char> data;
100     };
101     std::optional<SyncLoadResult> tryLoadingSynchronouslyUsingURLSchemeHandler(WebCore::FrameLoader&, ResourceLoadIdentifier, const WebCore::ResourceRequest&);
102
103     WebCore::ResourceResponse responseFromResourceLoadIdentifier(uint64_t resourceLoadIdentifier) final;
104     Vector<WebCore::NetworkTransactionInformation> intermediateLoadInformationFromResourceLoadIdentifier(uint64_t resourceLoadIdentifier) final;
105     WebCore::NetworkLoadMetrics networkMetricsFromResourceLoadIdentifier(uint64_t resourceLoadIdentifier) final;
106
107     bool shouldPerformSecurityChecks() const final;
108     bool havePerformedSecurityChecks(const WebCore::ResourceResponse&) const final;
109
110     Vector<uint64_t> ongoingLoads() const final
111     {
112         return WTF::map(m_webResourceLoaders, [](auto&& keyValue) -> uint64_t {
113             return keyValue.key;
114         });
115     }
116
117     HashSet<RefPtr<WebCore::ResourceLoader>> m_internallyFailedResourceLoaders;
118     RunLoop::Timer<WebLoaderStrategy> m_internallyFailedLoadTimer;
119     
120     HashMap<unsigned long, RefPtr<WebResourceLoader>> m_webResourceLoaders;
121     HashMap<unsigned long, WebURLSchemeTaskProxy*> m_urlSchemeTasks;
122     HashMap<unsigned long, PingLoadCompletionHandler> m_pingLoadCompletionHandlers;
123     HashMap<unsigned long, PreconnectCompletionHandler> m_preconnectCompletionHandlers;
124     Vector<Function<void(bool)>> m_onlineStateChangeListeners;
125     bool m_isOnLine { true };
126 };
127
128 } // namespace WebKit