OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/url_request/sdch_dictionary_fetcher.h" | 5 #include "net/url_request/sdch_dictionary_fetcher.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <queue> | 8 #include <queue> |
9 #include <set> | 9 #include <set> |
10 | 10 |
(...skipping 14 matching lines...) Expand all Loading... |
25 #include "net/url_request/url_request_throttler_manager.h" | 25 #include "net/url_request/url_request_throttler_manager.h" |
26 | 26 |
27 namespace net { | 27 namespace net { |
28 | 28 |
29 namespace { | 29 namespace { |
30 | 30 |
31 const int kBufferSize = 4096; | 31 const int kBufferSize = 4096; |
32 | 32 |
33 // Map the bytes_read result from a read attempt and a URLRequest's | 33 // Map the bytes_read result from a read attempt and a URLRequest's |
34 // status into a single net return value. | 34 // status into a single net return value. |
35 int GetReadResult(int bytes_read, const URLRequest* request) { | 35 int GetReadResult(int rv, const URLRequest* request) { |
36 int rv = request->status().error(); | 36 DCHECK_NE(ERR_IO_PENDING, rv); |
37 if (request->status().is_success() && bytes_read < 0) { | 37 |
| 38 if (rv < 0) { |
38 rv = ERR_FAILED; | 39 rv = ERR_FAILED; |
39 request->net_log().AddEventWithNetErrorCode( | 40 request->net_log().AddEventWithNetErrorCode( |
40 NetLogEventType::SDCH_DICTIONARY_FETCH_IMPLIED_ERROR, rv); | 41 NetLogEventType::SDCH_DICTIONARY_FETCH_IMPLIED_ERROR, rv); |
41 } | 42 } |
42 | 43 |
43 if (rv == OK) | |
44 rv = bytes_read; | |
45 | |
46 return rv; | 44 return rv; |
47 } | 45 } |
48 | 46 |
49 struct FetchInfo { | 47 struct FetchInfo { |
50 FetchInfo(const GURL& url, | 48 FetchInfo(const GURL& url, |
51 bool cache_only, | 49 bool cache_only, |
52 const SdchDictionaryFetcher::OnDictionaryFetchedCallback& callback) | 50 const SdchDictionaryFetcher::OnDictionaryFetchedCallback& callback) |
53 : url(url), cache_only(cache_only), callback(callback) {} | 51 : url(url), cache_only(cache_only), callback(callback) {} |
54 FetchInfo() {} | 52 FetchInfo() {} |
55 | 53 |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 URLRequest* request, | 147 URLRequest* request, |
150 const RedirectInfo& redirect_info, | 148 const RedirectInfo& redirect_info, |
151 bool* defer_redirect) { | 149 bool* defer_redirect) { |
152 DCHECK_EQ(next_state_, STATE_SEND_REQUEST_PENDING); | 150 DCHECK_EQ(next_state_, STATE_SEND_REQUEST_PENDING); |
153 | 151 |
154 next_state_ = STATE_RECEIVED_REDIRECT; | 152 next_state_ = STATE_RECEIVED_REDIRECT; |
155 | 153 |
156 DoLoop(OK); | 154 DoLoop(OK); |
157 } | 155 } |
158 | 156 |
159 void SdchDictionaryFetcher::OnResponseStarted(URLRequest* request) { | 157 void SdchDictionaryFetcher::OnResponseStarted(URLRequest* request, |
| 158 int net_error) { |
160 DCHECK(CalledOnValidThread()); | 159 DCHECK(CalledOnValidThread()); |
161 DCHECK_EQ(request, current_request_.get()); | 160 DCHECK_EQ(request, current_request_.get()); |
162 DCHECK_EQ(next_state_, STATE_SEND_REQUEST_PENDING); | 161 DCHECK_EQ(next_state_, STATE_SEND_REQUEST_PENDING); |
163 DCHECK(!in_loop_); | 162 DCHECK(!in_loop_); |
| 163 DCHECK_NE(ERR_IO_PENDING, net_error); |
164 | 164 |
165 // Confirm that the response isn't a stale read from the cache (as | 165 // Confirm that the response isn't a stale read from the cache (as |
166 // may happen in the reload case). If the response was not retrieved over | 166 // may happen in the reload case). If the response was not retrieved over |
167 // HTTP, it is presumed to be fresh. | 167 // HTTP, it is presumed to be fresh. |
168 HttpResponseHeaders* response_headers = request->response_headers(); | 168 HttpResponseHeaders* response_headers = request->response_headers(); |
169 int result = request->status().error(); | 169 if (net_error == OK && response_headers) { |
170 if (result == OK && response_headers) { | |
171 ValidationType validation_type = response_headers->RequiresValidation( | 170 ValidationType validation_type = response_headers->RequiresValidation( |
172 request->response_info().request_time, | 171 request->response_info().request_time, |
173 request->response_info().response_time, base::Time::Now()); | 172 request->response_info().response_time, base::Time::Now()); |
174 // TODO(rdsmith): Maybe handle VALIDATION_ASYNCHRONOUS by queueing | 173 // TODO(rdsmith): Maybe handle VALIDATION_ASYNCHRONOUS by queueing |
175 // a non-reload request for the dictionary. | 174 // a non-reload request for the dictionary. |
176 if (validation_type != VALIDATION_NONE) | 175 if (validation_type != VALIDATION_NONE) |
177 result = ERR_FAILED; | 176 net_error = ERR_FAILED; |
178 } | 177 } |
179 | 178 |
180 DoLoop(result); | 179 DoLoop(net_error); |
181 } | 180 } |
182 | 181 |
183 void SdchDictionaryFetcher::OnReadCompleted(URLRequest* request, | 182 void SdchDictionaryFetcher::OnReadCompleted(URLRequest* request, |
184 int bytes_read) { | 183 int bytes_read) { |
185 DCHECK(CalledOnValidThread()); | 184 DCHECK(CalledOnValidThread()); |
186 DCHECK_EQ(request, current_request_.get()); | 185 DCHECK_EQ(request, current_request_.get()); |
187 DCHECK_EQ(next_state_, STATE_READ_BODY_COMPLETE); | 186 DCHECK_EQ(next_state_, STATE_READ_BODY_COMPLETE); |
188 DCHECK(!in_loop_); | 187 DCHECK(!in_loop_); |
| 188 DCHECK_NE(ERR_IO_PENDING, bytes_read); |
189 | 189 |
190 DoLoop(GetReadResult(bytes_read, current_request_.get())); | 190 DoLoop(GetReadResult(bytes_read, current_request_.get())); |
191 } | 191 } |
192 | 192 |
193 bool SdchDictionaryFetcher::ScheduleInternal( | 193 bool SdchDictionaryFetcher::ScheduleInternal( |
194 const GURL& dictionary_url, | 194 const GURL& dictionary_url, |
195 bool reload, | 195 bool reload, |
196 const OnDictionaryFetchedCallback& callback) { | 196 const OnDictionaryFetchedCallback& callback) { |
197 DCHECK(CalledOnValidThread()); | 197 DCHECK(CalledOnValidThread()); |
198 | 198 |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 DCHECK(CalledOnValidThread()); | 323 DCHECK(CalledOnValidThread()); |
324 | 324 |
325 // If there's been an error, abort the current request. | 325 // If there's been an error, abort the current request. |
326 if (rv != OK) { | 326 if (rv != OK) { |
327 ResetRequest(); | 327 ResetRequest(); |
328 next_state_ = STATE_SEND_REQUEST; | 328 next_state_ = STATE_SEND_REQUEST; |
329 return OK; | 329 return OK; |
330 } | 330 } |
331 | 331 |
332 next_state_ = STATE_READ_BODY_COMPLETE; | 332 next_state_ = STATE_READ_BODY_COMPLETE; |
333 int bytes_read = 0; | 333 int bytes_read = current_request_->Read(buffer_.get(), kBufferSize); |
334 current_request_->Read(buffer_.get(), kBufferSize, &bytes_read); | 334 if (bytes_read == ERR_IO_PENDING) |
335 if (current_request_->status().is_io_pending()) | |
336 return ERR_IO_PENDING; | 335 return ERR_IO_PENDING; |
337 | 336 |
338 return GetReadResult(bytes_read, current_request_.get()); | 337 return GetReadResult(bytes_read, current_request_.get()); |
339 } | 338 } |
340 | 339 |
341 int SdchDictionaryFetcher::DoReadBodyComplete(int rv) { | 340 int SdchDictionaryFetcher::DoReadBodyComplete(int rv) { |
342 DCHECK(CalledOnValidThread()); | 341 DCHECK(CalledOnValidThread()); |
343 | 342 |
344 // An error; abort the current request. | 343 // An error; abort the current request. |
345 if (rv < 0) { | 344 if (rv < 0) { |
346 ResetRequest(); | 345 ResetRequest(); |
347 next_state_ = STATE_SEND_REQUEST; | 346 next_state_ = STATE_SEND_REQUEST; |
348 return OK; | 347 return OK; |
349 } | 348 } |
350 | 349 |
351 DCHECK(current_request_->status().is_success()); | 350 DCHECK_GE(rv, 0); |
352 | 351 |
353 // Data; append to the dictionary and look for more data. | 352 // Data; append to the dictionary and look for more data. |
354 if (rv > 0) { | 353 if (rv > 0) { |
355 dictionary_->append(buffer_->data(), rv); | 354 dictionary_->append(buffer_->data(), rv); |
356 next_state_ = STATE_READ_BODY; | 355 next_state_ = STATE_READ_BODY; |
357 return OK; | 356 return OK; |
358 } | 357 } |
359 | 358 |
360 // End of file; complete the request. | 359 // End of file; complete the request. |
361 next_state_ = STATE_REQUEST_COMPLETE; | 360 next_state_ = STATE_REQUEST_COMPLETE; |
362 return OK; | 361 return OK; |
363 } | 362 } |
364 | 363 |
365 int SdchDictionaryFetcher::DoCompleteRequest(int rv) { | 364 int SdchDictionaryFetcher::DoCompleteRequest(int rv) { |
366 DCHECK(CalledOnValidThread()); | 365 DCHECK(CalledOnValidThread()); |
367 | 366 |
368 // If the dictionary was successfully fetched, add it to the manager. | 367 // If the dictionary was successfully fetched, add it to the manager. |
369 if (rv == OK) { | 368 if (rv == OK) { |
370 current_callback_.Run(*dictionary_, current_request_->url(), | 369 current_callback_.Run(*dictionary_, current_request_->url(), |
371 current_request_->net_log(), | 370 current_request_->net_log(), |
372 current_request_->was_cached()); | 371 current_request_->was_cached()); |
373 } | 372 } |
374 | 373 |
375 ResetRequest(); | 374 ResetRequest(); |
376 next_state_ = STATE_SEND_REQUEST; | 375 next_state_ = STATE_SEND_REQUEST; |
377 return OK; | 376 return OK; |
378 } | 377 } |
379 | 378 |
380 } // namespace net | 379 } // namespace net |
OLD | NEW |