Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(48)

Side by Side Diff: net/base/sdch_dictionary_fetcher.cc

Issue 495523003: Change SDCHDictionaryFetcher to use URLRequest instead of URLFetcher. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Cleaned up state machine & handled recursion properly. Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/base/sdch_dictionary_fetcher.h" 5 #include "net/base/sdch_dictionary_fetcher.h"
6 6
7 #include <stdint.h>
8
9 #include "base/auto_reset.h"
7 #include "base/bind.h" 10 #include "base/bind.h"
8 #include "base/compiler_specific.h" 11 #include "base/compiler_specific.h"
9 #include "base/message_loop/message_loop.h" 12 #include "base/thread_task_runner_handle.h"
10 #include "net/base/load_flags.h" 13 #include "net/base/load_flags.h"
11 #include "net/url_request/url_fetcher.h" 14 #include "net/url_request/url_request_context.h"
12 #include "net/url_request/url_request_context_getter.h"
13 #include "net/url_request/url_request_status.h" 15 #include "net/url_request/url_request_status.h"
16 #include "net/url_request/url_request_throttler_manager.h"
17
18 namespace {
19
20 const int kBufferSize = 4096;
21
22 } // namespace
14 23
15 namespace net { 24 namespace net {
16 25
17 SdchDictionaryFetcher::SdchDictionaryFetcher( 26 SdchDictionaryFetcher::SdchDictionaryFetcher(
18 SdchManager* manager, 27 SdchManager* manager,
19 scoped_refptr<URLRequestContextGetter> context) 28 URLRequestContext* context)
20 : manager_(manager), 29 : next_state_(STATE_NONE),
21 weak_factory_(this), 30 in_loop_(false),
22 task_is_pending_(false), 31 manager_(manager),
23 context_(context) { 32 context_(context),
33 weak_factory_(this) {
24 DCHECK(CalledOnValidThread()); 34 DCHECK(CalledOnValidThread());
25 DCHECK(manager); 35 DCHECK(manager);
36 DCHECK(context);
26 } 37 }
27 38
28 SdchDictionaryFetcher::~SdchDictionaryFetcher() { 39 SdchDictionaryFetcher::~SdchDictionaryFetcher() {
29 DCHECK(CalledOnValidThread()); 40 DCHECK(CalledOnValidThread());
30 } 41 }
31 42
32 void SdchDictionaryFetcher::Schedule(const GURL& dictionary_url) { 43 void SdchDictionaryFetcher::Schedule(const GURL& dictionary_url) {
33 DCHECK(CalledOnValidThread()); 44 DCHECK(CalledOnValidThread());
34 45
35 // Avoid pushing duplicate copy onto queue. We may fetch this url again later 46 // Avoid pushing duplicate copy onto queue. We may fetch this url again later
36 // and get a different dictionary, but there is no reason to have it in the 47 // and get a different dictionary, but there is no reason to have it in the
37 // queue twice at one time. 48 // queue twice at one time.
38 if (!fetch_queue_.empty() && fetch_queue_.back() == dictionary_url) { 49 if (!fetch_queue_.empty() && fetch_queue_.back() == dictionary_url) {
39 SdchManager::SdchErrorRecovery( 50 SdchManager::SdchErrorRecovery(
40 SdchManager::DICTIONARY_ALREADY_SCHEDULED_TO_DOWNLOAD); 51 SdchManager::DICTIONARY_ALREADY_SCHEDULED_TO_DOWNLOAD);
41 return; 52 return;
42 } 53 }
43 if (attempted_load_.find(dictionary_url) != attempted_load_.end()) { 54 if (attempted_load_.find(dictionary_url) != attempted_load_.end()) {
44 SdchManager::SdchErrorRecovery( 55 SdchManager::SdchErrorRecovery(
45 SdchManager::DICTIONARY_ALREADY_TRIED_TO_DOWNLOAD); 56 SdchManager::DICTIONARY_ALREADY_TRIED_TO_DOWNLOAD);
46 return; 57 return;
47 } 58 }
48 attempted_load_.insert(dictionary_url); 59 attempted_load_.insert(dictionary_url);
49 fetch_queue_.push(dictionary_url); 60 fetch_queue_.push(dictionary_url);
50 ScheduleDelayedRun(); 61
62 next_state_ = STATE_IDLE;
63
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);
51 } 68 }
52 69
53 void SdchDictionaryFetcher::Cancel() { 70 void SdchDictionaryFetcher::Cancel() {
54 DCHECK(CalledOnValidThread()); 71 DCHECK(CalledOnValidThread());
55 72
73 next_state_ = STATE_NONE;
74
56 while (!fetch_queue_.empty()) 75 while (!fetch_queue_.empty())
57 fetch_queue_.pop(); 76 fetch_queue_.pop();
58 attempted_load_.clear(); 77 attempted_load_.clear();
59 weak_factory_.InvalidateWeakPtrs(); 78 weak_factory_.InvalidateWeakPtrs();
60 current_fetch_.reset(NULL); 79 current_request_.reset(NULL);
80 buffer_ = NULL;
81 dictionary_.clear();
61 } 82 }
62 83
63 void SdchDictionaryFetcher::ScheduleDelayedRun() { 84 int SdchDictionaryFetcher::DoLoop(int rv) {
64 if (fetch_queue_.empty() || current_fetch_.get() || task_is_pending_) 85 DCHECK(!in_loop_);
65 return; 86 base::AutoReset<bool> auto_reset_in_loop(&in_loop_, true);
66 base::MessageLoop::current()->PostDelayedTask(FROM_HERE, 87
67 base::Bind(&SdchDictionaryFetcher::StartFetching, 88 do {
68 weak_factory_.GetWeakPtr()), 89 State state = next_state_;
69 base::TimeDelta::FromMilliseconds(kMsDelayFromRequestTillDownload)); 90 next_state_ = STATE_NONE;
70 task_is_pending_ = true; 91 switch (state) {
92 case STATE_IDLE:
93 rv = DoDispatchRequest(rv);
94 break;
95 case STATE_REQUEST_STARTED:
96 rv = DoRequestStarted(rv);
97 break;
98 case STATE_REQUEST_READING:
99 rv = DoRead(rv);
100 break;
101 case STATE_REQUEST_COMPLETE:
102 rv = DoCompleteRequest(rv);
103 break;
104 case STATE_NONE:
105 NOTREACHED();
106 }
107 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
108
109 return rv;
71 } 110 }
72 111
73 void SdchDictionaryFetcher::StartFetching() { 112 int SdchDictionaryFetcher::DoDispatchRequest(int rv) {
74 DCHECK(CalledOnValidThread()); 113 DCHECK(CalledOnValidThread());
75 DCHECK(task_is_pending_);
76 task_is_pending_ = false;
77 114
78 // Handle losing the race against Cancel(). 115 // Ignoring rv, as we don't really care what happened to the last
79 if (fetch_queue_.empty()) 116 // request on initial dispatch.
Ryan Sleevi 2014/09/04 01:14:21 Avoid "we" in comments when possible (see https://
Randy Smith (Not in Mondays) 2014/09/04 18:52:57 Right, sorry, still training myself out of that ha
117
118 if (fetch_queue_.empty() || current_request_.get()) {
119 next_state_ = STATE_NONE;
120 return OK;
121 }
122
123 current_request_ = context_->CreateRequest(
124 fetch_queue_.front(), IDLE, this, NULL);
125 current_request_->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES |
126 LOAD_DO_NOT_SAVE_COOKIES);
127 buffer_ = new IOBuffer(kBufferSize);
128 fetch_queue_.pop();
129
130 next_state_ = STATE_REQUEST_STARTED;
131 current_request_->Start();
132
133 return OK;
134 }
135
136 int SdchDictionaryFetcher::DoRequestStarted(int rv) {
137 DCHECK(CalledOnValidThread());
138 DCHECK_EQ(rv, OK); // Can only come straight from above function.
139
140 next_state_ = STATE_REQUEST_STARTED;
141
142 // We're waiting to be gotten back to through OnResponseStarted().
Ryan Sleevi 2014/09/04 01:14:21 Avoid "we" in comments // While this seems like i
Randy Smith (Not in Mondays) 2014/09/04 18:52:57 I did some text that made sense to me (same meanin
143 return ERR_IO_PENDING;
144 }
145
146 int SdchDictionaryFetcher::DoRead(int rv) {
147 DCHECK(CalledOnValidThread());
148
149 // If there's been an error, abort the current request.
150 if (rv != OK) {
151 current_request_.reset();
152 buffer_ = NULL;
153 next_state_ = STATE_IDLE;
154
155 return OK;
156 }
157
158 next_state_ = STATE_REQUEST_READING;
159 int bytes_read = 0;
160 if (!current_request_->Read(buffer_.get(), kBufferSize, &bytes_read)) {
161 if (current_request_->status().is_io_pending())
162 return ERR_IO_PENDING;
163
164 DCHECK_NE(current_request_->status().error(), OK);
165
166 return current_request_->status().error();
167 }
168
169 if (bytes_read != 0)
170 dictionary_.append(buffer_->data(), bytes_read);
171 else
172 next_state_ = STATE_REQUEST_COMPLETE;
173
174 return OK;
175 }
176
177 int SdchDictionaryFetcher::DoCompleteRequest(int rv) {
178 DCHECK(CalledOnValidThread());
179
180 // If we haven't had an error, add the dictionary.
Ryan Sleevi 2014/09/04 01:14:21 // If the dictionary was successfully fetched, add
Randy Smith (Not in Mondays) 2014/09/04 18:52:57 Done.
181 if (rv == OK)
182 manager_->AddSdchDictionary(dictionary_, current_request_->url());
183
184 current_request_.reset();
185 buffer_ = NULL;
186 dictionary_.clear();
187
188 next_state_ = STATE_IDLE;
189
190 return OK;
191 }
192
193 void SdchDictionaryFetcher::OnResponseStarted(URLRequest* request) {
194 DCHECK(CalledOnValidThread());
195 DCHECK_EQ(request, current_request_.get());
196 DCHECK_EQ(next_state_, STATE_REQUEST_STARTED);
197
198 next_state_ = STATE_REQUEST_READING;
199
200 if (in_loop_)
Ryan Sleevi 2014/09/04 01:14:21 // If OnResponseStarted was synchronously called,
Randy Smith (Not in Mondays) 2014/09/04 18:52:57 If you ask me again, I'll do this, but it feels li
Ryan Sleevi 2014/09/04 19:05:30 I'm specifically trying to explain the subtlety of
80 return; 201 return;
81 202
82 DCHECK(context_.get()); 203 DoLoop(request->status().error());
83 current_fetch_.reset(URLFetcher::Create(
84 fetch_queue_.front(), URLFetcher::GET, this));
85 fetch_queue_.pop();
86 current_fetch_->SetRequestContext(context_.get());
87 current_fetch_->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES |
88 LOAD_DO_NOT_SAVE_COOKIES);
89 current_fetch_->Start();
90 } 204 }
91 205
92 void SdchDictionaryFetcher::OnURLFetchComplete( 206 void SdchDictionaryFetcher::OnReadCompleted(URLRequest* request,
93 const URLFetcher* source) { 207 int bytes_read) {
94 DCHECK(CalledOnValidThread()); 208 DCHECK(CalledOnValidThread());
95 if ((200 == source->GetResponseCode()) && 209 DCHECK_EQ(request, current_request_.get());
96 (source->GetStatus().status() == URLRequestStatus::SUCCESS)) { 210 DCHECK_EQ(next_state_, STATE_REQUEST_READING);
97 std::string data; 211
98 source->GetResponseAsString(&data); 212 if (request->status().is_success())
99 manager_->AddSdchDictionary(data, source->GetURL()); 213 dictionary_.append(buffer_->data(), bytes_read);
100 } 214
101 current_fetch_.reset(NULL); 215 if (in_loop_)
102 ScheduleDelayedRun(); 216 return;
Ryan Sleevi 2014/09/04 01:14:21 ditto comments above Why is next_state_ not neede
Randy Smith (Not in Mondays) 2014/09/04 18:52:57 ditto response above.
Ryan Sleevi 2014/09/04 19:05:30 I don't know. mmenke would/should.
mmenke 2014/09/04 19:34:23 Yes, Start doesn't have a return value, so always
217
218 DoLoop(request->status().error());
103 } 219 }
104 220
105 } // namespace net 221 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698