Add facility for tracking times and results of page and resource loading
[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/ResourceLoader.h>
31 #include <wtf/HashSet.h>
32 #include <wtf/RunLoop.h>
33
34 namespace WebCore {
35 struct FetchOptions;
36 }
37
38 namespace WebKit {
39
40 class NetworkProcessConnection;
41 class WebURLSchemeTaskProxy;
42 typedef uint64_t ResourceLoadIdentifier;
43
44 class WebLoaderStrategy final : public WebCore::LoaderStrategy {
45     WTF_MAKE_NONCOPYABLE(WebLoaderStrategy); WTF_MAKE_FAST_ALLOCATED;
46 public:
47     WebLoaderStrategy();
48     ~WebLoaderStrategy() final;
49     
50     void loadResource(WebCore::Frame&, WebCore::CachedResource&, WebCore::ResourceRequest&&, const WebCore::ResourceLoaderOptions&, CompletionHandler<void(RefPtr<WebCore::SubresourceLoader>&&)>&&) final;
51     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;
52     void pageLoadCompleted(uint64_t webPageID) final;
53
54     void remove(WebCore::ResourceLoader*) final;
55     void setDefersLoading(WebCore::ResourceLoader*, bool) final;
56     void crossOriginRedirectReceived(WebCore::ResourceLoader*, const WebCore::URL& redirectURL) final;
57     
58     void servePendingRequests(WebCore::ResourceLoadPriority minimumPriority) final;
59
60     void suspendPendingRequests() final;
61     void resumePendingRequests() final;
62
63     void startPingLoad(WebCore::Frame&, WebCore::ResourceRequest&, const WebCore::HTTPHeaderMap& originalRequestHeaders, const WebCore::FetchOptions&, PingLoadCompletionHandler&&) final;
64     void didFinishPingLoad(uint64_t pingLoadIdentifier, WebCore::ResourceError&&, WebCore::ResourceResponse&&);
65
66     void preconnectTo(WebCore::FrameLoader&, const WebCore::URL&, WebCore::StoredCredentialsPolicy, PreconnectCompletionHandler&&) final;
67     void didFinishPreconnection(uint64_t preconnectionIdentifier, WebCore::ResourceError&&);
68
69     void storeDerivedDataToCache(const SHA1::Digest& bodyHash, const String& type, const String& partition, WebCore::SharedBuffer&) final;
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     WebCore::ResourceResponse responseFromResourceLoadIdentifier(uint64_t resourceLoadIdentifier) final;
95     WebCore::NetworkLoadMetrics networkMetricsFromResourceLoadIdentifier(uint64_t resourceLoadIdentifier) final;
96
97     bool isDoingLoadingSecurityChecks() const final;
98
99     HashSet<RefPtr<WebCore::ResourceLoader>> m_internallyFailedResourceLoaders;
100     RunLoop::Timer<WebLoaderStrategy> m_internallyFailedLoadTimer;
101     
102     HashMap<unsigned long, RefPtr<WebResourceLoader>> m_webResourceLoaders;
103     HashMap<unsigned long, WebURLSchemeTaskProxy*> m_urlSchemeTasks;
104     HashMap<unsigned long, PingLoadCompletionHandler> m_pingLoadCompletionHandlers;
105     HashMap<unsigned long, PreconnectCompletionHandler> m_preconnectCompletionHandlers;
106     Vector<Function<void(bool)>> m_onlineStateChangeListeners;
107     bool m_isOnLine { true };
108 };
109
110 } // namespace WebKit