Chromium Code Reviews| 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/url_request/url_request_context.h" | 17 #include "net/url_request/url_request_context.h" |
| 18 #include "net/url_request/url_request_status.h" | 18 #include "net/url_request/url_request_status.h" |
| 19 #include "net/url_request/url_request_throttler_manager.h" | 19 #include "net/url_request/url_request_throttler_manager.h" |
| 20 | 20 |
| 21 namespace net { | |
| 22 | |
| 21 namespace { | 23 namespace { |
| 22 | 24 |
| 23 const int kBufferSize = 4096; | 25 const int kBufferSize = 4096; |
| 24 | 26 |
| 27 // Map the bytes_read result from a read attempt and a URLRequest's | |
| 28 // status into a single net return value. | |
| 29 int GetReadResult(int bytes_read, const URLRequest* request) { | |
| 30 int rv = request->status().error(); | |
| 31 if (request->status().is_success() && bytes_read < 0) { | |
| 32 rv = ERR_FAILED; | |
| 33 request->net_log().AddEventWithNetErrorCode( | |
| 34 NetLog::TYPE_SDCH_DICTIONARY_FETCH_IMPLIED_ERROR, rv); | |
| 35 } | |
| 36 | |
| 37 if (rv == OK) | |
| 38 rv = bytes_read; | |
| 39 | |
| 40 return rv; | |
| 41 } | |
| 42 | |
| 25 } // namespace | 43 } // namespace |
| 26 | 44 |
| 27 namespace net { | |
| 28 | |
| 29 SdchDictionaryFetcher::SdchDictionaryFetcher( | 45 SdchDictionaryFetcher::SdchDictionaryFetcher( |
| 30 URLRequestContext* context, | 46 URLRequestContext* context, |
| 31 const OnDictionaryFetchedCallback& callback) | 47 const OnDictionaryFetchedCallback& callback) |
| 32 : next_state_(STATE_NONE), | 48 : next_state_(STATE_NONE), |
| 33 in_loop_(false), | 49 in_loop_(false), |
| 34 context_(context), | 50 context_(context), |
| 35 dictionary_fetched_callback_(callback), | 51 dictionary_fetched_callback_(callback), |
| 36 weak_factory_(this) { | 52 weak_factory_(this) { |
| 37 DCHECK(CalledOnValidThread()); | 53 DCHECK(CalledOnValidThread()); |
| 38 DCHECK(context); | 54 DCHECK(context); |
| 39 } | 55 } |
| 40 | 56 |
| 41 SdchDictionaryFetcher::~SdchDictionaryFetcher() { | 57 SdchDictionaryFetcher::~SdchDictionaryFetcher() { |
| 42 DCHECK(CalledOnValidThread()); | 58 DCHECK(CalledOnValidThread()); |
| 43 } | 59 } |
| 44 | 60 |
| 45 void SdchDictionaryFetcher::Schedule(const GURL& dictionary_url) { | 61 void SdchDictionaryFetcher::Schedule(const GURL& dictionary_url) { |
| 46 DCHECK(CalledOnValidThread()); | 62 DCHECK(CalledOnValidThread()); |
| 47 | 63 |
| 48 // Avoid pushing duplicate copy onto queue. We may fetch this url again later | 64 // Avoid pushing duplicate copy onto queue. We may fetch this url again later |
| 49 // and get a different dictionary, but there is no reason to have it in the | 65 // and get a different dictionary, but there is no reason to have it in the |
| 50 // queue twice at one time. | 66 // queue twice at one time. |
| 51 if ((!fetch_queue_.empty() && fetch_queue_.back() == dictionary_url) || | 67 if ((!fetch_queue_.empty() && fetch_queue_.back() == dictionary_url) || |
|
mmenke
2015/01/30 16:20:43
Need to include the GURL header.
Randy Smith (Not in Mondays)
2015/01/30 19:21:58
Done in header file.
| |
| 52 attempted_load_.find(dictionary_url) != attempted_load_.end()) { | 68 attempted_load_.find(dictionary_url) != attempted_load_.end()) { |
| 53 // TODO(rdsmith): log this error to the net log of the URLRequest | 69 // TODO(rdsmith): log this error to the net log of the URLRequest |
| 54 // initiating this fetch, once URLRequest will be passed here. | 70 // initiating this fetch, once URLRequest will be passed here. |
| 55 SdchManager::SdchErrorRecovery( | 71 SdchManager::SdchErrorRecovery( |
| 56 SDCH_DICTIONARY_PREVIOUSLY_SCHEDULED_TO_DOWNLOAD); | 72 SDCH_DICTIONARY_PREVIOUSLY_SCHEDULED_TO_DOWNLOAD); |
| 57 return; | 73 return; |
| 58 } | 74 } |
| 59 | 75 |
| 60 attempted_load_.insert(dictionary_url); | 76 attempted_load_.insert(dictionary_url); |
| 61 fetch_queue_.push(dictionary_url); | 77 fetch_queue_.push(dictionary_url); |
| 62 | 78 |
| 63 // If the loop is already processing, it'll pick up the above in the | 79 // If the loop is already processing, it'll pick up the above in the |
| 64 // normal course of events. | 80 // normal course of events. |
| 65 if (next_state_ != STATE_NONE) | 81 if (next_state_ != STATE_NONE) |
| 66 return; | 82 return; |
| 67 | 83 |
| 68 next_state_ = STATE_IDLE; | 84 next_state_ = STATE_SEND_REQUEST; |
| 69 | 85 |
| 70 // There are no callbacks to user code from the dictionary fetcher, | 86 // 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() | 87 // and Schedule() is only called from user code, so this call to DoLoop() |
| 72 // does not require an |if (in_loop_) return;| guard. | 88 // does not require an |if (in_loop_) return;| guard. |
| 73 DoLoop(OK); | 89 DoLoop(OK); |
| 74 } | 90 } |
| 75 | 91 |
| 76 void SdchDictionaryFetcher::Cancel() { | 92 void SdchDictionaryFetcher::Cancel() { |
| 77 DCHECK(CalledOnValidThread()); | 93 DCHECK(CalledOnValidThread()); |
| 78 | 94 |
| 79 next_state_ = STATE_NONE; | 95 next_state_ = STATE_NONE; |
| 80 | 96 |
| 81 while (!fetch_queue_.empty()) | 97 while (!fetch_queue_.empty()) |
| 82 fetch_queue_.pop(); | 98 fetch_queue_.pop(); |
| 83 attempted_load_.clear(); | 99 attempted_load_.clear(); |
| 84 weak_factory_.InvalidateWeakPtrs(); | 100 weak_factory_.InvalidateWeakPtrs(); |
| 85 current_request_.reset(NULL); | 101 current_request_.reset(NULL); |
| 86 buffer_ = NULL; | 102 buffer_ = NULL; |
| 87 dictionary_.clear(); | 103 dictionary_.clear(); |
| 88 } | 104 } |
| 89 | 105 |
| 90 void SdchDictionaryFetcher::OnResponseStarted(URLRequest* request) { | 106 void SdchDictionaryFetcher::OnResponseStarted(URLRequest* request) { |
| 91 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed. | 107 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed. |
| 92 tracked_objects::ScopedTracker tracking_profile( | 108 tracked_objects::ScopedTracker tracking_profile( |
| 93 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 109 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 94 "423948 SdchDictionaryFetcher::OnResponseStarted")); | 110 "423948 SdchDictionaryFetcher::OnResponseStarted")); |
| 95 | 111 |
| 96 DCHECK(CalledOnValidThread()); | 112 DCHECK(CalledOnValidThread()); |
| 97 DCHECK_EQ(request, current_request_.get()); | 113 DCHECK_EQ(request, current_request_.get()); |
| 98 DCHECK_EQ(next_state_, STATE_REQUEST_STARTED); | 114 DCHECK_EQ(next_state_, STATE_SEND_REQUEST_COMPLETE); |
| 99 | 115 DCHECK(!in_loop_); |
| 100 // The response has started, so the stream can be read from. | |
| 101 next_state_ = STATE_REQUEST_READING; | |
| 102 | |
| 103 // If this function was synchronously called, the containing | |
| 104 // state machine loop will handle the state transition. Otherwise, | |
| 105 // restart the state machine loop. | |
| 106 if (in_loop_) | |
| 107 return; | |
| 108 | 116 |
| 109 DoLoop(request->status().error()); | 117 DoLoop(request->status().error()); |
| 110 } | 118 } |
| 111 | 119 |
| 112 void SdchDictionaryFetcher::OnReadCompleted(URLRequest* request, | 120 void SdchDictionaryFetcher::OnReadCompleted(URLRequest* request, |
| 113 int bytes_read) { | 121 int bytes_read) { |
| 114 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed. | 122 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed. |
| 115 tracked_objects::ScopedTracker tracking_profile( | 123 tracked_objects::ScopedTracker tracking_profile( |
| 116 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 124 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 117 "423948 SdchDictionaryFetcher::OnReadCompleted")); | 125 "423948 SdchDictionaryFetcher::OnReadCompleted")); |
| 118 | 126 |
| 119 DCHECK(CalledOnValidThread()); | 127 DCHECK(CalledOnValidThread()); |
| 120 DCHECK_EQ(request, current_request_.get()); | 128 DCHECK_EQ(request, current_request_.get()); |
| 121 DCHECK_EQ(next_state_, STATE_REQUEST_READING); | 129 DCHECK_EQ(next_state_, STATE_READ_BODY_COMPLETE); |
| 130 DCHECK(!in_loop_); | |
| 122 | 131 |
| 123 // No state transition is required in this function; the | 132 DoLoop(GetReadResult(bytes_read, current_request_.get())); |
| 124 // completion of the request is detected in DoRead(). | |
| 125 | |
| 126 if (request->status().is_success()) | |
| 127 dictionary_.append(buffer_->data(), bytes_read); | |
| 128 | |
| 129 // If this function was synchronously called, the containing | |
| 130 // state machine loop will handle the state transition. Otherwise, | |
| 131 // restart the state machine loop. | |
| 132 if (in_loop_) | |
| 133 return; | |
| 134 | |
| 135 DoLoop(request->status().error()); | |
| 136 } | 133 } |
| 137 | 134 |
| 138 int SdchDictionaryFetcher::DoLoop(int rv) { | 135 int SdchDictionaryFetcher::DoLoop(int rv) { |
| 139 DCHECK(!in_loop_); | 136 DCHECK(!in_loop_); |
| 140 base::AutoReset<bool> auto_reset_in_loop(&in_loop_, true); | 137 base::AutoReset<bool> auto_reset_in_loop(&in_loop_, true); |
| 141 | 138 |
| 142 do { | 139 do { |
| 143 State state = next_state_; | 140 State state = next_state_; |
| 144 next_state_ = STATE_NONE; | 141 next_state_ = STATE_NONE; |
| 145 switch (state) { | 142 switch (state) { |
| 146 case STATE_IDLE: | 143 case STATE_SEND_REQUEST: |
| 147 rv = DoDispatchRequest(rv); | 144 rv = DoSendRequest(rv); |
| 148 break; | 145 break; |
| 149 case STATE_REQUEST_STARTED: | 146 case STATE_SEND_REQUEST_COMPLETE: |
| 150 rv = DoRequestStarted(rv); | 147 rv = DoSendRequestComplete(rv); |
| 151 break; | 148 break; |
| 152 case STATE_REQUEST_READING: | 149 case STATE_READ_BODY: |
| 153 rv = DoRead(rv); | 150 rv = DoReadBody(rv); |
| 151 break; | |
| 152 case STATE_READ_BODY_COMPLETE: | |
| 153 rv = DoReadBodyComplete(rv); | |
| 154 break; | 154 break; |
| 155 case STATE_REQUEST_COMPLETE: | 155 case STATE_REQUEST_COMPLETE: |
| 156 rv = DoCompleteRequest(rv); | 156 rv = DoCompleteRequest(rv); |
| 157 break; | 157 break; |
| 158 case STATE_NONE: | 158 case STATE_NONE: |
| 159 NOTREACHED(); | 159 NOTREACHED(); |
| 160 } | 160 } |
| 161 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); | 161 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); |
| 162 | 162 |
| 163 return rv; | 163 return rv; |
| 164 } | 164 } |
| 165 | 165 |
| 166 int SdchDictionaryFetcher::DoDispatchRequest(int rv) { | 166 int SdchDictionaryFetcher::DoSendRequest(int rv) { |
| 167 DCHECK(CalledOnValidThread()); | 167 DCHECK(CalledOnValidThread()); |
| 168 | 168 |
| 169 // |rv| is ignored, as the result from the previous request doesn't | 169 // |rv| is ignored, as the result from the previous request doesn't |
| 170 // affect the next request. | 170 // affect the next request. |
| 171 | 171 |
| 172 if (fetch_queue_.empty() || current_request_.get()) { | 172 if (fetch_queue_.empty() || current_request_.get()) { |
| 173 next_state_ = STATE_NONE; | 173 next_state_ = STATE_NONE; |
| 174 return OK; | 174 return OK; |
| 175 } | 175 } |
| 176 | 176 |
| 177 next_state_ = STATE_SEND_REQUEST_COMPLETE; | |
| 178 | |
| 177 current_request_ = | 179 current_request_ = |
| 178 context_->CreateRequest(fetch_queue_.front(), IDLE, this, NULL); | 180 context_->CreateRequest(fetch_queue_.front(), IDLE, this, NULL); |
| 179 current_request_->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES | | 181 current_request_->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES | |
| 180 LOAD_DO_NOT_SAVE_COOKIES); | 182 LOAD_DO_NOT_SAVE_COOKIES); |
| 181 buffer_ = new IOBuffer(kBufferSize); | 183 buffer_ = new IOBuffer(kBufferSize); |
| 182 fetch_queue_.pop(); | 184 fetch_queue_.pop(); |
| 183 | 185 |
| 184 next_state_ = STATE_REQUEST_STARTED; | |
| 185 current_request_->Start(); | 186 current_request_->Start(); |
| 186 current_request_->net_log().AddEvent(NetLog::TYPE_SDCH_DICTIONARY_FETCH); | 187 current_request_->net_log().AddEvent(NetLog::TYPE_SDCH_DICTIONARY_FETCH); |
| 187 | 188 |
| 188 return OK; | |
| 189 } | |
| 190 | |
| 191 int SdchDictionaryFetcher::DoRequestStarted(int rv) { | |
| 192 DCHECK(CalledOnValidThread()); | |
| 193 DCHECK_EQ(rv, OK); // Can only come straight from above function. | |
| 194 | |
| 195 // The transition to STATE_REQUEST_READING occurs in the | |
| 196 // OnResponseStarted() callback triggered by URLRequest::Start() | |
| 197 // (called in DoDispatchRequest(), above). If that callback did not | |
| 198 // occur synchronously, this routine is executed; it returns ERR_IO_PENDING, | |
| 199 // indicating to the controlling loop that no further work should be done | |
| 200 // until the callback occurs (which will re-invoke DoLoop()). | |
| 201 next_state_ = STATE_REQUEST_STARTED; | |
| 202 return ERR_IO_PENDING; | 189 return ERR_IO_PENDING; |
| 203 } | 190 } |
| 204 | 191 |
| 205 int SdchDictionaryFetcher::DoRead(int rv) { | 192 int SdchDictionaryFetcher::DoSendRequestComplete(int rv) { |
| 206 DCHECK(CalledOnValidThread()); | 193 DCHECK(CalledOnValidThread()); |
| 207 | 194 |
| 208 // If there's been an error, abort the current request. | 195 // If there's been an error, abort the current request. |
| 209 if (rv != OK) { | 196 if (rv != OK) { |
| 210 current_request_.reset(); | 197 current_request_.reset(); |
| 211 buffer_ = NULL; | 198 buffer_ = NULL; |
| 212 next_state_ = STATE_IDLE; | 199 next_state_ = STATE_SEND_REQUEST; |
| 213 | 200 |
| 214 return OK; | 201 return OK; |
| 215 } | 202 } |
| 216 | 203 |
| 217 next_state_ = STATE_REQUEST_READING; | 204 next_state_ = STATE_READ_BODY; |
| 205 return OK; | |
| 206 } | |
| 207 | |
| 208 int SdchDictionaryFetcher::DoReadBody(int rv) { | |
| 209 DCHECK(CalledOnValidThread()); | |
| 210 | |
| 211 // If there's been an error, abort the current request. | |
| 212 if (rv != OK) { | |
| 213 current_request_.reset(); | |
| 214 buffer_ = NULL; | |
| 215 next_state_ = STATE_SEND_REQUEST; | |
| 216 | |
| 217 return OK; | |
| 218 } | |
| 219 | |
| 220 next_state_ = STATE_READ_BODY_COMPLETE; | |
| 218 int bytes_read = 0; | 221 int bytes_read = 0; |
| 219 current_request_->Read(buffer_.get(), kBufferSize, &bytes_read); | 222 current_request_->Read(buffer_.get(), kBufferSize, &bytes_read); |
| 220 if (current_request_->status().is_io_pending()) | 223 if (current_request_->status().is_io_pending()) |
| 221 return ERR_IO_PENDING; | 224 return ERR_IO_PENDING; |
| 222 | 225 |
| 223 if (bytes_read < 0 || !current_request_->status().is_success()) { | 226 return GetReadResult(bytes_read, current_request_.get()); |
| 224 if (current_request_->status().error() != OK) | 227 } |
| 225 return current_request_->status().error(); | |
| 226 | 228 |
| 227 // An error with request status of OK should not happen, | 229 int SdchDictionaryFetcher::DoReadBodyComplete(int rv) { |
| 228 // but there's enough machinery underneath URLRequest::Read() | 230 DCHECK(CalledOnValidThread()); |
| 229 // that this routine checks for that case. | 231 |
| 230 net::Error error = | 232 // An error; abort the current request. |
| 231 current_request_->status().status() == URLRequestStatus::CANCELED ? | 233 if (rv < 0) { |
| 232 ERR_ABORTED : ERR_FAILED; | 234 current_request_.reset(); |
| 233 current_request_->net_log().AddEventWithNetErrorCode( | 235 buffer_ = NULL; |
| 234 NetLog::TYPE_SDCH_DICTIONARY_FETCH_IMPLIED_ERROR, error); | 236 next_state_ = STATE_SEND_REQUEST; |
| 235 return error; | 237 return OK; |
| 236 } | 238 } |
| 237 | 239 |
| 238 if (bytes_read == 0) | 240 DCHECK(current_request_->status().is_success()); |
| 239 next_state_ = STATE_REQUEST_COMPLETE; | |
| 240 else | |
| 241 dictionary_.append(buffer_->data(), bytes_read); | |
| 242 | 241 |
| 242 // Data; append to the dictionary and look for more data. | |
| 243 if (rv > 0) { | |
| 244 dictionary_.append(buffer_->data(), rv); | |
| 245 next_state_ = STATE_READ_BODY; | |
| 246 return OK; | |
| 247 } | |
| 248 | |
| 249 // End of file; complete the request. | |
| 250 next_state_ = STATE_REQUEST_COMPLETE; | |
| 243 return OK; | 251 return OK; |
| 244 } | 252 } |
| 245 | 253 |
| 246 int SdchDictionaryFetcher::DoCompleteRequest(int rv) { | 254 int SdchDictionaryFetcher::DoCompleteRequest(int rv) { |
| 247 DCHECK(CalledOnValidThread()); | 255 DCHECK(CalledOnValidThread()); |
| 248 | 256 |
| 249 // If the dictionary was successfully fetched, add it to the manager. | 257 // DoReadBodyComplete() only transitions to this state |
| 250 if (rv == OK) { | 258 // on success. |
| 251 dictionary_fetched_callback_.Run(dictionary_, current_request_->url(), | 259 DCHECK_EQ(OK, rv); |
| 252 current_request_->net_log()); | |
| 253 } | |
| 254 | 260 |
| 261 dictionary_fetched_callback_.Run(dictionary_, current_request_->url(), | |
| 262 current_request_->net_log()); | |
| 255 current_request_.reset(); | 263 current_request_.reset(); |
| 256 buffer_ = NULL; | 264 buffer_ = NULL; |
| 257 dictionary_.clear(); | 265 dictionary_.clear(); |
| 258 | 266 |
| 259 next_state_ = STATE_IDLE; | 267 next_state_ = STATE_SEND_REQUEST; |
| 260 | 268 |
| 261 return OK; | 269 return OK; |
| 262 } | 270 } |
| 263 | 271 |
| 264 } // namespace net | 272 } // namespace net |
| OLD | NEW |