Home | History | Annotate | Download | only in loader
      1 /*
      2     Copyright (C) 1998 Lars Knoll (knoll (at) mpi-hd.mpg.de)
      3     Copyright (C) 2001 Dirk Mueller <mueller (at) 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 "PlatformString.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 
     37 namespace WebCore {
     38 
     39 class Frame;
     40 class KURL;
     41 class NetscapePlugInStreamLoader;
     42 class NetscapePlugInStreamLoaderClient;
     43 class ResourceLoader;
     44 class ResourceRequest;
     45 class SubresourceLoader;
     46 class SubresourceLoaderClient;
     47 
     48 class ResourceLoadScheduler {
     49     WTF_MAKE_NONCOPYABLE(ResourceLoadScheduler);
     50 public:
     51     friend ResourceLoadScheduler* resourceLoadScheduler();
     52 
     53     PassRefPtr<SubresourceLoader> scheduleSubresourceLoad(Frame*, SubresourceLoaderClient*, const ResourceRequest&, ResourceLoadPriority = ResourceLoadPriorityLow, SecurityCheckPolicy = DoSecurityCheck, bool sendResourceLoadCallbacks = true, bool shouldContentSniff = true, const String& optionalOutgoingReferrer = String(), bool shouldBufferData = true);
     54     PassRefPtr<NetscapePlugInStreamLoader> schedulePluginStreamLoad(Frame*, NetscapePlugInStreamLoaderClient*, const ResourceRequest&);
     55     void addMainResourceLoad(ResourceLoader*);
     56     void remove(ResourceLoader*);
     57     void crossOriginRedirectReceived(ResourceLoader*, const KURL& redirectURL);
     58 
     59     void servePendingRequests(ResourceLoadPriority minimumPriority = ResourceLoadPriorityVeryLow);
     60     void suspendPendingRequests();
     61     void resumePendingRequests();
     62 
     63     bool isSerialLoadingEnabled() const { return m_isSerialLoadingEnabled; }
     64     void setSerialLoadingEnabled(bool b) { m_isSerialLoadingEnabled = b; }
     65 
     66 private:
     67     ResourceLoadScheduler();
     68     ~ResourceLoadScheduler();
     69 
     70     void scheduleLoad(ResourceLoader*, ResourceLoadPriority);
     71     void scheduleServePendingRequests();
     72     void requestTimerFired(Timer<ResourceLoadScheduler>*);
     73 
     74     class HostInformation {
     75         WTF_MAKE_NONCOPYABLE(HostInformation);
     76     public:
     77         HostInformation(const String&, unsigned);
     78         ~HostInformation();
     79 
     80         const String& name() const { return m_name; }
     81         void schedule(ResourceLoader*, ResourceLoadPriority = ResourceLoadPriorityVeryLow);
     82         void addLoadInProgress(ResourceLoader*);
     83         void remove(ResourceLoader*);
     84         bool hasRequests() const;
     85         bool limitRequests(ResourceLoadPriority) const;
     86 
     87         typedef Deque<RefPtr<ResourceLoader> > RequestQueue;
     88         RequestQueue& requestsPending(ResourceLoadPriority priority) { return m_requestsPending[priority]; }
     89 
     90     private:
     91         RequestQueue m_requestsPending[ResourceLoadPriorityHighest + 1];
     92         typedef HashSet<RefPtr<ResourceLoader> > RequestMap;
     93         RequestMap m_requestsLoading;
     94         const String m_name;
     95         const int m_maxRequestsInFlight;
     96     };
     97 
     98     enum CreateHostPolicy {
     99         CreateIfNotFound,
    100         FindOnly
    101     };
    102 
    103     HostInformation* hostForURL(const KURL&, CreateHostPolicy = FindOnly);
    104     void servePendingRequests(HostInformation*, ResourceLoadPriority);
    105 
    106     typedef HashMap<String, HostInformation*, StringHash> HostMap;
    107     HostMap m_hosts;
    108     HostInformation* m_nonHTTPProtocolHost;
    109 
    110     Timer<ResourceLoadScheduler> m_requestTimer;
    111 
    112     bool m_isSuspendingPendingRequests;
    113     bool m_isSerialLoadingEnabled;
    114 };
    115 
    116 ResourceLoadScheduler* resourceLoadScheduler();
    117 
    118 }
    119 
    120 #endif
    121