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 "base/timer/timer.h"
16 #include "content/common/content_export.h"
17 #include "net/base/priority_queue.h"
18 #include "net/base/request_priority.h"
26 class ResourceThrottle
;
28 // There is one ResourceScheduler. All renderer-initiated HTTP requests are
29 // expected to pass through it.
31 // There are two types of input to the scheduler:
32 // 1. Requests to start, cancel, or finish fetching a resource.
33 // 2. Notifications for renderer events, such as new tabs, navigation and
36 // These input come from different threads, so they may not be in sync. The UI
37 // thread is considered the authority on renderer lifetime, which means some
38 // IPCs may be meaningless if they arrive after the UI thread signals a renderer
41 // The ResourceScheduler tracks many Clients, which should correlate with tabs.
42 // A client is uniquely identified by its child_id and route_id.
44 // Each Client may have many Requests in flight. Requests are uniquely
45 // identified within a Client by its ScheduledResourceRequest.
47 // Users should call ScheduleRequest() to notify this ResourceScheduler of a new
48 // request. The returned ResourceThrottle should be destroyed when the load
49 // finishes or is canceled, before the net::URLRequest.
51 // The scheduler may defer issuing the request via the ResourceThrottle
52 // interface or it may alter the request's priority by calling set_priority() on
54 class CONTENT_EXPORT ResourceScheduler
: public base::NonThreadSafe
{
56 enum ClientThrottleState
{
57 // TODO(aiolos): Add logic to ShouldStartRequest for PAUSED Clients to only
58 // issue synchronous requests.
59 // TODO(aiolos): Add max number of THROTTLED Clients, and logic to set
60 // subsquent Clients to PAUSED instead. Also add logic to unpause a Client
61 // when a background Client becomes COALESCED (ie, finishes loading.)
62 // TODO(aiolos): Add tests for the above mentioned logic.
64 // Currently being deleted client.
65 // This state currently follows the same logic for loading requests as
66 // UNTHROTTLED/ACTIVE_AND_LOADING Clients. See above TODO's.
68 // Loaded background client, all observable clients loaded.
70 // Background client, an observable client is loading.
72 // Observable (active) loaded client or
73 // Loading background client, all observable clients loaded.
74 // Note that clients which would be COALESCED are UNTHROTTLED until
75 // coalescing is turned on.
77 // Observable (active) loading client.
84 // Use a mock timer when testing.
85 void set_timer_for_testing(scoped_ptr
<base::Timer
> timer
) {
86 coalescing_timer_
.reset(timer
.release());
89 // TODO(aiolos): Remove when throttling and coalescing have landed
90 void SetThrottleOptionsForTesting(bool should_throttle
, bool should_coalesce
);
92 bool should_coalesce() const { return should_coalesce_
; }
93 bool should_throttle() const { return should_throttle_
; }
95 ClientThrottleState
GetClientStateForTesting(int child_id
, int route_id
);
97 // Requests that this ResourceScheduler schedule, and eventually loads, the
98 // specified |url_request|. Caller should delete the returned ResourceThrottle
99 // when the load completes or is canceled, before |url_request| is deleted.
100 scoped_ptr
<ResourceThrottle
> ScheduleRequest(int child_id
,
103 net::URLRequest
* url_request
);
105 // Signals from the UI thread, posted as tasks on the IO thread:
107 // Called when a renderer is created.
108 void OnClientCreated(int child_id
,
113 // Called when a renderer is destroyed.
114 void OnClientDeleted(int child_id
, int route_id
);
116 // Called when a renderer stops or restarts loading.
117 void OnLoadingStateChanged(int child_id
, int route_id
, bool is_loaded
);
119 // Called when a Client is shown or hidden.
120 void OnVisibilityChanged(int child_id
, int route_id
, bool is_visible
);
122 // Called when a Client starts or stops playing audio.
123 void OnAudibilityChanged(int child_id
, int route_id
, bool is_audible
);
125 // Signals from IPC messages directly from the renderers:
127 // Called when a client navigates to a new main document.
128 void OnNavigate(int child_id
, int route_id
);
130 // Called when the client has parsed the <body> element. This is a signal that
131 // resource loads won't interfere with first paint.
132 void OnWillInsertBody(int child_id
, int route_id
);
134 // Signals from the IO thread:
136 // Called when we received a response to a http request that was served
137 // from a proxy using SPDY.
138 void OnReceivedSpdyProxiedHttpResponse(int child_id
, int route_id
);
142 // Called to check if all user observable tabs have completed loading.
143 bool active_clients_loaded() const { return active_clients_loading_
== 0; }
145 bool IsClientVisibleForTesting(int child_id
, int route_id
);
147 // Returns true if at least one client is currently loading.
148 bool HasLoadingClients() const;
150 // Update the priority for |request|. Modifies request->priority(), and may
151 // start the request loading if it wasn't already started.
152 void ReprioritizeRequest(net::URLRequest
* request
,
153 net::RequestPriority new_priority
,
154 int intra_priority_value
);
157 // Returns true if limiting of outstanding requests is enabled.
158 bool limit_outstanding_requests() const {
159 return limit_outstanding_requests_
;
162 // Returns the outstanding request limit. Only valid if
163 // |IsLimitingOutstandingRequests()|.
164 size_t outstanding_request_limit() const {
165 return outstanding_request_limit_
;
168 // Returns the priority level above which resources are considered
169 // layout-blocking if the html_body has not started. It is also the threshold
170 // below which resources are considered delayable (and for completeness,
171 // a request that matches the threshold level is a high-priority but not
172 // layout-blocking request).
173 net::RequestPriority
non_delayable_threshold() const {
174 return non_delayable_threshold_
;
177 // Returns true if all delayable requests should be blocked while at least
178 // in_flight_layout_blocking_threshold() layout-blocking requests are
179 // in-flight during the layout-blocking phase of loading.
180 bool enable_in_flight_non_delayable_threshold() const {
181 return enable_in_flight_non_delayable_threshold_
;
184 // Returns the number of in-flight layout-blocking requests above which
185 // all delayable requests should be blocked when
186 // enable_layout_blocking_threshold is set.
187 size_t in_flight_non_delayable_threshold() const {
188 return in_flight_non_delayable_threshold_
;
191 // Returns the maximum number of delayable requests to allow be in-flight
192 // at any point in time while in the layout-blocking phase of loading.
193 size_t max_num_delayable_while_layout_blocking() const {
194 return max_num_delayable_while_layout_blocking_
;
197 // Returns the maximum number of delayable requests to all be in-flight at
198 // any point in time (across all hosts).
199 size_t max_num_delayable_requests() const {
200 return max_num_delayable_requests_
;
204 // Observable client.
206 // Non-observable client.
213 class ScheduledResourceRequest
;
214 struct RequestPriorityParams
;
215 struct ScheduledResourceSorter
{
216 bool operator()(const ScheduledResourceRequest
* a
,
217 const ScheduledResourceRequest
* b
) const;
221 typedef int64 ClientId
;
222 typedef std::map
<ClientId
, Client
*> ClientMap
;
223 typedef std::set
<ScheduledResourceRequest
*> RequestSet
;
225 // Called when a ScheduledResourceRequest is destroyed.
226 void RemoveRequest(ScheduledResourceRequest
* request
);
228 // These calls may update the ThrottleState of all clients, and have the
229 // potential to be re-entrant.
230 // Called when a Client newly becomes active loading.
231 void IncrementActiveClientsLoading();
232 // Called when an active and loading Client either completes loading or
234 void DecrementActiveClientsLoading();
236 void OnLoadingActiveClientsStateChangedForAllClients();
238 size_t CountActiveClientsLoading() const;
240 // Called when a Client becomes coalesced.
241 void IncrementCoalescedClients();
242 // Called when a client stops being coalesced.
243 void DecrementCoalescedClients();
245 void LoadCoalescedRequests();
247 size_t CountCoalescedClients() const;
249 // Returns UNKNOWN if the corresponding client is not found, else returns
250 // whether the client is ACTIVE (user-observable) or BACKGROUND.
251 ClientState
GetClientState(ClientId client_id
) const;
253 // Returns the client ID for the given |child_id| and |route_id| combo.
254 ClientId
MakeClientId(int child_id
, int route_id
);
256 // Returns the client for the given |child_id| and |route_id| combo.
257 Client
* GetClient(int child_id
, int route_id
);
259 bool should_coalesce_
;
260 bool should_throttle_
;
261 ClientMap client_map_
;
262 size_t active_clients_loading_
;
263 size_t coalesced_clients_
;
264 bool limit_outstanding_requests_
;
265 size_t outstanding_request_limit_
;
266 net::RequestPriority non_delayable_threshold_
;
267 bool enable_in_flight_non_delayable_threshold_
;
268 size_t in_flight_non_delayable_threshold_
;
269 size_t max_num_delayable_while_layout_blocking_
;
270 size_t max_num_delayable_requests_
;
271 // This is a repeating timer to initiate requests on COALESCED Clients.
272 scoped_ptr
<base::Timer
> coalescing_timer_
;
273 RequestSet unowned_requests_
;
275 DISALLOW_COPY_AND_ASSIGN(ResourceScheduler
);
278 } // namespace content
280 #endif // CONTENT_BROWSER_LOADER_RESOURCE_SCHEDULER_H_