Chromium Code Reviews| 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 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/threading/non_thread_safe.h" | 14 #include "base/threading/non_thread_safe.h" |
| 15 #include "base/timer/timer.h" | 15 #include "base/timer/timer.h" |
| 16 #include "content/common/content_export.h" | 16 #include "content/common/content_export.h" |
| 17 #include "content/public/browser/resource_throttle.h" | |
| 17 #include "net/base/priority_queue.h" | 18 #include "net/base/priority_queue.h" |
| 18 #include "net/base/request_priority.h" | 19 #include "net/base/request_priority.h" |
| 19 | 20 |
| 20 namespace net { | 21 namespace net { |
| 21 class HostPortPair; | 22 class HostPortPair; |
| 22 class URLRequest; | 23 class URLRequest; |
| 23 } | 24 } |
| 24 | 25 |
| 25 namespace content { | 26 namespace content { |
| 26 class ResourceThrottle; | 27 // Handle for a scheduled request. This is declared at namespace scope so that |
| 28 // it can be pre-declared in other header files. | |
| 29 class ScheduledResourceRequest : public ResourceThrottle { | |
| 30 public: | |
| 31 ScheduledResourceRequest(); | |
| 32 ~ScheduledResourceRequest() override; | |
|
davidben
2015/08/12 23:58:11
(Hrm, I would have thought this could be inlined.)
Adam Rice
2015/08/13 21:21:02
It can, but then the compiler has to generate a co
| |
| 33 | |
| 34 virtual void ChangePriority(net::RequestPriority new_priority, | |
| 35 int intra_priority_value) = 0; | |
| 36 | |
| 37 private: | |
| 38 DISALLOW_COPY_AND_ASSIGN(ScheduledResourceRequest); | |
| 39 }; | |
| 27 | 40 |
| 28 // There is one ResourceScheduler. All renderer-initiated HTTP requests are | 41 // There is one ResourceScheduler. All renderer-initiated HTTP requests are |
| 29 // expected to pass through it. | 42 // expected to pass through it. |
| 30 // | 43 // |
| 31 // There are two types of input to the scheduler: | 44 // There are two types of input to the scheduler: |
| 32 // 1. Requests to start, cancel, or finish fetching a resource. | 45 // 1. Requests to start, cancel, or finish fetching a resource. |
| 33 // 2. Notifications for renderer events, such as new tabs, navigation and | 46 // 2. Notifications for renderer events, such as new tabs, navigation and |
| 34 // painting. | 47 // painting. |
| 35 // | 48 // |
| 36 // These input come from different threads, so they may not be in sync. The UI | 49 // 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 | 50 // 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 | 51 // IPCs may be meaningless if they arrive after the UI thread signals a renderer |
| 39 // has been deleted. | 52 // has been deleted. |
| 40 // | 53 // |
| 41 // The ResourceScheduler tracks many Clients, which should correlate with tabs. | 54 // The ResourceScheduler tracks many Clients, which should correlate with tabs. |
| 42 // A client is uniquely identified by its child_id and route_id. | 55 // A client is uniquely identified by its child_id and route_id. |
| 43 // | 56 // |
| 44 // Each Client may have many Requests in flight. Requests are uniquely | 57 // Each Client may have many Requests in flight. Requests are uniquely |
| 45 // identified within a Client by its ScheduledResourceRequest. | 58 // identified within a Client by its ScheduledResourceRequest. |
| 46 // | 59 // |
| 47 // Users should call ScheduleRequest() to notify this ResourceScheduler of a | 60 // Users should call ScheduleRequest() to notify this ResourceScheduler of a new |
| 48 // new request. The returned ResourceThrottle should be destroyed when the load | 61 // request. The returned ScheduledResourceRequest should be destroyed when the |
| 49 // finishes or is canceled. | 62 // load finishes or is canceled. |
| 50 // | 63 // |
| 51 // The scheduler may defer issuing the request via the ResourceThrottle | 64 // 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 | 65 // interface or it may alter the request's priority by calling set_priority() on |
| 53 // the URLRequest. | 66 // the URLRequest. |
| 54 class CONTENT_EXPORT ResourceScheduler : public base::NonThreadSafe { | 67 class CONTENT_EXPORT ResourceScheduler : public base::NonThreadSafe { |
| 55 public: | 68 public: |
| 56 enum ClientThrottleState { | 69 enum ClientThrottleState { |
| 57 // TODO(aiolos): Add logic to ShouldStartRequest for PAUSED Clients to only | 70 // TODO(aiolos): Add logic to ShouldStartRequest for PAUSED Clients to only |
| 58 // issue synchronous requests. | 71 // issue synchronous requests. |
| 59 // TODO(aiolos): Add max number of THROTTLED Clients, and logic to set | 72 // TODO(aiolos): Add max number of THROTTLED Clients, and logic to set |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 96 | 109 |
| 97 // TODO(aiolos): Remove when throttling and coalescing have landed | 110 // TODO(aiolos): Remove when throttling and coalescing have landed |
| 98 void SetThrottleOptionsForTesting(bool should_throttle, bool should_coalesce); | 111 void SetThrottleOptionsForTesting(bool should_throttle, bool should_coalesce); |
| 99 | 112 |
| 100 bool should_coalesce() const { return should_coalesce_; } | 113 bool should_coalesce() const { return should_coalesce_; } |
| 101 bool should_throttle() const { return should_throttle_; } | 114 bool should_throttle() const { return should_throttle_; } |
| 102 | 115 |
| 103 ClientThrottleState GetClientStateForTesting(int child_id, int route_id); | 116 ClientThrottleState GetClientStateForTesting(int child_id, int route_id); |
| 104 | 117 |
| 105 // Requests that this ResourceScheduler schedule, and eventually loads, the | 118 // Requests that this ResourceScheduler schedule, and eventually loads, the |
| 106 // specified |url_request|. Caller should delete the returned ResourceThrottle | 119 // specified |url_request|. Caller should delete the returned |
| 107 // when the load completes or is canceled. | 120 // ScheduledResourceRequest when the load completes or is canceled. |
| 108 scoped_ptr<ResourceThrottle> ScheduleRequest( | 121 scoped_ptr<ScheduledResourceRequest> ScheduleRequest( |
| 109 int child_id, int route_id, net::URLRequest* url_request); | 122 int child_id, |
| 123 int route_id, | |
| 124 bool is_async, | |
| 125 net::URLRequest* url_request); | |
| 110 | 126 |
| 111 // Signals from the UI thread, posted as tasks on the IO thread: | 127 // Signals from the UI thread, posted as tasks on the IO thread: |
| 112 | 128 |
| 113 // Called when a renderer is created. | 129 // Called when a renderer is created. |
| 114 void OnClientCreated(int child_id, | 130 void OnClientCreated(int child_id, |
| 115 int route_id, | 131 int route_id, |
| 116 bool is_visible, | 132 bool is_visible, |
| 117 bool is_audible); | 133 bool is_audible); |
| 118 | 134 |
| 119 // Called when a renderer is destroyed. | 135 // Called when a renderer is destroyed. |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 168 enum ClientState { | 184 enum ClientState { |
| 169 // Observable client. | 185 // Observable client. |
| 170 ACTIVE, | 186 ACTIVE, |
| 171 // Non-observable client. | 187 // Non-observable client. |
| 172 BACKGROUND, | 188 BACKGROUND, |
| 173 // No client found. | 189 // No client found. |
| 174 UNKNOWN, | 190 UNKNOWN, |
| 175 }; | 191 }; |
| 176 | 192 |
| 177 class RequestQueue; | 193 class RequestQueue; |
| 178 class ScheduledResourceRequest; | 194 class ScheduledResourceRequestImpl; |
| 179 struct RequestPriorityParams; | 195 struct RequestPriorityParams; |
| 180 struct ScheduledResourceSorter { | 196 struct ScheduledResourceSorter { |
| 181 bool operator()(const ScheduledResourceRequest* a, | 197 bool operator()(const ScheduledResourceRequestImpl* a, |
| 182 const ScheduledResourceRequest* b) const; | 198 const ScheduledResourceRequestImpl* b) const; |
| 183 }; | 199 }; |
| 184 class Client; | 200 class Client; |
| 185 | 201 |
| 186 typedef int64 ClientId; | 202 typedef int64 ClientId; |
| 187 typedef std::map<ClientId, Client*> ClientMap; | 203 typedef std::map<ClientId, Client*> ClientMap; |
| 188 typedef std::set<ScheduledResourceRequest*> RequestSet; | 204 typedef std::set<ScheduledResourceRequestImpl*> RequestSet; |
| 189 | 205 |
| 190 // Called when a ScheduledResourceRequest is destroyed. | 206 // Called when a ScheduledResourceRequestImpl is destroyed. |
| 191 void RemoveRequest(ScheduledResourceRequest* request); | 207 void RemoveRequest(ScheduledResourceRequestImpl* request); |
| 192 | 208 |
| 193 // These calls may update the ThrottleState of all clients, and have the | 209 // These calls may update the ThrottleState of all clients, and have the |
| 194 // potential to be re-entrant. | 210 // potential to be re-entrant. |
| 195 // Called when a Client newly becomes active loading. | 211 // Called when a Client newly becomes active loading. |
| 196 void IncrementActiveClientsLoading(); | 212 void IncrementActiveClientsLoading(); |
| 197 // Called when an active and loading Client either completes loading or | 213 // Called when an active and loading Client either completes loading or |
| 198 // becomes inactive. | 214 // becomes inactive. |
| 199 void DecrementActiveClientsLoading(); | 215 void DecrementActiveClientsLoading(); |
| 200 | 216 |
| 201 void OnLoadingActiveClientsStateChangedForAllClients(); | 217 void OnLoadingActiveClientsStateChangedForAllClients(); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 214 // Returns UNKNOWN if the corresponding client is not found, else returns | 230 // Returns UNKNOWN if the corresponding client is not found, else returns |
| 215 // whether the client is ACTIVE (user-observable) or BACKGROUND. | 231 // whether the client is ACTIVE (user-observable) or BACKGROUND. |
| 216 ClientState GetClientState(ClientId client_id) const; | 232 ClientState GetClientState(ClientId client_id) const; |
| 217 | 233 |
| 218 // Update the queue position for |request|, possibly causing it to start | 234 // Update the queue position for |request|, possibly causing it to start |
| 219 // loading. | 235 // loading. |
| 220 // | 236 // |
| 221 // Queues are maintained for each priority level. When |request| is | 237 // Queues are maintained for each priority level. When |request| is |
| 222 // reprioritized, it will move to the end of the queue for that priority | 238 // reprioritized, it will move to the end of the queue for that priority |
| 223 // level. | 239 // level. |
| 224 void ReprioritizeRequest(ScheduledResourceRequest* request, | 240 void ReprioritizeRequest(ScheduledResourceRequestImpl* request, |
| 225 net::RequestPriority new_priority, | 241 net::RequestPriority new_priority, |
| 226 int intra_priority_value); | 242 int intra_priority_value); |
| 227 | 243 |
| 228 // Returns the client ID for the given |child_id| and |route_id| combo. | 244 // Returns the client ID for the given |child_id| and |route_id| combo. |
| 229 ClientId MakeClientId(int child_id, int route_id); | 245 ClientId MakeClientId(int child_id, int route_id); |
| 230 | 246 |
| 231 // Returns the client for the given |child_id| and |route_id| combo. | 247 // Returns the client for the given |child_id| and |route_id| combo. |
| 232 Client* GetClient(int child_id, int route_id); | 248 Client* GetClient(int child_id, int route_id); |
| 233 | 249 |
| 234 bool should_coalesce_; | 250 bool should_coalesce_; |
| 235 bool should_throttle_; | 251 bool should_throttle_; |
| 236 ClientMap client_map_; | 252 ClientMap client_map_; |
| 237 size_t active_clients_loading_; | 253 size_t active_clients_loading_; |
| 238 size_t coalesced_clients_; | 254 size_t coalesced_clients_; |
| 239 bool limit_outstanding_requests_; | 255 bool limit_outstanding_requests_; |
| 240 size_t outstanding_request_limit_; | 256 size_t outstanding_request_limit_; |
| 241 // This is a repeating timer to initiate requests on COALESCED Clients. | 257 // This is a repeating timer to initiate requests on COALESCED Clients. |
| 242 scoped_ptr<base::Timer> coalescing_timer_; | 258 scoped_ptr<base::Timer> coalescing_timer_; |
| 243 RequestSet unowned_requests_; | 259 RequestSet unowned_requests_; |
| 244 }; | 260 }; |
| 245 | 261 |
| 246 } // namespace content | 262 } // namespace content |
| 247 | 263 |
| 248 #endif // CONTENT_BROWSER_LOADER_RESOURCE_SCHEDULER_H_ | 264 #endif // CONTENT_BROWSER_LOADER_RESOURCE_SCHEDULER_H_ |
| OLD | NEW |