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