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

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: Use histograms instead of user actions. 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/histogram_macros.h"
14 #include "base/metrics/sparse_histogram.h"
15 #include "base/single_thread_task_runner.h"
16 #include "base/thread_task_runner_handle.h"
17 #include "base/time/time.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 // Give the handler a chance to delay the URLRequest from being started.
55 bool defer_start = false;
56 throttle_->WillStartRequest(&defer_start);
57
58 if (defer_start) {
59 RecordDefer();
60 } else {
61 StartRequestInternal();
62 }
63 }
64
65 void AsyncRevalidationDriver::OnReceivedRedirect(
66 net::URLRequest* request,
67 const net::RedirectInfo& redirect_info,
68 bool* defer) {
69 DCHECK_EQ(request_.get(), request);
70
71 // The async revalidation should not follow redirects, because caching is
72 // a property of an individual HTTP resource.
73 DVLOG(1) << "OnReceivedRedirect: " << request_->url().spec();
74 CancelRequestInternal(net::ERR_ABORTED, RESULT_GOT_REDIRECT);
75 }
76
77 void AsyncRevalidationDriver::OnAuthRequired(
78 net::URLRequest* request,
79 net::AuthChallengeInfo* auth_info) {
80 DCHECK_EQ(request_.get(), request);
81 // This error code doesn't have exactly the right semantics, but it should
82 // be sufficient to narrow down the problem in net logs.
83 CancelRequestInternal(net::ERR_ACCESS_DENIED, RESULT_AUTH_FAILED);
84 }
85
86 void AsyncRevalidationDriver::OnBeforeNetworkStart(net::URLRequest* request,
87 bool* defer) {
88 DCHECK_EQ(request_.get(), request);
89
90 // Verify that the ResourceScheduler does not defer here.
91 throttle_->WillStartUsingNetwork(defer);
92 DCHECK(!*defer);
93
94 // Start the response timer. This use of base::Unretained() is guaranteed safe
95 // by the semantics of base::OneShotTimer.
96 timer_.Start(FROM_HERE,
97 base::TimeDelta::FromSeconds(kResponseTimeoutInSeconds),
98 base::Bind(&AsyncRevalidationDriver::OnTimeout,
99 base::Unretained(this), RESULT_RESPONSE_TIMEOUT));
100 }
101
102 void AsyncRevalidationDriver::OnResponseStarted(net::URLRequest* request) {
103 DCHECK_EQ(request_.get(), request);
104
105 DVLOG(1) << "OnResponseStarted: " << request_->url().spec();
106
107 // We have the response. No need to wait any longer.
108 timer_.Stop();
109
110 if (!request_->status().is_success()) {
111 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.AsyncRevalidation.ResponseError",
112 -request_->status().ToNetError());
113 ResponseCompleted(RESULT_NET_ERROR);
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(RESULT_REVALIDATED);
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), RESULT_BODY_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 == 0 is EOF.
150 if (bytes_read == 0) {
151 ResponseCompleted(RESULT_LOADED);
152 return;
153 }
154 // bytes_read == -1 is an error.
155 if (bytes_read == -1 || !request_->status().is_success()) {
156 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.AsyncRevalidation.ReadError",
157 -request_->status().ToNetError());
158 ResponseCompleted(RESULT_READ_ERROR);
159 // |this| may be deleted after this point.
160 return;
161 }
162
163 DCHECK_GT(bytes_read, 0);
164 StartReading(true); // Read the next chunk.
165 }
166
167 void AsyncRevalidationDriver::Resume() {
168 DCHECK(is_deferred_);
169 DCHECK(request_);
170 is_deferred_ = false;
171 request_->LogUnblocked();
172 StartRequestInternal();
173 }
174
175 void AsyncRevalidationDriver::Cancel() {
176 NOTREACHED();
177 }
178
179 void AsyncRevalidationDriver::CancelAndIgnore() {
180 NOTREACHED();
181 }
182
183 void AsyncRevalidationDriver::CancelWithError(int error_code) {
184 NOTREACHED();
185 }
186
187 void AsyncRevalidationDriver::StartRequestInternal() {
188 DCHECK(request_);
189 DCHECK(!request_->is_pending());
190
191 request_->Start();
192 }
193
194 void AsyncRevalidationDriver::CancelRequestInternal(
195 int error,
196 AsyncRevalidationResult result) {
197 DVLOG(1) << "CancelRequestInternal: " << request_->url().spec();
198
199 // Set the error code since this will be reported to the NetworkDelegate and
200 // recorded in the NetLog.
201 request_->CancelWithError(error);
202
203 // The ResourceScheduler needs to be able to examine the request when the
204 // ResourceThrottle is destroyed, so delete it first.
205 throttle_.reset();
206
207 // Destroy the request so that it doesn't try to send an asynchronous
208 // notification of completion.
209 request_.reset();
210
211 // Cancel timer to prevent OnTimeout() being called.
212 timer_.Stop();
213
214 ResponseCompleted(result);
215 // |this| may deleted after this point.
216 }
217
218 void AsyncRevalidationDriver::StartReading(bool is_continuation) {
219 int bytes_read = 0;
220 ReadMore(&bytes_read);
221
222 // If IO is pending, wait for the URLRequest to call OnReadCompleted.
223 if (request_->status().is_io_pending())
224 return;
225
226 if (!is_continuation || bytes_read <= 0) {
227 OnReadCompleted(request_.get(), bytes_read);
228 } else {
229 // Else, trigger OnReadCompleted asynchronously to avoid starving the IO
230 // thread in case the URLRequest can provide data synchronously.
231 scoped_refptr<base::SingleThreadTaskRunner> single_thread_task_runner =
232 base::ThreadTaskRunnerHandle::Get();
233 single_thread_task_runner->PostTask(
234 FROM_HERE,
235 base::Bind(&AsyncRevalidationDriver::OnReadCompleted,
236 weak_ptr_factory_.GetWeakPtr(), request_.get(), bytes_read));
237 }
238 }
239
240 void AsyncRevalidationDriver::ReadMore(int* bytes_read) {
241 DCHECK(!is_deferred_);
242
243 if (!read_buffer_)
244 read_buffer_ = new net::IOBuffer(kReadBufSize);
245
246 timer_.Reset();
247 request_->Read(read_buffer_.get(), kReadBufSize, bytes_read);
248
249 // No need to check the return value here as we'll detect errors by
250 // inspecting the URLRequest's status.
251 }
252
253 void AsyncRevalidationDriver::ResponseCompleted(
254 AsyncRevalidationResult result) {
255 DVLOG(1) << "ResponseCompleted: "
256 << (request_ ? request_->url().spec() : "(request deleted)")
257 << "result = " << result;
258 UMA_HISTOGRAM_ENUMERATION("Net.AsyncRevalidation.Result", result, RESULT_MAX);
259 DCHECK(!completion_callback_.is_null());
260 base::ResetAndReturn(&completion_callback_).Run();
261 // |this| may be deleted after this point.
262 }
263
264 void AsyncRevalidationDriver::OnTimeout(AsyncRevalidationResult result) {
265 CancelRequestInternal(net::ERR_TIMED_OUT, result);
266 }
267
268 void AsyncRevalidationDriver::RecordDefer() {
269 request_->LogBlockedBy(throttle_->GetNameForLogging());
270 DCHECK(!is_deferred_);
271 is_deferred_ = true;
272 }
273
274 } // 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