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

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: More fixes Created 5 years, 9 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 #include <queue>
9 #include <set>
8 10
9 #include "base/auto_reset.h" 11 #include "base/auto_reset.h"
10 #include "base/bind.h" 12 #include "base/bind.h"
11 #include "base/compiler_specific.h" 13 #include "base/compiler_specific.h"
12 #include "base/profiler/scoped_tracker.h" 14 #include "base/profiler/scoped_tracker.h"
13 #include "base/thread_task_runner_handle.h" 15 #include "base/thread_task_runner_handle.h"
14 #include "net/base/io_buffer.h" 16 #include "net/base/io_buffer.h"
15 #include "net/base/load_flags.h" 17 #include "net/base/load_flags.h"
16 #include "net/base/net_log.h" 18 #include "net/base/net_log.h"
17 #include "net/base/sdch_net_log_params.h" 19 #include "net/base/sdch_net_log_params.h"
20 #include "net/http/http_response_headers.h"
18 #include "net/url_request/url_request_context.h" 21 #include "net/url_request/url_request_context.h"
19 #include "net/url_request/url_request_status.h" 22 #include "net/url_request/url_request_status.h"
20 #include "net/url_request/url_request_throttler_manager.h" 23 #include "net/url_request/url_request_throttler_manager.h"
21 24
22 namespace net { 25 namespace net {
23 26
24 namespace { 27 namespace {
25 28
26 const int kBufferSize = 4096; 29 const int kBufferSize = 4096;
27 30
28 // Map the bytes_read result from a read attempt and a URLRequest's 31 // Map the bytes_read result from a read attempt and a URLRequest's
29 // status into a single net return value. 32 // status into a single net return value.
30 int GetReadResult(int bytes_read, const URLRequest* request) { 33 int GetReadResult(int bytes_read, const URLRequest* request) {
31 int rv = request->status().error(); 34 int rv = request->status().error();
32 if (request->status().is_success() && bytes_read < 0) { 35 if (request->status().is_success() && bytes_read < 0) {
33 rv = ERR_FAILED; 36 rv = ERR_FAILED;
34 request->net_log().AddEventWithNetErrorCode( 37 request->net_log().AddEventWithNetErrorCode(
35 NetLog::TYPE_SDCH_DICTIONARY_FETCH_IMPLIED_ERROR, rv); 38 NetLog::TYPE_SDCH_DICTIONARY_FETCH_IMPLIED_ERROR, rv);
36 } 39 }
37 40
38 if (rv == OK) 41 if (rv == OK)
39 rv = bytes_read; 42 rv = bytes_read;
40 43
41 return rv; 44 return rv;
42 } 45 }
43 46
47 struct FetchInfo {
48 FetchInfo(const GURL& url,
49 bool cache_only,
50 const SdchDictionaryFetcher::OnDictionaryFetchedCallback& callback)
51 : url(url), cache_only(cache_only), callback(callback) {}
52 FetchInfo() {}
53
54 GURL url;
55 bool cache_only;
56 SdchDictionaryFetcher::OnDictionaryFetchedCallback callback;
57 };
58
44 } // namespace 59 } // namespace
45 60
46 SdchDictionaryFetcher::SdchDictionaryFetcher( 61 // A UniqueFetchQueue is used to queue outgoing requests, which are either cache
47 URLRequestContext* context, 62 // requests or network requests (which *may* still be served from cache).
48 const OnDictionaryFetchedCallback& callback) 63 // The UniqueFetchQueue enforces that a URL can only be queued for network fetch
64 // at most once. Calling Clear() resets UniqueFetchQueue's memory of which URLs
65 // have been queued.
66 class SdchDictionaryFetcher::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 Clear();
75
76 private:
77 std::queue<FetchInfo> queue_;
78 std::set<GURL> ever_network_queued_;
79 DISALLOW_COPY_AND_ASSIGN(UniqueFetchQueue);
mmenke 2015/03/03 21:51:53 nit: Blank line before DISALLOW_COPY_AND_ASSIGN.
Elly Fong-Jones 2015/03/04 15:43:41 Done.
80 };
81
82 SdchDictionaryFetcher::UniqueFetchQueue::UniqueFetchQueue() {}
83 SdchDictionaryFetcher::UniqueFetchQueue::~UniqueFetchQueue() {}
84
85 bool SdchDictionaryFetcher::UniqueFetchQueue::Push(const FetchInfo& info) {
86 if (ever_network_queued_.count(info.url) != 0)
87 return false;
88 if (!info.cache_only)
89 ever_network_queued_.insert(info.url);
90 queue_.push(info);
91 return true;
92 }
93
94 bool SdchDictionaryFetcher::UniqueFetchQueue::Pop(FetchInfo* info) {
95 if (IsEmpty())
96 return false;
97 *info = queue_.front();
98 queue_.pop();
99 return true;
100 }
101
102 bool SdchDictionaryFetcher::UniqueFetchQueue::IsEmpty() const {
103 return queue_.empty();
104 }
105
106 void SdchDictionaryFetcher::UniqueFetchQueue::Clear() {
107 ever_network_queued_.clear();
108 while (!queue_.empty())
109 queue_.pop();
110 }
111
112 SdchDictionaryFetcher::SdchDictionaryFetcher(URLRequestContext* context)
49 : next_state_(STATE_NONE), 113 : next_state_(STATE_NONE),
50 in_loop_(false), 114 in_loop_(false),
115 fetch_queue_(new UniqueFetchQueue()),
51 context_(context), 116 context_(context),
52 dictionary_fetched_callback_(callback),
53 weak_factory_(this) { 117 weak_factory_(this) {
54 DCHECK(CalledOnValidThread()); 118 DCHECK(CalledOnValidThread());
55 DCHECK(context); 119 DCHECK(context);
56 } 120 }
57 121
58 SdchDictionaryFetcher::~SdchDictionaryFetcher() { 122 SdchDictionaryFetcher::~SdchDictionaryFetcher() {
59 DCHECK(CalledOnValidThread());
60 } 123 }
61 124
62 void SdchDictionaryFetcher::Schedule(const GURL& dictionary_url) { 125 bool SdchDictionaryFetcher::Schedule(
63 DCHECK(CalledOnValidThread()); 126 const GURL& dictionary_url,
127 const OnDictionaryFetchedCallback& callback) {
128 return ScheduleInternal(dictionary_url, false, callback);
129 }
64 130
65 // Avoid pushing duplicate copy onto queue. We may fetch this url again later 131 bool SdchDictionaryFetcher::ScheduleReload(
66 // and get a different dictionary, but there is no reason to have it in the 132 const GURL& dictionary_url,
67 // queue twice at one time. 133 const OnDictionaryFetchedCallback& callback) {
68 if ((!fetch_queue_.empty() && fetch_queue_.back() == dictionary_url) || 134 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 } 135 }
92 136
93 void SdchDictionaryFetcher::Cancel() { 137 void SdchDictionaryFetcher::Cancel() {
94 DCHECK(CalledOnValidThread()); 138 DCHECK(CalledOnValidThread());
95 139
140 ResetRequest();
96 next_state_ = STATE_NONE; 141 next_state_ = STATE_NONE;
97 142
98 while (!fetch_queue_.empty()) 143 fetch_queue_->Clear();
99 fetch_queue_.pop();
100 attempted_load_.clear();
101 weak_factory_.InvalidateWeakPtrs(); 144 weak_factory_.InvalidateWeakPtrs();
102 current_request_.reset(NULL);
103 buffer_ = NULL;
104 dictionary_.clear();
105 } 145 }
106 146
107 void SdchDictionaryFetcher::OnResponseStarted(URLRequest* request) { 147 void SdchDictionaryFetcher::OnResponseStarted(URLRequest* request) {
108 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed. 148 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
109 tracked_objects::ScopedTracker tracking_profile( 149 tracked_objects::ScopedTracker tracking_profile(
110 FROM_HERE_WITH_EXPLICIT_FUNCTION( 150 FROM_HERE_WITH_EXPLICIT_FUNCTION(
111 "423948 SdchDictionaryFetcher::OnResponseStarted")); 151 "423948 SdchDictionaryFetcher::OnResponseStarted"));
112 152
113 DCHECK(CalledOnValidThread()); 153 DCHECK(CalledOnValidThread());
114 DCHECK_EQ(request, current_request_.get()); 154 DCHECK_EQ(request, current_request_.get());
115 DCHECK_EQ(next_state_, STATE_SEND_REQUEST_COMPLETE); 155 DCHECK_EQ(next_state_, STATE_SEND_REQUEST_COMPLETE);
116 DCHECK(!in_loop_); 156 DCHECK(!in_loop_);
117 157
118 DoLoop(request->status().error()); 158 // Confirm that the response isn't a stale read from the cache (as
159 // may happen in the reload case). If the response was not retrieved over
160 // HTTP, it is presumed to be fresh.
161 HttpResponseHeaders* response_headers = request->response_headers();
162 int result = request->status().error();
163 if (result == OK && response_headers) {
164 ValidationType validation_type = response_headers->RequiresValidation(
165 request->response_info().request_time,
166 request->response_info().response_time, base::Time::Now());
167 // TODO(rdsmith): Maybe handle VALIDATION_ASYNCHRONOUS by queueing
168 // a non-reload request for the dictionary.
169 if (validation_type != VALIDATION_NONE)
170 result = ERR_FAILED;
171 }
172
173 DoLoop(result);
119 } 174 }
120 175
121 void SdchDictionaryFetcher::OnReadCompleted(URLRequest* request, 176 void SdchDictionaryFetcher::OnReadCompleted(URLRequest* request,
122 int bytes_read) { 177 int bytes_read) {
123 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed. 178 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
124 tracked_objects::ScopedTracker tracking_profile( 179 tracked_objects::ScopedTracker tracking_profile(
125 FROM_HERE_WITH_EXPLICIT_FUNCTION( 180 FROM_HERE_WITH_EXPLICIT_FUNCTION(
126 "423948 SdchDictionaryFetcher::OnReadCompleted")); 181 "423948 SdchDictionaryFetcher::OnReadCompleted"));
127 182
128 DCHECK(CalledOnValidThread()); 183 DCHECK(CalledOnValidThread());
129 DCHECK_EQ(request, current_request_.get()); 184 DCHECK_EQ(request, current_request_.get());
130 DCHECK_EQ(next_state_, STATE_READ_BODY_COMPLETE); 185 DCHECK_EQ(next_state_, STATE_READ_BODY_COMPLETE);
131 DCHECK(!in_loop_); 186 DCHECK(!in_loop_);
132 187
133 DoLoop(GetReadResult(bytes_read, current_request_.get())); 188 DoLoop(GetReadResult(bytes_read, current_request_.get()));
134 } 189 }
135 190
191 bool SdchDictionaryFetcher::ScheduleInternal(
192 const GURL& dictionary_url,
193 bool reload,
194 const OnDictionaryFetchedCallback& callback) {
195 DCHECK(CalledOnValidThread());
196
197 // If Push() fails, |dictionary_url| has already been fetched or scheduled to
198 // be fetched.
199 if (!fetch_queue_->Push(FetchInfo(dictionary_url, reload, callback))) {
200 // TODO(rdsmith): Log this error to the net log. In the case of a
201 // normal fetch, this can be through the URLRequest
202 // initiating this fetch (once the URLRequest is passed to the fetcher);
203 // in the case of a reload, it's more complicated.
204 SdchManager::SdchErrorRecovery(
205 SDCH_DICTIONARY_PREVIOUSLY_SCHEDULED_TO_DOWNLOAD);
206 return false;
207 }
208
209 // If the loop is already processing, it'll pick up the above in the
210 // normal course of events.
211 if (next_state_ != STATE_NONE)
212 return true;
213
214 next_state_ = STATE_SEND_REQUEST;
215
216 // There are no callbacks to user code from the dictionary fetcher,
217 // and Schedule() is only called from user code, so this call to DoLoop()
218 // does not require an |if (in_loop_) return;| guard.
219 DoLoop(OK);
220 return true;
221 }
222
223 void SdchDictionaryFetcher::ResetRequest() {
224 current_request_.reset();
225 buffer_ = nullptr;
226 current_callback_.Reset();
227 dictionary_.clear();
228 return;
229 }
230
136 int SdchDictionaryFetcher::DoLoop(int rv) { 231 int SdchDictionaryFetcher::DoLoop(int rv) {
137 DCHECK(!in_loop_); 232 DCHECK(!in_loop_);
138 base::AutoReset<bool> auto_reset_in_loop(&in_loop_, true); 233 base::AutoReset<bool> auto_reset_in_loop(&in_loop_, true);
139 234
140 do { 235 do {
141 State state = next_state_; 236 State state = next_state_;
142 next_state_ = STATE_NONE; 237 next_state_ = STATE_NONE;
143 switch (state) { 238 switch (state) {
144 case STATE_SEND_REQUEST: 239 case STATE_SEND_REQUEST:
145 rv = DoSendRequest(rv); 240 rv = DoSendRequest(rv);
(...skipping 17 matching lines...) Expand all
163 258
164 return rv; 259 return rv;
165 } 260 }
166 261
167 int SdchDictionaryFetcher::DoSendRequest(int rv) { 262 int SdchDictionaryFetcher::DoSendRequest(int rv) {
168 DCHECK(CalledOnValidThread()); 263 DCHECK(CalledOnValidThread());
169 264
170 // |rv| is ignored, as the result from the previous request doesn't 265 // |rv| is ignored, as the result from the previous request doesn't
171 // affect the next request. 266 // affect the next request.
172 267
173 if (fetch_queue_.empty() || current_request_.get()) { 268 if (fetch_queue_->IsEmpty() || current_request_.get()) {
174 next_state_ = STATE_NONE; 269 next_state_ = STATE_NONE;
175 return OK; 270 return OK;
176 } 271 }
177 272
178 next_state_ = STATE_SEND_REQUEST_COMPLETE; 273 next_state_ = STATE_SEND_REQUEST_COMPLETE;
179 274
275 FetchInfo info;
276 bool success = fetch_queue_->Pop(&info);
277 DCHECK(success);
180 current_request_ = 278 current_request_ =
181 context_->CreateRequest(fetch_queue_.front(), IDLE, this, NULL); 279 context_->CreateRequest(info.url, IDLE, this, NULL);
182 current_request_->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES | 280 int load_flags = LOAD_DO_NOT_SEND_COOKIES | LOAD_DO_NOT_SAVE_COOKIES;
183 LOAD_DO_NOT_SAVE_COOKIES); 281 if (info.cache_only)
282 load_flags |= LOAD_ONLY_FROM_CACHE;
283 current_request_->SetLoadFlags(load_flags);
284
184 buffer_ = new IOBuffer(kBufferSize); 285 buffer_ = new IOBuffer(kBufferSize);
185 fetch_queue_.pop(); 286 current_callback_ = info.callback;
186 287
187 current_request_->Start(); 288 current_request_->Start();
188 current_request_->net_log().AddEvent(NetLog::TYPE_SDCH_DICTIONARY_FETCH); 289 current_request_->net_log().AddEvent(NetLog::TYPE_SDCH_DICTIONARY_FETCH);
189 290
190 return ERR_IO_PENDING; 291 return ERR_IO_PENDING;
191 } 292 }
192 293
193 int SdchDictionaryFetcher::DoSendRequestComplete(int rv) { 294 int SdchDictionaryFetcher::DoSendRequestComplete(int rv) {
194 DCHECK(CalledOnValidThread()); 295 DCHECK(CalledOnValidThread());
195 296
196 // If there's been an error, abort the current request. 297 // If there's been an error, abort the current request.
197 if (rv != OK) { 298 if (rv != OK) {
198 current_request_.reset(); 299 current_request_.reset();
199 buffer_ = NULL; 300 buffer_ = NULL;
200 next_state_ = STATE_SEND_REQUEST; 301 next_state_ = STATE_SEND_REQUEST;
201 302
202 return OK; 303 return OK;
203 } 304 }
204 305
205 next_state_ = STATE_READ_BODY; 306 next_state_ = STATE_READ_BODY;
206 return OK; 307 return OK;
207 } 308 }
208 309
209 int SdchDictionaryFetcher::DoReadBody(int rv) { 310 int SdchDictionaryFetcher::DoReadBody(int rv) {
210 DCHECK(CalledOnValidThread()); 311 DCHECK(CalledOnValidThread());
211 312
212 // If there's been an error, abort the current request. 313 // If there's been an error, abort the current request.
213 if (rv != OK) { 314 if (rv != OK) {
214 current_request_.reset(); 315 ResetRequest();
215 buffer_ = NULL;
216 next_state_ = STATE_SEND_REQUEST; 316 next_state_ = STATE_SEND_REQUEST;
217
218 return OK; 317 return OK;
219 } 318 }
220 319
221 next_state_ = STATE_READ_BODY_COMPLETE; 320 next_state_ = STATE_READ_BODY_COMPLETE;
222 int bytes_read = 0; 321 int bytes_read = 0;
223 current_request_->Read(buffer_.get(), kBufferSize, &bytes_read); 322 current_request_->Read(buffer_.get(), kBufferSize, &bytes_read);
224 if (current_request_->status().is_io_pending()) 323 if (current_request_->status().is_io_pending())
225 return ERR_IO_PENDING; 324 return ERR_IO_PENDING;
226 325
227 return GetReadResult(bytes_read, current_request_.get()); 326 return GetReadResult(bytes_read, current_request_.get());
(...skipping 20 matching lines...) Expand all
248 } 347 }
249 348
250 // End of file; complete the request. 349 // End of file; complete the request.
251 next_state_ = STATE_REQUEST_COMPLETE; 350 next_state_ = STATE_REQUEST_COMPLETE;
252 return OK; 351 return OK;
253 } 352 }
254 353
255 int SdchDictionaryFetcher::DoCompleteRequest(int rv) { 354 int SdchDictionaryFetcher::DoCompleteRequest(int rv) {
256 DCHECK(CalledOnValidThread()); 355 DCHECK(CalledOnValidThread());
257 356
258 // DoReadBodyComplete() only transitions to this state 357 // If the dictionary was successfully fetched, add it to the manager.
259 // on success. 358 if (rv == OK) {
260 DCHECK_EQ(OK, rv); 359 current_callback_.Run(dictionary_, current_request_->url(),
360 current_request_->net_log());
361 }
261 362
262 dictionary_fetched_callback_.Run(dictionary_, current_request_->url(), 363 ResetRequest();
263 current_request_->net_log());
264 current_request_.reset();
265 buffer_ = NULL;
266 dictionary_.clear();
267
268 next_state_ = STATE_SEND_REQUEST; 364 next_state_ = STATE_SEND_REQUEST;
269
270 return OK; 365 return OK;
271 } 366 }
272 367
273 } // namespace net 368 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698