OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/http/http_cache.h" | 5 #include "net/http/http_cache.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 | 10 |
11 #if defined(OS_POSIX) | 11 #if defined(OS_POSIX) |
12 #include <unistd.h> | 12 #include <unistd.h> |
13 #endif | 13 #endif |
14 | 14 |
15 #include "base/bind.h" | 15 #include "base/bind.h" |
16 #include "base/bind_helpers.h" | 16 #include "base/bind_helpers.h" |
17 #include "base/callback.h" | 17 #include "base/callback.h" |
18 #include "base/file_util.h" | 18 #include "base/file_util.h" |
19 #include "base/format_macros.h" | 19 #include "base/format_macros.h" |
20 #include "base/location.h" | 20 #include "base/location.h" |
21 #include "base/memory/ref_counted.h" | 21 #include "base/memory/ref_counted.h" |
22 #include "base/message_loop/message_loop.h" | 22 #include "base/message_loop/message_loop.h" |
23 #include "base/metrics/field_trial.h" | 23 #include "base/metrics/field_trial.h" |
24 #include "base/metrics/histogram.h" | |
24 #include "base/pickle.h" | 25 #include "base/pickle.h" |
25 #include "base/stl_util.h" | 26 #include "base/stl_util.h" |
26 #include "base/strings/string_number_conversions.h" | 27 #include "base/strings/string_number_conversions.h" |
27 #include "base/strings/string_util.h" | 28 #include "base/strings/string_util.h" |
28 #include "base/strings/stringprintf.h" | 29 #include "base/strings/stringprintf.h" |
29 #include "base/threading/worker_pool.h" | 30 #include "base/threading/worker_pool.h" |
31 #include "base/time/time.h" | |
30 #include "net/base/cache_type.h" | 32 #include "net/base/cache_type.h" |
31 #include "net/base/io_buffer.h" | 33 #include "net/base/io_buffer.h" |
32 #include "net/base/load_flags.h" | 34 #include "net/base/load_flags.h" |
33 #include "net/base/net_errors.h" | 35 #include "net/base/net_errors.h" |
36 #include "net/base/network_delegate.h" | |
34 #include "net/base/upload_data_stream.h" | 37 #include "net/base/upload_data_stream.h" |
35 #include "net/disk_cache/disk_cache.h" | 38 #include "net/disk_cache/disk_cache.h" |
36 #include "net/http/disk_based_cert_cache.h" | 39 #include "net/http/disk_based_cert_cache.h" |
37 #include "net/http/disk_cache_based_quic_server_info.h" | 40 #include "net/http/disk_cache_based_quic_server_info.h" |
38 #include "net/http/http_cache_transaction.h" | 41 #include "net/http/http_cache_transaction.h" |
39 #include "net/http/http_network_layer.h" | 42 #include "net/http/http_network_layer.h" |
40 #include "net/http/http_network_session.h" | 43 #include "net/http/http_network_session.h" |
41 #include "net/http/http_request_info.h" | 44 #include "net/http/http_request_info.h" |
42 #include "net/http/http_response_headers.h" | 45 #include "net/http/http_response_headers.h" |
43 #include "net/http/http_response_info.h" | 46 #include "net/http/http_response_info.h" |
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
285 virtual QuicServerInfo* GetForServer( | 288 virtual QuicServerInfo* GetForServer( |
286 const QuicServerId& server_id) OVERRIDE { | 289 const QuicServerId& server_id) OVERRIDE { |
287 return new DiskCacheBasedQuicServerInfo(server_id, http_cache_); | 290 return new DiskCacheBasedQuicServerInfo(server_id, http_cache_); |
288 } | 291 } |
289 | 292 |
290 private: | 293 private: |
291 HttpCache* const http_cache_; | 294 HttpCache* const http_cache_; |
292 }; | 295 }; |
293 | 296 |
294 //----------------------------------------------------------------------------- | 297 //----------------------------------------------------------------------------- |
298 | |
299 class HttpCache::AsyncValidation { | |
300 public: | |
301 AsyncValidation(const HttpRequestInfo& original_request, HttpCache* cache) | |
302 : request_(original_request), cache_(cache) {} | |
303 ~AsyncValidation() {} | |
304 | |
305 void Start(const BoundNetLog& net_log, | |
306 scoped_ptr<Transaction> transaction, | |
307 NetworkDelegate* network_delegate); | |
308 | |
309 private: | |
310 void OnStarted(int result); | |
311 void DoRead(); | |
312 void OnRead(int result); | |
313 | |
314 // Terminate this request with net error code |result|. Logs the transaction | |
315 // result and asks HttpCache to delete this object. | |
316 // If there was a client or server certificate error, it cannot be recovered | |
317 // asynchronously, so we need to prevent future attempts to asynchronously | |
318 // fetch the resource. In this case, the cache entry is doomed. | |
319 void Terminate(int result); | |
320 | |
321 HttpRequestInfo request_; | |
322 scoped_refptr<IOBuffer> buf_; | |
323 CompletionCallback read_callback_; | |
324 scoped_ptr<Transaction> transaction_; | |
325 base::Time start_time_; | |
326 | |
327 // The HttpCache object owns this object. This object is always deleted before | |
328 // the pointer to the cache becomes invalid. | |
329 HttpCache* cache_; | |
330 | |
331 DISALLOW_COPY_AND_ASSIGN(AsyncValidation); | |
332 }; | |
333 | |
334 void HttpCache::AsyncValidation::Start(const BoundNetLog& net_log, | |
335 scoped_ptr<Transaction> transaction, | |
336 NetworkDelegate* network_delegate) { | |
337 transaction_ = transaction.Pass(); | |
338 if (network_delegate) { | |
339 // This code is necessary to enable async transactions to pass over the | |
340 // data-reduction proxy. This is a violation of the "once-and-only-once" | |
341 // principle, since it copies code from URLRequestHttpJob. We cannot use the | |
342 // original callback passed to HttpCache::Transaction by URLRequestHttpJob | |
343 // as it will only be valid as long as the URLRequestHttpJob object is | |
344 // alive, and that object will be deleted as soon as the synchronous request | |
345 // completes. | |
346 // | |
347 // This code is also an encapsulation violation. We are exploiting the fact | |
348 // that the |request| parameter to NotifyBeforeSendProxyHeaders() is never | |
349 // actually used for anything, and so can be NULL. | |
350 // | |
351 // TODO(ricea): Do this better. | |
352 transaction_->SetBeforeProxyHeadersSentCallback( | |
353 base::Bind(&NetworkDelegate::NotifyBeforeSendProxyHeaders, | |
354 base::Unretained(network_delegate), | |
355 static_cast<URLRequest*>(NULL))); | |
356 // The above use of base::Unretained is safe because the NetworkDelegate has | |
357 // to live at least as long as the HttpNetworkSession which has to live as | |
358 // least as long as the HttpNetworkLayer which has to live at least as long | |
359 // this HttpCache object. | |
360 } | |
361 | |
362 DCHECK_EQ(0, request_.load_flags & LOAD_ASYNC_REVALIDATION); | |
363 request_.load_flags |= LOAD_ASYNC_REVALIDATION; | |
364 start_time_ = base::Time::Now(); | |
365 // This use of base::Unretained is safe because |transaction_| is owned by | |
366 // this object. | |
367 read_callback_ = base::Bind(&AsyncValidation::OnRead, base::Unretained(this)); | |
368 // This use of base::Unretained is safe as above. | |
369 int rv = transaction_->Start( | |
370 &request_, | |
371 base::Bind(&AsyncValidation::OnStarted, base::Unretained(this)), | |
372 net_log); | |
373 | |
374 if (rv == ERR_IO_PENDING) | |
375 return; | |
376 | |
377 OnStarted(rv); | |
378 } | |
379 | |
380 void HttpCache::AsyncValidation::OnStarted(int result) { | |
381 if (result != OK) { | |
382 DVLOG(1) << "Asynchronous transaction start failed for " << request_.url; | |
383 Terminate(result); | |
384 return; | |
385 } | |
386 | |
387 DoRead(); | |
388 } | |
389 | |
390 void HttpCache::AsyncValidation::DoRead() { | |
391 const size_t kBufSize = 4096; | |
392 if (!buf_) | |
393 buf_ = new IOBuffer(kBufSize); | |
394 | |
395 int rv = 0; | |
396 do { | |
397 rv = transaction_->Read(buf_.get(), kBufSize, read_callback_); | |
398 } while (rv > 0); | |
399 | |
400 if (rv == ERR_IO_PENDING) | |
401 return; | |
402 | |
403 OnRead(rv); | |
404 } | |
405 | |
406 void HttpCache::AsyncValidation::OnRead(int result) { | |
407 if (result > 0) { | |
408 DoRead(); | |
409 return; | |
410 } | |
411 Terminate(result); | |
412 } | |
413 | |
414 void HttpCache::AsyncValidation::Terminate(int result) { | |
415 if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED || IsCertificateError(result)) { | |
416 // We should not attempt to access this resource asynchronously again until | |
417 // the certificate problem has been resolved. | |
418 // TODO(ricea): Mark the entry as requiring synchronous revalidation rather | |
419 // than just deleting it. | |
420 cache_->DoomEntry(transaction_->key(), transaction_.get()); | |
421 } | |
422 base::TimeDelta duration = base::Time::Now() - start_time_; | |
423 UMA_HISTOGRAM_TIMES("HttpCache.AsyncValidationDuration", duration); | |
424 transaction_->net_log().EndEventWithNetErrorCode( | |
425 NetLog::TYPE_ASYNC_REVALIDATION, result); | |
426 // This relies on the generated cache key not depending on the value of | |
427 // load_flags. This should be true for the lifetime of this implementation. | |
rvargas (doing something else)
2014/09/12 18:33:12
breaking that invariant would mean breaking the de
Adam Rice
2014/09/16 11:53:01
I removed the comment.
| |
428 cache_->DeleteAsyncValidation(cache_->GenerateCacheKey(&request_)); | |
429 // |this| is deleted. | |
430 } | |
431 | |
432 //----------------------------------------------------------------------------- | |
295 HttpCache::HttpCache(const net::HttpNetworkSession::Params& params, | 433 HttpCache::HttpCache(const net::HttpNetworkSession::Params& params, |
296 BackendFactory* backend_factory) | 434 BackendFactory* backend_factory) |
297 : net_log_(params.net_log), | 435 : net_log_(params.net_log), |
298 backend_factory_(backend_factory), | 436 backend_factory_(backend_factory), |
299 building_backend_(false), | 437 building_backend_(false), |
300 bypass_lock_for_test_(false), | 438 bypass_lock_for_test_(false), |
439 use_stale_while_revalidate_(params.use_stale_while_revalidate), | |
301 mode_(NORMAL), | 440 mode_(NORMAL), |
302 network_layer_(new HttpNetworkLayer(new HttpNetworkSession(params))), | 441 network_layer_(new HttpNetworkLayer(new HttpNetworkSession(params))), |
303 weak_factory_(this) { | 442 weak_factory_(this) { |
304 SetupQuicServerInfoFactory(network_layer_->GetSession()); | 443 SetupQuicServerInfoFactory(network_layer_->GetSession()); |
305 } | 444 } |
306 | 445 |
307 | 446 |
308 // This call doesn't change the shared |session|'s QuicServerInfoFactory because | 447 // This call doesn't change the shared |session|'s QuicServerInfoFactory because |
309 // |session| is shared. | 448 // |session| is shared. |
310 HttpCache::HttpCache(HttpNetworkSession* session, | 449 HttpCache::HttpCache(HttpNetworkSession* session, |
311 BackendFactory* backend_factory) | 450 BackendFactory* backend_factory) |
312 : net_log_(session->net_log()), | 451 : net_log_(session->net_log()), |
313 backend_factory_(backend_factory), | 452 backend_factory_(backend_factory), |
314 building_backend_(false), | 453 building_backend_(false), |
315 bypass_lock_for_test_(false), | 454 bypass_lock_for_test_(false), |
455 use_stale_while_revalidate_(session->params().use_stale_while_revalidate), | |
316 mode_(NORMAL), | 456 mode_(NORMAL), |
317 network_layer_(new HttpNetworkLayer(session)), | 457 network_layer_(new HttpNetworkLayer(session)), |
318 weak_factory_(this) { | 458 weak_factory_(this) { |
319 } | 459 } |
320 | 460 |
321 HttpCache::HttpCache(HttpTransactionFactory* network_layer, | 461 HttpCache::HttpCache(HttpTransactionFactory* network_layer, |
322 NetLog* net_log, | 462 NetLog* net_log, |
323 BackendFactory* backend_factory) | 463 BackendFactory* backend_factory) |
324 : net_log_(net_log), | 464 : net_log_(net_log), |
325 backend_factory_(backend_factory), | 465 backend_factory_(backend_factory), |
326 building_backend_(false), | 466 building_backend_(false), |
327 bypass_lock_for_test_(false), | 467 bypass_lock_for_test_(false), |
468 use_stale_while_revalidate_(false), | |
328 mode_(NORMAL), | 469 mode_(NORMAL), |
329 network_layer_(network_layer), | 470 network_layer_(network_layer), |
330 weak_factory_(this) { | 471 weak_factory_(this) { |
331 SetupQuicServerInfoFactory(network_layer_->GetSession()); | 472 SetupQuicServerInfoFactory(network_layer_->GetSession()); |
473 HttpNetworkSession* session = network_layer_->GetSession(); | |
474 if (session) | |
475 use_stale_while_revalidate_ = session->params().use_stale_while_revalidate; | |
332 } | 476 } |
333 | 477 |
334 HttpCache::~HttpCache() { | 478 HttpCache::~HttpCache() { |
335 // Transactions should see an invalid cache after this point; otherwise they | 479 // Transactions should see an invalid cache after this point; otherwise they |
336 // could see an inconsistent object (half destroyed). | 480 // could see an inconsistent object (half destroyed). |
337 weak_factory_.InvalidateWeakPtrs(); | 481 weak_factory_.InvalidateWeakPtrs(); |
338 | 482 |
339 // If we have any active entries remaining, then we need to deactivate them. | 483 // If we have any active entries remaining, then we need to deactivate them. |
340 // We may have some pending calls to OnProcessPendingQueue, but since those | 484 // We may have some pending calls to OnProcessPendingQueue, but since those |
341 // won't run (due to our destruction), we can simply ignore the corresponding | 485 // won't run (due to our destruction), we can simply ignore the corresponding |
342 // will_process_pending_queue flag. | 486 // will_process_pending_queue flag. |
343 while (!active_entries_.empty()) { | 487 while (!active_entries_.empty()) { |
344 ActiveEntry* entry = active_entries_.begin()->second; | 488 ActiveEntry* entry = active_entries_.begin()->second; |
345 entry->will_process_pending_queue = false; | 489 entry->will_process_pending_queue = false; |
346 entry->pending_queue.clear(); | 490 entry->pending_queue.clear(); |
347 entry->readers.clear(); | 491 entry->readers.clear(); |
348 entry->writer = NULL; | 492 entry->writer = NULL; |
349 DeactivateEntry(entry); | 493 DeactivateEntry(entry); |
350 } | 494 } |
351 | 495 |
352 STLDeleteElements(&doomed_entries_); | 496 STLDeleteElements(&doomed_entries_); |
497 STLDeleteValues(&async_validations_); | |
353 | 498 |
354 // Before deleting pending_ops_, we have to make sure that the disk cache is | 499 // Before deleting pending_ops_, we have to make sure that the disk cache is |
355 // done with said operations, or it will attempt to use deleted data. | 500 // done with said operations, or it will attempt to use deleted data. |
356 cert_cache_.reset(); | 501 cert_cache_.reset(); |
357 disk_cache_.reset(); | 502 disk_cache_.reset(); |
358 | 503 |
359 PendingOpsMap::iterator pending_it = pending_ops_.begin(); | 504 PendingOpsMap::iterator pending_it = pending_ops_.begin(); |
360 for (; pending_it != pending_ops_.end(); ++pending_it) { | 505 for (; pending_it != pending_ops_.end(); ++pending_it) { |
361 // We are not notifying the transactions about the cache going away, even | 506 // We are not notifying the transactions about the cache going away, even |
362 // though they are waiting for a callback that will never fire. | 507 // though they are waiting for a callback that will never fire. |
(...skipping 668 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1031 // not delete the entry before OnProcessPendingQueue runs. | 1176 // not delete the entry before OnProcessPendingQueue runs. |
1032 if (entry->will_process_pending_queue) | 1177 if (entry->will_process_pending_queue) |
1033 return; | 1178 return; |
1034 entry->will_process_pending_queue = true; | 1179 entry->will_process_pending_queue = true; |
1035 | 1180 |
1036 base::MessageLoop::current()->PostTask( | 1181 base::MessageLoop::current()->PostTask( |
1037 FROM_HERE, | 1182 FROM_HERE, |
1038 base::Bind(&HttpCache::OnProcessPendingQueue, GetWeakPtr(), entry)); | 1183 base::Bind(&HttpCache::OnProcessPendingQueue, GetWeakPtr(), entry)); |
1039 } | 1184 } |
1040 | 1185 |
1186 void HttpCache::PerformAsyncValidation(const HttpRequestInfo& original_request, | |
1187 const BoundNetLog& net_log) { | |
1188 DCHECK(use_stale_while_revalidate_); | |
1189 std::string key = GenerateCacheKey(&original_request); | |
1190 AsyncValidation* async_validation = | |
1191 new AsyncValidation(original_request, this); | |
1192 typedef AsyncValidationMap::value_type AsyncValidationKeyValue; | |
1193 bool insert_ok = | |
1194 async_validations_.insert(AsyncValidationKeyValue(key, async_validation)) | |
1195 .second; | |
1196 if (!insert_ok) { | |
1197 DVLOG(1) << "Harmless race condition detected on URL " | |
1198 << original_request.url << "; discarding redundant revalidation."; | |
1199 delete async_validation; | |
1200 return; | |
1201 } | |
1202 HttpNetworkSession* network_session = GetSession(); | |
1203 NetworkDelegate* network_delegate = NULL; | |
1204 if (network_session) | |
1205 network_delegate = network_session->network_delegate(); | |
1206 scoped_ptr<HttpTransaction> transaction; | |
1207 CreateTransaction(IDLE, &transaction); | |
1208 scoped_ptr<Transaction> downcast_transaction( | |
1209 static_cast<Transaction*>(transaction.release())); | |
1210 async_validation->Start( | |
1211 net_log, downcast_transaction.Pass(), network_delegate); | |
rvargas (doing something else)
2014/09/12 18:33:12
use PassAs()
Adam Rice
2014/09/16 11:53:01
PassAs() doesn't work for downcasts. I can't find
| |
1212 // |async_validation| may have been deleted here. | |
1213 } | |
1214 | |
1215 void HttpCache::DeleteAsyncValidation(const std::string& url) { | |
1216 AsyncValidationMap::iterator it = async_validations_.find(url); | |
1217 CHECK(it != async_validations_.end()); // security-critical invariant | |
1218 AsyncValidation* async_validation = it->second; | |
1219 async_validations_.erase(it); | |
1220 delete async_validation; | |
1221 } | |
1222 | |
1041 void HttpCache::OnProcessPendingQueue(ActiveEntry* entry) { | 1223 void HttpCache::OnProcessPendingQueue(ActiveEntry* entry) { |
1042 entry->will_process_pending_queue = false; | 1224 entry->will_process_pending_queue = false; |
1043 DCHECK(!entry->writer); | 1225 DCHECK(!entry->writer); |
1044 | 1226 |
1045 // If no one is interested in this entry, then we can deactivate it. | 1227 // If no one is interested in this entry, then we can deactivate it. |
1046 if (entry->pending_queue.empty()) { | 1228 if (entry->pending_queue.empty()) { |
1047 if (entry->readers.empty()) | 1229 if (entry->readers.empty()) |
1048 DestroyEntry(entry); | 1230 DestroyEntry(entry); |
1049 return; | 1231 return; |
1050 } | 1232 } |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1201 building_backend_ = false; | 1383 building_backend_ = false; |
1202 DeletePendingOp(pending_op); | 1384 DeletePendingOp(pending_op); |
1203 } | 1385 } |
1204 | 1386 |
1205 // The cache may be gone when we return from the callback. | 1387 // The cache may be gone when we return from the callback. |
1206 if (!item->DoCallback(result, disk_cache_.get())) | 1388 if (!item->DoCallback(result, disk_cache_.get())) |
1207 item->NotifyTransaction(result, NULL); | 1389 item->NotifyTransaction(result, NULL); |
1208 } | 1390 } |
1209 | 1391 |
1210 } // namespace net | 1392 } // namespace net |
OLD | NEW |