Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(464)

Side by Side Diff: content/browser/loader/async_revalidation_driver.cc

Issue 1041993004: content::ResourceDispatcherHostImpl changes for stale-while-revalidate (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@s-w-r-yhirano-patch
Patch Set: Rebase. URLRequestJob is no longer ref-counted. Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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.
4
5 #include "content/browser/loader/async_revalidation_driver.h"
6
7 #include <utility>
8
9 #include "base/callback_helpers.h"
10 #include "base/location.h"
11 #include "base/logging.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/metrics/user_metrics_action.h"
14 #include "base/single_thread_task_runner.h"
15 #include "base/thread_task_runner_handle.h"
16 #include "base/time/time.h"
17 #include "content/public/browser/user_metrics.h"
18 #include "net/base/net_errors.h"
19 #include "net/url_request/url_request_status.h"
20
21 namespace content {
22
23 namespace {
24 // This matches the maximum allocation size of AsyncResourceHandler.
25 const int kReadBufSize = 32 * 1024;
26
27 // The time to wait for a response. Since this includes the time taken to
28 // connect, this has been set to match the connect timeout
29 // kTransportConnectJobTimeoutInSeconds.
30 const int kResponseTimeoutInSeconds = 240; // 4 minutes.
31
32 // This value should not be too large, as this request may be tying up a socket
33 // that could be used for something better. However, if it is too small, the
34 // cache entry will be truncated for no good reason.
35 // TODO(ricea): Find a more scientific way to set this timeout.
36 const int kReadTimeoutInSeconds = 30;
37 } // namespace
38
39 AsyncRevalidationDriver::AsyncRevalidationDriver(
40 scoped_ptr<net::URLRequest> request,
41 scoped_ptr<ResourceThrottle> throttle,
42 const base::Closure& completion_callback)
43 : request_(std::move(request)),
44 throttle_(std::move(throttle)),
45 completion_callback_(completion_callback),
46 weak_ptr_factory_(this) {
47 request_->set_delegate(this);
48 throttle_->set_controller(this);
49 }
50
51 AsyncRevalidationDriver::~AsyncRevalidationDriver() {}
52
53 void AsyncRevalidationDriver::StartRequest() {
54 RecordAction(base::UserMetricsAction("AsyncRevalidationCreated"));
55 // Give the handler a chance to delay the URLRequest from being started.
56 bool defer_start = false;
57 throttle_->WillStartRequest(&defer_start);
58
59 if (defer_start) {
60 RecordDefer();
61 } else {
62 StartRequestInternal();
63 }
64 }
65
66 void AsyncRevalidationDriver::OnReceivedRedirect(
67 net::URLRequest* request,
68 const net::RedirectInfo& redirect_info,
69 bool* defer) {
70 DCHECK_EQ(request_.get(), request);
71
72 // The async revalidation should not follow redirects, because caching is
73 // a property of an individual HTTP resource.
74 DVLOG(1) << "OnReceivedRedirect: " << request_->url().spec();
75 RecordAction(base::UserMetricsAction("AsyncRevalidationRedirected"));
76 CancelRequestInternal(net::ERR_ABORTED);
77 }
78
79 void AsyncRevalidationDriver::OnAuthRequired(
80 net::URLRequest* request,
81 net::AuthChallengeInfo* auth_info) {
82 DCHECK_EQ(request_.get(), request);
83 // This error code doesn't have exactly the right semantics, but it should
84 // be sufficient to narrow down the problem in net logs.
85 CancelRequestInternal(net::ERR_ACCESS_DENIED);
86 }
87
88 void AsyncRevalidationDriver::OnBeforeNetworkStart(net::URLRequest* request,
89 bool* defer) {
90 DCHECK_EQ(request_.get(), request);
91
92 // Verify that the ResourceScheduler does not defer here.
93 throttle_->WillStartUsingNetwork(defer);
94 DCHECK(!*defer);
95
96 // Start the response timer. This use of base::Unretained() is guaranteed safe
97 // by the semantics of base::OneShotTimer.
98 timer_.Start(FROM_HERE,
99 base::TimeDelta::FromSeconds(kResponseTimeoutInSeconds),
100 base::Bind(&AsyncRevalidationDriver::OnTimeout,
101 base::Unretained(this), RESPONSE_TIMEOUT));
102 }
103
104 void AsyncRevalidationDriver::OnResponseStarted(net::URLRequest* request) {
105 DCHECK_EQ(request_.get(), request);
106
107 DVLOG(1) << "OnResponseStarted: " << request_->url().spec();
108
109 // We have the response. No need to wait any longer.
110 timer_.Stop();
111
112 if (!request_->status().is_success()) {
113 ResponseCompleted();
114 // |this| may be deleted after this point.
115 return;
116 }
117
118 const net::HttpResponseInfo& response_info = request_->response_info();
119 if (!response_info.response_time.is_null() && response_info.was_cached) {
120 // The cached entry was revalidated. No need to read it in.
121 ResponseCompleted();
122 // |this| may be deleted after this point.
123 return;
124 }
125
126 bool defer = false;
127 throttle_->WillProcessResponse(&defer);
128 DCHECK(!defer);
129
130 // Set up the timer for reading the body. This use of base::Unretained() is
131 // guaranteed safe by the semantics of base::OneShotTimer.
132 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(kReadTimeoutInSeconds),
133 base::Bind(&AsyncRevalidationDriver::OnTimeout,
134 base::Unretained(this), READ_TIMEOUT));
135 StartReading(false); // Read the first chunk.
136 }
137
138 void AsyncRevalidationDriver::OnReadCompleted(net::URLRequest* request,
139 int bytes_read) {
140 // request_ could be NULL if a timeout happened while OnReadCompleted() was
141 // queued to run asynchronously.
142 if (!request_)
143 return;
144 DCHECK_EQ(request_.get(), request);
145 DCHECK(!is_deferred_);
146 DVLOG(1) << "OnReadCompleted: \"" << request_->url().spec() << "\""
147 << " bytes_read = " << bytes_read;
148
149 // bytes_read == -1 is an error.
150 // bytes_read == 0 is EOF.
151 if (bytes_read == -1 || bytes_read == 0 || !request_->status().is_success()) {
152 ResponseCompleted();
153 // |this| may be deleted after this point.
154 return;
155 }
156
157 DCHECK_GT(bytes_read, 0);
158 StartReading(true); // Read the next chunk.
159 }
160
161 void AsyncRevalidationDriver::Resume() {
162 DCHECK(is_deferred_);
163 DCHECK(request_);
164 is_deferred_ = false;
165 request_->LogUnblocked();
166 StartRequestInternal();
167 }
168
169 void AsyncRevalidationDriver::Cancel() {
170 NOTREACHED();
171 }
172
173 void AsyncRevalidationDriver::CancelAndIgnore() {
174 NOTREACHED();
175 }
176
177 void AsyncRevalidationDriver::CancelWithError(int error_code) {
178 NOTREACHED();
179 }
180
181 void AsyncRevalidationDriver::StartRequestInternal() {
182 DCHECK(request_);
183 DCHECK(!request_->is_pending());
184
185 request_->Start();
186 }
187
188 void AsyncRevalidationDriver::CancelRequestInternal(int error) {
189 DVLOG(1) << "CancelRequestInternal: " << request_->url().spec();
190
191 // Set the error code since this will be reported to the NetworkDelegate and
192 // recorded in the NetLog.
193 request_->CancelWithError(error);
194
195 // The ResourceScheduler needs to be able to examine the request when the
196 // ResourceThrottle is destroyed, so delete it first.
197 throttle_.reset();
198
199 // Destroy the request so that it doesn't try to send an asynchronous
200 // notification of completion.
201 request_.reset();
202
203 // Cancel timer to prevent OnTimeout() being called.
204 timer_.Stop();
205
206 ResponseCompleted();
207 // |this| may deleted after this point.
208 }
209
210 void AsyncRevalidationDriver::StartReading(bool is_continuation) {
211 int bytes_read = 0;
212 ReadMore(&bytes_read);
213
214 // If IO is pending, wait for the URLRequest to call OnReadCompleted.
215 if (request_->status().is_io_pending())
216 return;
217
218 if (!is_continuation || bytes_read <= 0) {
219 OnReadCompleted(request_.get(), bytes_read);
220 } else {
221 // Else, trigger OnReadCompleted asynchronously to avoid starving the IO
222 // thread in case the URLRequest can provide data synchronously.
223 scoped_refptr<base::SingleThreadTaskRunner> single_thread_task_runner =
224 base::ThreadTaskRunnerHandle::Get();
225 single_thread_task_runner->PostTask(
226 FROM_HERE,
227 base::Bind(&AsyncRevalidationDriver::OnReadCompleted,
228 weak_ptr_factory_.GetWeakPtr(), request_.get(), bytes_read));
229 }
230 }
231
232 void AsyncRevalidationDriver::ReadMore(int* bytes_read) {
233 DCHECK(!is_deferred_);
234
235 if (!read_buffer_)
236 read_buffer_ = new net::IOBuffer(kReadBufSize);
237
238 timer_.Reset();
239 request_->Read(read_buffer_.get(), kReadBufSize, bytes_read);
240
241 // No need to check the return value here as we'll detect errors by
242 // inspecting the URLRequest's status.
243 }
244
245 void AsyncRevalidationDriver::ResponseCompleted() {
246 DVLOG(1) << "ResponseCompleted: " << request_->url().spec();
247 DCHECK(!completion_callback_.is_null());
248 base::ResetAndReturn(&completion_callback_).Run();
249 // |this| may be deleted after this point.
250 }
251
252 void AsyncRevalidationDriver::OnTimeout(TimeoutType type) {
253 // UserMetricsAction must be passed a literal string otherwise
254 // //tools/metrics/actions/extract_actions.py won't understand it.
255 switch (type) {
256 case RESPONSE_TIMEOUT:
257 RecordAction(base::UserMetricsAction("AsyncRevalidationResponseTimeout"));
258 break;
259
260 case READ_TIMEOUT:
261 RecordAction(base::UserMetricsAction("AsyncRevalidationReadTimeout"));
Alexei Svitkine (slow) 2015/12/09 18:07:04 Given these are not user actions in the semantical
Adam Rice 2015/12/09 20:30:21 I suddenly understand the significance of the word
262 break;
263 }
264 CancelRequestInternal(net::ERR_TIMED_OUT);
265 }
266
267 void AsyncRevalidationDriver::RecordDefer() {
268 request_->LogBlockedBy(throttle_->GetNameForLogging());
269 DCHECK(!is_deferred_);
270 is_deferred_ = true;
271 }
272
273 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/loader/async_revalidation_driver.h ('k') | content/browser/loader/async_revalidation_driver_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698