Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CONTENT_BROWSER_LOADER_RESOURCE_SCHEDULER_H_ | |
| 6 #define CONTENT_BROWSER_LOADER_RESOURCE_SCHEDULER_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <set> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/basictypes.h" | |
| 13 #include "base/compiler_specific.h" | |
| 14 #include "base/containers/mru_cache.h" | |
| 15 #include "base/memory/linked_ptr.h" | |
| 16 #include "base/memory/scoped_ptr.h" | |
| 17 #include "base/threading/non_thread_safe.h" | |
| 18 #include "content/common/content_export.h" | |
| 19 #include "content/public/browser/global_request_id.h" | |
| 20 | |
| 21 namespace net { | |
| 22 class URLRequest; | |
| 23 } | |
| 24 | |
| 25 namespace content { | |
| 26 class ResourceThrottle; | |
| 27 | |
| 28 // There is one ResourceScheduler. All renderer-initiated HTTP requests are | |
| 29 // expected to pass through it. | |
| 30 // | |
| 31 // There are two types of input to the scheduler: | |
| 32 // 1. Requests to start, cancel, or finish fetching a resource. | |
| 33 // 2. Notifications for renderer events, such as navigation and painting. | |
| 34 // | |
| 35 // The ResourceScheduler tracks many Clients, which should correlate with tabs. | |
| 36 // A client is uniquely identified by its child_id and route_id. | |
| 37 // | |
| 38 // Each Client may have many Requests in flight. Requests are uniquely | |
| 39 // identified within a Client by its ScheduledResourceRequest. | |
| 40 // | |
| 41 // Users should call ScheduleRequest() to notify this ResourceScheduler of a | |
| 42 // new request. The returned ResourceThrottle should be destroyed when the load | |
| 43 // finishes or is canceled. | |
| 44 // | |
| 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 | |
| 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 { | |
| 53 public: | |
| 54 ResourceScheduler(); | |
| 55 ~ResourceScheduler(); | |
| 56 | |
| 57 // Requests that this ResourceScheduler schedule, and eventually loads, the | |
| 58 // specified |url_request|. Caller should delete the returned ResourceThrottle | |
| 59 // when the load completes or is canceled. | |
| 60 scoped_ptr<ResourceThrottle> ScheduleRequest( | |
| 61 int child_id, int route_id, net::URLRequest* url_request); | |
| 62 | |
| 63 // Called when a client navigates to a new main document. | |
| 64 void OnNavigate(int child_id, int route_id); | |
| 65 | |
| 66 // Called when the client has parsed the <body> element. This is a signal that | |
| 67 // resource loads won't interfere with first paint. | |
| 68 void OnWillInsertBody(int child_id, int route_id); | |
| 69 | |
| 70 private: | |
| 71 class ScheduledResourceRequest; | |
| 72 friend class ScheduledResourceRequest; | |
| 73 struct Client; | |
| 74 | |
| 75 typedef int64 ClientId; | |
| 76 typedef base::OwningMRUCache<ClientId, Client*> ClientMap; | |
| 77 typedef std::vector<ScheduledResourceRequest*> RequestQueue; | |
| 78 typedef std::set<ScheduledResourceRequest*> RequestSet; | |
| 79 | |
| 80 struct Client { | |
|
darin (slow to review)
2013/02/19 06:58:33
nit: it looks like you could get away with definin
James Simonsen
2013/02/19 23:08:58
Done.
| |
| 81 Client(ResourceScheduler* scheduler); | |
| 82 ~Client(); | |
| 83 | |
| 84 bool has_body; | |
| 85 RequestQueue pending_requests; | |
| 86 RequestSet in_flight_requests; | |
| 87 | |
| 88 private: | |
| 89 ResourceScheduler* scheduler_; | |
| 90 }; | |
| 91 | |
| 92 // Called when a ScheduledResourceRequest is destroyed. | |
| 93 void RemoveRequest(ScheduledResourceRequest* request); | |
| 94 | |
| 95 // Unthrottles the |request| and adds it to |client|. | |
| 96 void StartRequest(ScheduledResourceRequest* request, Client* client); | |
| 97 | |
| 98 // Calls StartRequest on all pending requests for |client|. | |
| 99 void LoadPendingRequests(Client* client); | |
| 100 | |
| 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. | |
| 105 ClientId MakeClientId(int child_id, int route_id); | |
| 106 | |
| 107 ClientMap client_map_; | |
| 108 RequestSet unowned_requests_; | |
| 109 }; | |
| 110 | |
| 111 } // namespace content | |
| 112 | |
| 113 #endif // CONTENT_BROWSER_LOADER_RESOURCE_SCHEDULER_H_ | |
| OLD | NEW |