OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_BROWSER_LOADER_RESOURCE_SCHEDULER_H_ | 5 #ifndef CONTENT_BROWSER_LOADER_RESOURCE_SCHEDULER_H_ |
6 #define CONTENT_BROWSER_LOADER_RESOURCE_SCHEDULER_H_ | 6 #define CONTENT_BROWSER_LOADER_RESOURCE_SCHEDULER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <vector> | |
11 | 10 |
12 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
13 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
14 #include "base/memory/linked_ptr.h" | 13 #include "base/memory/linked_ptr.h" |
15 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
16 #include "base/threading/non_thread_safe.h" | 15 #include "base/threading/non_thread_safe.h" |
17 #include "content/common/content_export.h" | 16 #include "content/common/content_export.h" |
18 #include "content/public/browser/global_request_id.h" | 17 #include "content/public/browser/global_request_id.h" |
18 #include "net/base/priority_queue.h" | |
19 #include "net/base/request_priority.h" | |
19 | 20 |
20 namespace net { | 21 namespace net { |
21 class URLRequest; | 22 class URLRequest; |
22 } | 23 } |
23 | 24 |
24 namespace content { | 25 namespace content { |
25 class ResourceThrottle; | 26 class ResourceThrottle; |
26 | 27 |
27 // There is one ResourceScheduler. All renderer-initiated HTTP requests are | 28 // There is one ResourceScheduler. All renderer-initiated HTTP requests are |
28 // expected to pass through it. | 29 // expected to pass through it. |
29 // | 30 // |
30 // There are two types of input to the scheduler: | 31 // There are two types of input to the scheduler: |
31 // 1. Requests to start, cancel, or finish fetching a resource. | 32 // 1. Requests to start, cancel, or finish fetching a resource. |
32 // 2. Notifications for renderer events, such as new tabs, navigation and | 33 // 2. Notifications for renderer events, such as new tabs, navigation and |
33 // painting. | 34 // painting. |
34 // | 35 // |
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 | |
39 // has been deleted. | |
40 // | |
35 // The ResourceScheduler tracks many Clients, which should correlate with tabs. | 41 // The ResourceScheduler tracks many Clients, which should correlate with tabs. |
36 // A client is uniquely identified by its child_id and route_id. | 42 // A client is uniquely identified by its child_id and route_id. |
37 // | 43 // |
38 // Each Client may have many Requests in flight. Requests are uniquely | 44 // Each Client may have many Requests in flight. Requests are uniquely |
39 // identified within a Client by its ScheduledResourceRequest. | 45 // identified within a Client by its ScheduledResourceRequest. |
40 // | 46 // |
41 // Users should call ScheduleRequest() to notify this ResourceScheduler of a | 47 // Users should call ScheduleRequest() to notify this ResourceScheduler of a |
42 // new request. The returned ResourceThrottle should be destroyed when the load | 48 // new request. The returned ResourceThrottle should be destroyed when the load |
43 // finishes or is canceled. | 49 // finishes or is canceled. |
44 // | 50 // |
45 // The scheduler may defer issuing the request via the ResourceThrottle | 51 // The scheduler may defer issuing the request via the ResourceThrottle |
46 // interface or it may alter the request's priority by calling set_priority() on | 52 // interface or it may alter the request's priority by calling set_priority() on |
47 // the URLRequest. | 53 // the URLRequest. |
48 class CONTENT_EXPORT ResourceScheduler : public base::NonThreadSafe { | 54 class CONTENT_EXPORT ResourceScheduler : public base::NonThreadSafe { |
49 public: | 55 public: |
50 ResourceScheduler(); | 56 ResourceScheduler(); |
51 ~ResourceScheduler(); | 57 ~ResourceScheduler(); |
52 | 58 |
53 // Requests that this ResourceScheduler schedule, and eventually loads, the | 59 // Requests that this ResourceScheduler schedule, and eventually loads, the |
54 // specified |url_request|. Caller should delete the returned ResourceThrottle | 60 // specified |url_request|. Caller should delete the returned ResourceThrottle |
55 // when the load completes or is canceled. | 61 // when the load completes or is canceled. |
56 scoped_ptr<ResourceThrottle> ScheduleRequest( | 62 scoped_ptr<ResourceThrottle> ScheduleRequest( |
57 int child_id, int route_id, net::URLRequest* url_request); | 63 int child_id, int route_id, net::URLRequest* url_request); |
58 | 64 |
65 // Signals from the UI thread, posted as tasks on the IO thread: | |
66 | |
59 // Called when a renderer is created. | 67 // Called when a renderer is created. |
60 void OnClientCreated(int child_id, int route_id); | 68 void OnClientCreated(int child_id, int route_id); |
61 | 69 |
62 // Called when a renderer is destroyed. | 70 // Called when a renderer is destroyed. |
63 void OnClientDeleted(int child_id, int route_id); | 71 void OnClientDeleted(int child_id, int route_id); |
64 | 72 |
73 // Signals from IPC messages directly from the renderers: | |
74 | |
65 // Called when a client navigates to a new main document. | 75 // Called when a client navigates to a new main document. |
66 void OnNavigate(int child_id, int route_id); | 76 void OnNavigate(int child_id, int route_id); |
67 | 77 |
68 // Called when the client has parsed the <body> element. This is a signal that | 78 // Called when the client has parsed the <body> element. This is a signal that |
69 // resource loads won't interfere with first paint. | 79 // resource loads won't interfere with first paint. |
70 void OnWillInsertBody(int child_id, int route_id); | 80 void OnWillInsertBody(int child_id, int route_id); |
71 | 81 |
72 private: | 82 private: |
83 struct Client; | |
84 class RequestQueue; | |
73 class ScheduledResourceRequest; | 85 class ScheduledResourceRequest; |
74 friend class ScheduledResourceRequest; | |
75 struct Client; | |
76 | 86 |
77 typedef int64 ClientId; | 87 typedef int64 ClientId; |
78 typedef std::map<ClientId, Client*> ClientMap; | 88 typedef std::map<ClientId, Client*> ClientMap; |
79 typedef std::vector<ScheduledResourceRequest*> RequestQueue; | |
80 typedef std::set<ScheduledResourceRequest*> RequestSet; | 89 typedef std::set<ScheduledResourceRequest*> RequestSet; |
81 | 90 |
82 struct Client { | |
83 Client(); | |
84 ~Client(); | |
85 | |
86 bool has_body; | |
87 RequestQueue pending_requests; | |
88 RequestSet in_flight_requests; | |
89 }; | |
90 | |
91 // Called when a ScheduledResourceRequest is destroyed. | 91 // Called when a ScheduledResourceRequest is destroyed. |
92 void RemoveRequest(ScheduledResourceRequest* request); | 92 void RemoveRequest(ScheduledResourceRequest* request); |
93 | 93 |
94 // Unthrottles the |request| and adds it to |client|. | 94 // Unthrottles the |request| and adds it to |client|. |
95 void StartRequest(ScheduledResourceRequest* request, Client* client); | 95 void StartRequest(ScheduledResourceRequest* request, Client* client); |
96 | 96 |
97 // Calls StartRequest on all pending requests for |client|. | 97 // Update the queue position for |request|, possibly causing it to start |
98 void LoadPendingRequests(Client* client); | 98 // loading. |
99 // | |
100 // Queues are maintained for each priority level. When |request| is | |
101 // reprioritized, it will move to the end of the queue for that priority | |
102 // level. | |
willchan no longer on Chromium
2013/03/19 20:53:53
Add a test for this behavior.
James Simonsen
2013/03/19 23:00:46
Done.
| |
103 void ReprioritizeRequest(ScheduledResourceRequest* request, | |
104 net::RequestPriority new_priority); | |
105 | |
106 // Attempts to load any pending requests in |client|, based on the | |
107 // results of ShouldStartRequest(). | |
108 void LoadAnyStartablePendingRequests(Client* client); | |
109 | |
110 // Returns the number of requests with priority < LOW that are currently in | |
111 // flight. | |
112 size_t GetNumLowPriorityRequestsInFlight(Client* client) const; | |
113 | |
114 // Returns true if the request should start. This is the core scheduling | |
115 // algorithm. | |
116 bool ShouldStartRequest(ScheduledResourceRequest* request, | |
117 Client* client) const; | |
99 | 118 |
100 // Returns the client ID for the given |child_id| and |route_id| combo. | 119 // Returns the client ID for the given |child_id| and |route_id| combo. |
101 ClientId MakeClientId(int child_id, int route_id); | 120 ClientId MakeClientId(int child_id, int route_id); |
102 | 121 |
103 ClientMap client_map_; | 122 ClientMap client_map_; |
104 RequestSet unowned_requests_; | 123 RequestSet unowned_requests_; |
105 }; | 124 }; |
106 | 125 |
107 } // namespace content | 126 } // namespace content |
108 | 127 |
109 #endif // CONTENT_BROWSER_LOADER_RESOURCE_SCHEDULER_H_ | 128 #endif // CONTENT_BROWSER_LOADER_RESOURCE_SCHEDULER_H_ |
OLD | NEW |