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, 2007, 2008 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#ifndef ResourceLoadScheduler_h 24#define ResourceLoadScheduler_h 25 26#include "FrameLoaderTypes.h" 27#include "ResourceLoaderOptions.h" 28#include "ResourceLoadPriority.h" 29#include "Timer.h" 30#include <wtf/Deque.h> 31#include <wtf/HashMap.h> 32#include <wtf/HashSet.h> 33#include <wtf/Noncopyable.h> 34#include <wtf/text/StringHash.h> 35#include <wtf/text/WTFString.h> 36 37namespace WebCore { 38 39class CachedResource; 40class Frame; 41class KURL; 42class NetscapePlugInStreamLoader; 43class NetscapePlugInStreamLoaderClient; 44class ResourceLoader; 45class ResourceRequest; 46class SubresourceLoader; 47 48class ResourceLoadScheduler { 49 WTF_MAKE_NONCOPYABLE(ResourceLoadScheduler); WTF_MAKE_FAST_ALLOCATED; 50public: 51 friend ResourceLoadScheduler* resourceLoadScheduler(); 52 53 virtual PassRefPtr<SubresourceLoader> scheduleSubresourceLoad(Frame*, CachedResource*, const ResourceRequest&, ResourceLoadPriority, const ResourceLoaderOptions&); 54 virtual PassRefPtr<NetscapePlugInStreamLoader> schedulePluginStreamLoad(Frame*, NetscapePlugInStreamLoaderClient*, const ResourceRequest&); 55 virtual void remove(ResourceLoader*); 56 virtual void crossOriginRedirectReceived(ResourceLoader*, const KURL& redirectURL); 57 58 virtual void servePendingRequests(ResourceLoadPriority minimumPriority = ResourceLoadPriorityVeryLow); 59 virtual void suspendPendingRequests(); 60 virtual void resumePendingRequests(); 61 62 bool isSerialLoadingEnabled() const { return m_isSerialLoadingEnabled; } 63 virtual void setSerialLoadingEnabled(bool b) { m_isSerialLoadingEnabled = b; } 64 65 class Suspender { 66 public: 67 explicit Suspender(ResourceLoadScheduler& scheduler) : m_scheduler(scheduler) { m_scheduler.suspendPendingRequests(); } 68 ~Suspender() { m_scheduler.resumePendingRequests(); } 69 private: 70 ResourceLoadScheduler& m_scheduler; 71 }; 72 73protected: 74 ResourceLoadScheduler(); 75 virtual ~ResourceLoadScheduler(); 76 77 void notifyDidScheduleResourceRequest(ResourceLoader*); 78 79private: 80 void scheduleLoad(ResourceLoader*, ResourceLoadPriority); 81 void scheduleServePendingRequests(); 82 void requestTimerFired(Timer<ResourceLoadScheduler>*); 83 84 bool isSuspendingPendingRequests() const { return !!m_suspendPendingRequestsCount; } 85 86 class HostInformation { 87 WTF_MAKE_NONCOPYABLE(HostInformation); WTF_MAKE_FAST_ALLOCATED; 88 public: 89 HostInformation(const String&, unsigned); 90 ~HostInformation(); 91 92 const String& name() const { return m_name; } 93 void schedule(ResourceLoader*, ResourceLoadPriority = ResourceLoadPriorityVeryLow); 94 void addLoadInProgress(ResourceLoader*); 95 void remove(ResourceLoader*); 96 bool hasRequests() const; 97 bool limitRequests(ResourceLoadPriority) const; 98 99 typedef Deque<RefPtr<ResourceLoader> > RequestQueue; 100 RequestQueue& requestsPending(ResourceLoadPriority priority) { return m_requestsPending[priority]; } 101 102 private: 103 RequestQueue m_requestsPending[ResourceLoadPriorityHighest + 1]; 104 typedef HashSet<RefPtr<ResourceLoader> > RequestMap; 105 RequestMap m_requestsLoading; 106 const String m_name; 107 const int m_maxRequestsInFlight; 108 }; 109 110 enum CreateHostPolicy { 111 CreateIfNotFound, 112 FindOnly 113 }; 114 115 HostInformation* hostForURL(const KURL&, CreateHostPolicy = FindOnly); 116 void servePendingRequests(HostInformation*, ResourceLoadPriority); 117 118 typedef HashMap<String, HostInformation*, StringHash> HostMap; 119 HostMap m_hosts; 120 HostInformation* m_nonHTTPProtocolHost; 121 122 Timer<ResourceLoadScheduler> m_requestTimer; 123 124 unsigned m_suspendPendingRequestsCount; 125 bool m_isSerialLoadingEnabled; 126}; 127 128ResourceLoadScheduler* resourceLoadScheduler(); 129 130} 131 132#endif 133