| 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> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 14 #include "base/containers/mru_cache.h" | |
| 15 #include "base/memory/linked_ptr.h" | 14 #include "base/memory/linked_ptr.h" |
| 16 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/threading/non_thread_safe.h" | 16 #include "base/threading/non_thread_safe.h" |
| 18 #include "content/common/content_export.h" | 17 #include "content/common/content_export.h" |
| 19 #include "content/public/browser/global_request_id.h" | 18 #include "content/public/browser/global_request_id.h" |
| 20 | 19 |
| 21 namespace net { | 20 namespace net { |
| 22 class URLRequest; | 21 class URLRequest; |
| 23 } | 22 } |
| 24 | 23 |
| 25 namespace content { | 24 namespace content { |
| 26 class ResourceThrottle; | 25 class ResourceThrottle; |
| 27 | 26 |
| 28 // There is one ResourceScheduler. All renderer-initiated HTTP requests are | 27 // There is one ResourceScheduler. All renderer-initiated HTTP requests are |
| 29 // expected to pass through it. | 28 // expected to pass through it. |
| 30 // | 29 // |
| 31 // There are two types of input to the scheduler: | 30 // There are two types of input to the scheduler: |
| 32 // 1. Requests to start, cancel, or finish fetching a resource. | 31 // 1. Requests to start, cancel, or finish fetching a resource. |
| 33 // 2. Notifications for renderer events, such as navigation and painting. | 32 // 2. Notifications for renderer events, such as new tabs, navigation and |
| 33 // painting. |
| 34 // | 34 // |
| 35 // The ResourceScheduler tracks many Clients, which should correlate with tabs. | 35 // The ResourceScheduler tracks many Clients, which should correlate with tabs. |
| 36 // A client is uniquely identified by its child_id and route_id. | 36 // A client is uniquely identified by its child_id and route_id. |
| 37 // | 37 // |
| 38 // Each Client may have many Requests in flight. Requests are uniquely | 38 // Each Client may have many Requests in flight. Requests are uniquely |
| 39 // identified within a Client by its ScheduledResourceRequest. | 39 // identified within a Client by its ScheduledResourceRequest. |
| 40 // | 40 // |
| 41 // Users should call ScheduleRequest() to notify this ResourceScheduler of a | 41 // Users should call ScheduleRequest() to notify this ResourceScheduler of a |
| 42 // new request. The returned ResourceThrottle should be destroyed when the load | 42 // new request. The returned ResourceThrottle should be destroyed when the load |
| 43 // finishes or is canceled. | 43 // finishes or is canceled. |
| 44 // | 44 // |
| 45 // The scheduler may defer issuing the request via the ResourceThrottle | 45 // 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 | 46 // interface or it may alter the request's priority by calling set_priority() on |
| 47 // the URLRequest. | 47 // the URLRequest. |
| 48 // | |
| 49 // The scheduler only tracks the most recently used Clients. If a tab hasn't | |
| 50 // navigated or fetched a resource for some time, its state may be forgotten | |
| 51 // until its next navigation. In such situations, no request throttling occurs. | |
| 52 class CONTENT_EXPORT ResourceScheduler : public base::NonThreadSafe { | 48 class CONTENT_EXPORT ResourceScheduler : public base::NonThreadSafe { |
| 53 public: | 49 public: |
| 54 ResourceScheduler(); | 50 ResourceScheduler(); |
| 55 ~ResourceScheduler(); | 51 ~ResourceScheduler(); |
| 56 | 52 |
| 57 // Requests that this ResourceScheduler schedule, and eventually loads, the | 53 // Requests that this ResourceScheduler schedule, and eventually loads, the |
| 58 // specified |url_request|. Caller should delete the returned ResourceThrottle | 54 // specified |url_request|. Caller should delete the returned ResourceThrottle |
| 59 // when the load completes or is canceled. | 55 // when the load completes or is canceled. |
| 60 scoped_ptr<ResourceThrottle> ScheduleRequest( | 56 scoped_ptr<ResourceThrottle> ScheduleRequest( |
| 61 int child_id, int route_id, net::URLRequest* url_request); | 57 int child_id, int route_id, net::URLRequest* url_request); |
| 62 | 58 |
| 59 // Called when a renderer is created. |
| 60 void OnClientCreated(int child_id, int route_id); |
| 61 |
| 62 // Called when a renderer is destroyed. |
| 63 void OnClientDeleted(int child_id, int route_id); |
| 64 |
| 63 // Called when a client navigates to a new main document. | 65 // Called when a client navigates to a new main document. |
| 64 void OnNavigate(int child_id, int route_id); | 66 void OnNavigate(int child_id, int route_id); |
| 65 | 67 |
| 66 // Called when the client has parsed the <body> element. This is a signal that | 68 // Called when the client has parsed the <body> element. This is a signal that |
| 67 // resource loads won't interfere with first paint. | 69 // resource loads won't interfere with first paint. |
| 68 void OnWillInsertBody(int child_id, int route_id); | 70 void OnWillInsertBody(int child_id, int route_id); |
| 69 | 71 |
| 70 private: | 72 private: |
| 71 class ScheduledResourceRequest; | 73 class ScheduledResourceRequest; |
| 72 friend class ScheduledResourceRequest; | 74 friend class ScheduledResourceRequest; |
| 73 struct Client; | 75 struct Client; |
| 74 | 76 |
| 75 typedef int64 ClientId; | 77 typedef int64 ClientId; |
| 76 typedef base::OwningMRUCache<ClientId, Client*> ClientMap; | 78 typedef std::map<ClientId, Client*> ClientMap; |
| 77 typedef std::vector<ScheduledResourceRequest*> RequestQueue; | 79 typedef std::vector<ScheduledResourceRequest*> RequestQueue; |
| 78 typedef std::set<ScheduledResourceRequest*> RequestSet; | 80 typedef std::set<ScheduledResourceRequest*> RequestSet; |
| 79 | 81 |
| 80 struct Client { | 82 struct Client { |
| 81 Client(ResourceScheduler* scheduler); | 83 Client(); |
| 82 ~Client(); | 84 ~Client(); |
| 83 | 85 |
| 84 bool has_body; | 86 bool has_body; |
| 85 RequestQueue pending_requests; | 87 RequestQueue pending_requests; |
| 86 RequestSet in_flight_requests; | 88 RequestSet in_flight_requests; |
| 87 | |
| 88 private: | |
| 89 ResourceScheduler* scheduler_; | |
| 90 }; | 89 }; |
| 91 | 90 |
| 92 // Called when a ScheduledResourceRequest is destroyed. | 91 // Called when a ScheduledResourceRequest is destroyed. |
| 93 void RemoveRequest(ScheduledResourceRequest* request); | 92 void RemoveRequest(ScheduledResourceRequest* request); |
| 94 | 93 |
| 95 // Unthrottles the |request| and adds it to |client|. | 94 // Unthrottles the |request| and adds it to |client|. |
| 96 void StartRequest(ScheduledResourceRequest* request, Client* client); | 95 void StartRequest(ScheduledResourceRequest* request, Client* client); |
| 97 | 96 |
| 98 // Calls StartRequest on all pending requests for |client|. | 97 // Calls StartRequest on all pending requests for |client|. |
| 99 void LoadPendingRequests(Client* client); | 98 void LoadPendingRequests(Client* client); |
| 100 | 99 |
| 101 // Called when a Client is evicted from the MRUCache. | |
| 102 void RemoveClient(Client* client); | |
| 103 | |
| 104 // Returns the client ID for the given |child_id| and |route_id| combo. | 100 // Returns the client ID for the given |child_id| and |route_id| combo. |
| 105 ClientId MakeClientId(int child_id, int route_id); | 101 ClientId MakeClientId(int child_id, int route_id); |
| 106 | 102 |
| 107 ClientMap client_map_; | 103 ClientMap client_map_; |
| 108 RequestSet unowned_requests_; | 104 RequestSet unowned_requests_; |
| 109 }; | 105 }; |
| 110 | 106 |
| 111 } // namespace content | 107 } // namespace content |
| 112 | 108 |
| 113 #endif // CONTENT_BROWSER_LOADER_RESOURCE_SCHEDULER_H_ | 109 #endif // CONTENT_BROWSER_LOADER_RESOURCE_SCHEDULER_H_ |
| OLD | NEW |