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.
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.
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.
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.
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>
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>
38 class WebResourceLoadScheduler;
45 WebResourceLoadScheduler& webResourceLoadScheduler();
47 class WebResourceLoadScheduler final : public WebCore::LoaderStrategy {
48 WTF_MAKE_NONCOPYABLE(WebResourceLoadScheduler); WTF_MAKE_FAST_ALLOCATED;
50 WebResourceLoadScheduler();
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, const WebCore::ResourceRequest&, WebCore::ClientCredentialPolicy, const WebCore::FetchOptions&, const WebCore::HTTPHeaderMap&, 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;
58 void servePendingRequests(WebCore::ResourceLoadPriority minimumPriority = WebCore::ResourceLoadPriority::VeryLow) final;
59 void suspendPendingRequests() final;
60 void resumePendingRequests() final;
62 void startPingLoad(WebCore::Frame&, WebCore::ResourceRequest&, const WebCore::HTTPHeaderMap&, const WebCore::FetchOptions&, PingLoadCompletionHandler&&) final;
64 void preconnectTo(WebCore::FrameLoader&, const WebCore::URL&, WebCore::StoredCredentialsPolicy, PreconnectCompletionHandler&&) final;
66 void storeDerivedDataToCache(const SHA1::Digest&, const String&, const String&, WebCore::SharedBuffer&) final { }
68 void setCaptureExtraNetworkLoadMetricsEnabled(bool) final { }
70 bool isSerialLoadingEnabled() const { return m_isSerialLoadingEnabled; }
71 void setSerialLoadingEnabled(bool b) { m_isSerialLoadingEnabled = b; }
73 void schedulePluginStreamLoad(WebCore::Frame&, WebCore::NetscapePlugInStreamLoaderClient&, WebCore::ResourceRequest&&, CompletionHandler<void(RefPtr<WebCore::NetscapePlugInStreamLoader>&&)>&&);
75 bool isOnLine() const final;
76 void addOnlineStateChangeListener(WTF::Function<void(bool)>&&) final;
79 virtual ~WebResourceLoadScheduler();
82 void scheduleLoad(WebCore::ResourceLoader*);
83 void scheduleServePendingRequests();
84 void requestTimerFired();
86 bool isSuspendingPendingRequests() const { return !!m_suspendPendingRequestsCount; }
88 class HostInformation {
89 WTF_MAKE_NONCOPYABLE(HostInformation); WTF_MAKE_FAST_ALLOCATED;
91 HostInformation(const String&, unsigned);
94 const String& name() const { return m_name; }
95 void schedule(WebCore::ResourceLoader*, WebCore::ResourceLoadPriority = WebCore::ResourceLoadPriority::VeryLow);
96 void addLoadInProgress(WebCore::ResourceLoader*);
97 void remove(WebCore::ResourceLoader*);
98 bool hasRequests() const;
99 bool limitRequests(WebCore::ResourceLoadPriority) const;
101 typedef Deque<RefPtr<WebCore::ResourceLoader>> RequestQueue;
102 RequestQueue& requestsPending(WebCore::ResourceLoadPriority priority) { return m_requestsPending[priorityToIndex(priority)]; }
105 static unsigned priorityToIndex(WebCore::ResourceLoadPriority);
107 std::array<RequestQueue, WebCore::resourceLoadPriorityCount> m_requestsPending;
108 typedef HashSet<RefPtr<WebCore::ResourceLoader>> RequestMap;
109 RequestMap m_requestsLoading;
111 const unsigned m_maxRequestsInFlight;
114 enum CreateHostPolicy {
119 HostInformation* hostForURL(const WebCore::URL&, CreateHostPolicy = FindOnly);
120 void servePendingRequests(HostInformation*, WebCore::ResourceLoadPriority);
122 typedef HashMap<String, HostInformation*, StringHash> HostMap;
124 HostInformation* m_nonHTTPProtocolHost;
126 WebCore::Timer m_requestTimer;
128 unsigned m_suspendPendingRequestsCount;
129 bool m_isSerialLoadingEnabled;