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" | |
15 #include "net/url_request/url_request_context.h" | 14 #include "net/url_request/url_request_context.h" |
16 #include "net/url_request/url_request_status.h" | 15 #include "net/url_request/url_request_status.h" |
17 #include "net/url_request/url_request_throttler_manager.h" | 16 #include "net/url_request/url_request_throttler_manager.h" |
18 | 17 |
19 namespace { | 18 namespace { |
20 | 19 |
21 const int kBufferSize = 4096; | 20 const int kBufferSize = 4096; |
22 | 21 |
23 } // namespace | 22 } // namespace |
24 | 23 |
25 namespace net { | 24 namespace net { |
26 | 25 |
27 SdchDictionaryFetcher::SdchDictionaryFetcher( | 26 SdchDictionaryFetcher::SdchDictionaryFetcher( |
28 SdchFetcher::Delegate* consumer, | 27 SdchFetcher::Delegate* consumer, |
29 URLRequestContext* context) | 28 URLRequestContext* context) |
30 : next_state_(STATE_NONE), | 29 : next_state_(STATE_NONE), |
31 in_loop_(false), | 30 in_loop_(false), |
32 consumer_(consumer), | 31 consumer_(consumer), |
33 context_(context), | 32 context_(context), |
34 weak_factory_(this) { | 33 weak_factory_(this) { |
35 DCHECK(CalledOnValidThread()); | 34 DCHECK(CalledOnValidThread()); |
36 DCHECK(consumer); | 35 DCHECK(consumer); |
37 DCHECK(context); | 36 DCHECK(context); |
38 } | 37 } |
39 | 38 |
40 SdchDictionaryFetcher::~SdchDictionaryFetcher() { | 39 SdchDictionaryFetcher::~SdchDictionaryFetcher() { |
41 DCHECK(CalledOnValidThread()); | 40 DCHECK(CalledOnValidThread()); |
42 } | 41 } |
43 | 42 |
44 bool SdchDictionaryFetcher::Schedule(const GURL& dictionary_url) { | 43 void SdchDictionaryFetcher::Schedule(const GURL& dictionary_url) { |
45 DCHECK(CalledOnValidThread()); | 44 DCHECK(CalledOnValidThread()); |
46 | 45 |
47 // Avoid pushing duplicate copy onto queue. We may fetch this url again later | 46 // Avoid pushing duplicate copy onto queue. We may fetch this url again later |
48 // and get a different dictionary, but there is no reason to have it in the | 47 // and get a different dictionary, but there is no reason to have it in the |
49 // queue twice at one time. | 48 // queue twice at one time. |
50 if (!fetch_queue_.empty() && fetch_queue_.back() == dictionary_url) | 49 if (!fetch_queue_.empty() && fetch_queue_.back() == dictionary_url) { |
51 return false; | 50 SdchManager::SdchErrorRecovery( |
52 | 51 SdchManager::DICTIONARY_ALREADY_SCHEDULED_TO_DOWNLOAD); |
53 if (attempted_load_.find(dictionary_url) != attempted_load_.end()) | 52 return; |
54 return false; | 53 } |
55 | 54 if (attempted_load_.find(dictionary_url) != attempted_load_.end()) { |
| 55 SdchManager::SdchErrorRecovery( |
| 56 SdchManager::DICTIONARY_ALREADY_TRIED_TO_DOWNLOAD); |
| 57 return; |
| 58 } |
56 attempted_load_.insert(dictionary_url); | 59 attempted_load_.insert(dictionary_url); |
57 fetch_queue_.push(dictionary_url); | 60 fetch_queue_.push(dictionary_url); |
58 | 61 |
59 next_state_ = STATE_IDLE; | 62 next_state_ = STATE_IDLE; |
60 | 63 |
61 // There are no callbacks to user code from the dictionary fetcher, | 64 // There are no callbacks to user code from the dictionary fetcher, |
62 // and Schedule() is only called from user code, so this call to DoLoop() | 65 // and Schedule() is only called from user code, so this call to DoLoop() |
63 // does not require an |if (in_loop_) return;| guard. | 66 // does not require an |if (in_loop_) return;| guard. |
64 DoLoop(OK); | 67 DoLoop(OK); |
65 | |
66 return true; | |
67 } | 68 } |
68 | 69 |
69 void SdchDictionaryFetcher::Cancel() { | 70 void SdchDictionaryFetcher::Cancel() { |
70 DCHECK(CalledOnValidThread()); | 71 DCHECK(CalledOnValidThread()); |
71 | 72 |
72 next_state_ = STATE_NONE; | 73 next_state_ = STATE_NONE; |
73 | 74 |
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(); |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 | 160 |
160 current_request_ = context_->CreateRequest( | 161 current_request_ = context_->CreateRequest( |
161 fetch_queue_.front(), IDLE, this, NULL); | 162 fetch_queue_.front(), IDLE, this, NULL); |
162 current_request_->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES | | 163 current_request_->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES | |
163 LOAD_DO_NOT_SAVE_COOKIES); | 164 LOAD_DO_NOT_SAVE_COOKIES); |
164 buffer_ = new IOBuffer(kBufferSize); | 165 buffer_ = new IOBuffer(kBufferSize); |
165 fetch_queue_.pop(); | 166 fetch_queue_.pop(); |
166 | 167 |
167 next_state_ = STATE_REQUEST_STARTED; | 168 next_state_ = STATE_REQUEST_STARTED; |
168 current_request_->Start(); | 169 current_request_->Start(); |
169 current_request_->net_log().AddEvent(NetLog::TYPE_SDCH_DICTIONARY_FETCH); | |
170 | 170 |
171 return OK; | 171 return OK; |
172 } | 172 } |
173 | 173 |
174 int SdchDictionaryFetcher::DoRequestStarted(int rv) { | 174 int SdchDictionaryFetcher::DoRequestStarted(int rv) { |
175 DCHECK(CalledOnValidThread()); | 175 DCHECK(CalledOnValidThread()); |
176 DCHECK_EQ(rv, OK); // Can only come straight from above function. | 176 DCHECK_EQ(rv, OK); // Can only come straight from above function. |
177 | 177 |
178 // The transition to STATE_REQUEST_READING occurs in the | 178 // The transition to STATE_REQUEST_READING occurs in the |
179 // OnResponseStarted() callback triggered by URLRequest::Start() | 179 // OnResponseStarted() callback triggered by URLRequest::Start() |
(...skipping 21 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 LogDictionaryFetchError(SDCH_DICTIONARY_FETCH_READ_FAILED); | 211 SdchManager::SdchErrorRecovery(SdchManager::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 SdchProblemCode problem = | 233 consumer_->AddSdchDictionary(dictionary_, current_request_->url()); |
234 consumer_->AddSdchDictionary(dictionary_, current_request_->url()); | |
235 if (problem != SDCH_OK) | |
236 LogDictionaryFetchError(problem); | |
237 } | |
238 | 234 |
239 current_request_.reset(); | 235 current_request_.reset(); |
240 buffer_ = NULL; | 236 buffer_ = NULL; |
241 dictionary_.clear(); | 237 dictionary_.clear(); |
242 | 238 |
243 next_state_ = STATE_IDLE; | 239 next_state_ = STATE_IDLE; |
244 | 240 |
245 return OK; | 241 return OK; |
246 } | 242 } |
247 | 243 |
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 | |
259 } // namespace net | 244 } // namespace net |
OLD | NEW |