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/thread_task_runner_handle.h" | 13 #include "base/thread_task_runner_handle.h" |
13 #include "net/base/load_flags.h" | 14 #include "net/base/load_flags.h" |
14 #include "net/base/sdch_net_log_params.h" | 15 #include "net/base/sdch_net_log_params.h" |
15 #include "net/url_request/url_request_context.h" | 16 #include "net/url_request/url_request_context.h" |
16 #include "net/url_request/url_request_status.h" | 17 #include "net/url_request/url_request_status.h" |
17 #include "net/url_request/url_request_throttler_manager.h" | 18 #include "net/url_request/url_request_throttler_manager.h" |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 const int kBufferSize = 4096; | 22 const int kBufferSize = 4096; |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 while (!fetch_queue_.empty()) | 75 while (!fetch_queue_.empty()) |
75 fetch_queue_.pop(); | 76 fetch_queue_.pop(); |
76 attempted_load_.clear(); | 77 attempted_load_.clear(); |
77 weak_factory_.InvalidateWeakPtrs(); | 78 weak_factory_.InvalidateWeakPtrs(); |
78 current_request_.reset(NULL); | 79 current_request_.reset(NULL); |
79 buffer_ = NULL; | 80 buffer_ = NULL; |
80 dictionary_.clear(); | 81 dictionary_.clear(); |
81 } | 82 } |
82 | 83 |
83 void SdchDictionaryFetcher::OnResponseStarted(URLRequest* request) { | 84 void SdchDictionaryFetcher::OnResponseStarted(URLRequest* request) { |
| 85 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed. |
| 86 tracked_objects::ScopedTracker tracking_profile( |
| 87 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 88 "423948 SdchDictionaryFetcher::OnResponseStarted")); |
| 89 |
84 DCHECK(CalledOnValidThread()); | 90 DCHECK(CalledOnValidThread()); |
85 DCHECK_EQ(request, current_request_.get()); | 91 DCHECK_EQ(request, current_request_.get()); |
86 DCHECK_EQ(next_state_, STATE_REQUEST_STARTED); | 92 DCHECK_EQ(next_state_, STATE_REQUEST_STARTED); |
87 | 93 |
88 // The response has started, so the stream can be read from. | 94 // The response has started, so the stream can be read from. |
89 next_state_ = STATE_REQUEST_READING; | 95 next_state_ = STATE_REQUEST_READING; |
90 | 96 |
91 // If this function was synchronously called, the containing | 97 // If this function was synchronously called, the containing |
92 // state machine loop will handle the state transition. Otherwise, | 98 // state machine loop will handle the state transition. Otherwise, |
93 // restart the state machine loop. | 99 // restart the state machine loop. |
94 if (in_loop_) | 100 if (in_loop_) |
95 return; | 101 return; |
96 | 102 |
97 DoLoop(request->status().error()); | 103 DoLoop(request->status().error()); |
98 } | 104 } |
99 | 105 |
100 void SdchDictionaryFetcher::OnReadCompleted(URLRequest* request, | 106 void SdchDictionaryFetcher::OnReadCompleted(URLRequest* request, |
101 int bytes_read) { | 107 int bytes_read) { |
| 108 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed. |
| 109 tracked_objects::ScopedTracker tracking_profile( |
| 110 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 111 "423948 SdchDictionaryFetcher::OnReadCompleted")); |
| 112 |
102 DCHECK(CalledOnValidThread()); | 113 DCHECK(CalledOnValidThread()); |
103 DCHECK_EQ(request, current_request_.get()); | 114 DCHECK_EQ(request, current_request_.get()); |
104 DCHECK_EQ(next_state_, STATE_REQUEST_READING); | 115 DCHECK_EQ(next_state_, STATE_REQUEST_READING); |
105 | 116 |
106 // No state transition is required in this function; the | 117 // No state transition is required in this function; the |
107 // completion of the request is detected in DoRead(). | 118 // completion of the request is detected in DoRead(). |
108 | 119 |
109 if (request->status().is_success()) | 120 if (request->status().is_success()) |
110 dictionary_.append(buffer_->data(), bytes_read); | 121 dictionary_.append(buffer_->data(), bytes_read); |
111 | 122 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 if (rv != OK) { | 203 if (rv != OK) { |
193 current_request_.reset(); | 204 current_request_.reset(); |
194 buffer_ = NULL; | 205 buffer_ = NULL; |
195 next_state_ = STATE_IDLE; | 206 next_state_ = STATE_IDLE; |
196 | 207 |
197 return OK; | 208 return OK; |
198 } | 209 } |
199 | 210 |
200 next_state_ = STATE_REQUEST_READING; | 211 next_state_ = STATE_REQUEST_READING; |
201 int bytes_read = 0; | 212 int bytes_read = 0; |
202 if (!current_request_->Read(buffer_.get(), kBufferSize, &bytes_read)) { | 213 current_request_->Read(buffer_.get(), kBufferSize, &bytes_read); |
203 if (current_request_->status().is_io_pending()) | 214 if (current_request_->status().is_io_pending()) |
204 return ERR_IO_PENDING; | 215 return ERR_IO_PENDING; |
205 | 216 |
206 if (current_request_->status().error() == OK) { | 217 if (bytes_read < 0 || !current_request_->status().is_success()) { |
207 // This "should never happen", but if it does the result will be | 218 if (current_request_->status().error() != OK) |
208 // an infinite loop. It's not clear how to handle a read failure | 219 return current_request_->status().error(); |
209 // without a promise to invoke the callback at some point in the future, | |
210 // so the request is 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)); | |
217 DLOG(FATAL) | |
218 << "URLRequest::Read() returned false without IO pending or error!"; | |
219 return ERR_FAILED; | |
220 } | |
221 | 220 |
222 return current_request_->status().error(); | 221 // An error with request status of OK should not happen, |
| 222 // but there's enough machinery underneath URLRequest::Read() |
| 223 // that this routine checks for that case. |
| 224 net::Error error = |
| 225 current_request_->status().status() == URLRequestStatus::CANCELED ? |
| 226 ERR_ABORTED : ERR_FAILED; |
| 227 current_request_->net_log().AddEventWithNetErrorCode( |
| 228 NetLog::TYPE_SDCH_DICTIONARY_FETCH_IMPLIED_ERROR, error); |
| 229 return error; |
223 } | 230 } |
224 | 231 |
225 if (bytes_read != 0) | 232 if (bytes_read == 0) |
| 233 next_state_ = STATE_REQUEST_COMPLETE; |
| 234 else |
226 dictionary_.append(buffer_->data(), bytes_read); | 235 dictionary_.append(buffer_->data(), bytes_read); |
227 else | |
228 next_state_ = STATE_REQUEST_COMPLETE; | |
229 | 236 |
230 return OK; | 237 return OK; |
231 } | 238 } |
232 | 239 |
233 int SdchDictionaryFetcher::DoCompleteRequest(int rv) { | 240 int SdchDictionaryFetcher::DoCompleteRequest(int rv) { |
234 DCHECK(CalledOnValidThread()); | 241 DCHECK(CalledOnValidThread()); |
235 | 242 |
236 // If the dictionary was successfully fetched, add it to the manager. | 243 // If the dictionary was successfully fetched, add it to the manager. |
237 if (rv == OK) { | 244 if (rv == OK) { |
238 dictionary_fetched_callback_.Run(dictionary_, current_request_->url(), | 245 dictionary_fetched_callback_.Run(dictionary_, current_request_->url(), |
239 current_request_->net_log()); | 246 current_request_->net_log()); |
240 } | 247 } |
241 | 248 |
242 current_request_.reset(); | 249 current_request_.reset(); |
243 buffer_ = NULL; | 250 buffer_ = NULL; |
244 dictionary_.clear(); | 251 dictionary_.clear(); |
245 | 252 |
246 next_state_ = STATE_IDLE; | 253 next_state_ = STATE_IDLE; |
247 | 254 |
248 return OK; | 255 return OK; |
249 } | 256 } |
250 | 257 |
251 } // namespace net | 258 } // namespace net |
OLD | NEW |