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

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

Issue 895853003: Update from https://crrev.com/314320 (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 10 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
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/url_request/sdch_dictionary_fetcher.h" 5 #include "net/url_request/sdch_dictionary_fetcher.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include "base/auto_reset.h" 9 #include "base/auto_reset.h"
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/compiler_specific.h" 11 #include "base/compiler_specific.h"
12 #include "base/profiler/scoped_tracker.h" 12 #include "base/profiler/scoped_tracker.h"
13 #include "base/thread_task_runner_handle.h" 13 #include "base/thread_task_runner_handle.h"
14 #include "net/base/io_buffer.h" 14 #include "net/base/io_buffer.h"
15 #include "net/base/load_flags.h" 15 #include "net/base/load_flags.h"
16 #include "net/base/net_log.h"
16 #include "net/base/sdch_net_log_params.h" 17 #include "net/base/sdch_net_log_params.h"
17 #include "net/url_request/url_request_context.h" 18 #include "net/url_request/url_request_context.h"
18 #include "net/url_request/url_request_status.h" 19 #include "net/url_request/url_request_status.h"
19 #include "net/url_request/url_request_throttler_manager.h" 20 #include "net/url_request/url_request_throttler_manager.h"
20 21
22 namespace net {
23
21 namespace { 24 namespace {
22 25
23 const int kBufferSize = 4096; 26 const int kBufferSize = 4096;
24 27
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);
36 }
37
38 if (rv == OK)
39 rv = bytes_read;
40
41 return rv;
42 }
43
25 } // namespace 44 } // namespace
26 45
27 namespace net {
28
29 SdchDictionaryFetcher::SdchDictionaryFetcher( 46 SdchDictionaryFetcher::SdchDictionaryFetcher(
30 URLRequestContext* context, 47 URLRequestContext* context,
31 const OnDictionaryFetchedCallback& callback) 48 const OnDictionaryFetchedCallback& callback)
32 : next_state_(STATE_NONE), 49 : next_state_(STATE_NONE),
33 in_loop_(false), 50 in_loop_(false),
34 context_(context), 51 context_(context),
35 dictionary_fetched_callback_(callback), 52 dictionary_fetched_callback_(callback),
36 weak_factory_(this) { 53 weak_factory_(this) {
37 DCHECK(CalledOnValidThread()); 54 DCHECK(CalledOnValidThread());
38 DCHECK(context); 55 DCHECK(context);
(...skipping 19 matching lines...) Expand all
58 } 75 }
59 76
60 attempted_load_.insert(dictionary_url); 77 attempted_load_.insert(dictionary_url);
61 fetch_queue_.push(dictionary_url); 78 fetch_queue_.push(dictionary_url);
62 79
63 // If the loop is already processing, it'll pick up the above in the 80 // If the loop is already processing, it'll pick up the above in the
64 // normal course of events. 81 // normal course of events.
65 if (next_state_ != STATE_NONE) 82 if (next_state_ != STATE_NONE)
66 return; 83 return;
67 84
68 next_state_ = STATE_IDLE; 85 next_state_ = STATE_SEND_REQUEST;
69 86
70 // There are no callbacks to user code from the dictionary fetcher, 87 // There are no callbacks to user code from the dictionary fetcher,
71 // and Schedule() is only called from user code, so this call to DoLoop() 88 // and Schedule() is only called from user code, so this call to DoLoop()
72 // does not require an |if (in_loop_) return;| guard. 89 // does not require an |if (in_loop_) return;| guard.
73 DoLoop(OK); 90 DoLoop(OK);
74 } 91 }
75 92
76 void SdchDictionaryFetcher::Cancel() { 93 void SdchDictionaryFetcher::Cancel() {
77 DCHECK(CalledOnValidThread()); 94 DCHECK(CalledOnValidThread());
78 95
79 next_state_ = STATE_NONE; 96 next_state_ = STATE_NONE;
80 97
81 while (!fetch_queue_.empty()) 98 while (!fetch_queue_.empty())
82 fetch_queue_.pop(); 99 fetch_queue_.pop();
83 attempted_load_.clear(); 100 attempted_load_.clear();
84 weak_factory_.InvalidateWeakPtrs(); 101 weak_factory_.InvalidateWeakPtrs();
85 current_request_.reset(NULL); 102 current_request_.reset(NULL);
86 buffer_ = NULL; 103 buffer_ = NULL;
87 dictionary_.clear(); 104 dictionary_.clear();
88 } 105 }
89 106
90 void SdchDictionaryFetcher::OnResponseStarted(URLRequest* request) { 107 void SdchDictionaryFetcher::OnResponseStarted(URLRequest* request) {
91 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed. 108 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
92 tracked_objects::ScopedTracker tracking_profile( 109 tracked_objects::ScopedTracker tracking_profile(
93 FROM_HERE_WITH_EXPLICIT_FUNCTION( 110 FROM_HERE_WITH_EXPLICIT_FUNCTION(
94 "423948 SdchDictionaryFetcher::OnResponseStarted")); 111 "423948 SdchDictionaryFetcher::OnResponseStarted"));
95 112
96 DCHECK(CalledOnValidThread()); 113 DCHECK(CalledOnValidThread());
97 DCHECK_EQ(request, current_request_.get()); 114 DCHECK_EQ(request, current_request_.get());
98 DCHECK_EQ(next_state_, STATE_REQUEST_STARTED); 115 DCHECK_EQ(next_state_, STATE_SEND_REQUEST_COMPLETE);
99 116 DCHECK(!in_loop_);
100 // The response has started, so the stream can be read from.
101 next_state_ = STATE_REQUEST_READING;
102
103 // If this function was synchronously called, the containing
104 // state machine loop will handle the state transition. Otherwise,
105 // restart the state machine loop.
106 if (in_loop_)
107 return;
108 117
109 DoLoop(request->status().error()); 118 DoLoop(request->status().error());
110 } 119 }
111 120
112 void SdchDictionaryFetcher::OnReadCompleted(URLRequest* request, 121 void SdchDictionaryFetcher::OnReadCompleted(URLRequest* request,
113 int bytes_read) { 122 int bytes_read) {
114 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed. 123 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
115 tracked_objects::ScopedTracker tracking_profile( 124 tracked_objects::ScopedTracker tracking_profile(
116 FROM_HERE_WITH_EXPLICIT_FUNCTION( 125 FROM_HERE_WITH_EXPLICIT_FUNCTION(
117 "423948 SdchDictionaryFetcher::OnReadCompleted")); 126 "423948 SdchDictionaryFetcher::OnReadCompleted"));
118 127
119 DCHECK(CalledOnValidThread()); 128 DCHECK(CalledOnValidThread());
120 DCHECK_EQ(request, current_request_.get()); 129 DCHECK_EQ(request, current_request_.get());
121 DCHECK_EQ(next_state_, STATE_REQUEST_READING); 130 DCHECK_EQ(next_state_, STATE_READ_BODY_COMPLETE);
131 DCHECK(!in_loop_);
122 132
123 // No state transition is required in this function; the 133 DoLoop(GetReadResult(bytes_read, current_request_.get()));
124 // completion of the request is detected in DoRead().
125
126 if (request->status().is_success())
127 dictionary_.append(buffer_->data(), bytes_read);
128
129 // If this function was synchronously called, the containing
130 // state machine loop will handle the state transition. Otherwise,
131 // restart the state machine loop.
132 if (in_loop_)
133 return;
134
135 DoLoop(request->status().error());
136 } 134 }
137 135
138 int SdchDictionaryFetcher::DoLoop(int rv) { 136 int SdchDictionaryFetcher::DoLoop(int rv) {
139 DCHECK(!in_loop_); 137 DCHECK(!in_loop_);
140 base::AutoReset<bool> auto_reset_in_loop(&in_loop_, true); 138 base::AutoReset<bool> auto_reset_in_loop(&in_loop_, true);
141 139
142 do { 140 do {
143 State state = next_state_; 141 State state = next_state_;
144 next_state_ = STATE_NONE; 142 next_state_ = STATE_NONE;
145 switch (state) { 143 switch (state) {
146 case STATE_IDLE: 144 case STATE_SEND_REQUEST:
147 rv = DoDispatchRequest(rv); 145 rv = DoSendRequest(rv);
148 break; 146 break;
149 case STATE_REQUEST_STARTED: 147 case STATE_SEND_REQUEST_COMPLETE:
150 rv = DoRequestStarted(rv); 148 rv = DoSendRequestComplete(rv);
151 break; 149 break;
152 case STATE_REQUEST_READING: 150 case STATE_READ_BODY:
153 rv = DoRead(rv); 151 rv = DoReadBody(rv);
152 break;
153 case STATE_READ_BODY_COMPLETE:
154 rv = DoReadBodyComplete(rv);
154 break; 155 break;
155 case STATE_REQUEST_COMPLETE: 156 case STATE_REQUEST_COMPLETE:
156 rv = DoCompleteRequest(rv); 157 rv = DoCompleteRequest(rv);
157 break; 158 break;
158 case STATE_NONE: 159 case STATE_NONE:
159 NOTREACHED(); 160 NOTREACHED();
160 } 161 }
161 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); 162 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
162 163
163 return rv; 164 return rv;
164 } 165 }
165 166
166 int SdchDictionaryFetcher::DoDispatchRequest(int rv) { 167 int SdchDictionaryFetcher::DoSendRequest(int rv) {
167 DCHECK(CalledOnValidThread()); 168 DCHECK(CalledOnValidThread());
168 169
169 // |rv| is ignored, as the result from the previous request doesn't 170 // |rv| is ignored, as the result from the previous request doesn't
170 // affect the next request. 171 // affect the next request.
171 172
172 if (fetch_queue_.empty() || current_request_.get()) { 173 if (fetch_queue_.empty() || current_request_.get()) {
173 next_state_ = STATE_NONE; 174 next_state_ = STATE_NONE;
174 return OK; 175 return OK;
175 } 176 }
176 177
178 next_state_ = STATE_SEND_REQUEST_COMPLETE;
179
177 current_request_ = 180 current_request_ =
178 context_->CreateRequest(fetch_queue_.front(), IDLE, this, NULL); 181 context_->CreateRequest(fetch_queue_.front(), IDLE, this, NULL);
179 current_request_->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES | 182 current_request_->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES |
180 LOAD_DO_NOT_SAVE_COOKIES); 183 LOAD_DO_NOT_SAVE_COOKIES);
181 buffer_ = new IOBuffer(kBufferSize); 184 buffer_ = new IOBuffer(kBufferSize);
182 fetch_queue_.pop(); 185 fetch_queue_.pop();
183 186
184 next_state_ = STATE_REQUEST_STARTED;
185 current_request_->Start(); 187 current_request_->Start();
186 current_request_->net_log().AddEvent(NetLog::TYPE_SDCH_DICTIONARY_FETCH); 188 current_request_->net_log().AddEvent(NetLog::TYPE_SDCH_DICTIONARY_FETCH);
187 189
188 return OK;
189 }
190
191 int SdchDictionaryFetcher::DoRequestStarted(int rv) {
192 DCHECK(CalledOnValidThread());
193 DCHECK_EQ(rv, OK); // Can only come straight from above function.
194
195 // The transition to STATE_REQUEST_READING occurs in the
196 // OnResponseStarted() callback triggered by URLRequest::Start()
197 // (called in DoDispatchRequest(), above). If that callback did not
198 // occur synchronously, this routine is executed; it returns ERR_IO_PENDING,
199 // indicating to the controlling loop that no further work should be done
200 // until the callback occurs (which will re-invoke DoLoop()).
201 next_state_ = STATE_REQUEST_STARTED;
202 return ERR_IO_PENDING; 190 return ERR_IO_PENDING;
203 } 191 }
204 192
205 int SdchDictionaryFetcher::DoRead(int rv) { 193 int SdchDictionaryFetcher::DoSendRequestComplete(int rv) {
206 DCHECK(CalledOnValidThread()); 194 DCHECK(CalledOnValidThread());
207 195
208 // If there's been an error, abort the current request. 196 // If there's been an error, abort the current request.
209 if (rv != OK) { 197 if (rv != OK) {
210 current_request_.reset(); 198 current_request_.reset();
211 buffer_ = NULL; 199 buffer_ = NULL;
212 next_state_ = STATE_IDLE; 200 next_state_ = STATE_SEND_REQUEST;
213 201
214 return OK; 202 return OK;
215 } 203 }
216 204
217 next_state_ = STATE_REQUEST_READING; 205 next_state_ = STATE_READ_BODY;
206 return OK;
207 }
208
209 int SdchDictionaryFetcher::DoReadBody(int rv) {
210 DCHECK(CalledOnValidThread());
211
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;
217
218 return OK;
219 }
220
221 next_state_ = STATE_READ_BODY_COMPLETE;
218 int bytes_read = 0; 222 int bytes_read = 0;
219 current_request_->Read(buffer_.get(), kBufferSize, &bytes_read); 223 current_request_->Read(buffer_.get(), kBufferSize, &bytes_read);
220 if (current_request_->status().is_io_pending()) 224 if (current_request_->status().is_io_pending())
221 return ERR_IO_PENDING; 225 return ERR_IO_PENDING;
222 226
223 if (bytes_read < 0 || !current_request_->status().is_success()) { 227 return GetReadResult(bytes_read, current_request_.get());
224 if (current_request_->status().error() != OK) 228 }
225 return current_request_->status().error();
226 229
227 // An error with request status of OK should not happen, 230 int SdchDictionaryFetcher::DoReadBodyComplete(int rv) {
228 // but there's enough machinery underneath URLRequest::Read() 231 DCHECK(CalledOnValidThread());
229 // that this routine checks for that case. 232
230 net::Error error = 233 // An error; abort the current request.
231 current_request_->status().status() == URLRequestStatus::CANCELED ? 234 if (rv < 0) {
232 ERR_ABORTED : ERR_FAILED; 235 current_request_.reset();
233 current_request_->net_log().AddEventWithNetErrorCode( 236 buffer_ = NULL;
234 NetLog::TYPE_SDCH_DICTIONARY_FETCH_IMPLIED_ERROR, error); 237 next_state_ = STATE_SEND_REQUEST;
235 return error; 238 return OK;
236 } 239 }
237 240
238 if (bytes_read == 0) 241 DCHECK(current_request_->status().is_success());
239 next_state_ = STATE_REQUEST_COMPLETE;
240 else
241 dictionary_.append(buffer_->data(), bytes_read);
242 242
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;
248 }
249
250 // End of file; complete the request.
251 next_state_ = STATE_REQUEST_COMPLETE;
243 return OK; 252 return OK;
244 } 253 }
245 254
246 int SdchDictionaryFetcher::DoCompleteRequest(int rv) { 255 int SdchDictionaryFetcher::DoCompleteRequest(int rv) {
247 DCHECK(CalledOnValidThread()); 256 DCHECK(CalledOnValidThread());
248 257
249 // If the dictionary was successfully fetched, add it to the manager. 258 // DoReadBodyComplete() only transitions to this state
250 if (rv == OK) { 259 // on success.
251 dictionary_fetched_callback_.Run(dictionary_, current_request_->url(), 260 DCHECK_EQ(OK, rv);
252 current_request_->net_log());
253 }
254 261
262 dictionary_fetched_callback_.Run(dictionary_, current_request_->url(),
263 current_request_->net_log());
255 current_request_.reset(); 264 current_request_.reset();
256 buffer_ = NULL; 265 buffer_ = NULL;
257 dictionary_.clear(); 266 dictionary_.clear();
258 267
259 next_state_ = STATE_IDLE; 268 next_state_ = STATE_SEND_REQUEST;
260 269
261 return OK; 270 return OK;
262 } 271 }
263 272
264 } // namespace net 273 } // namespace net
OLDNEW
« no previous file with comments | « net/url_request/sdch_dictionary_fetcher.h ('k') | net/url_request/sdch_dictionary_fetcher_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698