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

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

Issue 901303002: Make SDCH dictionaries persistent across browser restart. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Beef up SdchDictionaryFetcher tests 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/net_log.h"
17 #include "net/base/sdch_net_log_params.h" 17 #include "net/base/sdch_net_log_params.h"
18 #include "net/http/http_response_headers.h"
18 #include "net/url_request/url_request_context.h" 19 #include "net/url_request/url_request_context.h"
19 #include "net/url_request/url_request_status.h" 20 #include "net/url_request/url_request_status.h"
20 #include "net/url_request/url_request_throttler_manager.h" 21 #include "net/url_request/url_request_throttler_manager.h"
21 22
22 namespace net { 23 namespace net {
23 24
24 namespace { 25 namespace {
25 26
26 const int kBufferSize = 4096; 27 const int kBufferSize = 4096;
27 28
28 // Map the bytes_read result from a read attempt and a URLRequest's 29 // Map the bytes_read result from a read attempt and a URLRequest's
29 // status into a single net return value. 30 // status into a single net return value.
30 int GetReadResult(int bytes_read, const URLRequest* request) { 31 int GetReadResult(int bytes_read, const URLRequest* request) {
31 int rv = request->status().error(); 32 int rv = request->status().error();
32 if (request->status().is_success() && bytes_read < 0) { 33 if (request->status().is_success() && bytes_read < 0) {
33 rv = ERR_FAILED; 34 rv = ERR_FAILED;
34 request->net_log().AddEventWithNetErrorCode( 35 request->net_log().AddEventWithNetErrorCode(
35 NetLog::TYPE_SDCH_DICTIONARY_FETCH_IMPLIED_ERROR, rv); 36 NetLog::TYPE_SDCH_DICTIONARY_FETCH_IMPLIED_ERROR, rv);
36 } 37 }
37 38
38 if (rv == OK) 39 if (rv == OK)
39 rv = bytes_read; 40 rv = bytes_read;
40 41
41 return rv; 42 return rv;
42 } 43 }
43 44
45 struct FetchInfo {
46 FetchInfo(const GURL& url,
47 bool cache_only,
48 const SdchDictionaryFetcher::OnDictionaryFetchedCallback& callback)
49 : url(url), cache_only(cache_only), callback(callback) {}
50 FetchInfo() {}
51 GURL url;
mmenke 2015/02/26 17:27:20 blank line before url.
Elly Fong-Jones 2015/03/03 21:37:46 Done.
52 bool cache_only;
53 SdchDictionaryFetcher::OnDictionaryFetchedCallback callback;
54 };
55
56 // A UniqueFetchQueue is used to queue outgoing requests, which are either cache
57 // requests or network requests (which *may* still be served from cache).
58 // The UniqueFetchQueue enforces that a dictionary can be loaded-from-cache and
59 // loaded-from-network each at most once, and that if a dictionary has been
60 // loaded-from-network, it cannot subsequently be loaded-from-cache. Practically
61 // this means there are three valid patterns of requests for a URL u:
62 // 1. load-from-cache(u)
63 // 2. load-from-network(u)
64 // 3. load-from-cache(u); load-from-network(u)
65 // Any fetches that would violate these invariants are rejected by Push().
66 class UniqueFetchQueue {
67 public:
68 UniqueFetchQueue();
69 ~UniqueFetchQueue();
70
71 bool Push(const FetchInfo& info);
72 bool Pop(FetchInfo* info);
73 bool IsEmpty() const;
74 void ForgetPriorFetches();
Randy Smith (Not in Mondays) 2015/02/23 21:23:49 nit, suggestion: Name change to Clear()?
Elly Fong-Jones 2015/03/03 21:37:45 Done.
75 private:
mmenke 2015/02/26 17:27:20 nit: Blank line before private.
Elly Fong-Jones 2015/03/03 21:37:45 Done.
76 std::list<FetchInfo> queue_;
mmenke 2015/02/26 17:27:20 Any reason this is a list and not a queue? Also,
Elly Fong-Jones 2015/03/03 21:37:45 Oops, it was meant to be a queue.
77 std::set<GURL> ever_cache_fetched_;
mmenke 2015/02/26 17:27:20 Do we even need this? We load a dictionary of URL
Elly Fong-Jones 2015/03/03 21:37:45 Done.
78 std::set<GURL> ever_fetched_;
Randy Smith (Not in Mondays) 2015/02/23 21:23:49 nit, suggestion: This is a bit wasteful of space;
mmenke 2015/02/26 17:27:20 +1.
mmenke 2015/02/26 17:27:20 The fact that something that was just fetched from
mmenke 2015/02/26 17:27:20 DISALLOW_COPY_AND_ASSIGN
Elly Fong-Jones 2015/03/03 21:37:45 Done.
Elly Fong-Jones 2015/03/03 21:37:46 Done.
Elly Fong-Jones 2015/03/03 21:37:48 Done.
Elly Fong-Jones 2015/03/03 21:37:48 Done.
79 };
80
81 UniqueFetchQueue::UniqueFetchQueue() {}
82 UniqueFetchQueue::~UniqueFetchQueue() {}
83
84 bool UniqueFetchQueue::Push(const FetchInfo& info) {
85 if (info.cache_only) {
86 if (ever_cache_fetched_.count(info.url) != 0 ||
87 ever_fetched_.count(info.url) != 0) {
88 return false;
89 }
90 ever_cache_fetched_.insert(info.url);
91 } else {
92 if (ever_fetched_.count(info.url) != 0) {
mmenke 2015/02/26 17:27:20 optional: Think it's a bit clearer if we do this
Elly Fong-Jones 2015/03/03 21:37:46 Done.
93 return false;
94 }
mmenke 2015/02/26 17:27:20 nit: Don't use braces with single-line if's.
Elly Fong-Jones 2015/03/03 21:37:45 Done.
95 ever_fetched_.insert(info.url);
96 }
Randy Smith (Not in Mondays) 2015/02/23 21:23:49 suggestion: I'd find this code easier to read if i
Elly Fong-Jones 2015/03/03 21:37:46 Done.
97 queue_.push_back(info);
98 return true;
99 }
100
101 bool UniqueFetchQueue::Pop(FetchInfo* info) {
102 if (IsEmpty())
103 return false;
104 *info = queue_.front();
105 queue_.pop_front();
106 return true;
107 }
108
109 bool UniqueFetchQueue::IsEmpty() const {
110 return queue_.empty();
111 }
112
113 void UniqueFetchQueue::ForgetPriorFetches() {
114 ever_cache_fetched_.clear();
115 ever_fetched_.clear();
116 }
117
44 } // namespace 118 } // namespace
45 119
46 SdchDictionaryFetcher::SdchDictionaryFetcher( 120 SdchDictionaryFetcher::SdchDictionaryFetcher(URLRequestContext* context)
47 URLRequestContext* context,
48 const OnDictionaryFetchedCallback& callback)
49 : next_state_(STATE_NONE), 121 : next_state_(STATE_NONE),
50 in_loop_(false), 122 in_loop_(false),
123 fetch_queue_(new UniqueFetchQueue()),
51 context_(context), 124 context_(context),
52 dictionary_fetched_callback_(callback),
53 weak_factory_(this) { 125 weak_factory_(this) {
54 DCHECK(CalledOnValidThread()); 126 DCHECK(CalledOnValidThread());
55 DCHECK(context); 127 DCHECK(context);
56 } 128 }
57 129
58 SdchDictionaryFetcher::~SdchDictionaryFetcher() { 130 SdchDictionaryFetcher::~SdchDictionaryFetcher() {
59 DCHECK(CalledOnValidThread());
60 } 131 }
61 132
62 void SdchDictionaryFetcher::Schedule(const GURL& dictionary_url) { 133 bool SdchDictionaryFetcher::Schedule(
63 DCHECK(CalledOnValidThread()); 134 const GURL& dictionary_url,
135 const OnDictionaryFetchedCallback& callback) {
136 return ScheduleInternal(dictionary_url, false, callback);
137 }
64 138
65 // Avoid pushing duplicate copy onto queue. We may fetch this url again later 139 bool SdchDictionaryFetcher::ScheduleReload(
66 // and get a different dictionary, but there is no reason to have it in the 140 const GURL& dictionary_url,
67 // queue twice at one time. 141 const OnDictionaryFetchedCallback& callback) {
68 if ((!fetch_queue_.empty() && fetch_queue_.back() == dictionary_url) || 142 return ScheduleInternal(dictionary_url, true, callback);
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;
75 }
76
77 attempted_load_.insert(dictionary_url);
78 fetch_queue_.push(dictionary_url);
79
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;
84
85 next_state_ = STATE_SEND_REQUEST;
86
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);
91 } 143 }
92 144
93 void SdchDictionaryFetcher::Cancel() { 145 void SdchDictionaryFetcher::Cancel() {
94 DCHECK(CalledOnValidThread()); 146 DCHECK(CalledOnValidThread());
95 147
96 next_state_ = STATE_NONE; 148 next_state_ = STATE_NONE;
149 current_request_.reset();
150 buffer_ = nullptr;
151 current_callback_.Reset();
mmenke 2015/02/26 17:27:20 This looks a lot like ResetRequest...In fact, we'r
Elly Fong-Jones 2015/03/03 21:37:48 Done.
97 152
98 while (!fetch_queue_.empty()) 153 fetch_queue_->ForgetPriorFetches();
99 fetch_queue_.pop();
100 attempted_load_.clear();
101 weak_factory_.InvalidateWeakPtrs(); 154 weak_factory_.InvalidateWeakPtrs();
102 current_request_.reset(NULL);
103 buffer_ = NULL;
104 dictionary_.clear(); 155 dictionary_.clear();
105 } 156 }
106 157
107 void SdchDictionaryFetcher::OnResponseStarted(URLRequest* request) { 158 void SdchDictionaryFetcher::OnResponseStarted(URLRequest* request) {
108 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed. 159 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
109 tracked_objects::ScopedTracker tracking_profile( 160 tracked_objects::ScopedTracker tracking_profile(
110 FROM_HERE_WITH_EXPLICIT_FUNCTION( 161 FROM_HERE_WITH_EXPLICIT_FUNCTION(
111 "423948 SdchDictionaryFetcher::OnResponseStarted")); 162 "423948 SdchDictionaryFetcher::OnResponseStarted"));
112 163
113 DCHECK(CalledOnValidThread()); 164 DCHECK(CalledOnValidThread());
114 DCHECK_EQ(request, current_request_.get()); 165 DCHECK_EQ(request, current_request_.get());
115 DCHECK_EQ(next_state_, STATE_SEND_REQUEST_COMPLETE); 166 DCHECK_EQ(next_state_, STATE_SEND_REQUEST_COMPLETE);
116 DCHECK(!in_loop_); 167 DCHECK(!in_loop_);
117 168
118 DoLoop(request->status().error()); 169 // Confirm that the response isn't a stale read from the cache (as
170 // may happen in the reload case). If the response was not retrieved over
171 // HTTP, it is presumed to be fresh.
172 HttpResponseHeaders* response_headers = request->response_headers();
173 int result = request->status().error();
174 if (result == OK && response_headers) {
175 ValidationType validation_type = response_headers->RequiresValidation(
176 request->response_info().request_time,
177 request->response_info().response_time, base::Time::Now());
178 // TODO(rdsmith): Maybe handle VALIDATION_ASYNCHRONOUS by queueing
179 // a non-reload request for the dictionary.
180 if (validation_type != VALIDATION_NONE) {
181 result = ERR_FAILED;
182 }
mmenke 2015/02/26 17:27:20 nit: Don't use braces on 2 line ifs.
Elly Fong-Jones 2015/03/03 21:37:45 Done.
183 }
184
185 DoLoop(result);
119 } 186 }
120 187
121 void SdchDictionaryFetcher::OnReadCompleted(URLRequest* request, 188 void SdchDictionaryFetcher::OnReadCompleted(URLRequest* request,
122 int bytes_read) { 189 int bytes_read) {
123 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed. 190 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
124 tracked_objects::ScopedTracker tracking_profile( 191 tracked_objects::ScopedTracker tracking_profile(
125 FROM_HERE_WITH_EXPLICIT_FUNCTION( 192 FROM_HERE_WITH_EXPLICIT_FUNCTION(
126 "423948 SdchDictionaryFetcher::OnReadCompleted")); 193 "423948 SdchDictionaryFetcher::OnReadCompleted"));
127 194
128 DCHECK(CalledOnValidThread()); 195 DCHECK(CalledOnValidThread());
129 DCHECK_EQ(request, current_request_.get()); 196 DCHECK_EQ(request, current_request_.get());
130 DCHECK_EQ(next_state_, STATE_READ_BODY_COMPLETE); 197 DCHECK_EQ(next_state_, STATE_READ_BODY_COMPLETE);
131 DCHECK(!in_loop_); 198 DCHECK(!in_loop_);
132 199
133 DoLoop(GetReadResult(bytes_read, current_request_.get())); 200 DoLoop(GetReadResult(bytes_read, current_request_.get()));
134 } 201 }
135 202
203 bool SdchDictionaryFetcher::ScheduleInternal(
204 const GURL& dictionary_url,
205 bool reload,
206 const OnDictionaryFetchedCallback& callback) {
207 DCHECK(CalledOnValidThread());
208
209 // If Push() fails, |dictionary_url| has already been fetched or scheduled to
210 // be fetched.
211 if (!fetch_queue_->Push(FetchInfo(dictionary_url, reload, callback))) {
212 // TODO(rdsmith): Log this error to the net log. In the case of a
213 // normal fetch, this can be through the URLRequest
214 // initiating this fetch (once the URLRequest is passed to the fetcher);
215 // in the case of a reload, it's more complicated.
216 SdchManager::SdchErrorRecovery(
217 SDCH_DICTIONARY_PREVIOUSLY_SCHEDULED_TO_DOWNLOAD);
218 return false;
219 }
220
221 // If the loop is already processing, it'll pick up the above in the
222 // normal course of events.
223 if (next_state_ != STATE_NONE)
224 return true;
225
226 next_state_ = STATE_SEND_REQUEST;
227
228 // There are no callbacks to user code from the dictionary fetcher,
229 // and Schedule() is only called from user code, so this call to DoLoop()
230 // does not require an |if (in_loop_) return;| guard.
231 DoLoop(OK);
232 return true;
233 }
234
235 void SdchDictionaryFetcher::ResetRequest() {
236 current_request_.reset();
237 buffer_ = nullptr;
238 current_callback_.Reset();
239 next_state_ = STATE_SEND_REQUEST;
240 dictionary_.clear();
241 return;
242 }
243
136 int SdchDictionaryFetcher::DoLoop(int rv) { 244 int SdchDictionaryFetcher::DoLoop(int rv) {
137 DCHECK(!in_loop_); 245 DCHECK(!in_loop_);
138 base::AutoReset<bool> auto_reset_in_loop(&in_loop_, true); 246 base::AutoReset<bool> auto_reset_in_loop(&in_loop_, true);
139 247
140 do { 248 do {
141 State state = next_state_; 249 State state = next_state_;
142 next_state_ = STATE_NONE; 250 next_state_ = STATE_NONE;
143 switch (state) { 251 switch (state) {
144 case STATE_SEND_REQUEST: 252 case STATE_SEND_REQUEST:
145 rv = DoSendRequest(rv); 253 rv = DoSendRequest(rv);
(...skipping 17 matching lines...) Expand all
163 271
164 return rv; 272 return rv;
165 } 273 }
166 274
167 int SdchDictionaryFetcher::DoSendRequest(int rv) { 275 int SdchDictionaryFetcher::DoSendRequest(int rv) {
168 DCHECK(CalledOnValidThread()); 276 DCHECK(CalledOnValidThread());
169 277
170 // |rv| is ignored, as the result from the previous request doesn't 278 // |rv| is ignored, as the result from the previous request doesn't
171 // affect the next request. 279 // affect the next request.
172 280
173 if (fetch_queue_.empty() || current_request_.get()) { 281 if (fetch_queue_->IsEmpty() || current_request_.get()) {
174 next_state_ = STATE_NONE; 282 next_state_ = STATE_NONE;
175 return OK; 283 return OK;
176 } 284 }
177 285
178 next_state_ = STATE_SEND_REQUEST_COMPLETE; 286 next_state_ = STATE_SEND_REQUEST_COMPLETE;
179 287
288 FetchInfo info;
289 fetch_queue_->Pop(&info);
Randy Smith (Not in Mondays) 2015/02/23 21:23:49 DCHECK that return value is true?
Elly Fong-Jones 2015/03/03 21:37:46 Done.
180 current_request_ = 290 current_request_ =
181 context_->CreateRequest(fetch_queue_.front(), IDLE, this, NULL); 291 context_->CreateRequest(info.url, IDLE, this, NULL);
182 current_request_->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES | 292 int load_flags = LOAD_DO_NOT_SEND_COOKIES | LOAD_DO_NOT_SAVE_COOKIES;
183 LOAD_DO_NOT_SAVE_COOKIES); 293 if (info.cache_only)
294 load_flags |= LOAD_ONLY_FROM_CACHE;
295 current_request_->SetLoadFlags(load_flags);
296
184 buffer_ = new IOBuffer(kBufferSize); 297 buffer_ = new IOBuffer(kBufferSize);
185 fetch_queue_.pop(); 298 current_callback_ = info.callback;
186 299
187 current_request_->Start(); 300 current_request_->Start();
188 current_request_->net_log().AddEvent(NetLog::TYPE_SDCH_DICTIONARY_FETCH); 301 current_request_->net_log().AddEvent(NetLog::TYPE_SDCH_DICTIONARY_FETCH);
189 302
190 return ERR_IO_PENDING; 303 return ERR_IO_PENDING;
191 } 304 }
192 305
193 int SdchDictionaryFetcher::DoSendRequestComplete(int rv) { 306 int SdchDictionaryFetcher::DoSendRequestComplete(int rv) {
194 DCHECK(CalledOnValidThread()); 307 DCHECK(CalledOnValidThread());
195 308
196 // If there's been an error, abort the current request. 309 // If there's been an error, abort the current request.
197 if (rv != OK) { 310 if (rv != OK) {
198 current_request_.reset(); 311 current_request_.reset();
199 buffer_ = NULL; 312 buffer_ = NULL;
200 next_state_ = STATE_SEND_REQUEST; 313 next_state_ = STATE_SEND_REQUEST;
201 314
202 return OK; 315 return OK;
203 } 316 }
204 317
205 next_state_ = STATE_READ_BODY; 318 next_state_ = STATE_READ_BODY;
206 return OK; 319 return OK;
207 } 320 }
208 321
209 int SdchDictionaryFetcher::DoReadBody(int rv) { 322 int SdchDictionaryFetcher::DoReadBody(int rv) {
210 DCHECK(CalledOnValidThread()); 323 DCHECK(CalledOnValidThread());
211 324
212 // If there's been an error, abort the current request. 325 // If there's been an error, abort the current request.
213 if (rv != OK) { 326 if (rv != OK) {
214 current_request_.reset(); 327 ResetRequest();
215 buffer_ = NULL;
216 next_state_ = STATE_SEND_REQUEST;
217
218 return OK; 328 return OK;
219 } 329 }
220 330
221 next_state_ = STATE_READ_BODY_COMPLETE; 331 next_state_ = STATE_READ_BODY_COMPLETE;
222 int bytes_read = 0; 332 int bytes_read = 0;
223 current_request_->Read(buffer_.get(), kBufferSize, &bytes_read); 333 current_request_->Read(buffer_.get(), kBufferSize, &bytes_read);
224 if (current_request_->status().is_io_pending()) 334 if (current_request_->status().is_io_pending())
225 return ERR_IO_PENDING; 335 return ERR_IO_PENDING;
226 336
227 return GetReadResult(bytes_read, current_request_.get()); 337 return GetReadResult(bytes_read, current_request_.get());
(...skipping 20 matching lines...) Expand all
248 } 358 }
249 359
250 // End of file; complete the request. 360 // End of file; complete the request.
251 next_state_ = STATE_REQUEST_COMPLETE; 361 next_state_ = STATE_REQUEST_COMPLETE;
252 return OK; 362 return OK;
253 } 363 }
254 364
255 int SdchDictionaryFetcher::DoCompleteRequest(int rv) { 365 int SdchDictionaryFetcher::DoCompleteRequest(int rv) {
256 DCHECK(CalledOnValidThread()); 366 DCHECK(CalledOnValidThread());
257 367
258 // DoReadBodyComplete() only transitions to this state 368 // If the dictionary was successfully fetched, add it to the manager.
259 // on success. 369 if (rv == OK) {
260 DCHECK_EQ(OK, rv); 370 current_callback_.Run(dictionary_, current_request_->url(),
371 current_request_->net_log());
372 }
261 373
262 dictionary_fetched_callback_.Run(dictionary_, current_request_->url(), 374 ResetRequest();
263 current_request_->net_log());
264 current_request_.reset();
265 buffer_ = NULL;
266 dictionary_.clear();
267
268 next_state_ = STATE_SEND_REQUEST;
269
270 return OK; 375 return OK;
271 } 376 }
272 377
273 } // namespace net 378 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698