Simplify calls to LoaderStrategy::startPingLoad()
[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     RefPtr<WebCore::SubresourceLoader> loadResource(WebCore::Frame&, WebCore::CachedResource&, const WebCore::ResourceRequest&, const WebCore::ResourceLoaderOptions&) final;
53     void loadResourceSynchronously(WebCore::NetworkingContext*, unsigned long, const WebCore::ResourceRequest&, WebCore::StoredCredentials, 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 storeDerivedDataToCache(const SHA1::Digest&, const String&, const String&, WebCore::SharedBuffer&) final { }
65
66     void setCaptureExtraNetworkLoadMetricsEnabled(bool) final { }
67
68     bool isSerialLoadingEnabled() const { return m_isSerialLoadingEnabled; }
69     void setSerialLoadingEnabled(bool b) { m_isSerialLoadingEnabled = b; }
70
71     RefPtr<WebCore::NetscapePlugInStreamLoader> schedulePluginStreamLoad(WebCore::Frame&, WebCore::NetscapePlugInStreamLoaderClient&, const WebCore::ResourceRequest&);
72
73 protected:
74     virtual ~WebResourceLoadScheduler();
75
76 private:
77     void scheduleLoad(WebCore::ResourceLoader*);
78     void scheduleServePendingRequests();
79     void requestTimerFired();
80
81     bool isSuspendingPendingRequests() const { return !!m_suspendPendingRequestsCount; }
82
83     class HostInformation {
84         WTF_MAKE_NONCOPYABLE(HostInformation); WTF_MAKE_FAST_ALLOCATED;
85     public:
86         HostInformation(const String&, unsigned);
87         ~HostInformation();
88         
89         const String& name() const { return m_name; }
90         void schedule(WebCore::ResourceLoader*, WebCore::ResourceLoadPriority = WebCore::ResourceLoadPriority::VeryLow);
91         void addLoadInProgress(WebCore::ResourceLoader*);
92         void remove(WebCore::ResourceLoader*);
93         bool hasRequests() const;
94         bool limitRequests(WebCore::ResourceLoadPriority) const;
95
96         typedef Deque<RefPtr<WebCore::ResourceLoader>> RequestQueue;
97         RequestQueue& requestsPending(WebCore::ResourceLoadPriority priority) { return m_requestsPending[priorityToIndex(priority)]; }
98
99     private:
100         static unsigned priorityToIndex(WebCore::ResourceLoadPriority);
101
102         std::array<RequestQueue, WebCore::resourceLoadPriorityCount> m_requestsPending;
103         typedef HashSet<RefPtr<WebCore::ResourceLoader>> RequestMap;
104         RequestMap m_requestsLoading;
105         const String m_name;
106         const unsigned m_maxRequestsInFlight;
107     };
108
109     enum CreateHostPolicy {
110         CreateIfNotFound,
111         FindOnly
112     };
113     
114     HostInformation* hostForURL(const WebCore::URL&, CreateHostPolicy = FindOnly);
115     void servePendingRequests(HostInformation*, WebCore::ResourceLoadPriority);
116
117     typedef HashMap<String, HostInformation*, StringHash> HostMap;
118     HostMap m_hosts;
119     HostInformation* m_nonHTTPProtocolHost;
120         
121     WebCore::Timer m_requestTimer;
122
123     unsigned m_suspendPendingRequestsCount;
124     bool m_isSerialLoadingEnabled;
125 };