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;
40 WebResourceLoadScheduler& webResourceLoadScheduler();
42 class WebResourceLoadScheduler final : public WebCore::LoaderStrategy {
43 WTF_MAKE_NONCOPYABLE(WebResourceLoadScheduler); WTF_MAKE_FAST_ALLOCATED;
45 WebResourceLoadScheduler();
47 void loadResource(WebCore::Frame&, WebCore::CachedResource&, WebCore::ResourceRequest&&, const WebCore::ResourceLoaderOptions&, CompletionHandler<void(RefPtr<WebCore::SubresourceLoader>&&)>&&) final;
48 void loadResourceSynchronously(WebCore::FrameLoader&, unsigned long, const WebCore::ResourceRequest&, WebCore::ClientCredentialPolicy, const WebCore::FetchOptions&, const WebCore::HTTPHeaderMap&, WebCore::ResourceError&, WebCore::ResourceResponse&, Vector<char>&) final;
49 void pageLoadCompleted(WebCore::Page&) final;
51 void remove(WebCore::ResourceLoader*) final;
52 void setDefersLoading(WebCore::ResourceLoader&, bool) final;
53 void crossOriginRedirectReceived(WebCore::ResourceLoader*, const URL& redirectURL) final;
55 void servePendingRequests(WebCore::ResourceLoadPriority minimumPriority = WebCore::ResourceLoadPriority::VeryLow) final;
56 void suspendPendingRequests() final;
57 void resumePendingRequests() final;
59 void startPingLoad(WebCore::Frame&, WebCore::ResourceRequest&, const WebCore::HTTPHeaderMap&, const WebCore::FetchOptions&, WebCore::ContentSecurityPolicyImposition, PingLoadCompletionHandler&&) final;
61 void preconnectTo(WebCore::FrameLoader&, const URL&, WebCore::StoredCredentialsPolicy, PreconnectCompletionHandler&&) final;
63 void setCaptureExtraNetworkLoadMetricsEnabled(bool) final { }
65 bool isSerialLoadingEnabled() const { return m_isSerialLoadingEnabled; }
66 void setSerialLoadingEnabled(bool b) { m_isSerialLoadingEnabled = b; }
68 void schedulePluginStreamLoad(WebCore::Frame&, WebCore::NetscapePlugInStreamLoaderClient&, WebCore::ResourceRequest&&, CompletionHandler<void(RefPtr<WebCore::NetscapePlugInStreamLoader>&&)>&&);
70 bool isOnLine() const final;
71 void addOnlineStateChangeListener(WTF::Function<void(bool)>&&) final;
74 virtual ~WebResourceLoadScheduler();
77 void scheduleLoad(WebCore::ResourceLoader*);
78 void scheduleServePendingRequests();
79 void requestTimerFired();
81 bool isSuspendingPendingRequests() const { return !!m_suspendPendingRequestsCount; }
83 class HostInformation {
84 WTF_MAKE_NONCOPYABLE(HostInformation); WTF_MAKE_FAST_ALLOCATED;
86 HostInformation(const String&, unsigned);
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;
96 typedef Deque<RefPtr<WebCore::ResourceLoader>> RequestQueue;
97 RequestQueue& requestsPending(WebCore::ResourceLoadPriority priority) { return m_requestsPending[priorityToIndex(priority)]; }
100 static unsigned priorityToIndex(WebCore::ResourceLoadPriority);
102 std::array<RequestQueue, WebCore::resourceLoadPriorityCount> m_requestsPending;
103 typedef HashSet<RefPtr<WebCore::ResourceLoader>> RequestMap;
104 RequestMap m_requestsLoading;
106 const unsigned m_maxRequestsInFlight;
109 enum CreateHostPolicy {
114 HostInformation* hostForURL(const URL&, CreateHostPolicy = FindOnly);
115 void servePendingRequests(HostInformation*, WebCore::ResourceLoadPriority);
117 typedef HashMap<String, HostInformation*, StringHash> HostMap;
119 HostInformation* m_nonHTTPProtocolHost;
121 WebCore::Timer m_requestTimer;
123 unsigned m_suspendPendingRequestsCount;
124 bool m_isSerialLoadingEnabled;