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/base/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 |
24 namespace net { | 25 namespace net { |
25 | 26 |
26 SdchDictionaryFetcher::SdchDictionaryFetcher( | 27 SdchDictionaryFetcher::SdchDictionaryFetcher( |
27 SdchFetcher::Delegate* consumer, | 28 SdchFetcher::Delegate* consumer, |
28 URLRequestContext* context) | 29 URLRequestContext* context) |
29 : next_state_(STATE_NONE), | 30 : next_state_(STATE_NONE), |
30 in_loop_(false), | 31 in_loop_(false), |
31 consumer_(consumer), | 32 consumer_(consumer), |
32 context_(context), | 33 context_(context), |
33 weak_factory_(this) { | 34 weak_factory_(this) { |
34 DCHECK(CalledOnValidThread()); | 35 DCHECK(CalledOnValidThread()); |
35 DCHECK(consumer); | 36 DCHECK(consumer); |
36 DCHECK(context); | 37 DCHECK(context); |
37 } | 38 } |
38 | 39 |
39 SdchDictionaryFetcher::~SdchDictionaryFetcher() { | 40 SdchDictionaryFetcher::~SdchDictionaryFetcher() { |
40 DCHECK(CalledOnValidThread()); | 41 DCHECK(CalledOnValidThread()); |
41 } | 42 } |
42 | 43 |
43 void SdchDictionaryFetcher::Schedule(const GURL& dictionary_url) { | 44 bool SdchDictionaryFetcher::Schedule(const GURL& dictionary_url) { |
44 DCHECK(CalledOnValidThread()); | 45 DCHECK(CalledOnValidThread()); |
45 | 46 |
46 // Avoid pushing duplicate copy onto queue. We may fetch this url again later | 47 // Avoid pushing duplicate copy onto queue. We may fetch this url again later |
47 // and get a different dictionary, but there is no reason to have it in the | 48 // and get a different dictionary, but there is no reason to have it in the |
48 // queue twice at one time. | 49 // queue twice at one time. |
49 if (!fetch_queue_.empty() && fetch_queue_.back() == dictionary_url) { | 50 if (!fetch_queue_.empty() && fetch_queue_.back() == dictionary_url) |
50 SdchManager::SdchErrorRecovery( | 51 return false; |
51 SdchManager::DICTIONARY_ALREADY_SCHEDULED_TO_DOWNLOAD); | 52 |
52 return; | 53 if (attempted_load_.find(dictionary_url) != attempted_load_.end()) |
53 } | 54 return false; |
54 if (attempted_load_.find(dictionary_url) != attempted_load_.end()) { | 55 |
55 SdchManager::SdchErrorRecovery( | |
56 SdchManager::DICTIONARY_ALREADY_TRIED_TO_DOWNLOAD); | |
57 return; | |
58 } | |
59 attempted_load_.insert(dictionary_url); | 56 attempted_load_.insert(dictionary_url); |
60 fetch_queue_.push(dictionary_url); | 57 fetch_queue_.push(dictionary_url); |
61 | 58 |
62 next_state_ = STATE_IDLE; | 59 next_state_ = STATE_IDLE; |
63 | 60 |
64 // There are no callbacks to user code from the dictionary fetcher, | 61 // There are no callbacks to user code from the dictionary fetcher, |
65 // and Schedule() is only called from user code, so this call to DoLoop() | 62 // and Schedule() is only called from user code, so this call to DoLoop() |
66 // does not require an |if (in_loop_) return;| guard. | 63 // does not require an |if (in_loop_) return;| guard. |
67 DoLoop(OK); | 64 DoLoop(OK); |
| 65 |
| 66 return true; |
68 } | 67 } |
69 | 68 |
70 void SdchDictionaryFetcher::Cancel() { | 69 void SdchDictionaryFetcher::Cancel() { |
71 DCHECK(CalledOnValidThread()); | 70 DCHECK(CalledOnValidThread()); |
72 | 71 |
73 next_state_ = STATE_NONE; | 72 next_state_ = STATE_NONE; |
74 | 73 |
75 while (!fetch_queue_.empty()) | 74 while (!fetch_queue_.empty()) |
76 fetch_queue_.pop(); | 75 fetch_queue_.pop(); |
77 attempted_load_.clear(); | 76 attempted_load_.clear(); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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_ = context_->CreateRequest( |
162 fetch_queue_.front(), IDLE, this, NULL); | 161 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); |
| 164 current_request_->net_log().AddEvent(NetLog::TYPE_SDCH_DICTIONARY_FETCH); |
165 buffer_ = new IOBuffer(kBufferSize); | 165 buffer_ = new IOBuffer(kBufferSize); |
166 fetch_queue_.pop(); | 166 fetch_queue_.pop(); |
167 | 167 |
168 next_state_ = STATE_REQUEST_STARTED; | 168 next_state_ = STATE_REQUEST_STARTED; |
169 current_request_->Start(); | 169 current_request_->Start(); |
170 | 170 |
171 return OK; | 171 return OK; |
172 } | 172 } |
173 | 173 |
174 int SdchDictionaryFetcher::DoRequestStarted(int rv) { | 174 int SdchDictionaryFetcher::DoRequestStarted(int rv) { |
(...skipping 26 matching lines...) Expand all Loading... |
201 int bytes_read = 0; | 201 int bytes_read = 0; |
202 if (!current_request_->Read(buffer_.get(), kBufferSize, &bytes_read)) { | 202 if (!current_request_->Read(buffer_.get(), kBufferSize, &bytes_read)) { |
203 if (current_request_->status().is_io_pending()) | 203 if (current_request_->status().is_io_pending()) |
204 return ERR_IO_PENDING; | 204 return ERR_IO_PENDING; |
205 | 205 |
206 if (current_request_->status().error() == OK) { | 206 if (current_request_->status().error() == OK) { |
207 // 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 |
208 // 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 |
209 // 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, |
210 // so the request is failed. | 210 // so the request is failed. |
211 SdchManager::SdchErrorRecovery(SdchManager::DICTIONARY_FETCH_READ_FAILED); | 211 LogDictionaryFetchError(SDCH_DICTIONARY_FETCH_READ_FAILED); |
212 DLOG(FATAL) << | 212 DLOG(FATAL) << |
213 "URLRequest::Read() returned false without IO pending or error!"; | 213 "URLRequest::Read() returned false without IO pending or error!"; |
214 return ERR_FAILED; | 214 return ERR_FAILED; |
215 } | 215 } |
216 | 216 |
217 return current_request_->status().error(); | 217 return current_request_->status().error(); |
218 } | 218 } |
219 | 219 |
220 if (bytes_read != 0) | 220 if (bytes_read != 0) |
221 dictionary_.append(buffer_->data(), bytes_read); | 221 dictionary_.append(buffer_->data(), bytes_read); |
222 else | 222 else |
223 next_state_ = STATE_REQUEST_COMPLETE; | 223 next_state_ = STATE_REQUEST_COMPLETE; |
224 | 224 |
225 return OK; | 225 return OK; |
226 } | 226 } |
227 | 227 |
228 int SdchDictionaryFetcher::DoCompleteRequest(int rv) { | 228 int SdchDictionaryFetcher::DoCompleteRequest(int rv) { |
229 DCHECK(CalledOnValidThread()); | 229 DCHECK(CalledOnValidThread()); |
230 | 230 |
231 // If the dictionary was successfully fetched, add it to the manager. | 231 // If the dictionary was successfully fetched, add it to the manager. |
232 if (rv == OK) | 232 if (rv == OK) { |
233 consumer_->AddSdchDictionary(dictionary_, current_request_->url()); | 233 SdchProblemCode problem = |
| 234 consumer_->AddSdchDictionary(dictionary_, current_request_->url()); |
| 235 if (problem != SDCH_OK) |
| 236 LogDictionaryFetchError(problem); |
| 237 } |
234 | 238 |
235 current_request_.reset(); | 239 current_request_.reset(); |
236 buffer_ = NULL; | 240 buffer_ = NULL; |
237 dictionary_.clear(); | 241 dictionary_.clear(); |
238 | 242 |
239 next_state_ = STATE_IDLE; | 243 next_state_ = STATE_IDLE; |
240 | 244 |
241 return OK; | 245 return OK; |
242 } | 246 } |
243 | 247 |
| 248 void SdchDictionaryFetcher::LogDictionaryFetchError(SdchProblemCode error) { |
| 249 CHECK(current_request_.get()); |
| 250 SdchManager::SdchErrorRecovery(error); |
| 251 current_request_->net_log().AddEvent( |
| 252 NetLog::TYPE_SDCH_DICTIONARY_ERROR, |
| 253 base::Bind(&NetLogSdchDictionaryFetchProblemCallback, |
| 254 error, |
| 255 current_request_->url(), |
| 256 true)); |
| 257 } |
| 258 |
244 } // namespace net | 259 } // namespace net |
OLD | NEW |