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 use_stale_while_revalidate_(params.use_stale_while_revalidate), | 458 use_stale_while_revalidate_(params.use_stale_while_revalidate), |
458 mode_(NORMAL), | 459 mode_(NORMAL), |
459 network_layer_(new HttpNetworkLayer(new HttpNetworkSession(params))), | 460 network_layer_(new HttpNetworkLayer(new HttpNetworkSession(params))), |
| 461 clock_(new base::DefaultClock()), |
460 weak_factory_(this) { | 462 weak_factory_(this) { |
461 SetupQuicServerInfoFactory(network_layer_->GetSession()); | 463 SetupQuicServerInfoFactory(network_layer_->GetSession()); |
462 } | 464 } |
463 | 465 |
464 | 466 |
465 // This call doesn't change the shared |session|'s QuicServerInfoFactory because | 467 // This call doesn't change the shared |session|'s QuicServerInfoFactory because |
466 // |session| is shared. | 468 // |session| is shared. |
467 HttpCache::HttpCache(HttpNetworkSession* session, | 469 HttpCache::HttpCache(HttpNetworkSession* session, |
468 BackendFactory* backend_factory) | 470 BackendFactory* backend_factory) |
469 : net_log_(session->net_log()), | 471 : net_log_(session->net_log()), |
470 backend_factory_(backend_factory), | 472 backend_factory_(backend_factory), |
471 building_backend_(false), | 473 building_backend_(false), |
472 bypass_lock_for_test_(false), | 474 bypass_lock_for_test_(false), |
473 use_stale_while_revalidate_(session->params().use_stale_while_revalidate), | 475 use_stale_while_revalidate_(session->params().use_stale_while_revalidate), |
474 mode_(NORMAL), | 476 mode_(NORMAL), |
475 network_layer_(new HttpNetworkLayer(session)), | 477 network_layer_(new HttpNetworkLayer(session)), |
| 478 clock_(new base::DefaultClock()), |
476 weak_factory_(this) { | 479 weak_factory_(this) { |
477 } | 480 } |
478 | 481 |
479 HttpCache::HttpCache(HttpTransactionFactory* network_layer, | 482 HttpCache::HttpCache(HttpTransactionFactory* network_layer, |
480 NetLog* net_log, | 483 NetLog* net_log, |
481 BackendFactory* backend_factory) | 484 BackendFactory* backend_factory) |
482 : net_log_(net_log), | 485 : net_log_(net_log), |
483 backend_factory_(backend_factory), | 486 backend_factory_(backend_factory), |
484 building_backend_(false), | 487 building_backend_(false), |
485 bypass_lock_for_test_(false), | 488 bypass_lock_for_test_(false), |
486 use_stale_while_revalidate_(false), | 489 use_stale_while_revalidate_(false), |
487 mode_(NORMAL), | 490 mode_(NORMAL), |
488 network_layer_(network_layer), | 491 network_layer_(network_layer), |
| 492 clock_(new base::DefaultClock()), |
489 weak_factory_(this) { | 493 weak_factory_(this) { |
490 SetupQuicServerInfoFactory(network_layer_->GetSession()); | 494 SetupQuicServerInfoFactory(network_layer_->GetSession()); |
491 HttpNetworkSession* session = network_layer_->GetSession(); | 495 HttpNetworkSession* session = network_layer_->GetSession(); |
492 if (session) | 496 if (session) |
493 use_stale_while_revalidate_ = session->params().use_stale_while_revalidate; | 497 use_stale_while_revalidate_ = session->params().use_stale_while_revalidate; |
494 } | 498 } |
495 | 499 |
496 HttpCache::~HttpCache() { | 500 HttpCache::~HttpCache() { |
497 // Transactions should see an invalid cache after this point; otherwise they | 501 // Transactions should see an invalid cache after this point; otherwise they |
498 // could see an inconsistent object (half destroyed). | 502 // could see an inconsistent object (half destroyed). |
(...skipping 907 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1406 building_backend_ = false; | 1410 building_backend_ = false; |
1407 DeletePendingOp(pending_op); | 1411 DeletePendingOp(pending_op); |
1408 } | 1412 } |
1409 | 1413 |
1410 // The cache may be gone when we return from the callback. | 1414 // The cache may be gone when we return from the callback. |
1411 if (!item->DoCallback(result, disk_cache_.get())) | 1415 if (!item->DoCallback(result, disk_cache_.get())) |
1412 item->NotifyTransaction(result, NULL); | 1416 item->NotifyTransaction(result, NULL); |
1413 } | 1417 } |
1414 | 1418 |
1415 } // namespace net | 1419 } // namespace net |
OLD | NEW |