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"
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"
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) {
34 request
->net_log().AddEventWithNetErrorCode(
35 NetLog::TYPE_SDCH_DICTIONARY_FETCH_IMPLIED_ERROR
, rv
);
46 SdchDictionaryFetcher::SdchDictionaryFetcher(
47 URLRequestContext
* context
,
48 const OnDictionaryFetchedCallback
& callback
)
49 : next_state_(STATE_NONE
),
52 dictionary_fetched_callback_(callback
),
54 DCHECK(CalledOnValidThread());
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
);
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
)
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.
93 void SdchDictionaryFetcher::Cancel() {
94 DCHECK(CalledOnValidThread());
96 next_state_
= STATE_NONE
;
98 while (!fetch_queue_
.empty())
100 attempted_load_
.clear();
101 weak_factory_
.InvalidateWeakPtrs();
102 current_request_
.reset(NULL
);
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
);
118 DoLoop(request
->status().error());
121 void SdchDictionaryFetcher::OnReadCompleted(URLRequest
* request
,
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
);
133 DoLoop(GetReadResult(bytes_read
, current_request_
.get()));
136 int SdchDictionaryFetcher::DoLoop(int rv
) {
138 base::AutoReset
<bool> auto_reset_in_loop(&in_loop_
, true);
141 State state
= next_state_
;
142 next_state_
= STATE_NONE
;
144 case STATE_SEND_REQUEST
:
145 rv
= DoSendRequest(rv
);
147 case STATE_SEND_REQUEST_COMPLETE
:
148 rv
= DoSendRequestComplete(rv
);
150 case STATE_READ_BODY
:
153 case STATE_READ_BODY_COMPLETE
:
154 rv
= DoReadBodyComplete(rv
);
156 case STATE_REQUEST_COMPLETE
:
157 rv
= DoCompleteRequest(rv
);
162 } while (rv
!= ERR_IO_PENDING
&& next_state_
!= STATE_NONE
);
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
;
178 next_state_
= STATE_SEND_REQUEST_COMPLETE
;
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
);
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.
198 current_request_
.reset();
200 next_state_
= STATE_SEND_REQUEST
;
205 next_state_
= STATE_READ_BODY
;
209 int SdchDictionaryFetcher::DoReadBody(int rv
) {
210 DCHECK(CalledOnValidThread());
212 // If there's been an error, abort the current request.
214 current_request_
.reset();
216 next_state_
= STATE_SEND_REQUEST
;
221 next_state_
= STATE_READ_BODY_COMPLETE
;
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.
235 current_request_
.reset();
237 next_state_
= STATE_SEND_REQUEST
;
241 DCHECK(current_request_
->status().is_success());
243 // Data; append to the dictionary and look for more data.
245 dictionary_
.append(buffer_
->data(), rv
);
246 next_state_
= STATE_READ_BODY
;
250 // End of file; complete the request.
251 next_state_
= STATE_REQUEST_COMPLETE
;
255 int SdchDictionaryFetcher::DoCompleteRequest(int rv
) {
256 DCHECK(CalledOnValidThread());
258 // DoReadBodyComplete() only transitions to this state
262 dictionary_fetched_callback_
.Run(dictionary_
, current_request_
->url(),
263 current_request_
->net_log());
264 current_request_
.reset();
268 next_state_
= STATE_SEND_REQUEST
;