Chromium Code Reviews| Index: content/browser/renderer_host/resource_scheduler.cc |
| diff --git a/content/browser/renderer_host/resource_scheduler.cc b/content/browser/renderer_host/resource_scheduler.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..246e027811a61328b8a6826862114692cd9784b4 |
| --- /dev/null |
| +++ b/content/browser/renderer_host/resource_scheduler.cc |
| @@ -0,0 +1,127 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/browser/renderer_host/resource_scheduler.h" |
| + |
| +#include "base/stl_util.h" |
| +#include "content/browser/renderer_host/resource_loader.h" |
| +#include "net/base/request_priority.h" |
| +#include "net/url_request/url_request.h" |
| + |
| +namespace content { |
| + |
| +namespace { |
| + |
| +int64 MakeClientId(int child_id, int route_id) { |
| + return static_cast<int64>(child_id) << 32 | route_id; |
|
willchan no longer on Chromium
2012/10/25 19:44:49
I don't memorize the order of operations between <
James Simonsen
2012/11/17 02:56:15
Done.
|
| +} |
| + |
| +} // unnamed namespace |
| + |
| +ResourceScheduler::ResourceScheduler() { |
| +} |
| + |
| +ResourceScheduler::~ResourceScheduler() { |
| +} |
| + |
| +void ResourceScheduler::ScheduleLoad(int child_id, |
| + int route_id, |
| + int request_id, |
| + ResourceLoader* loader) { |
| + int64 client_id = MakeClientId(child_id, route_id); |
| + if (!ContainsKey(client_map_, client_id)) { |
|
willchan no longer on Chromium
2012/10/25 19:44:49
I don't understand this. If the client doesn't exi
James Simonsen
2012/11/17 02:56:15
Turns out these were the <a ping> requests. I adde
|
| + loader->StartRequest(); |
| + return; |
| + } |
| + |
| + Client* client = client_map_[client_id]; |
| + Request request = { request_id, loader }; |
| + if (loader->request()->priority() < net::MEDIUM && |
| + !client->in_flight_requests.empty() && !client->has_painted) { |
| + client->pending_requests.push_back(request); |
| + return; |
| + } |
| + |
| + StartRequest(request, client); |
| +} |
| + |
| +void ResourceScheduler::RemoveLoad(int child_id, int route_id, int request_id) { |
| + int64 client_id = MakeClientId(child_id, route_id); |
| + if (!ContainsKey(client_map_, client_id)) { |
|
willchan no longer on Chromium
2012/10/25 19:44:49
Why would this ever happen? Is this a bug? Should
James Simonsen
2012/11/17 02:56:15
When a tab goes away suddenly the OnDestroy() come
|
| + return; |
| + } |
| + |
| + Client* client = client_map_[client_id]; |
| + RequestIdSet::iterator it = client->in_flight_requests.find(request_id); |
| + if (it == client->in_flight_requests.end()) { |
| + bool removed = false; |
| + RequestQueue::iterator queue_it; |
| + for (queue_it = client->pending_requests.begin(); |
| + queue_it != client->pending_requests.end(); ++queue_it) { |
| + if (queue_it->request_id == request_id) { |
| + client->pending_requests.erase(queue_it); |
| + removed = true; |
| + break; |
| + } |
| + } |
| + DCHECK(removed); |
| + DCHECK(!ContainsKey(client->in_flight_requests, request_id)); |
| + } else { |
| + bool erased = client->in_flight_requests.erase(request_id); |
| + DCHECK(erased); |
| + } |
| + |
| + if (client->in_flight_requests.empty()) { |
| + LoadPendingRequests(client); |
| + } |
| +} |
| + |
| +void ResourceScheduler::OnCreate(int child_id, int route_id) { |
| + int64 client_id = MakeClientId(child_id, route_id); |
| + DCHECK(!ContainsKey(client_map_, client_id)); |
| + client_map_[client_id] = new Client; |
| +} |
| + |
| +void ResourceScheduler::OnNavigate(int child_id, int route_id) { |
| + int64 client_id = MakeClientId(child_id, route_id); |
| + DCHECK(ContainsKey(client_map_, client_id)); |
| + Client* client = client_map_[client_id]; |
| + client->has_painted = false; |
| +} |
| + |
| +void ResourceScheduler::OnPaint(int child_id, int route_id) { |
| + int64 client_id = MakeClientId(child_id, route_id); |
| + DCHECK(ContainsKey(client_map_, client_id)); |
| + Client* client = client_map_[client_id]; |
| + if (!client->has_painted) { |
| + client->has_painted = true; |
| + LoadPendingRequests(client); |
| + } |
| +} |
| + |
| +void ResourceScheduler::OnDestroy(int child_id, int route_id) { |
| + int64 client_id = MakeClientId(child_id, route_id); |
| + bool erased = client_map_.erase(client_id); |
| + DCHECK(erased); |
| +} |
| + |
| +void ResourceScheduler::StartRequest(Request request, Client* client) { |
| + client->in_flight_requests.insert(request.request_id); |
| + request.loader->StartRequest(); |
| +} |
| + |
| +void ResourceScheduler::LoadPendingRequests(Client* client) { |
| + RequestQueue::iterator it; |
| + for (it = client->pending_requests.begin(); |
| + it != client->pending_requests.end(); ++it) { |
| + StartRequest(*it, client); |
| + } |
| + client->pending_requests.clear(); |
| +} |
| + |
| +ResourceScheduler::Client::Client() |
| + : has_painted(false) { |
| +} |
| + |
| +} // namespace content |