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

Side by Side Diff: components/precache/content/precache_manager.cc

Issue 2507753003: Do not precache when the cache size is small (Closed)
Patch Set: Sync'ed to HEAD Created 4 years 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/memory/ref_counted.h"
14 #include "base/metrics/field_trial.h" 15 #include "base/metrics/field_trial.h"
16 #include "base/metrics/histogram_macros.h"
17 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_util.h" 18 #include "base/strings/string_util.h"
16 #include "base/time/time.h" 19 #include "base/time/time.h"
17 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_sett ings.h" 20 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_sett ings.h"
18 #include "components/history/core/browser/history_service.h" 21 #include "components/history/core/browser/history_service.h"
19 #include "components/precache/core/precache_database.h" 22 #include "components/precache/core/precache_database.h"
20 #include "components/precache/core/precache_switches.h" 23 #include "components/precache/core/precache_switches.h"
21 #include "components/precache/core/proto/unfinished_work.pb.h" 24 #include "components/precache/core/proto/unfinished_work.pb.h"
22 #include "components/prefs/pref_service.h" 25 #include "components/prefs/pref_service.h"
23 #include "components/sync/driver/sync_service.h" 26 #include "components/sync/driver/sync_service.h"
24 #include "components/variations/metrics_util.h" 27 #include "components/variations/metrics_util.h"
25 #include "components/variations/variations_associated_data.h" 28 #include "components/variations/variations_associated_data.h"
26 #include "content/public/browser/browser_context.h" 29 #include "content/public/browser/browser_context.h"
27 #include "content/public/browser/browser_thread.h" 30 #include "content/public/browser/browser_thread.h"
28 #include "content/public/browser/storage_partition.h" 31 #include "content/public/browser/storage_partition.h"
29 #include "net/base/network_change_notifier.h" 32 #include "net/base/network_change_notifier.h"
33 #include "net/http/http_cache.h"
34 #include "net/url_request/url_request_context.h"
35 #include "net/url_request/url_request_context_getter.h"
30 36
31 using content::BrowserThread; 37 using content::BrowserThread;
32 38
39 namespace precache {
40
41 const char kPrecacheFieldTrialName[] = "Precache";
42 const char kMinCacheSizeParam[] = "min_cache_size";
43
33 namespace { 44 namespace {
34 45
35 const char kPrecacheFieldTrialName[] = "Precache";
36 const char kPrecacheFieldTrialEnabledGroup[] = "Enabled"; 46 const char kPrecacheFieldTrialEnabledGroup[] = "Enabled";
37 const char kPrecacheFieldTrialControlGroup[] = "Control"; 47 const char kPrecacheFieldTrialControlGroup[] = "Control";
38 const char kConfigURLParam[] = "config_url"; 48 const char kConfigURLParam[] = "config_url";
39 const char kManifestURLPrefixParam[] = "manifest_url_prefix"; 49 const char kManifestURLPrefixParam[] = "manifest_url_prefix";
40 const char kDataReductionProxyParam[] = "disable_if_data_reduction_proxy"; 50 const char kDataReductionProxyParam[] = "disable_if_data_reduction_proxy";
41 const size_t kNumTopHosts = 100; 51 const size_t kNumTopHosts = 100;
42 52
43 } // namespace 53 } // namespace
44 54
45 namespace precache {
46
47 size_t NumTopHosts() { 55 size_t NumTopHosts() {
48 return kNumTopHosts; 56 return kNumTopHosts;
49 } 57 }
50 58
51 PrecacheManager::PrecacheManager( 59 PrecacheManager::PrecacheManager(
52 content::BrowserContext* browser_context, 60 content::BrowserContext* browser_context,
53 const syncer::SyncService* const sync_service, 61 const syncer::SyncService* const sync_service,
54 const history::HistoryService* const history_service, 62 const history::HistoryService* const history_service,
55 const data_reduction_proxy::DataReductionProxySettings* 63 const data_reduction_proxy::DataReductionProxySettings*
56 data_reduction_proxy_settings, 64 data_reduction_proxy_settings,
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 124
117 // SyncService delegates to SyncPrefs, which must be called on the UI thread. 125 // SyncService delegates to SyncPrefs, which must be called on the UI thread.
118 if (history_service_ && 126 if (history_service_ &&
119 sync_service_->GetActiveDataTypes().Has(syncer::SESSIONS) && 127 sync_service_->GetActiveDataTypes().Has(syncer::SESSIONS) &&
120 !sync_service_->GetEncryptedDataTypes().Has(syncer::SESSIONS)) 128 !sync_service_->GetEncryptedDataTypes().Has(syncer::SESSIONS))
121 return AllowedType::ALLOWED; 129 return AllowedType::ALLOWED;
122 130
123 return AllowedType::DISALLOWED; 131 return AllowedType::DISALLOWED;
124 } 132 }
125 133
134 void PrecacheManager::OnCacheBackendReceived(int net_error_code) {
135 DCHECK_CURRENTLY_ON(BrowserThread::IO);
136 if (net_error_code != net::OK) {
137 // Assume there is no cache.
138 cache_backend_ = nullptr;
139 OnCacheSizeReceived(0);
140 return;
141 }
142 DCHECK(cache_backend_);
143 int result = cache_backend_->CalculateSizeOfAllEntries(base::Bind(
144 &PrecacheManager::OnCacheSizeReceived, base::Unretained(this)));
145 if (result == net::ERR_IO_PENDING) {
146 // Wait for the callback.
147 } else if (result >= 0) {
148 // The result is the expected bytes already.
149 OnCacheSizeReceived(result);
150 } else {
151 // Error occurred. Couldn't get the size. Assume there is no cache.
152 OnCacheSizeReceived(0);
153 }
154 cache_backend_ = nullptr;
155 }
156
157 void PrecacheManager::OnCacheSizeReceived(int cache_size_bytes) {
158 DCHECK_CURRENTLY_ON(BrowserThread::IO);
159
160 BrowserThread::PostTask(
161 BrowserThread::UI, FROM_HERE,
162 base::Bind(&PrecacheManager::OnCacheSizeReceivedInUIThread,
163 base::Unretained(this), cache_size_bytes));
164 }
165
166 void PrecacheManager::OnCacheSizeReceivedInUIThread(int cache_size_bytes) {
167 DCHECK_CURRENTLY_ON(BrowserThread::UI);
168
169 UMA_HISTOGRAM_MEMORY_KB("Precache.CacheSize.AllEntries",
170 cache_size_bytes / 1024);
171
172 if (cache_size_bytes < min_cache_size_bytes_) {
173 OnDone(); // Do not continue.
174 } else {
175 BrowserThread::PostTaskAndReplyWithResult(
176 BrowserThread::DB, FROM_HERE,
177 base::Bind(&PrecacheDatabase::GetUnfinishedWork,
178 base::Unretained(precache_database_.get())),
179 base::Bind(&PrecacheManager::OnGetUnfinishedWorkDone, AsWeakPtr()));
180 }
181 }
182
183 void PrecacheManager::PrecacheIfCacheIsBigEnough(
184 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter) {
185 DCHECK_CURRENTLY_ON(BrowserThread::IO);
186 CHECK(url_request_context_getter);
187
188 // Continue with OnGetUnfinishedWorkDone only if the size of the cache is
189 // at least min_cache_size_bytes_.
190 // Class disk_cache::Backend does not expose its maximum size. However, caches
191 // are usually full, so we can use the size of all the entries stored in the
192 // cache (via CalculateSizeOfAllEntries) as a proxy of its maximum size.
193 net::URLRequestContext* context =
194 url_request_context_getter->GetURLRequestContext();
195 if (!context) {
196 OnCacheSizeReceived(0);
197 return;
198 }
199 net::HttpTransactionFactory* factory = context->http_transaction_factory();
200 if (!factory) {
201 OnCacheSizeReceived(0);
202 return;
203 }
204 net::HttpCache* cache = factory->GetCache();
205 if (!cache) {
206 // There is no known cache. Assume that there is no cache.
207 // TODO(jamartin): I'm not sure this can be an actual posibility. Consider
208 // making this a CHECK(cache).
209 OnCacheSizeReceived(0);
210 return;
211 }
212 const int net_error_code = cache->GetBackend(
213 &cache_backend_, base::Bind(&PrecacheManager::OnCacheBackendReceived,
214 base::Unretained(this)));
215 if (net_error_code != net::ERR_IO_PENDING) {
216 // No need to wait for the callback. The callback hasn't been called with
217 // the appropriate code, so we call it directly.
218 OnCacheBackendReceived(net_error_code);
219 }
220 }
221
126 void PrecacheManager::StartPrecaching( 222 void PrecacheManager::StartPrecaching(
127 const PrecacheCompletionCallback& precache_completion_callback) { 223 const PrecacheCompletionCallback& precache_completion_callback) {
128 DCHECK_CURRENTLY_ON(BrowserThread::UI); 224 DCHECK_CURRENTLY_ON(BrowserThread::UI);
129 225
130 if (is_precaching_) { 226 if (is_precaching_) {
131 DLOG(WARNING) << "Cannot start precaching because precaching is already " 227 DLOG(WARNING) << "Cannot start precaching because precaching is already "
132 "in progress."; 228 "in progress.";
133 return; 229 return;
134 } 230 }
135 precache_completion_callback_ = precache_completion_callback; 231 precache_completion_callback_ = precache_completion_callback;
136 232
137 is_precaching_ = true; 233 is_precaching_ = true;
138 BrowserThread::PostTask( 234 BrowserThread::PostTask(
139 BrowserThread::DB, FROM_HERE, 235 BrowserThread::DB, FROM_HERE,
140 base::Bind(&PrecacheDatabase::SetLastPrecacheTimestamp, 236 base::Bind(&PrecacheDatabase::SetLastPrecacheTimestamp,
141 base::Unretained(precache_database_.get()), 237 base::Unretained(precache_database_.get()),
142 base::Time::Now())); 238 base::Time::Now()));
143 BrowserThread::PostTaskAndReplyWithResult( 239
144 BrowserThread::DB, 240 // Ignore boolean return value. In all documented failure cases, it sets the
145 FROM_HERE, 241 // int to a reasonable value.
146 base::Bind(&PrecacheDatabase::GetUnfinishedWork, 242 base::StringToInt(variations::GetVariationParamValue(kPrecacheFieldTrialName,
147 base::Unretained(precache_database_.get())), 243 kMinCacheSizeParam),
148 base::Bind(&PrecacheManager::OnGetUnfinishedWorkDone, AsWeakPtr())); 244 &min_cache_size_bytes_);
245 if (min_cache_size_bytes_ <= 0) {
246 // Skip looking up the cache size, because it doesn't matter.
247 OnCacheSizeReceivedInUIThread(0);
248 return;
249 }
250
251 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter(
252 content::BrowserContext::GetDefaultStoragePartition(browser_context_)
253 ->GetURLRequestContext());
254 if (!url_request_context_getter) {
255 OnCacheSizeReceivedInUIThread(0);
256 return;
257 }
258
259 BrowserThread::PostTask(
260 BrowserThread::IO, FROM_HERE,
261 base::Bind(&PrecacheManager::PrecacheIfCacheIsBigEnough, AsWeakPtr(),
262 std::move(url_request_context_getter)));
149 } 263 }
150 264
151 void PrecacheManager::OnGetUnfinishedWorkDone( 265 void PrecacheManager::OnGetUnfinishedWorkDone(
152 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work) { 266 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work) {
153 // Reset progress on a prefetch that has taken too long to complete. 267 // Reset progress on a prefetch that has taken too long to complete.
154 if (unfinished_work->has_start_time() && 268 if (unfinished_work->has_start_time() &&
155 base::Time::Now() - 269 base::Time::Now() -
156 base::Time::FromInternalValue(unfinished_work->start_time()) > 270 base::Time::FromInternalValue(unfinished_work->start_time()) >
157 base::TimeDelta::FromHours(6)) { 271 base::TimeDelta::FromHours(6)) {
158 PrecacheFetcher::RecordCompletionStatistics( 272 PrecacheFetcher::RecordCompletionStatistics(
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 this)); 481 this));
368 precache_fetcher_->Start(); 482 precache_fetcher_->Start();
369 } 483 }
370 484
371 void PrecacheManager::OnHostsReceivedThenDone( 485 void PrecacheManager::OnHostsReceivedThenDone(
372 const history::TopHostsList& host_counts) { 486 const history::TopHostsList& host_counts) {
373 OnDone(); 487 OnDone();
374 } 488 }
375 489
376 } // namespace precache 490 } // namespace precache
OLDNEW
« 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