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"
25 class ResourceThrottle
;
27 // There is one ResourceScheduler. All renderer-initiated HTTP requests are
28 // expected to pass through it.
30 // There are two types of input to the scheduler:
31 // 1. Requests to start, cancel, or finish fetching a resource.
32 // 2. Notifications for renderer events, such as new tabs, navigation and
35 // These input come from different threads, so they may not be in sync. The UI
36 // thread is considered the authority on renderer lifetime, which means some
37 // IPCs may be meaningless if they arrive after the UI thread signals a renderer
40 // The ResourceScheduler tracks many Clients, which should correlate with tabs.
41 // A client is uniquely identified by its child_id and route_id.
43 // Each Client may have many Requests in flight. Requests are uniquely
44 // identified within a Client by its ScheduledResourceRequest.
46 // Users should call ScheduleRequest() to notify this ResourceScheduler of a
47 // new request. The returned ResourceThrottle should be destroyed when the load
48 // finishes or is canceled.
50 // The scheduler may defer issuing the request via the ResourceThrottle
51 // interface or it may alter the request's priority by calling set_priority() on
53 class CONTENT_EXPORT ResourceScheduler
: public base::NonThreadSafe
{
58 // Requests that this ResourceScheduler schedule, and eventually loads, the
59 // specified |url_request|. Caller should delete the returned ResourceThrottle
60 // when the load completes or is canceled.
61 scoped_ptr
<ResourceThrottle
> ScheduleRequest(
62 int child_id
, int route_id
, net::URLRequest
* url_request
);
64 // Signals from the UI thread, posted as tasks on the IO thread:
66 // Called when a renderer is created.
67 void OnClientCreated(int child_id
, int route_id
);
69 // Called when a renderer is destroyed.
70 void OnClientDeleted(int child_id
, int route_id
);
72 // Signals from IPC messages directly from the renderers:
74 // Called when a client navigates to a new main document.
75 void OnNavigate(int child_id
, int route_id
);
77 // Called when the client has parsed the <body> element. This is a signal that
78 // resource loads won't interfere with first paint.
79 void OnWillInsertBody(int child_id
, int route_id
);
81 // Signals from the IO thread
83 // Called when we received a response to a http request that was served
84 // from a proxy using SPDY.
85 void OnReceivedSpdyProxiedHttpResponse(int child_id
, int route_id
);
89 class ScheduledResourceRequest
;
90 struct RequestPriorityParams
;
91 struct ScheduledResourceSorter
{
92 bool operator()(const ScheduledResourceRequest
* a
,
93 const ScheduledResourceRequest
* b
) const;
97 typedef int64 ClientId
;
98 typedef std::map
<ClientId
, Client
*> ClientMap
;
99 typedef std::set
<ScheduledResourceRequest
*> RequestSet
;
101 // Called when a ScheduledResourceRequest is destroyed.
102 void RemoveRequest(ScheduledResourceRequest
* request
);
104 // Update the queue position for |request|, possibly causing it to start
107 // Queues are maintained for each priority level. When |request| is
108 // reprioritized, it will move to the end of the queue for that priority
110 void ReprioritizeRequest(ScheduledResourceRequest
* request
,
111 net::RequestPriority new_priority
,
112 int intra_priority_value
);
114 // Returns the client ID for the given |child_id| and |route_id| combo.
115 ClientId
MakeClientId(int child_id
, int route_id
);
117 ClientMap client_map_
;
118 RequestSet unowned_requests_
;
121 } // namespace content
123 #endif // CONTENT_BROWSER_LOADER_RESOURCE_SCHEDULER_H_