Add wfh@ to chrome/browser/resources/plugin_metadata OWNERS.
[chromium-blink-merge.git] / net / url_request / sdch_dictionary_fetcher.cc
blob3607bfd42a08531c55d05bc0075aa61e48261d0c
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/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"
22 namespace net {
24 namespace {
26 const int kBufferSize = 4096;
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);
38 if (rv == OK)
39 rv = bytes_read;
41 return rv;
44 } // namespace
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);
58 SdchDictionaryFetcher::~SdchDictionaryFetcher() {
59 DCHECK(CalledOnValidThread());
62 void SdchDictionaryFetcher::Schedule(const GURL& dictionary_url) {
63 DCHECK(CalledOnValidThread());
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;
77 attempted_load_.insert(dictionary_url);
78 fetch_queue_.push(dictionary_url);
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;
85 next_state_ = STATE_SEND_REQUEST;
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);
93 void SdchDictionaryFetcher::Cancel() {
94 DCHECK(CalledOnValidThread());
96 next_state_ = STATE_NONE;
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();
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"));
113 DCHECK(CalledOnValidThread());
114 DCHECK_EQ(request, current_request_.get());
115 DCHECK_EQ(next_state_, STATE_SEND_REQUEST_COMPLETE);
116 DCHECK(!in_loop_);
118 DoLoop(request->status().error());
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"));
128 DCHECK(CalledOnValidThread());
129 DCHECK_EQ(request, current_request_.get());
130 DCHECK_EQ(next_state_, STATE_READ_BODY_COMPLETE);
131 DCHECK(!in_loop_);
133 DoLoop(GetReadResult(bytes_read, current_request_.get()));
136 int SdchDictionaryFetcher::DoLoop(int rv) {
137 DCHECK(!in_loop_);
138 base::AutoReset<bool> auto_reset_in_loop(&in_loop_, true);
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();
162 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
164 return rv;
167 int SdchDictionaryFetcher::DoSendRequest(int rv) {
168 DCHECK(CalledOnValidThread());
170 // |rv| is ignored, as the result from the previous request doesn't
171 // affect the next request.
173 if (fetch_queue_.empty() || current_request_.get()) {
174 next_state_ = STATE_NONE;
175 return OK;
178 next_state_ = STATE_SEND_REQUEST_COMPLETE;
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();
187 current_request_->Start();
188 current_request_->net_log().AddEvent(NetLog::TYPE_SDCH_DICTIONARY_FETCH);
190 return ERR_IO_PENDING;
193 int SdchDictionaryFetcher::DoSendRequestComplete(int rv) {
194 DCHECK(CalledOnValidThread());
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;
202 return OK;
205 next_state_ = STATE_READ_BODY;
206 return OK;
209 int SdchDictionaryFetcher::DoReadBody(int rv) {
210 DCHECK(CalledOnValidThread());
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;
218 return OK;
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;
227 return GetReadResult(bytes_read, current_request_.get());
230 int SdchDictionaryFetcher::DoReadBodyComplete(int rv) {
231 DCHECK(CalledOnValidThread());
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;
241 DCHECK(current_request_->status().is_success());
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;
250 // End of file; complete the request.
251 next_state_ = STATE_REQUEST_COMPLETE;
252 return OK;
255 int SdchDictionaryFetcher::DoCompleteRequest(int rv) {
256 DCHECK(CalledOnValidThread());
258 // DoReadBodyComplete() only transitions to this state
259 // on success.
260 DCHECK_EQ(OK, rv);
262 dictionary_fetched_callback_.Run(dictionary_, current_request_->url(),
263 current_request_->net_log());
264 current_request_.reset();
265 buffer_ = NULL;
266 dictionary_.clear();
268 next_state_ = STATE_SEND_REQUEST;
270 return OK;
273 } // namespace net