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 |
(...skipping 11 matching lines...) Expand all Loading... |
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/metrics/histogram.h" |
25 #include "base/pickle.h" | 25 #include "base/pickle.h" |
26 #include "base/profiler/scoped_tracker.h" | 26 #include "base/profiler/scoped_tracker.h" |
27 #include "base/stl_util.h" | 27 #include "base/stl_util.h" |
28 #include "base/strings/string_number_conversions.h" | 28 #include "base/strings/string_number_conversions.h" |
29 #include "base/strings/string_util.h" | 29 #include "base/strings/string_util.h" |
30 #include "base/strings/stringprintf.h" | 30 #include "base/strings/stringprintf.h" |
31 #include "base/threading/worker_pool.h" | 31 #include "base/threading/worker_pool.h" |
| 32 #include "base/time/default_clock.h" |
32 #include "base/time/time.h" | 33 #include "base/time/time.h" |
33 #include "net/base/cache_type.h" | 34 #include "net/base/cache_type.h" |
34 #include "net/base/io_buffer.h" | 35 #include "net/base/io_buffer.h" |
35 #include "net/base/load_flags.h" | 36 #include "net/base/load_flags.h" |
36 #include "net/base/net_errors.h" | 37 #include "net/base/net_errors.h" |
37 #include "net/base/network_delegate.h" | 38 #include "net/base/network_delegate.h" |
38 #include "net/base/upload_data_stream.h" | 39 #include "net/base/upload_data_stream.h" |
39 #include "net/disk_cache/disk_cache.h" | 40 #include "net/disk_cache/disk_cache.h" |
40 #include "net/http/disk_based_cert_cache.h" | 41 #include "net/http/disk_based_cert_cache.h" |
41 #include "net/http/disk_cache_based_quic_server_info.h" | 42 #include "net/http/disk_cache_based_quic_server_info.h" |
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 base::Unretained(network_delegate), | 355 base::Unretained(network_delegate), |
355 static_cast<URLRequest*>(NULL))); | 356 static_cast<URLRequest*>(NULL))); |
356 // The above use of base::Unretained is safe because the NetworkDelegate has | 357 // 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 // 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 // least as long as the HttpNetworkLayer which has to live at least as long |
359 // this HttpCache object. | 360 // this HttpCache object. |
360 } | 361 } |
361 | 362 |
362 DCHECK_EQ(0, request_.load_flags & LOAD_ASYNC_REVALIDATION); | 363 DCHECK_EQ(0, request_.load_flags & LOAD_ASYNC_REVALIDATION); |
363 request_.load_flags |= LOAD_ASYNC_REVALIDATION; | 364 request_.load_flags |= LOAD_ASYNC_REVALIDATION; |
364 start_time_ = base::Time::Now(); | 365 start_time_ = cache_->clock()->Now(); |
365 // This use of base::Unretained is safe because |transaction_| is owned by | 366 // This use of base::Unretained is safe because |transaction_| is owned by |
366 // this object. | 367 // this object. |
367 read_callback_ = base::Bind(&AsyncValidation::OnRead, base::Unretained(this)); | 368 read_callback_ = base::Bind(&AsyncValidation::OnRead, base::Unretained(this)); |
368 // This use of base::Unretained is safe as above. | 369 // This use of base::Unretained is safe as above. |
369 int rv = transaction_->Start( | 370 int rv = transaction_->Start( |
370 &request_, | 371 &request_, |
371 base::Bind(&AsyncValidation::OnStarted, base::Unretained(this)), | 372 base::Bind(&AsyncValidation::OnStarted, base::Unretained(this)), |
372 net_log); | 373 net_log); |
373 | 374 |
374 if (rv == ERR_IO_PENDING) | 375 if (rv == ERR_IO_PENDING) |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
432 void HttpCache::AsyncValidation::Terminate(int result) { | 433 void HttpCache::AsyncValidation::Terminate(int result) { |
433 if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED || IsCertificateError(result)) { | 434 if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED || IsCertificateError(result)) { |
434 // We should not attempt to access this resource asynchronously again until | 435 // We should not attempt to access this resource asynchronously again until |
435 // the certificate problem has been resolved. | 436 // the certificate problem has been resolved. |
436 // TODO(ricea): For ERR_SSL_CLIENT_AUTH_CERT_NEEDED, mark the entry as | 437 // TODO(ricea): For ERR_SSL_CLIENT_AUTH_CERT_NEEDED, mark the entry as |
437 // requiring synchronous revalidation rather than just deleting it. Other | 438 // requiring synchronous revalidation rather than just deleting it. Other |
438 // certificate errors cause the resource to be considered uncacheable | 439 // certificate errors cause the resource to be considered uncacheable |
439 // anyway. | 440 // anyway. |
440 cache_->DoomEntry(transaction_->key(), transaction_.get()); | 441 cache_->DoomEntry(transaction_->key(), transaction_.get()); |
441 } | 442 } |
442 base::TimeDelta duration = base::Time::Now() - start_time_; | 443 base::TimeDelta duration = cache_->clock()->Now() - start_time_; |
443 UMA_HISTOGRAM_TIMES("HttpCache.AsyncValidationDuration", duration); | 444 UMA_HISTOGRAM_TIMES("HttpCache.AsyncValidationDuration", duration); |
444 transaction_->net_log().EndEventWithNetErrorCode( | 445 transaction_->net_log().EndEventWithNetErrorCode( |
445 NetLog::TYPE_ASYNC_REVALIDATION, result); | 446 NetLog::TYPE_ASYNC_REVALIDATION, result); |
446 cache_->DeleteAsyncValidation(cache_->GenerateCacheKey(&request_)); | 447 cache_->DeleteAsyncValidation(cache_->GenerateCacheKey(&request_)); |
447 // |this| is deleted. | 448 // |this| is deleted. |
448 } | 449 } |
449 | 450 |
450 //----------------------------------------------------------------------------- | 451 //----------------------------------------------------------------------------- |
451 HttpCache::HttpCache(const net::HttpNetworkSession::Params& params, | 452 HttpCache::HttpCache(const net::HttpNetworkSession::Params& params, |
452 BackendFactory* backend_factory) | 453 BackendFactory* backend_factory) |
453 : net_log_(params.net_log), | 454 : net_log_(params.net_log), |
454 backend_factory_(backend_factory), | 455 backend_factory_(backend_factory), |
455 building_backend_(false), | 456 building_backend_(false), |
456 bypass_lock_for_test_(false), | 457 bypass_lock_for_test_(false), |
457 fail_conditionalization_for_test_(false), | 458 fail_conditionalization_for_test_(false), |
458 use_stale_while_revalidate_(params.use_stale_while_revalidate), | 459 use_stale_while_revalidate_(params.use_stale_while_revalidate), |
459 mode_(NORMAL), | 460 mode_(NORMAL), |
460 network_layer_(new HttpNetworkLayer(new HttpNetworkSession(params))), | 461 network_layer_(new HttpNetworkLayer(new HttpNetworkSession(params))), |
| 462 clock_(new base::DefaultClock()), |
461 weak_factory_(this) { | 463 weak_factory_(this) { |
462 SetupQuicServerInfoFactory(network_layer_->GetSession()); | 464 SetupQuicServerInfoFactory(network_layer_->GetSession()); |
463 } | 465 } |
464 | 466 |
465 | 467 |
466 // This call doesn't change the shared |session|'s QuicServerInfoFactory because | 468 // This call doesn't change the shared |session|'s QuicServerInfoFactory because |
467 // |session| is shared. | 469 // |session| is shared. |
468 HttpCache::HttpCache(HttpNetworkSession* session, | 470 HttpCache::HttpCache(HttpNetworkSession* session, |
469 BackendFactory* backend_factory) | 471 BackendFactory* backend_factory) |
470 : net_log_(session->net_log()), | 472 : net_log_(session->net_log()), |
471 backend_factory_(backend_factory), | 473 backend_factory_(backend_factory), |
472 building_backend_(false), | 474 building_backend_(false), |
473 bypass_lock_for_test_(false), | 475 bypass_lock_for_test_(false), |
474 fail_conditionalization_for_test_(false), | 476 fail_conditionalization_for_test_(false), |
475 use_stale_while_revalidate_(session->params().use_stale_while_revalidate), | 477 use_stale_while_revalidate_(session->params().use_stale_while_revalidate), |
476 mode_(NORMAL), | 478 mode_(NORMAL), |
477 network_layer_(new HttpNetworkLayer(session)), | 479 network_layer_(new HttpNetworkLayer(session)), |
| 480 clock_(new base::DefaultClock()), |
478 weak_factory_(this) { | 481 weak_factory_(this) { |
479 } | 482 } |
480 | 483 |
481 HttpCache::HttpCache(HttpTransactionFactory* network_layer, | 484 HttpCache::HttpCache(HttpTransactionFactory* network_layer, |
482 NetLog* net_log, | 485 NetLog* net_log, |
483 BackendFactory* backend_factory) | 486 BackendFactory* backend_factory) |
484 : net_log_(net_log), | 487 : net_log_(net_log), |
485 backend_factory_(backend_factory), | 488 backend_factory_(backend_factory), |
486 building_backend_(false), | 489 building_backend_(false), |
487 bypass_lock_for_test_(false), | 490 bypass_lock_for_test_(false), |
488 fail_conditionalization_for_test_(false), | 491 fail_conditionalization_for_test_(false), |
489 use_stale_while_revalidate_(false), | 492 use_stale_while_revalidate_(false), |
490 mode_(NORMAL), | 493 mode_(NORMAL), |
491 network_layer_(network_layer), | 494 network_layer_(network_layer), |
| 495 clock_(new base::DefaultClock()), |
492 weak_factory_(this) { | 496 weak_factory_(this) { |
493 SetupQuicServerInfoFactory(network_layer_->GetSession()); | 497 SetupQuicServerInfoFactory(network_layer_->GetSession()); |
494 HttpNetworkSession* session = network_layer_->GetSession(); | 498 HttpNetworkSession* session = network_layer_->GetSession(); |
495 if (session) | 499 if (session) |
496 use_stale_while_revalidate_ = session->params().use_stale_while_revalidate; | 500 use_stale_while_revalidate_ = session->params().use_stale_while_revalidate; |
497 } | 501 } |
498 | 502 |
499 HttpCache::~HttpCache() { | 503 HttpCache::~HttpCache() { |
500 // Transactions should see an invalid cache after this point; otherwise they | 504 // Transactions should see an invalid cache after this point; otherwise they |
501 // could see an inconsistent object (half destroyed). | 505 // could see an inconsistent object (half destroyed). |
(...skipping 909 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1411 building_backend_ = false; | 1415 building_backend_ = false; |
1412 DeletePendingOp(pending_op); | 1416 DeletePendingOp(pending_op); |
1413 } | 1417 } |
1414 | 1418 |
1415 // The cache may be gone when we return from the callback. | 1419 // The cache may be gone when we return from the callback. |
1416 if (!item->DoCallback(result, disk_cache_.get())) | 1420 if (!item->DoCallback(result, disk_cache_.get())) |
1417 item->NotifyTransaction(result, NULL); | 1421 item->NotifyTransaction(result, NULL); |
1418 } | 1422 } |
1419 | 1423 |
1420 } // namespace net | 1424 } // namespace net |
OLD | NEW |