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/base/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/url_request/url_request_context.h" | 14 #include "net/url_request/url_request_context.h" |
15 #include "net/url_request/url_request_status.h" | 15 #include "net/url_request/url_request_status.h" |
16 #include "net/url_request/url_request_throttler_manager.h" | 16 #include "net/url_request/url_request_throttler_manager.h" |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 const int kBufferSize = 4096; | 20 const int kBufferSize = 4096; |
21 | 21 |
22 } // namespace | 22 } // namespace |
23 | 23 |
24 namespace net { | 24 namespace net { |
25 | 25 |
26 SdchDictionaryFetcher::SdchDictionaryFetcher( | 26 SdchDictionaryFetcher::SdchDictionaryFetcher( |
27 SdchFetcher::Delegate* consumer, | 27 URLRequestContext* context, |
28 URLRequestContext* context) | 28 const OnDictionaryFetchedCallback& callback) |
29 : next_state_(STATE_NONE), | 29 : next_state_(STATE_NONE), |
30 in_loop_(false), | 30 in_loop_(false), |
31 consumer_(consumer), | |
32 context_(context), | 31 context_(context), |
| 32 dictionary_fetched_callback_(callback), |
33 weak_factory_(this) { | 33 weak_factory_(this) { |
34 DCHECK(CalledOnValidThread()); | 34 DCHECK(CalledOnValidThread()); |
35 DCHECK(consumer); | |
36 DCHECK(context); | 35 DCHECK(context); |
37 } | 36 } |
38 | 37 |
39 SdchDictionaryFetcher::~SdchDictionaryFetcher() { | 38 SdchDictionaryFetcher::~SdchDictionaryFetcher() { |
40 DCHECK(CalledOnValidThread()); | 39 DCHECK(CalledOnValidThread()); |
41 } | 40 } |
42 | 41 |
43 void SdchDictionaryFetcher::Schedule(const GURL& dictionary_url) { | 42 void SdchDictionaryFetcher::Schedule(const GURL& dictionary_url) { |
44 DCHECK(CalledOnValidThread()); | 43 DCHECK(CalledOnValidThread()); |
45 | 44 |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 DCHECK(CalledOnValidThread()); | 150 DCHECK(CalledOnValidThread()); |
152 | 151 |
153 // |rv| is ignored, as the result from the previous request doesn't | 152 // |rv| is ignored, as the result from the previous request doesn't |
154 // affect the next request. | 153 // affect the next request. |
155 | 154 |
156 if (fetch_queue_.empty() || current_request_.get()) { | 155 if (fetch_queue_.empty() || current_request_.get()) { |
157 next_state_ = STATE_NONE; | 156 next_state_ = STATE_NONE; |
158 return OK; | 157 return OK; |
159 } | 158 } |
160 | 159 |
161 current_request_ = context_->CreateRequest( | 160 current_request_ = |
162 fetch_queue_.front(), IDLE, this, NULL); | 161 context_->CreateRequest(fetch_queue_.front(), IDLE, this, NULL); |
163 current_request_->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES | | 162 current_request_->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES | |
164 LOAD_DO_NOT_SAVE_COOKIES); | 163 LOAD_DO_NOT_SAVE_COOKIES); |
165 buffer_ = new IOBuffer(kBufferSize); | 164 buffer_ = new IOBuffer(kBufferSize); |
166 fetch_queue_.pop(); | 165 fetch_queue_.pop(); |
167 | 166 |
168 next_state_ = STATE_REQUEST_STARTED; | 167 next_state_ = STATE_REQUEST_STARTED; |
169 current_request_->Start(); | 168 current_request_->Start(); |
170 | 169 |
171 return OK; | 170 return OK; |
172 } | 171 } |
(...skipping 29 matching lines...) Expand all Loading... |
202 if (!current_request_->Read(buffer_.get(), kBufferSize, &bytes_read)) { | 201 if (!current_request_->Read(buffer_.get(), kBufferSize, &bytes_read)) { |
203 if (current_request_->status().is_io_pending()) | 202 if (current_request_->status().is_io_pending()) |
204 return ERR_IO_PENDING; | 203 return ERR_IO_PENDING; |
205 | 204 |
206 if (current_request_->status().error() == OK) { | 205 if (current_request_->status().error() == OK) { |
207 // This "should never happen", but if it does the result will be | 206 // This "should never happen", but if it does the result will be |
208 // an infinite loop. It's not clear how to handle a read failure | 207 // an infinite loop. It's not clear how to handle a read failure |
209 // without a promise to invoke the callback at some point in the future, | 208 // without a promise to invoke the callback at some point in the future, |
210 // so the request is failed. | 209 // so the request is failed. |
211 SdchManager::SdchErrorRecovery(SdchManager::DICTIONARY_FETCH_READ_FAILED); | 210 SdchManager::SdchErrorRecovery(SdchManager::DICTIONARY_FETCH_READ_FAILED); |
212 DLOG(FATAL) << | 211 DLOG(FATAL) |
213 "URLRequest::Read() returned false without IO pending or error!"; | 212 << "URLRequest::Read() returned false without IO pending or error!"; |
214 return ERR_FAILED; | 213 return ERR_FAILED; |
215 } | 214 } |
216 | 215 |
217 return current_request_->status().error(); | 216 return current_request_->status().error(); |
218 } | 217 } |
219 | 218 |
220 if (bytes_read != 0) | 219 if (bytes_read != 0) |
221 dictionary_.append(buffer_->data(), bytes_read); | 220 dictionary_.append(buffer_->data(), bytes_read); |
222 else | 221 else |
223 next_state_ = STATE_REQUEST_COMPLETE; | 222 next_state_ = STATE_REQUEST_COMPLETE; |
224 | 223 |
225 return OK; | 224 return OK; |
226 } | 225 } |
227 | 226 |
228 int SdchDictionaryFetcher::DoCompleteRequest(int rv) { | 227 int SdchDictionaryFetcher::DoCompleteRequest(int rv) { |
229 DCHECK(CalledOnValidThread()); | 228 DCHECK(CalledOnValidThread()); |
230 | 229 |
231 // If the dictionary was successfully fetched, add it to the manager. | 230 // If the dictionary was successfully fetched, add it to the manager. |
232 if (rv == OK) | 231 if (rv == OK) |
233 consumer_->AddSdchDictionary(dictionary_, current_request_->url()); | 232 dictionary_fetched_callback_.Run(dictionary_, current_request_->url()); |
234 | 233 |
235 current_request_.reset(); | 234 current_request_.reset(); |
236 buffer_ = NULL; | 235 buffer_ = NULL; |
237 dictionary_.clear(); | 236 dictionary_.clear(); |
238 | 237 |
239 next_state_ = STATE_IDLE; | 238 next_state_ = STATE_IDLE; |
240 | 239 |
241 return OK; | 240 return OK; |
242 } | 241 } |
243 | 242 |
244 } // namespace net | 243 } // namespace net |
OLD | NEW |