| 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 "content/common/content_export.h" | 16 #include "content/common/content_export.h" |
| 16 #include "net/base/priority_queue.h" | 17 #include "net/base/priority_queue.h" |
| 17 #include "net/base/request_priority.h" | 18 #include "net/base/request_priority.h" |
| 18 | 19 |
| 19 namespace net { | 20 namespace net { |
| 20 class HostPortPair; | 21 class HostPortPair; |
| 21 class URLRequest; | 22 class URLRequest; |
| 22 } | 23 } |
| 23 | 24 |
| 24 namespace content { | 25 namespace content { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 46 // Users should call ScheduleRequest() to notify this ResourceScheduler of a | 47 // Users should call ScheduleRequest() to notify this ResourceScheduler of a |
| 47 // new request. The returned ResourceThrottle should be destroyed when the load | 48 // new request. The returned ResourceThrottle should be destroyed when the load |
| 48 // finishes or is canceled. | 49 // finishes or is canceled. |
| 49 // | 50 // |
| 50 // The scheduler may defer issuing the request via the ResourceThrottle | 51 // 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 | 52 // interface or it may alter the request's priority by calling set_priority() on |
| 52 // the URLRequest. | 53 // the URLRequest. |
| 53 class CONTENT_EXPORT ResourceScheduler : public base::NonThreadSafe { | 54 class CONTENT_EXPORT ResourceScheduler : public base::NonThreadSafe { |
| 54 public: | 55 public: |
| 55 enum ClientThrottleState { | 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. |
| 63 |
| 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. |
| 67 PAUSED, |
| 56 // Loaded background client, all observable clients loaded. | 68 // Loaded background client, all observable clients loaded. |
| 57 COALESCED, | 69 COALESCED, |
| 58 // Background client, an observable client is loading. | 70 // Background client, an observable client is loading. |
| 59 THROTTLED, | 71 THROTTLED, |
| 60 // Observable (active) loaded client or | 72 // Observable (active) loaded client or |
| 61 // Loading background client, all observable clients loaded. | 73 // Loading background client, all observable clients loaded. |
| 62 // Note that clients which would be COALESCED are UNTHROTTLED until | 74 // Note that clients which would be COALESCED are UNTHROTTLED until |
| 63 // coalescing is turned on. | 75 // coalescing is turned on. |
| 64 UNTHROTTLED, | 76 UNTHROTTLED, |
| 65 // Observable (active) loading client. | 77 // Observable (active) loading client. |
| 66 ACTIVE_AND_LOADING, | 78 ACTIVE_AND_LOADING, |
| 67 }; | 79 }; |
| 68 | 80 |
| 69 ResourceScheduler(); | 81 ResourceScheduler(); |
| 70 ~ResourceScheduler(); | 82 ~ResourceScheduler(); |
| 71 | 83 |
| 84 // Use a mock timer when testing. |
| 85 void set_timer_for_testing(scoped_ptr<base::Timer> timer) { |
| 86 coalescing_timer_.reset(timer.release()); |
| 87 } |
| 88 |
| 72 // TODO(aiolos): Remove when throttling and coalescing have landed | 89 // TODO(aiolos): Remove when throttling and coalescing have landed |
| 73 void SetThrottleOptionsForTesting(bool should_throttle, bool should_coalesce); | 90 void SetThrottleOptionsForTesting(bool should_throttle, bool should_coalesce); |
| 74 | 91 |
| 75 bool should_coalesce() const { return should_coalesce_; } | 92 bool should_coalesce() const { return should_coalesce_; } |
| 76 bool should_throttle() const { return should_throttle_; } | 93 bool should_throttle() const { return should_throttle_; } |
| 77 | 94 |
| 78 ClientThrottleState GetClientStateForTesting(int child_id, int route_id); | 95 ClientThrottleState GetClientStateForTesting(int child_id, int route_id); |
| 79 | 96 |
| 80 // Requests that this ResourceScheduler schedule, and eventually loads, the | 97 // Requests that this ResourceScheduler schedule, and eventually loads, the |
| 81 // specified |url_request|. Caller should delete the returned ResourceThrottle | 98 // specified |url_request|. Caller should delete the returned ResourceThrottle |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 }; | 146 }; |
| 130 class Client; | 147 class Client; |
| 131 | 148 |
| 132 typedef int64 ClientId; | 149 typedef int64 ClientId; |
| 133 typedef std::map<ClientId, Client*> ClientMap; | 150 typedef std::map<ClientId, Client*> ClientMap; |
| 134 typedef std::set<ScheduledResourceRequest*> RequestSet; | 151 typedef std::set<ScheduledResourceRequest*> RequestSet; |
| 135 | 152 |
| 136 // Called when a ScheduledResourceRequest is destroyed. | 153 // Called when a ScheduledResourceRequest is destroyed. |
| 137 void RemoveRequest(ScheduledResourceRequest* request); | 154 void RemoveRequest(ScheduledResourceRequest* request); |
| 138 | 155 |
| 139 // These Calls may update the ThrottleState of all clients, and have the | 156 // These calls may update the ThrottleState of all clients, and have the |
| 140 // potential to be re-entarant. | 157 // potential to be re-entrant. |
| 141 // Called when a Client newly becomes active loading. | 158 // Called when a Client newly becomes active loading. |
| 159 void IncrementActiveClientsLoading(); |
| 160 // Called when an active and loading Client either completes loading or |
| 161 // becomes inactive. |
| 142 void DecrementActiveClientsLoading(); | 162 void DecrementActiveClientsLoading(); |
| 143 // Caled when a Client stops being active loading. | |
| 144 void IncrementActiveClientsLoading(); | |
| 145 | 163 |
| 146 void OnLoadingActiveClientsStateChanged(); | 164 void OnLoadingActiveClientsStateChangedForAllClients(); |
| 147 | 165 |
| 148 size_t CountActiveClientsLoading(); | 166 size_t CountActiveClientsLoading() const; |
| 167 |
| 168 // Called when a Client becomes coalesced. |
| 169 void IncrementCoalescedClients(); |
| 170 // Called when a client stops being coalesced. |
| 171 void DecrementCoalescedClients(); |
| 172 |
| 173 void LoadCoalescedRequests(); |
| 174 |
| 175 size_t CountCoalescedClients() const; |
| 149 | 176 |
| 150 // Update the queue position for |request|, possibly causing it to start | 177 // Update the queue position for |request|, possibly causing it to start |
| 151 // loading. | 178 // loading. |
| 152 // | 179 // |
| 153 // Queues are maintained for each priority level. When |request| is | 180 // Queues are maintained for each priority level. When |request| is |
| 154 // reprioritized, it will move to the end of the queue for that priority | 181 // reprioritized, it will move to the end of the queue for that priority |
| 155 // level. | 182 // level. |
| 156 void ReprioritizeRequest(ScheduledResourceRequest* request, | 183 void ReprioritizeRequest(ScheduledResourceRequest* request, |
| 157 net::RequestPriority new_priority, | 184 net::RequestPriority new_priority, |
| 158 int intra_priority_value); | 185 int intra_priority_value); |
| 159 | 186 |
| 160 // Returns the client ID for the given |child_id| and |route_id| combo. | 187 // Returns the client ID for the given |child_id| and |route_id| combo. |
| 161 ClientId MakeClientId(int child_id, int route_id); | 188 ClientId MakeClientId(int child_id, int route_id); |
| 162 | 189 |
| 163 // Returns the client for the given |child_id| and |route_id| combo. | 190 // Returns the client for the given |child_id| and |route_id| combo. |
| 164 Client* GetClient(int child_id, int route_id); | 191 Client* GetClient(int child_id, int route_id); |
| 165 | 192 |
| 166 bool should_coalesce_; | 193 bool should_coalesce_; |
| 167 bool should_throttle_; | 194 bool should_throttle_; |
| 168 ClientMap client_map_; | 195 ClientMap client_map_; |
| 169 size_t active_clients_loading_; | 196 size_t active_clients_loading_; |
| 197 size_t coalesced_clients_; |
| 198 // This is a repeating timer to initiate requests on COALESCED Clients. |
| 199 scoped_ptr<base::Timer> coalescing_timer_; |
| 170 RequestSet unowned_requests_; | 200 RequestSet unowned_requests_; |
| 171 }; | 201 }; |
| 172 | 202 |
| 173 } // namespace content | 203 } // namespace content |
| 174 | 204 |
| 175 #endif // CONTENT_BROWSER_LOADER_RESOURCE_SCHEDULER_H_ | 205 #endif // CONTENT_BROWSER_LOADER_RESOURCE_SCHEDULER_H_ |
| OLD | NEW |