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.
5 #include "net/base/sdch_dictionary_fetcher.h"
9 #include "base/auto_reset.h"
10 #include "base/bind.h"
11 #include "base/compiler_specific.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "net/base/load_flags.h"
14 #include "net/url_request/url_request_context.h"
15 #include "net/url_request/url_request_status.h"
16 #include "net/url_request/url_request_throttler_manager.h"
20 const int kBufferSize = 4096;
26 SdchDictionaryFetcher::SdchDictionaryFetcher(
27 SdchFetcher::Delegate* consumer,
28 URLRequestContext* context)
29 : next_state_(STATE_NONE),
34 DCHECK(CalledOnValidThread());
39 SdchDictionaryFetcher::~SdchDictionaryFetcher() {
40 DCHECK(CalledOnValidThread());
43 void SdchDictionaryFetcher::Schedule(const GURL& dictionary_url) {
44 DCHECK(CalledOnValidThread());
46 // 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 // queue twice at one time.
49 if (!fetch_queue_.empty() && fetch_queue_.back() == dictionary_url) {
50 SdchManager::SdchErrorRecovery(
51 SdchManager::DICTIONARY_ALREADY_SCHEDULED_TO_DOWNLOAD);
54 if (attempted_load_.find(dictionary_url) != attempted_load_.end()) {
55 SdchManager::SdchErrorRecovery(
56 SdchManager::DICTIONARY_ALREADY_TRIED_TO_DOWNLOAD);
59 attempted_load_.insert(dictionary_url);
60 fetch_queue_.push(dictionary_url);
62 next_state_ = STATE_IDLE;
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.
70 void SdchDictionaryFetcher::Cancel() {
71 DCHECK(CalledOnValidThread());
73 next_state_ = STATE_NONE;
75 while (!fetch_queue_.empty())
77 attempted_load_.clear();
78 weak_factory_.InvalidateWeakPtrs();
79 current_request_.reset(NULL);
84 void SdchDictionaryFetcher::OnResponseStarted(URLRequest* request) {
85 DCHECK(CalledOnValidThread());
86 DCHECK_EQ(request, current_request_.get());
87 DCHECK_EQ(next_state_, STATE_REQUEST_STARTED);
89 // The response has started, so the stream can be read from.
90 next_state_ = STATE_REQUEST_READING;
92 // If this function was synchronously called, the containing
93 // state machine loop will handle the state transition. Otherwise,
94 // restart the state machine loop.
98 DoLoop(request->status().error());
101 void SdchDictionaryFetcher::OnReadCompleted(URLRequest* request,
103 DCHECK(CalledOnValidThread());
104 DCHECK_EQ(request, current_request_.get());
105 DCHECK_EQ(next_state_, STATE_REQUEST_READING);
107 // No state transition is required in this function; the
108 // completion of the request is detected in DoRead().
110 if (request->status().is_success())
111 dictionary_.append(buffer_->data(), bytes_read);
113 // If this function was synchronously called, the containing
114 // state machine loop will handle the state transition. Otherwise,
115 // restart the state machine loop.
119 DoLoop(request->status().error());
122 int SdchDictionaryFetcher::DoLoop(int rv) {
124 base::AutoReset<bool> auto_reset_in_loop(&in_loop_, true);
127 State state = next_state_;
128 next_state_ = STATE_NONE;
131 rv = DoDispatchRequest(rv);
133 case STATE_REQUEST_STARTED:
134 rv = DoRequestStarted(rv);
136 case STATE_REQUEST_READING:
139 case STATE_REQUEST_COMPLETE:
140 rv = DoCompleteRequest(rv);
145 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
150 int SdchDictionaryFetcher::DoDispatchRequest(int rv) {
151 DCHECK(CalledOnValidThread());
153 // |rv| is ignored, as the result from the previous request doesn't
154 // affect the next request.
156 if (fetch_queue_.empty() || current_request_.get()) {
157 next_state_ = STATE_NONE;
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);
168 next_state_ = STATE_REQUEST_STARTED;
169 current_request_->Start();
174 int SdchDictionaryFetcher::DoRequestStarted(int rv) {
175 DCHECK(CalledOnValidThread());
176 DCHECK_EQ(rv, OK); // Can only come straight from above function.
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
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;
188 int SdchDictionaryFetcher::DoRead(int rv) {
189 DCHECK(CalledOnValidThread());
191 // If there's been an error, abort the current request.
193 current_request_.reset();
195 next_state_ = STATE_IDLE;
200 next_state_ = STATE_REQUEST_READING;
202 if (!current_request_->Read(buffer_.get(), kBufferSize, &bytes_read)) {
203 if (current_request_->status().is_io_pending())
204 return ERR_IO_PENDING;
206 if (current_request_->status().error() == OK) {
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
209 // without a promise to invoke the callback at some point in the future,
210 // so the request is failed.
211 SdchManager::SdchErrorRecovery(SdchManager::DICTIONARY_FETCH_READ_FAILED);
213 "URLRequest::Read() returned false without IO pending or error!";
217 return current_request_->status().error();
221 dictionary_.append(buffer_->data(), bytes_read);
223 next_state_ = STATE_REQUEST_COMPLETE;
228 int SdchDictionaryFetcher::DoCompleteRequest(int rv) {
229 DCHECK(CalledOnValidThread());
231 // If the dictionary was successfully fetched, add it to the manager.
233 consumer_->AddSdchDictionary(dictionary_, current_request_->url());
235 current_request_.reset();
239 next_state_ = STATE_IDLE;