1/*
2 * Copyright (C) 2012 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef NetworkResourceLoadScheduler_h
27#define NetworkResourceLoadScheduler_h
28
29#include <WebCore/ResourceLoadPriority.h>
30#include <WebCore/Timer.h>
31#include <wtf/HashMap.h>
32#include <wtf/HashSet.h>
33#include <wtf/text/StringHash.h>
34
35#if ENABLE(NETWORK_PROCESS)
36
37namespace WebCore {
38class URL;
39}
40
41namespace WebKit {
42
43class HostRecord;
44class NetworkResourceLoader;
45
46class NetworkResourceLoadScheduler {
47    WTF_MAKE_NONCOPYABLE(NetworkResourceLoadScheduler); WTF_MAKE_FAST_ALLOCATED;
48
49public:
50    NetworkResourceLoadScheduler();
51
52    // Adds the request to the queue for its host.
53    void scheduleLoader(PassRefPtr<NetworkResourceLoader>);
54
55    // Called by the WebProcess when a ResourceLoader is being cleaned up.
56    void removeLoader(NetworkResourceLoader*);
57
58    // Called within the NetworkProcess on a background thread when a resource load has finished.
59    void scheduleRemoveLoader(NetworkResourceLoader*);
60
61    void receivedRedirect(NetworkResourceLoader*, const WebCore::URL& redirectURL);
62    void servePendingRequests(WebCore::ResourceLoadPriority = WebCore::ResourceLoadPriorityVeryLow);
63
64    // For NetworkProcess statistics reporting.
65    uint64_t hostsPendingCount() const;
66    uint64_t loadsPendingCount() const;
67    uint64_t hostsActiveCount() const;
68    uint64_t loadsActiveCount() const;
69
70private:
71    enum CreateHostPolicy {
72        CreateIfNotFound,
73        FindOnly
74    };
75
76    HostRecord* hostForURL(const WebCore::URL&, CreateHostPolicy = FindOnly);
77
78    void scheduleServePendingRequests();
79    void requestTimerFired(WebCore::Timer<NetworkResourceLoadScheduler>*);
80
81    void platformInitializeMaximumHTTPConnectionCountPerHost();
82
83    static void removeScheduledLoaders(void* context);
84    void removeScheduledLoaders();
85
86    typedef HashMap<String, RefPtr<HostRecord>, StringHash> HostMap;
87    HostMap m_hosts;
88
89    typedef HashSet<RefPtr<NetworkResourceLoader>> NetworkResourceLoaderSet;
90    NetworkResourceLoaderSet m_loaders;
91
92    RefPtr<HostRecord> m_nonHTTPProtocolHost;
93
94    bool m_isSerialLoadingEnabled;
95
96    WebCore::Timer<NetworkResourceLoadScheduler> m_requestTimer;
97
98    Mutex m_loadersToRemoveMutex;
99    Vector<RefPtr<NetworkResourceLoader>> m_loadersToRemove;
100
101    unsigned m_maxRequestsInFlightPerHost;
102};
103
104} // namespace WebKit
105
106#endif // ENABLE(NETWORK_PROCESS)
107
108#endif // NetworkResourceLoadScheduler_h
109