Move URL from WebCore to WTF
[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::FrameLoader&, unsigned long, const WebCore::ResourceRequest&, WebCore::ClientCredentialPolicy, const WebCore::FetchOptions&, const WebCore::HTTPHeaderMap&, WebCore::ResourceError&, WebCore::ResourceResponse&, Vector<char>&) final;
54     void pageLoadCompleted(uint64_t webPageID) final;
55
56     void remove(WebCore::ResourceLoader*) final;
57     void setDefersLoading(WebCore::ResourceLoader&, bool) final;
58     void crossOriginRedirectReceived(WebCore::ResourceLoader*, const URL& redirectURL) final;
59     
60     void servePendingRequests(WebCore::ResourceLoadPriority minimumPriority = WebCore::ResourceLoadPriority::VeryLow) final;
61     void suspendPendingRequests() final;
62     void resumePendingRequests() final;
63
64     void startPingLoad(WebCore::Frame&, WebCore::ResourceRequest&, const WebCore::HTTPHeaderMap&, const WebCore::FetchOptions&, PingLoadCompletionHandler&&) final;
65
66     void preconnectTo(WebCore::FrameLoader&, const URL&, WebCore::StoredCredentialsPolicy, PreconnectCompletionHandler&&) final;
67
68     void storeDerivedDataToCache(const SHA1::Digest&, const String&, const String&, WebCore::SharedBuffer&) final { }
69
70     void setCaptureExtraNetworkLoadMetricsEnabled(bool) final { }
71
72     bool isSerialLoadingEnabled() const { return m_isSerialLoadingEnabled; }
73     void setSerialLoadingEnabled(bool b) { m_isSerialLoadingEnabled = b; }
74
75     void schedulePluginStreamLoad(WebCore::Frame&, WebCore::NetscapePlugInStreamLoaderClient&, WebCore::ResourceRequest&&, CompletionHandler<void(RefPtr<WebCore::NetscapePlugInStreamLoader>&&)>&&);
76
77     bool isOnLine() const final;
78     void addOnlineStateChangeListener(WTF::Function<void(bool)>&&) final;
79
80 protected:
81     virtual ~WebResourceLoadScheduler();
82
83 private:
84     void scheduleLoad(WebCore::ResourceLoader*);
85     void scheduleServePendingRequests();
86     void requestTimerFired();
87
88     bool isSuspendingPendingRequests() const { return !!m_suspendPendingRequestsCount; }
89
90     class HostInformation {
91         WTF_MAKE_NONCOPYABLE(HostInformation); WTF_MAKE_FAST_ALLOCATED;
92     public:
93         HostInformation(const String&, unsigned);
94         ~HostInformation();
95         
96         const String& name() const { return m_name; }
97         void schedule(WebCore::ResourceLoader*, WebCore::ResourceLoadPriority = WebCore::ResourceLoadPriority::VeryLow);
98         void addLoadInProgress(WebCore::ResourceLoader*);
99         void remove(WebCore::ResourceLoader*);
100         bool hasRequests() const;
101         bool limitRequests(WebCore::ResourceLoadPriority) const;
102
103         typedef Deque<RefPtr<WebCore::ResourceLoader>> RequestQueue;
104         RequestQueue& requestsPending(WebCore::ResourceLoadPriority priority) { return m_requestsPending[priorityToIndex(priority)]; }
105
106     private:
107         static unsigned priorityToIndex(WebCore::ResourceLoadPriority);
108
109         std::array<RequestQueue, WebCore::resourceLoadPriorityCount> m_requestsPending;
110         typedef HashSet<RefPtr<WebCore::ResourceLoader>> RequestMap;
111         RequestMap m_requestsLoading;
112         const String m_name;
113         const unsigned m_maxRequestsInFlight;
114     };
115
116     enum CreateHostPolicy {
117         CreateIfNotFound,
118         FindOnly
119     };
120     
121     HostInformation* hostForURL(const URL&, CreateHostPolicy = FindOnly);
122     void servePendingRequests(HostInformation*, WebCore::ResourceLoadPriority);
123
124     typedef HashMap<String, HostInformation*, StringHash> HostMap;
125     HostMap m_hosts;
126     HostInformation* m_nonHTTPProtocolHost;
127         
128     WebCore::Timer m_requestTimer;
129
130     unsigned m_suspendPendingRequestsCount;
131     bool m_isSerialLoadingEnabled;
132 };