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/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"
22 const int kBufferSize
= 4096;
28 SdchDictionaryFetcher::SdchDictionaryFetcher(
29 URLRequestContext
* context
,
30 const OnDictionaryFetchedCallback
& callback
)
31 : next_state_(STATE_NONE
),
34 dictionary_fetched_callback_(callback
),
36 DCHECK(CalledOnValidThread());
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
);
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 // 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.
103 DoLoop(request
->status().error());
106 void SdchDictionaryFetcher::OnReadCompleted(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::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.
129 DoLoop(request
->status().error());
132 int SdchDictionaryFetcher::DoLoop(int rv
) {
134 base::AutoReset
<bool> auto_reset_in_loop(&in_loop_
, true);
137 State state
= next_state_
;
138 next_state_
= STATE_NONE
;
141 rv
= DoDispatchRequest(rv
);
143 case STATE_REQUEST_STARTED
:
144 rv
= DoRequestStarted(rv
);
146 case STATE_REQUEST_READING
:
149 case STATE_REQUEST_COMPLETE
:
150 rv
= DoCompleteRequest(rv
);
155 } while (rv
!= ERR_IO_PENDING
&& next_state_
!= STATE_NONE
);
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
;
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
);
178 next_state_
= STATE_REQUEST_STARTED
;
179 current_request_
->Start();
180 current_request_
->net_log().AddEvent(NetLog::TYPE_SDCH_DICTIONARY_FETCH
);
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.
204 current_request_
.reset();
206 next_state_
= STATE_IDLE
;
211 next_state_
= STATE_REQUEST_READING
;
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.
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
);
233 next_state_
= STATE_REQUEST_COMPLETE
;
235 dictionary_
.append(buffer_
->data(), bytes_read
);
240 int SdchDictionaryFetcher::DoCompleteRequest(int rv
) {
241 DCHECK(CalledOnValidThread());
243 // If the dictionary was successfully fetched, add it to the manager.
245 dictionary_fetched_callback_
.Run(dictionary_
, current_request_
->url(),
246 current_request_
->net_log());
249 current_request_
.reset();
253 next_state_
= STATE_IDLE
;