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/profiler/scoped_tracker.h"
10 #include "base/stl_util.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "net/base/io_buffer.h"
13 #include "net/base/load_flags.h"
14 #include "net/base/request_priority.h"
15 #include "net/url_request/url_request_context.h"
19 // The size of the buffer used to read the resource.
20 static const size_t kResourceBufferSizeBytes
= 50000;
24 namespace predictors
{
26 ResourcePrefetcher::Request::Request(const GURL
& i_resource_url
)
27 : resource_url(i_resource_url
),
28 prefetch_status(PREFETCH_STATUS_NOT_STARTED
),
29 usage_status(USAGE_STATUS_NOT_REQUESTED
) {
32 ResourcePrefetcher::Request::Request(const Request
& other
)
33 : resource_url(other
.resource_url
),
34 prefetch_status(other
.prefetch_status
),
35 usage_status(other
.usage_status
) {
38 ResourcePrefetcher::ResourcePrefetcher(
40 const ResourcePrefetchPredictorConfig
& config
,
41 const NavigationID
& navigation_id
,
42 PrefetchKeyType key_type
,
43 scoped_ptr
<RequestVector
> requests
)
44 : state_(INITIALIZED
),
47 navigation_id_(navigation_id
),
49 request_vector_(requests
.Pass()) {
50 DCHECK_CURRENTLY_ON(content::BrowserThread::IO
);
51 DCHECK(request_vector_
.get());
53 std::copy(request_vector_
->begin(), request_vector_
->end(),
54 std::back_inserter(request_queue_
));
57 ResourcePrefetcher::~ResourcePrefetcher() {
58 // Delete any pending net::URLRequests.
59 STLDeleteContainerPairFirstPointers(inflight_requests_
.begin(),
60 inflight_requests_
.end());
63 void ResourcePrefetcher::Start() {
64 DCHECK(thread_checker_
.CalledOnValidThread());
66 CHECK_EQ(state_
, INITIALIZED
);
69 TryToLaunchPrefetchRequests();
72 void ResourcePrefetcher::Stop() {
73 DCHECK(thread_checker_
.CalledOnValidThread());
75 if (state_
== FINISHED
)
81 void ResourcePrefetcher::TryToLaunchPrefetchRequests() {
82 CHECK(state_
== RUNNING
|| state_
== STOPPED
);
84 // Try to launch new requests if the state is RUNNING.
85 if (state_
== RUNNING
) {
86 bool request_available
= true;
88 // Loop through the requests while we are under the
89 // max_prefetches_inflight_per_host_per_navigation limit, looking for a URL
90 // for which the max_prefetches_inflight_per_host_per_navigation limit has
91 // not been reached. Try to launch as many requests as possible.
92 while ((inflight_requests_
.size() <
93 config_
.max_prefetches_inflight_per_navigation
) &&
95 std::list
<Request
*>::iterator request_it
= request_queue_
.begin();
96 for (; request_it
!= request_queue_
.end(); ++request_it
) {
97 const std::string
& host
= (*request_it
)->resource_url
.host();
99 std::map
<std::string
, size_t>::iterator host_it
=
100 host_inflight_counts_
.find(host
);
101 if (host_it
== host_inflight_counts_
.end() ||
103 config_
.max_prefetches_inflight_per_host_per_navigation
)
106 request_available
= request_it
!= request_queue_
.end();
108 if (request_available
) {
109 SendRequest(*request_it
);
110 request_queue_
.erase(request_it
);
115 // If the inflight_requests_ is empty, we cant launch any more. Finish.
116 if (inflight_requests_
.empty()) {
117 CHECK(host_inflight_counts_
.empty());
118 CHECK(request_queue_
.empty() || state_
== STOPPED
);
121 delegate_
->ResourcePrefetcherFinished(this, request_vector_
.release());
125 void ResourcePrefetcher::SendRequest(Request
* request
) {
126 request
->prefetch_status
= Request::PREFETCH_STATUS_STARTED
;
128 net::URLRequest
* url_request
=
129 delegate_
->GetURLRequestContext()->CreateRequest(
130 request
->resource_url
, net::LOW
, this, NULL
).release();
132 inflight_requests_
[url_request
] = request
;
133 host_inflight_counts_
[url_request
->original_url().host()] += 1;
135 url_request
->set_method("GET");
136 url_request
->set_first_party_for_cookies(navigation_id_
.main_frame_url
);
137 url_request
->SetReferrer(navigation_id_
.main_frame_url
.spec());
138 url_request
->SetLoadFlags(url_request
->load_flags() | net::LOAD_PREFETCH
);
139 StartURLRequest(url_request
);
142 void ResourcePrefetcher::StartURLRequest(net::URLRequest
* request
) {
146 void ResourcePrefetcher::FinishRequest(net::URLRequest
* request
,
147 Request::PrefetchStatus status
) {
148 std::map
<net::URLRequest
*, Request
*>::iterator request_it
=
149 inflight_requests_
.find(request
);
150 CHECK(request_it
!= inflight_requests_
.end());
152 const std::string host
= request
->original_url().host();
153 std::map
<std::string
, size_t>::iterator host_it
= host_inflight_counts_
.find(
155 CHECK_GT(host_it
->second
, 0U);
156 host_it
->second
-= 1;
157 if (host_it
->second
== 0)
158 host_inflight_counts_
.erase(host
);
160 request_it
->second
->prefetch_status
= status
;
161 inflight_requests_
.erase(request_it
);
165 TryToLaunchPrefetchRequests();
168 void ResourcePrefetcher::ReadFullResponse(net::URLRequest
* request
) {
172 scoped_refptr
<net::IOBuffer
> buffer(new net::IOBuffer(
173 kResourceBufferSizeBytes
));
174 status
= request
->Read(buffer
.get(), kResourceBufferSizeBytes
, &bytes_read
);
177 status
= ShouldContinueReadingRequest(request
, bytes_read
);
178 } else if (request
->status().error()) {
179 FinishRequest(request
, Request::PREFETCH_STATUS_FAILED
);
185 bool ResourcePrefetcher::ShouldContinueReadingRequest(net::URLRequest
* request
,
187 if (bytes_read
== 0) { // When bytes_read == 0, no more data.
188 if (request
->was_cached())
189 FinishRequest(request
, Request::PREFETCH_STATUS_FROM_CACHE
);
191 FinishRequest(request
, Request::PREFETCH_STATUS_FROM_NETWORK
);
198 void ResourcePrefetcher::OnReceivedRedirect(
199 net::URLRequest
* request
,
200 const net::RedirectInfo
& redirect_info
,
201 bool* defer_redirect
) {
202 FinishRequest(request
, Request::PREFETCH_STATUS_REDIRECTED
);
205 void ResourcePrefetcher::OnAuthRequired(net::URLRequest
* request
,
206 net::AuthChallengeInfo
* auth_info
) {
207 FinishRequest(request
, Request::PREFETCH_STATUS_AUTH_REQUIRED
);
210 void ResourcePrefetcher::OnCertificateRequested(
211 net::URLRequest
* request
,
212 net::SSLCertRequestInfo
* cert_request_info
) {
213 FinishRequest(request
, Request::PREFETCH_STATUS_CERT_REQUIRED
);
216 void ResourcePrefetcher::OnSSLCertificateError(net::URLRequest
* request
,
217 const net::SSLInfo
& ssl_info
,
219 FinishRequest(request
, Request::PREFETCH_STATUS_CERT_ERROR
);
222 void ResourcePrefetcher::OnResponseStarted(net::URLRequest
* request
) {
223 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
224 tracked_objects::ScopedTracker
tracking_profile(
225 FROM_HERE_WITH_EXPLICIT_FUNCTION(
226 "423948 ResourcePrefetcher::OnResponseStarted"));
228 if (request
->status().error()) {
229 FinishRequest(request
, Request::PREFETCH_STATUS_FAILED
);
233 // TODO(shishir): Do not read cached entries, or ones that are not cacheable.
234 ReadFullResponse(request
);
237 void ResourcePrefetcher::OnReadCompleted(net::URLRequest
* request
,
239 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
240 tracked_objects::ScopedTracker
tracking_profile(
241 FROM_HERE_WITH_EXPLICIT_FUNCTION(
242 "423948 ResourcePrefetcher::OnReadCompleted"));
244 if (request
->status().error()) {
245 FinishRequest(request
, Request::PREFETCH_STATUS_FAILED
);
249 if (ShouldContinueReadingRequest(request
, bytes_read
))
250 ReadFullResponse(request
);
253 } // namespace predictors