| 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/profiler/scoped_tracker.h" | 12 #include "base/profiler/scoped_tracker.h" |
| 13 #include "base/thread_task_runner_handle.h" | 13 #include "base/thread_task_runner_handle.h" |
| 14 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
| 15 #include "net/base/load_flags.h" | 15 #include "net/base/load_flags.h" |
| 16 #include "net/base/sdch_net_log_params.h" | 16 #include "net/base/sdch_net_log_params.h" |
| 17 #include "net/http/http_response_headers.h" |
| 17 #include "net/url_request/url_request_context.h" | 18 #include "net/url_request/url_request_context.h" |
| 18 #include "net/url_request/url_request_status.h" | 19 #include "net/url_request/url_request_status.h" |
| 19 #include "net/url_request/url_request_throttler_manager.h" | 20 #include "net/url_request/url_request_throttler_manager.h" |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 23 const int kBufferSize = 4096; | 24 const int kBufferSize = 4096; |
| 24 | 25 |
| 25 } // namespace | 26 } // namespace |
| 26 | 27 |
| 27 namespace net { | 28 namespace net { |
| 28 | 29 |
| 29 SdchDictionaryFetcher::SdchDictionaryFetcher( | 30 SdchDictionaryFetcher::SdchDictionaryFetcher(URLRequestContext* context) |
| 30 URLRequestContext* context, | |
| 31 const OnDictionaryFetchedCallback& callback) | |
| 32 : next_state_(STATE_NONE), | 31 : next_state_(STATE_NONE), |
| 33 in_loop_(false), | 32 in_loop_(false), |
| 34 context_(context), | 33 context_(context), |
| 35 dictionary_fetched_callback_(callback), | |
| 36 weak_factory_(this) { | 34 weak_factory_(this) { |
| 37 DCHECK(CalledOnValidThread()); | 35 DCHECK(CalledOnValidThread()); |
| 38 DCHECK(context); | 36 DCHECK(context); |
| 39 } | 37 } |
| 40 | 38 |
| 41 SdchDictionaryFetcher::~SdchDictionaryFetcher() { | 39 SdchDictionaryFetcher::~SdchDictionaryFetcher() { |
| 42 DCHECK(CalledOnValidThread()); | |
| 43 } | 40 } |
| 44 | 41 |
| 45 void SdchDictionaryFetcher::Schedule(const GURL& dictionary_url) { | 42 void SdchDictionaryFetcher::Schedule( |
| 46 DCHECK(CalledOnValidThread()); | 43 const GURL& dictionary_url, |
| 44 const OnDictionaryFetchedCallback& callback) { |
| 45 ScheduleInternal(dictionary_url, false, callback); |
| 46 } |
| 47 | 47 |
| 48 // Avoid pushing duplicate copy onto queue. We may fetch this url again later | 48 void SdchDictionaryFetcher::ScheduleReload( |
| 49 // and get a different dictionary, but there is no reason to have it in the | 49 const GURL& dictionary_url, |
| 50 // queue twice at one time. | 50 const OnDictionaryFetchedCallback& callback) { |
| 51 if ((!fetch_queue_.empty() && fetch_queue_.back() == dictionary_url) || | 51 ScheduleInternal(dictionary_url, true, callback); |
| 52 attempted_load_.find(dictionary_url) != attempted_load_.end()) { | |
| 53 // TODO(rdsmith): log this error to the net log of the URLRequest | |
| 54 // initiating this fetch, once URLRequest will be passed here. | |
| 55 SdchManager::SdchErrorRecovery( | |
| 56 SDCH_DICTIONARY_PREVIOUSLY_SCHEDULED_TO_DOWNLOAD); | |
| 57 return; | |
| 58 } | |
| 59 | |
| 60 attempted_load_.insert(dictionary_url); | |
| 61 fetch_queue_.push(dictionary_url); | |
| 62 | |
| 63 // If the loop is already processing, it'll pick up the above in the | |
| 64 // normal course of events. | |
| 65 if (next_state_ != STATE_NONE) | |
| 66 return; | |
| 67 | |
| 68 next_state_ = STATE_IDLE; | |
| 69 | |
| 70 // There are no callbacks to user code from the dictionary fetcher, | |
| 71 // and Schedule() is only called from user code, so this call to DoLoop() | |
| 72 // does not require an |if (in_loop_) return;| guard. | |
| 73 DoLoop(OK); | |
| 74 } | 52 } |
| 75 | 53 |
| 76 void SdchDictionaryFetcher::Cancel() { | 54 void SdchDictionaryFetcher::Cancel() { |
| 77 DCHECK(CalledOnValidThread()); | 55 DCHECK(CalledOnValidThread()); |
| 78 | 56 |
| 79 next_state_ = STATE_NONE; | 57 next_state_ = STATE_NONE; |
| 58 current_request_.reset(); |
| 59 buffer_ = nullptr; |
| 60 current_callback_.Reset(); |
| 80 | 61 |
| 81 while (!fetch_queue_.empty()) | |
| 82 fetch_queue_.pop(); | |
| 83 attempted_load_.clear(); | 62 attempted_load_.clear(); |
| 84 weak_factory_.InvalidateWeakPtrs(); | 63 weak_factory_.InvalidateWeakPtrs(); |
| 85 current_request_.reset(NULL); | |
| 86 buffer_ = NULL; | |
| 87 dictionary_.clear(); | 64 dictionary_.clear(); |
| 88 } | 65 } |
| 89 | 66 |
| 90 void SdchDictionaryFetcher::OnResponseStarted(URLRequest* request) { | 67 void SdchDictionaryFetcher::OnResponseStarted(URLRequest* request) { |
| 91 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed. | 68 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed. |
| 92 tracked_objects::ScopedTracker tracking_profile( | 69 tracked_objects::ScopedTracker tracking_profile( |
| 93 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 70 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 94 "423948 SdchDictionaryFetcher::OnResponseStarted")); | 71 "423948 SdchDictionaryFetcher::OnResponseStarted")); |
| 95 | 72 |
| 96 DCHECK(CalledOnValidThread()); | 73 DCHECK(CalledOnValidThread()); |
| 97 DCHECK_EQ(request, current_request_.get()); | 74 DCHECK_EQ(request, current_request_.get()); |
| 98 DCHECK_EQ(next_state_, STATE_REQUEST_STARTED); | 75 DCHECK_EQ(next_state_, STATE_REQUEST_STARTED); |
| 99 | 76 |
| 77 // Confirm that the response isn't a stale read from the cache (as |
| 78 // may happen in the reload case). If the response was not retrieved over |
| 79 // HTTP, it is presumed to be fresh. |
| 80 HttpResponseHeaders* response_headers = request->response_headers(); |
| 81 if (response_headers) { |
| 82 ValidationType validation_type = response_headers->RequiresValidation( |
| 83 request->response_info().request_time, |
| 84 request->response_info().response_time, base::Time::Now()); |
| 85 // TODO(rdsmith): Maybe handle VALIDATION_ASYNCHRONOUS by queueing |
| 86 // a non-reload request for the dictionary. |
| 87 if (validation_type != VALIDATION_NONE) { |
| 88 // Stale entry; drop it on the floor. |
| 89 ResetRequest(); |
| 90 return; |
| 91 } |
| 92 } |
| 93 |
| 100 // The response has started, so the stream can be read from. | 94 // The response has started, so the stream can be read from. |
| 101 next_state_ = STATE_REQUEST_READING; | 95 next_state_ = STATE_REQUEST_READING; |
| 102 | 96 |
| 103 // If this function was synchronously called, the containing | 97 // If this function was synchronously called, the containing |
| 104 // state machine loop will handle the state transition. Otherwise, | 98 // state machine loop will handle the state transition. Otherwise, |
| 105 // restart the state machine loop. | 99 // restart the state machine loop. |
| 106 if (in_loop_) | 100 if (in_loop_) |
| 107 return; | 101 return; |
| 108 | 102 |
| 109 DoLoop(request->status().error()); | 103 DoLoop(request->status().error()); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 128 | 122 |
| 129 // If this function was synchronously called, the containing | 123 // If this function was synchronously called, the containing |
| 130 // state machine loop will handle the state transition. Otherwise, | 124 // state machine loop will handle the state transition. Otherwise, |
| 131 // restart the state machine loop. | 125 // restart the state machine loop. |
| 132 if (in_loop_) | 126 if (in_loop_) |
| 133 return; | 127 return; |
| 134 | 128 |
| 135 DoLoop(request->status().error()); | 129 DoLoop(request->status().error()); |
| 136 } | 130 } |
| 137 | 131 |
| 132 SdchDictionaryFetcher::QueuedInfo::QueuedInfo() |
| 133 : download_only_from_cache(false) { |
| 134 } |
| 135 |
| 136 SdchDictionaryFetcher::QueuedInfo::QueuedInfo( |
| 137 const GURL& url, |
| 138 bool download_only_from_cache, |
| 139 const OnDictionaryFetchedCallback& callback) |
| 140 : url(url), |
| 141 download_only_from_cache(download_only_from_cache), |
| 142 callback(callback) { |
| 143 } |
| 144 |
| 145 SdchDictionaryFetcher::QueuedInfo::~QueuedInfo() { |
| 146 } |
| 147 |
| 148 void SdchDictionaryFetcher::ScheduleInternal( |
| 149 const GURL& dictionary_url, |
| 150 bool reload, |
| 151 const OnDictionaryFetchedCallback& callback) { |
| 152 DCHECK(CalledOnValidThread()); |
| 153 |
| 154 // Avoid pushing duplicate copy onto queue. We may fetch this url again later |
| 155 // and get a different dictionary, but there is no reason to have it in the |
| 156 // queue twice at one time. |
| 157 if ((!fetch_queue_.empty() && fetch_queue_.back().url == dictionary_url) || |
| 158 attempted_load_.find(dictionary_url) != attempted_load_.end()) { |
| 159 // TODO(rdsmith): Log this error to the net log. In the case of a |
| 160 // normal fetch, this can be through the URLRequest |
| 161 // initiating this fetch (once the URLRequest is passed to the fetcher); |
| 162 // in the case of a reload, it's more complicated. |
| 163 SdchManager::SdchErrorRecovery( |
| 164 SDCH_DICTIONARY_PREVIOUSLY_SCHEDULED_TO_DOWNLOAD); |
| 165 return; |
| 166 } |
| 167 |
| 168 if (!reload) |
| 169 attempted_load_.insert(dictionary_url); |
| 170 fetch_queue_.push(QueuedInfo(dictionary_url, reload, callback)); |
| 171 |
| 172 // If the loop is already processing, it'll pick up the above in the |
| 173 // normal course of events. |
| 174 if (next_state_ != STATE_NONE) |
| 175 return; |
| 176 |
| 177 next_state_ = STATE_IDLE; |
| 178 |
| 179 // There are no callbacks to user code from the dictionary fetcher, |
| 180 // and Schedule() is only called from user code, so this call to DoLoop() |
| 181 // does not require an |if (in_loop_) return;| guard. |
| 182 DoLoop(OK); |
| 183 } |
| 184 |
| 185 void SdchDictionaryFetcher::ResetRequest() { |
| 186 current_request_.reset(); |
| 187 buffer_ = nullptr; |
| 188 current_callback_.Reset(); |
| 189 next_state_ = STATE_IDLE; |
| 190 dictionary_.clear(); |
| 191 |
| 192 if (!in_loop_) |
| 193 DoLoop(OK); |
| 194 return; |
| 195 } |
| 196 |
| 138 int SdchDictionaryFetcher::DoLoop(int rv) { | 197 int SdchDictionaryFetcher::DoLoop(int rv) { |
| 139 DCHECK(!in_loop_); | 198 DCHECK(!in_loop_); |
| 140 base::AutoReset<bool> auto_reset_in_loop(&in_loop_, true); | 199 base::AutoReset<bool> auto_reset_in_loop(&in_loop_, true); |
| 141 | 200 |
| 142 do { | 201 do { |
| 143 State state = next_state_; | 202 State state = next_state_; |
| 144 next_state_ = STATE_NONE; | 203 next_state_ = STATE_NONE; |
| 145 switch (state) { | 204 switch (state) { |
| 146 case STATE_IDLE: | 205 case STATE_IDLE: |
| 147 rv = DoDispatchRequest(rv); | 206 rv = DoDispatchRequest(rv); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 168 | 227 |
| 169 // |rv| is ignored, as the result from the previous request doesn't | 228 // |rv| is ignored, as the result from the previous request doesn't |
| 170 // affect the next request. | 229 // affect the next request. |
| 171 | 230 |
| 172 if (fetch_queue_.empty() || current_request_.get()) { | 231 if (fetch_queue_.empty() || current_request_.get()) { |
| 173 next_state_ = STATE_NONE; | 232 next_state_ = STATE_NONE; |
| 174 return OK; | 233 return OK; |
| 175 } | 234 } |
| 176 | 235 |
| 177 current_request_ = | 236 current_request_ = |
| 178 context_->CreateRequest(fetch_queue_.front(), IDLE, this, NULL); | 237 context_->CreateRequest(fetch_queue_.front().url, IDLE, this, NULL); |
| 179 current_request_->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES | | 238 current_request_->SetLoadFlags( |
| 180 LOAD_DO_NOT_SAVE_COOKIES); | 239 LOAD_DO_NOT_SEND_COOKIES | LOAD_DO_NOT_SAVE_COOKIES | |
| 240 (fetch_queue_.front().download_only_from_cache ? LOAD_ONLY_FROM_CACHE |
| 241 : 0)); |
| 242 |
| 181 buffer_ = new IOBuffer(kBufferSize); | 243 buffer_ = new IOBuffer(kBufferSize); |
| 244 current_callback_ = fetch_queue_.front().callback; |
| 182 fetch_queue_.pop(); | 245 fetch_queue_.pop(); |
| 183 | 246 |
| 184 next_state_ = STATE_REQUEST_STARTED; | 247 next_state_ = STATE_REQUEST_STARTED; |
| 185 current_request_->Start(); | 248 current_request_->Start(); |
| 186 current_request_->net_log().AddEvent(NetLog::TYPE_SDCH_DICTIONARY_FETCH); | 249 current_request_->net_log().AddEvent(NetLog::TYPE_SDCH_DICTIONARY_FETCH); |
| 187 | 250 |
| 188 return OK; | 251 return OK; |
| 189 } | 252 } |
| 190 | 253 |
| 191 int SdchDictionaryFetcher::DoRequestStarted(int rv) { | 254 int SdchDictionaryFetcher::DoRequestStarted(int rv) { |
| 192 DCHECK(CalledOnValidThread()); | 255 DCHECK(CalledOnValidThread()); |
| 193 DCHECK_EQ(rv, OK); // Can only come straight from above function. | 256 DCHECK_EQ(rv, OK); // Can only come straight from above function. |
| 194 | 257 |
| 195 // The transition to STATE_REQUEST_READING occurs in the | 258 // The transition to STATE_REQUEST_READING occurs in the |
| 196 // OnResponseStarted() callback triggered by URLRequest::Start() | 259 // OnResponseStarted() callback triggered by URLRequest::Start() |
| 197 // (called in DoDispatchRequest(), above). If that callback did not | 260 // (called in DoDispatchRequest(), above). If that callback did not |
| 198 // occur synchronously, this routine is executed; it returns ERR_IO_PENDING, | 261 // occur synchronously, this routine is executed; it returns ERR_IO_PENDING, |
| 199 // indicating to the controlling loop that no further work should be done | 262 // indicating to the controlling loop that no further work should be done |
| 200 // until the callback occurs (which will re-invoke DoLoop()). | 263 // until the callback occurs (which will re-invoke DoLoop()). |
| 201 next_state_ = STATE_REQUEST_STARTED; | 264 next_state_ = STATE_REQUEST_STARTED; |
| 202 return ERR_IO_PENDING; | 265 return ERR_IO_PENDING; |
| 203 } | 266 } |
| 204 | 267 |
| 205 int SdchDictionaryFetcher::DoRead(int rv) { | 268 int SdchDictionaryFetcher::DoRead(int rv) { |
| 206 DCHECK(CalledOnValidThread()); | 269 DCHECK(CalledOnValidThread()); |
| 207 | 270 |
| 208 // If there's been an error, abort the current request. | 271 // If there's been an error, abort the current request. |
| 209 if (rv != OK) { | 272 if (rv != OK) { |
| 210 current_request_.reset(); | 273 ResetRequest(); |
| 211 buffer_ = NULL; | |
| 212 next_state_ = STATE_IDLE; | |
| 213 | |
| 214 return OK; | 274 return OK; |
| 215 } | 275 } |
| 216 | 276 |
| 217 next_state_ = STATE_REQUEST_READING; | 277 next_state_ = STATE_REQUEST_READING; |
| 218 int bytes_read = 0; | 278 int bytes_read = 0; |
| 219 current_request_->Read(buffer_.get(), kBufferSize, &bytes_read); | 279 current_request_->Read(buffer_.get(), kBufferSize, &bytes_read); |
| 220 if (current_request_->status().is_io_pending()) | 280 if (current_request_->status().is_io_pending()) |
| 221 return ERR_IO_PENDING; | 281 return ERR_IO_PENDING; |
| 222 | 282 |
| 223 if (bytes_read < 0 || !current_request_->status().is_success()) { | 283 if (bytes_read < 0 || !current_request_->status().is_success()) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 241 dictionary_.append(buffer_->data(), bytes_read); | 301 dictionary_.append(buffer_->data(), bytes_read); |
| 242 | 302 |
| 243 return OK; | 303 return OK; |
| 244 } | 304 } |
| 245 | 305 |
| 246 int SdchDictionaryFetcher::DoCompleteRequest(int rv) { | 306 int SdchDictionaryFetcher::DoCompleteRequest(int rv) { |
| 247 DCHECK(CalledOnValidThread()); | 307 DCHECK(CalledOnValidThread()); |
| 248 | 308 |
| 249 // If the dictionary was successfully fetched, add it to the manager. | 309 // If the dictionary was successfully fetched, add it to the manager. |
| 250 if (rv == OK) { | 310 if (rv == OK) { |
| 251 dictionary_fetched_callback_.Run(dictionary_, current_request_->url(), | 311 current_callback_.Run(dictionary_, current_request_->url(), |
| 252 current_request_->net_log()); | 312 current_request_->net_log()); |
| 253 } | 313 } |
| 254 | 314 |
| 255 current_request_.reset(); | 315 ResetRequest(); |
| 256 buffer_ = NULL; | |
| 257 dictionary_.clear(); | |
| 258 | |
| 259 next_state_ = STATE_IDLE; | |
| 260 | |
| 261 return OK; | 316 return OK; |
| 262 } | 317 } |
| 263 | 318 |
| 264 } // namespace net | 319 } // namespace net |
| OLD | NEW |