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

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

Issue 2507753003: Do not precache when the cache size is small (Closed)
Patch Set: Use IO thread and report UMA on the cache size 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..81a9514e2916e04f17ce98e77ab3487fdf78905c 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,83 @@ 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, base::AsWeakPtr(this)));
twifkak 2016/11/18 20:27:37 s/base::AsWeakPtr(this)/AsWeakPtr()/. Provided by
jamartin 2016/11/19 02:00:13 Done.
+ 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) {
+ // We can be either in IO or UI threads.
+
+ UMA_HISTOGRAM_MEMORY_KB("Precache.CacheSize.AllEntries",
+ cache_size_bytes / 1024);
+ if (cache_size_bytes < min_cache_size_bytes_) {
+ BrowserThread::PostTask(
+ BrowserThread::UI, FROM_HERE,
+ base::Bind(&PrecacheManager::OnDone, base::AsWeakPtr(this)));
+ } else {
+ BrowserThread::PostTaskAndReplyWithResult(
+ BrowserThread::DB, FROM_HERE,
+ base::Bind(&PrecacheDatabase::GetUnfinishedWork,
+ base::Unretained(precache_database_.get())),
+ base::Bind(&PrecacheManager::OnGetUnfinishedWorkDone, AsWeakPtr()));
twifkak 2016/11/18 20:27:37 AFAICT, this means OnGetUnfinishedWorkDone may be
jamartin 2016/11/19 02:00:13 Good catch.
+ }
+}
+
+void PrecacheManager::PrecacheIfCacheIsBigEnough() {
+ 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();
+ url_request_context_getter_ = nullptr;
+ 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,
+ base::AsWeakPtr(this)));
+ 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 +209,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()));
+
+ url_request_context_getter_ =
twifkak 2016/11/18 20:27:37 Why is this an instance var? Pass via base::Bind(.
jamartin 2016/11/19 02:00:13 Way better. Thanks.
+ content::BrowserContext::GetDefaultStoragePartition(browser_context_)
+ ->GetURLRequestContext();
+ if (url_request_context_getter_) {
+ BrowserThread::PostTask(
+ BrowserThread::IO, FROM_HERE,
+ base::Bind(&PrecacheManager::PrecacheIfCacheIsBigEnough,
+ base::AsWeakPtr(this)));
+ } else { // !url_request_context_getter_.
+ OnCacheSizeReceived(0);
+ }
}
void PrecacheManager::OnGetUnfinishedWorkDone(

Powered by Google App Engine
This is Rietveld 408576698