| 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 | 8 |
| 9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/thread_task_runner_handle.h" | 12 #include "base/thread_task_runner_handle.h" |
| 13 #include "net/base/load_flags.h" | 13 #include "net/base/load_flags.h" |
| 14 #include "net/base/sdch_net_log_params.h" |
| 14 #include "net/url_request/url_request_context.h" | 15 #include "net/url_request/url_request_context.h" |
| 15 #include "net/url_request/url_request_status.h" | 16 #include "net/url_request/url_request_status.h" |
| 16 #include "net/url_request/url_request_throttler_manager.h" | 17 #include "net/url_request/url_request_throttler_manager.h" |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 const int kBufferSize = 4096; | 21 const int kBufferSize = 4096; |
| 21 | 22 |
| 22 } // namespace | 23 } // namespace |
| 23 | 24 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 38 SdchDictionaryFetcher::~SdchDictionaryFetcher() { | 39 SdchDictionaryFetcher::~SdchDictionaryFetcher() { |
| 39 DCHECK(CalledOnValidThread()); | 40 DCHECK(CalledOnValidThread()); |
| 40 } | 41 } |
| 41 | 42 |
| 42 void SdchDictionaryFetcher::Schedule(const GURL& dictionary_url) { | 43 void SdchDictionaryFetcher::Schedule(const GURL& dictionary_url) { |
| 43 DCHECK(CalledOnValidThread()); | 44 DCHECK(CalledOnValidThread()); |
| 44 | 45 |
| 45 // Avoid pushing duplicate copy onto queue. We may fetch this url again later | 46 // Avoid pushing duplicate copy onto queue. We may fetch this url again later |
| 46 // and get a different dictionary, but there is no reason to have it in the | 47 // and get a different dictionary, but there is no reason to have it in the |
| 47 // queue twice at one time. | 48 // queue twice at one time. |
| 48 if (!fetch_queue_.empty() && fetch_queue_.back() == dictionary_url) { | 49 if ((!fetch_queue_.empty() && fetch_queue_.back() == dictionary_url) || |
| 50 attempted_load_.find(dictionary_url) != attempted_load_.end()) { |
| 51 // TODO(rdsmith): log this error to the net log of the URLRequest |
| 52 // initiating this fetch, once URLRequest will be passed here. |
| 49 SdchManager::SdchErrorRecovery( | 53 SdchManager::SdchErrorRecovery( |
| 50 SdchManager::DICTIONARY_ALREADY_SCHEDULED_TO_DOWNLOAD); | 54 SDCH_DICTIONARY_PREVIOUSLY_SCHEDULED_TO_DOWNLOAD); |
| 51 return; | 55 return; |
| 52 } | 56 } |
| 53 if (attempted_load_.find(dictionary_url) != attempted_load_.end()) { | 57 |
| 54 SdchManager::SdchErrorRecovery( | |
| 55 SdchManager::DICTIONARY_ALREADY_TRIED_TO_DOWNLOAD); | |
| 56 return; | |
| 57 } | |
| 58 attempted_load_.insert(dictionary_url); | 58 attempted_load_.insert(dictionary_url); |
| 59 fetch_queue_.push(dictionary_url); | 59 fetch_queue_.push(dictionary_url); |
| 60 | 60 |
| 61 next_state_ = STATE_IDLE; | 61 next_state_ = STATE_IDLE; |
| 62 | 62 |
| 63 // There are no callbacks to user code from the dictionary fetcher, | 63 // There are no callbacks to user code from the dictionary fetcher, |
| 64 // and Schedule() is only called from user code, so this call to DoLoop() | 64 // and Schedule() is only called from user code, so this call to DoLoop() |
| 65 // does not require an |if (in_loop_) return;| guard. | 65 // does not require an |if (in_loop_) return;| guard. |
| 66 DoLoop(OK); | 66 DoLoop(OK); |
| 67 } | 67 } |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 | 159 |
| 160 current_request_ = | 160 current_request_ = |
| 161 context_->CreateRequest(fetch_queue_.front(), IDLE, this, NULL); | 161 context_->CreateRequest(fetch_queue_.front(), IDLE, this, NULL); |
| 162 current_request_->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES | | 162 current_request_->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES | |
| 163 LOAD_DO_NOT_SAVE_COOKIES); | 163 LOAD_DO_NOT_SAVE_COOKIES); |
| 164 buffer_ = new IOBuffer(kBufferSize); | 164 buffer_ = new IOBuffer(kBufferSize); |
| 165 fetch_queue_.pop(); | 165 fetch_queue_.pop(); |
| 166 | 166 |
| 167 next_state_ = STATE_REQUEST_STARTED; | 167 next_state_ = STATE_REQUEST_STARTED; |
| 168 current_request_->Start(); | 168 current_request_->Start(); |
| 169 current_request_->net_log().AddEvent(NetLog::TYPE_SDCH_DICTIONARY_FETCH); |
| 169 | 170 |
| 170 return OK; | 171 return OK; |
| 171 } | 172 } |
| 172 | 173 |
| 173 int SdchDictionaryFetcher::DoRequestStarted(int rv) { | 174 int SdchDictionaryFetcher::DoRequestStarted(int rv) { |
| 174 DCHECK(CalledOnValidThread()); | 175 DCHECK(CalledOnValidThread()); |
| 175 DCHECK_EQ(rv, OK); // Can only come straight from above function. | 176 DCHECK_EQ(rv, OK); // Can only come straight from above function. |
| 176 | 177 |
| 177 // The transition to STATE_REQUEST_READING occurs in the | 178 // The transition to STATE_REQUEST_READING occurs in the |
| 178 // OnResponseStarted() callback triggered by URLRequest::Start() | 179 // OnResponseStarted() callback triggered by URLRequest::Start() |
| (...skipping 21 matching lines...) Expand all Loading... |
| 200 int bytes_read = 0; | 201 int bytes_read = 0; |
| 201 if (!current_request_->Read(buffer_.get(), kBufferSize, &bytes_read)) { | 202 if (!current_request_->Read(buffer_.get(), kBufferSize, &bytes_read)) { |
| 202 if (current_request_->status().is_io_pending()) | 203 if (current_request_->status().is_io_pending()) |
| 203 return ERR_IO_PENDING; | 204 return ERR_IO_PENDING; |
| 204 | 205 |
| 205 if (current_request_->status().error() == OK) { | 206 if (current_request_->status().error() == OK) { |
| 206 // This "should never happen", but if it does the result will be | 207 // This "should never happen", but if it does the result will be |
| 207 // an infinite loop. It's not clear how to handle a read failure | 208 // an infinite loop. It's not clear how to handle a read failure |
| 208 // without a promise to invoke the callback at some point in the future, | 209 // without a promise to invoke the callback at some point in the future, |
| 209 // so the request is failed. | 210 // so the request is failed. |
| 210 SdchManager::SdchErrorRecovery(SdchManager::DICTIONARY_FETCH_READ_FAILED); | 211 SdchManager::SdchErrorRecovery(SDCH_DICTIONARY_FETCH_READ_FAILED); |
| 212 current_request_->net_log().AddEvent( |
| 213 NetLog::TYPE_SDCH_DICTIONARY_ERROR, |
| 214 base::Bind(&NetLogSdchDictionaryFetchProblemCallback, |
| 215 SDCH_DICTIONARY_FETCH_READ_FAILED, current_request_->url(), |
| 216 true)); |
| 211 DLOG(FATAL) | 217 DLOG(FATAL) |
| 212 << "URLRequest::Read() returned false without IO pending or error!"; | 218 << "URLRequest::Read() returned false without IO pending or error!"; |
| 213 return ERR_FAILED; | 219 return ERR_FAILED; |
| 214 } | 220 } |
| 215 | 221 |
| 216 return current_request_->status().error(); | 222 return current_request_->status().error(); |
| 217 } | 223 } |
| 218 | 224 |
| 219 if (bytes_read != 0) | 225 if (bytes_read != 0) |
| 220 dictionary_.append(buffer_->data(), bytes_read); | 226 dictionary_.append(buffer_->data(), bytes_read); |
| 221 else | 227 else |
| 222 next_state_ = STATE_REQUEST_COMPLETE; | 228 next_state_ = STATE_REQUEST_COMPLETE; |
| 223 | 229 |
| 224 return OK; | 230 return OK; |
| 225 } | 231 } |
| 226 | 232 |
| 227 int SdchDictionaryFetcher::DoCompleteRequest(int rv) { | 233 int SdchDictionaryFetcher::DoCompleteRequest(int rv) { |
| 228 DCHECK(CalledOnValidThread()); | 234 DCHECK(CalledOnValidThread()); |
| 229 | 235 |
| 230 // If the dictionary was successfully fetched, add it to the manager. | 236 // If the dictionary was successfully fetched, add it to the manager. |
| 231 if (rv == OK) | 237 if (rv == OK) { |
| 232 dictionary_fetched_callback_.Run(dictionary_, current_request_->url()); | 238 dictionary_fetched_callback_.Run(dictionary_, current_request_->url(), |
| 239 current_request_->net_log()); |
| 240 } |
| 233 | 241 |
| 234 current_request_.reset(); | 242 current_request_.reset(); |
| 235 buffer_ = NULL; | 243 buffer_ = NULL; |
| 236 dictionary_.clear(); | 244 dictionary_.clear(); |
| 237 | 245 |
| 238 next_state_ = STATE_IDLE; | 246 next_state_ = STATE_IDLE; |
| 239 | 247 |
| 240 return OK; | 248 return OK; |
| 241 } | 249 } |
| 242 | 250 |
| 243 } // namespace net | 251 } // namespace net |
| OLD | NEW |