1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
// 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 <iterator>
#include "chrome/browser/predictors/resource_prefetcher.h"
#include "base/stl_util.h"
#include "content/public/browser/browser_thread.h"
#include "net/base/io_buffer.h"
namespace {
// The size of the buffer used to read the resource.
static const size_t kResourceBufferSizeBytes = 50000;
} // namespace
namespace predictors {
ResourcePrefetcher::Request::Request(const GURL& i_resource_url)
: resource_url(i_resource_url),
prefetch_status(PREFETCH_STATUS_NOT_STARTED),
usage_status(USAGE_STATUS_NOT_REQUESTED) {
}
ResourcePrefetcher::Request::Request(const Request& other)
: resource_url(other.resource_url),
prefetch_status(other.prefetch_status),
usage_status(other.usage_status) {
}
ResourcePrefetcher::ResourcePrefetcher(
Delegate* delegate,
const ResourcePrefetchPredictorConfig& config,
const NavigationID& navigation_id,
PrefetchKeyType key_type,
scoped_ptr<RequestVector> requests)
: state_(INITIALIZED),
delegate_(delegate),
config_(config),
navigation_id_(navigation_id),
key_type_(key_type),
request_vector_(requests.Pass()) {
CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
DCHECK(request_vector_.get());
std::copy(request_vector_->begin(), request_vector_->end(),
std::back_inserter(request_queue_));
}
ResourcePrefetcher::~ResourcePrefetcher() {
// Delete any pending net::URLRequests.
STLDeleteContainerPairFirstPointers(inflight_requests_.begin(),
inflight_requests_.end());
}
void ResourcePrefetcher::Start() {
CHECK(CalledOnValidThread());
CHECK_EQ(state_, INITIALIZED);
state_ = RUNNING;
TryToLaunchPrefetchRequests();
}
void ResourcePrefetcher::Stop() {
CHECK(CalledOnValidThread());
if (state_ == FINISHED)
return;
state_ = STOPPED;
}
void ResourcePrefetcher::TryToLaunchPrefetchRequests() {
CHECK(state_ == RUNNING || state_ == STOPPED);
// Try to launch new requests if the state is RUNNING.
if (state_ == RUNNING) {
bool request_available = true;
// Loop through the requests while we are under the
// max_prefetches_inflight_per_host_per_navigation limit, looking for a URL
// for which the max_prefetches_inflight_per_host_per_navigation limit has
// not been reached. Try to launch as many requests as possible.
while ((static_cast<int>(inflight_requests_.size()) <
config_.max_prefetches_inflight_per_navigation) &&
request_available) {
std::list<Request*>::iterator request_it = request_queue_.begin();
for (; request_it != request_queue_.end(); ++request_it) {
const std::string& host = (*request_it)->resource_url.host();
std::map<std::string, int>::iterator host_it =
host_inflight_counts_.find(host);
if (host_it == host_inflight_counts_.end() ||
host_it->second <
config_.max_prefetches_inflight_per_host_per_navigation)
break;
}
request_available = request_it != request_queue_.end();
if (request_available) {
SendRequest(*request_it);
request_queue_.erase(request_it);
}
}
}
// If the inflight_requests_ is empty, we cant launch any more. Finish.
if (inflight_requests_.empty()) {
CHECK(host_inflight_counts_.empty());
CHECK(request_queue_.empty() || state_ == STOPPED);
state_ = FINISHED;
delegate_->ResourcePrefetcherFinished(this, request_vector_.release());
}
}
void ResourcePrefetcher::SendRequest(Request* request) {
request->prefetch_status = Request::PREFETCH_STATUS_STARTED;
net::URLRequest* url_request =
new net::URLRequest(request->resource_url,
this,
delegate_->GetURLRequestContext());
inflight_requests_[url_request] = request;
host_inflight_counts_[url_request->original_url().host()] += 1;
url_request->set_method("GET");
url_request->set_first_party_for_cookies(navigation_id_.main_frame_url);
url_request->set_referrer(navigation_id_.main_frame_url.spec());
url_request->set_priority(net::LOW);
StartURLRequest(url_request);
}
void ResourcePrefetcher::StartURLRequest(net::URLRequest* request) {
request->Start();
}
void ResourcePrefetcher::FinishRequest(net::URLRequest* request,
Request::PrefetchStatus status) {
std::map<net::URLRequest*, Request*>::iterator request_it =
inflight_requests_.find(request);
CHECK(request_it != inflight_requests_.end());
const std::string host = request->original_url().host();
std::map<std::string, int>::iterator host_it = host_inflight_counts_.find(
host);
CHECK_GT(host_it->second, 0);
host_it->second -= 1;
if (host_it->second == 0)
host_inflight_counts_.erase(host);
request_it->second->prefetch_status = status;
inflight_requests_.erase(request_it);
delete request;
TryToLaunchPrefetchRequests();
}
void ResourcePrefetcher::ReadFullResponse(net::URLRequest* request) {
bool status = true;
while (status) {
int bytes_read = 0;
scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(
kResourceBufferSizeBytes));
status = request->Read(buffer, kResourceBufferSizeBytes, &bytes_read);
if (status) {
status = ShouldContinueReadingRequest(request, bytes_read);
} else if (request->status().error()) {
FinishRequest(request, Request::PREFETCH_STATUS_FAILED);
return;
}
}
}
bool ResourcePrefetcher::ShouldContinueReadingRequest(net::URLRequest* request,
int bytes_read) {
if (bytes_read == 0) { // When bytes_read == 0, no more data.
if (request->was_cached())
FinishRequest(request, Request::PREFETCH_STATUS_FROM_CACHE);
else
FinishRequest(request, Request::PREFETCH_STATUS_FROM_NETWORK);
return false;
}
return true;
}
void ResourcePrefetcher::OnReceivedRedirect(net::URLRequest* request,
const GURL& new_url,
bool* defer_redirect) {
FinishRequest(request, Request::PREFETCH_STATUS_REDIRECTED);
}
void ResourcePrefetcher::OnAuthRequired(net::URLRequest* request,
net::AuthChallengeInfo* auth_info) {
FinishRequest(request, Request::PREFETCH_STATUS_AUTH_REQUIRED);
}
void ResourcePrefetcher::OnCertificateRequested(
net::URLRequest* request,
net::SSLCertRequestInfo* cert_request_info) {
FinishRequest(request, Request::PREFETCH_STATUS_CERT_REQUIRED);
}
void ResourcePrefetcher::OnSSLCertificateError(net::URLRequest* request,
const net::SSLInfo& ssl_info,
bool fatal) {
FinishRequest(request, Request::PREFETCH_STATUS_CERT_ERROR);
}
void ResourcePrefetcher::OnResponseStarted(net::URLRequest* request) {
if (request->status().error()) {
FinishRequest(request, Request::PREFETCH_STATUS_FAILED);
return;
}
// TODO(shishir): Do not read cached entries, or ones that are not cacheable.
ReadFullResponse(request);
}
void ResourcePrefetcher::OnReadCompleted(net::URLRequest* request,
int bytes_read) {
if (request->status().error()) {
FinishRequest(request, Request::PREFETCH_STATUS_FAILED);
return;
}
if (ShouldContinueReadingRequest(request, bytes_read))
ReadFullResponse(request);
}
} // namespace predictors
|