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

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