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

Side by Side 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 unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "components/precache/content/precache_manager.h" 5 #include "components/precache/content/precache_manager.h"
6 6
7 #include <string> 7 #include <string>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/command_line.h" 12 #include "base/command_line.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/metrics/field_trial.h" 14 #include "base/metrics/field_trial.h"
15 #include "base/metrics/histogram_macros.h"
15 #include "base/strings/string_util.h" 16 #include "base/strings/string_util.h"
16 #include "base/time/time.h" 17 #include "base/time/time.h"
17 #include "components/history/core/browser/history_service.h" 18 #include "components/history/core/browser/history_service.h"
18 #include "components/precache/core/precache_database.h" 19 #include "components/precache/core/precache_database.h"
19 #include "components/precache/core/precache_switches.h" 20 #include "components/precache/core/precache_switches.h"
20 #include "components/precache/core/proto/unfinished_work.pb.h" 21 #include "components/precache/core/proto/unfinished_work.pb.h"
21 #include "components/prefs/pref_service.h" 22 #include "components/prefs/pref_service.h"
22 #include "components/sync/driver/sync_service.h" 23 #include "components/sync/driver/sync_service.h"
23 #include "components/variations/metrics_util.h" 24 #include "components/variations/metrics_util.h"
24 #include "components/variations/variations_associated_data.h" 25 #include "components/variations/variations_associated_data.h"
25 #include "content/public/browser/browser_context.h" 26 #include "content/public/browser/browser_context.h"
26 #include "content/public/browser/browser_thread.h" 27 #include "content/public/browser/browser_thread.h"
27 #include "content/public/browser/storage_partition.h" 28 #include "content/public/browser/storage_partition.h"
28 #include "net/base/network_change_notifier.h" 29 #include "net/base/network_change_notifier.h"
30 #include "net/http/http_cache.h"
31 #include "net/url_request/url_request_context.h"
32 #include "net/url_request/url_request_context_getter.h"
29 33
30 using content::BrowserThread; 34 using content::BrowserThread;
31 35
32 namespace { 36 namespace {
33 37
34 const char kPrecacheFieldTrialName[] = "Precache"; 38 const char kPrecacheFieldTrialName[] = "Precache";
35 const char kPrecacheFieldTrialEnabledGroup[] = "Enabled"; 39 const char kPrecacheFieldTrialEnabledGroup[] = "Enabled";
36 const char kPrecacheFieldTrialControlGroup[] = "Control"; 40 const char kPrecacheFieldTrialControlGroup[] = "Control";
37 const char kConfigURLParam[] = "config_url"; 41 const char kConfigURLParam[] = "config_url";
38 const char kManifestURLPrefixParam[] = "manifest_url_prefix"; 42 const char kManifestURLPrefixParam[] = "manifest_url_prefix";
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 108
105 // SyncService delegates to SyncPrefs, which must be called on the UI thread. 109 // SyncService delegates to SyncPrefs, which must be called on the UI thread.
106 if (history_service_ && 110 if (history_service_ &&
107 sync_service_->GetActiveDataTypes().Has(syncer::SESSIONS) && 111 sync_service_->GetActiveDataTypes().Has(syncer::SESSIONS) &&
108 !sync_service_->GetEncryptedDataTypes().Has(syncer::SESSIONS)) 112 !sync_service_->GetEncryptedDataTypes().Has(syncer::SESSIONS))
109 return AllowedType::ALLOWED; 113 return AllowedType::ALLOWED;
110 114
111 return AllowedType::DISALLOWED; 115 return AllowedType::DISALLOWED;
112 } 116 }
113 117
118 void PrecacheManager::OnCacheBackendReceived(int net_error_code) {
119 DCHECK_CURRENTLY_ON(BrowserThread::IO);
120 if (net_error_code == net::OK) {
121 DCHECK(cache_backend_);
122 int result = cache_backend_->CalculateSizeOfAllEntries(base::Bind(
123 &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.
124 if (result == net::ERR_IO_PENDING) {
125 // Wait for the callback.
126 } else if (result >= 0) {
127 // The result is the expected bytes already.
128 OnCacheSizeReceived(result);
129 } else {
130 // Error occurred. Couldn't get the size. Assume there is no cache.
131 OnCacheSizeReceived(0);
132 }
133 } else { // net_error_code != net::OK
134 // Assume there is no cache.
135 OnCacheSizeReceived(0);
136 }
137 cache_backend_ = nullptr;
138 }
139
140 void PrecacheManager::OnCacheSizeReceived(int cache_size_bytes) {
141 // We can be either in IO or UI threads.
142
143 UMA_HISTOGRAM_MEMORY_KB("Precache.CacheSize.AllEntries",
144 cache_size_bytes / 1024);
145 if (cache_size_bytes < min_cache_size_bytes_) {
146 BrowserThread::PostTask(
147 BrowserThread::UI, FROM_HERE,
148 base::Bind(&PrecacheManager::OnDone, base::AsWeakPtr(this)));
149 } else {
150 BrowserThread::PostTaskAndReplyWithResult(
151 BrowserThread::DB, FROM_HERE,
152 base::Bind(&PrecacheDatabase::GetUnfinishedWork,
153 base::Unretained(precache_database_.get())),
154 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.
155 }
156 }
157
158 void PrecacheManager::PrecacheIfCacheIsBigEnough() {
159 DCHECK_CURRENTLY_ON(BrowserThread::IO);
160 CHECK(url_request_context_getter_);
161
162 // Continue with OnGetUnfinishedWorkDone only if the size of the cache is
163 // at least min_cache_size_bytes_.
164 // Class disk_cache::Backend does not expose its maximum size. However, caches
165 // are usually full, so we can use the size of all the entries stored in the
166 // cache (via CalculateSizeOfAllEntries) as a proxy of its maximum size.
167 net::URLRequestContext* context =
168 url_request_context_getter_->GetURLRequestContext();
169 url_request_context_getter_ = nullptr;
170 if (!context) {
171 OnCacheSizeReceived(0);
172 return;
173 }
174 net::HttpTransactionFactory* factory = context->http_transaction_factory();
175 if (!factory) {
176 OnCacheSizeReceived(0);
177 return;
178 }
179 net::HttpCache* cache = factory->GetCache();
180 if (cache) {
181 const int net_error_code = cache->GetBackend(
182 &cache_backend_, base::Bind(&PrecacheManager::OnCacheBackendReceived,
183 base::AsWeakPtr(this)));
184 if (net_error_code != net::ERR_IO_PENDING) {
185 // No need to wait for the callback. The callback hasn't been called with
186 // the appropriate code, so we call it directly.
187 OnCacheBackendReceived(net_error_code);
188 }
189 } else { // !cache.
190 // There is no known cache. Assume that there is no cache.
191 OnCacheSizeReceived(0);
192 }
193 }
194
114 void PrecacheManager::StartPrecaching( 195 void PrecacheManager::StartPrecaching(
115 const PrecacheCompletionCallback& precache_completion_callback) { 196 const PrecacheCompletionCallback& precache_completion_callback) {
116 DCHECK_CURRENTLY_ON(BrowserThread::UI); 197 DCHECK_CURRENTLY_ON(BrowserThread::UI);
117 198
118 if (is_precaching_) { 199 if (is_precaching_) {
119 DLOG(WARNING) << "Cannot start precaching because precaching is already " 200 DLOG(WARNING) << "Cannot start precaching because precaching is already "
120 "in progress."; 201 "in progress.";
121 return; 202 return;
122 } 203 }
123 precache_completion_callback_ = precache_completion_callback; 204 precache_completion_callback_ = precache_completion_callback;
124 205
125 is_precaching_ = true; 206 is_precaching_ = true;
126 BrowserThread::PostTask( 207 BrowserThread::PostTask(
127 BrowserThread::DB, FROM_HERE, 208 BrowserThread::DB, FROM_HERE,
128 base::Bind(&PrecacheDatabase::SetLastPrecacheTimestamp, 209 base::Bind(&PrecacheDatabase::SetLastPrecacheTimestamp,
129 base::Unretained(precache_database_.get()), 210 base::Unretained(precache_database_.get()),
130 base::Time::Now())); 211 base::Time::Now()));
131 BrowserThread::PostTaskAndReplyWithResult( 212
132 BrowserThread::DB, 213 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.
133 FROM_HERE, 214 content::BrowserContext::GetDefaultStoragePartition(browser_context_)
134 base::Bind(&PrecacheDatabase::GetUnfinishedWork, 215 ->GetURLRequestContext();
135 base::Unretained(precache_database_.get())), 216 if (url_request_context_getter_) {
136 base::Bind(&PrecacheManager::OnGetUnfinishedWorkDone, AsWeakPtr())); 217 BrowserThread::PostTask(
218 BrowserThread::IO, FROM_HERE,
219 base::Bind(&PrecacheManager::PrecacheIfCacheIsBigEnough,
220 base::AsWeakPtr(this)));
221 } else { // !url_request_context_getter_.
222 OnCacheSizeReceived(0);
223 }
137 } 224 }
138 225
139 void PrecacheManager::OnGetUnfinishedWorkDone( 226 void PrecacheManager::OnGetUnfinishedWorkDone(
140 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work) { 227 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work) {
141 // Reset progress on a prefetch that has taken too long to complete. 228 // Reset progress on a prefetch that has taken too long to complete.
142 if (unfinished_work->has_start_time() && 229 if (unfinished_work->has_start_time() &&
143 base::Time::Now() - 230 base::Time::Now() -
144 base::Time::FromInternalValue(unfinished_work->start_time()) > 231 base::Time::FromInternalValue(unfinished_work->start_time()) >
145 base::TimeDelta::FromHours(6)) { 232 base::TimeDelta::FromHours(6)) {
146 PrecacheFetcher::RecordCompletionStatistics( 233 PrecacheFetcher::RecordCompletionStatistics(
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
355 this)); 442 this));
356 precache_fetcher_->Start(); 443 precache_fetcher_->Start();
357 } 444 }
358 445
359 void PrecacheManager::OnHostsReceivedThenDone( 446 void PrecacheManager::OnHostsReceivedThenDone(
360 const history::TopHostsList& host_counts) { 447 const history::TopHostsList& host_counts) {
361 OnDone(); 448 OnDone();
362 } 449 }
363 450
364 } // namespace precache 451 } // namespace precache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698