Chromium Code Reviews| Index: components/precache/content/precache_manager.cc |
| diff --git a/components/precache/content/precache_manager.cc b/components/precache/content/precache_manager.cc |
| index f4333c12d64e9b02779ca6a8de304a99f2e8bcf5..87879b9017b894e78c8689cf4d607df0c6a90c92 100644 |
| --- a/components/precache/content/precache_manager.cc |
| +++ b/components/precache/content/precache_manager.cc |
| @@ -26,6 +26,9 @@ |
| #include "content/public/browser/browser_thread.h" |
| #include "content/public/browser/storage_partition.h" |
| #include "net/base/network_change_notifier.h" |
| +#include "net/http/http_cache.h" |
| +#include "net/url_request/url_request_context.h" |
| +#include "net/url_request/url_request_context_getter.h" |
| using content::BrowserThread; |
| @@ -111,6 +114,39 @@ PrecacheManager::AllowedType PrecacheManager::PrecachingAllowed() const { |
| return AllowedType::DISALLOWED; |
| } |
| +void PrecacheManager::OnCacheBackendReceived(int net_error_code) { |
| + if (net_error_code == net::OK) { |
|
twifkak
2016/11/18 00:18:00
Add a DCHECK_CURRENTLY_ON here? Also, are you sure
jamartin
2016/11/18 03:55:31
Discussed offline. It seems that most of this need
|
| + DCHECK(cache_backend_); |
| + int result = cache_backend_->CalculateSizeOfAllEntries(base::Bind( |
| + &PrecacheManager::OnCacheSizeReceived, base::Unretained(this))); |
| + if (result == net::ERR_IO_PENDING) { |
| + // Wait for the callback. |
| + } else if (result >= 0) { |
| + // The result is the expected bytes already. |
| + OnCacheSizeReceived(result); |
| + } else { |
| + // Error occurred. Couldn't get the size. Assume there is no cache. |
| + OnCacheSizeReceived(0); |
| + } |
| + } else { // net_error_code != net::OK |
| + // Assume there is no cache. |
| + OnCacheSizeReceived(0); |
| + } |
| +} |
| + |
| +void PrecacheManager::OnCacheSizeReceived(int cache_size_bytes) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + if (cache_size_bytes < min_cache_size_bytes_) { |
| + OnDone(); |
|
twifkak
2016/11/17 21:52:04
Add an UMA for the cache_size_bytes when this happ
jamartin
2016/11/18 03:55:32
Done.
|
| + } else { |
| + BrowserThread::PostTaskAndReplyWithResult( |
| + BrowserThread::DB, FROM_HERE, |
| + base::Bind(&PrecacheDatabase::GetUnfinishedWork, |
| + base::Unretained(precache_database_.get())), |
| + base::Bind(&PrecacheManager::OnGetUnfinishedWorkDone, AsWeakPtr())); |
| + } |
| +} |
| + |
| void PrecacheManager::StartPrecaching( |
| const PrecacheCompletionCallback& precache_completion_callback) { |
| DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| @@ -128,12 +164,32 @@ void PrecacheManager::StartPrecaching( |
| base::Bind(&PrecacheDatabase::SetLastPrecacheTimestamp, |
| base::Unretained(precache_database_.get()), |
| base::Time::Now())); |
| - BrowserThread::PostTaskAndReplyWithResult( |
| - BrowserThread::DB, |
| - FROM_HERE, |
| - base::Bind(&PrecacheDatabase::GetUnfinishedWork, |
| - base::Unretained(precache_database_.get())), |
| - base::Bind(&PrecacheManager::OnGetUnfinishedWorkDone, AsWeakPtr())); |
| + |
| + // Continue with OnGetUnfinishedWorkDone only if the size of the cache is |
|
twifkak
2016/11/17 21:52:04
Perhaps this check should be inside OnGetUnfinishe
twifkak
2016/11/18 01:26:41
Actually, my next CL will want to pass the cache b
jamartin
2016/11/18 03:55:32
Acknowledged.
|
| + // at least min_cache_size_bytes_. |
| + // Class disk_cache::Backend does not expose its maximum size. However, caches |
| + // are usually full, so we can use the size of all the entries stored in the |
| + // cache (via CalculateSizeOfAllEntries) as a proxy of its maximum size. |
| + net::HttpCache* cache = |
| + content::BrowserContext::GetDefaultStoragePartition(browser_context_) |
| + ->GetURLRequestContext() |
|
twifkak
2016/11/17 21:52:04
May return null? https://cs.chromium.org/chromium/
jamartin
2016/11/18 03:55:32
Done.
|
| + ->GetURLRequestContext() |
|
twifkak
2016/11/17 21:52:04
May return null: https://cs.chromium.org/chromium/
jamartin
2016/11/18 03:55:32
Done.
|
| + ->http_transaction_factory() |
|
twifkak
2016/11/17 21:52:04
May return null? https://cs.chromium.org/chromium/
jamartin
2016/11/18 03:55:32
Done.
|
| + ->GetCache(); |
| + if (cache) { |
| + const int net_error_code = cache->GetBackend( |
| + &cache_backend_, base::Bind(&PrecacheManager::OnCacheBackendReceived, |
| + base::Unretained(this))); |
|
twifkak
2016/11/17 23:15:19
I think you should use AsWeakPtr() instead?
jamartin
2016/11/18 03:55:31
Done.
|
| + if (net_error_code != net::ERR_IO_PENDING) { |
| + // No need to wait for the callback. The callback hasn't been called with |
| + // the appropriate code, so we call it directly. |
| + OnCacheBackendReceived(net_error_code); |
| + } |
| + } else { // !cache |
| + // There is no known cache. Assume that there is no cache. |
| + // TODO(jamartin): Verify that it is not only that it lacks initialization. |
| + OnCacheSizeReceived(0); |
| + } |
| } |
| void PrecacheManager::OnGetUnfinishedWorkDone( |