1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CONTENT_BROWSER_LOADER_RESOURCE_SCHEDULER_H_
6 #define CONTENT_BROWSER_LOADER_RESOURCE_SCHEDULER_H_
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/threading/non_thread_safe.h"
15 #include "content/common/content_export.h"
16 #include "net/base/priority_queue.h"
17 #include "net/base/request_priority.h"
24 class ResourceThrottle;
26 // There is one ResourceScheduler. All renderer-initiated HTTP requests are
27 // expected to pass through it.
29 // There are two types of input to the scheduler:
30 // 1. Requests to start, cancel, or finish fetching a resource.
31 // 2. Notifications for renderer events, such as new tabs, navigation and
34 // These input come from different threads, so they may not be in sync. The UI
35 // thread is considered the authority on renderer lifetime, which means some
36 // IPCs may be meaningless if they arrive after the UI thread signals a renderer
39 // The ResourceScheduler tracks many Clients, which should correlate with tabs.
40 // A client is uniquely identified by its child_id and route_id.
42 // Each Client may have many Requests in flight. Requests are uniquely
43 // identified within a Client by its ScheduledResourceRequest.
45 // Users should call ScheduleRequest() to notify this ResourceScheduler of a
46 // new request. The returned ResourceThrottle should be destroyed when the load
47 // finishes or is canceled.
49 // The scheduler may defer issuing the request via the ResourceThrottle
50 // interface or it may alter the request's priority by calling set_priority() on
52 class CONTENT_EXPORT ResourceScheduler : public base::NonThreadSafe {
57 // Requests that this ResourceScheduler schedule, and eventually loads, the
58 // specified |url_request|. Caller should delete the returned ResourceThrottle
59 // when the load completes or is canceled.
60 scoped_ptr<ResourceThrottle> ScheduleRequest(
61 int child_id, int route_id, net::URLRequest* url_request);
63 // Signals from the UI thread, posted as tasks on the IO thread:
65 // Called when a renderer is created.
66 void OnClientCreated(int child_id, int route_id);
68 // Called when a renderer is destroyed.
69 void OnClientDeleted(int child_id, int route_id);
71 // Signals from IPC messages directly from the renderers:
73 // Called when a client navigates to a new main document.
74 void OnNavigate(int child_id, int route_id);
76 // Called when the client has parsed the <body> element. This is a signal that
77 // resource loads won't interfere with first paint.
78 void OnWillInsertBody(int child_id, int route_id);
82 class ScheduledResourceRequest;
85 typedef int64 ClientId;
86 typedef std::map<ClientId, Client*> ClientMap;
87 typedef std::set<ScheduledResourceRequest*> RequestSet;
89 // Called when a ScheduledResourceRequest is destroyed.
90 void RemoveRequest(ScheduledResourceRequest* request);
92 // Unthrottles the |request| and adds it to |client|.
93 void StartRequest(ScheduledResourceRequest* request, Client* client);
95 // Update the queue position for |request|, possibly causing it to start
98 // Queues are maintained for each priority level. When |request| is
99 // reprioritized, it will move to the end of the queue for that priority
101 void ReprioritizeRequest(ScheduledResourceRequest* request,
102 net::RequestPriority new_priority);
104 // Attempts to load any pending requests in |client|, based on the
105 // results of ShouldStartRequest().
106 void LoadAnyStartablePendingRequests(Client* client);
108 // Returns the number of requests with priority < LOW that are currently in
110 size_t GetNumDelayableRequestsInFlight(Client* client) const;
112 // Returns true if the request should start. This is the core scheduling
114 bool ShouldStartRequest(ScheduledResourceRequest* request,
115 Client* client) const;
117 // Returns the client ID for the given |child_id| and |route_id| combo.
118 ClientId MakeClientId(int child_id, int route_id);
120 ClientMap client_map_;
121 RequestSet unowned_requests_;
124 } // namespace content
126 #endif // CONTENT_BROWSER_LOADER_RESOURCE_SCHEDULER_H_