Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / net / url_request / sdch_dictionary_fetcher.cc
blob9aeed9a6237ef1624f10fb5cff5b0823d7d2fc4e
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/url_request/sdch_dictionary_fetcher.h"
7 #include <stdint.h>
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/load_flags.h"
15 #include "net/base/sdch_net_log_params.h"
16 #include "net/url_request/url_request_context.h"
17 #include "net/url_request/url_request_status.h"
18 #include "net/url_request/url_request_throttler_manager.h"
20 namespace {
22 const int kBufferSize = 4096;
24 } // namespace
26 namespace net {
28 SdchDictionaryFetcher::SdchDictionaryFetcher(
29 URLRequestContext* context,
30 const OnDictionaryFetchedCallback& callback)
31 : next_state_(STATE_NONE),
32 in_loop_(false),
33 context_(context),
34 dictionary_fetched_callback_(callback),
35 weak_factory_(this) {
36 DCHECK(CalledOnValidThread());
37 DCHECK(context);
40 SdchDictionaryFetcher::~SdchDictionaryFetcher() {
41 DCHECK(CalledOnValidThread());
44 void SdchDictionaryFetcher::Schedule(const GURL& dictionary_url) {
45 DCHECK(CalledOnValidThread());
47 // 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
49 // queue twice at one time.
50 if ((!fetch_queue_.empty() && fetch_queue_.back() == dictionary_url) ||
51 attempted_load_.find(dictionary_url) != attempted_load_.end()) {
52 // TODO(rdsmith): log this error to the net log of the URLRequest
53 // initiating this fetch, once URLRequest will be passed here.
54 SdchManager::SdchErrorRecovery(
55 SDCH_DICTIONARY_PREVIOUSLY_SCHEDULED_TO_DOWNLOAD);
56 return;
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.
67 DoLoop(OK);
70 void SdchDictionaryFetcher::Cancel() {
71 DCHECK(CalledOnValidThread());
73 next_state_ = STATE_NONE;
75 while (!fetch_queue_.empty())
76 fetch_queue_.pop();
77 attempted_load_.clear();
78 weak_factory_.InvalidateWeakPtrs();
79 current_request_.reset(NULL);
80 buffer_ = NULL;
81 dictionary_.clear();
84 void SdchDictionaryFetcher::OnResponseStarted(URLRequest* request) {
85 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
86 tracked_objects::ScopedTracker tracking_profile(
87 FROM_HERE_WITH_EXPLICIT_FUNCTION(
88 "423948 SdchDictionaryFetcher::OnResponseStarted"));
90 DCHECK(CalledOnValidThread());
91 DCHECK_EQ(request, current_request_.get());
92 DCHECK_EQ(next_state_, STATE_REQUEST_STARTED);
94 // The response has started, so the stream can be read from.
95 next_state_ = STATE_REQUEST_READING;
97 // If this function was synchronously called, the containing
98 // state machine loop will handle the state transition. Otherwise,
99 // restart the state machine loop.
100 if (in_loop_)
101 return;
103 DoLoop(request->status().error());
106 void SdchDictionaryFetcher::OnReadCompleted(URLRequest* request,
107 int bytes_read) {
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::OnReadCompleted"));
113 DCHECK(CalledOnValidThread());
114 DCHECK_EQ(request, current_request_.get());
115 DCHECK_EQ(next_state_, STATE_REQUEST_READING);
117 // No state transition is required in this function; the
118 // completion of the request is detected in DoRead().
120 if (request->status().is_success())
121 dictionary_.append(buffer_->data(), bytes_read);
123 // If this function was synchronously called, the containing
124 // state machine loop will handle the state transition. Otherwise,
125 // restart the state machine loop.
126 if (in_loop_)
127 return;
129 DoLoop(request->status().error());
132 int SdchDictionaryFetcher::DoLoop(int rv) {
133 DCHECK(!in_loop_);
134 base::AutoReset<bool> auto_reset_in_loop(&in_loop_, true);
136 do {
137 State state = next_state_;
138 next_state_ = STATE_NONE;
139 switch (state) {
140 case STATE_IDLE:
141 rv = DoDispatchRequest(rv);
142 break;
143 case STATE_REQUEST_STARTED:
144 rv = DoRequestStarted(rv);
145 break;
146 case STATE_REQUEST_READING:
147 rv = DoRead(rv);
148 break;
149 case STATE_REQUEST_COMPLETE:
150 rv = DoCompleteRequest(rv);
151 break;
152 case STATE_NONE:
153 NOTREACHED();
155 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
157 return rv;
160 int SdchDictionaryFetcher::DoDispatchRequest(int rv) {
161 DCHECK(CalledOnValidThread());
163 // |rv| is ignored, as the result from the previous request doesn't
164 // affect the next request.
166 if (fetch_queue_.empty() || current_request_.get()) {
167 next_state_ = STATE_NONE;
168 return OK;
171 current_request_ =
172 context_->CreateRequest(fetch_queue_.front(), IDLE, this, NULL);
173 current_request_->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES |
174 LOAD_DO_NOT_SAVE_COOKIES);
175 buffer_ = new IOBuffer(kBufferSize);
176 fetch_queue_.pop();
178 next_state_ = STATE_REQUEST_STARTED;
179 current_request_->Start();
180 current_request_->net_log().AddEvent(NetLog::TYPE_SDCH_DICTIONARY_FETCH);
182 return OK;
185 int SdchDictionaryFetcher::DoRequestStarted(int rv) {
186 DCHECK(CalledOnValidThread());
187 DCHECK_EQ(rv, OK); // Can only come straight from above function.
189 // The transition to STATE_REQUEST_READING occurs in the
190 // OnResponseStarted() callback triggered by URLRequest::Start()
191 // (called in DoDispatchRequest(), above). If that callback did not
192 // occur synchronously, this routine is executed; it returns ERR_IO_PENDING,
193 // indicating to the controlling loop that no further work should be done
194 // until the callback occurs (which will re-invoke DoLoop()).
195 next_state_ = STATE_REQUEST_STARTED;
196 return ERR_IO_PENDING;
199 int SdchDictionaryFetcher::DoRead(int rv) {
200 DCHECK(CalledOnValidThread());
202 // If there's been an error, abort the current request.
203 if (rv != OK) {
204 current_request_.reset();
205 buffer_ = NULL;
206 next_state_ = STATE_IDLE;
208 return OK;
211 next_state_ = STATE_REQUEST_READING;
212 int bytes_read = 0;
213 current_request_->Read(buffer_.get(), kBufferSize, &bytes_read);
214 if (current_request_->status().is_io_pending())
215 return ERR_IO_PENDING;
217 if (bytes_read < 0 || !current_request_->status().is_success()) {
218 if (current_request_->status().error() != OK)
219 return current_request_->status().error();
221 // An error with request status of OK should not happen,
222 // but there's enough machinery underneath URLRequest::Read()
223 // that this routine checks for that case.
224 net::Error error =
225 current_request_->status().status() == URLRequestStatus::CANCELED ?
226 ERR_ABORTED : ERR_FAILED;
227 current_request_->net_log().AddEventWithNetErrorCode(
228 NetLog::TYPE_SDCH_DICTIONARY_FETCH_IMPLIED_ERROR, error);
229 return error;
232 if (bytes_read == 0)
233 next_state_ = STATE_REQUEST_COMPLETE;
234 else
235 dictionary_.append(buffer_->data(), bytes_read);
237 return OK;
240 int SdchDictionaryFetcher::DoCompleteRequest(int rv) {
241 DCHECK(CalledOnValidThread());
243 // If the dictionary was successfully fetched, add it to the manager.
244 if (rv == OK) {
245 dictionary_fetched_callback_.Run(dictionary_, current_request_->url(),
246 current_request_->net_log());
249 current_request_.reset();
250 buffer_ = NULL;
251 dictionary_.clear();
253 next_state_ = STATE_IDLE;
255 return OK;
258 } // namespace net