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> | |
8 | |
9 #include "base/auto_reset.h" | |
7 #include "base/bind.h" | 10 #include "base/bind.h" |
8 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
9 #include "base/message_loop/message_loop.h" | 12 #include "base/thread_task_runner_handle.h" |
10 #include "net/base/load_flags.h" | 13 #include "net/base/load_flags.h" |
11 #include "net/url_request/url_fetcher.h" | 14 #include "net/url_request/url_request_context.h" |
12 #include "net/url_request/url_request_context_getter.h" | |
13 #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" | |
17 | |
18 namespace { | |
19 | |
20 const int kBufferSize = 4096; | |
21 | |
22 } // namespace | |
14 | 23 |
15 namespace net { | 24 namespace net { |
16 | 25 |
17 SdchDictionaryFetcher::SdchDictionaryFetcher( | 26 SdchDictionaryFetcher::SdchDictionaryFetcher( |
18 SdchManager* manager, | 27 SdchManager* manager, |
19 scoped_refptr<URLRequestContextGetter> context) | 28 URLRequestContext* context) |
20 : manager_(manager), | 29 : next_state_(STATE_NONE), |
21 task_is_pending_(false), | 30 in_loop_(false), |
31 manager_(manager), | |
22 context_(context), | 32 context_(context), |
23 weak_factory_(this) { | 33 weak_factory_(this) { |
24 DCHECK(CalledOnValidThread()); | 34 DCHECK(CalledOnValidThread()); |
25 DCHECK(manager); | 35 DCHECK(manager); |
36 DCHECK(context); | |
26 } | 37 } |
27 | 38 |
28 SdchDictionaryFetcher::~SdchDictionaryFetcher() { | 39 SdchDictionaryFetcher::~SdchDictionaryFetcher() { |
29 DCHECK(CalledOnValidThread()); | 40 DCHECK(CalledOnValidThread()); |
30 } | 41 } |
31 | 42 |
32 void SdchDictionaryFetcher::Schedule(const GURL& dictionary_url) { | 43 void SdchDictionaryFetcher::Schedule(const GURL& dictionary_url) { |
33 DCHECK(CalledOnValidThread()); | 44 DCHECK(CalledOnValidThread()); |
34 | 45 |
35 // 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 |
Ryan Sleevi
2014/09/04 20:06:20
here
Randy Smith (Not in Mondays)
2014/09/07 14:09:39
Done.
| |
36 // 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 |
37 // queue twice at one time. | 48 // queue twice at one time. |
38 if (!fetch_queue_.empty() && fetch_queue_.back() == dictionary_url) { | 49 if (!fetch_queue_.empty() && fetch_queue_.back() == dictionary_url) { |
39 SdchManager::SdchErrorRecovery( | 50 SdchManager::SdchErrorRecovery( |
40 SdchManager::DICTIONARY_ALREADY_SCHEDULED_TO_DOWNLOAD); | 51 SdchManager::DICTIONARY_ALREADY_SCHEDULED_TO_DOWNLOAD); |
41 return; | 52 return; |
42 } | 53 } |
43 if (attempted_load_.find(dictionary_url) != attempted_load_.end()) { | 54 if (attempted_load_.find(dictionary_url) != attempted_load_.end()) { |
44 SdchManager::SdchErrorRecovery( | 55 SdchManager::SdchErrorRecovery( |
45 SdchManager::DICTIONARY_ALREADY_TRIED_TO_DOWNLOAD); | 56 SdchManager::DICTIONARY_ALREADY_TRIED_TO_DOWNLOAD); |
46 return; | 57 return; |
47 } | 58 } |
48 attempted_load_.insert(dictionary_url); | 59 attempted_load_.insert(dictionary_url); |
49 fetch_queue_.push(dictionary_url); | 60 fetch_queue_.push(dictionary_url); |
50 ScheduleDelayedRun(); | 61 |
62 next_state_ = STATE_IDLE; | |
63 | |
64 // 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() | |
66 // does not require an |if (in_loop_) return;| guard. | |
67 DoLoop(OK); | |
51 } | 68 } |
52 | 69 |
53 void SdchDictionaryFetcher::Cancel() { | 70 void SdchDictionaryFetcher::Cancel() { |
54 DCHECK(CalledOnValidThread()); | 71 DCHECK(CalledOnValidThread()); |
55 | 72 |
73 next_state_ = STATE_NONE; | |
74 | |
56 while (!fetch_queue_.empty()) | 75 while (!fetch_queue_.empty()) |
57 fetch_queue_.pop(); | 76 fetch_queue_.pop(); |
58 attempted_load_.clear(); | 77 attempted_load_.clear(); |
59 weak_factory_.InvalidateWeakPtrs(); | 78 weak_factory_.InvalidateWeakPtrs(); |
60 current_fetch_.reset(NULL); | 79 current_request_.reset(NULL); |
80 buffer_ = NULL; | |
81 dictionary_.clear(); | |
61 } | 82 } |
62 | 83 |
63 void SdchDictionaryFetcher::ScheduleDelayedRun() { | 84 void SdchDictionaryFetcher::OnResponseStarted(URLRequest* request) { |
64 if (fetch_queue_.empty() || current_fetch_.get() || task_is_pending_) | 85 DCHECK(CalledOnValidThread()); |
86 DCHECK_EQ(request, current_request_.get()); | |
87 DCHECK_EQ(next_state_, STATE_REQUEST_STARTED); | |
88 | |
89 // The response has started, so the stream can be read from. | |
90 next_state_ = STATE_REQUEST_READING; | |
91 | |
92 // If this function was synchronously called, the containing | |
93 // state machine loop will handle the state transition. Otherwise, | |
Ryan Sleevi
2014/09/04 20:06:20
here
Randy Smith (Not in Mondays)
2014/09/07 14:09:38
Done.
| |
94 // restart the state machine loop. | |
95 if (in_loop_) | |
65 return; | 96 return; |
66 base::MessageLoop::current()->PostDelayedTask(FROM_HERE, | 97 |
67 base::Bind(&SdchDictionaryFetcher::StartFetching, | 98 DoLoop(request->status().error()); |
68 weak_factory_.GetWeakPtr()), | |
69 base::TimeDelta::FromMilliseconds(kMsDelayFromRequestTillDownload)); | |
70 task_is_pending_ = true; | |
71 } | 99 } |
72 | 100 |
73 void SdchDictionaryFetcher::StartFetching() { | 101 void SdchDictionaryFetcher::OnReadCompleted(URLRequest* request, |
102 int bytes_read) { | |
74 DCHECK(CalledOnValidThread()); | 103 DCHECK(CalledOnValidThread()); |
75 DCHECK(task_is_pending_); | 104 DCHECK_EQ(request, current_request_.get()); |
76 task_is_pending_ = false; | 105 DCHECK_EQ(next_state_, STATE_REQUEST_READING); |
77 | 106 |
78 // Handle losing the race against Cancel(). | 107 // No state transition is required in this function; the |
79 if (fetch_queue_.empty()) | 108 // completion of the request is detected in DoRead(). |
109 | |
110 if (request->status().is_success()) | |
111 dictionary_.append(buffer_->data(), bytes_read); | |
112 | |
113 // If this function was synchronously called, the containing | |
114 // state machine loop will handle the state transition. Otherwise, | |
Ryan Sleevi
2014/09/04 20:06:20
here
Randy Smith (Not in Mondays)
2014/09/07 14:09:38
Done.
| |
115 // restart the state machine loop. | |
116 if (in_loop_) | |
80 return; | 117 return; |
81 | 118 |
82 DCHECK(context_.get()); | 119 DoLoop(request->status().error()); |
83 current_fetch_.reset(URLFetcher::Create( | |
84 fetch_queue_.front(), URLFetcher::GET, this)); | |
85 fetch_queue_.pop(); | |
86 current_fetch_->SetRequestContext(context_.get()); | |
87 current_fetch_->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES | | |
88 LOAD_DO_NOT_SAVE_COOKIES); | |
89 current_fetch_->Start(); | |
90 } | 120 } |
91 | 121 |
92 void SdchDictionaryFetcher::OnURLFetchComplete( | 122 int SdchDictionaryFetcher::DoLoop(int rv) { |
93 const URLFetcher* source) { | 123 DCHECK(!in_loop_); |
124 base::AutoReset<bool> auto_reset_in_loop(&in_loop_, true); | |
125 | |
126 do { | |
127 State state = next_state_; | |
128 next_state_ = STATE_NONE; | |
129 switch (state) { | |
130 case STATE_IDLE: | |
131 rv = DoDispatchRequest(rv); | |
132 break; | |
133 case STATE_REQUEST_STARTED: | |
134 rv = DoRequestStarted(rv); | |
135 break; | |
136 case STATE_REQUEST_READING: | |
137 rv = DoRead(rv); | |
138 break; | |
139 case STATE_REQUEST_COMPLETE: | |
140 rv = DoCompleteRequest(rv); | |
141 break; | |
142 case STATE_NONE: | |
143 NOTREACHED(); | |
144 } | |
145 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); | |
146 | |
147 return rv; | |
148 } | |
149 | |
150 int SdchDictionaryFetcher::DoDispatchRequest(int rv) { | |
94 DCHECK(CalledOnValidThread()); | 151 DCHECK(CalledOnValidThread()); |
95 if ((200 == source->GetResponseCode()) && | 152 |
96 (source->GetStatus().status() == URLRequestStatus::SUCCESS)) { | 153 // |rv| is ignored, as the result from the previous request doesn't |
97 std::string data; | 154 // affect the next request. |
98 source->GetResponseAsString(&data); | 155 |
99 manager_->AddSdchDictionary(data, source->GetURL()); | 156 if (fetch_queue_.empty() || current_request_.get()) { |
157 next_state_ = STATE_NONE; | |
158 return OK; | |
100 } | 159 } |
101 current_fetch_.reset(NULL); | 160 |
102 ScheduleDelayedRun(); | 161 current_request_ = context_->CreateRequest( |
162 fetch_queue_.front(), IDLE, this, NULL); | |
163 current_request_->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES | | |
164 LOAD_DO_NOT_SAVE_COOKIES); | |
165 buffer_ = new IOBuffer(kBufferSize); | |
166 fetch_queue_.pop(); | |
167 | |
168 next_state_ = STATE_REQUEST_STARTED; | |
169 current_request_->Start(); | |
170 | |
171 return OK; | |
172 } | |
173 | |
174 int SdchDictionaryFetcher::DoRequestStarted(int rv) { | |
175 DCHECK(CalledOnValidThread()); | |
176 DCHECK_EQ(rv, OK); // Can only come straight from above function. | |
177 | |
178 // The transition to STATE_REQUEST_READING occurs in the | |
179 // OnResponseStarted() callback triggered by URLRequest::Start() | |
180 // (called in DoDispatchRequest(), above). If that callback did not | |
Ryan Sleevi
2014/09/04 20:06:20
here
Randy Smith (Not in Mondays)
2014/09/07 14:09:39
Done.
| |
181 // occur synchronously, this routine is executed; it returns ERR_IO_PENDING, | |
182 // indicating to the controlling loop that no further work should be done | |
183 // until the callback occurs (which will re-invoke DoLoop()). | |
184 next_state_ = STATE_REQUEST_STARTED; | |
185 return ERR_IO_PENDING; | |
186 } | |
187 | |
188 int SdchDictionaryFetcher::DoRead(int rv) { | |
189 DCHECK(CalledOnValidThread()); | |
190 | |
191 // If there's been an error, abort the current request. | |
192 if (rv != OK) { | |
193 current_request_.reset(); | |
194 buffer_ = NULL; | |
195 next_state_ = STATE_IDLE; | |
196 | |
197 return OK; | |
198 } | |
199 | |
200 next_state_ = STATE_REQUEST_READING; | |
201 int bytes_read = 0; | |
202 if (!current_request_->Read(buffer_.get(), kBufferSize, &bytes_read)) { | |
203 if (current_request_->status().is_io_pending()) | |
204 return ERR_IO_PENDING; | |
205 | |
206 DCHECK_NE(current_request_->status().error(), OK); | |
207 | |
208 return current_request_->status().error(); | |
209 } | |
210 | |
211 if (bytes_read != 0) | |
212 dictionary_.append(buffer_->data(), bytes_read); | |
213 else | |
214 next_state_ = STATE_REQUEST_COMPLETE; | |
215 | |
216 return OK; | |
217 } | |
218 | |
219 int SdchDictionaryFetcher::DoCompleteRequest(int rv) { | |
220 DCHECK(CalledOnValidThread()); | |
221 | |
222 // If the dictionary was successfully fetched, add it to the manager. | |
223 if (rv == OK) | |
224 manager_->AddSdchDictionary(dictionary_, current_request_->url()); | |
225 | |
226 current_request_.reset(); | |
227 buffer_ = NULL; | |
228 dictionary_.clear(); | |
229 | |
230 next_state_ = STATE_IDLE; | |
231 | |
232 return OK; | |
103 } | 233 } |
104 | 234 |
105 } // namespace net | 235 } // namespace net |
OLD | NEW |