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_RENDERER_HOST_RESOURCE_SCHEDULER_H_ | |
| 6 #define CONTENT_BROWSER_RENDERER_HOST_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/memory/linked_ptr.h" | |
| 15 #include "content/common/content_export.h" | |
| 16 #include "content/public/browser/global_request_id.h" | |
| 17 | |
| 18 namespace content { | |
| 19 class ResourceLoader; | |
| 20 | |
| 21 // There is one ResourceScheduler, which is owned by the ResourceDispatcherHost. | |
| 22 // | |
| 23 // These are sources of input to the scheduler: | |
| 24 // 1. Requests to load, cancel, or finish resources from the | |
| 25 // ResourceDispatcherHost. | |
| 26 // 2. Notifications of renderer events from the ResourceSchedulerFilter. | |
| 27 // | |
| 28 // The ResourceScheduler tracks many Clients, which should correlate with tabs. | |
| 29 // A client is uniquely identified by its child_id and route_id. | |
| 30 // | |
| 31 // Each Client may have many Requests in flight. Requests are uniquely | |
| 32 // identified within a Client by their request_ids. | |
| 33 // | |
| 34 // The ResourceDispatcherHost should call ScheduleLoad() when it's ready to | |
| 35 // load a request. The returned LoadHandle should be destroyed when the load | |
| 36 // finishes or is canceled. | |
| 37 // | |
| 38 // The scheduling behavior mimicks WebKit's scheduler, which blocks fetching | |
| 39 // low priority resources until the first paint has occurred. One exception is | |
| 40 // if the client is not fetching anything else, low priority fetching begins | |
| 41 // immediately. | |
| 42 class CONTENT_EXPORT_PRIVATE ResourceScheduler { | |
| 43 public: | |
| 44 typedef int64 ClientId; | |
| 45 | |
| 46 // A LoadHandle is returned to the ResourceDispatcherHost. When the host | |
| 47 // deletes the handle, it's automatically removed from ResourceScheduler's | |
| 48 // state. | |
| 49 class LoadHandle { | |
| 50 public: | |
| 51 virtual ~LoadHandle() {} | |
| 52 virtual linked_ptr<ResourceLoader>& loader() = 0; | |
| 53 }; | |
| 54 | |
| 55 ResourceScheduler(); | |
| 56 ~ResourceScheduler(); | |
| 57 | |
| 58 // Called when the ResourceDispatcherHost is ready to load a resource. | |
| 59 // Caller should delete the returned handle when the load completes or is | |
| 60 // canceled. | |
| 61 LoadHandle* ScheduleLoad(int child_id, | |
|
willchan no longer on Chromium
2012/11/21 09:04:30
Perhaps return a scoped_ptr<LoadHandle> to convey
James Simonsen
2012/11/27 02:20:51
Done.
| |
| 62 int route_id, | |
| 63 int request_id, | |
| 64 const linked_ptr<ResourceLoader>& loader); | |
| 65 | |
| 66 // These events are triggered by ViewHostMsgs. | |
| 67 | |
| 68 // Called when a new client is created. | |
| 69 void OnCreate(int child_id, int route_id); | |
| 70 | |
| 71 // Called when a client navigates to a new main document. | |
| 72 void OnNavigate(int child_id, int route_id); | |
| 73 | |
| 74 // Called any time a client paints. | |
| 75 void OnPaint(int child_id, int route_id); | |
| 76 | |
| 77 // Called when a client is destroyed. | |
| 78 void OnDestroy(int child_id, int route_id); | |
| 79 | |
| 80 private: | |
| 81 friend class ScheduledLoadHandle; | |
| 82 struct Client; | |
| 83 struct Request; | |
| 84 | |
| 85 typedef std::vector<Request> RequestQueue; | |
| 86 typedef std::set<int> RequestIdSet; | |
| 87 typedef std::map<ClientId, Client*> ClientMap; | |
| 88 | |
| 89 struct Request { | |
| 90 int request_id; | |
| 91 ResourceLoader* loader; | |
| 92 }; | |
| 93 | |
| 94 struct Client { | |
| 95 Client(); | |
| 96 | |
| 97 bool has_painted; | |
| 98 bool closed; | |
| 99 RequestQueue pending_requests; | |
| 100 RequestIdSet in_flight_requests; | |
| 101 }; | |
| 102 | |
| 103 // A load that must be managed by the ResourceScheduler. | |
| 104 class ScheduledLoadHandle : public LoadHandle { | |
|
willchan no longer on Chromium
2012/11/21 09:04:30
Move these subclasses to the .cc file to keep the
James Simonsen
2012/11/27 02:20:51
Done.
| |
| 105 public: | |
| 106 ScheduledLoadHandle(ClientId client_id, | |
| 107 int request_id, | |
| 108 const linked_ptr<ResourceLoader>& loader, | |
| 109 ResourceScheduler* scheduler); | |
| 110 virtual ~ScheduledLoadHandle(); | |
| 111 | |
| 112 virtual linked_ptr<ResourceLoader>& loader() OVERRIDE { return loader_; } | |
|
willchan no longer on Chromium
2012/11/21 09:04:30
no returning non-const references. probably best j
James Simonsen
2012/11/27 02:20:51
Done.
| |
| 113 | |
| 114 private: | |
| 115 ClientId client_id_; | |
| 116 int request_id_; | |
| 117 linked_ptr<ResourceLoader> loader_; | |
| 118 ResourceScheduler* scheduler_; | |
| 119 }; | |
| 120 | |
| 121 // A load that fires immediately and is ignored by the ResourceScheduler. | |
|
willchan no longer on Chromium
2012/11/21 09:04:30
I think this class shouldn't exist and we should j
James Simonsen
2012/11/27 02:20:51
Good point. I prefer to handle the "background" re
| |
| 122 class UnscheduledLoadHandle : public LoadHandle { | |
| 123 public: | |
| 124 UnscheduledLoadHandle(const linked_ptr<ResourceLoader>& loader); | |
| 125 virtual ~UnscheduledLoadHandle(); | |
| 126 | |
| 127 virtual linked_ptr<ResourceLoader>& loader() OVERRIDE { return loader_; } | |
| 128 | |
| 129 private: | |
| 130 linked_ptr<ResourceLoader> loader_; | |
| 131 }; | |
| 132 | |
| 133 // Called when a ScheduledLoadHandle is destroyed. | |
| 134 void RemoveLoad(ClientId client_id, int request_id); | |
| 135 | |
| 136 // Kicks off the loader for |request| and adds it to |client|. | |
| 137 void StartRequest(Request request, Client* client); | |
| 138 | |
| 139 // Calls StartRequest on all pending requests for |client|. | |
| 140 void LoadPendingRequests(Client* client); | |
| 141 | |
| 142 ClientMap client_map_; | |
| 143 }; | |
| 144 | |
| 145 } // namespace content | |
| 146 | |
| 147 #endif // CONTENT_BROWSER_RENDERER_HOST_RESOURCE_SCHEDULER_H_ | |
| OLD | NEW |