Make ResourceLoader::willSendRequestInternal asynchronous
[WebKit-https.git] / Source / WebKitLegacy / WebCoreSupport / WebResourceLoadScheduler.h
1 /*
2     Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de)
3     Copyright (C) 2001 Dirk Mueller <mueller@kde.org>
4     Copyright (C) 2004, 2006-2008, 2015 Apple Inc. All rights reserved.
5     Copyright (C) 2010 Google Inc. All rights reserved.
6
7     This library is free software; you can redistribute it and/or
8     modify it under the terms of the GNU Library General Public
9     License as published by the Free Software Foundation; either
10     version 2 of the License, or (at your option) any later version.
11
12     This library is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15     Library General Public License for more details.
16
17     You should have received a copy of the GNU Library General Public License
18     along with this library; see the file COPYING.LIB.  If not, write to
19     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20     Boston, MA 02110-1301, USA.
21  */
22
23 #pragma once
24
25 #include <WebCore/FrameLoaderTypes.h>
26 #include <WebCore/LoaderStrategy.h>
27 #include <WebCore/ResourceLoadPriority.h>
28 #include <WebCore/ResourceLoaderOptions.h>
29 #include <WebCore/Timer.h>
30 #include <array>
31 #include <wtf/Deque.h>
32 #include <wtf/HashMap.h>
33 #include <wtf/HashSet.h>
34 #include <wtf/Noncopyable.h>
35 #include <wtf/text/StringHash.h>
36 #include <wtf/text/WTFString.h>
37
38 class WebResourceLoadScheduler;
39
40 namespace WebCore {
41 struct FetchOptions;
42 class SecurityOrigin;
43 }
44
45 WebResourceLoadScheduler& webResourceLoadScheduler();
46
47 class WebResourceLoadScheduler final : public WebCore::LoaderStrategy {
48     WTF_MAKE_NONCOPYABLE(WebResourceLoadScheduler); WTF_MAKE_FAST_ALLOCATED;
49 public:
50     WebResourceLoadScheduler();
51
52     void loadResource(WebCore::Frame&, WebCore::CachedResource&, WebCore::ResourceRequest&&, const WebCore::ResourceLoaderOptions&, CompletionHandler<void(RefPtr<WebCore::SubresourceLoader>&&)>&&) final;
53     void loadResourceSynchronously(WebCore::NetworkingContext*, unsigned long, const WebCore::ResourceRequest&, WebCore::StoredCredentialsPolicy, WebCore::ClientCredentialPolicy, WebCore::ResourceError&, WebCore::ResourceResponse&, Vector<char>&) final;
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 = WebCore::ResourceLoadPriority::VeryLow) final;
59     void suspendPendingRequests() final;
60     void resumePendingRequests() final;
61
62     void startPingLoad(WebCore::Frame&, WebCore::ResourceRequest&, const WebCore::HTTPHeaderMap&, const WebCore::FetchOptions&, PingLoadCompletionHandler&&) final;
63
64     void preconnectTo(WebCore::NetworkingContext&, const WebCore::URL&, WebCore::StoredCredentialsPolicy, PreconnectCompletionHandler&&) final;
65
66     void storeDerivedDataToCache(const SHA1::Digest&, const String&, const String&, WebCore::SharedBuffer&) final { }
67
68     void setCaptureExtraNetworkLoadMetricsEnabled(bool) final { }
69
70     bool isSerialLoadingEnabled() const { return m_isSerialLoadingEnabled; }
71     void setSerialLoadingEnabled(bool b) { m_isSerialLoadingEnabled = b; }
72
73     void schedulePluginStreamLoad(WebCore::Frame&, WebCore::NetscapePlugInStreamLoaderClient&, WebCore::ResourceRequest&&, CompletionHandler<void(RefPtr<WebCore::NetscapePlugInStreamLoader>&&)>&&);
74
75 protected:
76     virtual ~WebResourceLoadScheduler();
77
78 private:
79     void scheduleLoad(WebCore::ResourceLoader*);
80     void scheduleServePendingRequests();
81     void requestTimerFired();
82
83     bool isSuspendingPendingRequests() const { return !!m_suspendPendingRequestsCount; }
84
85     class HostInformation {
86         WTF_MAKE_NONCOPYABLE(HostInformation); WTF_MAKE_FAST_ALLOCATED;
87     public:
88         HostInformation(const String&, unsigned);
89         ~HostInformation();
90         
91         const String& name() const { return m_name; }
92         void schedule(WebCore::ResourceLoader*, WebCore::ResourceLoadPriority = WebCore::ResourceLoadPriority::VeryLow);
93         void addLoadInProgress(WebCore::ResourceLoader*);
94         void remove(WebCore::ResourceLoader*);
95         bool hasRequests() const;
96         bool limitRequests(WebCore::ResourceLoadPriority) const;
97
98         typedef Deque<RefPtr<WebCore::ResourceLoader>> RequestQueue;
99         RequestQueue& requestsPending(WebCore::ResourceLoadPriority priority) { return m_requestsPending[priorityToIndex(priority)]; }
100
101     private:
102         static unsigned priorityToIndex(WebCore::ResourceLoadPriority);
103
104         std::array<RequestQueue, WebCore::resourceLoadPriorityCount> m_requestsPending;
105         typedef HashSet<RefPtr<WebCore::ResourceLoader>> RequestMap;
106         RequestMap m_requestsLoading;
107         const String m_name;
108         const unsigned m_maxRequestsInFlight;
109     };
110
111     enum CreateHostPolicy {
112         CreateIfNotFound,
113         FindOnly
114     };
115     
116     HostInformation* hostForURL(const WebCore::URL&, CreateHostPolicy = FindOnly);
117     void servePendingRequests(HostInformation*, WebCore::ResourceLoadPriority);
118
119     typedef HashMap<String, HostInformation*, StringHash> HostMap;
120     HostMap m_hosts;
121     HostInformation* m_nonHTTPProtocolHost;
122         
123     WebCore::Timer m_requestTimer;
124
125     unsigned m_suspendPendingRequestsCount;
126     bool m_isSerialLoadingEnabled;
127 };