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