Chromium Code Reviews| 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 42 : url(url) {} | 42 : url(url) {} |
| 43 | 43 |
| 44 ResourcePrefetcher::PrefetcherStats::~PrefetcherStats() {} | 44 ResourcePrefetcher::PrefetcherStats::~PrefetcherStats() {} |
| 45 | 45 |
| 46 ResourcePrefetcher::PrefetcherStats::PrefetcherStats( | 46 ResourcePrefetcher::PrefetcherStats::PrefetcherStats( |
| 47 const PrefetcherStats& other) | 47 const PrefetcherStats& other) |
| 48 : url(other.url), | 48 : url(other.url), |
| 49 start_time(other.start_time), | 49 start_time(other.start_time), |
| 50 requests_stats(other.requests_stats) {} | 50 requests_stats(other.requests_stats) {} |
| 51 | 51 |
| 52 ResourcePrefetcher::ResourcePrefetcher(Delegate* delegate, | 52 ResourcePrefetcher::ResourcePrefetcher( |
| 53 size_t max_concurrent_requests, | 53 base::WeakPtr<Delegate> delegate, |
| 54 size_t max_concurrent_requests_per_host, | 54 scoped_refptr<net::URLRequestContextGetter> context_getter, |
| 55 const GURL& main_frame_url, | 55 size_t max_concurrent_requests, |
| 56 const std::vector<GURL>& urls) | 56 size_t max_concurrent_requests_per_host, |
| 57 const GURL& main_frame_url, | |
| 58 const std::vector<GURL>& urls) | |
| 57 : state_(INITIALIZED), | 59 : state_(INITIALIZED), |
| 58 delegate_(delegate), | 60 delegate_(delegate), |
| 61 context_getter_(context_getter), | |
| 59 max_concurrent_requests_(max_concurrent_requests), | 62 max_concurrent_requests_(max_concurrent_requests), |
| 60 max_concurrent_requests_per_host_(max_concurrent_requests_per_host), | 63 max_concurrent_requests_per_host_(max_concurrent_requests_per_host), |
| 61 main_frame_url_(main_frame_url), | 64 main_frame_url_(main_frame_url), |
| 62 prefetched_count_(0), | 65 prefetched_count_(0), |
| 63 prefetched_bytes_(0), | 66 prefetched_bytes_(0), |
| 67 request_queue_(urls.begin(), urls.end()), | |
|
alexilin
2017/07/03 15:57:14
std::move won't really help if we're copying vecto
Benoit L
2017/07/03 16:19:28
Ah, indeed, dropping the move and keeping the cons
| |
| 64 stats_(base::MakeUnique<PrefetcherStats>(main_frame_url)) { | 68 stats_(base::MakeUnique<PrefetcherStats>(main_frame_url)) { |
| 65 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 69 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 66 | |
| 67 std::copy(urls.begin(), urls.end(), std::back_inserter(request_queue_)); | |
| 68 } | 70 } |
| 69 | 71 |
| 70 ResourcePrefetcher::~ResourcePrefetcher() {} | 72 ResourcePrefetcher::~ResourcePrefetcher() { |
| 73 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 74 } | |
| 71 | 75 |
| 72 void ResourcePrefetcher::Start() { | 76 void ResourcePrefetcher::Start() { |
| 73 TRACE_EVENT_ASYNC_BEGIN0("browser", "ResourcePrefetcher::Prefetch", this); | 77 TRACE_EVENT_ASYNC_BEGIN0("browser", "ResourcePrefetcher::Prefetch", this); |
| 74 DCHECK(thread_checker_.CalledOnValidThread()); | 78 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 75 | 79 |
| 76 CHECK_EQ(state_, INITIALIZED); | 80 CHECK_EQ(state_, INITIALIZED); |
| 77 state_ = RUNNING; | 81 state_ = RUNNING; |
| 78 | 82 |
| 79 stats_->start_time = base::TimeTicks::Now(); | 83 stats_->start_time = base::TimeTicks::Now(); |
| 80 TryToLaunchPrefetchRequests(); | 84 TryToLaunchPrefetchRequests(); |
| 81 } | 85 } |
| 82 | 86 |
| 83 void ResourcePrefetcher::Stop() { | 87 void ResourcePrefetcher::Stop() { |
| 84 TRACE_EVENT_ASYNC_END0("browser", "ResourcePrefetcher::Prefetch", this); | 88 TRACE_EVENT_ASYNC_END0("browser", "ResourcePrefetcher::Prefetch", this); |
| 85 DCHECK(thread_checker_.CalledOnValidThread()); | 89 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 86 | 90 |
| 87 if (state_ == FINISHED) | 91 if (state_ == FINISHED) |
| 88 return; | 92 return; |
| 89 | 93 |
| 90 state_ = STOPPED; | 94 state_ = STOPPED; |
| 91 } | 95 } |
| 92 | 96 |
| 93 void ResourcePrefetcher::TryToLaunchPrefetchRequests() { | 97 void ResourcePrefetcher::TryToLaunchPrefetchRequests() { |
| 94 CHECK(state_ == RUNNING || state_ == STOPPED); | 98 CHECK(state_ == RUNNING || state_ == STOPPED); |
| 95 | 99 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 128 CHECK(request_queue_.empty() || state_ == STOPPED); | 132 CHECK(request_queue_.empty() || state_ == STOPPED); |
| 129 | 133 |
| 130 UMA_HISTOGRAM_COUNTS_100( | 134 UMA_HISTOGRAM_COUNTS_100( |
| 131 internal::kResourcePrefetchPredictorPrefetchedCountHistogram, | 135 internal::kResourcePrefetchPredictorPrefetchedCountHistogram, |
| 132 prefetched_count_); | 136 prefetched_count_); |
| 133 UMA_HISTOGRAM_COUNTS_10000( | 137 UMA_HISTOGRAM_COUNTS_10000( |
| 134 internal::kResourcePrefetchPredictorPrefetchedSizeHistogram, | 138 internal::kResourcePrefetchPredictorPrefetchedSizeHistogram, |
| 135 prefetched_bytes_ / 1024); | 139 prefetched_bytes_ / 1024); |
| 136 | 140 |
| 137 state_ = FINISHED; | 141 state_ = FINISHED; |
| 138 delegate_->ResourcePrefetcherFinished(this, std::move(stats_)); | 142 |
| 143 content::BrowserThread::PostTask( | |
| 144 content::BrowserThread::UI, FROM_HERE, | |
| 145 base::BindOnce(&Delegate::ResourcePrefetcherFinished, delegate_, this, | |
| 146 base::Passed(std::move(stats_)))); | |
| 139 } | 147 } |
| 140 } | 148 } |
| 141 | 149 |
| 142 void ResourcePrefetcher::SendRequest(const GURL& url) { | 150 void ResourcePrefetcher::SendRequest(const GURL& url) { |
| 143 net::NetworkTrafficAnnotationTag traffic_annotation = | 151 net::NetworkTrafficAnnotationTag traffic_annotation = |
| 144 net::DefineNetworkTrafficAnnotation("resource_prefetch", R"( | 152 net::DefineNetworkTrafficAnnotation("resource_prefetch", R"( |
| 145 semantics { | 153 semantics { |
| 146 sender: "Resource Prefetch" | 154 sender: "Resource Prefetch" |
| 147 description: | 155 description: |
| 148 "Speculative Prefetch is based on the observation that users do " | 156 "Speculative Prefetch is based on the observation that users do " |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 174 "load pages more quickly' setting under Privacy. The feature is " | 182 "load pages more quickly' setting under Privacy. The feature is " |
| 175 "enabled by default." | 183 "enabled by default." |
| 176 chrome_policy { | 184 chrome_policy { |
| 177 NetworkPredictionOptions { | 185 NetworkPredictionOptions { |
| 178 policy_options {mode: MANDATORY} | 186 policy_options {mode: MANDATORY} |
| 179 NetworkPredictionOptions: 2 | 187 NetworkPredictionOptions: 2 |
| 180 } | 188 } |
| 181 } | 189 } |
| 182 })"); | 190 })"); |
| 183 std::unique_ptr<net::URLRequest> url_request = | 191 std::unique_ptr<net::URLRequest> url_request = |
| 184 delegate_->GetURLRequestContext()->CreateRequest(url, net::IDLE, this, | 192 context_getter_->GetURLRequestContext()->CreateRequest( |
| 185 traffic_annotation); | 193 url, net::IDLE, this, traffic_annotation); |
| 186 host_inflight_counts_[url.host()] += 1; | 194 host_inflight_counts_[url.host()] += 1; |
| 187 | 195 |
| 188 url_request->set_method("GET"); | 196 url_request->set_method("GET"); |
| 189 url_request->set_first_party_for_cookies(main_frame_url_); | 197 url_request->set_first_party_for_cookies(main_frame_url_); |
| 190 url_request->set_initiator(url::Origin(main_frame_url_)); | 198 url_request->set_initiator(url::Origin(main_frame_url_)); |
| 191 | 199 |
| 192 content::Referrer referrer(main_frame_url_, blink::kWebReferrerPolicyDefault); | 200 content::Referrer referrer(main_frame_url_, blink::kWebReferrerPolicyDefault); |
| 193 content::Referrer sanitized_referrer = | 201 content::Referrer sanitized_referrer = |
| 194 content::Referrer::SanitizeForRequest(url, referrer); | 202 content::Referrer::SanitizeForRequest(url, referrer); |
| 195 content::Referrer::SetReferrerForRequest(url_request.get(), | 203 content::Referrer::SetReferrerForRequest(url_request.get(), |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 279 | 287 |
| 280 void ResourcePrefetcher::OnResponseStarted(net::URLRequest* request, | 288 void ResourcePrefetcher::OnResponseStarted(net::URLRequest* request, |
| 281 int net_error) { | 289 int net_error) { |
| 282 DCHECK_NE(net::ERR_IO_PENDING, net_error); | 290 DCHECK_NE(net::ERR_IO_PENDING, net_error); |
| 283 | 291 |
| 284 if (net_error != net::OK) { | 292 if (net_error != net::OK) { |
| 285 FinishRequest(request); | 293 FinishRequest(request); |
| 286 return; | 294 return; |
| 287 } | 295 } |
| 288 | 296 |
| 289 // TODO(shishir): Do not read cached entries, or ones that are not cacheable. | |
| 290 ReadFullResponse(request); | 297 ReadFullResponse(request); |
| 291 } | 298 } |
| 292 | 299 |
| 293 void ResourcePrefetcher::OnReadCompleted(net::URLRequest* request, | 300 void ResourcePrefetcher::OnReadCompleted(net::URLRequest* request, |
| 294 int bytes_read) { | 301 int bytes_read) { |
| 295 DCHECK_NE(net::ERR_IO_PENDING, bytes_read); | 302 DCHECK_NE(net::ERR_IO_PENDING, bytes_read); |
| 296 | 303 |
| 297 if (bytes_read <= 0) { | 304 if (bytes_read <= 0) { |
| 298 FinishRequest(request); | 305 FinishRequest(request); |
| 299 return; | 306 return; |
| 300 } | 307 } |
| 301 | 308 |
| 302 if (bytes_read > 0) | 309 if (bytes_read > 0) |
| 303 ReadFullResponse(request); | 310 ReadFullResponse(request); |
| 304 } | 311 } |
| 305 | 312 |
| 306 } // namespace predictors | 313 } // namespace predictors |
| OLD | NEW |