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