OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "chrome/browser/predictors/resource_prefetcher.h" | 5 #include "chrome/browser/predictors/resource_prefetcher.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <iterator> | 8 #include <iterator> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 30 matching lines...) Expand all Loading... |
41 : url(url) {} | 41 : url(url) {} |
42 | 42 |
43 ResourcePrefetcher::PrefetcherStats::~PrefetcherStats() {} | 43 ResourcePrefetcher::PrefetcherStats::~PrefetcherStats() {} |
44 | 44 |
45 ResourcePrefetcher::PrefetcherStats::PrefetcherStats( | 45 ResourcePrefetcher::PrefetcherStats::PrefetcherStats( |
46 const PrefetcherStats& other) | 46 const PrefetcherStats& other) |
47 : url(other.url), | 47 : url(other.url), |
48 start_time(other.start_time), | 48 start_time(other.start_time), |
49 requests_stats(other.requests_stats) {} | 49 requests_stats(other.requests_stats) {} |
50 | 50 |
51 ResourcePrefetcher::ResourcePrefetcher( | 51 ResourcePrefetcher::ResourcePrefetcher(Delegate* delegate, |
52 Delegate* delegate, | 52 size_t max_concurrent_requests, |
53 const ResourcePrefetchPredictorConfig& config, | 53 size_t max_concurrent_requests_per_host, |
54 const GURL& main_frame_url, | 54 const GURL& main_frame_url, |
55 const std::vector<GURL>& urls) | 55 const std::vector<GURL>& urls) |
56 : state_(INITIALIZED), | 56 : state_(INITIALIZED), |
57 delegate_(delegate), | 57 delegate_(delegate), |
58 config_(config), | 58 max_concurrent_requests_(max_concurrent_requests), |
| 59 max_concurrent_requests_per_host_(max_concurrent_requests_per_host), |
59 main_frame_url_(main_frame_url), | 60 main_frame_url_(main_frame_url), |
60 prefetched_count_(0), | 61 prefetched_count_(0), |
61 prefetched_bytes_(0), | 62 prefetched_bytes_(0), |
62 stats_(base::MakeUnique<PrefetcherStats>(main_frame_url)) { | 63 stats_(base::MakeUnique<PrefetcherStats>(main_frame_url)) { |
63 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 64 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
64 | 65 |
65 std::copy(urls.begin(), urls.end(), std::back_inserter(request_queue_)); | 66 std::copy(urls.begin(), urls.end(), std::back_inserter(request_queue_)); |
66 } | 67 } |
67 | 68 |
68 ResourcePrefetcher::~ResourcePrefetcher() {} | 69 ResourcePrefetcher::~ResourcePrefetcher() {} |
(...skipping 23 matching lines...) Expand all Loading... |
92 CHECK(state_ == RUNNING || state_ == STOPPED); | 93 CHECK(state_ == RUNNING || state_ == STOPPED); |
93 | 94 |
94 // Try to launch new requests if the state is RUNNING. | 95 // Try to launch new requests if the state is RUNNING. |
95 if (state_ == RUNNING) { | 96 if (state_ == RUNNING) { |
96 bool request_available = true; | 97 bool request_available = true; |
97 | 98 |
98 // Loop through the requests while we are under the | 99 // Loop through the requests while we are under the |
99 // max_prefetches_inflight_per_host_per_navigation limit, looking for a URL | 100 // max_prefetches_inflight_per_host_per_navigation limit, looking for a URL |
100 // for which the max_prefetches_inflight_per_host_per_navigation limit has | 101 // for which the max_prefetches_inflight_per_host_per_navigation limit has |
101 // not been reached. Try to launch as many requests as possible. | 102 // not been reached. Try to launch as many requests as possible. |
102 while ((inflight_requests_.size() < | 103 while ((inflight_requests_.size() < max_concurrent_requests_) && |
103 config_.max_prefetches_inflight_per_navigation) && | |
104 request_available) { | 104 request_available) { |
105 auto request_it = request_queue_.begin(); | 105 auto request_it = request_queue_.begin(); |
106 for (; request_it != request_queue_.end(); ++request_it) { | 106 for (; request_it != request_queue_.end(); ++request_it) { |
107 const std::string& host = request_it->host(); | 107 const std::string& host = request_it->host(); |
108 | 108 |
109 std::map<std::string, size_t>::iterator host_it = | 109 std::map<std::string, size_t>::iterator host_it = |
110 host_inflight_counts_.find(host); | 110 host_inflight_counts_.find(host); |
111 if (host_it == host_inflight_counts_.end() || | 111 if (host_it == host_inflight_counts_.end() || |
112 host_it->second < | 112 host_it->second < max_concurrent_requests_per_host_) |
113 config_.max_prefetches_inflight_per_host_per_navigation) | |
114 break; | 113 break; |
115 } | 114 } |
116 request_available = request_it != request_queue_.end(); | 115 request_available = request_it != request_queue_.end(); |
117 | 116 |
118 if (request_available) { | 117 if (request_available) { |
119 SendRequest(*request_it); | 118 SendRequest(*request_it); |
120 request_queue_.erase(request_it); | 119 request_queue_.erase(request_it); |
121 } | 120 } |
122 } | 121 } |
123 } | 122 } |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 if (bytes_read <= 0) { | 255 if (bytes_read <= 0) { |
257 FinishRequest(request); | 256 FinishRequest(request); |
258 return; | 257 return; |
259 } | 258 } |
260 | 259 |
261 if (bytes_read > 0) | 260 if (bytes_read > 0) |
262 ReadFullResponse(request); | 261 ReadFullResponse(request); |
263 } | 262 } |
264 | 263 |
265 } // namespace predictors | 264 } // namespace predictors |
OLD | NEW |