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

Unified Diff: components/precache/content/precache_manager.cc

Issue 2507753003: Do not precache when the cache size is small (Closed)
Patch Set: Comment on the IO thread Created 4 years, 1 month 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 side-by-side diff with in-line comments
Download patch
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..048412f8544f7895de8b3064d524e35d6149fe12 100644
--- a/components/precache/content/precache_manager.cc
+++ b/components/precache/content/precache_manager.cc
@@ -12,6 +12,7 @@
#include "base/command_line.h"
#include "base/logging.h"
#include "base/metrics/field_trial.h"
+#include "base/metrics/histogram_macros.h"
#include "base/strings/string_util.h"
#include "base/time/time.h"
#include "components/history/core/browser/history_service.h"
@@ -26,6 +27,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 +115,90 @@ PrecacheManager::AllowedType PrecacheManager::PrecachingAllowed() const {
return AllowedType::DISALLOWED;
}
+void PrecacheManager::OnCacheBackendReceived(int net_error_code) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+ if (net_error_code == net::OK) {
+ DCHECK(cache_backend_);
+ int result = cache_backend_->CalculateSizeOfAllEntries(
+ base::Bind(&PrecacheManager::OnCacheSizeReceived, AsWeakPtr()));
+ 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);
+ }
+ cache_backend_ = nullptr;
+}
+
+void PrecacheManager::OnCacheSizeReceived(int cache_size_bytes) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+
+ BrowserThread::PostTask(
+ BrowserThread::UI, FROM_HERE,
+ base::Bind(&PrecacheManager::OnCacheSizeReceivedInUIThread, AsWeakPtr(),
+ cache_size_bytes));
+}
+
+void PrecacheManager::OnCacheSizeReceivedInUIThread(int cache_size_bytes) {
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+
+ UMA_HISTOGRAM_MEMORY_KB("Precache.CacheSize.AllEntries",
+ cache_size_bytes / 1024);
+ if (cache_size_bytes < min_cache_size_bytes_) {
+ OnDone(); // Do not continue.
+ } else {
+ BrowserThread::PostTaskAndReplyWithResult(
+ BrowserThread::DB, FROM_HERE,
+ base::Bind(&PrecacheDatabase::GetUnfinishedWork,
+ base::Unretained(precache_database_.get())),
+ base::Bind(&PrecacheManager::OnGetUnfinishedWorkDone, AsWeakPtr()));
+ }
+}
+
+void PrecacheManager::PrecacheIfCacheIsBigEnough(
+ net::URLRequestContextGetter* url_request_context_getter) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+ CHECK(url_request_context_getter);
+
+ // Continue with OnGetUnfinishedWorkDone only if the size of the cache is
+ // 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::URLRequestContext* context =
+ url_request_context_getter->GetURLRequestContext();
+ if (!context) {
+ OnCacheSizeReceived(0);
+ return;
+ }
+ net::HttpTransactionFactory* factory = context->http_transaction_factory();
+ if (!factory) {
+ OnCacheSizeReceived(0);
+ return;
+ }
+ net::HttpCache* cache = factory->GetCache();
+ if (cache) {
+ const int net_error_code = cache->GetBackend(
+ &cache_backend_,
+ base::Bind(&PrecacheManager::OnCacheBackendReceived, AsWeakPtr()));
+ 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.
+ OnCacheSizeReceived(0);
+ }
+}
+
void PrecacheManager::StartPrecaching(
const PrecacheCompletionCallback& precache_completion_callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
@@ -128,12 +216,18 @@ 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()));
+
+ net::URLRequestContextGetter* url_request_context_getter =
+ content::BrowserContext::GetDefaultStoragePartition(browser_context_)
+ ->GetURLRequestContext();
+ if (url_request_context_getter) {
+ BrowserThread::PostTask(
+ BrowserThread::IO, FROM_HERE,
+ base::Bind(&PrecacheManager::PrecacheIfCacheIsBigEnough, AsWeakPtr(),
+ base::Unretained(url_request_context_getter)));
twifkak 2016/11/19 02:15:06 Actually, on further thought, use scoped_refptr<>
jamartin 2016/11/19 02:54:22 Done.
+ } else { // !url_request_context_getter_.
+ OnCacheSizeReceivedInUIThread(0);
+ }
}
void PrecacheManager::OnGetUnfinishedWorkDone(
« no previous file with comments | « components/precache/content/precache_manager.h ('k') | components/precache/content/precache_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698