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
3 // found in the LICENSE file.
5 #include "chrome/browser/predictors/resource_prefetcher.h"
9 #include "base/stl_util.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "net/base/io_buffer.h"
12 #include "net/base/load_flags.h"
13 #include "net/base/request_priority.h"
14 #include "net/url_request/url_request_context.h"
18 // The size of the buffer used to read the resource.
19 static const size_t kResourceBufferSizeBytes
= 50000;
23 namespace predictors
{
25 ResourcePrefetcher::Request::Request(const GURL
& i_resource_url
)
26 : resource_url(i_resource_url
),
27 prefetch_status(PREFETCH_STATUS_NOT_STARTED
),
28 usage_status(USAGE_STATUS_NOT_REQUESTED
) {
31 ResourcePrefetcher::Request::Request(const Request
& other
)
32 : resource_url(other
.resource_url
),
33 prefetch_status(other
.prefetch_status
),
34 usage_status(other
.usage_status
) {
37 ResourcePrefetcher::ResourcePrefetcher(
39 const ResourcePrefetchPredictorConfig
& config
,
40 const NavigationID
& navigation_id
,
41 PrefetchKeyType key_type
,
42 scoped_ptr
<RequestVector
> requests
)
43 : state_(INITIALIZED
),
46 navigation_id_(navigation_id
),
48 request_vector_(requests
.Pass()) {
49 DCHECK_CURRENTLY_ON(content::BrowserThread::IO
);
50 DCHECK(request_vector_
.get());
52 std::copy(request_vector_
->begin(), request_vector_
->end(),
53 std::back_inserter(request_queue_
));
56 ResourcePrefetcher::~ResourcePrefetcher() {
57 // Delete any pending net::URLRequests.
58 STLDeleteContainerPairFirstPointers(inflight_requests_
.begin(),
59 inflight_requests_
.end());
62 void ResourcePrefetcher::Start() {
63 DCHECK(thread_checker_
.CalledOnValidThread());
65 CHECK_EQ(state_
, INITIALIZED
);
68 TryToLaunchPrefetchRequests();
71 void ResourcePrefetcher::Stop() {
72 DCHECK(thread_checker_
.CalledOnValidThread());
74 if (state_
== FINISHED
)
80 void ResourcePrefetcher::TryToLaunchPrefetchRequests() {
81 CHECK(state_
== RUNNING
|| state_
== STOPPED
);
83 // Try to launch new requests if the state is RUNNING.
84 if (state_
== RUNNING
) {
85 bool request_available
= true;
87 // Loop through the requests while we are under the
88 // max_prefetches_inflight_per_host_per_navigation limit, looking for a URL
89 // for which the max_prefetches_inflight_per_host_per_navigation limit has
90 // not been reached. Try to launch as many requests as possible.
91 while ((inflight_requests_
.size() <
92 config_
.max_prefetches_inflight_per_navigation
) &&
94 std::list
<Request
*>::iterator request_it
= request_queue_
.begin();
95 for (; request_it
!= request_queue_
.end(); ++request_it
) {
96 const std::string
& host
= (*request_it
)->resource_url
.host();
98 std::map
<std::string
, size_t>::iterator host_it
=
99 host_inflight_counts_
.find(host
);
100 if (host_it
== host_inflight_counts_
.end() ||
102 config_
.max_prefetches_inflight_per_host_per_navigation
)
105 request_available
= request_it
!= request_queue_
.end();
107 if (request_available
) {
108 SendRequest(*request_it
);
109 request_queue_
.erase(request_it
);
114 // If the inflight_requests_ is empty, we cant launch any more. Finish.
115 if (inflight_requests_
.empty()) {
116 CHECK(host_inflight_counts_
.empty());
117 CHECK(request_queue_
.empty() || state_
== STOPPED
);
120 delegate_
->ResourcePrefetcherFinished(this, request_vector_
.release());
124 void ResourcePrefetcher::SendRequest(Request
* request
) {
125 request
->prefetch_status
= Request::PREFETCH_STATUS_STARTED
;
127 net::URLRequest
* url_request
=
128 delegate_
->GetURLRequestContext()->CreateRequest(
129 request
->resource_url
, net::LOW
, this).release();
131 inflight_requests_
[url_request
] = request
;
132 host_inflight_counts_
[url_request
->original_url().host()] += 1;
134 url_request
->set_method("GET");
135 url_request
->set_first_party_for_cookies(navigation_id_
.main_frame_url
);
136 url_request
->SetReferrer(navigation_id_
.main_frame_url
.spec());
137 url_request
->SetLoadFlags(url_request
->load_flags() | net::LOAD_PREFETCH
);
138 StartURLRequest(url_request
);
141 void ResourcePrefetcher::StartURLRequest(net::URLRequest
* request
) {
145 void ResourcePrefetcher::FinishRequest(net::URLRequest
* request
,
146 Request::PrefetchStatus status
) {
147 std::map
<net::URLRequest
*, Request
*>::iterator request_it
=
148 inflight_requests_
.find(request
);
149 CHECK(request_it
!= inflight_requests_
.end());
151 const std::string host
= request
->original_url().host();
152 std::map
<std::string
, size_t>::iterator host_it
= host_inflight_counts_
.find(
154 CHECK_GT(host_it
->second
, 0U);
155 host_it
->second
-= 1;
156 if (host_it
->second
== 0)
157 host_inflight_counts_
.erase(host
);
159 request_it
->second
->prefetch_status
= status
;
160 inflight_requests_
.erase(request_it
);
164 TryToLaunchPrefetchRequests();
167 void ResourcePrefetcher::ReadFullResponse(net::URLRequest
* request
) {
171 scoped_refptr
<net::IOBuffer
> buffer(new net::IOBuffer(
172 kResourceBufferSizeBytes
));
173 status
= request
->Read(buffer
.get(), kResourceBufferSizeBytes
, &bytes_read
);
176 status
= ShouldContinueReadingRequest(request
, bytes_read
);
177 } else if (request
->status().error()) {
178 FinishRequest(request
, Request::PREFETCH_STATUS_FAILED
);
184 bool ResourcePrefetcher::ShouldContinueReadingRequest(net::URLRequest
* request
,
186 if (bytes_read
== 0) { // When bytes_read == 0, no more data.
187 if (request
->was_cached())
188 FinishRequest(request
, Request::PREFETCH_STATUS_FROM_CACHE
);
190 FinishRequest(request
, Request::PREFETCH_STATUS_FROM_NETWORK
);
197 void ResourcePrefetcher::OnReceivedRedirect(
198 net::URLRequest
* request
,
199 const net::RedirectInfo
& redirect_info
,
200 bool* defer_redirect
) {
201 FinishRequest(request
, Request::PREFETCH_STATUS_REDIRECTED
);
204 void ResourcePrefetcher::OnAuthRequired(net::URLRequest
* request
,
205 net::AuthChallengeInfo
* auth_info
) {
206 FinishRequest(request
, Request::PREFETCH_STATUS_AUTH_REQUIRED
);
209 void ResourcePrefetcher::OnCertificateRequested(
210 net::URLRequest
* request
,
211 net::SSLCertRequestInfo
* cert_request_info
) {
212 FinishRequest(request
, Request::PREFETCH_STATUS_CERT_REQUIRED
);
215 void ResourcePrefetcher::OnSSLCertificateError(net::URLRequest
* request
,
216 const net::SSLInfo
& ssl_info
,
218 FinishRequest(request
, Request::PREFETCH_STATUS_CERT_ERROR
);
221 void ResourcePrefetcher::OnResponseStarted(net::URLRequest
* request
) {
222 if (request
->status().error()) {
223 FinishRequest(request
, Request::PREFETCH_STATUS_FAILED
);
227 // TODO(shishir): Do not read cached entries, or ones that are not cacheable.
228 ReadFullResponse(request
);
231 void ResourcePrefetcher::OnReadCompleted(net::URLRequest
* request
,
233 if (request
->status().error()) {
234 FinishRequest(request
, Request::PREFETCH_STATUS_FAILED
);
238 if (ShouldContinueReadingRequest(request
, bytes_read
))
239 ReadFullResponse(request
);
242 } // namespace predictors